/** * Returns nginx rules path * * @return string */ function w3_get_nginx_rules_path() { $config = w3_instance('W3_Config'); $path = $config->get_string('config.path'); if (!$path) { $path = w3_get_document_root() . '/nginx.conf'; } return $path; }
/** * @throws FilesystemWriteException * @throws FilesystemWriteException */ public function create() { $path = trim(w3_get_wp_sitepath(), "/"); if ($path) { $path .= '/'; } $file_data = "\n<?php\n if (W3TC_WP_LOADING)\n require_once '" . w3_get_document_root() . '/' . $path . "wp-load.php';\n"; $filename = W3TC_WP_LOADER; $data = $file_data; w3_require_once(W3TC_INC_DIR . '/functions/rule.php'); $current_data = @file_get_contents($filename); if (strstr(w3_clean_rules($current_data), w3_clean_rules($data)) !== false) { return; } w3_require_once(W3TC_INC_DIR . '/functions/activation.php'); w3_wp_write_to_file($filename, $data, '', $_SERVER['REQUEST_URI']); }
/** * Taks an absolute path and converts to a relative path to root * @param $path * @return mixed */ function abspath_to_relative_path($path) { return str_replace(w3_get_document_root(), '', $path); }
/** * Imports library * * @param integer $limit * @param integer $offset * @param integer $count * @param integer $total * @param array $results * @return boolean */ function import_library($limit, $offset, &$count, &$total, &$results) { global $wpdb; $count = 0; $total = 0; $results = array(); $upload_info = w3_upload_info(); $uploads_use_yearmonth_folders = get_option('uploads_use_yearmonth_folders'); $document_root = w3_get_document_root(); @set_time_limit($this->_config->get_integer('timelimit.cdn_import')); if ($upload_info) { /** * Search for posts with links or images */ $sql = sprintf('SELECT ID, post_content, post_date FROM %sposts WHERE post_status = "publish" AND (post_type = "post" OR post_type = "page") AND (post_content LIKE "%%src=%%" OR post_content LIKE "%%href=%%") ', $wpdb->prefix); if ($limit) { $sql .= sprintf(' LIMIT %d', $limit); if ($offset) { $sql .= sprintf(' OFFSET %d', $offset); } } $posts = $wpdb->get_results($sql); if ($posts) { $count = count($posts); $total = $this->get_import_posts_count(); $regexp = '~(' . $this->get_regexp_by_mask($this->_config->get_string('cdn.import.files')) . ')$~'; $import_external = $this->_config->get_boolean('cdn.import.external'); foreach ($posts as $post) { $matches = null; $replaced = array(); $attachments = array(); $post_content = $post->post_content; /** * Search for all link and image sources */ if (preg_match_all('~(href|src)=[\'"]?([^\'"<>\\s]+)[\'"]?~', $post_content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { list($search, $attribute, $origin) = $match; /** * Check if $search is already replaced */ if (isset($replaced[$search])) { continue; } $error = ''; $result = false; $src = w3_normalize_file_minify($origin); $dst = ''; /** * Check if file exists in the library */ if (stristr($origin, $upload_info['baseurl']) === false) { /** * Check file extension */ $check_src = $src; if (w3_is_url($check_src)) { $qpos = strpos($check_src, '?'); if ($qpos !== false) { $check_src = substr($check_src, 0, $qpos); } } if (preg_match($regexp, $check_src)) { /** * Check for already uploaded attachment */ if (isset($attachments[$src])) { list($dst, $dst_url) = $attachments[$src]; $result = true; } else { if ($uploads_use_yearmonth_folders) { $upload_subdir = date('Y/m', strtotime($post->post_date)); $upload_dir = sprintf('%s/%s', $upload_info['basedir'], $upload_subdir); $upload_url = sprintf('%s/%s', $upload_info['baseurl'], $upload_subdir); } else { $upload_subdir = ''; $upload_dir = $upload_info['basedir']; $upload_url = $upload_info['baseurl']; } $src_filename = pathinfo($src, PATHINFO_FILENAME); $src_extension = pathinfo($src, PATHINFO_EXTENSION); /** * Get available filename */ for ($i = 0;; $i++) { $dst = sprintf('%s/%s%s%s', $upload_dir, $src_filename, $i ? $i : '', $src_extension ? '.' . $src_extension : ''); if (!file_exists($dst)) { break; } } $dst_basename = basename($dst); $dst_url = sprintf('%s/%s', $upload_url, $dst_basename); $dst_path = ltrim(str_replace($document_root, '', w3_path($dst)), '/'); if ($upload_subdir) { w3_mkdir($upload_subdir, 0777, $upload_info['basedir']); } $download_result = false; /** * Check if file is remote URL */ if (w3_is_url($src)) { /** * Download file */ if ($import_external) { $download_result = w3_download($src, $dst); if (!$download_result) { $error = 'Unable to download file'; } } else { $error = 'External file import is disabled'; } } else { /** * Otherwise copy file from local path */ $src_path = $document_root . '/' . urldecode($src); if (file_exists($src_path)) { $download_result = @copy($src_path, $dst); if (!$download_result) { $error = 'Unable to copy file'; } } else { $error = 'Source file doesn\'t exists'; } } /** * Check if download or copy was successful */ if ($download_result) { w3_require_once(W3TC_INC_DIR . '/functions/mime.php'); $title = $dst_basename; $guid = ltrim($upload_info['baseurlpath'] . $title, ','); $mime_type = w3_get_mime_type($dst); $GLOBALS['wp_rewrite'] = new WP_Rewrite(); /** * Insert attachment */ $id = wp_insert_attachment(array('post_mime_type' => $mime_type, 'guid' => $guid, 'post_title' => $title, 'post_content' => '', 'post_parent' => $post->ID), $dst); if (!is_wp_error($id)) { /** * Generate attachment metadata and upload to CDN */ require_once ABSPATH . 'wp-admin/includes/image.php'; wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $dst)); $attachments[$src] = array($dst, $dst_url); $result = true; } else { $error = 'Unable to insert attachment'; } } } /** * If attachment was successfully created then replace links */ if ($result) { $replace = sprintf('%s="%s"', $attribute, $dst_url); // replace $search with $replace $post_content = str_replace($search, $replace, $post_content); $replaced[$search] = $replace; $error = 'OK'; } } else { $error = 'File type rejected'; } } else { $error = 'File already exists in the media library'; } /** * Add new entry to the log file */ $results[] = array('src' => $src, 'dst' => $dst_path, 'result' => $result, 'error' => $error); } } /** * If post content was chenged then update DB */ if ($post_content != $post->post_content) { wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content)); } } } } }
function w3_normalize_file_minify($file) { global $wp_rewrite; $hmwp = new HideMyWP(); $hmwp->init(); $hmwp->add_rewrite_rules($wp_rewrite); $file = $hmwp->reverse_partial_filter($file); if (w3_is_url($file)) { if (strstr($file, '?') === false) { $domain_url_regexp = '~' . w3_get_domain_url_regexp() . '~i'; $file = preg_replace($domain_url_regexp, '', $file); } } if (!w3_is_url($file)) { $file = w3_path($file); $file = str_replace(w3_get_document_root(), '', $file); $file = ltrim($file, '/'); } return $file; }
/** * URL file filter * * @param string $file * @return bool */ public function is_file_for_minification($file) { static $external; $ext = strrchr($file, '.'); if ($ext != '.js' && $ext != '.css') { return false; } if (!isset($external)) { $external = $this->config->get_array('minify.cache.files'); } foreach ($external as $ext) { if (preg_match('#' . w3_get_url_regexp($ext) . '#', $file)) { return true; } } if (w3_is_url($file)) { return false; } /** @var W3_MinifyFileTool $file_tool */ $file_tool = w3_instance('W3_MinifyFileTool'); $file_tool->setDocumentRoot(w3_get_document_root()); if (!$file_tool->fileExists($file)) { return false; } return true; }
/** * Paths used to minify minifyfile paths * @return array */ private function _minify_path_replacements() { $theme = get_theme_root(); return array(ltrim(str_replace(w3_get_document_root(), '', w3_path($theme)), '/'), ltrim(str_replace(w3_get_document_root(), '', w3_path(WP_PLUGIN_DIR)), '/'), ltrim(str_replace(w3_get_document_root(), '', w3_path(WPMU_PLUGIN_DIR)), '/'), WPINC . '/js/jquery', WPINC . '/js', WPINC . '/css', WPINC); }
/** * Normalizes file name for minify * * Relative to document root! * * @param string $file * @return string */ function w3_normalize_file_minify($file) { if (w3_is_url($file)) { if (strstr($file, '?') === false) { $domain_url_regexp = '~' . w3_get_domain_url_regexp() . '~i'; $file = preg_replace($domain_url_regexp, '', $file); } } if (!w3_is_url($file)) { $file = w3_path($file); $file = str_replace(w3_get_document_root(), '', $file); $file = ltrim($file, '/'); } return $file; }
/** * Exports custom files to CDN * * @return array */ function get_files_custom() { $files = array(); $document_root = w3_get_document_root(); $custom_files = $this->_config->get_array('cdn.custom.files'); $custom_files = array_map('w3_parse_path', $custom_files); foreach ($custom_files as $custom_file) { if ($custom_file != '') { $custom_file = w3_normalize_file($custom_file); $dir = trim(dirname($custom_file), '/\\'); if ($dir == '.') { $dir = ''; } $mask = basename($custom_file); $files = array_merge($files, $this->search_files($document_root . '/' . $dir, $dir, $mask)); } } return $files; }
public function w3tc_loader_file_data() { $filename = W3TC_WP_LOADER; $data = "\n<?php\n if (W3TC_WP_LOADING)\n require_once '" . w3_get_document_root() . '/' . trim(w3_get_site_path(), "/") . "/wp-load.php';\n"; return array('filename' => $filename, 'data' => $data); }
/** * Paths used to minify minifyfile paths * @return array */ private function _minify_path_replacements() { if (w3_is_network()) { $theme = get_theme_root(); } else { $theme = get_stylesheet_directory(); } return array(ltrim(str_replace(w3_get_document_root(), '', w3_path($theme)), '/'), ltrim(str_replace(w3_get_document_root(), '', w3_path(WP_PLUGIN_DIR)), '/'), ltrim(str_replace(w3_get_document_root(), '', w3_path(WPMU_PLUGIN_DIR)), '/'), WPINC . '/js/jquery', WPINC . '/js', WPINC . '/css', WPINC); }
/** * Exports custom files to CDN * * @return array */ function get_files_custom() { $files = array(); $document_root = w3_get_document_root(); $custom_files = $this->_config->get_array('cdn.custom.files'); $custom_files = array_map('w3_parse_path', $custom_files); $site_root = w3_get_site_root(); $path = w3_get_site_path(); if (strstr(WP_CONTENT_DIR, w3_get_site_root()) === false) { $site_root = w3_get_document_root(); $path = ''; } $content_path = trim(substr(WP_CONTENT_DIR, strlen($site_root)), '/\\'); $wp_content_folder = basename(WP_CONTENT_DIR); foreach ($custom_files as $custom_file) { if ($custom_file != '') { $custom_file = w3_normalize_file($custom_file); if (!w3_is_multisite()) { $dir = trim(dirname($path . $custom_file), '/\\'); $rel_path = trim(dirname($custom_file), '/\\'); } else { $rel_path = $dir = trim(dirname($custom_file), '/\\'); } if (strpos($dir, '<currentblog>') != false) { $rel_path = $dir = str_replace('<currentblog>', 'blogs.dir/' . w3_get_blog_id(), $dir); } if ($content_path && $content_path != $wp_content_folder) { $dir = str_replace($wp_content_folder, $content_path, $dir); $rel_path = str_replace($wp_content_folder, $content_path, $rel_path); } if ($dir == '.') { $rel_path = $dir = ''; } $mask = basename($custom_file); $files = array_merge($files, $this->search_files($document_root . '/' . $dir, $rel_path, $mask)); } } return $files; }
private function _replace_folder_placeholders($file) { static $content_dir, $plugin_dir, $upload_dir; if (empty($content_dir)) { $content_dir = str_replace(w3_get_document_root(), '', WP_CONTENT_DIR); $content_dir = trim($content_dir, '/'); if (defined('WP_PLUGIN_DIR')) { $plugin_dir = str_replace(w3_get_document_root(), '', WP_PLUGIN_DIR); $plugin_dir = trim($plugin_dir, '/'); } else { $plugin_dir = str_replace(w3_get_document_root(), '', WP_CONTENT_DIR . '/plugins'); $plugin_dir = trim($plugin_dir, '/'); } $upload_dir = wp_upload_dir(); $upload_dir = str_replace(w3_get_document_root(), '', $upload_dir['basedir']); $upload_dir = trim($upload_dir, '/'); } $file = str_replace('{wp_content_dir}', $content_dir, $file); $file = str_replace('{plugins_dir}', $plugin_dir, $file); $file = str_replace('{uploads_dir}', $upload_dir, $file); return $file; }
/** * Returns blog path * * @return string */ function w3_get_site_path() { $document_root = w3_get_document_root(); $path = str_replace($document_root, '', w3_path(ABSPATH)); $path = '/' . ltrim($path, '/'); if (substr($path, -1) != '/') { $path .= '/'; } return $path; }
/** * Returns server info * * @return array */ function get_server_info() { global $wp_version, $wp_db_version, $wpdb; $wordpress_plugins = get_plugins(); $wordpress_plugins_active = array(); foreach ($wordpress_plugins as $wordpress_plugin_file => $wordpress_plugin) { if (is_plugin_active($wordpress_plugin_file)) { $wordpress_plugins_active[$wordpress_plugin_file] = $wordpress_plugin; } } $mysql_version = $wpdb->get_var('SELECT VERSION()'); $mysql_variables_result = (array) $wpdb->get_results('SHOW VARIABLES', ARRAY_N); $mysql_variables = array(); foreach ($mysql_variables_result as $mysql_variables_row) { $mysql_variables[$mysql_variables_row[0]] = $mysql_variables_row[1]; } $server_info = array('w3tc' => array('version' => W3TC_VERSION, 'server' => !empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'Unknown', 'dir' => W3TC_DIR, 'cache_dir' => W3TC_CACHE_DIR, 'blog_id' => w3_get_blog_id(), 'document_root' => w3_get_document_root(), 'home_root' => w3_get_home_root(), 'site_root' => w3_get_site_root(), 'base_path' => w3_get_base_path(), 'home_path' => w3_get_home_path(), 'site_path' => w3_get_site_path()), 'wp' => array('version' => $wp_version, 'db_version' => $wp_db_version, 'abspath' => ABSPATH, 'home' => get_option('home'), 'siteurl' => get_option('siteurl'), 'email' => get_option('admin_email'), 'upload_info' => (array) w3_upload_info(), 'theme' => w3tc_get_current_theme(), 'wp_cache' => defined('WP_CACHE') && WP_CACHE ? 'true' : 'false', 'plugins' => $wordpress_plugins_active), 'mysql' => array('version' => $mysql_version, 'variables' => $mysql_variables)); return $server_info; }
/** * Generates directives for file cache dir * * @param W3_Config $config * @return string */ private function rules_cache_generate_nginx($config) { $cache_root = w3_path(W3TC_CACHE_PAGE_ENHANCED_DIR); $cache_dir = rtrim(str_replace(w3_get_document_root(), '', $cache_root), '/'); if (w3_is_network()) { $cache_dir = preg_replace('~/w3tc.*?/~', '/w3tc.*?/', $cache_dir, 1); } $browsercache = $config->get_boolean('browsercache.enabled'); $compression = $browsercache && $config->get_boolean('browsercache.html.compression'); $expires = $browsercache && $config->get_boolean('browsercache.html.expires'); $lifetime = $browsercache ? $config->get_integer('browsercache.html.lifetime') : 0; $cache_control = $browsercache && $config->get_boolean('browsercache.html.cache.control'); $w3tc = $browsercache && $config->get_integer('browsercache.html.w3tc'); $common_rules = ''; if ($expires) { $common_rules .= " expires modified " . $lifetime . "s;\n"; } if ($w3tc) { $common_rules .= " add_header X-Powered-By \"" . W3TC_POWERED_BY . "\";\n"; } if ($compression) { $common_rules .= " add_header Vary \"Accept-Encoding, Cookie\";\n"; } else { $common_rules .= " add_header Vary Cookie;\n"; } if ($cache_control) { $cache_policy = $config->get_string('browsercache.html.cache.policy'); switch ($cache_policy) { case 'cache': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"public\";\n"; break; case 'cache_public_maxage': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n"; break; case 'cache_validation': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n"; break; case 'cache_noproxy': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"private, must-revalidate\";\n"; break; case 'cache_maxage': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n"; break; case 'no_cache': $common_rules .= " add_header Pragma \"no-cache\";\n"; $common_rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n"; break; } } $rules = ''; $rules .= W3TC_MARKER_BEGIN_PGCACHE_CACHE . "\n"; $rules .= "location ~ " . $cache_dir . ".*html\$ {\n"; $rules .= $common_rules; $rules .= "}\n"; if ($compression) { $rules .= "location ~ " . $cache_dir . ".*gzip\$ {\n"; $rules .= " gzip off;\n"; $rules .= " types {}\n"; $rules .= " default_type text/html;\n"; $rules .= $common_rules; $rules .= " add_header Content-Encoding gzip;\n"; $rules .= "}\n"; } $rules .= W3TC_MARKER_END_PGCACHE_CACHE . "\n"; return $rules; }
/** * CDN Test action * * @return void */ function action_cdn_test() { w3_require_once(W3TC_LIB_W3_DIR . '/Request.php'); w3_require_once(W3TC_LIB_W3_DIR . '/Cdn.php'); $engine = W3_Request::get_string('engine'); $config = W3_Request::get_array('config'); //TODO: Workaround to support test case cdn/a04 if (!isset($config['host'])) { $config = W3_Request::get_string('config'); $config = json_decode($config, true); } $config = array_merge($config, array('debug' => false)); if (!is_array($config['domain'])) { $config['domain'] = explode(',', $config['domain']); } if (w3_is_cdn_engine($engine)) { $result = true; $error = null; } else { $result = false; $error = __('Incorrect engine.', 'w3-total-cache'); } if (!isset($config['docroot'])) { $config['docroot'] = w3_get_document_root(); } if ($result) { $w3_cdn = W3_Cdn::instance($engine, $config); @set_time_limit($this->_config->get_integer('timelimit.cdn_test')); if ($w3_cdn->test($error)) { $result = true; $error = __('Test passed', 'w3-total-cache'); } else { $result = false; $error = sprintf(__('Error: %s', 'w3-total-cache'), $error); } } $response = array('result' => $result, 'error' => $error); echo json_encode($response); }
/** * URL file filter * * @param string $file * @return bool */ function _filter_files($file) { if (w3_is_url($file)) { return false; } $ext = strrchr($file, '.'); if ($ext != '.js' && $ext != '.css') { return false; } $path = w3_get_document_root() . '/' . $file; if (!file_exists($path)) { return false; } return true; }
/** * Returns array of custom sources * * @param string $hash * @param string $type * @return array */ function get_sources_custom($hash, $type) { $sources = array(); $files = $this->get_custom_files($hash, $type); if (count($files)) { $document_root = w3_get_document_root(); foreach ($files as $file) { $sources[] = $document_root . '/' . $file; } } return $sources; }
/** * URL file filter * * @param string $file * @return bool */ function _filter_files($file) { static $external; $ext = strrchr($file, '.'); if ($ext != '.js' && $ext != '.css') { return false; } if (!isset($external)) { $external = $this->_config->get_array('minify.cache.files'); } foreach ($external as $ext) { if (preg_match('#' . w3_get_url_regexp($ext) . '#', $file)) { return true; } } if (w3_is_url($file)) { return false; } $path = w3_get_document_root() . '/' . $file; if (!file_exists($path)) { return false; } return true; }
/** * Paths used to minify minifyfile paths * @return array */ private function _minify_path_replacements() { return array(ltrim(str_replace(w3_get_document_root(), '', w3_path(get_stylesheet_directory())), '/'), ltrim(str_replace(w3_get_document_root(), '', w3_path(WP_PLUGIN_DIR)), '/'), ltrim(str_replace(w3_get_document_root(), '', w3_path(WPMU_PLUGIN_DIR)), '/'), WPINC . '/js/jquery', WPINC . '/js', WPINC . '/css', WPINC); }