PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/legacy/includes/objects/cache.php

http://novaboard.googlecode.com/
PHP | 260 lines | 157 code | 43 blank | 60 comment | 20 complexity | 564b0d87458b9855b66a3f8252836144 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | NovaBoard
  5. | ========================================
  6. | By The NovaBoard team
  7. | Released under the Artistic License 2.0
  8. | http://www.novaboard.net
  9. | ========================================
  10. | cache.php - Cache class to handle loading/saving of repeat data.
  11. */
  12. if (!defined('NOVA_RUN'))
  13. {
  14. exit('<h1>ACCESS DENIED</h1>You cannot access this file directly.');
  15. }
  16. class cache
  17. {
  18. private
  19. $ROOT = '',
  20. $db_prefix = '',
  21. $cache = array()
  22. ;
  23. /*
  24. Constructor function
  25. @param string $root : Path to novaboard's root folder.
  26. @return null
  27. */
  28. public function __construct($root, $db_prefix)
  29. {
  30. $this->ROOT = $root;
  31. $this->db_prefix = $db_prefix;
  32. }
  33. /*
  34. Deletes the specified cache file.
  35. @param string $file : The name of the file to delete.
  36. @return bool : True/false depending on successful deletion.
  37. */
  38. public function delete($file)
  39. {
  40. if (file_exists($this->ROOT . 'cache/' . $file . '.php'))
  41. {
  42. unlink($this->ROOT . 'cache/' . $file . '.php');
  43. if (isset($this->cache[$file]))
  44. {
  45. unset($this->cache[$file]);
  46. }
  47. return true;
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. }
  54. /*
  55. Loads the given cache file.
  56. @param string $file : The file to load.
  57. @param bool $remember : Whether to save the info to an array (for when a file is loaded more than once).
  58. @return array : The contents of the cache file.
  59. */
  60. public function load($file, $remember = false)
  61. {
  62. if (in_array($file, $this->cache))
  63. {
  64. return $this->cache[$file];
  65. }
  66. else
  67. {
  68. if (!file_exists($this->ROOT . 'cache/' . $file . '.php'))
  69. {
  70. return ($remember) ? $this->cache[$file] = $this->reCache($file) : $this->reCache($file);
  71. }
  72. else
  73. {
  74. include $this->ROOT . 'cache/' . $file . '.php';
  75. if (empty($cache))
  76. {
  77. return ($remember) ? $this->cache[$file] = $this->reCache($file) : $this->reCache($file);
  78. }
  79. return ($remember) ? $this->cache[$file] = $cache : $cache;
  80. }
  81. }
  82. }
  83. /*
  84. Saves the given array to a cache file.
  85. @param string $file : The name to give the file.
  86. @param array $info : The contents to add to the file.
  87. @return bool : True if file was created, false otherwise.
  88. */
  89. public function save($file, $info = '')
  90. {
  91. $file = $this->ROOT . 'cache/' . $file . '.php';
  92. if (is_writeable($this->ROOT . 'cache/'))
  93. {
  94. $content = '<?php' . "\n";
  95. if ($info != '')
  96. {
  97. $content .= '$cache = ' . var_export($info, true) . ';';
  98. }
  99. else
  100. {
  101. $content .= '$cache = array();';
  102. }
  103. $content .= "\n" . '?>';
  104. return (file_put_contents($file, $content)) ? true : false;
  105. }
  106. else
  107. {
  108. return false;
  109. }
  110. }
  111. /*
  112. Re-Caches the specified file.
  113. @param string $file : The name of the file.
  114. @return array : Information retrieved from the DB.
  115. */
  116. private function reCache($file)
  117. {
  118. /*
  119. Recache for board settings
  120. */
  121. if ($file == 'settings')
  122. {
  123. $query = mysql_query('SELECT * FROM ' . $this->db_prefix . 'settings');
  124. $row = mysql_fetch_assoc($query);
  125. $this->save('settings', $row);
  126. return $row;
  127. }
  128. /*
  129. Word censors in posts.
  130. */
  131. elseif ($file == 'censor')
  132. {
  133. $censor = array();
  134. $query = mysql_query('SELECT * FROM ' . $this->db_prefix . 'censor');
  135. while ($row = mysql_fetch_assoc($query))
  136. {
  137. $censor[$row['row']] = $row;
  138. }
  139. $this->save('censor', $censor);
  140. return $censor;
  141. }
  142. /*
  143. User groups
  144. */
  145. elseif ($file == 'groups')
  146. {
  147. $groups = array();
  148. $query = mysql_query('SELECT * FROM ' . $this->db_prefix . 'groups');
  149. while ($row = mysql_fetch_assoc($query))
  150. {
  151. $groups[$row['group_id']] = $row;
  152. }
  153. $this->save($file, $groups);
  154. return $row;
  155. }
  156. /*
  157. Moderator permissions
  158. */
  159. elseif ($file == 'moderators')
  160. {
  161. $moderators = array();
  162. $query = mysql_query('SELECT * FROM ' . $this->db_prefix . 'moderators');
  163. while ($row = mysql_fetch_assoc($query))
  164. {
  165. $moderators[$row['member_id']][$row['forum_id']] = $row;
  166. }
  167. $this->save('moderators', $moderators);
  168. return $moderators;
  169. }
  170. /*
  171. Hooks
  172. */
  173. elseif ($file == 'hooks')
  174. {
  175. $hooks = array();
  176. $query = mysql_query('
  177. SELECT h.file, h.location, m.module_name
  178. FROM ' . $this->db_prefix . 'modules_hooks h
  179. INNER JOIN ' . $this->db_prefix . 'modules m
  180. ON h.module_id = m.id
  181. WHERE m.installed = 1
  182. ');
  183. while ($row = mysql_fetch_assoc($query))
  184. {
  185. $hooks[$row['file']][$row['location']][] = $row;
  186. }
  187. $this->save('hooks', $hooks);
  188. return $hooks;
  189. }
  190. /*
  191. Emoticons/smilies (whatever you wanna call them).
  192. */
  193. elseif (strpos($file, 'emoticons_') !== false)
  194. {
  195. $theme = str_replace('emoticons_', '', $file);
  196. $smilies = array();
  197. $query219 = mysql_query('
  198. SELECT row, code, link
  199. FROM ' . $this->db_prefix . 'smilies
  200. WHERE emoticon_on = 1 AND code != "" AND link != "" AND theme = "' . $theme . '"
  201. ORDER BY row DESC
  202. ');
  203. while ($row = mysql_fetch_assoc($query219))
  204. {
  205. $smilies[$row['row']] = $row;
  206. }
  207. $this->save($file, $smilies);
  208. return $smilies;
  209. }
  210. }
  211. }
  212. ?>