/dbtech/livewall/includes/class_cache.php

https://gitlab.com/elasa/vb-elasa.ir · PHP · 254 lines · 115 code · 35 blank · 104 comment · 15 complexity · 74c63bc008599670f1f1882bd34a2e1c MD5 · raw file

  1. <?php
  2. /*======================================================================*\
  3. || #################################################################### ||
  4. || # ---------------------------------------------------------------- # ||
  5. || # Copyright ©2007-2009 Fillip Hannisdal AKA Revan/NeoRevan/Belazor # ||
  6. || # All Rights Reserved. # ||
  7. || # This file may not be redistributed in whole or significant part. # ||
  8. || # ---------------------------------------------------------------- # ||
  9. || # You are not allowed to use this on your server unless the files # ||
  10. || # you downloaded were done so with permission. # ||
  11. || # ---------------------------------------------------------------- # ||
  12. || #################################################################### ||
  13. \*======================================================================*/
  14. // #############################################################################
  15. // cache functionality class
  16. /**
  17. * Class that handles keeping the database cache up to date.
  18. *
  19. * @package Framework
  20. * @version $ $Rev$ $
  21. * @date $ $Date$ $
  22. */
  23. class LIVEWALL_CACHE
  24. {
  25. /**
  26. * The vBulletin registry object
  27. *
  28. * @private vB_Registry
  29. */
  30. private static $vbulletin = NULL;
  31. /**
  32. * The prefix for the mod we are working with
  33. *
  34. * @public string
  35. */
  36. public static $prefix = 'dbtech_livewall_';
  37. /**
  38. * Array of cache fields
  39. *
  40. * @public array
  41. */
  42. public static $cachefields = array();
  43. /**
  44. * Array of items to fetch
  45. *
  46. * @protected array
  47. */
  48. protected static $queryfields = array();
  49. /**
  50. * Array of items to NOT fetch
  51. *
  52. * @protected array
  53. */
  54. protected static $exclude = array();
  55. /**
  56. * Initialises the database caching by setting the cache
  57. * list and begins verification of the data.
  58. *
  59. * @param vB_Registry Registry object
  60. * @param string Prefix
  61. * @param array (Optional) List of all cached arrays
  62. * @param array (Optional) List of values to not fetch
  63. *
  64. * @return none Nothing
  65. */
  66. public function init($vbulletin, $cachefields = array(), $exclude = array())
  67. {
  68. // Check if the vBulletin Registry is an object
  69. if (is_object($vbulletin))
  70. {
  71. // Yep, all good
  72. self::$vbulletin =& $vbulletin;
  73. }
  74. else
  75. {
  76. // Something went wrong here I think
  77. trigger_error(__CLASS__ . "::Registry object is not an object", E_USER_ERROR);
  78. }
  79. // Set exclude
  80. self::$exclude = $exclude;
  81. if (count($cachefields) > 0)
  82. {
  83. foreach ($cachefields as $key => $title)
  84. {
  85. if (strpos($title, self::$prefix) === false)
  86. {
  87. // Get rid of the non-relevant fields
  88. unset($cachefields["$key"]);
  89. }
  90. }
  91. // Set the cleaned cachefields variable
  92. self::$cachefields = $cachefields;
  93. }
  94. if (count(self::$cachefields) == 0)
  95. {
  96. // We don't need this stuff
  97. return;
  98. // Something went wrong here I think
  99. //trigger_error("DBTech_Framework_Cache::Cachefields has no elements.", E_USER_ERROR);
  100. }
  101. // Check for valid info
  102. self::check_datastore();
  103. if (count(self::$queryfields) > 0)
  104. {
  105. // We need to re-query - prepare the string
  106. $itemlist = "'" . implode("','", self::$queryfields) . "'";
  107. if ($itemlist != "''")
  108. {
  109. // Do fetch from the database
  110. self::$vbulletin->datastore->do_db_fetch($itemlist);
  111. }
  112. }
  113. // Set the cache fields
  114. self::set_cache();
  115. }
  116. /**
  117. * Builds the cache in case the datastore has been cleaned out.
  118. *
  119. * @param string Database table we are working with
  120. * @param string (Optional) Any additional clauses to the query
  121. */
  122. public static function build_cache($type, $clauses = '')
  123. {
  124. // Premove the prefix
  125. $dbtype = self::$prefix . $type;
  126. // Initialise the some arrays so we can add to them quicker
  127. $data = array();
  128. // Prepare the variable for the identifier
  129. $firstrow = $type . 'id';
  130. self::$vbulletin->db->hide_errors();
  131. $default_query = self::$vbulletin->db->query_read("SELECT $dbtype.* FROM `" . TABLE_PREFIX . "$dbtype` AS $dbtype $clauses");
  132. while ($default = self::$vbulletin->db->fetch_array($default_query))
  133. {
  134. $data["$default[$firstrow]"]["$firstrow"] = $default["$firstrow"];
  135. foreach ($default as $key => $value)
  136. {
  137. // Loop through the query result and build the array
  138. $data["$default[$firstrow]"]["$key"] = addslashes($value);
  139. }
  140. }
  141. self::$vbulletin->db->free_result($default_query);
  142. self::$vbulletin->db->show_errors();
  143. // Finally update the datastore with the new value
  144. build_datastore($dbtype, serialize($data), 1);
  145. // Premove the prefix
  146. $field_short = substr($dbtype, strlen(self::$prefix));
  147. // Strip the slashes
  148. self::$vbulletin->input->stripslashes_deep($data);
  149. // Set the data
  150. LIVEWALL::$cache["$field_short"] = $data;
  151. foreach ((array)LIVEWALL::$cache["$field_short"] as $id => $arr)
  152. {
  153. foreach ((array)LIVEWALL::$unserialize["$field_short"] as $key)
  154. {
  155. // Do unserialize
  156. LIVEWALL::$cache["$field_short"]["$id"]["$key"] = @unserialize(stripslashes($arr["$key"]));
  157. }
  158. }
  159. }
  160. /**
  161. * Checks whether or not datastore items are present,
  162. * and schedules for re-query if needed.
  163. */
  164. private static function check_datastore()
  165. {
  166. foreach (self::$cachefields as $title)
  167. {
  168. if (strpos($title, self::$prefix) === false)
  169. {
  170. // We don't care.
  171. continue;
  172. }
  173. // Check if the value is set
  174. if (!isset(self::$vbulletin->$title))
  175. {
  176. if (in_array($title, self::$exclude))
  177. {
  178. // Skip this
  179. self::$vbulletin->$title = self::$exclude["$title"];
  180. }
  181. else
  182. {
  183. // It wasn't :(
  184. self::$queryfields[] = $title;
  185. // Build datastore
  186. self::build_cache(substr($title, strlen(self::$prefix)));
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * Sets the specified cache field after making sure all slashes
  193. * are stripped again
  194. */
  195. private static function set_cache()
  196. {
  197. foreach (self::$cachefields as $field)
  198. {
  199. // Premove the prefix
  200. $field_short = substr($field, strlen(self::$prefix));
  201. // Fetch the data from the vB array
  202. $data = self::$vbulletin->$field;
  203. if (is_array($data))
  204. {
  205. // Strip the slashes
  206. self::$vbulletin->input->stripslashes_deep($data);
  207. // Unset from the vbulletin array to save memory
  208. unset(self::$vbulletin->$field);
  209. }
  210. else if (!in_array($field, self::$exclude))
  211. {
  212. // Ensure this is an array
  213. $data = array();
  214. }
  215. // Set the data
  216. LIVEWALL::$cache["$field_short"] = $data;
  217. }
  218. }
  219. }