PluginProbe
wpForo Forum / 3.2.0
wpForo Forum v3.2.0
3.2.0 3.1.7 3.1.6 3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 All 140 releases
← All changes | classes/Cache.php +45 -15 3.1.4 → 3.2.0 View file →
@@ -124,9 +124,9 @@
124 124 if( in_array( $template, $loop_templates ) ) {
125 125 $cache_file = $this->dir . '/' . $template . '/' . $key;
126 126 $array = wpforo_get_file_content( $cache_file );
127 127
128 - return @unserialize( $array );
128 + return @unserialize( $array, [ 'allowed_classes' => false ] );
129 129 }
130 130 }
131 131 }
132 132 }
@@ -137,9 +137,9 @@
137 137 $domain_path = preg_replace('|https?://|i', '', get_site_url() );
138 138 if( $this->exists( $key, 'item', $type, $sub_type ) ) {
139 139 $cache_file = $this->dir . '/item/' . $type . '/' . ( $sub_type ? $sub_type . '_' : '' ) . $key;
140 140 $array = wpforo_get_file_content( $cache_file );
141 - $data = @unserialize( $array );
141 + $data = @unserialize( $array, [ 'allowed_classes' => false ] );
142 142 if( $type === 'url' ) {
143 143 // Always make sure the cached URLs are pointed to current website
144 144 if( strpos( (string) $data, $domain_path ) === FALSE ) return null;
145 145 }
@@ -442,22 +442,52 @@
442 442
443 443 public function check( $directory ) {
444 444 $directory = (string) $directory;
445 445 $directory = wpforo_fix_dir_sep( $directory );
446 - $filecount = 0;
447 - if( class_exists( 'FilesystemIterator' ) && is_dir( $directory ) ) {
448 - $fi = new FilesystemIterator( $directory, FilesystemIterator::SKIP_DOTS );
449 - $filecount = iterator_count( $fi );
446 +
447 + if( ! class_exists( 'FilesystemIterator' ) || ! is_dir( $directory ) ) return;
448 +
449 + $fi = new FilesystemIterator( $directory, FilesystemIterator::SKIP_DOTS );
450 + $filecount = iterator_count( $fi );
451 + if( ! $filecount ) return;
452 +
453 + // Dynamic limit based on online members (scales with forum activity)
454 + // Use direct call to avoid recursion: statistic() calls post->get_url() which triggers cache->create() -> check() -> statistic()
455 + $online = WPF()->member ? intval( WPF()->member->online_members_count() ) : 0;
456 + $online = max( 2, $online );
457 + $max = $online * 500;
458 + $max = max( 1000, min( $max, 50000 ) );
459 +
460 + /**
461 + * Filter the maximum cache files allowed per directory.
462 + *
463 + * @param int $max Maximum files (default: dynamic based on online users)
464 + * @param string $directory The cache directory being checked
465 + */
466 + $max = (int) apply_filters( 'wpforo_cache_dir_max_files', $max, $directory );
467 + if( $max === 0 ) return; // 0 = disable cache limit
468 +
469 + if( $filecount > $max ) {
470 + $this->clean_old_files( $directory, (int) ceil( $filecount * 0.5 ) );
450 471 }
451 - if( ! $filecount ) {
452 - $directory_ns = trim( $directory, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '*';
453 - $directory_ws = DIRECTORY_SEPARATOR . trim( $directory, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '*';
454 - $files = glob( $directory_ns );
455 - if( empty( $files ) ) $files = glob( $directory_ws );
456 - $filecount = count( $files );
457 - }
458 - if( $filecount > 1000 ) {
459 - $this->clean_files( $directory );
472 + }
473 +
474 + private function clean_old_files( $directory, $delete_count ) {
475 + $directory_ns = trim( $directory, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '*';
476 + $directory_ws = DIRECTORY_SEPARATOR . trim( $directory, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '*';
477 + $files = glob( $directory_ns );
478 + if( empty( $files ) ) $files = glob( $directory_ws );
479 + if( empty( $files ) ) return;
480 +
481 + // Sort by modification time (oldest first)
482 + usort( $files, function( $a, $b ) {
483 + return filemtime( $a ) - filemtime( $b );
484 + });
485 +
486 + // Delete oldest 50%
487 + foreach( array_slice( $files, 0, $delete_count ) as $file ) {
488 + if( strpos( (string) $file, 'index.html' ) !== false || strpos( (string) $file, '.htaccess' ) !== false ) continue;
489 + if( ! is_dir( $file ) && file_exists( $file ) ) @unlink( $file );
460 490 }
461 491 }
462 492
463 493 public function clear_visitor_tracking() {