Example #1
0
/**
 * Returns real path of given path
 *
 * @param string $path
 * @return string
 */
function w3_realpath($path)
{
    $path = w3_path($path);
    $parts = explode('/', $path);
    $absolutes = array();
    foreach ($parts as $part) {
        if ('.' == $part) {
            continue;
        }
        if ('..' == $part) {
            array_pop($absolutes);
        } else {
            $absolutes[] = $part;
        }
    }
    return implode('/', $absolutes);
}
Example #2
0
 /**
  * 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);
 }
 /**
  * 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));
                 }
             }
         }
     }
 }
 /**
  * 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;
 }
Example #5
0
 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;
 }
Example #6
0
            ?>

            <?php 
        }
        ?>

        </li>
        <?php 
    }
}
?>


        <li>
            <?php 
echo w3_path(WP_CONTENT_DIR);
?>
:
            <?php 
if (w3_is_writable_dir(WP_CONTENT_DIR)) {
    ?>

            <code>OK</code>
            <?php 
} else {
    ?>

            <code>Not write-able</code>
            <?php 
}
?>
Example #7
0
 /**
  * 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);
 }
Example #8
0
 /**
  * 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')))) {
         require_once W3TC_INC_DIR . '/functions/http.php';
         $minify =& w3_instance('W3_Plugin_Minify');
         $document_root = w3_get_document_root();
         $site_root = w3_get_site_root();
         $minify_root = w3_path(W3TC_CACHE_FILE_MINIFY_DIR);
         $minify_path = ltrim(str_replace($site_root, rtrim(w3_get_site_path(), '/'), $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;
 }
Example #9
0
 /**
  * Exports theme to CDN
  *
  * @return array
  */
 function get_files_theme()
 {
     /**
      * If mobile or referrer support enabled
      * we should upload whole themes directory
      */
     if ($this->_config->get_boolean('mobile.enabled') || $this->_config->get_boolean('referrer.enabled')) {
         $themes_root = get_theme_root();
     } else {
         $themes_root = get_stylesheet_directory();
     }
     $themes_root = w3_path($themes_root);
     $site_root = w3_get_document_root();
     $themes_path = ltrim(str_replace($site_root, '', $themes_root), '/');
     $files = $this->search_files($themes_root, $themes_path, $this->_config->get_string('cdn.theme.files'));
     return $files;
 }
Example #10
0
/**
 * Converts file path to relative 
 * 
 * @param string $file
 * @return string
 */
function w3_normalize_file($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);
        }
    } else {
        $abspath = w3_path(ABSPATH);
        $file = w3_path($file);
        $file = str_replace($abspath, '', $file);
    }
    $file = ltrim($file, '/');
    return $file;
}
Example #11
0
                <code>Not write-able</code>
                <?php 
            }
            ?>
            <?php 
        }
        ?>
        </li>
        <?php 
    }
}
?>

        <li>
            <?php 
echo w3_path(nxt_CONTENT_DIR);
?>
:
            <?php 
if (w3_is_writable_dir(nxt_CONTENT_DIR)) {
    ?>
            <code>OK</code>
            <?php 
} else {
    ?>
            <code>Not write-able</code>
            <?php 
}
?>
        </li>
 /**
  * Generates rules for WP dir
  *
  * @return string
  */
 function generate_rules_core()
 {
     global $w3_reserved_blognames;
     /**
      * Auto reject cookies
      */
     $reject_cookies = array('comment_author', 'wp-postpass');
     /**
      * Auto reject URIs
      */
     $reject_uris = array('\\/wp-admin\\/', '\\/xmlrpc.php', '\\/wp-(app|cron|login|register).php');
     /**
      * Reject cache for logged in users
      */
     if ($this->_config->get_boolean('pgcache.reject.logged')) {
         $reject_cookies = array_merge($reject_cookies, array('wordpress_[a-f0-9]+', 'wordpress_logged_in'));
     }
     /**
      * Reject cache for home page
      */
     if (!$this->_config->get_boolean('pgcache.cache.home')) {
         $reject_uris[] = '^(\\/|\\/index.php)$';
     }
     /**
      * Reject cache for feeds
      */
     if (!$this->_config->get_boolean('pgcache.cache.feed')) {
         $reject_uris[] = 'feed';
     }
     /**
      * Custom config
      */
     $reject_cookies = array_merge($reject_cookies, $this->_config->get_array('pgcache.reject.cookie'));
     $reject_uris = array_merge($reject_uris, $this->_config->get_array('pgcache.reject.uri'));
     $reject_user_agents = $this->_config->get_array('pgcache.reject.ua');
     $accept_files = $this->_config->get_array('pgcache.accept.files');
     /**
      * WPMU support
      */
     $is_wpmu = w3_is_wpmu();
     $is_vhost = w3_is_vhost();
     /**
      * Generate directives
      */
     $rules = '';
     $rules .= "# BEGIN W3 Total Cache\n";
     $setenvif_rules = '';
     if ($is_wpmu) {
         $setenvif_rules .= "    SetEnvIfNoCase Host ^(www\\.)?([a-z0-9\\-\\.]+\\.[a-z]+)\\.?(:[0-9]+)?\$ DOMAIN=\$2\n";
         if (!$is_vhost) {
             $setenvif_rules .= "    SetEnvIfNoCase Request_URI ^" . w3_get_site_path() . "([a-z0-9\\-]+)/ BLOGNAME=\$1\n";
         }
     }
     $compression = $this->_config->get_string('pgcache.compression');
     if ($compression != '') {
         $compressions = array();
         if (stristr($compression, 'gzip') !== false) {
             $compressions[] = 'gzip';
         }
         if (stristr($compression, 'deflate') !== false) {
             $compressions[] = 'deflate';
         }
         if (count($compressions)) {
             $setenvif_rules .= "    SetEnvIfNoCase Accept-Encoding (" . implode('|', $compressions) . ") APPEND_EXT=.\$1\n";
         }
     }
     if ($setenvif_rules != '') {
         $rules .= "<IfModule mod_setenvif.c>\n" . ($setenvif_rules .= "</IfModule>\n");
     }
     $rules .= "<IfModule mod_rewrite.c>\n";
     $rules .= "    RewriteEngine On\n";
     $mobile_redirect = $this->_config->get_string('pgcache.mobile.redirect');
     if ($mobile_redirect != '') {
         $mobile_agents = $this->_config->get_array('pgcache.mobile.agents');
         $rules .= "    RewriteCond %{HTTP_USER_AGENT} (" . implode('|', array_map('w3_preg_quote', $mobile_agents)) . ") [NC]\n";
         $rules .= "    RewriteRule .* " . $mobile_redirect . " [R,L]\n";
     }
     $rules .= "    RewriteCond %{REQUEST_URI} \\/\$\n";
     $rules .= "    RewriteCond %{REQUEST_URI} !(" . implode('|', $reject_uris) . ")";
     if (count($accept_files)) {
         $rules .= " [OR]\n    RewriteCond %{REQUEST_URI} (" . implode('|', array_map('w3_preg_quote', $accept_files)) . ") [NC]\n";
     } else {
         $rules .= "\n";
     }
     $rules .= "    RewriteCond %{REQUEST_METHOD} !=POST\n";
     $rules .= "    RewriteCond %{QUERY_STRING} =\"\"\n";
     $rules .= "    RewriteCond %{HTTP_COOKIE} !(" . implode('|', array_map('w3_preg_quote', $reject_cookies)) . ") [NC]\n";
     if (count($reject_user_agents)) {
         $rules .= "    RewriteCond %{HTTP_USER_AGENT} !(" . implode('|', array_map('w3_preg_quote', $reject_user_agents)) . ") [NC]\n";
     }
     if ($is_wpmu) {
         if ($is_vhost) {
             $replacement = '/w3tc-%{ENV:DOMAIN}/';
         } else {
             $rules .= "    RewriteCond %{ENV:BLOGNAME} !^(" . implode('|', $w3_reserved_blognames) . ")\$\n";
             $rules .= "    RewriteCond %{ENV:BLOGNAME} !-f\n";
             $rules .= "    RewriteCond %{ENV:BLOGNAME} !-d\n";
             $replacement = '/w3tc-%{ENV:BLOGNAME}.%{ENV:DOMAIN}/';
         }
         $cache_dir = preg_replace('~/w3tc.*/~U', $replacement, W3TC_CACHE_FILE_PGCACHE_DIR, 1);
     } else {
         $cache_dir = W3TC_CACHE_FILE_PGCACHE_DIR;
     }
     $rules .= "    RewriteCond " . w3_path($cache_dir) . "/\$1/_default_.html%{ENV:APPEND_EXT} -f\n";
     $rules .= "    RewriteRule (.*) " . str_replace(ABSPATH, '', $cache_dir) . "/\$1/_default_.html%{ENV:APPEND_EXT} [L]\n";
     $rules .= "</IfModule>\n";
     $rules .= "# END W3 Total Cache\n\n";
     if (!$this->check_rules_wp()) {
         $rules .= "# BEGIN WordPress\n";
         $rules .= "<IfModule mod_rewrite.c>\n";
         $rules .= "    RewriteEngine On\n";
         $rules .= "    RewriteCond %{REQUEST_FILENAME} !-f\n";
         $rules .= "    RewriteCond %{REQUEST_FILENAME} !-d\n";
         $rules .= "    RewriteRule .* index.php [L]\n";
         $rules .= "</IfModule>\n";
         $rules .= "# END WordPress\n\n";
     }
     return $rules;
 }
Example #13
0
 /**
  * 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);
 }