/content/plugins/w3-total-cache/Cache_File_Generic.php
https://gitlab.com/karlen/ayo_wp · PHP · 285 lines · 161 code · 52 blank · 72 comment · 38 complexity · 49eaa5409477404dea43f9ee24f73030 MD5 · raw file
- <?php
- namespace W3TC;
- /**
- * Generic file cache
- */
- /**
- * class Cache_File_Generic
- */
- class Cache_File_Generic extends Cache_File {
- /**
- * Expire
- *
- * @var integer
- */
- var $_expire = 0;
- /**
- * PHP5-style constructor
- *
- * @param array $config
- */
- function __construct( $config = array() ) {
- parent::__construct( $config );
- $this->_expire = ( isset( $config['expire'] ) ? (int) $config['expire'] : 0 );
- if ( !$this->_expire || $this->_expire > W3TC_CACHE_FILE_EXPIRE_MAX ) {
- $this->_expire = W3TC_CACHE_FILE_EXPIRE_MAX;
- }
- }
- /**
- * Sets data
- *
- * @param string $key
- * @param string $var
- * @param int $expire
- * @param string $group Used to differentiate between groups of cache values
- * @return boolean
- */
- function set( $key, $var, $expire = 0, $group = '' ) {
- $key = $this->get_item_key( $key );
- $sub_path = $this->_get_path( $key );
- $path = $this->_cache_dir . '/' . $sub_path;
- $dir = dirname( $path );
- if ( !@is_dir( $dir ) ) {
- if ( !Util_File::mkdir_from( $dir, W3TC_CACHE_DIR ) )
- return false;
- }
- $tmppath = $path . '.' . getmypid();
- $fp = @fopen( $tmppath, 'w' );
- if ( !$fp )
- return false;
- if ( $this->_locking )
- @flock( $fp, LOCK_EX );
- @fputs( $fp, $var['content'] );
- @fclose( $fp );
- if ( $this->_locking )
- @flock( $fp, LOCK_UN );
- // some hostings create files with restrictive permissions
- // not allowing apache to read it later
- @chmod( $path, 0644 );
- if ( @filesize( $tmppath ) > 0 ) {
- @unlink( $path );
- @rename( $tmppath, $path );
- }
- @unlink( $tmppath );
- $old_entry_path = $path . '.old';
- @unlink( $old_entry_path );
- if ( Util_Environment::is_apache() && isset( $var['headers'] ) &&
- isset( $var['headers']['Content-Type'] ) &&
- substr( $var['headers']['Content-Type'], 0, 8 ) == 'text/xml' ) {
- file_put_contents( dirname( $path ) . '/.htaccess',
- "<IfModule mod_mime.c>\n" .
- " RemoveType .html_gzip\n" .
- " AddType text/xml .html_gzip\n" .
- " RemoveType .html\n" .
- " AddType text/xml .html\n".
- "</IfModule>" );
- }
- return true;
- }
- /**
- * Returns data
- *
- * @param string $key
- * @param string $group Used to differentiate between groups of cache values
- * @return array
- */
- function get_with_old( $key, $group = '' ) {
- $has_old_data = false;
- $key = $this->get_item_key( $key );
- $path = $this->_cache_dir . '/' . $this->_get_path( $key );
- $data = $this->_read( $path );
- if ( $data != null )
- return array( $data, $has_old_data );
- $path_old = $path . '.old';
- $too_old_time = time() - 30;
- if ( $exists = file_exists( $path_old ) ) {
- $file_time = @filemtime( $path_old );
- if ( $file_time ) {
- if ( $file_time > $too_old_time ) {
- // return old data
- $has_old_data = true;
- return array( $this->_read( $path_old ), $has_old_data );
- }
- @touch( $path_old );
- }
- }
- $has_old_data = $exists;
- return array( null, $has_old_data );
- }
- /**
- * Reads file
- *
- * @param string $path
- * @return array
- */
- private function _read( $path ) {
- if ( !is_readable( $path ) )
- return null;
- $fp = @fopen( $path, 'r' );
- if ( !$fp )
- return null;
- if ( $this->_locking )
- @flock( $fp, LOCK_SH );
- $var = '';
- while ( !@feof( $fp ) )
- $var .= @fread( $fp, 4096 );
- @fclose( $fp );
- if ( $this->_locking )
- @flock( $fp, LOCK_UN );
- return array(
- '404' => false,
- 'headers' => array(),
- 'time' => null,
- 'content' => $var
- );
- }
- /**
- * Deletes data
- *
- * @param string $key
- * @param string $group Used to differentiate between groups of cache values
- * @return boolean
- */
- function delete( $key, $group = '' ) {
- $key = $this->get_item_key( $key );
- $path = $this->_cache_dir . DIRECTORY_SEPARATOR . $this->_get_path( $key );
- if ( !file_exists( $path ) )
- return true;
- $old_entry_path = $path . '.old';
- if ( @rename( $path, $old_entry_path ) )
- return true;
- // if we can delete old entry - do second attempt to store in old-entry file
- if ( @unlink( $old_entry_path ) ) {
- if ( @rename( $path, $old_entry_path ) )
- return true;
- }
- return @unlink( $path );
- }
- /**
- * Key to delete, deletes .old and primary if exists.
- *
- * @param unknown $key
- * @return bool
- */
- function hard_delete( $key ) {
- $key = $this->get_item_key( $key );
- $path = $this->_cache_dir . DIRECTORY_SEPARATOR . $this->_get_path( $key );
- $old_entry_path = $path . '.old';
- @unlink( $old_entry_path );
- if ( !file_exists( $path ) )
- return true;
- @unlink( $path );
- return true;
- }
- /**
- * Flushes all data
- *
- * @param string $group Used to differentiate between groups of cache values
- * @return boolean
- */
- function flush( $group = '' ) {
- if ( $group == 'sitemaps' ) {
- $config = Dispatcher::config();
- $sitemap_regex = $config->get_string( 'pgcache.purge.sitemap_regex' );
- $this->_flush_based_on_regex( $sitemap_regex );
- } else {
- $c = new Cache_File_Cleaner_Generic( array(
- 'cache_dir' => $this->_flush_dir,
- 'exclude' => $this->_exclude,
- 'clean_timelimit' => $this->_flush_timelimit
- ) );
- $c->clean();
- }
- }
- /**
- * Returns cache file path by key
- *
- * @param string $key
- * @return string
- */
- function _get_path( $key ) {
- return $key;
- }
- function get_item_key( $key ) {
- return $key;
- }
- /**
- * Flush cache based on regex
- *
- * @param string $regex
- */
- private function _flush_based_on_regex( $regex ) {
- if ( Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain() ) {
- $domain = get_home_url();
- $parsed = parse_url( $domain );
- $host = $parsed['host'];
- $path = isset( $parsed['path'] ) ? '/' . trim( $parsed['path'], '/' ) : '';
- $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . $host . $path;
- } else
- $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . Util_Environment::host();
- $dir = @opendir( $flush_dir );
- if ( $dir ) {
- while ( ( $entry = @readdir( $dir ) ) !== false ) {
- if ( $entry == '.' || $entry == '..' ) {
- continue;
- }
- if ( preg_match( '/' . $regex . '/', basename( $entry ) ) ) {
- Util_File::rmdir( $flush_dir . DIRECTORY_SEPARATOR . $entry );
- }
- }
- @closedir( $dir );
- }
- }
- }