PluginProbe
FV Player 8 / 8.1
FV Player 8 v8.1
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 / cdn.class.php

cdn.class.php in FV Player 8 8.1, at models/cdn.class.php

199 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( !class_exists('FV_Player_CDN') ) :
4
5 abstract class FV_Player_CDN {
6
7 var $aDomains;
8
9 var $aSecureTokens;
10
11 var $key = false;
12
13 var $title = false;
14
15 function __construct( $args ) {
16 if( !empty($args['key']) ) $this->key = $args['key'];
17 if( !empty($args['title']) ) $this->title = $args['title'];
18
19 if( !$this->aDomains && !$this->aSecureTokens ) {
20 // we use priority 21 to make sure it's after any FV_Player_Pro_Ajax_Loader::register_meta_boxes()
21 add_action( 'admin_init', array( $this, 'register_meta_boxes' ), 21 );
22 }
23 add_filter( 'plugins_loaded', array( $this, 'load_options' ), 8 );
24 add_action( 'fv_player_shortinit_loaded', array( $this, 'load_options' ) );
25 }
26
27
28 /*
29 * Used by FV Player Pro
30 */
31 function ajax() {
32 if( isset($_POST['action']) && sanitize_key( $_POST['action'] ) == 'fv_fp_get_video_url' ) {
33 $bFound = false;
34 foreach( $this->aDomains AS $i => $sDomains ) {
35 $aDomains = explode(',',$sDomains);
36 foreach( $aDomains AS $sDomain ) {
37 foreach( $_POST['sources'] AS $key => $aVideo ) {
38 if( !isset($aVideo['src']) || !isset($aVideo['type']) ) continue;
39
40 if( stripos($aVideo['src'],$sDomain) !== false ) {
41 $bFound = true;
42 $aVideo['src'] = $this->secure_link($aVideo['src'],$this->aSecureTokens[$i]);
43 $_POST['sources'][$key] = $aVideo;
44 }
45 }
46 }
47 }
48
49 if( $bFound ) {
50 echo '<FVFLOWPLAYER>';
51 // TODO: Sanitize
52 echo wp_json_encode($_POST['sources']);
53 echo '</FVFLOWPLAYER>';
54 die();
55 }
56 }
57
58 }
59
60
61 function args( $args ) {
62 // add the query arg you use in URL into this array
63 return $args;
64 }
65
66
67 function domains( $aDomains ) {
68 foreach( $this->aDomains AS $sDomains ) {
69 $aTemp = explode(',',$sDomains);
70 foreach( $aTemp AS $sDomain ) {
71 if( $sDomain ) $aDomains[] = $sDomain;
72 }
73 }
74
75 return $aDomains;
76 }
77
78
79 function get_signed_url( $url, $args, $ttl = false ) {
80 // either there is no FV_Player_Pro_Ajax_Loader from FV Player Pro or it's a back-end request for signed file URL
81 if( !class_exists('FV_Player_Pro_Ajax_Loader') || is_array($args) && isset($args['dynamic']) && $args['dynamic'] ) {
82 $bFound = false;
83 foreach( $this->aDomains AS $i => $sDomains ) {
84 $aDomains = explode(',',$sDomains);
85 foreach( $aDomains AS $sDomain ) {
86 if( stripos($url,$sDomain) !== false ) {
87
88 // Use video duration as the TTL if not provided
89 if ( ! $ttl ) {
90 global $fv_fp, $post;
91 if( !empty($fv_fp) && method_exists($fv_fp,'current_video') && $fv_fp->current_video() ) {
92 $ttl = intval( $fv_fp->current_video()->getDuration() );
93 } else if( !empty($post) && !empty($post->ID) ) {
94 if( $sDuration = flowplayer::get_duration( $post->ID, $url, true ) ) {
95 $ttl = intval($sDuration);
96 }
97 }
98 }
99
100 $secureToken = ! empty( $this->aSecureTokens[ $i ] ) ? $this->aSecureTokens[ $i ] : false;
101
102 $url = $this->secure_link( $url, $secureToken ,$ttl );
103 }
104 }
105 }
106 }
107
108 return $url;
109 }
110
111
112 function get_signed_url_long( $url ) {
113 // 1 week is the maximum of AWS S3 signed URLs: https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html#PresignedUrl-Expiration
114 return $this->get_signed_url( $url, array( 'dynamic' => true ), WEEK_IN_SECONDS );
115 }
116
117
118 function get_domains() {
119 global $fv_fp;
120 if( isset($fv_fp->conf[$this->key]) && isset($fv_fp->conf[$this->key]['domain']) ) {
121 return array( $fv_fp->conf[$this->key]['domain'] );
122 }
123 return false;
124 }
125
126
127 function get_secure_tokens() {
128 global $fv_fp;
129 if( isset($fv_fp->conf[$this->key]) && isset($fv_fp->conf[$this->key]['secure_token']) ) {
130 return array( $fv_fp->conf[$this->key]['secure_token'] );
131 }
132 return false;
133 }
134
135
136 function load_options() {
137 global $fv_fp;
138 if( empty($fv_fp) ) return;
139
140 if( !$this->aDomains ) {
141 $this->aDomains = $this->get_domains();
142 }
143 if( !$this->aSecureTokens ) {
144 $this->aSecureTokens = $this->get_secure_tokens();
145 }
146
147 if( $this->aDomains && $this->aSecureTokens ) {
148 add_filter( 'fv_player_pro_video_ajaxify_domains', array( $this, 'domains'), 999, 2 );
149 add_filter( 'fv_player_pro_video_ajaxify_args', array( $this, 'args'), 999, 2 );
150
151 add_action( 'plugins_loaded', array( $this, 'ajax' ), 9 );
152
153 add_filter( 'fv_flowplayer_video_src', array( $this, 'get_signed_url'), 10, 2 );
154
155 add_filter( 'fv_flowplayer_splash', array( $this, 'get_signed_url_long') );
156 add_filter( 'fv_flowplayer_playlist_splash', array( $this, 'get_signed_url_long') );
157 add_filter( 'fv_flowplayer_resource', array( $this, 'get_signed_url_long') );
158 }
159 }
160
161
162 function options() {
163 global $fv_fp;
164 ?>
165 <table class="form-table2" style="margin: 5px; ">
166 <tr>
167 <td style="vertical-align:top"><label for="<?php echo esc_attr( $this->key ); ?>[domain]"><?php esc_html_e( 'Domain', 'fv-player' ); ?>:</label></td>
168 <td>
169 <input type="text" size="40" name="<?php echo esc_attr( $this->key ); ?>[domain]" id="<?php echo esc_attr( $this->key ); ?>[keycdn_domain]" value="<?php if( isset($fv_fp->conf[$this->key][$this->key.'_domain']) && strlen(trim($fv_fp->conf[$this->key]['domain'])) ) echo esc_attr( trim($fv_fp->conf[$this->key]['domain']) ); ?>" />
170 <p class="description"><?php esc_html_e( 'You can enter multiple domains separated by <code>,</code>.', 'fv-player' ); ?></p>
171 </td>
172 </tr>
173 <tr>
174 <td><label for="<?php echo esc_attr( $this->key ); ?>[secure_token]"><?php esc_html_e( 'Secure Token', 'fv-player' ); ?>:</label></td>
175 <td>
176 <input type="text" size="40" name="<?php echo esc_attr( $this->key ); ?>[secure_token]" id="<?php echo esc_attr( $this->key ); ?>[secure_token]" value="<?php if( isset($fv_fp->conf[$this->key][$this->key.'_secure_token']) && strlen(trim($fv_fp->conf[$this->key]['secure_token'])) ) echo esc_attr( trim($fv_fp->conf[$this->key]['secure_token']) ); ?>" />
177 </td>
178 </tr>
179 <tr>
180 <td colspan="4">
181 <a class="fv-wordpress-flowplayer-save button button-primary" href="#" style="margin-top: 2ex;"><?php esc_html_e( 'Save', 'fv-player' ); ?></a>
182 </td>
183 </tr>
184 </table>
185 <?php
186 }
187
188
189 function register_meta_boxes() {
190 add_meta_box( 'fv_player_'.$this->key, $this->title, array( $this, 'options' ), 'fv_flowplayer_settings_hosting', 'normal', 'low' );
191 }
192
193
194 abstract function secure_link( $url, $secret, $ttl = false );
195
196 }
197
198 endif;
199