PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/wp-content/plugins/w3-total-cache/lib/W3/PgCache.php

https://github.com/kennethreitz-archive/wordpress-skeleton
PHP | 1020 lines | 565 code | 151 blank | 304 comment | 116 complexity | 5126deadfef0f9ddfdeadf228d94383c MD5 | raw file
  1. <?php
  2. /**
  3. * W3 PgCache
  4. */
  5. /**
  6. * Class W3_PgCache
  7. */
  8. class W3_PgCache
  9. {
  10. /**
  11. * Advanced cache config
  12. *
  13. * @var W3_Config
  14. */
  15. var $_config = null;
  16. /**
  17. * Caching flag
  18. *
  19. * @var boolean
  20. */
  21. var $_caching = false;
  22. /**
  23. * Time start
  24. *
  25. * @var double
  26. */
  27. var $_time_start = 0;
  28. /**
  29. * Lifetime
  30. * @var integer
  31. */
  32. var $_lifetime = 0;
  33. /**
  34. * Enhanced mode flag
  35. *
  36. * @var boolean
  37. */
  38. var $_enhanced_mode = false;
  39. /**
  40. * Debug flag
  41. *
  42. * @var boolean
  43. */
  44. var $_debug = false;
  45. /**
  46. * Cache reject reason
  47. *
  48. * @var string
  49. */
  50. var $cache_reject_reason = '';
  51. /**
  52. * PHP5 Constructor
  53. */
  54. function __construct()
  55. {
  56. require_once W3TC_LIB_W3_DIR . '/Config.php';
  57. $this->_config = & W3_Config::instance();
  58. $this->_debug = $this->_config->get_boolean('pgcache.debug');
  59. $this->_lifetime = $this->_config->get_integer('pgcache.lifetime');
  60. $this->_enhanced_mode = ($this->_config->get_string('pgcache.engine') == 'file_pgcache');
  61. }
  62. /**
  63. * PHP4 Constructor
  64. */
  65. function W3_PgCache()
  66. {
  67. $this->__construct();
  68. }
  69. /**
  70. * Do cache logic
  71. */
  72. function process()
  73. {
  74. /**
  75. * Skip caching for some pages
  76. */
  77. switch (true) {
  78. case defined('DOING_AJAX'):
  79. case defined('DOING_CRON'):
  80. case defined('APP_REQUEST'):
  81. case defined('XMLRPC_REQUEST'):
  82. case defined('WP_ADMIN'):
  83. return;
  84. }
  85. /**
  86. * Handle mobile redirects
  87. */
  88. $mobile_redirect = $this->_config->get_string('pgcache.mobile.redirect');
  89. if ($mobile_redirect != '' && $this->_is_mobile()) {
  90. w3_redirect($mobile_redirect);
  91. exit();
  92. }
  93. /**
  94. * Do page cache logic
  95. */
  96. if ($this->_debug) {
  97. $this->_time_start = w3_microtime();
  98. }
  99. $this->_caching = $this->_can_cache();
  100. $compression = $this->_get_compression();
  101. $page_key = $this->_get_page_key($_SERVER['REQUEST_URI'], $compression);
  102. if ($this->_caching) {
  103. /**
  104. * Check if page is cached
  105. */
  106. $cache = & $this->_get_cache();
  107. $data = $cache->get($page_key);
  108. if ($data) {
  109. /**
  110. * Do Bad Behavior check
  111. */
  112. $this->_bad_behavior();
  113. if ($this->_enhanced_mode) {
  114. $is_404 = false;
  115. $headers = array();
  116. $time = $cache->mtime($page_key);
  117. $content = $data;
  118. } else {
  119. $is_404 = $data['404'];
  120. $headers = $data['headers'];
  121. $time = $data['time'];
  122. $content = $data['content'];
  123. }
  124. /**
  125. * Calculate content etag
  126. */
  127. $etag = md5($content);
  128. /**
  129. * Send headers
  130. */
  131. $this->_send_headers($is_404, $time, $etag, $compression, $headers);
  132. /**
  133. * Append debug info
  134. */
  135. if ($this->_debug) {
  136. $time_total = w3_microtime() - $this->_time_start;
  137. $debug_info = $this->_get_debug_info($page_key, true, '', true, $time_total);
  138. $this->_append_content($content, "\r\n\r\n" . $debug_info, $compression);
  139. }
  140. echo $content;
  141. exit();
  142. }
  143. }
  144. /**
  145. * Start output buffering
  146. */
  147. ob_start(array(
  148. &$this,
  149. 'ob_callback'
  150. ));
  151. }
  152. /**
  153. * Output buffering callback
  154. *
  155. * @param string $buffer
  156. * @return string
  157. */
  158. function ob_callback($buffer)
  159. {
  160. if ($buffer != '') {
  161. if (w3_is_xml($buffer) && $this->_can_cache2()) {
  162. $compression = $this->_get_compression();
  163. $compressions = $this->_get_compressions();
  164. if ($this->_enhanced_mode) {
  165. $is_404 = false;
  166. $headers = array();
  167. } else {
  168. $is_404 = (function_exists('is_404') ? is_404() : false);
  169. $headers = $this->_get_cached_headers();
  170. }
  171. $time = time();
  172. $cache = & $this->_get_cache();
  173. foreach ($compressions as $_compression) {
  174. $_page_key = $this->_get_page_key($_SERVER['REQUEST_URI'], $_compression);
  175. /**
  176. * Encode content
  177. */
  178. switch ($_compression) {
  179. case false:
  180. $_content = $buffer;
  181. break;
  182. case 'gzip':
  183. $_content = gzencode($buffer);
  184. break;
  185. case 'deflate':
  186. $_content = gzdeflate($buffer);
  187. break;
  188. }
  189. /**
  190. * Store cache data
  191. */
  192. if ($this->_enhanced_mode) {
  193. $cache->set($_page_key, $_content);
  194. } else {
  195. $_data = array(
  196. '404' => $is_404,
  197. 'headers' => $headers,
  198. 'time' => $time,
  199. 'content' => $_content
  200. );
  201. $cache->set($_page_key, $_data, $this->_lifetime);
  202. }
  203. if ($compression == $_compression) {
  204. $page_key = $_page_key;
  205. $buffer = $_content;
  206. }
  207. }
  208. /**
  209. * Calculate content etag
  210. */
  211. $etag = md5($buffer);
  212. /**
  213. * Send headers
  214. */
  215. $this->_send_headers($is_404, $time, $etag, $compression, $headers);
  216. /**
  217. * Append debug info
  218. */
  219. if ($this->_debug) {
  220. $time_total = w3_microtime() - $this->_time_start;
  221. $debug_info = $this->_get_debug_info($page_key, true, '', false, $time_total);
  222. $this->_append_content($buffer, "\r\n\r\n" . $debug_info, $compression);
  223. }
  224. } elseif ($this->_debug) {
  225. /**
  226. * Append debug info
  227. */
  228. $page_key = $this->_get_page_key($_SERVER['REQUEST_URI'], false);
  229. $time_total = w3_microtime() - $this->_time_start;
  230. $debug_info = $this->_get_debug_info($page_key, false, $this->cache_reject_reason, false, $time_total);
  231. $this->_append_content($buffer, "\r\n\r\n" . $debug_info, false);
  232. }
  233. }
  234. return $buffer;
  235. }
  236. /**
  237. * Flushes all caches
  238. *
  239. * @return boolean
  240. */
  241. function flush()
  242. {
  243. $cache = & $this->_get_cache();
  244. return $cache->flush();
  245. }
  246. /**
  247. * Flushes post cache
  248. *
  249. * @param integer $post_id
  250. * @return boolean
  251. */
  252. function flush_post($post_id)
  253. {
  254. if (!$post_id) {
  255. $post_id = $this->_detect_post_id();
  256. }
  257. if ($post_id) {
  258. $home = get_option('home');
  259. $page_keys = array(
  260. $this->_get_page_key(str_replace($home, '', post_permalink($post_id)), false),
  261. $this->_get_page_key(str_replace($home, '', post_permalink($post_id)), 'gzip'),
  262. $this->_get_page_key(str_replace($home, '', post_permalink($post_id)), 'deflate'),
  263. $this->_get_page_key('/', false),
  264. $this->_get_page_key('/', 'gzip'),
  265. $this->_get_page_key('/', 'deflate')
  266. );
  267. $cache = & $this->_get_cache();
  268. foreach ($page_keys as $page_key) {
  269. $cache->delete($page_key);
  270. }
  271. }
  272. return false;
  273. }
  274. /**
  275. * Returns object instance
  276. *
  277. * @return W3_PgCache
  278. */
  279. function &instance()
  280. {
  281. static $instances = array();
  282. if (!isset($instances[0])) {
  283. $class = __CLASS__;
  284. $instances[0] = & new $class();
  285. }
  286. return $instances[0];
  287. }
  288. /**
  289. * Checks if can we do cache logic
  290. *
  291. * @return boolean
  292. */
  293. function _can_cache()
  294. {
  295. /**
  296. * Skip if disabled
  297. */
  298. if (!$this->_config->get_boolean('pgcache.enabled')) {
  299. $this->cache_reject_reason = 'page caching is disabled';
  300. return false;
  301. }
  302. /**
  303. * Skip if posting
  304. */
  305. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  306. $this->cache_reject_reason = 'request method is POST';
  307. return false;
  308. }
  309. /**
  310. * Skip if session defined
  311. */
  312. if (defined('SID') && SID != '') {
  313. $this->cache_reject_reason = 'session is started';
  314. return false;
  315. }
  316. /**
  317. * Skip if there is query in the request uri
  318. */
  319. if (!$this->_config->get_boolean('pgcache.cache.query') && strstr($_SERVER['REQUEST_URI'], '?') !== false) {
  320. $this->cache_reject_reason = 'request URI contains query';
  321. return false;
  322. }
  323. /**
  324. * Check request URI
  325. */
  326. if (!in_array($_SERVER['PHP_SELF'], $this->_config->get_array('pgcache.accept.files')) && !$this->_check_request_uri()) {
  327. $this->cache_reject_reason = 'request URI is rejected';
  328. return false;
  329. }
  330. /**
  331. * Check User Agent
  332. */
  333. if (!$this->_check_ua()) {
  334. $this->cache_reject_reason = 'user agent is rejected';
  335. return false;
  336. }
  337. /**
  338. * Check WordPress cookies
  339. */
  340. if (!$this->_check_cookies()) {
  341. $this->cache_reject_reason = 'cookie is rejected';
  342. return false;
  343. }
  344. /**
  345. * Skip if user is logged in
  346. */
  347. if ($this->_config->get_boolean('pgcache.reject.logged') && !$this->_check_logged_in()) {
  348. $this->cache_reject_reason = 'user is logged in';
  349. return false;
  350. }
  351. return true;
  352. }
  353. /**
  354. * Checks if can we do cache logic
  355. *
  356. * @param string $buffer
  357. * @return boolean
  358. */
  359. function _can_cache2()
  360. {
  361. /**
  362. * Skip if caching is disabled
  363. */
  364. if (!$this->_caching) {
  365. return false;
  366. }
  367. /**
  368. * Don't cache 404 pages
  369. */
  370. if (!$this->_config->get_boolean('pgcache.cache.404') && function_exists('is_404') && is_404()) {
  371. $this->cache_reject_reason = 'page is 404';
  372. return false;
  373. }
  374. /**
  375. * Don't cache homepage
  376. */
  377. if (!$this->_config->get_boolean('pgcache.cache.home') && function_exists('is_home') && is_home()) {
  378. $this->cache_reject_reason = 'page is home';
  379. return false;
  380. }
  381. /**
  382. * Don't cache feed
  383. */
  384. if (!$this->_config->get_boolean('pgcache.cache.feed') && function_exists('is_feed') && is_feed()) {
  385. $this->cache_reject_reason = 'page is feed';
  386. return false;
  387. }
  388. return true;
  389. }
  390. /**
  391. * Returns cache object
  392. *
  393. * @return W3_Cache_Base
  394. */
  395. function &_get_cache()
  396. {
  397. static $cache = array();
  398. if (!isset($cache[0])) {
  399. $engine = $this->_config->get_string('pgcache.engine');
  400. switch ($engine) {
  401. case 'memcached':
  402. $engineConfig = array(
  403. 'servers' => $this->_config->get_array('pgcache.memcached.servers'),
  404. 'persistant' => $this->_config->get_boolean('pgcache.memcached.persistant')
  405. );
  406. break;
  407. case 'file':
  408. $engineConfig = array(
  409. 'cache_dir' => W3TC_CACHE_FILE_PGCACHE_DIR
  410. );
  411. break;
  412. case 'file_pgcache':
  413. $engineConfig = array(
  414. 'cache_dir' => W3TC_CACHE_FILE_PGCACHE_DIR,
  415. 'expire' => $this->_lifetime
  416. );
  417. break;
  418. default:
  419. $engineConfig = array();
  420. }
  421. require_once W3TC_LIB_W3_DIR . '/Cache.php';
  422. $cache[0] = & W3_Cache::instance($engine, $engineConfig);
  423. }
  424. return $cache[0];
  425. }
  426. /**
  427. * Checks request URI
  428. *
  429. * @return boolean
  430. */
  431. function _check_request_uri()
  432. {
  433. $auto_reject_uri = array(
  434. 'wp-login',
  435. 'wp-register'
  436. );
  437. foreach ($auto_reject_uri as $uri) {
  438. if (strstr($_SERVER['REQUEST_URI'], $uri) !== false) {
  439. return false;
  440. }
  441. }
  442. foreach ($this->_config->get_array('pgcache.reject.uri') as $expr) {
  443. $expr = trim($expr);
  444. if ($expr != '' && preg_match('@' . $expr . '@i', $_SERVER['REQUEST_URI'])) {
  445. return false;
  446. }
  447. }
  448. return true;
  449. }
  450. /**
  451. * Checks User Agent
  452. *
  453. * @return boolean
  454. */
  455. function _check_ua()
  456. {
  457. foreach ($this->_config->get_array('pgcache.reject.ua') as $ua) {
  458. if (stristr($_SERVER['HTTP_USER_AGENT'], $ua) !== false) {
  459. return false;
  460. }
  461. }
  462. return true;
  463. }
  464. /**
  465. * Checks WordPress cookies
  466. *
  467. * @return boolean
  468. */
  469. function _check_cookies()
  470. {
  471. foreach (array_keys($_COOKIE) as $cookie_name) {
  472. if ($cookie_name == 'wordpress_test_cookie') {
  473. continue;
  474. }
  475. if (preg_match('/^(wp-postpass|comment_author)/', $cookie_name)) {
  476. return false;
  477. }
  478. }
  479. foreach ($this->_config->get_array('pgcache.reject.cookie') as $reject_cookie) {
  480. foreach (array_keys($_COOKIE) as $cookie_name) {
  481. if (strstr($cookie_name, $reject_cookie) !== false) {
  482. return false;
  483. }
  484. }
  485. }
  486. return true;
  487. }
  488. /**
  489. * Check if user is logged in
  490. *
  491. * @return boolean
  492. */
  493. function _check_logged_in()
  494. {
  495. foreach (array_keys($_COOKIE) as $cookie_name) {
  496. if ($cookie_name == 'wordpress_test_cookie') {
  497. continue;
  498. }
  499. if (strpos($cookie_name, 'wordpress') === 0) {
  500. return false;
  501. }
  502. }
  503. return true;
  504. }
  505. /**
  506. * Checks gzip availability
  507. *
  508. * @return boolean
  509. */
  510. function _get_compression()
  511. {
  512. if (!w3_zlib_output_compression() && !headers_sent() && !$this->_is_buggy_ie()) {
  513. $compressions = $this->_get_compressions();
  514. foreach ($compressions as $compression) {
  515. if (stristr($_SERVER['HTTP_ACCEPT_ENCODING'], $compression) !== false) {
  516. return $compression;
  517. }
  518. }
  519. }
  520. return false;
  521. }
  522. /**
  523. * Returns array of supported compressions
  524. *
  525. * @return array
  526. */
  527. function _get_compressions()
  528. {
  529. $compression = $this->_config->get_string('pgcache.compression');
  530. $compressions = array(
  531. false
  532. );
  533. if ($compression != '') {
  534. if (stristr($compression, 'gzip') !== false && function_exists('gzencode')) {
  535. $compressions[] = 'gzip';
  536. }
  537. if (stristr($compression, 'deflate') !== false && function_exists('gzdeflate')) {
  538. $compressions[] = 'deflate';
  539. }
  540. }
  541. return $compressions;
  542. }
  543. /**
  544. * Returns array of response headers
  545. *
  546. * @return array
  547. */
  548. function _get_response_headers()
  549. {
  550. $headers = array();
  551. if (function_exists('headers_list')) {
  552. $headers_list = headers_list();
  553. if ($headers_list) {
  554. foreach ($headers_list as $header) {
  555. list($header_name, $header_value) = explode(': ', $header, 2);
  556. $headers[$header_name] = $header_value;
  557. }
  558. }
  559. }
  560. return $headers;
  561. }
  562. /**
  563. * Checks for buggy IE6 that doesn't support compression
  564. *
  565. * @return boolean
  566. */
  567. function _is_buggy_ie()
  568. {
  569. $ua = $_SERVER['HTTP_USER_AGENT'];
  570. if (strpos($ua, 'Mozilla/4.0 (compatible; MSIE ') === 0 && strpos($ua, 'Opera') === false) {
  571. $version = (float) substr($ua, 30);
  572. return ($version < 6 || ($version == 6 && strpos($ua, 'SV1') === false));
  573. }
  574. return false;
  575. }
  576. /**
  577. * Returns array of data headers
  578. *
  579. * @return array
  580. */
  581. function _get_cached_headers()
  582. {
  583. $data_headers = array();
  584. $cache_headers = $this->_config->get_array('pgcache.cache.headers');
  585. $response_headers = $this->_get_response_headers();
  586. foreach ($response_headers as $header_name => $header_value) {
  587. foreach ($cache_headers as $cache_header_name) {
  588. if (strcasecmp($header_name, $cache_header_name) == 0) {
  589. $data_headers[$header_name] = $header_value;
  590. }
  591. }
  592. }
  593. return $data_headers;
  594. }
  595. /**
  596. * Returns page key
  597. *
  598. * @param string $request_uri
  599. * @param string $compression
  600. * @return string
  601. */
  602. function _get_page_key($request_uri, $compression)
  603. {
  604. if ($this->_config->get_string('pgcache.engine') == 'file_pgcache') {
  605. $key = preg_replace('~[/\\\]+~', '/', $key);
  606. $key = preg_replace('~\?.*$~', '', $request_uri);
  607. $key = str_replace(w3_get_site_path(), '/', $key);
  608. $key = str_replace('/index.php', '/', $key);
  609. if ($key == '') {
  610. $key = '/';
  611. }
  612. if (substr($key, -1) == '/') {
  613. $key .= '_default_.html';
  614. }
  615. $key = ltrim($key, '/');
  616. if (!empty($compression)) {
  617. $key .= '.' . $compression;
  618. }
  619. } else {
  620. $blogname = w3_get_blogname();
  621. if ($blogname == '') {
  622. $blogname = $_SERVER['HTTP_HOST'];
  623. }
  624. $key = sprintf('w3tc_%s_page_%s', md5($blogname), md5($request_uri));
  625. if (!empty($compression)) {
  626. $key .= '_' . $compression;
  627. }
  628. }
  629. return $key;
  630. }
  631. /**
  632. * Detects post ID
  633. *
  634. * @return integer
  635. */
  636. function _detect_post_id()
  637. {
  638. global $posts, $comment_post_ID, $post_ID;
  639. if ($post_ID) {
  640. return $post_ID;
  641. } elseif ($comment_post_ID) {
  642. return $comment_post_ID;
  643. } elseif (is_single() || is_page() && count($posts)) {
  644. return $posts[0]->ID;
  645. } elseif (isset($_REQUEST['p'])) {
  646. return (integer) $_REQUEST['p'];
  647. }
  648. return 0;
  649. }
  650. /**
  651. * Returns debug info
  652. *
  653. * @param string $page_key
  654. * @param boolean $cache
  655. * @param string $reason
  656. * @param boolean $status
  657. * @param double $time
  658. * @return string
  659. */
  660. function _get_debug_info($page_key, $cache, $reason, $status, $time)
  661. {
  662. $debug_info = "<!-- W3 Total Cache: Page cache debug info:\r\n";
  663. $debug_info .= sprintf("%s%s\r\n", str_pad('Engine: ', 20), w3_get_engine_name($this->_config->get_string('pgcache.engine')));
  664. $debug_info .= sprintf("%s%s\r\n", str_pad('Key: ', 20), $page_key);
  665. $debug_info .= sprintf("%s%s\r\n", str_pad('Caching: ', 20), ($cache ? 'enabled' : 'disabled'));
  666. if (!$cache) {
  667. $debug_info .= sprintf("%s%s\r\n", str_pad('Reject reason: ', 20), $reason);
  668. }
  669. $debug_info .= sprintf("%s%s\r\n", str_pad('Status: ', 20), ($status ? 'cached' : 'not cached'));
  670. $debug_info .= sprintf("%s%.3fs\r\n", str_pad('Creation Time: ', 20), $time);
  671. $headers = $this->_get_response_headers();
  672. if (count($headers)) {
  673. $debug_info .= "Header info:\r\n";
  674. foreach ($headers as $header_name => $header_value) {
  675. $debug_info .= sprintf("%s%s\r\n", str_pad($header_name . ': ', 20), $header_value);
  676. }
  677. }
  678. $debug_info .= '-->';
  679. return $debug_info;
  680. }
  681. /**
  682. * Appends content to data content
  683. *
  684. * @param array $data
  685. * @param string $content
  686. * @param string $compression
  687. * @return string
  688. */
  689. function _append_content(&$data, $content, $compression)
  690. {
  691. switch ($compression) {
  692. case false:
  693. $data .= $content;
  694. break;
  695. case 'gzip':
  696. $data = (function_exists('gzdecode') ? gzdecode($data) : w3_gzdecode($data));
  697. $data .= $content;
  698. $data = gzencode($data);
  699. break;
  700. case 'deflate':
  701. $data = gzinflate($data);
  702. $data .= $content;
  703. $data = gzdeflate($data);
  704. break;
  705. }
  706. }
  707. /**
  708. * Sends headers
  709. *
  710. * @param array $headers
  711. * @return boolean
  712. */
  713. function _headers($headers)
  714. {
  715. if (!headers_sent()) {
  716. foreach ($headers as $name => $value) {
  717. $header = ($name == 'Status' ? $value : $name . ': ' . $value);
  718. @header($header);
  719. }
  720. return true;
  721. }
  722. return false;
  723. }
  724. /**
  725. * Sends headers
  726. * @param boolean $is_404
  727. * @param string $etag
  728. * @param integer $time
  729. * @param string $compression
  730. * @param array $custom_headers
  731. * @return boolean
  732. */
  733. function _send_headers($is_404, $time, $etag, $compression, $custom_headers = array())
  734. {
  735. $exit = false;
  736. $headers = array();
  737. $curr_time = time();
  738. $expires = $time + $this->_lifetime;
  739. $max_age = ($expires > $curr_time ? $expires - $curr_time : 0);
  740. if ($is_404) {
  741. /**
  742. * Add 404 header
  743. */
  744. $headers = array_merge($headers, array(
  745. 'Status' => 'HTTP/1.1 404 Not Found'
  746. ));
  747. } elseif ($this->_check_modified_since($time) || $this->_check_match($etag)) {
  748. /**
  749. * Add 304 header
  750. */
  751. $headers = array_merge($headers, array(
  752. 'Status' => 'HTTP/1.1 304 Not Modified'
  753. ));
  754. /**
  755. * Don't send content if it isn't modified
  756. */
  757. $exit = true;
  758. }
  759. /**
  760. * Add default headers
  761. */
  762. $headers = array_merge($headers, array(
  763. 'Pragma' => 'public',
  764. 'Expires' => $this->_gmdate($expires),
  765. 'Last-Modified' => $this->_gmdate($time),
  766. 'Cache-Control' => sprintf('max-age=%d, public, must-revalidate, proxy-revalidate', $max_age),
  767. 'Vary' => 'Cookie',
  768. 'Etag' => $etag
  769. ));
  770. if ($compression) {
  771. /**
  772. * Add Content-Encoding header
  773. */
  774. $headers = array_merge($headers, array(
  775. 'Vary' => 'Accept-Encoding, Cookie',
  776. 'Content-Encoding' => $compression
  777. ));
  778. }
  779. /**
  780. * Add custom headers
  781. */
  782. $headers = array_merge($headers, $custom_headers);
  783. /**
  784. * Send headers to client
  785. */
  786. $result = $this->_headers($headers);
  787. if ($exit) {
  788. exit();
  789. }
  790. return $result;
  791. }
  792. /**
  793. * Checks if User Agent is mobile
  794. *
  795. * @return boolean
  796. */
  797. function _is_mobile()
  798. {
  799. $mobile_agents = $this->_config->get_array('pgcache.mobile.agents');
  800. foreach ($mobile_agents as $mobile_agent) {
  801. if (stristr($_SERVER['HTTP_USER_AGENT'], $mobile_agent) !== false) {
  802. return true;
  803. }
  804. }
  805. return false;
  806. }
  807. /**
  808. * Check if content was modified by time
  809. * @param integer $time
  810. * @return boolean
  811. */
  812. function _check_modified_since($time)
  813. {
  814. if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  815. $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  816. // IE has tacked on extra data to this header, strip it
  817. if (($semicolon = strrpos($if_modified_since, ';')) !== false) {
  818. $if_modified_since = substr($if_modified_since, 0, $semicolon);
  819. }
  820. return ($time == strtotime($if_modified_since));
  821. }
  822. return false;
  823. }
  824. /**
  825. * Check if content was modified by etag
  826. * @param string $etag
  827. * @return boolean
  828. */
  829. function _check_match($etag)
  830. {
  831. if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
  832. $if_none_match = (get_magic_quotes_gpc() ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : $_SERVER['HTTP_IF_NONE_MATCH']);
  833. $client_etags = explode(',', $if_none_match);
  834. foreach ($client_etags as $client_etag) {
  835. $client_etag = trim($client_etag);
  836. if ($etag == $client_etag) {
  837. return true;
  838. }
  839. }
  840. }
  841. return false;
  842. }
  843. /**
  844. * Returns GMT date
  845. * @param integer $time
  846. * @return string
  847. */
  848. function _gmdate($time)
  849. {
  850. return gmdate('D, d M Y H:i:s \G\M\T', $time);
  851. }
  852. /**
  853. * Bad Behavior support
  854. * @return void
  855. */
  856. function _bad_behavior()
  857. {
  858. if (file_exists(WP_CONTENT_DIR . '/plugins/bad-behavior/bad-behavior-generic.php')) {
  859. $bb_file = WP_CONTENT_DIR . '/plugins/bad-behavior/bad-behavior-generic.php';
  860. } elseif (file_exists(WP_CONTENT_DIR . '/plugins/Bad-Behavior/bad-behavior-generic.php')) {
  861. $bb_file = WP_CONTENT_DIR . '/plugins/Bad-Behavior/bad-behavior-generic.php';
  862. } else {
  863. $bb_file = false;
  864. }
  865. if ($bb_file) {
  866. require_once $bb_file;
  867. }
  868. }
  869. }