PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/inc/define.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 1244 lines | 647 code | 189 blank | 408 comment | 103 complexity | de0586a2a5cb46b5f31e03c732a98f64 MD5 | raw file
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. die();
  4. }
  5. define('W3TC', true);
  6. define('W3TC_VERSION', '0.9.2.4');
  7. define('W3TC_POWERED_BY', 'W3 Total Cache/' . W3TC_VERSION);
  8. define('W3TC_EMAIL', 'w3tc@w3-edge.com');
  9. define('W3TC_PAYPAL_URL', 'https://www.paypal.com/cgi-bin/webscr');
  10. define('W3TC_PAYPAL_BUSINESS', 'w3tc-team@w3-edge.com');
  11. define('W3TC_LINK_URL', 'http://www.w3-edge.com/wordpress-plugins/');
  12. define('W3TC_LINK_NAME', 'WordPress Plugins');
  13. define('W3TC_FEED_URL', 'http://feeds.feedburner.com/W3TOTALCACHE');
  14. define('W3TC_README_URL', 'http://plugins.trac.wordpress.org/browser/w3-total-cache/trunk/readme.txt?format=txt');
  15. define('W3TC_SUPPORT_US_TIMEOUT', 2592000);
  16. define('W3TC_PHP5', PHP_VERSION >= 5);
  17. define('W3TC_WIN', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'));
  18. defined('W3TC_DIR') || define('W3TC_DIR', realpath(dirname(__FILE__) . '/..'));
  19. define('W3TC_FILE', 'w3-total-cache/w3-total-cache.php');
  20. define('W3TC_INC_DIR', W3TC_DIR . '/inc');
  21. define('W3TC_LIB_DIR', W3TC_DIR . '/lib');
  22. define('W3TC_LIB_W3_DIR', W3TC_LIB_DIR . '/W3');
  23. define('W3TC_LIB_MINIFY_DIR', W3TC_LIB_DIR . '/Minify');
  24. define('W3TC_LIB_CF_DIR', W3TC_LIB_DIR . '/CF');
  25. define('W3TC_LIB_CSSTIDY_DIR', W3TC_LIB_DIR . '/CSSTidy');
  26. define('W3TC_LIB_MICROSOFT_DIR', W3TC_LIB_DIR . '/Microsoft');
  27. define('W3TC_LIB_NUSOAP_DIR', W3TC_LIB_DIR . '/Nusoap');
  28. define('W3TC_PLUGINS_DIR', W3TC_DIR . '/plugins');
  29. define('W3TC_INSTALL_DIR', W3TC_DIR . '/wp-content');
  30. define('W3TC_INSTALL_MINIFY_DIR', W3TC_INSTALL_DIR . '/w3tc/min');
  31. define('W3TC_BLOGNAMES_PATH', WP_CONTENT_DIR . '/w3-total-cache-blognames.php');
  32. define('W3TC_BLOGNAME', w3_get_blogname());
  33. define('W3TC_SUFFIX', (W3TC_BLOGNAME != '' ? '-' . W3TC_BLOGNAME : ''));
  34. defined('WP_CONTENT_DIR') || define('WP_CONTENT_DIR', realpath(W3TC_DIR . '/../..'));
  35. define('WP_CONTENT_DIR_PATH', dirname(WP_CONTENT_DIR));
  36. define('WP_CONTENT_DIR_NAME', basename(WP_CONTENT_DIR));
  37. define('W3TC_CONTENT_DIR_NAME', WP_CONTENT_DIR_NAME . '/w3tc' . W3TC_SUFFIX);
  38. define('W3TC_CONTENT_DIR', WP_CONTENT_DIR_PATH . '/' . W3TC_CONTENT_DIR_NAME);
  39. define('W3TC_CONTENT_MINIFY_DIR_NAME', W3TC_CONTENT_DIR_NAME . '/min');
  40. define('W3TC_CONTENT_MINIFY_DIR', WP_CONTENT_DIR_PATH . '/' . W3TC_CONTENT_DIR_NAME . '/min');
  41. define('W3TC_CACHE_FILE_DBCACHE_DIR', W3TC_CONTENT_DIR . '/dbcache');
  42. define('W3TC_CACHE_FILE_OBJECTCACHE_DIR', W3TC_CONTENT_DIR . '/objectcache');
  43. define('W3TC_CACHE_FILE_PGCACHE_DIR', W3TC_CONTENT_DIR . '/pgcache');
  44. define('W3TC_CACHE_FILE_MINIFY_DIR', W3TC_CONTENT_DIR . '/min');
  45. define('W3TC_LOG_DIR', W3TC_CONTENT_DIR . '/log');
  46. define('W3TC_TMP_DIR', W3TC_CONTENT_DIR . '/tmp');
  47. define('W3TC_CONFIG_PATH', WP_CONTENT_DIR . '/w3-total-cache-config' . W3TC_SUFFIX . '.php');
  48. define('W3TC_CONFIG_PREVIEW_PATH', WP_CONTENT_DIR . '/w3-total-cache-config' . W3TC_SUFFIX . '-preview.php');
  49. define('W3TC_CONFIG_MASTER_PATH', WP_CONTENT_DIR . '/w3-total-cache-config.php');
  50. define('W3TC_MINIFY_LOG_FILE', W3TC_LOG_DIR . '/minify.log');
  51. define('W3TC_CDN_COMMAND_UPLOAD', 1);
  52. define('W3TC_CDN_COMMAND_DELETE', 2);
  53. define('W3TC_CDN_COMMAND_PURGE', 3);
  54. define('W3TC_CDN_TABLE_QUEUE', 'w3tc_cdn_queue');
  55. define('W3TC_CDN_LOG_FILE', W3TC_LOG_DIR . '/cdn.log');
  56. define('W3TC_VARNISH_LOG_FILE', W3TC_LOG_DIR . '/varnish.log');
  57. define('W3TC_MARKER_BEGIN_WORDPRESS', '# BEGIN WordPress');
  58. define('W3TC_MARKER_BEGIN_PGCACHE_CORE', '# BEGIN W3TC Page Cache core');
  59. define('W3TC_MARKER_BEGIN_PGCACHE_CACHE', '# BEGIN W3TC Page Cache cache');
  60. define('W3TC_MARKER_BEGIN_PGCACHE_LEGACY', '# BEGIN W3TC Page Cache');
  61. define('W3TC_MARKER_BEGIN_PGCACHE_WPSC', '# BEGIN WPSuperCache');
  62. define('W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE', '# BEGIN W3TC Browser Cache');
  63. define('W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP', '# BEGIN W3TC Skip 404 error handling by WordPress for static files');
  64. define('W3TC_MARKER_BEGIN_MINIFY_CORE', '# BEGIN W3TC Minify core');
  65. define('W3TC_MARKER_BEGIN_MINIFY_CACHE', '# BEGIN W3TC Minify cache');
  66. define('W3TC_MARKER_BEGIN_MINIFY_LEGACY', '# BEGIN W3TC Minify');
  67. define('W3TC_MARKER_END_WORDPRESS', '# END WordPress');
  68. define('W3TC_MARKER_END_PGCACHE_CORE', '# END W3TC Page Cache core');
  69. define('W3TC_MARKER_END_PGCACHE_CACHE', '# END W3TC Page Cache cache');
  70. define('W3TC_MARKER_END_PGCACHE_LEGACY', '# END W3TC Page Cache');
  71. define('W3TC_MARKER_END_PGCACHE_WPSC', '# END WPSuperCache');
  72. define('W3TC_MARKER_END_BROWSERCACHE_CACHE', '# END W3TC Browser Cache');
  73. define('W3TC_MARKER_END_BROWSERCACHE_NO404WP', '# END W3TC Skip 404 error handling by WordPress for static files');
  74. define('W3TC_MARKER_END_MINIFY_CORE', '# END W3TC Minify core');
  75. define('W3TC_MARKER_END_MINIFY_CACHE', '# END W3TC Minify cache');
  76. define('W3TC_MARKER_END_MINIFY_LEGACY', '# END W3TC Minify');
  77. define('W3TC_INSTALL_FILE_ADVANCED_CACHE', W3TC_INSTALL_DIR . '/advanced-cache.php');
  78. define('W3TC_INSTALL_FILE_DB', W3TC_INSTALL_DIR . '/db.php');
  79. define('W3TC_INSTALL_FILE_OBJECT_CACHE', W3TC_INSTALL_DIR . '/object-cache.php');
  80. define('W3TC_ADDIN_FILE_ADVANCED_CACHE', WP_CONTENT_DIR . '/advanced-cache.php');
  81. define('W3TC_ADDIN_FILE_DB', WP_CONTENT_DIR . '/db.php');
  82. define('W3TC_ADDIN_FILE_OBJECT_CACHE', WP_CONTENT_DIR . '/object-cache.php');
  83. require_once W3TC_INC_DIR . '/functions/compat.php';
  84. require_once W3TC_INC_DIR . '/functions/plugin.php';
  85. @ini_set('pcre.backtrack_limit', 4194304);
  86. @ini_set('pcre.recursion_limit', 4194304);
  87. /**
  88. * Returns current microtime
  89. *
  90. * @return double
  91. */
  92. function w3_microtime() {
  93. list ($usec, $sec) = explode(' ', microtime());
  94. return ((double) $usec + (double) $sec);
  95. }
  96. /**
  97. * Check if content is HTML or XML
  98. *
  99. * @param string $content
  100. * @return boolean
  101. */
  102. function w3_is_xml($content) {
  103. if (strlen($content) > 1000) {
  104. $content = substr($content, 0, 1000);
  105. }
  106. if (strstr($content, '<!--') !== false) {
  107. $content = preg_replace('~<!--.*?-->~s', '', $content);
  108. }
  109. $content = ltrim($content, "\x00\x09\x0A\x0D\x20\xBB\xBF\xEF");
  110. return (stripos($content, '<?xml') === 0 || stripos($content, '<html') === 0 || stripos($content, '<!DOCTYPE') === 0);
  111. }
  112. /**
  113. * Returns true if it's WPMU
  114. *
  115. * @return boolean
  116. */
  117. function w3_is_wpmu() {
  118. static $wpmu = null;
  119. if ($wpmu === null) {
  120. $wpmu = file_exists(ABSPATH . 'wpmu-settings.php');
  121. }
  122. return $wpmu;
  123. }
  124. /**
  125. * Returns true if WPMU uses vhosts
  126. *
  127. * @return boolean
  128. */
  129. function w3_is_subdomain_install() {
  130. return ((defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) || (defined('VHOST') && VHOST == 'yes'));
  131. }
  132. /**
  133. * Returns true if it's WP with enabled Network mode
  134. *
  135. * @return boolean
  136. */
  137. function w3_is_multisite() {
  138. static $multisite = null;
  139. if ($multisite === null) {
  140. $multisite = ((defined('MULTISITE') && MULTISITE) || defined('SUNRISE') || w3_is_subdomain_install());
  141. }
  142. return $multisite;
  143. }
  144. /**
  145. * Returns if there is multisite mode
  146. *
  147. * @return boolean
  148. */
  149. function w3_is_network() {
  150. return (w3_is_wpmu() || w3_is_multisite());
  151. }
  152. /**
  153. * Check if URL is valid
  154. *
  155. * @param string $url
  156. * @return boolean
  157. */
  158. function w3_is_url($url) {
  159. return preg_match('~^(https?:)?//~', $url);
  160. }
  161. /**
  162. * Returns true if current connection is secure
  163. *
  164. * @return boolean
  165. */
  166. function w3_is_https() {
  167. switch (true) {
  168. case (isset($_SERVER['HTTPS']) && w3_to_boolean($_SERVER['HTTPS'])):
  169. case (isset($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] == 443):
  170. return true;
  171. }
  172. return false;
  173. }
  174. /**
  175. * Check if there was database error
  176. *
  177. * @param string $content
  178. * @return boolean
  179. */
  180. function w3_is_database_error(&$content) {
  181. return (stristr($content, '<title>Database Error</title>') !== false);
  182. }
  183. /**
  184. * Returns true if preview config exists
  185. *
  186. * @return boolean
  187. */
  188. function w3_is_preview_config() {
  189. return file_exists(W3TC_CONFIG_PREVIEW_PATH);
  190. }
  191. /**
  192. * Retuns true if preview settings active
  193. *
  194. * @return boolean
  195. */
  196. function w3_is_preview_mode() {
  197. return (w3_is_preview_config() && (defined('WP_ADMIN') || isset($_REQUEST['w3tc_preview']) || (isset($_SERVER['HTTP_REFERER']) && strstr($_SERVER['HTTP_REFERER'], 'w3tc_preview') !== false)));
  198. }
  199. /**
  200. * Returns true if server is Apache
  201. *
  202. * @return boolean
  203. */
  204. function w3_is_apache() {
  205. return (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false);
  206. }
  207. /**
  208. * Check whether server is LiteSpeed
  209. *
  210. * @return bool
  211. */
  212. function w3_is_litespeed() {
  213. return (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false);
  214. }
  215. /**
  216. * Returns true if server is nginx
  217. *
  218. * @return boolean
  219. */
  220. function w3_is_nginx() {
  221. return (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false);
  222. }
  223. /**
  224. * Check whether $engine is correct CDN engine
  225. *
  226. * @param string $engine
  227. * @return boolean
  228. */
  229. function w3_is_cdn_engine($engine) {
  230. return in_array($engine, array('ftp', 's3', 'cf', 'cf2', 'rscf', 'azure', 'mirror', 'netdna', 'cotendo', 'edgecast'));
  231. }
  232. /**
  233. * Returns true if CDN engine is mirror
  234. *
  235. * @param string $engine
  236. * @return bool
  237. */
  238. function w3_is_cdn_mirror($engine) {
  239. return in_array($engine, array('mirror', 'netdna', 'cotendo', 'cf2', 'edgecast'));
  240. }
  241. /**
  242. * Returns domain from host
  243. *
  244. * @param string $host
  245. * @return string
  246. */
  247. function w3_get_domain($host) {
  248. $host = strtolower($host);
  249. if (strpos($host, 'www.') === 0) {
  250. $host = substr($host, 4);
  251. }
  252. if (($pos = strpos($host, ':')) !== false) {
  253. $host = substr($host, 0, $pos);
  254. }
  255. $host = rtrim($host, '.');
  256. return $host;
  257. }
  258. /**
  259. * Returns array of all available blognames
  260. *
  261. * @return array
  262. */
  263. function w3_get_blognames() {
  264. global $wpdb;
  265. $blognames = array();
  266. $sql = sprintf('SELECT domain, path FROM %s', $wpdb->blogs);
  267. $blogs = $wpdb->get_results($sql);
  268. if ($blogs) {
  269. $base_path = w3_get_base_path();
  270. foreach ($blogs as $blog) {
  271. $blogname = trim(str_replace($base_path, '', $blog->path), '/');
  272. if ($blogname) {
  273. $blognames[] = $blogname;
  274. }
  275. }
  276. }
  277. return $blognames;
  278. }
  279. /**
  280. * Load blognames from file
  281. *
  282. * @return array
  283. */
  284. function w3_load_blognames() {
  285. $blognames = include W3TC_BLOGNAMES_PATH;
  286. return $blognames;
  287. }
  288. /**
  289. * Save blognames into file
  290. *
  291. * @param string $blognames
  292. * @return boolean
  293. */
  294. function w3_save_blognames($blognames = null) {
  295. if (!$blognames) {
  296. $blognames = w3_get_blognames();
  297. }
  298. $strings = array();
  299. foreach ($blognames as $blogname) {
  300. $strings[] = sprintf("'%s'", addslashes($blogname));
  301. }
  302. $data = sprintf('<?php return array(%s);', implode(', ', $strings));
  303. return @file_put_contents(W3TC_BLOGNAMES_PATH, $data);
  304. }
  305. /**
  306. * Detect WPMU blogname
  307. *
  308. * @return string
  309. */
  310. function w3_get_blogname() {
  311. static $blogname = null;
  312. if ($blogname === null) {
  313. if (w3_is_network()) {
  314. $host = w3_get_host();
  315. $domain = w3_get_domain($host);
  316. if (w3_is_subdomain_install()) {
  317. $blogname = $domain;
  318. } else {
  319. $uri = $_SERVER['REQUEST_URI'];
  320. $base_path = w3_get_base_path();
  321. if ($base_path != '' && strpos($uri, $base_path) === 0) {
  322. $uri = substr_replace($uri, '/', 0, strlen($base_path));
  323. }
  324. $blogname = w3_get_blogname_from_uri($uri);
  325. if ($blogname != '') {
  326. $blogname = $blogname . '.' . $domain;
  327. } else {
  328. $blogname = $domain;
  329. }
  330. }
  331. } else {
  332. $blogname = '';
  333. }
  334. }
  335. return $blogname;
  336. }
  337. /**
  338. * Returns blogname from URI
  339. *
  340. * @param string $uri
  341. * @return string
  342. */
  343. function w3_get_blogname_from_uri($uri) {
  344. $blogname = '';
  345. $matches = null;
  346. $uri = strtolower($uri);
  347. if (preg_match('~^/([a-z0-9-]+)/~', $uri, $matches)) {
  348. if (file_exists(W3TC_BLOGNAMES_PATH)) {
  349. // Get blognames from cache
  350. $blognames = w3_load_blognames();
  351. } elseif (isset($GLOBALS['wpdb'])) {
  352. // Get blognames from DB
  353. $blognames = w3_get_blognames();
  354. } else {
  355. $blognames = array();
  356. }
  357. if (is_array($blognames) && in_array($matches[1], $blognames)) {
  358. $blogname = $matches[1];
  359. }
  360. }
  361. return $blogname;
  362. }
  363. /**
  364. * Returns current blog ID
  365. *
  366. * @return integer
  367. */
  368. function w3_get_blog_id() {
  369. return (isset($GLOBALS['blog_id']) ? (int) $GLOBALS['blog_id'] : 0);
  370. }
  371. /**
  372. * Returns URL regexp from URL
  373. *
  374. * @param string $url
  375. * @return string
  376. */
  377. function w3_get_url_regexp($url) {
  378. $url = preg_replace('~(https?:)?//~i', '', $url);
  379. $url = preg_replace('~^www\.~i', '', $url);
  380. $regexp = '(https?:)?//(www\.)?' . w3_preg_quote($url);
  381. return $regexp;
  382. }
  383. /**
  384. * Returns SSL URL if current connection is https
  385. * @param string $url
  386. * @return string
  387. */
  388. function w3_get_url_ssl($url) {
  389. if (w3_is_https()) {
  390. $url = str_replace('http://', 'https://', $url);
  391. }
  392. return $url;
  393. }
  394. /**
  395. * Get domain URL
  396. *
  397. * @return string
  398. */
  399. function w3_get_domain_url() {
  400. $home_url = w3_get_home_url();
  401. $parse_url = @parse_url($home_url);
  402. if ($parse_url && isset($parse_url['scheme']) && isset($parse_url['host'])) {
  403. $scheme = $parse_url['scheme'];
  404. $host = $parse_url['host'];
  405. $port = (isset($parse_url['port']) && $parse_url['port'] != 80 ? ':' . (int) $parse_url['port'] : '');
  406. $domain_url = sprintf('%s://%s%s', $scheme, $host, $port);
  407. return $domain_url;
  408. }
  409. return false;
  410. }
  411. /**
  412. * Returns domain url regexp
  413. *
  414. * @return string
  415. */
  416. function w3_get_domain_url_regexp() {
  417. $domain_url = w3_get_domain_url();
  418. $regexp = w3_get_url_regexp($domain_url);
  419. return $regexp;
  420. }
  421. /**
  422. * Returns home URL
  423. *
  424. * No trailing slash!
  425. *
  426. * @return string
  427. */
  428. function w3_get_home_url() {
  429. static $home_url = null;
  430. if ($home_url === null) {
  431. $home_url = get_option('home');
  432. $home_url = rtrim($home_url, '/');
  433. }
  434. return $home_url;
  435. }
  436. /**
  437. * Returns SSL home url
  438. *
  439. * @return string
  440. */
  441. function w3_get_home_url_ssl() {
  442. $home_url = w3_get_home_url();
  443. $ssl = w3_get_url_ssl($home_url);
  444. return $ssl;
  445. }
  446. /**
  447. * Returns home url regexp
  448. *
  449. * @return string
  450. */
  451. function w3_get_home_url_regexp() {
  452. $home_url = w3_get_home_url();
  453. $regexp = w3_get_url_regexp($home_url);
  454. return $regexp;
  455. }
  456. /**
  457. * Returns site URL
  458. *
  459. * No trailing slash!
  460. *
  461. * @return string
  462. */
  463. function w3_get_site_url() {
  464. static $site_url = null;
  465. if ($site_url === null) {
  466. $site_url = get_option('siteurl');
  467. $site_url = rtrim($site_url, '/');
  468. }
  469. return $site_url;
  470. }
  471. /**
  472. * Returns SSL site URL
  473. *
  474. * @return string
  475. */
  476. function w3_get_site_url_ssl() {
  477. $site_url = w3_get_site_url();
  478. $ssl = w3_get_url_ssl($site_url);
  479. return $ssl;
  480. }
  481. /**
  482. * Returns absolute path to document root
  483. *
  484. * No trailing slash!
  485. *
  486. * @return string
  487. */
  488. function w3_get_document_root() {
  489. static $document_root = null;
  490. if ($document_root === null) {
  491. if (!empty($_SERVER['SCRIPT_FILENAME'])) {
  492. $document_root = substr(w3_path($_SERVER['SCRIPT_FILENAME']), 0, -strlen(w3_path($_SERVER['PHP_SELF'])));
  493. } elseif (!empty($_SERVER['PATH_TRANSLATED'])) {
  494. $document_root = substr(w3_path($_SERVER['PATH_TRANSLATED']), 0, -strlen(w3_path($_SERVER['PHP_SELF'])));
  495. } elseif (!empty($_SERVER['DOCUMENT_ROOT'])) {
  496. $document_root = w3_path($_SERVER['DOCUMENT_ROOT']);
  497. } else {
  498. $document_root = w3_get_site_root();
  499. }
  500. $document_root = realpath($document_root);
  501. $document_root = w3_path($document_root);
  502. }
  503. return $document_root;
  504. }
  505. /**
  506. * Returns absolute path to home directory
  507. *
  508. * Example:
  509. *
  510. * DOCUMENT_ROOT=/var/www/vhosts/domain.com
  511. * Install dir=/var/www/vhosts/domain.com/site/blog
  512. * home=http://domain.com/site
  513. * siteurl=http://domain.com/site/blog
  514. * return /var/www/vhosts/domain.com/site
  515. *
  516. * No trailing slash!
  517. *
  518. * @return string
  519. */
  520. function w3_get_home_root() {
  521. if (w3_is_network()) {
  522. $path = w3_get_base_path();
  523. } else {
  524. $path = w3_get_home_path();
  525. }
  526. $home_root = w3_get_document_root() . $path;
  527. $home_root = realpath($home_root);
  528. $home_root = w3_path($home_root);
  529. return $home_root;
  530. }
  531. /**
  532. * Returns absolute path to blog install dir
  533. *
  534. * Example:
  535. *
  536. * DOCUMENT_ROOT=/var/www/vhosts/domain.com
  537. * install dir=/var/www/vhosts/domain.com/site/blog
  538. * return /var/www/vhosts/domain.com/site/blog
  539. *
  540. * No trailing slash!
  541. *
  542. * @return string
  543. */
  544. function w3_get_site_root() {
  545. $site_root = ABSPATH;
  546. $site_root = realpath($site_root);
  547. $site_root = w3_path($site_root);
  548. return $site_root;
  549. }
  550. /**
  551. * Returns blog path
  552. *
  553. * Example:
  554. *
  555. * siteurl=http://domain.com/site/blog
  556. * return /site/blog/
  557. *
  558. * With trailing slash!
  559. *
  560. * @return string
  561. */
  562. function w3_get_site_path() {
  563. $site_url = w3_get_site_url();
  564. $parse_url = @parse_url($site_url);
  565. if ($parse_url && isset($parse_url['path'])) {
  566. $site_path = '/' . ltrim($parse_url['path'], '/');
  567. } else {
  568. $site_path = '/';
  569. }
  570. if (substr($site_path, -1) != '/') {
  571. $site_path .= '/';
  572. }
  573. return $site_path;
  574. }
  575. /**
  576. * Returns home domain
  577. *
  578. * @return string
  579. */
  580. function w3_get_home_domain() {
  581. $home_url = w3_get_home_url();
  582. $parse_url = @parse_url($home_url);
  583. if ($parse_url && isset($parse_url['host'])) {
  584. return $parse_url['host'];
  585. }
  586. return w3_get_host();
  587. }
  588. /**
  589. * Returns home path
  590. *
  591. * Example:
  592. *
  593. * home=http://domain.com/site/
  594. * siteurl=http://domain.com/site/blog
  595. * return /site/
  596. *
  597. * With trailing slash!
  598. *
  599. * @return string
  600. */
  601. function w3_get_home_path() {
  602. $home_url = w3_get_home_url();
  603. $parse_url = @parse_url($home_url);
  604. if ($parse_url && isset($parse_url['path'])) {
  605. $home_path = '/' . ltrim($parse_url['path'], '/');
  606. } else {
  607. $home_path = '/';
  608. }
  609. if (substr($home_path, -1) != '/') {
  610. $home_path .= '/';
  611. }
  612. return $home_path;
  613. }
  614. /**
  615. * Returns path to WP directory relative to document root
  616. *
  617. * Example:
  618. *
  619. * DOCUMENT_ROOT=/var/www/vhosts/domain.com/
  620. * Install dir=/var/www/vhosts/domain.com/site/blog/
  621. * return /site/blog/
  622. *
  623. * With trailing slash!
  624. *
  625. * @return string
  626. */
  627. function w3_get_base_path() {
  628. $document_root = w3_get_document_root();
  629. $site_root = w3_get_site_root();
  630. $base_path = str_replace($document_root, '', $site_root);
  631. $base_path = '/' . ltrim($base_path, '/');
  632. if (substr($base_path, -1) != '/') {
  633. $base_path .= '/';
  634. }
  635. return $base_path;
  636. }
  637. /**
  638. * Returns server hostname
  639. *
  640. * @return string
  641. */
  642. function w3_get_host() {
  643. static $host = null;
  644. if ($host === null) {
  645. $host = (!empty($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']);
  646. }
  647. return $host;
  648. }
  649. /**
  650. * Returns host ID
  651. *
  652. * @return string
  653. */
  654. function w3_get_host_id() {
  655. static $host_id = null;
  656. if ($host_id === null) {
  657. $host = w3_get_host();
  658. $blog_id = w3_get_blog_id();
  659. $host_id = sprintf('%s_%d', $host, $blog_id);
  660. }
  661. return $host_id;
  662. }
  663. /**
  664. * Returns WP config file path
  665. *
  666. * @return string
  667. */
  668. function w3_get_wp_config_path() {
  669. $search = array(
  670. ABSPATH . 'wp-config.php',
  671. dirname(ABSPATH) . '/wp-config.php'
  672. );
  673. foreach ($search as $path) {
  674. if (file_exists($path)) {
  675. return $path;
  676. }
  677. }
  678. return false;
  679. }
  680. /**
  681. * Returns theme key
  682. *
  683. * @param string $theme_root
  684. * @param string $template
  685. * @param string $stylesheet
  686. * @return string
  687. */
  688. function w3_get_theme_key($theme_root, $template, $stylesheet) {
  689. $site_root = w3_get_site_root();
  690. $theme_path = ltrim(str_replace($site_root, '', w3_path($theme_root)), '/');
  691. return substr(md5($theme_path . $template . $stylesheet), 0, 5);
  692. }
  693. /**
  694. * Returns theme key (legacy support)
  695. *
  696. * @param string $theme_root
  697. * @param string $template
  698. * @param string $stylesheet
  699. * @return string
  700. */
  701. function w3_get_theme_key_legacy($theme_root, $template, $stylesheet) {
  702. return substr(md5($theme_root . $template . $stylesheet), 0, 6);
  703. }
  704. /**
  705. * Returns true if we can check rules
  706. *
  707. * @return bool
  708. */
  709. function w3_can_check_rules() {
  710. return (w3_is_apache() || w3_is_litespeed() || w3_is_nginx());
  711. }
  712. /**
  713. * Returns true if CDN engine is supporting purge
  714. *
  715. * @param string $engine
  716. * @return bool
  717. */
  718. function w3_can_cdn_purge($engine) {
  719. return in_array($engine, array('ftp', 's3', 'cf', 'cf2', 'rscf', 'azure', 'netdna', 'cotendo', 'edgecast'));
  720. }
  721. /**
  722. * Parses path
  723. *
  724. * @param string $path
  725. * @return mixed
  726. */
  727. function w3_parse_path($path) {
  728. $path = str_replace(array(
  729. '%BLOG_ID%',
  730. '%POST_ID%',
  731. '%BLOGNAME%',
  732. '%HOST%',
  733. '%DOMAIN%',
  734. '%BASE_PATH%'
  735. ), array(
  736. (isset($GLOBALS['blog_id']) ? (int) $GLOBALS['blog_id'] : 0),
  737. (isset($GLOBALS['post_id']) ? (int) $GLOBALS['post_id'] : 0),
  738. w3_get_blogname(),
  739. w3_get_host(),
  740. w3_get_domain(w3_get_host()),
  741. trim(w3_get_base_path(), '/')
  742. ), $path);
  743. return $path;
  744. }
  745. /**
  746. * Normalizes file name
  747. *
  748. * Relative to site root!
  749. *
  750. * @param string $file
  751. * @return string
  752. */
  753. function w3_normalize_file($file) {
  754. if (w3_is_url($file)) {
  755. if (strstr($file, '?') === false) {
  756. $home_url_regexp = '~' . w3_get_home_url_regexp() . '~i';
  757. $file = preg_replace($home_url_regexp, '', $file);
  758. }
  759. }
  760. if (!w3_is_url($file)) {
  761. $file = w3_path($file);
  762. $file = str_replace(w3_get_site_root(), '', $file);
  763. $file = ltrim($file, '/');
  764. }
  765. return $file;
  766. }
  767. /**
  768. * Normalizes file name for minify
  769. *
  770. * Relative to document root!
  771. *
  772. * @param string $file
  773. * @return string
  774. */
  775. function w3_normalize_file_minify($file) {
  776. if (w3_is_url($file)) {
  777. if (strstr($file, '?') === false) {
  778. $domain_url_regexp = '~' . w3_get_domain_url_regexp() . '~i';
  779. $file = preg_replace($domain_url_regexp, '', $file);
  780. }
  781. }
  782. if (!w3_is_url($file)) {
  783. $file = w3_path($file);
  784. $file = str_replace(w3_get_document_root(), '', $file);
  785. $file = ltrim($file, '/');
  786. }
  787. return $file;
  788. }
  789. /**
  790. * Normalizes file name for minify
  791. *
  792. * Relative to document root!
  793. *
  794. * @param string $file
  795. * @return string
  796. */
  797. function w3_normalize_file_minify2($file) {
  798. $file = w3_remove_query($file);
  799. $file = w3_normalize_file_minify($file);
  800. $file = w3_translate_file($file);
  801. return $file;
  802. }
  803. /**
  804. * Translates remote file to local file
  805. *
  806. * @param string $file
  807. * @return string
  808. */
  809. function w3_translate_file($file) {
  810. if (!w3_is_url($file)) {
  811. $file = '/' . ltrim($file, '/');
  812. $regexp = '~^' . w3_preg_quote(w3_get_site_path()) . '~';
  813. $file = preg_replace($regexp, w3_get_base_path(), $file);
  814. $file = ltrim($file, '/');
  815. }
  816. return $file;
  817. }
  818. /**
  819. * Remove WP query string from URL
  820. *
  821. * @param string $url
  822. * @return string
  823. */
  824. function w3_remove_query($url) {
  825. $url = preg_replace('~[&\?]+(ver=[a-z0-9-_\.]+|[0-9-]+)~i', '', $url);
  826. return $url;
  827. }
  828. /**
  829. * Converts win path to unix
  830. *
  831. * @param string $path
  832. * @return string
  833. */
  834. function w3_path($path) {
  835. $path = preg_replace('~[/\\\]+~', '/', $path);
  836. $path = rtrim($path, '/');
  837. return $path;
  838. }
  839. /**
  840. * Returns real path of given path
  841. *
  842. * @param string $path
  843. * @return string
  844. */
  845. function w3_realpath($path) {
  846. $path = w3_path($path);
  847. $parts = explode('/', $path);
  848. $absolutes = array();
  849. foreach ($parts as $part) {
  850. if ('.' == $part) {
  851. continue;
  852. }
  853. if ('..' == $part) {
  854. array_pop($absolutes);
  855. } else {
  856. $absolutes[] = $part;
  857. }
  858. }
  859. return implode('/', $absolutes);
  860. }
  861. /**
  862. * Returns GMT date
  863. * @param integer $time
  864. * @return string
  865. */
  866. function w3_http_date($time) {
  867. return gmdate('D, d M Y H:i:s \G\M\T', $time);
  868. }
  869. /**
  870. * Redirects to URL
  871. *
  872. * @param string $url
  873. * @param array $params
  874. * @return string
  875. */
  876. function w3_redirect($url = '', $params = array()) {
  877. require_once W3TC_INC_DIR . '/functions/url.php';
  878. $url = w3_url_format($url, $params);
  879. @header('Location: ' . $url);
  880. exit();
  881. }
  882. /**
  883. * Returns caching engine name
  884. *
  885. * @param $engine
  886. * @return string
  887. */
  888. function w3_get_engine_name($engine) {
  889. switch ($engine) {
  890. case 'memcached':
  891. $engine_name = 'memcached';
  892. break;
  893. case 'apc':
  894. $engine_name = 'apc';
  895. break;
  896. case 'eaccelerator':
  897. $engine_name = 'eaccelerator';
  898. break;
  899. case 'xcache':
  900. $engine_name = 'xcache';
  901. break;
  902. case 'wincache':
  903. $engine_name = 'wincache';
  904. break;
  905. case 'file':
  906. $engine_name = 'disk: basic';
  907. break;
  908. case 'file_generic':
  909. $engine_name = 'disk: enhanced';
  910. break;
  911. case 'ftp':
  912. $engine_name = 'self-hosted / file transfer protocol upload';
  913. break;
  914. case 's3':
  915. $engine_name = 'amazon simple storage service (s3)';
  916. break;
  917. case 'cf':
  918. $engine_name = 'amazon cloudfront';
  919. break;
  920. case 'cf2':
  921. $engine_name = 'amazon cloudfront';
  922. break;
  923. case 'rscf':
  924. $engine_name = 'rackspace cloud files';
  925. break;
  926. case 'azure':
  927. $engine_name = 'microsoft azure storage';
  928. break;
  929. case 'mirror':
  930. $engine_name = 'mirror';
  931. break;
  932. case 'netdna':
  933. $engine_name = 'netdna / maxcdn';
  934. break;
  935. case 'cotendo':
  936. $engine_name = 'cotendo';
  937. break;
  938. case 'edgecast':
  939. $engine_name = 'media template procdn / edgecast';
  940. break;
  941. default:
  942. $engine_name = 'n/a';
  943. break;
  944. }
  945. return $engine_name;
  946. }
  947. /**
  948. * Converts value to boolean
  949. *
  950. * @param mixed $value
  951. * @return boolean
  952. */
  953. function w3_to_boolean($value) {
  954. if (is_string($value)) {
  955. switch (strtolower($value)) {
  956. case '+':
  957. case '1':
  958. case 'y':
  959. case 'on':
  960. case 'yes':
  961. case 'true':
  962. case 'enabled':
  963. return true;
  964. case '-':
  965. case '0':
  966. case 'n':
  967. case 'no':
  968. case 'off':
  969. case 'false':
  970. case 'disabled':
  971. return false;
  972. }
  973. }
  974. return (boolean) $value;
  975. }
  976. /**
  977. * Quotes regular expression string
  978. *
  979. * @param string $string
  980. * @param string $delimiter
  981. * @return string
  982. */
  983. function w3_preg_quote($string, $delimiter = null) {
  984. $string = preg_quote($string, $delimiter);
  985. $string = strtr($string, array(
  986. ' ' => '\ '
  987. ));
  988. return $string;
  989. }
  990. /**
  991. * Returns true if zlib output compression is enabled otherwise false
  992. *
  993. * @return boolean
  994. */
  995. function w3_zlib_output_compression() {
  996. return w3_to_boolean(ini_get('zlib.output_compression'));
  997. }
  998. /**
  999. * Recursive strips slahes from the var
  1000. *
  1001. * @param mixed $var
  1002. * @return mixed
  1003. */
  1004. function w3_stripslashes($var) {
  1005. if (is_string($var)) {
  1006. return stripslashes($var);
  1007. } elseif (is_array($var)) {
  1008. $var = array_map('w3_stripslashes', $var);
  1009. }
  1010. return $var;
  1011. }
  1012. /**
  1013. * Escapes HTML comment
  1014. *
  1015. * @param string $comment
  1016. * @return mixed
  1017. */
  1018. function w3_escape_comment($comment) {
  1019. while (strstr($comment, '--') !== false) {
  1020. $comment = str_replace('--', '- -', $comment);
  1021. }
  1022. return $comment;
  1023. }
  1024. /**
  1025. * Returns instance of singleton class
  1026. *
  1027. * @param string $class
  1028. * @return object
  1029. */
  1030. function &w3_instance($class) {
  1031. static $instances = array();
  1032. if (!isset($instances[$class])) {
  1033. require_once W3TC_LIB_W3_DIR . '/' .
  1034. str_replace('_', '/', substr($class, 3)) . '.php';
  1035. @$instances[$class] = & new $class();
  1036. }
  1037. $v = $instances[$class]; // Don't return reference
  1038. return $v;
  1039. }
  1040. /**
  1041. * Loads plugins
  1042. *
  1043. * @return void
  1044. */
  1045. function w3_load_plugins() {
  1046. $dir = @opendir(W3TC_PLUGINS_DIR);
  1047. if ($dir) {
  1048. while (($entry = @readdir($dir)) !== false) {
  1049. if (strrchr($entry, '.') === '.php') {
  1050. require_once W3TC_PLUGINS_DIR . '/' . $entry;
  1051. }
  1052. }
  1053. @closedir($dir);
  1054. }
  1055. }