= 5);
define('W3TC_WIN', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'));
defined('W3TC_DIR') || define('W3TC_DIR', realpath(dirname(__FILE__) . '/..'));
define('W3TC_FILE', 'w3-total-cache/w3-total-cache.php');
define('W3TC_LIB_DIR', W3TC_DIR . '/lib');
define('W3TC_LIB_W3_DIR', W3TC_LIB_DIR . '/W3');
define('W3TC_LIB_MINIFY_DIR', W3TC_LIB_DIR . '/Minify');
define('W3TC_LIB_CF_DIR', W3TC_LIB_DIR . '/CF');
define('W3TC_PLUGINS_DIR', W3TC_DIR . '/plugins');
define('W3TC_INSTALL_DIR', W3TC_DIR . '/wp-content');
define('W3TC_INSTALL_MINIFY_DIR', W3TC_INSTALL_DIR . '/w3tc/min');
define('W3TC_BLOGNAMES_PATH', WP_CONTENT_DIR . '/w3-total-cache-blognames.php');
define('W3TC_BLOGNAME', w3_get_blogname());
define('W3TC_SUFFIX', (W3TC_BLOGNAME != '' ? '-' . W3TC_BLOGNAME : ''));
defined('WP_CONTENT_DIR') || define('WP_CONTENT_DIR', realpath(W3TC_DIR . '/../..'));
define('WP_CONTENT_DIR_PATH', dirname(WP_CONTENT_DIR));
define('WP_CONTENT_DIR_NAME', basename(WP_CONTENT_DIR));
define('W3TC_CONTENT_DIR_NAME', WP_CONTENT_DIR_NAME . '/w3tc' . W3TC_SUFFIX);
define('W3TC_CONTENT_DIR', WP_CONTENT_DIR_PATH . '/' . W3TC_CONTENT_DIR_NAME);
define('W3TC_CONTENT_MINIFY_DIR_NAME', W3TC_CONTENT_DIR_NAME . '/min');
define('W3TC_CONTENT_MINIFY_DIR', WP_CONTENT_DIR_PATH . '/' . W3TC_CONTENT_DIR_NAME . '/min');
define('W3TC_CACHE_FILE_DBCACHE_DIR', W3TC_CONTENT_DIR . '/dbcache');
define('W3TC_CACHE_FILE_OBJECTCACHE_DIR', W3TC_CONTENT_DIR . '/objectcache');
define('W3TC_CACHE_FILE_PGCACHE_DIR', W3TC_CONTENT_DIR . '/pgcache');
define('W3TC_CACHE_FILE_MINIFY_DIR', W3TC_CONTENT_DIR . '/min');
define('W3TC_LOG_DIR', W3TC_CONTENT_DIR . '/log');
define('W3TC_TMP_DIR', W3TC_CONTENT_DIR . '/tmp');
define('W3TC_CONFIG_PATH', WP_CONTENT_DIR . '/w3-total-cache-config' . W3TC_SUFFIX . '.php');
define('W3TC_CONFIG_PREVIEW_PATH', WP_CONTENT_DIR . '/w3-total-cache-config' . W3TC_SUFFIX . '-preview.php');
define('W3TC_CONFIG_MASTER_PATH', WP_CONTENT_DIR . '/w3-total-cache-config.php');
define('W3TC_MINIFY_LOG_FILE', W3TC_LOG_DIR . '/minify.log');
define('W3TC_CDN_COMMAND_UPLOAD', 1);
define('W3TC_CDN_COMMAND_DELETE', 2);
define('W3TC_CDN_TABLE_QUEUE', 'w3tc_cdn_queue');
@ini_set('pcre.backtrack_limit', 4194304);
@ini_set('pcre.recursion_limit', 4194304);
$_w3tc_actions = array();
/**
* Deactivate plugin after activation error
*
* @return void
*/
function w3_activation_cleanup()
{
$active_plugins = (array) get_option('active_plugins');
$active_plugins_network = (array) get_site_option('active_sitewide_plugins');
// workaround for WPMU deactivation bug
remove_action('deactivate_' . W3TC_FILE, 'deactivate_sitewide_plugin');
do_action('deactivate_plugin', W3TC_FILE);
$key = array_search(W3TC_FILE, $active_plugins);
if ($key !== false) {
array_splice($active_plugins, $key, 1);
}
unset($active_plugins_network[W3TC_FILE]);
do_action('deactivate_' . W3TC_FILE);
do_action('deactivated_plugin', W3TC_FILE);
update_option('active_plugins', $active_plugins);
update_site_option('active_sitewide_plugins', $active_plugins_network);
}
/**
* W3 activate error
*
* @param string $error
* @return void
*/
function w3_activate_error($error)
{
w3_activation_cleanup();
include W3TC_DIR . '/inc/error.phtml';
exit();
}
/**
* W3 writable error
*
* @param string $path
* @return string
*/
function w3_writable_error($path)
{
$activate_url = wp_nonce_url('plugins.php?action=activate&plugin=' . W3TC_FILE, 'activate-plugin_' . W3TC_FILE);
$reactivate_button = sprintf('', addslashes($activate_url));
if (w3_check_open_basedir($path)) {
$error = sprintf('%s could not be created, please run following command:
chmod 777 %s
then %s.', $path, (file_exists($path) ? $path : dirname($path)), $reactivate_button);
} else {
$error = sprintf('%s could not be created, open_basedir restriction in effect, please check your php.ini settings:
open_basedir = "%s"
then %s.', $path, ini_get('open_basedir'), $reactivate_button);
}
w3_activate_error($error);
}
/**
* W3 Network activation error
*
* @return void
*/
function w3_network_activate_error()
{
w3_activation_cleanup();
wp_redirect(plugins_url('inc/network_activation.php', W3TC_FILE));
echo '
W3 Total Cache Error: plugin cannot be activated network-wide.
'; echo 'Back';
exit();
}
/**
* Returns current microtime
*
* @return float
*/
function w3_microtime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
/**
* Recursive creates directory
*
* @param string $path
* @param integer $mask
* @param string
* @return boolean
*/
function w3_mkdir($path, $mask = 0755, $curr_path = '')
{
$path = w3_realpath($path);
$path = trim($path, '/');
$dirs = explode('/', $path);
foreach ($dirs as $dir) {
if ($dir == '') {
return false;
}
$curr_path .= ($curr_path == '' ? '' : '/') . $dir;
if (!@is_dir($curr_path)) {
if (@mkdir($curr_path, $mask)) {
@chmod($curr_path, $mask);
} else {
return false;
}
}
}
return true;
}
/**
* Recursive remove dir
*
* @param string $path
* @param array $exclude
* @return void
*/
function w3_rmdir($path, $exclude = array(), $remove = true)
{
$dir = @opendir($path);
if ($dir) {
while (($entry = @readdir($dir)) !== false) {
$full_path = $path . '/' . $entry;
if ($entry != '.' && $entry != '..' && !in_array($full_path, $exclude)) {
if (@is_dir($full_path)) {
w3_rmdir($full_path, $exclude);
} else {
@unlink($full_path);
}
}
}
@closedir($dir);
if ($remove) {
@rmdir($path);
}
}
}
/**
* Recursive empty dir
*
* @param string $path
* @param array $exclude
* @return void
*/
function w3_emptydir($path, $exclude = array())
{
w3_rmdir($path, $exclude, false);
}
/**
* Check if content is HTML or XML
*
* @param string $content
* @return boolean
*/
function w3_is_xml(&$content)
{
return (stristr($content, 'Database Error') !== false);
}
/**
* Returns true if preview config exists
*
* @return boolean
*/
function w3_is_preview_config()
{
return file_exists(W3TC_CONFIG_PREVIEW_PATH);
}
/**
* Retuns true if preview settings active
*
* @return boolean
*/
function w3_is_preview_mode()
{
return (w3_is_preview_config() && (defined('WP_ADMIN') || isset($_REQUEST['w3tc_preview']) || strstr($_SERVER['HTTP_REFERER'], 'w3tc_preview') !== false));
}
/**
* Check if file is write-able
*
* @param string $path
* @return boolean
*/
function w3_is_writable($file)
{
$exists = file_exists($file);
$fp = @fopen($file, 'a');
if ($fp) {
fclose($fp);
if (!$exists) {
@unlink($file);
}
return true;
}
return false;
}
/**
* Cehck if dir is write-able
*
* @param string $dir
* @return boolean
*/
function w3_is_writable_dir($dir)
{
$file = $dir . '/' . uniqid(mt_rand()) . '.tmp';
return w3_is_writable($file);
}
/**
* Returns domain from host
*
* @param string $host
* @return string
*/
function w3_get_domain($host)
{
$host = strtolower($host);
if (strpos($host, 'www.') === 0) {
$host = substr($host, 4);
}
if (($pos = strpos($host, ':')) !== false) {
$host = substr($host, 0, $pos);
}
$host = rtrim($host, '.');
return $host;
}
/**
* Returns array of all available blognames
*
* @return array
*/
function w3_get_blognames()
{
global $wpdb;
$blognames = array();
$sql = sprintf('SELECT domain, path FROM %s', $wpdb->blogs);
$blogs = $wpdb->get_results($sql);
if ($blogs) {
$base_path = w3_get_base_path();
foreach ($blogs as $blog) {
$blogname = trim(str_replace($base_path, '', $blog->path), '/');
if ($blogname) {
$blognames[] = $blogname;
}
}
}
return $blognames;
}
/**
* Load blognames from file
*
* @return array
*/
function w3_load_blognames()
{
$blognames = include W3TC_BLOGNAMES_PATH;
return $blognames;
}
/**
* Save blognames into file
*
* @param string $blognames
* @return boolean
*/
function w3_save_blognames($blognames = null)
{
if (!$blognames) {
$blognames = w3_get_blognames();
}
$strings = array();
foreach ($blognames as $blogname) {
$strings[] = sprintf("'%s'", addslashes($blogname));
}
$data = sprintf(' $value) {
$count--;
$query .= urlencode($param) . (!empty($value) ? '=' . urlencode($value) : '') . ($count ? '&' : '');
}
$url .= (strpos($url, '?') === false ? '?' : '&') . $query;
}
if ($fragment != '') {
$url .= $fragment;
}
@header('Location: ' . $url);
exit();
}
/**
* Returns caching engine name
*
* @param $engine
* @return string
*/
function w3_get_engine_name($engine)
{
switch ($engine) {
case 'memcached':
$engine_name = 'memcached';
break;
case 'apc':
$engine_name = 'apc';
break;
case 'eaccelerator':
$engine_name = 'eaccelerator';
break;
case 'xcache':
$engine_name = 'xcache';
break;
case 'file':
$engine_name = 'disk';
break;
case 'file_pgcache':
$engine_name = 'disk (enhanced)';
break;
case 'mirror':
$engine_name = 'mirror';
break;
case 'netdna':
$engine_name = 'mirror: netdna / maxcdn';
break;
case 'ftp':
$engine_name = 'self-hosted / file transfer protocol upload';
break;
case 's3':
$engine_name = 'amazon simple storage service (s3)';
break;
case 'cf':
$engine_name = 'amazon cloudfront';
break;
case 'rscf':
$engine_name = 'rackspace cloud files';
break;
default:
$engine_name = 'n/a';
break;
}
return $engine_name;
}
/**
* Converts value to boolean
*
* @param mixed $value
* @return boolean
*/
function w3_to_boolean($value)
{
if (is_string($value)) {
switch (strtolower($value)) {
case '+':
case '1':
case 'y':
case 'on':
case 'yes':
case 'true':
case 'enabled':
return true;
case '-':
case '0':
case 'n':
case 'no':
case 'off':
case 'false':
case 'disabled':
return false;
}
}
return (boolean) $value;
}
/**
* Loads plugins
*
* @return void
*/
function w3_load_plugins()
{
$dir = @opendir(W3TC_PLUGINS_DIR);
if ($dir) {
while (($entry = @readdir($dir)) !== false) {
if (strrchr($entry, '.') === '.php') {
require_once W3TC_PLUGINS_DIR . '/' . $entry;
}
}
@closedir($dir);
}
}
/**
* Returns file mime type
*
* @param string $file
* @return string
*/
function w3_get_mime_type($file)
{
static $cache = array();
if (!isset($cache[$file])) {
$mime_type = false;
/**
* Try to detect by extension (fast)
*/
$mime_types = include W3TC_DIR . '/inc/mime/all.php';
foreach ($mime_types as $extension => $type) {
if (preg_match('~\.(' . $extension . ')$~i', $file)) {
$mime_type = $type;
break;
}
}
/**
* Try to detect using file info function
*/
if (!$mime_type && function_exists('finfo_open')) {
$finfo = @finfo_open(FILEINFO_MIME);
if (!$finfo) {
$finfo = @finfo_open(FILEINFO_MIME);
}
if ($finfo) {
$mime_type = @finfo_file($finfo, $file);
if ($mime_type) {
$extra_mime_type_info = strpos($mime_type, "; ");
if ($extra_mime_type_info) {
$mime_type = substr($mime_type, 0, $extra_mime_type_info);
}
if ($mime_type == 'application/octet-stream') {
$mime_type = false;
}
}
@finfo_close($finfo);
}
}
/**
* Try to detect using mime type function
*/
if (!$mime_type && function_exists('mime_content_type')) {
$mime_type = @mime_content_type($file);
}
/**
* If detection failed use default mime type
*/
if (!$mime_type) {
$mime_type = 'application/octet-stream';
}
$cache[$file] = $mime_type;
}
return $cache[$file];
}
/**
* Send twitter update status request
*
* @param string $username
* @param string $password
* @param string $status
* @param string $error
* @return string
*/
function w3_twitter_status_update($username, $password, $status, &$error)
{
$data = sprintf('status=%s', urlencode($status));
$auth = sprintf('%s:%s', $username, $password);
$xml = w3_http_post('http://twitter.com/statuses/update.xml', $data, $auth);
if ($xml) {
$matches = null;
if (preg_match('~