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 / controller / track.php

track.php in FV Player 8 8.1, at controller/track.php

337 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /* This file doesn't load WordPress, it simple increment counters for posts in wp-content/cache/fv-tracker/{tag}-{site id}.data */
3
4 if( !defined('SHORTINIT') ) {
5 define('SHORTINIT',true);
6 }
7
8 // include wp-load.php
9 if( file_exists('../../../../wp-load.php') ) {
10 require('../../../../wp-load.php');
11 }
12
13 //require_once( ABSPATH . WPINC . '/pluggable.php' );
14
15 /**
16 * Including what's necessary for nonce verification
17 */
18 require_once( ABSPATH . WPINC . '/capabilities.php' );
19 require_once( ABSPATH . WPINC . '/class-wp-roles.php' );
20 require_once( ABSPATH . WPINC . '/class-wp-role.php' );
21 require_once( ABSPATH . WPINC . '/class-wp-user.php' );
22 require_once( ABSPATH . WPINC . '/user.php' );
23
24 // Translation and localization.
25 require_once( ABSPATH . WPINC . '/pomo/mo.php' );
26 require_once( ABSPATH . WPINC . '/l10n.php' );
27
28 if ( file_exists( ABSPATH . WPINC . '/class-wp-textdomain-registry.php' ) ) {
29 require_once( ABSPATH . WPINC . '/class-wp-textdomain-registry.php' );
30 }
31
32 require_once( ABSPATH . WPINC . '/class-wp-locale.php' );
33 require_once( ABSPATH . WPINC . '/class-wp-locale-switcher.php' );
34
35 if ( class_exists( 'WP_Textdomain_Registry' ) ) {
36 global $wp_textdomain_registry;
37 if ( ! $wp_textdomain_registry instanceof WP_Textdomain_Registry ) {
38 $wp_textdomain_registry = new WP_Textdomain_Registry();
39 }
40 }
41
42 require_once( ABSPATH . WPINC . '/pluggable.php' );
43 require_once( ABSPATH . WPINC . '/functions.php' );
44 require_once( ABSPATH . WPINC . '/formatting.php' );
45 require_once( ABSPATH . WPINC . '/link-template.php' );
46 require_once( ABSPATH . WPINC . '/shortcodes.php' );
47 require_once( ABSPATH . WPINC . '/general-template.php' );
48 require_once( ABSPATH . WPINC . '/class-wp-session-tokens.php' );
49 require_once( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' );
50 require_once( ABSPATH . WPINC . '/meta.php' );
51 require_once( ABSPATH . WPINC . '/kses.php' );
52 require_once( ABSPATH . WPINC . '/rest-api.php' );
53 require_once( ABSPATH . WPINC . '/blocks.php' );
54
55 // Without this plugins_url() won't work
56 wp_plugin_directory_constants();
57
58 // Without this the user login status won't work
59 wp_cookie_constants();
60
61 Class FvPlayerTrackerWorker {
62
63 private $wp_content = false;
64 private $cache_path = false;
65 private $cache_filename = false;
66 private $video_id = false;
67 private $post_id = false;
68 private $player_id = false;
69 private $user_id = 0;
70 private $guest_user_id = 0;
71 private $watched = false;
72 private $tag = false;
73
74 private $file = false;
75
76 function __construct() {
77
78 if(
79 !isset( $_REQUEST['blog_id'] ) ||
80 !isset( $_REQUEST['tag'] ) ||
81 !isset( $_REQUEST['video_id'] ) && !isset( $_REQUEST['watched'] )
82 ){
83 die( "Error: missing arguments!" );
84 }
85
86 // $action has one been added in WordPress 6.1 unfortunately
87 add_filter(
88 'nonce_life',
89 function( $seconds, $action = false ) {
90 if ( 'fv_player_track' === $action ) {
91 $seconds = 7 * DAY_IN_SECONDS;
92 }
93 return $seconds;
94 },
95 PHP_INT_MAX,
96 2
97 );
98
99 // Do not check HTTP auth as we did not load WP_Application_Passwords class
100 remove_filter( 'determine_current_user', 'wp_validate_application_password', 20 );
101
102 if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'fv_player_track' ) ) {
103 die( "Error: invalid nonce!" );
104 }
105
106 if( sanitize_key( $_REQUEST['tag'] ) == 'click' ) {
107 $a = 1;
108 }
109
110 $blog_id = intval($_REQUEST['blog_id']);
111 $tag = preg_replace( '~[^a-z]~', '', substr( sanitize_key( $_REQUEST['tag'] ), 0, 16 ) );
112 $this->tag = $tag;
113
114 $this->wp_content = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
115 $this->cache_path = $this->wp_content."/fv-player-tracking";
116 $this->cache_filename = "{$tag}-{$blog_id}.data";
117
118 $this->video_id = !empty($_REQUEST['video_id']) ? intval($_REQUEST['video_id']) : false;
119 $this->player_id = !empty($_REQUEST['player_id']) ? intval($_REQUEST['player_id']) : false;
120 $this->post_id = !empty($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : false;
121 $this->user_id = intval($_REQUEST['user_id']);
122 $this->watched = !empty($_REQUEST['watched']) ? sanitize_text_field( urldecode( $_REQUEST['watched'] ) ) : false;
123
124 // TODO: Verify some kind of signature here
125
126 $this->checkCacheFile();
127 }
128
129 /**
130 * Check and initialize cache file
131 * @return void
132 */
133 function checkCacheFile() {
134 $full_path = $this->cache_path . "/" . $this->cache_filename;
135
136 //cache file exists?
137 if( file_exists( $full_path ) ) return;
138
139 //cache directory exists
140 if( !file_exists( $this->cache_path ) ){
141 //create dir
142 //todo: actually don't create it, if it doesn't exist it should mean the option is not enabled and this script shouldn't write anything!
143 if( !mkdir( $this->cache_path, 0775, true ) ){
144 die("Error: failed to create cache directory.");
145 }
146 }
147
148 //init file
149 touch( $full_path );
150 }
151
152 /**
153 * Load cache file data, find specific video_id and increment coutner for it
154 * @return boolean True when file lock was obtained, this doesn't ensure successful write. Otherwise false is returned
155 */
156 function incrementCacheCounter() {
157 $max_attempts = 3;
158
159 for( $i = 0; $i < $max_attempts; $i++ ){
160
161 if( flock( $this->file, LOCK_EX | LOCK_NB ) ) {
162
163 //increment counter
164 $encoded_data = fgets( $this->file );
165 $data = false;
166 if( $encoded_data ) {
167 $data = json_decode( $encoded_data, true );
168
169 $json_error = json_last_error();
170 if( $json_error !== JSON_ERROR_NONE ) {
171 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
172 file_put_contents( $this->wp_content.'/fv-player-track-error.log', gmdate('r')." JSON decode error:\n".var_export( array( 'err' => $json_error, 'data' => $encoded_data ), true )."\n", FILE_APPEND ); // todo: remove
173 ftruncate( $this->file, 0 );
174 return false;
175 }
176 }
177
178 if( !$data ) {
179 $data = array();
180 }
181
182 if ( 'seconds' === $this->tag ) {
183 $this->watched = json_decode( $this->watched, true );
184
185 $json_error = json_last_error();
186 if( $json_error !== JSON_ERROR_NONE ) {
187 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
188 file_put_contents( $this->wp_content.'/fv-player-track-error.log', gmdate('r')." JSON decode error for watched:\n".var_export( array( 'err' => $json_error, 'data' => $this->watched ), true )."\n", FILE_APPEND ); // todo: remove
189 return false;
190 }
191
192 foreach ( $this->watched as $player_id => $players ) {
193
194 foreach( $players as $post_id => $videos ) {
195
196 foreach( $videos as $video_id => $seconds ) {
197
198 // Add to the existing JSON data
199 $found = false;
200 foreach( $data as $index => $item ) {
201 if( $item['video_id'] == $video_id && $item['post_id'] == $post_id && $item['player_id'] == $player_id && $item['user_id'] == $this->user_id && $item['guest_user_id'] == $this->guest_user_id ) {
202 $data[$index]['seconds'] = round( $data[$index]['seconds'] + $seconds );
203 $found = true;
204 }
205 }
206
207 // New JSON data
208 if ( ! $found ) {
209 $data[] = array(
210 'video_id' => $video_id,
211 'post_id' => $post_id,
212 'player_id' => $player_id,
213 'user_id' => $this->user_id,
214 'guest_user_id' => $this->guest_user_id,
215 'seconds' => round($seconds)
216 );
217 }
218 }
219 }
220 }
221
222 } else if ( 'play' === $this->tag ) {
223 $found = false;
224 foreach( $data as $index => $item ) {
225 if( $item['video_id'] == $this->video_id && $item['post_id'] == $this->post_id && $item['player_id'] == $this->player_id && $item['user_id'] == $this->user_id && $item['guest_user_id'] == $this->guest_user_id ) {
226 $data[$index]['play'] += 1;
227 $found = true;
228 break;
229 }
230 }
231
232 if( !$found ) {
233 $data[] = array(
234 'video_id' => $this->video_id,
235 'post_id' => $this->post_id,
236 'player_id' => $this->player_id,
237 'user_id' => $this->user_id,
238 'guest_user_id' => $this->guest_user_id,
239 'play' => 1
240 );
241 }
242 } else if ( 'click' === $this->tag ) {
243 $found = false;
244 foreach( $data as $index => $item ) {
245 if( $item['video_id'] == $this->video_id && $item['post_id'] == $this->post_id && $item['player_id'] == $this->player_id && $item['user_id'] == $this->user_id && $item['guest_user_id'] == $this->guest_user_id ) {
246 $data[$index]['click'] += 1;
247 $found = true;
248 break;
249 }
250 }
251
252 if( !$found ) {
253 $data[] = array(
254 'video_id' => $this->video_id,
255 'post_id' => $this->post_id,
256 'player_id' => $this->player_id,
257 'user_id' => $this->user_id,
258 'guest_user_id' => $this->guest_user_id,
259 'click' => 1
260 );
261 }
262 }
263
264 $encoded_data = wp_json_encode($data);
265
266 ftruncate( $this->file, 0 );
267 rewind( $this->file );
268 fputs( $this->file, $encoded_data );
269
270 //UNLOCK
271 flock( $this->file, LOCK_UN );
272 return true;
273 }
274 else{
275 //wait random interval from 50ms to 100ms
276 usleep( wp_rand(50,100) );
277 }
278 }
279
280 return false;
281 }
282
283 /**
284 * Main tracker functionality
285 * @return void
286 */
287 function track() {
288
289 if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'fv_player_track' ) ) {
290 die( "Error: invalid nonce!" );
291 }
292
293 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
294 $this->file = fopen( $this->cache_path."/".$this->cache_filename, 'r+');
295
296 $options = get_option('fvwpflowplayer');
297 $guest_user_id = 0;
298
299 if( absint( $_REQUEST['user_id'] ) == 0 && ! empty( $options['video_stats_enable_guest'] ) && 'true' === $options['video_stats_enable_guest']) { // guest user
300
301 if( isset( $_COOKIE['fv_player_stats_guest_user_id'] ) ) { // check if cookie is set
302 $guest_user_id = intval( $_COOKIE['fv_player_stats_guest_user_id'] );
303 } else { // create new guest user id
304 $last_guest_id = get_option( 'fv_player_stats_last_guest_user_id', 0 );
305 $last_guest_id = $last_guest_id + 1;
306
307 $guest_user_id = $last_guest_id;
308
309 update_option( 'fv_player_stats_last_guest_user_id', $last_guest_id );
310
311 // save cookie fo 1 year
312 setcookie( 'fv_player_stats_guest_user_id', $last_guest_id, time() + 60 * 60 * 24 * 365, '/' );
313 }
314 }
315
316 $this->guest_user_id = $guest_user_id;
317
318 if( ! $this->incrementCacheCounter() ) {
319 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
320 file_put_contents( $this->wp_content.'/fv-player-track-error.log', gmdate('r') . " flock or other error:\n".var_export( $this,true )."\n", FILE_APPEND ); // todo: remove
321 }
322
323 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
324 fclose( $this->file );
325
326 // Add .htaccess to deny all direct access
327 $htaccess_path = $this->cache_path . '/.htaccess';
328 if ( ! file_exists( $htaccess_path ) ) {
329 file_put_contents( $htaccess_path, "# Deny access to tracking files\nOrder allow,deny\nDeny from all\n" );
330 }
331
332 }
333 }
334
335 $fv_player_tracker_worker = new FvPlayerTrackerWorker();
336 $fv_player_tracker_worker->track();
337