/** * Return full path to log file for module * Path used in priority * 1) W3TC_DEBUG_DIR * 2) WP_DEBUG_LOG * 3) W3TC_CACHE_DIR * * @param unknown $module * @param null $blog_id * @return string */ public static function log_filename($module, $blog_id = null) { if (is_null($blog_id)) { $blog_id = Util_Environment::blog_id(); } $postfix = sprintf('%06d', $blog_id); if (defined('W3TC_BLOG_LEVELS')) { for ($n = 0; $n < W3TC_BLOG_LEVELS; $n++) { $postfix = substr($postfix, strlen($postfix) - 1 - $n, 1) . '/' . $postfix; } } $from_dir = W3TC_CACHE_DIR; if (defined('W3TC_DEBUG_DIR') && W3TC_DEBUG_DIR) { $dir_path = W3TC_DEBUG_DIR; if (!is_dir(W3TC_DEBUG_DIR)) { $from_dir = dirname(W3TC_DEBUG_DIR); } } else { $dir_path = Util_Environment::cache_dir('log'); } $filename = $dir_path . '/' . $postfix . '/' . $module . '.log'; if (!is_dir(dirname($filename))) { Util_File::mkdir_from(dirname($filename), $from_dir); } return $filename; }
/** * * * @param unknown $folder * @param string $method Which method to use when creating * @param string $url Where to redirect after creation * @param bool|string $context folder to create folder in * @throws Util_WpFile_FilesystemMkdirException */ private static function create_folder($folder, $from_folder) { if (@is_dir($folder)) { return; } if (Util_File::mkdir_from($folder, $from_folder)) { return; } try { self::request_filesystem_credentials(); } catch (Util_WpFile_FilesystemOperationException $ex) { throw new Util_WpFile_FilesystemMkdirException($ex->getMessage(), $ex->credentials_form(), $folder); } global $wp_filesystem; if (!$wp_filesystem->mkdir($folder, FS_CHMOD_DIR)) { throw new Util_WpFile_FilesystemMkdirException('FTP credentials don\'t allow to create folder <strong>' . $folder . '</strong>', self::get_filesystem_credentials_form(), $folder); } }
/** * 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; }
/** * Pre-caches external file * * @param string $url * @param string $type * @return string */ function _precache_file($url, $type) { $lifetime = $this->_config->get_integer('minify.lifetime'); $cache_path = sprintf('%s/minify_%s.%s', Util_Environment::cache_blog_dir('minify'), md5($url), $type); if (!file_exists($cache_path) || @filemtime($cache_path) < time() - $lifetime) { if (!@is_dir(dirname($cache_path))) { Util_File::mkdir_from(dirname($cache_path), W3TC_CACHE_DIR); } Util_Http::download($url, $cache_path); } return file_exists($cache_path) ? $this->_get_minify_source($cache_path, $url) : false; }
private function fopen_write($key, $group, $mode) { $storage_key = $this->get_item_key($key); $sub_path = $this->_get_path($storage_key); $path = $this->_cache_dir . DIRECTORY_SEPARATOR . ($group ? $group . DIRECTORY_SEPARATOR : '') . $sub_path; $dir = dirname($path); if (!@is_dir($dir)) { if (!Util_File::mkdir_from($dir, W3TC_CACHE_DIR)) { return false; } } $fp = @fopen($path, $mode); return $fp; }
/** * Atomically writes file inside W3TC_CACHE_DIR dir * * @param unknown $filename * @param unknown $content * @throws Exception * @return void */ public static function file_put_contents_atomic($filename, $content) { Util_File::create_tmp_dir(); $temp = tempnam(W3TC_CACHE_TMP_DIR, 'temp'); try { if (!($f = @fopen($temp, 'wb'))) { if (file_exists($temp)) { @unlink($temp); } throw new \Exception('Can\'t write to temporary file <strong>' . $temp . '</strong>'); } fwrite($f, $content); fclose($f); if (!@rename($temp, $filename)) { @unlink($filename); if (!@rename($temp, $filename)) { Util_File::mkdir_from(dirname($filename), W3TC_CACHE_DIR); if (!@rename($temp, $filename)) { throw new \Exception('Can\'t write to file <strong>' . $filename . '</strong>'); } } } $chmod = 0644; if (defined('FS_CHMOD_FILE')) { $chmod = FS_CHMOD_FILE; } @chmod($filename, $chmod); } catch (\Exception $ex) { if (file_exists($temp)) { @unlink($temp); } throw $ex; } }
/** * Creates file for CDN upload. * Needed because minify can handle urls of non-existing files but CDN needs * real file to upload it */ public static function create_file_for_cdn($filename) { $minify_enabled = self::config()->get_boolean('minify.enabled'); if ($minify_enabled) { $minify_document_root = Util_Environment::cache_blog_dir('minify') . '/'; if (!substr($filename, 0, strlen($minify_document_root)) == $minify_document_root) { // unexpected file name return; } $short_filename = substr($filename, strlen($minify_document_root)); $minify = Dispatcher::component('Minify_MinifiedFileRequestHandler'); $data = $minify->process($short_filename, true); if (!file_exists($filename) && isset($data['content']['content'])) { if (!file_exists(dirname($filename))) { Util_File::mkdir_from(dirname($filename), W3TC_CACHE_DIR); } } @file_put_contents($filename, $data['content']['content']); } }
/** * * * @param Util_Environment_Exceptions $exs * @param string $path * @param string $rules * @param string $start * @param string $end * @param array $order */ public static function add_rules($exs, $path, $rules, $start, $end, $order) { if (empty($path)) { return; } $data = @file_get_contents($path); if ($data === false) { $data = ''; } $rules_missing = !empty($rules) && strstr(Util_Rule::clean_rules($data), Util_Rule::clean_rules($rules)) === false; if (!$rules_missing) { return; } $replace_start = strpos($data, $start); $replace_end = strpos($data, $end); if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) { $replace_length = $replace_end - $replace_start + strlen($end) + 1; } else { $replace_start = false; $replace_length = 0; $search = $order; foreach ($search as $string => $length) { $replace_start = strpos($data, $string); if ($replace_start !== false) { $replace_start += $length; break; } } } if ($replace_start !== false) { $data = Util_Rule::trim_rules(substr_replace($data, $rules, $replace_start, $replace_length)); } else { $data = Util_Rule::trim_rules($data . $rules); } if (strpos($path, W3TC_CACHE_DIR) === false || Util_Environment::is_nginx()) { try { Util_WpFile::write_to_file($path, $data); } catch (Util_WpFile_FilesystemOperationException $ex) { if ($replace_start !== false) { $exs->push(new Util_WpFile_FilesystemModifyException($ex->getMessage(), $ex->credentials_form(), sprintf(__('Edit file <strong>%s </strong> and replace all lines between and including <strong>%s</strong> and <strong>%s</strong> markers with:', 'w3-total-caceh'), $path, $start, $end), $path, $rules)); } else { $exs->push(new Util_WpFile_FilesystemModifyException($ex->getMessage(), $ex->credentials_form(), sprintf(__('Edit file <strong>%s</strong> and add the following rules above the WordPress directives:', 'w3-total-cache'), $path), $path, $rules)); } return; } } else { if (!@file_exists(dirname($path))) { Util_File::mkdir_from(dirname($path), W3TC_CACHE_DIR); } if (!@file_put_contents($path, $data)) { try { Util_WpFile::delete_folder(dirname($path), '', $_SERVER['REQUEST_URI']); } catch (Util_WpFile_FilesystemOperationException $ex) { $exs->push($ex); return; } } } Util_Rule::after_rules_modified(); }