PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / media-browser-s3.php

media-browser-s3.php in FV Player 8 trunk, at models/media-browser-s3.php

237 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class FV_Player_Media_Browser_S3 extends FV_Player_Media_Browser {
8
9 function init() {
10 global $fv_fp, $fv_wp_flowplayer_ver;
11 if ($fv_fp->_get_option('s3_browser')) {
12 wp_enqueue_script( 'flowplayer-aws-s3', flowplayer::get_plugin_url().'/js/s3-browser.js', array('flowplayer-browser-base'), $fv_wp_flowplayer_ver, true );
13 wp_localize_script( 'flowplayer-aws-s3', 'flowplayer_aws_s3', array(
14 'nonce' => wp_create_nonce( $this->ajax_action_name ),
15 )
16 );
17 }
18 }
19
20 function decode_link_components( $link ) {
21 $link = str_replace( '%20', '+', $link );
22
23 return $link;
24 }
25
26 function get_custom_domain_url( $link, $bucket, $custom_domain ) {
27 // replace S3 URLs with buckets in the S3 subdomain, like https://fv-flowplayer-cloudfront.s3-us-west-2.amazonaws.com/video.mp4
28 $link = preg_replace('/https?:\/\/' . $bucket . '\.s3[^.]*\.amazonaws\.com\/(.*)/i', rtrim($custom_domain, '/').'/$1', $link);
29
30 // replace S3 URLs with buckets in the S3 subdomain, like https://fv-flowplayer-cloudfront.s3.us-west-2.amazonaws.com/video.mp4
31 $link = preg_replace('/https?:\/\/' . $bucket . '\.s3\.[^.]*\.amazonaws\.com\/(.*)/i', rtrim($custom_domain, '/').'/$1', $link);
32
33 // replace S3 URLs with bucket name as a subfolder
34 $link = preg_replace('/https?:\/\/[^\/]+\/' . $bucket . '\/(.*)/i', rtrim($custom_domain, '/').'/$1', $link);
35
36 return $link;
37 }
38
39 function get_formatted_assets_data() {
40 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
41 return array(
42 'items' => array(),
43 'name' => '/',
44 'path' => '/',
45 'type' => 'folder',
46 'err' => 'Invalid nonce'
47 );
48 }
49
50 $this->include_aws_sdk();
51 global $fv_fp, $s3Client;
52
53 $regions = $fv_fp->_get_option('amazon_region');
54 $secrets = $fv_fp->_get_option('amazon_secret');
55 $keys = $fv_fp->_get_option('amazon_key');
56 $buckets = $fv_fp->_get_option('amazon_bucket');
57 $region_names = fv_player_get_aws_regions();
58 $domains = array();
59
60 if (isset($_POST['bucket']) && isset($buckets[$_POST['bucket']])) {
61 $array_id = absint( $_POST['bucket'] );
62 } else {
63 $array_id = 0;
64 }
65
66 // remove all buckets with missing name
67 $keep_checking = true;
68 while ($keep_checking) {
69 // break here if there are no buckets left
70 if (!count($buckets)) {
71 break;
72 } else {
73 // remove any bucket without a name
74 $all_ok = true;
75 foreach ($buckets as $bucket_id => $bucket_name) {
76 if (!$bucket_name) {
77 unset($buckets[$bucket_id], $regions[$bucket_id], $secrets[$bucket_id], $keys[$bucket_id]);
78
79 // adjust the selected bucket to the first array ID if we just
80 // removed the one we chose to display
81 if ($array_id == $bucket_id) {
82 reset($buckets);
83 $array_id = key($buckets);
84 }
85
86 $all_ok = false;
87 break;
88 }
89 }
90
91 // all buckets have names, we can stop the check now
92 if ($all_ok) {
93 $keep_checking = false;
94 }
95 }
96 }
97
98 // if the selected bucket is a region-less one, change the $array_id variable
99 // to one that has a region
100 $regioned_bucket_found = (count($buckets) ? true : false);
101 if (!$regions[$array_id]) {
102 $regioned_bucket_found = false;
103 foreach ($buckets as $bucket_id => $unused) {
104 if ($regions[$bucket_id]) {
105 $array_id = $bucket_id;
106 $regioned_bucket_found = true;
107 }
108 }
109 }
110
111 if ($regioned_bucket_found) {
112
113 $output = $this->get_output();
114
115 $region = $regions[ $array_id ];
116 $secret = $secrets[ $array_id ];
117 $key = $keys[ $array_id ];
118 $bucket = $buckets[ $array_id ];
119
120 $credentials = new Aws\Credentials\Credentials( $key, $secret );
121
122 try {
123 $cloudfronts = get_transient('fv_player_s3_browser_cf');
124 if( !$cloudfronts ) {
125 $cfClient = Aws\CloudFront\CloudFrontClient::factory( array(
126 'credentials' => $credentials,
127 'region' => 'us-east-1',
128 'version' => 'latest',
129 'use_aws_shared_config_files' => false
130 ) );
131
132 $cloudfronts = $cfClient->listDistributions();
133 if( !empty($cloudfronts['DistributionList']['Items']) ) {
134 set_transient('fv_player_s3_browser_cf',$cloudfronts,60);
135 }
136 }
137
138 foreach( $cloudfronts['DistributionList']['Items'] AS $item ) {
139 if( !$item['Enabled'] ) continue;
140
141 $cf_domain = $item['DomainName'];
142 if( !empty($item['Aliases']) && !empty($item['Aliases']['Items']) && !empty($item['Aliases']['Items'][0]) ) {
143 $cf_domain = $item['Aliases']['Items'][0];
144 }
145 $origin = false;
146 if( !empty($item['Origins']) && !empty($item['Origins']['Items']) && !empty($item['Origins']['Items'][0]) && !empty($item['Origins']['Items'][0]['DomainName']) ) {
147 $origin = $item['Origins']['Items'][0]['DomainName'];
148 }
149
150 foreach( $buckets as $bucket_id => $bucket_name ) {
151 if( !empty($regions) && !empty($regions[$bucket_id]) && $bucket_name.'.s3.'.$regions[$bucket_id].'.amazonaws.com' == $origin ) {
152 $domains[$bucket_id] = 'https://'.$cf_domain; // todo: check if SSL is enabled for custom domains!
153 } else if( $bucket_name.'.s3.amazonaws.com' == $origin ) {
154 $domains[$bucket_id] = 'https://'.$cf_domain; // todo: check if SSL is enabled for custom domains!
155 }
156 }
157
158 }
159
160 } catch ( Aws\CloudFront\Exception\CloudFrontException $e ) {
161 $err = 'It appears that the policy of AWS IAM user identified by '.$key.' doesn\'t permit List and Read operations for the CloudFront service. Please add CloudFrontReadOnlyAccess policy for the user if you are using CloudFront for your S3 buckets. Otherwise you won\'t be getting the proper CloudFront links for your videos.';
162 }
163
164 // instantiate the S3 client with AWS credentials
165 $s3Client = Aws\S3\S3Client::factory( array(
166 'credentials' => $credentials,
167 'region' => $region,
168 'version' => 'latest',
169 'use_aws_shared_config_files' => false
170 ) );
171
172 try {
173
174 list( $request_path, $paged, $date_format ) = $this->get_metadata( $s3Client, $bucket );
175
176 list( $output, $sum_up ) = $this->get_output_items( $output, $s3Client, $request_path, $paged, $date_format, $bucket, array(), $domains[$array_id] );
177
178 foreach( $sum_up AS $ext => $count ) {
179 $output['items'][] = array(
180 'name' => '*.ts',
181 'link' => '',
182 'size' => $count.' .'.$ext.' files hidden',
183 'type' => 'placeholder'
184 );
185 }
186
187 } catch ( Aws\S3\Exception\S3Exception $e ) {
188 //echo $e->getMessage() . "\n";
189 $err = $e->getMessage();
190 $output = array(
191 'items' => array(),
192 'name' => '/',
193 'path' => '/',
194 'type' => 'folder'
195 );
196 }
197 }
198
199 // prepare list of buckets for the selection dropdown
200 $buckets_output = array();
201 $negative_ids = -1;
202 foreach ( $buckets as $bucket_index => $bucket_name ) {
203 $has_all_data = ($regions[ $bucket_index ] && $keys[ $bucket_index ] && $secrets[ $bucket_index ]);
204 $buckets_output[] = array(
205 'id' => ($has_all_data ? $bucket_index : $negative_ids--),
206 'name' => $bucket_name . ' (' . ($regions[ $bucket_index ] ? $regions[ $bucket_index ] : translate('no region', 'fv-wordpress-flowplayer')) . ')' . (!empty($domains[$bucket_index]) ? ' - '. $domains[$bucket_index] : '')
207 );
208 }
209
210 $json_final = array(
211 'buckets' => $buckets_output,
212 'region_names' => $region_names,
213 'active_bucket_id' => $array_id,
214 'items' => (
215 $regioned_bucket_found ?
216 $output :
217 array(
218 'items' => array(),
219 'name' => '/',
220 'path' => '/',
221 'type' => 'folder'
222 )
223 )
224 );
225
226 if (isset($err) && $err) {
227 $json_final['err'] = $err;
228 }
229
230 return $json_final;
231 }
232
233 }
234
235 global $FV_Player_Media_Browser_S3;
236 $FV_Player_Media_Browser_S3 = new FV_Player_Media_Browser_S3('wp_ajax_load_s3_assets');
237