/** * Pre-caches external file * * @param string $url * @param string $type * @return string */ function _precache_file($url, $type) { $lifetime = $this->_config->get_integer('minify.lifetime'); $cache_path = sprintf('%s/minify_%s.%s', w3_cache_blog_dir('minify'), md5($url), $type); if (!file_exists($cache_path) || @filemtime($cache_path) < time() - $lifetime) { w3_require_once(W3TC_INC_DIR . '/functions/http.php'); if (!@is_dir(dirname($cache_path))) { w3_require_once(W3TC_INC_DIR . '/functions/file.php'); w3_mkdir_from(dirname($cache_path), W3TC_CACHE_DIR); } w3_download($url, $cache_path); } return file_exists($cache_path) ? $this->_get_minify_source($cache_path, $url) : false; }
/** * 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)); } } } } }
/** * Pre-caches external file * * @param string $url * @param string $type * @return string */ function _precache_file($url, $type) { $lifetime = $this->_config->get_integer('minify.lifetime'); $cache_path = sprintf('%s/minify_%s.%s', W3TC_CACHE_FILE_MINIFY_DIR, md5($url), $type); if (!file_exists($cache_path) || @filemtime($cache_path) < time() - $lifetime) { require_once W3TC_INC_DIR . '/functions/http.php'; w3_download($url, $cache_path); } return file_exists($cache_path) ? $this->_get_minify_source($cache_path, $url) : false; }
/** * 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; }
/** * Exports min files to CDN */ function get_files_minify() { $files = array(); if (W3TC_PHP5) { require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php'; $minify =& W3_Plugin_Minify::instance(); $urls = $minify->get_urls(); foreach ($urls as $url) { $file = basename($url); if (w3_download($url, W3TC_CONTENT_MINIFY_DIR . '/' . $file) !== false) { $files[] = W3TC_CONTENT_MINIFY_DIR_NAME . '/' . $file; } } } return $files; }
/** * Precaches external file * * @param string $url * @param string $type * @return string */ function _precache_file($url, $type) { $lifetime = $this->_config->get_integer('minify.lifetime'); $file_path = sprintf('%s/minify_%s.%s', W3TC_CACHE_FILE_MINIFY_DIR, md5($url), $type); $file_exists = file_exists($file_path); if ($file_exists && @filemtime($file_path) >= time() - $lifetime) { return $this->_get_minify_source($file_path, $url); } if (is_dir(W3TC_CACHE_FILE_MINIFY_DIR)) { if (w3_download($url, $file_path) !== false) { return $this->_get_minify_source($file_path, $url); } else { $this->log(sprintf('Unable to download URL: %s', $url)); } } else { $this->log(sprintf('The cache directory %s does not exist', W3TC_CACHE_FILE_MINIFY_DIR)); } return $file_exists ? $this->_get_minify_source($file_path, $url) : false; }