/** * Constructor * * @param array $config */ function __construct($config = array()) { parent::__construct($config); if (isset($config['cache_dir'])) { $this->_cache_dir = trim($config['cache_dir']); } else { $this->_cache_dir = w3_cache_blog_dir($config['section'], $config['blog_id']); } $this->_exclude = isset($config['exclude']) ? (array) $config['exclude'] : array(); $this->_flush_timelimit = isset($config['flush_timelimit']) ? (int) $config['flush_timelimit'] : 180; $this->_locking = isset($config['locking']) ? (bool) $config['locking'] : false; if (isset($config['flush_dir'])) { $this->_flush_dir = $config['flush_dir']; } else { if ($config['blog_id'] <= 0) { // clear whole section if we operate on master cache $this->_flush_dir = w3_cache_dir($config['section']); } else { $this->_flush_dir = $this->_cache_dir; } } if (isset($config['use_wp_hash']) && $config['use_wp_hash'] && function_exists('wp_hash')) { $this->_use_wp_hash = true; } }
function cleanup() { $engine = $this->_config->get_string('fragmentcache.engine'); switch ($engine) { case 'file': w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner.php'); $w3_cache_file_cleaner = new W3_Cache_File_Cleaner(array('cache_dir' => w3_cache_blog_dir('fragment'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner->clean(); break; } }
/** * Called on successful file upload to CDN * * @param $file_name */ function on_cdn_file_upload($file_name) { if ($this->_minify_enabled) { $minify_document_root = w3_cache_blog_dir('minify') . '/'; if (!substr($file_name, 0, strlen($minify_document_root)) == $minify_document_root) { // unexpected file name return; } $short_file_name = substr($file_name, strlen($minify_document_root)); $this->_get_minify()->set_file_custom_data($short_file_name, array('cdn.status' => 'uploaded')); } }
function print_script() { ?> <script type="text/javascript"> function w3tc_start_minify_try_solve() { var testUrl = '<?php echo w3_filename_to_url(w3_cache_blog_dir('minify') . '/'); ?> '; w3tc_filename_auto_solve(testUrl); } </script> <?php }
/** * Write log entry * * @param string message * @param string $file * @return bool|int */ function w3tc_file_log($message, $file) { if (defined('W3_SUPPORT_DEBUG') && W3_SUPPORT_DEBUG) { w3_require_once(W3TC_INC_DIR . '/functions/file.php'); $data = sprintf("[%s] %s %s\n", date('r'), $message, $file); if (get_site_option('w3tc_support_request')) { $blog_id = 0; } else { $blog_id = null; } $filename = w3_cache_blog_dir('log', $blog_id) . '/file-sender.log'; if (!is_dir(dirname($filename))) { w3_mkdir_from(dirname($filename), W3TC_CACHE_DIR); } @file_put_contents($filename, $data, FILE_APPEND); } }
function cleanup_local() { $engine = $this->_config->get_string('pgcache.engine'); switch ($engine) { case 'file': w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner.php'); $w3_cache_file_cleaner = new W3_Cache_File_Cleaner(array('cache_dir' => w3_cache_blog_dir('page'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner->clean(); break; case 'file_generic': w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner/Generic.php'); if (w3_get_blog_id() == 0) { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . w3_get_domain(w3_get_host()); } $w3_cache_file_cleaner_generic = new W3_Cache_File_Cleaner_Generic(array('exclude' => array('.htaccess'), 'cache_dir' => $flush_dir, 'expire' => $this->_config->get_integer('browsercache.html.lifetime'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner_generic->clean(); break; } }
/** * Formats custom URL * * @param array $files * @param string $type * @return array */ function get_minify_urls_for_files($files, $type) { $w3_minify = w3_instance('W3_Minify'); $url = array(); $minify_filenames = $w3_minify->compress_minify_files($files, $type); foreach ($minify_filenames as $minify_filename) { $filename = w3_cache_blog_dir('minify') . '/' . $minify_filename; if ($this->_config->get_boolean('minify.rewrite')) { $url[] = w3_filename_to_url($filename); } else { $url[] = plugins_url('pub/minify.php?file=' . $minify_filename, W3TC_FILE); } } return $url; }
/** * Compresses an array of files into a filename containing all files. * If filename length exceeds 246 characters or value defined in minify.auto.filename_length when gzcompress/gzdeflate is available * multiple compressed filenames will be returned * @param $files * @param $type * @return array */ function compress_minify_files($files, $type) { $optimized = array(); foreach ($files as $file) { // from PHP.net $pattern = '/\\w+\\/\\.\\.\\//'; while (preg_match($pattern, $file)) { $file = preg_replace($pattern, '', $file); } if (!w3_is_url($file)) { $optimized[] = dirname($file) . '/' . basename($file, '.' . $type); } else { $optimized[] = $file; } } $input = array(); $replace = $this->_minify_path_replacements(); $replaced = false; foreach ($optimized as $file) { foreach ($replace as $key => $path) { if (strpos($file, $path) === 0) { $input[] = str_replace($path, $key, $file); $replaced = true; break; } } if ($replaced) { $replaced = false; } else { $input[] = $file; } } $minify_filename = array(); $imploded = implode(',', $input); $config = w3_instance('W3_Config'); if (!W3TC_WIN) { $fn_length = $config->get_integer('minify.auto.filename_length', 246); $fn_length = $fn_length > 246 ? 246 : $fn_length; } else { $dir = w3_cache_blog_dir('minify'); $fn_length = 246 - strlen($dir); } $compressed = $this->_compress($imploded); if (strlen($compressed) >= $fn_length) { $arr_chunks = $this->_combine_and_check_filenames($input, $fn_length); foreach ($arr_chunks as $part) { $part_imploded = implode(',', $part); $base = rtrim(strtr(base64_encode($this->_compress($part_imploded)), '+/', '-_'), '='); $minify_filename[] = $base . '.' . $type; } } else { $base = rtrim(strtr(base64_encode($compressed), '+/', '-_'), '='); $minify_filename[] = $base . '.' . $type; } return $minify_filename; }
/** * Does disk cache cleanup * * @return void */ function cleanup() { w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner.php'); $w3_cache_file_cleaner = new W3_Cache_File_Cleaner(array('cache_dir' => w3_cache_blog_dir('object'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner->clean(); }
/** * Does disk cache cleanup * * @return void */ function cleanup() { w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner/Generic.php'); $w3_cache_file_cleaner_generic = new W3_Cache_File_Cleaner_Generic(array('exclude' => array('*.files', '.htaccess', 'index.php'), 'cache_dir' => w3_cache_blog_dir('minify'), 'expire' => $this->_config->get_integer('minify.file.gc'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner_generic->clean(); }
/** * Send support request action * * @return void */ function action_support_request() { w3_require_once(W3TC_LIB_W3_DIR . '/Request.php'); $request_type = W3_Request::get_string('request_type'); $payment = W3_Request::get_boolean('payment'); $request_id = W3_Request::get_string('request_id'); $url = W3_Request::get_string('url'); $name = W3_Request::get_string('name'); $email = W3_Request::get_string('email'); $twitter = W3_Request::get_string('twitter'); $phone = W3_Request::get_string('phone'); $subject = W3_Request::get_string('subject'); $description = W3_Request::get_string('description'); $templates = W3_Request::get_array('templates'); $forum_url = W3_Request::get_string('forum_url'); $wp_login = W3_Request::get_string('wp_login'); $wp_password = W3_Request::get_string('wp_password'); $ftp_host = W3_Request::get_string('ftp_host'); $ftp_login = W3_Request::get_string('ftp_login'); $ftp_password = W3_Request::get_string('ftp_password'); $subscribe_releases = W3_Request::get_string('subscribe_releases'); $subscribe_customer = W3_Request::get_string('subscribe_customer'); $params = array('request_type' => $request_type, 'payment' => $payment, 'url' => $url, 'name' => $name, 'email' => $email, 'twitter' => $twitter, 'phone' => $phone, 'subject' => $subject, 'description' => $description, 'forum_url' => $forum_url, 'wp_login' => $wp_login, 'wp_password' => $wp_password, 'ftp_host' => $ftp_host, 'ftp_login' => $ftp_login, 'ftp_password' => $ftp_password, 'subscribe_releases' => $subscribe_releases, 'subscribe_customer' => $subscribe_customer); $post = $params; foreach ($templates as $template_index => $template) { $template_key = sprintf('templates[%d]', $template_index); $params[$template_key] = $template; } if (!isset($this->_request_types[$request_type])) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_type')), false); } $required = array('bug_report' => 'url,name,email,subject,description', 'new_feature' => 'url,name,email,subject,description', 'email_support' => 'url,name,email,subject,description', 'phone_support' => 'url,name,email,subject,description,phone', 'plugin_config' => 'url,name,email,subject,description,wp_login,wp_password', 'theme_config' => 'url,name,email,subject,description,wp_login,wp_password,ftp_host,ftp_login,ftp_password', 'linux_config' => 'url,name,email,subject,description,wp_login,wp_password,ftp_host,ftp_login,ftp_password'); if (strstr($required[$request_type], 'url') !== false && $url == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_url')), false); } if (strstr($required[$request_type], 'name') !== false && $name == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_name')), false); } if (strstr($required[$request_type], 'email') !== false && !preg_match('~^[a-z0-9_\\-\\.]+@[a-z0-9-\\.]+\\.[a-z]{2,5}$~', $email)) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_email')), false); } if (strstr($required[$request_type], 'phone') !== false && !preg_match('~^[0-9\\-\\.\\ \\(\\)\\+]+$~', $phone)) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_phone')), false); } if (strstr($required[$request_type], 'subject') !== false && $subject == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_subject')), false); } if (strstr($required[$request_type], 'description') !== false && $description == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_description')), false); } if (strstr($required[$request_type], 'wp_login') !== false && $wp_login == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_wp_login')), false); } if (strstr($required[$request_type], 'wp_password') !== false && $wp_password == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_wp_password')), false); } if (strstr($required[$request_type], 'ftp_host') !== false && $ftp_host == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_ftp_host')), false); } if (strstr($required[$request_type], 'ftp_login') !== false && $ftp_login == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_ftp_login')), false); } if (strstr($required[$request_type], 'ftp_password') !== false && $ftp_password == '') { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'support_request_ftp_password')), false); } /** * Add attachments */ $attachments = array(); $attach_files = array(w3_get_wp_config_path(), w3_cache_blog_dir('log') . '/minify.log', w3_get_pgcache_rules_core_path(), w3_get_pgcache_rules_cache_path(), w3_get_browsercache_rules_cache_path(), w3_get_browsercache_rules_no404wp_path(), w3_get_minify_rules_core_path(), w3_get_minify_rules_cache_path()); /** * Attach config files */ if ($handle = opendir(W3TC_CONFIG_DIR)) { while (($entry = @readdir($handle)) !== false) { if ($entry == '.' || $entry == '..' || $entry == 'index.html') { continue; } $attachments[] = W3TC_CONFIG_DIR . '/' . $entry; } closedir($handle); } foreach ($attach_files as $attach_file) { if ($attach_file && file_exists($attach_file) && !in_array($attach_file, $attachments)) { $attachments[] = $attach_file; } } /** * Attach server info */ $server_info = print_r($this->get_server_info(), true); $server_info = str_replace("\n", "\r\n", $server_info); $server_info_path = W3TC_CACHE_TMP_DIR . '/server_info.txt'; if (@file_put_contents($server_info_path, $server_info)) { $attachments[] = $server_info_path; } /** * Attach phpinfo */ ob_start(); phpinfo(); $php_info = ob_get_contents(); ob_end_clean(); $php_info_path = W3TC_CACHE_TMP_DIR . '/php_info.html'; if (@file_put_contents($php_info_path, $php_info)) { $attachments[] = $php_info_path; } /** * Attach self-test */ ob_start(); $this->action_self_test(); $self_test = ob_get_contents(); ob_end_clean(); $self_test_path = W3TC_CACHE_TMP_DIR . '/self_test.html'; if (@file_put_contents($self_test_path, $self_test)) { $attachments[] = $self_test_path; } /** * Attach templates */ foreach ($templates as $template) { if (!empty($template)) { $attachments[] = $template; } } /** * Attach other files */ if (!empty($_FILES['files'])) { $files = (array) $_FILES['files']; for ($i = 0, $l = count($files); $i < $l; $i++) { if (isset($files['tmp_name'][$i]) && isset($files['name'][$i]) && isset($files['error'][$i]) && $files['error'][$i] == UPLOAD_ERR_OK) { $path = W3TC_CACHE_TMP_DIR . '/' . $files['name'][$i]; if (@move_uploaded_file($files['tmp_name'][$i], $path)) { $attachments[] = $path; } } } } $data = array(); if (!empty($wp_login) && !empty($wp_password)) { $data['WP Admin login'] = $wp_login; $data['WP Admin password'] = $wp_password; } if (!empty($ftp_host) && !empty($ftp_login) && !empty($ftp_password)) { $data['SSH / FTP host'] = $ftp_host; $data['SSH / FTP login'] = $ftp_login; $data['SSH / FTP password'] = $ftp_password; } /** * Store request data for future access */ if (count($data)) { $hash = md5(microtime()); $request_data = get_option('w3tc_request_data', array()); $request_data[$hash] = $data; update_option('w3tc_request_data', $request_data); $request_data_url = sprintf('%s/w3tc_request_data/%s', w3_get_home_url(), $hash); } else { $request_data_url = ''; } $nonce = wp_create_nonce('w3tc_support_request'); if (is_network_admin()) { update_site_option('w3tc_support_request', $nonce); } else { update_option('w3tc_support_request', $nonce); } $file_access = WP_PLUGIN_URL . '/' . dirname(W3TC_FILE) . '/pub/files.php'; if (w3_get_domain(w3_get_home_url()) != w3_get_domain(w3_get_site_url())) { $file_access = str_replace(w3_get_domain(w3_get_home_url()), w3_get_domain(w3_get_site_url()), $file_access); } $post['file_access'] = $file_access; $post['nonce'] = $nonce; $post['request_data_url'] = $request_data_url; $post['ip'] = $_SERVER['REMOTE_ADDR']; $post['user_agent'] = $_SERVER['HTTP_USER_AGENT']; $post['version'] = W3TC_VERSION; $post['plugin'] = 'W3 Total Cache'; $post['request_id'] = $request_id; $license_level = 'community'; if (w3_is_pro($this->_config)) { $license_level = 'pro'; } elseif (w3_is_enterprise($this->_config)) { $license_level = 'enterprise'; } $post['license_level'] = $license_level; $unset = array('wp_login', 'wp_password', 'ftp_host', 'ftp_login', 'ftp_password'); foreach ($unset as $key) { unset($post[$key]); } foreach ($attachments as $attachment) { if (is_network_admin()) { update_site_option('attachment_' . md5($attachment), $attachment); } else { update_option('attachment_' . md5($attachment), $attachment); } } $post = array_merge($post, array('files' => $attachments)); if (defined('W3_SUPPORT_DEBUG') && W3_SUPPORT_DEBUG) { $data = sprintf("[%s] Post support request\n", date('r')); foreach ($post as $key => $value) { $data .= sprintf("%s => %s\n", $key, is_array($value) ? implode(',', $value) : $value); } $filename = w3_cache_blog_dir('log') . '/support.log'; if (!is_dir(dirname($filename))) { w3_mkdir_from(dirname($filename), W3TC_CACHE_DIR); } @file_put_contents($filename, $data, FILE_APPEND); } $response = wp_remote_post(W3TC_SUPPORT_REQUEST_URL, array('body' => $post, 'timeout' => $this->_config->get_integer('timelimit.email_send'))); if (defined('W3_SUPPORT_DEBUG') && W3_SUPPORT_DEBUG) { $filename = w3_cache_blog_dir('log') . '/support.log'; $data = sprintf("[%s] Post response \n%s\n", date('r'), print_r($response, true)); @file_put_contents($filename, $data, FILE_APPEND); } if (!is_wp_error($response)) { $result = $response['response']['code'] == 200 && $response['body'] == 'Ok'; } else { $result = false; } /** * Remove temporary files */ foreach ($attachments as $attachment) { if (strstr($attachment, W3TC_CACHE_TMP_DIR) !== false) { @unlink($attachment); } if (is_network_admin()) { delete_site_option('attachment_' . md5($attachment)); } else { delete_option('attachment_' . md5($attachment)); } } if (is_network_admin()) { delete_site_option('w3tc_support_request'); } else { delete_option('w3tc_support_request'); } if ($result) { w3_admin_redirect(array('tab' => 'general', 'w3tc_note' => 'support_request'), false); } else { w3_admin_redirect(array_merge($params, array('request_type' => $request_type, 'w3tc_error' => 'support_request')), false); } }
private function verify_rewrite_working() { $url = w3_filename_to_url(w3_cache_blog_dir('minify') . '/w3tc_rewrite_test'); if (!$this->test_rewrite($url)) { $key = sprintf('w3tc_rewrite_test_result_%s', substr(md5($url), 0, 16)); $result = get_transient($key); $home_url = w3_get_home_url(); $tech_message = (w3_is_nginx() ? 'nginx configuration file' : '.htaccess file') . ' contains rules to rewrite url ' . $url . '. If handled by ' . 'plugin, it returns "OK" message.<br/>'; $tech_message .= 'The plugin made a request to ' . $url . ' but received: <br />' . $result . '<br />'; $tech_message .= 'instead of "OK" response. <br />'; $error = '<strong>W3 Total Cache error:</strong>It appears Minify ' . '<acronym title="Uniform Resource Locator">URL</acronym> ' . 'rewriting is not working. '; if (w3_is_nginx()) { $error .= 'Please verify that all configuration files are ' . 'included in the configuration file ' . '(and that you have reloaded / restarted nginx).'; } else { $error .= 'Please verify that the server configuration ' . 'allows .htaccess'; } $error .= '<br />Unfortunately minification will ' . 'not function without custom rewrite rules. ' . 'Please ask your server administrator for assistance. ' . 'Also refer to <a href="' . admin_url('admin.php?page=w3tc_install') . '">the install page</a> for the rules for your server.'; throw new SelfTestFailedException($error, $tech_message); } }
function print_script() { ?> <script type="text/javascript"> var w3_use_network_link = <?php echo w3_force_master() ? 'true' : 'false'; ?> ; function w3tc_start_minify_try_solve() { var testUrl = '<?php echo w3_filename_to_url(w3_cache_blog_dir('minify') . '/'); ?> '; w3tc_filename_auto_solve(testUrl); } </script> <?php }
/** * Exports min files to CDN * * @return array */ function get_files_minify() { $files = array(); if (W3TC_PHP5 && $this->_config->get_boolean('minify.rewrite') && (!$this->_config->get_boolean('minify.auto') || w3_is_cdn_mirror($this->_config->get_string('cdn.engine')))) { w3_require_once(W3TC_INC_DIR . '/functions/http.php'); $minify = w3_instance('W3_Plugin_Minify'); $document_root = w3_get_document_root(); $minify_root = w3_cache_blog_dir('minify'); $minify_path = ltrim(str_replace($document_root, '', $minify_root), '/'); $urls = $minify->get_urls(); if ($this->_config->get_string('minify.engine') == 'file') { foreach ($urls as $url) { w3_http_get($url); } $files = $this->search_files($minify_root, $minify_path, '*.css;*.js'); } else { foreach ($urls as $url) { $file = w3_normalize_file_minify($url); $file = w3_translate_file($file); if (!w3_is_url($file)) { $file = $document_root . '/' . $file; $file = ltrim(str_replace($minify_root, '', $file), '/'); $dir = dirname($file); if ($dir) { w3_mkdir($dir, 0777, $minify_root); } if (w3_download($url, $minify_root . '/' . $file) !== false) { $files[] = $minify_path . '/' . $file; } } } } } return $files; }
function print_script() { ?> <script type="text/javascript"> var w3_use_network_link = <?php echo is_network_admin() || w3_is_multisite() && w3_force_master() ? 'true' : 'false'; ?> ; function w3tc_start_minify_try_solve() { var testUrl = '<?php echo w3_filename_to_url(w3_cache_blog_dir('minify') . '/', w3_get_domain(w3_get_home_url()) != w3_get_domain(w3_get_site_url())); ?> '; w3tc_filename_auto_solve(testUrl); } </script> <?php }
/** * Perform minify rules rewrite test * * @return bool */ function test_rewrite_minify() { $url = w3_filename_to_url(w3_cache_blog_dir('minify') . '/w3tc_rewrite_test'); return $this->test_rewrite($url); }