PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class_cache.php

https://github.com/MightyGorgon/icy_phoenix
PHP | 1016 lines | 692 code | 130 blank | 194 comment | 89 complexity | cf5769385d06e56b8ee60bb724bf223a MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Icy Phoenix is based on phpBB
  13. * @copyright (c) 2008 phpBB Group
  14. *
  15. */
  16. if (!defined('IN_ICYPHOENIX'))
  17. {
  18. die('Hacking attempt');
  19. }
  20. /**
  21. * We store mysqli results as keys in SplObjectStorage / DbObjectStorage, but those need keys to be objects.
  22. * When pulled out from cache, however, we do not have an object, so we create a fake ID using this class.
  23. */
  24. class sql_db_fake_id
  25. {
  26. public $id;
  27. public function __construct($id)
  28. {
  29. $this->id = $id;
  30. }
  31. }
  32. /**
  33. * This class implements a key-value store based on SplObjectStorage,
  34. * but special-cases sql_db_fake_id to compare equal if same ID (in getHash).
  35. *
  36. * sql_db_fake_id is necessary because mysqli queries return objects, not resources like mysql does.
  37. */
  38. class DbObjectStorage extends SplObjectStorage
  39. {
  40. public function getHash($o)
  41. {
  42. if ($o instanceof sql_db_fake_id)
  43. {
  44. return 'sql_db_fake_id:' . $o->id;
  45. }
  46. else
  47. {
  48. return parent::getHash($o);
  49. }
  50. }
  51. }
  52. /**
  53. * Cache management class
  54. */
  55. class acm
  56. {
  57. var $vars = array();
  58. var $var_expires = array();
  59. var $is_modified = false;
  60. var $sql_rowset;
  61. var $sql_row_pointer;
  62. var $sql_query_id = '';
  63. var $cache_dir = '';
  64. var $cache_dir_sql = '';
  65. var $cache_dir_backup = '';
  66. var $cache_dirs = array();
  67. var $last_query_id = 1;
  68. var $use_old_ip_cache = false;
  69. /**
  70. * Set cache path
  71. */
  72. function __construct()
  73. {
  74. $this->cache_dir = defined('MAIN_CACHE_FOLDER') ? MAIN_CACHE_FOLDER : 'cache/';
  75. $this->cache_dir_sql = defined('SQL_CACHE_FOLDER') ? SQL_CACHE_FOLDER : 'cache/sql/';
  76. $this->cache_dir_backup = $this->cache_dir;
  77. $this->sql_rowset = new DbObjectStorage();
  78. $this->sql_row_pointer = new DbObjectStorage();
  79. $this->cache_dirs = defined('MAIN_CACHE_FOLDER') ? array(MAIN_CACHE_FOLDER, CMS_CACHE_FOLDER, FORUMS_CACHE_FOLDER, POSTS_CACHE_FOLDER, SQL_CACHE_FOLDER, TOPICS_CACHE_FOLDER, USERS_CACHE_FOLDER) : array($this->cache_dir, $this->cache_dir_sql);
  80. }
  81. /**
  82. * Load global cache
  83. */
  84. function load()
  85. {
  86. return $this->_read('data_global', $this->cache_dir);
  87. }
  88. /**
  89. * Unload cache object
  90. */
  91. function unload()
  92. {
  93. $this->save();
  94. unset($this->vars);
  95. unset($this->var_expires);
  96. unset($this->sql_rowset);
  97. unset($this->sql_row_pointer);
  98. $this->vars = array();
  99. $this->var_expires = array();
  100. $this->sql_rowset = new DbObjectStorage;
  101. $this->sql_row_pointer = new DbObjectStorage;
  102. }
  103. /**
  104. * Save modified objects
  105. */
  106. function save()
  107. {
  108. if (!$this->is_modified)
  109. {
  110. return;
  111. }
  112. if (!$this->_write('data_global', $this->vars, $this->var_expires, $this->cache_dir))
  113. {
  114. // Now, this occurred how often? ... phew, just tell the user then...
  115. if (!@is_writable($this->cache_dir))
  116. {
  117. // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload())
  118. die($this->cache_dir . ' is NOT writable.');
  119. exit;
  120. }
  121. die('Not able to open ' . $this->cache_dir . 'data_global.' . PHP_EXT);
  122. exit;
  123. }
  124. $this->is_modified = false;
  125. }
  126. /**
  127. * Tidy cache
  128. */
  129. function tidy()
  130. {
  131. foreach ($this->cache_dirs as $cache_folder)
  132. {
  133. $cache_folder = $this->validate_cache_folder($cache_folder, false, true);
  134. $dir = @opendir($cache_folder);
  135. if (!$dir)
  136. {
  137. return;
  138. }
  139. $time = time();
  140. while (($entry = readdir($dir)) !== false)
  141. {
  142. if (!preg_match('/^(sql_|data_(?!global))/', $entry))
  143. {
  144. continue;
  145. }
  146. $expired = $this->is_expired($time, $entry, $cache_folder);
  147. if ($expired)
  148. {
  149. $this->remove_file($entry, false, $cache_folder);
  150. }
  151. }
  152. closedir($dir);
  153. }
  154. if (file_exists($this->cache_dir . 'data_global.' . PHP_EXT))
  155. {
  156. if (!sizeof($this->vars))
  157. {
  158. $this->load();
  159. }
  160. foreach ($this->var_expires as $var_name => $expires)
  161. {
  162. if ($time >= $expires)
  163. {
  164. $this->destroy($var_name);
  165. }
  166. }
  167. }
  168. set_config('cron_cache_last_run', time());
  169. }
  170. /**
  171. * Get saved cache object
  172. */
  173. function get($var_name)
  174. {
  175. if ($var_name[0] == '_')
  176. {
  177. if (!$this->_exists($var_name))
  178. {
  179. return false;
  180. }
  181. return $this->_read('data' . $var_name, $this->cache_dir);
  182. }
  183. else
  184. {
  185. return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
  186. }
  187. }
  188. /**
  189. * Put data into cache
  190. */
  191. function put($var_name, $var, $ttl = 31536000)
  192. {
  193. if ($var_name[0] == '_')
  194. {
  195. $this->_write('data' . $var_name, $var, time() + $ttl);
  196. }
  197. else
  198. {
  199. $this->vars[$var_name] = $var;
  200. $this->var_expires[$var_name] = time() + $ttl;
  201. $this->is_modified = true;
  202. }
  203. }
  204. /**
  205. * Purge cache data
  206. */
  207. function purge()
  208. {
  209. // Purge all cache files
  210. foreach ($this->cache_dirs as $cache_folder)
  211. {
  212. $cache_folder = $this->validate_cache_folder($cache_folder, false, true);
  213. $dir = @opendir($cache_folder);
  214. if (!$dir)
  215. {
  216. return;
  217. }
  218. while (($entry = readdir($dir)) !== false)
  219. {
  220. if ((strpos($entry, 'sql_') !== 0) && (strpos($entry, 'data_') !== 0) && (strpos($entry, 'ctpl_') !== 0) && (strpos($entry, 'tpl_') !== 0))
  221. {
  222. continue;
  223. }
  224. $this->remove_file($entry, false, $cache_folder);
  225. }
  226. closedir($dir);
  227. }
  228. unset($this->vars);
  229. unset($this->var_expires);
  230. unset($this->sql_rowset);
  231. unset($this->sql_row_pointer);
  232. $this->vars = array();
  233. $this->var_expires = array();
  234. $this->sql_rowset = new DbObjectStorage;
  235. $this->sql_row_pointer = new DbObjectStorage;
  236. $this->is_modified = false;
  237. }
  238. /**
  239. * Destroy cache data
  240. */
  241. function destroy($var_name, $table = '', $cache_folder = '')
  242. {
  243. if (($var_name == 'sql') && !empty($table))
  244. {
  245. if (!is_array($table))
  246. {
  247. $table = array($table);
  248. }
  249. $cache_folder = $this->validate_cache_folder($cache_folder, true, false);
  250. $dir = @opendir($cache_folder);
  251. if (!$dir)
  252. {
  253. return;
  254. }
  255. while (($entry = readdir($dir)) !== false)
  256. {
  257. if (strpos($entry, 'sql_') !== 0)
  258. {
  259. continue;
  260. }
  261. $query = $this->get_query_string($entry);
  262. if (empty($query))
  263. {
  264. continue;
  265. }
  266. foreach ($table as $check_table)
  267. {
  268. // Better catch partial table names than no table names. ;)
  269. if (strpos($query, $check_table) !== false)
  270. {
  271. $this->remove_file($entry, false, $cache_folder);
  272. break;
  273. }
  274. }
  275. }
  276. closedir($dir);
  277. return;
  278. }
  279. if (!$this->_exists($var_name))
  280. {
  281. return;
  282. }
  283. if ($var_name[0] == '_')
  284. {
  285. $this->remove_file('data' . $var_name . '.' . PHP_EXT, true, $this->cache_dir);
  286. }
  287. elseif (isset($this->vars[$var_name]))
  288. {
  289. $this->is_modified = true;
  290. unset($this->vars[$var_name]);
  291. unset($this->var_expires[$var_name]);
  292. // We save here to let the following cache hits succeed
  293. $this->save();
  294. }
  295. }
  296. /**
  297. * Destroy cache data files
  298. */
  299. function destroy_datafiles($datafiles, $cache_folder = '', $prefix = 'data', $prefix_lookup = false)
  300. {
  301. $deleted = 0;
  302. if (empty($datafiles))
  303. {
  304. return $deleted;
  305. }
  306. $cache_folder = $this->validate_cache_folder($cache_folder, false, true);
  307. $datafiles = !is_array($datafiles) ? array($datafiles) : $datafiles;
  308. if (!$prefix_lookup)
  309. {
  310. foreach ($datafiles as $datafile)
  311. {
  312. $file_deleted = $this->remove_file($prefix . $datafile . '.' . PHP_EXT, false, $cache_folder);
  313. $deleted = $file_deleted ? $deleted++ : $deleted;
  314. }
  315. }
  316. else
  317. {
  318. $dir = @opendir($cache_folder);
  319. if (!$dir)
  320. {
  321. return;
  322. }
  323. while (($entry = readdir($dir)) !== false)
  324. {
  325. foreach ($datafiles as $datafile)
  326. {
  327. if ((strpos($entry, $prefix . $datafile) === 0) && (substr($entry, -(strlen(PHP_EXT) + 1)) === ('.' . PHP_EXT)))
  328. {
  329. $file_deleted = $this->remove_file($entry, false, $cache_folder);
  330. $deleted = $file_deleted ? $deleted++ : $deleted;
  331. break;
  332. }
  333. }
  334. }
  335. }
  336. return $deleted;
  337. }
  338. /**
  339. * Check if a given cache entry exist
  340. */
  341. function _exists($var_name)
  342. {
  343. if ($var_name[0] == '_')
  344. {
  345. return file_exists($this->cache_dir . 'data' . $var_name . '.' . PHP_EXT);
  346. }
  347. else
  348. {
  349. if (!sizeof($this->vars))
  350. {
  351. $this->load();
  352. }
  353. if (!isset($this->var_expires[$var_name]))
  354. {
  355. return false;
  356. }
  357. return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
  358. }
  359. }
  360. /**
  361. * Build query Hash
  362. */
  363. function sql_query_hash($query = '')
  364. {
  365. return md5($query);
  366. }
  367. /**
  368. * Load cached sql query
  369. */
  370. function sql_load($query, $cache_prefix = '', $cache_folder = '')
  371. {
  372. $cache_prefix = 'sql_' . $cache_prefix;
  373. $cache_folder = $this->validate_cache_folder($cache_folder, true, false);
  374. // Remove extra spaces and tabs
  375. $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
  376. if (($rowset = $this->_read($cache_prefix . $this->sql_query_hash($query), $cache_folder)) === false)
  377. {
  378. return false;
  379. }
  380. $this->sql_query_id = new sql_db_fake_id($this->last_query_id++);
  381. $this->sql_rowset[$this->sql_query_id] = $rowset;
  382. $this->sql_row_pointer[$this->sql_query_id] = 0;
  383. return $this->sql_query_id;
  384. }
  385. /**
  386. * Save sql query
  387. */
  388. function sql_save($query, &$query_result, $ttl = CACHE_SQL_EXPIRY, $cache_prefix = '', $cache_folder = '')
  389. {
  390. global $db;
  391. $cache_prefix = 'sql_' . $cache_prefix;
  392. $cache_folder = $this->validate_cache_folder($cache_folder, true, true);
  393. // Remove extra spaces and tabs
  394. $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
  395. $this->sql_query_id = new sql_db_fake_id($this->last_query_id++);
  396. $this->sql_row_pointer[$this->sql_query_id] = 0;
  397. $this->sql_rowset[$this->sql_query_id] = $db->sql_fetchrowset($query_result);
  398. $db->sql_freeresult($query_result);
  399. if ($this->_write($cache_prefix . $this->sql_query_hash($query), $this->sql_rowset[$this->sql_query_id], time() + $ttl, $query, $cache_folder))
  400. {
  401. $query_result = $this->sql_query_id;
  402. }
  403. }
  404. /**
  405. * Check if a given sql query exist in cache
  406. */
  407. function sql_exists($query_id)
  408. {
  409. return isset($this->sql_rowset[$query_id]);
  410. }
  411. /**
  412. * Fetch row from cache (database)
  413. */
  414. function sql_fetchrow($query_id)
  415. {
  416. if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
  417. {
  418. // SplObjectStorage doesn't support ++
  419. $newp = $this->sql_row_pointer[$query_id];
  420. $this->sql_row_pointer[$query_id] += 1;
  421. return $this->sql_rowset[$query_id][$newp];
  422. }
  423. return false;
  424. }
  425. /**
  426. * Fetch a field from the current row of a cached database result (database)
  427. */
  428. function sql_fetchfield($query_id, $field)
  429. {
  430. if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
  431. {
  432. // SplObjectStorage doesn't support ++
  433. $newp = $this->sql_row_pointer[$query_id];
  434. $this->sql_row_pointer[$query_id] += 1;
  435. return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$newp][$field] : false;
  436. }
  437. return false;
  438. }
  439. /**
  440. * Seek a specific row in an a cached database result (database)
  441. */
  442. function sql_rowseek($rownum, $query_id)
  443. {
  444. if ($rownum >= sizeof($this->sql_rowset[$query_id]))
  445. {
  446. return false;
  447. }
  448. $this->sql_row_pointer[$query_id] = $rownum;
  449. return true;
  450. }
  451. /**
  452. * Free memory used for a cached database result (database)
  453. */
  454. function sql_freeresult($query_id)
  455. {
  456. if (!isset($this->sql_rowset[$query_id]))
  457. {
  458. return false;
  459. }
  460. unset($this->sql_rowset[$query_id]);
  461. unset($this->sql_row_pointer[$query_id]);
  462. return true;
  463. }
  464. /**
  465. * Read cached data from a specified file
  466. *
  467. * @access private
  468. * @param string $filename Filename to write
  469. * @return mixed False if an error was encountered, otherwise the data type of the cached data
  470. */
  471. function _read($filename, $cache_folder = '')
  472. {
  473. if (!empty($this->use_old_ip_cache))
  474. {
  475. return $this->_read_ip($filename, $cache_folder);
  476. }
  477. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  478. $file = $cache_folder . $filename . '.' . PHP_EXT;
  479. $type = substr($filename, 0, strpos($filename, '_'));
  480. if (!file_exists($file))
  481. {
  482. return false;
  483. }
  484. if (!($handle = @fopen($file, 'rb')))
  485. {
  486. return false;
  487. }
  488. // Skip the PHP header
  489. fgets($handle);
  490. if ($filename == 'data_global')
  491. {
  492. $this->vars = $this->var_expires = array();
  493. $time = time();
  494. while (($expires = (int) fgets($handle)) && !feof($handle))
  495. {
  496. // Number of bytes of data
  497. $bytes = substr(fgets($handle), 0, -1);
  498. if (!is_numeric($bytes) || ($bytes = (int) $bytes) === 0)
  499. {
  500. // We cannot process the file without a valid number of bytes so we discard it
  501. fclose($handle);
  502. $this->vars = $this->var_expires = array();
  503. $this->is_modified = false;
  504. $this->remove_file($file, false, $cache_folder);
  505. return false;
  506. }
  507. if ($time >= $expires)
  508. {
  509. fseek($handle, $bytes, SEEK_CUR);
  510. continue;
  511. }
  512. $var_name = substr(fgets($handle), 0, -1);
  513. // Read the length of bytes that consists of data.
  514. $data = fread($handle, $bytes - strlen($var_name));
  515. $data = @unserialize($data);
  516. // Don't use the data if it was invalid
  517. if ($data !== false)
  518. {
  519. $this->vars[$var_name] = $data;
  520. $this->var_expires[$var_name] = $expires;
  521. }
  522. // Absorb the LF
  523. fgets($handle);
  524. }
  525. fclose($handle);
  526. $this->is_modified = false;
  527. return true;
  528. }
  529. else
  530. {
  531. $data = false;
  532. $line = 0;
  533. while (($buffer = fgets($handle)) && !feof($handle))
  534. {
  535. $buffer = substr($buffer, 0, -1); // Remove the LF
  536. // $buffer is only used to read integers
  537. // if it is non numeric we have an invalid
  538. // cache file, which we will now remove.
  539. if (!is_numeric($buffer))
  540. {
  541. break;
  542. }
  543. if ($line == 0)
  544. {
  545. $expires = (int) $buffer;
  546. if (time() >= $expires)
  547. {
  548. break;
  549. }
  550. if ($type == 'sql')
  551. {
  552. // Skip the query
  553. fgets($handle);
  554. }
  555. }
  556. elseif ($line == 1)
  557. {
  558. $bytes = (int) $buffer;
  559. // Never should have 0 bytes
  560. if (!$bytes)
  561. {
  562. break;
  563. }
  564. // Grab the serialized data
  565. $data = fread($handle, $bytes);
  566. // Read 1 byte, to trigger EOF
  567. fread($handle, 1);
  568. if (!feof($handle))
  569. {
  570. // Somebody tampered with our data
  571. $data = false;
  572. }
  573. break;
  574. }
  575. else
  576. {
  577. // Something went wrong
  578. break;
  579. }
  580. $line++;
  581. }
  582. fclose($handle);
  583. // unserialize if we got some data
  584. $data = ($data !== false) ? @unserialize($data) : $data;
  585. if ($data === false)
  586. {
  587. $this->remove_file($file, false, $cache_folder);
  588. return false;
  589. }
  590. return $data;
  591. }
  592. }
  593. /**
  594. * Write cache data to a specified file
  595. *
  596. * 'data_global' is a special case and the generated format is different for this file:
  597. * <code>
  598. * < ? php exit; ? >
  599. * (expiration)
  600. * (length of var and serialised data)
  601. * (var)
  602. * (serialised data)
  603. * ... (repeat)
  604. * </code>
  605. *
  606. * The other files have a similar format:
  607. * <code>
  608. * < ? php exit; ? >
  609. * (expiration)
  610. * (query) [SQL files only]
  611. * (length of serialised data)
  612. * (serialised data)
  613. * </code>
  614. *
  615. * @access private
  616. * @param string $filename Filename to write
  617. * @param mixed $data Data to store
  618. * @param int $expires Timestamp when the data expires
  619. * @param string $query Query when caching SQL queries
  620. * @return bool True if the file was successfully created, otherwise false
  621. */
  622. function _write($filename, $data = null, $expires = 0, $query = '', $cache_folder = '')
  623. {
  624. if (!empty($this->use_old_ip_cache))
  625. {
  626. return $this->_write_ip($filename, $data, $expires, $query, $cache_folder);
  627. }
  628. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  629. $file = $cache_folder . $filename . '.' . PHP_EXT;
  630. if ($handle = @fopen($file, 'wb'))
  631. {
  632. @flock($handle, LOCK_EX);
  633. // File header
  634. fwrite($handle, '<' . '?php exit; ?' . '>');
  635. if ($filename == 'data_global')
  636. {
  637. // Global data is a different format
  638. foreach ($this->vars as $var => $data)
  639. {
  640. if ((strpos($var, "\r") !== false) || (strpos($var, "\n") !== false))
  641. {
  642. // CR/LF would cause fgets() to read the cache file incorrectly
  643. // do not cache test entries, they probably won't be read back
  644. // the cache keys should really be alphanumeric with a few symbols.
  645. continue;
  646. }
  647. $data = serialize($data);
  648. // Write out the expiration time
  649. fwrite($handle, "\n" . $this->var_expires[$var] . "\n");
  650. // Length of the remaining data for this var (ignoring two LF's)
  651. fwrite($handle, strlen($data . $var) . "\n");
  652. fwrite($handle, $var . "\n");
  653. fwrite($handle, $data);
  654. }
  655. }
  656. else
  657. {
  658. fwrite($handle, "\n" . $expires . "\n");
  659. if (strpos($filename, 'sql_') === 0)
  660. {
  661. fwrite($handle, $query . "\n");
  662. }
  663. $data = serialize($data);
  664. fwrite($handle, strlen($data) . "\n");
  665. fwrite($handle, $data);
  666. }
  667. @flock($handle, LOCK_UN);
  668. fclose($handle);
  669. if (!function_exists('phpbb_chmod'))
  670. {
  671. include(IP_ROOT_PATH . 'includes/functions.' . PHP_EXT);
  672. }
  673. phpbb_chmod($file, CHMOD_READ | CHMOD_WRITE);
  674. return true;
  675. }
  676. return false;
  677. }
  678. /**
  679. * Read cached data (IP Version)
  680. *
  681. * @access private
  682. * @param string $filename Filename to write
  683. * @return mixed False if an error was encountered, otherwise the data type of the cached data
  684. */
  685. function _read_ip($filename, $cache_folder = '')
  686. {
  687. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  688. $file = $cache_folder . $filename . '.' . PHP_EXT;
  689. if (file_exists($file))
  690. {
  691. @include($file);
  692. if (!empty($expired))
  693. {
  694. $this->remove_file($filename . '.' . PHP_EXT, true, $cache_folder);
  695. return false;
  696. }
  697. }
  698. else
  699. {
  700. return false;
  701. }
  702. if ($filename == 'data_global')
  703. {
  704. return true;
  705. }
  706. else
  707. {
  708. return (isset($data)) ? $data : false;
  709. }
  710. }
  711. /**
  712. * Write cache data to a specified file (IP Version)
  713. *
  714. * @access private
  715. * @param string $filename Filename to write
  716. * @param mixed $data Data to store
  717. * @param int $expires Timestamp when the data expires
  718. * @param string $query Query when caching SQL queries
  719. * @return bool True if the file was successfully created, otherwise false
  720. */
  721. function _write_ip($filename, $data = null, $expires = 0, $query = '', $cache_folder = '')
  722. {
  723. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  724. $file = $cache_folder . $filename . '.' . PHP_EXT;
  725. if ($fp = @fopen($file, 'wb'))
  726. {
  727. @flock($fp, LOCK_EX);
  728. $file_content = "<" . "?php\nif (!defined('IN_ICYPHOENIX')) exit;\n\n";
  729. $file_content .= "\$created = " . time() . "; // " . gmdate('Y/m/d - H:i:s') . "\n";
  730. if ($filename == 'data_global')
  731. {
  732. $file_content .= "\n\$this->vars = " . var_export($data, true) . ";\n";
  733. $file_content .= "\n\$this->var_expires = " . var_export($expires, true) . ";\n";
  734. }
  735. elseif (!empty($query))
  736. {
  737. $file_content .= "/* " . str_replace('*/', '*\/', $query) . " */\n";
  738. $file_content .= "\$expired = (time() >= " . $expires . ") ? true : false;\nif (\$expired) { return; }\n";
  739. $file_content .= "\n\$this->sql_rowset[\$this->sql_query_id] = " . (sizeof($this->sql_rowset[$this->sql_query_id]) ? "unserialize(" . var_export(serialize($this->sql_rowset[$this->sql_query_id]), true) . ");" : 'array();') . "\n";
  740. }
  741. else
  742. {
  743. $file_content .= "\$expired = (time() >= " . $expires . ") ? true : false;\nif (\$expired) { return; }\n";
  744. $file_content .= "\n\$data = " . (sizeof($data) ? "unserialize(" . var_export(serialize($data), true) . ");" : 'array();') . "\n";
  745. }
  746. $file_content .= "\n?" . ">";
  747. fwrite($fp, $file_content);
  748. @flock($fp, LOCK_UN);
  749. fclose($fp);
  750. if (!function_exists('phpbb_chmod'))
  751. {
  752. include(IP_ROOT_PATH . 'includes/functions.' . PHP_EXT);
  753. }
  754. phpbb_chmod($file, CHMOD_WRITE);
  755. return true;
  756. }
  757. else
  758. {
  759. return false;
  760. }
  761. }
  762. /**
  763. * Removes/unlinks file
  764. */
  765. function remove_file($filename, $check = false, $cache_folder = '')
  766. {
  767. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  768. $cache_filename = $cache_folder . $filename;
  769. if (@file_exists($cache_filename))
  770. {
  771. $file_unlink = @unlink($cache_filename);
  772. if ($check && !$file_unlink && !@is_writable($cache_folder))
  773. {
  774. // Better avoid calling trigger_error
  775. die('Unable to remove ' . $cache_filename . '. Please check directory permissions.');
  776. }
  777. return $file_unlink;
  778. }
  779. return true;
  780. }
  781. /**
  782. * Checks cache folder
  783. */
  784. function validate_cache_folder($cache_folder, $is_sql = false, $deep_check = false)
  785. {
  786. $default_cache_folder = (!empty($is_sql) ? $this->cache_dir_sql : $this->cache_dir);
  787. $cache_folder = (!empty($cache_folder) && in_array($cache_folder, $this->cache_dirs)) ? $cache_folder : $default_cache_folder;
  788. if (!empty($deep_check))
  789. {
  790. $cache_folder = @is_dir($cache_folder) ? $cache_folder : $default_cache_folder;
  791. // This part of code should should ensure realpath folder identified...
  792. $cache_folder = @is_dir($cache_folder) ? $cache_folder : @phpbb_realpath($cache_folder);
  793. }
  794. return $cache_folder;
  795. }
  796. /**
  797. * Checks if cache expired
  798. */
  799. function is_expired($time, $filename, $cache_folder = '')
  800. {
  801. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  802. if (!file_exists($cache_folder . $filename))
  803. {
  804. return false;
  805. }
  806. if (!empty($this->use_old_ip_cache))
  807. {
  808. $expired = true;
  809. @include($cache_folder . $filename);
  810. return (!empty($expired) ? true : false);
  811. }
  812. else
  813. {
  814. if (!($handle = @fopen($cache_folder . $filename, 'rb')))
  815. {
  816. return true;
  817. }
  818. // Skip the PHP header
  819. fgets($handle);
  820. // Skip expiration
  821. $expires = (int) fgets($handle);
  822. fclose($handle);
  823. $expired = ($time >= $expires) ? true : false;
  824. return (!empty($expired) ? true : false);
  825. }
  826. }
  827. /**
  828. * Gets query string
  829. */
  830. function get_query_string($filename, $cache_folder = '')
  831. {
  832. $cache_folder = $this->validate_cache_folder($cache_folder, false, false);
  833. if (!empty($this->use_old_ip_cache))
  834. {
  835. $check_line = @file_get_contents($cache_folder . $filename);
  836. if (empty($check_line))
  837. {
  838. return false;
  839. }
  840. // Now get the contents between /* and */
  841. $query = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
  842. }
  843. else
  844. {
  845. if (!($handle = @fopen($cache_folder . $filename, 'rb')))
  846. {
  847. return false;
  848. }
  849. // Skip the PHP header
  850. fgets($handle);
  851. // Skip expiration
  852. fgets($handle);
  853. // Grab the query, remove the LF
  854. $query = substr(fgets($handle), 0, -1);
  855. fclose($handle);
  856. }
  857. return $query;
  858. }
  859. }
  860. ?>