Ejemplo n.º 1
0
 private static function _processUriCB($m)
 {
     // $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
     $isImport = $m[0][0] === '@';
     // determine URI and the quote character (if any)
     if ($isImport) {
         $quoteChar = $m[1];
         $uri = $m[2];
     } else {
         // $m[1] is either quoted or not
         $quoteChar = $m[1][0] === "'" || $m[1][0] === '"' ? $m[1][0] : '';
         $uri = $quoteChar === '' ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2);
     }
     // analyze URI
     if (false === strpos($uri, '//') && 0 !== strpos($uri, 'data:')) {
         // prepend
         if (self::$_prependRelativePath) {
             if (w3_is_url(self::$_prependRelativePath)) {
                 $parse_url = @parse_url(self::$_prependRelativePath);
                 if ($parse_url && isset($parse_url['host'])) {
                     $scheme = $parse_url['scheme'];
                     $host = $parse_url['host'];
                     $port = isset($parse_url['port']) && $parse_url['port'] != 80 ? ':' . (int) $parse_url['port'] : '';
                     $path = !empty($parse_url['path']) ? $parse_url['path'] : '/';
                     $dir_css = preg_replace('~[^/]+$~', '', $path);
                     $dir_obj = preg_replace('~[^/]+$~', '', $uri);
                     $dir = ltrim(strpos($dir_obj, '/') === 0 ? w3_realpath($dir_obj) : w3_realpath($dir_css . $dir_obj), '/');
                     $file = basename($uri);
                     $uri = sprintf('%s://%s%s/%s/%s', $scheme, $host, $port, $dir, $file);
                 }
             } else {
                 $uri = self::$_prependRelativePath . $uri;
             }
         } else {
             $uri = self::_rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks);
             if (self::$_prependAbsolutePath) {
                 $prependAbsolutePath = self::$_prependAbsolutePath;
             } elseif (self::$_prependAbsolutePathCallback) {
                 $prependAbsolutePath = call_user_func(self::$_prependAbsolutePathCallback, $uri);
             } else {
                 $prependAbsolutePath = '';
             }
             if ($prependAbsolutePath) {
                 $uri = rtrim($prependAbsolutePath, '/') . $uri;
             }
         }
         if (self::$_browserCacheId && count(self::$_browserCacheExtensions)) {
             $matches = null;
             if (preg_match('~\\.([a-z-_]+)(\\?.*)?$~', $uri, $matches)) {
                 $extension = $matches[1];
                 $query = isset($matches[2]) ? $matches[2] : '';
                 if ($extension && in_array($extension, self::$_browserCacheExtensions)) {
                     $uri = w3_remove_query($uri);
                     $uri .= ($query ? '&' : '?') . self::$_browserCacheId;
                 }
             }
         }
     }
     return $isImport ? "@import {$quoteChar}{$uri}{$quoteChar}" : "url({$quoteChar}{$uri}{$quoteChar})";
 }
Ejemplo n.º 2
0
/**
 * Translates remote file to local file
 *
 * @param string $file
 * @return string
 */
function w3_translate_file($file)
{
    if (!w3_is_url($file)) {
        $file = '/' . ltrim($file, '/');
        $regexp = '~^' . w3_preg_quote(w3_get_site_path()) . '~';
        $file = preg_replace($regexp, w3_get_base_path(), $file);
        $file = ltrim($file, '/');
    }
    return $file;
}
Ejemplo n.º 3
0
 /**
  * Remove script tags from the source
  *
  * @param string $content
  * @param array $files
  * @return void
  */
 function remove_scripts(&$content, $files)
 {
     $regexps = array();
     $domain_url_regexp = w3_get_domain_url_regexp();
     foreach ($files as $file) {
         $this->replaced_scripts[] = $file;
         if (w3_is_url($file) && !preg_match('~' . $domain_url_regexp . '~i', $file)) {
             // external JS files
             $regexps[] = w3_preg_quote($file);
         } else {
             // local JS files
             $file = ltrim(preg_replace('~' . $domain_url_regexp . '~i', '', $file), '/\\');
             $regexps[] = '(' . $domain_url_regexp . ')?/?' . w3_preg_quote($file);
         }
     }
     foreach ($regexps as $regexp) {
         $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content);
     }
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
 /**
  * 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));
                 }
             }
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * Returns minify groups
  * @param string $group
  * @param string $type
  * @return array
  */
 function get_groups($group, $type)
 {
     $result = array();
     switch ($type) {
         case 'css':
             $groups = $this->_config->get_array('minify.css.groups');
             break;
         case 'js':
             $groups = $this->_config->get_array('minify.js.groups');
             break;
         default:
             return $result;
     }
     if (isset($groups['default'])) {
         $locations = (array) $groups['default'];
     } else {
         $locations = array();
     }
     if ($group != 'default' && isset($groups[$group])) {
         $locations = array_merge_recursive($locations, (array) $groups[$group]);
     }
     foreach ($locations as $location => $config) {
         if (!empty($config['files'])) {
             foreach ((array) $config['files'] as $file) {
                 if (w3_is_url($file)) {
                     if ($precached_file = $this->_precache_file($file, $type)) {
                         $result[$location][$file] = $precached_file;
                     }
                 } else {
                     if (w3_get_blog_id()) {
                         // for WPMU we have to remove blog path
                         $file = str_replace(w3_get_site_path(), '', $file);
                     }
                     $result[$location][$file] = '//' . $file;
                 }
             }
         }
     }
     return $result;
 }
Ejemplo n.º 7
0
 /**
  * Returns minify groups
  *
  * @param string $theme
  * @param string $template
  * @param string $type
  * @return array
  */
 function get_groups($theme, $template, $type)
 {
     $result = array();
     switch ($type) {
         case 'css':
             $groups = $this->_config->get_array('minify.css.groups');
             break;
         case 'js':
             $groups = $this->_config->get_array('minify.js.groups');
             break;
         default:
             return $result;
     }
     if (isset($groups[$theme]['default'])) {
         $locations = (array) $groups[$theme]['default'];
     } else {
         $locations = array();
     }
     if ($template != 'default' && isset($groups[$theme][$template])) {
         $locations = array_merge_recursive($locations, (array) $groups[$theme][$template]);
     }
     foreach ($locations as $location => $config) {
         if (!empty($config['files'])) {
             foreach ((array) $config['files'] as $file) {
                 $file = w3_normalize_file_minify2($file);
                 if (w3_is_url($file)) {
                     $precached_file = $this->_precache_file($file, $type);
                     if ($precached_file) {
                         $result[$location][$file] = $precached_file;
                     } else {
                         $this->error(sprintf('Unable to cache remote file: "%s"', $file));
                     }
                 } else {
                     $path = w3_get_document_root() . '/' . $file;
                     if (file_exists($path)) {
                         $result[$location][$file] = '//' . $file;
                     } else {
                         $this->error(sprintf('File "%s" doesn\'t exist', $path));
                     }
                 }
             }
         }
     }
     return $result;
 }
 /**
  * Cleans scripts
  *
  * @param string $content
  * @return string
  */
 function clean_scripts($content)
 {
     $regexps = array();
     $groups = $this->_config->get_array('minify.js.groups');
     $domain_url_regexp = w3_get_domain_url_regexp();
     foreach ($groups as $group => $locations) {
         foreach ((array) $locations as $location => $config) {
             if (!empty($config['files'])) {
                 foreach ((array) $config['files'] as $file) {
                     if (w3_is_url($file) && !preg_match('~' . $domain_url_regexp . '~i', $file)) {
                         // external JS files
                         $regexps[] = w3_preg_quote($file);
                     } else {
                         // local JS files
                         $file = ltrim(preg_replace('~' . $domain_url_regexp . '~i', '', $file), '/\\');
                         $regexps[] = '(' . $domain_url_regexp . ')?/?' . w3_preg_quote($file);
                     }
                 }
             }
         }
     }
     foreach ($regexps as $regexp) {
         $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~is', '', $content);
     }
     return $content;
 }
Ejemplo n.º 9
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;
 }
Ejemplo n.º 10
0
 /**
  * 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();
     $site_url = w3_get_site_url();
     $upload_info = w3_upload_info();
     if ($upload_info) {
         $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;
                 $post_content = $post->post_content;
                 if (preg_match_all('~(href|src)=[\'"]?([^\'"<>\\s]+)[\'"]?~', $post_content, $matches, PREG_SET_ORDER)) {
                     foreach ($matches as $match) {
                         $src = w3_normalize_file($match[2]);
                         if (preg_match('~(' . $regexp . ')$~', $src)) {
                             $src_dir = date('Y/m', strtotime($post->post_date));
                             $src_base = basename($src);
                             $dst = sprintf('%s/%s/%s', $upload_info['upload_dir'], $src_dir, $src_base);
                             $dst_dir = dirname($dst);
                             $dst_path = ABSPATH . $dst;
                             $dst_url = sprintf('%s%s/%s/%s', $site_url, $upload_info['upload_url'], $src_dir, $src_base);
                             $result = false;
                             $error = '';
                             $download_result = null;
                             w3_mkdir($dst_dir, 0755, ABSPATH);
                             // file already exists
                             if (!file_exists($dst_path)) {
                                 // source is external URL
                                 if (w3_is_url($src)) {
                                     if ($import_external) {
                                         $download_result = $this->download($src, $dst_path);
                                     } else {
                                         $error = 'External file import is disabled';
                                     }
                                     // source is local file not in wp-content/uploads dir
                                 } elseif (strstr($src, $upload_info['upload_dir']) === false) {
                                     $src_path = ABSPATH . $src;
                                     $download_result = @copy($src_path, $dst_path);
                                     // file is already in wp-content/uploads dir
                                 } else {
                                     $error = 'Source file already exists';
                                 }
                                 if ($download_result !== null) {
                                     if ($download_result) {
                                         $title = $src_base;
                                         $guid = $upload_info['upload_url'] . '/' . $title;
                                         $mime_type = w3_get_mime_type($src_base);
                                         $GLOBALS['wp_rewrite'] =& new WP_Rewrite();
                                         $id = wp_insert_attachment(array('post_mime_type' => $mime_type, 'guid' => $guid, 'post_title' => $title, 'post_content' => ''), $dst_path);
                                         if (!is_wp_error($id)) {
                                             require_once ABSPATH . 'wp-admin/includes/image.php';
                                             wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $dst_path));
                                             $post_content = str_replace($src, $dst_url, $post_content);
                                             $result = true;
                                             $error = 'OK';
                                         } else {
                                             $error = 'Unable to insert attachment';
                                         }
                                     } else {
                                         $error = 'Unable to download file';
                                     }
                                 }
                             } else {
                                 $error = 'Destination file already exists';
                             }
                             $results[] = array('src' => $src, 'dst' => $dst, 'result' => $result, 'error' => $error);
                         }
                     }
                 }
                 if ($post_content != $post->post_content) {
                     wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content));
                 }
             }
         }
     }
 }
Ejemplo n.º 11
0
/**
 * Translates URL to local path
 * @param string $url
 * @return string
 */
function w3_translate_file($url)
{
    if (!w3_is_url($url)) {
        $url = w3_get_domain_url() . '/' . ltrim($url, '/\\');
    }
    $site_url_regexp = '~' . w3_get_site_url_regexp() . '~i';
    if (preg_match($site_url_regexp, $url) && strstr($url, '?') === false) {
        $url = preg_replace($site_url_regexp, '', $url);
        $url = w3_get_site_path() . ltrim($url, '/\\');
    }
    $url = ltrim($url, '/');
    return $url;
}
 /**
  * Returns minify groups
  * @param string $group
  * @param string $type
  * @return array
  */
 function get_groups($group, $type)
 {
     $result = array();
     switch ($type) {
         case 'css':
             $groups = $this->_config->get_array('minify.css.groups');
             break;
         case 'js':
             $groups = $this->_config->get_array('minify.js.groups');
             break;
         default:
             return $result;
     }
     if (isset($groups['default'])) {
         $locations = (array) $groups['default'];
     } else {
         $locations = array();
     }
     if ($group != 'default' && isset($groups[$group])) {
         $locations = array_merge_recursive($locations, (array) $groups[$group]);
     }
     $site_url = w3_get_site_url();
     foreach ($locations as $location => $config) {
         if (!empty($config['files'])) {
             foreach ((array) $config['files'] as $file) {
                 $file = $this->_normalize_file($file);
                 if (w3_is_url($file)) {
                     $precached_file = $this->_precache_file($file, $type);
                     if ($precached_file) {
                         $result[$location][$file] = $precached_file;
                     }
                 } else {
                     $result[$location][$file] = '//' . $file;
                 }
             }
         }
     }
     return $result;
 }
Ejemplo n.º 13
0
 /**
  * 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;
 }
Ejemplo n.º 14
0
 /**
  * Parses sitemap
  *
  * @param string $url
  * @return array
  */
 function parse_sitemap($url)
 {
     w3_require_once(W3TC_INC_DIR . '/functions/http.php');
     if (!w3_is_url($url)) {
         $url = home_url($url);
     }
     $urls = array();
     $response = w3_http_get($url);
     if (!is_wp_error($response) && $response['response']['code'] == 200) {
         $url_matches = null;
         $sitemap_matches = null;
         if (preg_match_all('~<sitemap>(.*?)</sitemap>~is', $response['body'], $sitemap_matches)) {
             $loc_matches = null;
             foreach ($sitemap_matches[1] as $sitemap_match) {
                 if (preg_match('~<loc>(.*?)</loc>~is', $sitemap_match, $loc_matches)) {
                     $loc = trim($loc_matches[1]);
                     if ($loc) {
                         $urls = array_merge($urls, $this->parse_sitemap($loc));
                     }
                 }
             }
         } elseif (preg_match_all('~<url>(.*?)</url>~is', $response['body'], $url_matches)) {
             $locs = array();
             $loc_matches = null;
             $priority_matches = null;
             foreach ($url_matches[1] as $url_match) {
                 $loc = '';
                 $priority = 0;
                 if (preg_match('~<loc>(.*?)</loc>~is', $url_match, $loc_matches)) {
                     $loc = trim($loc_matches[1]);
                 }
                 if (preg_match('~<priority>(.*?)</priority>~is', $url_match, $priority_matches)) {
                     $priority = (double) trim($priority_matches[1]);
                 }
                 if ($loc && $priority) {
                     $locs[$loc] = $priority;
                 }
             }
             arsort($locs);
             $urls = array_keys($locs);
         }
     }
     return $urls;
 }
Ejemplo n.º 15
0
 /**
  * Uncompresses a minify auto filename into an array of files.
  * @param $compressed
  * @param $type
  * @return array
  */
 function uncompress_minify_files($compressed, $type)
 {
     $no_type_files = array();
     $compressed = basename($compressed, '.' . $type);
     $uncompressed = $this->_uncompress(base64_decode(strtr($compressed, '-_', '+/')));
     $exploded = explode(',', $uncompressed);
     $replacements = $this->_minify_path_replacements();
     foreach ($exploded as $file) {
         if (!w3_is_url($file)) {
             $prefix = substr($file, 0, 1);
             $after_pre = substr($file, 1, 1);
             if (isset($replacements[$prefix]) && $after_pre == '/') {
                 $file = $replacements[$prefix] . substr($file, 1);
                 $no_type_files[] = $file;
             } else {
                 $no_type_files[] = $file;
             }
         } else {
             $no_type_files[] = $file;
         }
     }
     $files = array();
     foreach ($no_type_files as $no_type_file) {
         $file = !w3_is_url($no_type_file) ? $no_type_file . '.' . $type : $no_type_file;
         $verified = false;
         if (w3_is_url($file)) {
             $external = $this->_config->get_array('minify.cache.files');
             foreach ($external as $ext) {
                 if (preg_match('#' . w3_get_url_regexp($ext) . '#', $file) && !$verified) {
                     $verified = true;
                 }
             }
             if (!$verified) {
                 $this->error(sprintf('Remote file not in external files/libraries list: "%s"', $file));
             }
         } elseif (strpos($file, '..') != false || strpos($file, '//') !== false || strpos($file, '\\') !== false && strtoupper(substr(PHP_OS, 0, 3)) != 'WIN' || preg_match('/(?:^|[^\\.])\\.\\//', $file) || !preg_match('/^[a-zA-Z0-9_.\\/-]|[\\\\]+$/', $file)) {
             $verified = false;
             $this->error(sprintf('File path invalid: "%s"', $file));
         } else {
             $verified = true;
         }
         if ($verified) {
             $files[] = $file;
         }
     }
     return $files;
 }
Ejemplo n.º 16
0
 private static function _processUriCB($m)
 {
     // $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
     $isImport = $m[0][0] === '@';
     // determine URI and the quote character (if any)
     if ($isImport) {
         $quoteChar = $m[1];
         $uri = $m[2];
     } else {
         // $m[1] is either quoted or not
         $quoteChar = $m[1][0] === "'" || $m[1][0] === '"' ? $m[1][0] : '';
         $uri = $quoteChar === '' ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2);
     }
     // analyze URI
     if (false === strpos($uri, '//') && 0 !== strpos($uri, 'data:')) {
         if (self::$_prependPath !== null) {
             if (w3_is_url(self::$_prependPath)) {
                 $parse_url = @parse_url(self::$_prependPath);
                 if ($parse_url && isset($parse_url['host'])) {
                     $scheme = $parse_url['scheme'];
                     $host = $parse_url['host'];
                     $port = isset($parse_url['port']) && $parse_url['port'] != 80 ? ':' . (int) $parse_url['port'] : '';
                     $path = !empty($parse_url['path']) ? $parse_url['path'] : '/';
                     $dir_css = preg_replace('~[^/]+$~', '', $path);
                     $dir_obj = preg_replace('~[^/]+$~', '', $uri);
                     $dir = ltrim(strpos($dir_obj, '/') === 0 ? w3_realpath($dir_obj) : w3_realpath($dir_css . $dir_obj), '/');
                     $file = basename($uri);
                     $uri = sprintf('%s://%s%s/%s%s', $scheme, $host, $port, $dir, $file);
                 }
             } else {
                 $uri = self::$_prependPath . $uri;
             }
         } else {
             $uri = self::rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks);
         }
     }
     return $isImport ? "@import {$quoteChar}{$uri}{$quoteChar}" : "url({$quoteChar}{$uri}{$quoteChar})";
 }
Ejemplo n.º 17
0
 /**
  * Remove script tags from the source
  *
  * @param string $content
  * @param array $files
  * @return void
  */
 function remove_scripts(&$content, $files)
 {
     $regexps = array();
     $home_url_regexp = w3_get_home_url_regexp();
     $path = '';
     if (w3_is_network() && !w3_is_subdomain_install()) {
         $path = ltrim(w3_get_home_path(), '/');
     }
     foreach ($files as $file) {
         if ($path && strpos($file, $path) === 0) {
             $file = substr($file, strlen($path));
         }
         $this->replaced_scripts[] = $file;
         if (w3_is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) {
             // external JS files
             $regexps[] = w3_preg_quote($file);
         } else {
             // local JS files
             $file = ltrim($file, '/');
             if (ltrim(w3_get_site_path(), '/') && strpos($file, ltrim(w3_get_site_path(), '/')) === 0) {
                 $file = str_replace(ltrim(w3_get_site_path(), '/'), '', $file);
             }
             $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\');
             $regexps[] = '(' . $home_url_regexp . ')?/?' . w3_preg_quote($file);
         }
     }
     foreach ($regexps as $regexp) {
         $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content);
     }
 }
Ejemplo n.º 18
0
 /**
  * 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;
     }
     $path = w3_get_document_root() . '/' . $file;
     if (!file_exists($path)) {
         return false;
     }
     return true;
 }