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

/includes/functions/cache.php

https://github.com/vaughnpaul/NOS
PHP | 245 lines | 190 code | 0 blank | 55 comment | 56 complexity | d9bfd8b974b393a0e5f91b0d9f321083 MD5 | raw file
  1. <?php
  2. /*
  3. $Id: cache.php,v 1.2 2008/06/23 00:18:17 datazen Exp $
  4. CRE Loaded, Open Source E-Commerce Solutions
  5. http://www.creloaded.com
  6. Copyright (c) 2008 CRE Loaded
  7. Copyright (c) 2003 osCommerce
  8. Released under the GNU General Public License
  9. */
  10. // write out serialized data.
  11. // write_cache uses serialize() to store $var in $filename.
  12. // $var - The variable to be written out.
  13. // $filename - The name of the file to write to.
  14. function write_cache(&$var, $filename) {
  15. $filename = DIR_FS_CATALOG . DIR_FS_CACHE . $filename;
  16. $success = false;
  17. // try to open the file
  18. if ($fp = @fopen($filename, 'w')) {
  19. // obtain a file lock to stop corruptions occuring
  20. flock($fp, 2); // LOCK_EX
  21. // write serialized data
  22. fputs($fp, serialize($var));
  23. // release the file lock
  24. flock($fp, 3); // LOCK_UN
  25. fclose($fp);
  26. $success = true;
  27. }
  28. return $success;
  29. }
  30. // read in seralized data.
  31. // read_cache reads the serialized data in $filename and
  32. // fills $var using unserialize().
  33. // $var - The variable to be filled.
  34. // $filename - The name of the file to read.
  35. function read_cache(&$var, $filename, $auto_expire = false) {
  36. $filename = DIR_FS_CATALOG . DIR_FS_CACHE . $filename;
  37. $success = false;
  38. if (($auto_expire == true) && file_exists($filename)) {
  39. $now = time();
  40. $filetime = filemtime($filename);
  41. $difference = $now - $filetime;
  42. if ($difference >= $auto_expire) {
  43. return false;
  44. }
  45. }
  46. // try to open file
  47. if ($fp = @fopen($filename, 'r')) {
  48. // read in serialized data
  49. $szdata = fread($fp, filesize($filename));
  50. fclose($fp);
  51. // unserialze the data
  52. $var = unserialize($szdata);
  53. $success = true;
  54. }
  55. return $success;
  56. }
  57. // get data from the cache or the database.
  58. // get_db_cache checks the cache for cached SQL data in $filename
  59. // or retreives it from the database is the cache is not present.
  60. // $SQL - The SQL query to exectue if needed.
  61. // $filename - The name of the cache file.
  62. // $var - The variable to be filled.
  63. // $refresh - Optional. If true, do not read from the cache.
  64. function get_db_cache($sql, &$var, $filename, $refresh = false) {
  65. $var = array();
  66. // check for the refresh flag and try to the data
  67. if (($refresh == true)|| !read_cache($var, $filename)) {
  68. // Didn' get cache so go to the database.
  69. $res = tep_db_query($sql);
  70. // loop through the results and add them to an array
  71. while ($rec = tep_db_fetch_array($res)) {
  72. $var[] = $rec;
  73. }
  74. // write the data to the file
  75. write_cache($var, $filename);
  76. }
  77. }
  78. // cache the categories box
  79. function tep_cache_categories_box($auto_expire = false, $refresh = false) {
  80. global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string, $foo, $id, $aa;
  81. if (($refresh == true) || !read_cache($cache_output, 'categories_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath, $auto_expire)) {
  82. //get default template name
  83. $customer_template_query = tep_db_query("SELECT customers_selected_template as template_selected
  84. from " . TABLE_CUSTOMERS . "
  85. WHERE customers_id = '" . $_SESSION['customer_id'] . "'");
  86. $cptemplate1 = tep_db_fetch_array($customer_template_query);
  87. if (tep_not_null($cptemplate1['template_selected'])) {
  88. define(TEMPLATE_NAME, $cptemplate['template_selected']);
  89. } else if (tep_not_null(DEFAULT_TEMPLATE)){
  90. define(TEMPLATE_NAME, DEFAULT_TEMPLATE);
  91. } else {
  92. define(TEMPLATE_NAME, 'default');
  93. }
  94. // set all possible catagories X.php names
  95. ob_start();
  96. include(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/boxes/categories.php');
  97. $cache_output = ob_get_contents();
  98. ob_end_clean();
  99. write_cache($cache_output, 'categories_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath);
  100. }
  101. return $cache_output;
  102. }
  103. // cache the categories1 box
  104. function tep_cache_categories_box1($auto_expire = false, $refresh = false) {
  105. global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string1, $foo, $id, $aa;
  106. if (($refresh == true) || !read_cache($cache_output, 'categories1_box-' . TEMPLATE_NAME . '.' . $language . '.cache' . $cPath, $auto_expire)) {
  107. //get default template name
  108. $customer_template_query = tep_db_query("SELECT customers_selected_template as template_selected
  109. from " . TABLE_CUSTOMERS . "
  110. WHERE customers_id = '" . $_SESSION['customer_id'] . "'");
  111. $cptemplate1 = tep_db_fetch_array($customer_template_query);
  112. if (tep_not_null($cptemplate1['template_selected'])) {
  113. define(TEMPLATE_NAME, $cptemplate['template_selected']);
  114. } else if (tep_not_null(DEFAULT_TEMPLATE)){
  115. define(TEMPLATE_NAME, DEFAULT_TEMPLATE);
  116. } else {
  117. define(TEMPLATE_NAME, 'default');
  118. }
  119. //set all possible catagories X.php names
  120. ob_start();
  121. include(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/boxes/categories1.php');
  122. $cache_output = ob_get_contents();
  123. ob_end_clean();
  124. write_cache($cache_output, 'categories1_box-' . TEMPLATE_NAME . '.' . $language . '.cache' . $cPath);
  125. }
  126. return $cache_output;
  127. }
  128. // cache the categories2 box
  129. function tep_cache_categories_box2($auto_expire = false, $refresh = false) {
  130. global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string2, $foo, $id, $aa;
  131. if (($refresh == true) || !read_cache($cache_output, 'categories2_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath, $auto_expire)) {
  132. //get default template name
  133. $customer_template_query = tep_db_query("SELECT customers_selected_template as template_selected
  134. from " . TABLE_CUSTOMERS . "
  135. WHERE customers_id = '" . $_SESSION['customer_id'] . "'");
  136. $cptemplate1 = tep_db_fetch_array($customer_template_query);
  137. if (tep_not_null($cptemplate1['template_selected'])) {
  138. define(TEMPLATE_NAME, $cptemplate['template_selected']);
  139. } else if (tep_not_null(DEFAULT_TEMPLATE)){
  140. define(TEMPLATE_NAME, DEFAULT_TEMPLATE);
  141. } else {
  142. define(TEMPLATE_NAME, 'default');
  143. }
  144. //set all possible catagories X.php names
  145. ob_start();
  146. include(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/boxes/categories2.php');
  147. $cache_output = ob_get_contents();
  148. ob_end_clean();
  149. write_cache($cache_output, 'categories2_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath);
  150. }
  151. return $cache_output;
  152. }
  153. // cache the categories3 box
  154. function tep_cache_categories_box3($auto_expire = false, $refresh = false) {
  155. global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string3, $foo, $id, $aa;
  156. if (($refresh == true) || !read_cache($cache_output, 'categories3_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath, $auto_expire)) {
  157. // get default template name
  158. $customer_template_query = tep_db_query("SELECT customers_selected_template as template_selected
  159. from " . TABLE_CUSTOMERS . "
  160. WHERE customers_id = '" . $_SESSION['customer_id'] . "'");
  161. $cptemplate1 = tep_db_fetch_array($customer_template_query);
  162. if (tep_not_null($cptemplate1['template_selected'])) {
  163. define(TEMPLATE_NAME, $cptemplate['template_selected']);
  164. } else if (tep_not_null(DEFAULT_TEMPLATE)){
  165. define(TEMPLATE_NAME, DEFAULT_TEMPLATE);
  166. } else {
  167. define(TEMPLATE_NAME, 'default');
  168. }
  169. // set all possible catagories X.php names
  170. ob_start();
  171. include(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/boxes/categories3.php');
  172. $cache_output = ob_get_contents();
  173. ob_end_clean();
  174. write_cache($cache_output, 'categories3_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath);
  175. }
  176. return $cache_output;
  177. }
  178. // cache the categories4 box
  179. function tep_cache_categories_box4($auto_expire = false, $refresh = false) {
  180. global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string4, $foo, $id, $aa;
  181. if (($refresh == true) || !read_cache($cache_output, 'categories4_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath, $auto_expire)) {
  182. //get default template name
  183. $customer_template_query = tep_db_query("SELECT customers_selected_template as template_selected
  184. from " . TABLE_CUSTOMERS . "
  185. WHERE customers_id = '" . $_SESSION['customer_id'] . "'");
  186. $cptemplate1 = tep_db_fetch_array($customer_template_query);
  187. if (tep_not_null($cptemplate1['template_selected'])) {
  188. define(TEMPLATE_NAME, $cptemplate['template_selected']);
  189. } else if (tep_not_null(DEFAULT_TEMPLATE)){
  190. define(TEMPLATE_NAME, DEFAULT_TEMPLATE);
  191. } else {
  192. define(TEMPLATE_NAME, 'default');
  193. }
  194. //set all possible catagories X.php names
  195. ob_start();
  196. include(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/boxes/categories4.php');
  197. $cache_output = ob_get_contents();
  198. ob_end_clean();
  199. write_cache($cache_output, 'categories4_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $cPath);
  200. }
  201. return $cache_output;
  202. }
  203. // cache the manufacturers box
  204. function tep_cache_manufacturers_box($auto_expire = false, $refresh = false) {
  205. global $language;
  206. $customer_template_query = tep_db_query("SELECT customers_selected_template as template_selected
  207. from " . TABLE_CUSTOMERS . "
  208. WHERE customers_id = '" . $_SESSION['customer_id'] . "'");
  209. $cptemplate1 = tep_db_fetch_array($customer_template_query);
  210. if (tep_not_null($cptemplate1['template_selected'])) {
  211. define(TEMPLATE_NAME, $cptemplate['template_selected']);
  212. } else if (tep_not_null(DEFAULT_TEMPLATE)){
  213. define(TEMPLATE_NAME, DEFAULT_TEMPLATE);
  214. } else {
  215. define(TEMPLATE_NAME, 'default');
  216. }
  217. $manufacturers_id = '';
  218. if (isset($_GET['manufactuers_id']) && tep_not_null($_GET['manufacturers_id'])) {
  219. $manufacturers_id = $_GET['manufacturers_id'];
  220. }
  221. if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $manufacturers_id, $auto_expire)) {
  222. ob_start();
  223. include(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/boxes/manufacturers.php');
  224. $cache_output = ob_get_contents();
  225. ob_end_clean();
  226. write_cache($cache_output, 'manufacturers_box-'. TEMPLATE_NAME . '.' . $language . '.cache' . $manufacturers_id);
  227. }
  228. return $cache_output;
  229. }
  230. // cache the also purchased module
  231. function tep_cache_also_purchased($auto_expire = false, $refresh = false) {
  232. global $language, $languages_id;
  233. if (($refresh == true) || !read_cache($cache_output, 'also_purchased-'. TEMPLATE_NAME . '.' . $language . '.cache' . $_GET['products_id'], $auto_expire)) {
  234. ob_start();
  235. include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS);
  236. $cache_output = ob_get_contents();
  237. ob_end_clean();
  238. write_cache($cache_output, 'also_purchased-'. TEMPLATE_NAME . '.' . $language . '.cache' . $_GET['products_id']);
  239. }
  240. return $cache_output;
  241. }
  242. ?>