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

/includes/acm/acm_file.php

http://seo-phpbb.googlecode.com/
PHP | 504 lines | 380 code | 65 blank | 59 comment | 49 complexity | c049108ab3bf999fec959745831c5c12 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package acm
  5. * @version $Id: acm_file.php 8479 2008-03-29 00:22:48Z naderman $
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * ACM File Based Caching
  19. * @package acm
  20. */
  21. class acm
  22. {
  23. var $vars = array();
  24. var $var_expires = array();
  25. var $is_modified = false;
  26. var $sql_rowset = array();
  27. var $sql_row_pointer = array();
  28. var $cache_dir = '';
  29. /**
  30. * Set cache path
  31. */
  32. function acm()
  33. {
  34. global $phpbb_root_path;
  35. $this->cache_dir = $phpbb_root_path . 'cache/';
  36. }
  37. /**
  38. * Load global cache
  39. */
  40. function load()
  41. {
  42. global $phpEx;
  43. if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
  44. {
  45. @include($this->cache_dir . 'data_global.' . $phpEx);
  46. }
  47. else
  48. {
  49. return false;
  50. }
  51. return true;
  52. }
  53. /**
  54. * Unload cache object
  55. */
  56. function unload()
  57. {
  58. $this->save();
  59. unset($this->vars);
  60. unset($this->var_expires);
  61. unset($this->sql_rowset);
  62. unset($this->sql_row_pointer);
  63. $this->vars = array();
  64. $this->var_expires = array();
  65. $this->sql_rowset = array();
  66. $this->sql_row_pointer = array();
  67. }
  68. /**
  69. * Save modified objects
  70. */
  71. function save()
  72. {
  73. if (!$this->is_modified)
  74. {
  75. return;
  76. }
  77. global $phpEx;
  78. if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
  79. {
  80. @flock($fp, LOCK_EX);
  81. fwrite($fp, "<?php\n\$this->vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>");
  82. @flock($fp, LOCK_UN);
  83. fclose($fp);
  84. @chmod($this->cache_dir . 'data_global.' . $phpEx, 0666);
  85. }
  86. else
  87. {
  88. // Now, this occurred how often? ... phew, just tell the user then...
  89. if (!@is_writable($this->cache_dir))
  90. {
  91. trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR);
  92. }
  93. trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR);
  94. }
  95. $this->is_modified = false;
  96. }
  97. /**
  98. * Tidy cache
  99. */
  100. function tidy()
  101. {
  102. global $phpEx;
  103. $dir = @opendir($this->cache_dir);
  104. if (!$dir)
  105. {
  106. return;
  107. }
  108. while (($entry = readdir($dir)) !== false)
  109. {
  110. if (!preg_match('/^(sql_|data_(?!global))/', $entry))
  111. {
  112. continue;
  113. }
  114. $expired = true;
  115. @include($this->cache_dir . $entry);
  116. if ($expired)
  117. {
  118. $this->remove_file($this->cache_dir . $entry);
  119. }
  120. }
  121. closedir($dir);
  122. if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
  123. {
  124. if (!sizeof($this->vars))
  125. {
  126. $this->load();
  127. }
  128. foreach ($this->var_expires as $var_name => $expires)
  129. {
  130. if (time() > $expires)
  131. {
  132. $this->destroy($var_name);
  133. }
  134. }
  135. }
  136. set_config('cache_last_gc', time(), true);
  137. }
  138. /**
  139. * Get saved cache object
  140. */
  141. function get($var_name)
  142. {
  143. if ($var_name[0] == '_')
  144. {
  145. global $phpEx;
  146. if (!$this->_exists($var_name))
  147. {
  148. return false;
  149. }
  150. @include($this->cache_dir . "data{$var_name}.$phpEx");
  151. return (isset($data)) ? $data : false;
  152. }
  153. else
  154. {
  155. return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
  156. }
  157. }
  158. /**
  159. * Put data into cache
  160. */
  161. function put($var_name, $var, $ttl = 31536000)
  162. {
  163. if ($var_name[0] == '_')
  164. {
  165. global $phpEx;
  166. if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb'))
  167. {
  168. @flock($fp, LOCK_EX);
  169. fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = " . var_export($var, true) . ";\n?>");
  170. @flock($fp, LOCK_UN);
  171. fclose($fp);
  172. @chmod($this->cache_dir . "data{$var_name}.$phpEx", 0666);
  173. }
  174. }
  175. else
  176. {
  177. $this->vars[$var_name] = $var;
  178. $this->var_expires[$var_name] = time() + $ttl;
  179. $this->is_modified = true;
  180. }
  181. }
  182. /**
  183. * Purge cache data
  184. */
  185. function purge()
  186. {
  187. // Purge all phpbb cache files
  188. $dir = @opendir($this->cache_dir);
  189. if (!$dir)
  190. {
  191. return;
  192. }
  193. while (($entry = readdir($dir)) !== false)
  194. {
  195. if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
  196. {
  197. continue;
  198. }
  199. $this->remove_file($this->cache_dir . $entry);
  200. }
  201. closedir($dir);
  202. unset($this->vars);
  203. unset($this->var_expires);
  204. unset($this->sql_rowset);
  205. unset($this->sql_row_pointer);
  206. $this->vars = array();
  207. $this->var_expires = array();
  208. $this->sql_rowset = array();
  209. $this->sql_row_pointer = array();
  210. $this->is_modified = false;
  211. }
  212. /**
  213. * Destroy cache data
  214. */
  215. function destroy($var_name, $table = '')
  216. {
  217. global $phpEx;
  218. if ($var_name == 'sql' && !empty($table))
  219. {
  220. if (!is_array($table))
  221. {
  222. $table = array($table);
  223. }
  224. $dir = @opendir($this->cache_dir);
  225. if (!$dir)
  226. {
  227. return;
  228. }
  229. while (($entry = readdir($dir)) !== false)
  230. {
  231. if (strpos($entry, 'sql_') !== 0)
  232. {
  233. continue;
  234. }
  235. // The following method is more failproof than simply assuming the query is on line 3 (which it should be)
  236. $check_line = @file_get_contents($this->cache_dir . $entry);
  237. if (empty($check_line))
  238. {
  239. continue;
  240. }
  241. // Now get the contents between /* and */
  242. $check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
  243. $found = false;
  244. foreach ($table as $check_table)
  245. {
  246. // Better catch partial table names than no table names. ;)
  247. if (strpos($check_line, $check_table) !== false)
  248. {
  249. $found = true;
  250. break;
  251. }
  252. }
  253. if ($found)
  254. {
  255. $this->remove_file($this->cache_dir . $entry);
  256. }
  257. }
  258. closedir($dir);
  259. return;
  260. }
  261. if (!$this->_exists($var_name))
  262. {
  263. return;
  264. }
  265. if ($var_name[0] == '_')
  266. {
  267. $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true);
  268. }
  269. else if (isset($this->vars[$var_name]))
  270. {
  271. $this->is_modified = true;
  272. unset($this->vars[$var_name]);
  273. unset($this->var_expires[$var_name]);
  274. // We save here to let the following cache hits succeed
  275. $this->save();
  276. }
  277. }
  278. /**
  279. * Check if a given cache entry exist
  280. */
  281. function _exists($var_name)
  282. {
  283. if ($var_name[0] == '_')
  284. {
  285. global $phpEx;
  286. return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
  287. }
  288. else
  289. {
  290. if (!sizeof($this->vars))
  291. {
  292. $this->load();
  293. }
  294. if (!isset($this->var_expires[$var_name]))
  295. {
  296. return false;
  297. }
  298. return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
  299. }
  300. }
  301. /**
  302. * Load cached sql query
  303. */
  304. function sql_load($query)
  305. {
  306. global $phpEx;
  307. // Remove extra spaces and tabs
  308. $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
  309. $query_id = sizeof($this->sql_rowset);
  310. if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"))
  311. {
  312. return false;
  313. }
  314. @include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
  315. if (!isset($expired))
  316. {
  317. return false;
  318. }
  319. else if ($expired)
  320. {
  321. $this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx", true);
  322. return false;
  323. }
  324. $this->sql_row_pointer[$query_id] = 0;
  325. return $query_id;
  326. }
  327. /**
  328. * Save sql query
  329. */
  330. function sql_save($query, &$query_result, $ttl)
  331. {
  332. global $db, $phpEx;
  333. // Remove extra spaces and tabs
  334. $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
  335. $filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx;
  336. if ($fp = @fopen($filename, 'wb'))
  337. {
  338. @flock($fp, LOCK_EX);
  339. $query_id = sizeof($this->sql_rowset);
  340. $this->sql_rowset[$query_id] = array();
  341. $this->sql_row_pointer[$query_id] = 0;
  342. while ($row = $db->sql_fetchrow($query_result))
  343. {
  344. $this->sql_rowset[$query_id][] = $row;
  345. }
  346. $db->sql_freeresult($query_result);
  347. $file = "<?php\n\n/* " . str_replace('*/', '*\/', $query) . " */\n";
  348. $file .= "\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n";
  349. fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . var_export($this->sql_rowset[$query_id], true) . ";\n?>");
  350. @flock($fp, LOCK_UN);
  351. fclose($fp);
  352. @chmod($filename, 0666);
  353. $query_result = $query_id;
  354. }
  355. }
  356. /**
  357. * Ceck if a given sql query exist in cache
  358. */
  359. function sql_exists($query_id)
  360. {
  361. return isset($this->sql_rowset[$query_id]);
  362. }
  363. /**
  364. * Fetch row from cache (database)
  365. */
  366. function sql_fetchrow($query_id)
  367. {
  368. if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
  369. {
  370. return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
  371. }
  372. return false;
  373. }
  374. /**
  375. * Fetch a field from the current row of a cached database result (database)
  376. */
  377. function sql_fetchfield($query_id, $field)
  378. {
  379. if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
  380. {
  381. return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false;
  382. }
  383. return false;
  384. }
  385. /**
  386. * Seek a specific row in an a cached database result (database)
  387. */
  388. function sql_rowseek($rownum, $query_id)
  389. {
  390. if ($rownum >= sizeof($this->sql_rowset[$query_id]))
  391. {
  392. return false;
  393. }
  394. $this->sql_row_pointer[$query_id] = $rownum;
  395. return true;
  396. }
  397. /**
  398. * Free memory used for a cached database result (database)
  399. */
  400. function sql_freeresult($query_id)
  401. {
  402. if (!isset($this->sql_rowset[$query_id]))
  403. {
  404. return false;
  405. }
  406. unset($this->sql_rowset[$query_id]);
  407. unset($this->sql_row_pointer[$query_id]);
  408. return true;
  409. }
  410. /**
  411. * Removes/unlinks file
  412. */
  413. function remove_file($filename, $check = false)
  414. {
  415. if ($check && !@is_writeable($this->cache_dir))
  416. {
  417. // E_USER_ERROR - not using language entry - intended.
  418. trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
  419. }
  420. return @unlink($filename);
  421. }
  422. }
  423. ?>