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.php

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

283 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class FV_Player_Media_Browser {
4
5 public $ajax_action_name = false;
6 public $ajax_action_name_add_new_folder = false;
7 private $s3_assets_loaded = false;
8
9 public function __construct($args) {
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 // load base JS
16 add_action( 'edit_form_after_editor', array($this, 'init_base'), 1 ); // for old WP editor
17 add_action( 'enqueue_block_editor_assets', array($this, 'init_base') ); // for Gutenberg
18 add_action( 'admin_print_scripts-toplevel_page_fv_player', array($this, 'init_base'), 0 ); // wp-admin -> FV Player
19 add_action( 'admin_print_scripts-widgets.php', array($this, 'init_base'), 0 ); // wp-admin -> Widgets
20
21 add_action( 'fvplayer_editor_load', array($this, 'init_base'), 0 ); // Front-end editor
22
23 // TODO: Video encoder class should take care of this
24 add_action( 'admin_print_scripts-fv-player_page_fv_player_coconut', array($this, 'init_base'), 0 ); // wp-admin -> FV Player -> Coconut Jobs
25 add_action( 'admin_print_scripts-fv-player_page_fv_player_bunny_stream', array($this, 'init_base'), 0 ); // wp-admin -> FV Player -> Bunny Stream Jobs
26 add_action( 'fv_player_media_browser_enqueue_base_uploader_css', array( $this, 'include_base_uploader_css' ) );
27
28 if( is_array($args) ) {
29 $args = wp_parse_args($args ,array(
30 'ajax_action_name' => false,
31 'ajax_action_name_add_new_folder' => false,
32 )
33 );
34
35 $this->ajax_action_name = $args['ajax_action_name'];
36 $this->ajax_action_name_add_new_folder = $args['ajax_action_name_add_new_folder'];
37
38 } else {
39 // register extending class WP AJAX action
40 $this->ajax_action_name = $args;
41 }
42
43 $this->register();
44 }
45
46 abstract function init();
47
48 // TODO: should be abstract
49 public function add_folder_ajax() {}
50
51 // TOTO: should be abstract
52 function decode_link_components( $link ) {}
53
54 // TOTO: should be abstract
55 function get_custom_domain_url( $link, $bucket, $custom_domain ) {}
56
57 function init_base() {
58 global $fv_wp_flowplayer_ver;
59 wp_enqueue_media();
60 wp_enqueue_script( 'flowplayer-browser-base', flowplayer::get_plugin_url().'/js/media-library-browser-base.js', array('jquery'), filemtime( dirname( __FILE__ ) . '/../js/media-library-browser-base.js' ), true );
61 wp_enqueue_style('fvwpflowplayer-s3-browser', flowplayer::get_plugin_url().'/css/s3-browser.css','',$fv_wp_flowplayer_ver);
62 wp_localize_script('flowplayer-browser-base', 'fv_flowplayer_browser', array(
63 'ajaxurl' => flowplayer::get_plugin_url().'/controller/s3-ajax.php',
64 )
65 );
66 $this->init();
67 }
68
69 function init_for_gutenberg_base() {
70 add_action( 'admin_footer', array($this, 'init_base'), 1 );
71 }
72
73 function register() {
74 add_action( $this->ajax_action_name, array($this, 'load_assets') );
75 }
76
77 function include_aws_sdk() {
78 if ( ! class_exists( 'Aws\S3\S3Client' ) ) {
79 if ( file_exists( dirname( __FILE__ ) . "/../vendor/autoload.php" ) ) {
80
81 // Setup a custom handler for (fatal) errors that might be thrown by the AWS SDK.
82 set_error_handler(
83 function( $errno, $errstr ) {
84 throw new Exception( $errstr );
85 },
86 E_USER_ERROR
87 );
88
89 // Avoid any output from the AWS SDK autoloading.
90 ob_start();
91 try {
92 require_once( dirname( __FILE__ ) . "/../vendor/autoload.php" );
93
94 } catch ( Exception $e ) {
95 // Throw away any output from the AWS SDK autoloading.
96 if ( ob_get_level() ) {
97 ob_end_clean();
98 }
99
100 // The composer might want to send HTTP 500, so we send HTTP 200 instead to be able to handle it in JavaScript.
101 http_response_code( 200 );
102
103 // Send the error message to the JavaScript.
104 wp_send_json( array( 'err' => $e->getMessage() ) );
105 exit;
106
107 } finally {
108 // Throw away any output from the AWS SDK autoloading.
109 if ( ob_get_level() ) {
110 ob_end_clean();
111 }
112
113 // Restore the original PHP error handler.
114 restore_error_handler();
115 }
116
117 } else {
118 wp_send_json( array( 'err' => 'AWS SDK not found. please make sure you run <code>composer install --no-dev</code> in FV Player plugin folder or install plugin from WordPress.org.' ) );
119 exit;
120 }
121 }
122 }
123
124 function include_base_uploader_css() {
125 wp_enqueue_style( 'fv-player-s3-uploader-css', flowplayer::get_plugin_url() . '/css/s3-uploader.css', array(), filemtime( dirname(__FILE__).'/../css/s3-uploader.css' ) );
126 }
127
128 function include_s3_upload_assets() {
129 if ( $this->s3_assets_loaded ) {
130 return;
131 }
132
133 global $fv_wp_flowplayer_ver;
134
135 wp_enqueue_script( 'fv-player-s3-uploader', flowplayer::get_plugin_url().'/js/s3upload.js', array( 'flowplayer-browser-base' ), $fv_wp_flowplayer_ver );
136 wp_enqueue_script( 'fv-player-s3-uploader-base', flowplayer::get_plugin_url().'/js/s3-upload-base.js', array( 'flowplayer-browser-base' ), $fv_wp_flowplayer_ver );
137 wp_localize_script( 'fv-player-s3-uploader', 'fv_player_s3_uploader', array(
138 'validate_file_nonce' => wp_create_nonce( 'fv_flowplayer_validate_file' ),
139 )
140 );
141
142 $this->include_base_uploader_css();
143
144 $this->s3_assets_loaded = true;
145 }
146
147 function get_formatted_assets_data() {
148 return json_decode('{"items":{"name":"Home","type":"folder","path":"Home\/","items":[{"name":"01 The Beginning.mp3","size":2117536,"type":"file","path":"Home\/01 The Beginning.mp3","link":"http:\/\/sjdua7x04ygyx.cloudfront.net\/01%20The%20Beginning.mp3"},{"name":"Fender_Bass_Guitar_Patent.jpg","size":495756,"type":"file","path":"Home\/Fender_Bass_Guitar_Patent.jpg","link":"http:\/\/sjdua7x04ygyx.cloudfront.net\/Fender_Bass_Guitar_Patent.jpg"}]}}', true);
149 }
150
151 function get_output() {
152 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
153 return array(
154 'items' => array(),
155 'name' => '/',
156 'path' => '/',
157 'type' => 'folder',
158 'err' => 'Invalid nonce'
159 );
160 }
161
162 $output = array(
163 'name' => 'Home',
164 'type' => 'folder',
165 'path' => !empty($_POST['path']) ? sanitize_text_field( $_POST['path'] ) : 'Home/',
166 'items' => array()
167 );
168
169 return $output;
170 }
171
172 function get_metadata( $s3Client, $bucket ) {
173 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
174 return array(
175 'items' => array(),
176 'name' => '/',
177 'path' => '/',
178 'type' => 'folder',
179 'err' => 'Invalid nonce'
180 );
181 }
182
183 $args = array(
184 'Bucket' => $bucket,
185 'Delimiter' => '/',
186 );
187
188 $request_path = !empty($_POST['path']) ? str_replace( 'Home/', '', sanitize_text_field( $_POST['path'] ) ) : false;
189
190 if( $request_path ) {
191 $args['Prefix'] = $request_path;
192 }
193
194 $paged = $s3Client->getPaginator('ListObjects',$args);
195
196 $date_format = get_option( 'date_format' );
197
198 return array( $request_path, $paged, $date_format );
199 }
200
201 function get_output_items( $output, $s3Client, $request_path, $paged, $date_format, $bucket, $sum_up = NULL, $custom_domain = NULL ) {
202 foreach( $paged AS $res ) {
203
204 $folders = !empty($res['CommonPrefixes']) ? $res['CommonPrefixes'] : array();
205 $files = $res->get('Contents');
206 if( !$files ) $files = array();
207
208 $objects = array_merge( $folders, $files );
209
210 foreach ( $objects as $object ) {
211 if ( ! isset( $objectarray ) ) {
212 $objectarray = array();
213 }
214
215 $item = array();
216
217 $path = ! empty( $object['Prefix'] ) ? $object['Prefix'] : $object['Key'];
218
219 if( isset($sum_up) && !empty($object['Key']) && preg_match( '~\.ts$~', $object['Key'] ) ) {
220 if( empty($sum_up['ts']) ) $sum_up['ts'] = 0;
221 $sum_up['ts']++;
222 continue;
223 }
224
225 $item['path'] = 'Home/' . $path;
226
227 if( $request_path ) {
228 if( $request_path == $path ) continue; // sometimes the current folder is present in the response, weird
229
230 $item['name'] = str_replace( $request_path, '', $path );
231 } else {
232 $item['name'] = $path;
233 }
234
235 if( !empty($object['Size']) ) {
236 $item['type'] = 'file';
237 $item['size'] = $object['Size'];
238 $item['modified'] = gmdate($date_format, strtotime($object['LastModified']));
239
240 if( isset($object['LastModified']) ) {
241 $item['LastModified'] = $object['LastModified'];
242 }
243
244 $link = (string) $s3Client->getObjectUrl( $bucket, $path );
245
246 $link = $this->decode_link_components( $link );
247
248 // replace link with CloudFront URL, if we have one
249 if( !empty($custom_domain) ) {
250 $link = $this->get_custom_domain_url($link ,$bucket, $custom_domain);
251 }
252
253 $item['link'] = $link;
254
255 if (preg_match('/\.(jpg|jpeg|png|gif)$/i', $item['name'])) {
256 $item['splash'] = htmlspecialchars( apply_filters('fv_flowplayer_splash', $link ) );
257 }
258 } else {
259 $item['type'] = 'folder';
260 $item['items'] = array();
261 }
262
263 $output['items'][] = $item;
264
265 if (strtolower(substr($item['name'], strrpos($item['name'], '.') + 1)) === 'ts') {
266 continue;
267 }
268
269 }
270 }
271
272 return array( $output, $sum_up );
273 }
274
275 function load_assets() {
276 $json_final = $this->get_formatted_assets_data();
277
278 wp_send_json( $json_final );
279 wp_die();
280 }
281
282 }
283