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

/modules/shop/includes/functions/cache.php

https://github.com/severnaya99/Sg-2010
PHP | 158 lines | 64 code | 20 blank | 74 comment | 15 complexity | 3c616183c722e9ad24dbbe63a4a49472 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // |zen-cart Open Source E-commerce |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2003 The zen-cart developers |
  7. // | |
  8. // | http://www.zen-cart.com/index.php |
  9. // | |
  10. // | Portions Copyright (c) 2003 osCommerce |
  11. // +----------------------------------------------------------------------+
  12. // | This source file is subject to version 2.0 of the GPL license, |
  13. // | that is bundled with this package in the file LICENSE, and is |
  14. // | available through the world-wide-web at the following url: |
  15. // | http://www.zen-cart.com/license/2_0.txt. |
  16. // | If you did not receive a copy of the zen-cart license and are unable |
  17. // | to obtain it through the world-wide-web, please send a note to |
  18. // | license@zen-cart.com so we can mail you a copy immediately. |
  19. // +----------------------------------------------------------------------+
  20. // $Id: cache.php 290 2004-09-15 19:48:26Z wilt $
  21. //
  22. /**
  23. * @package ZenCart_Functions
  24. */
  25. ////
  26. //! Write out serialized data.
  27. // write_cache uses serialize() to store $var in $filename.
  28. // $var - The variable to be written out.
  29. // $filename - The name of the file to write to.
  30. function write_cache(&$var, $filename) {
  31. $filename = DIR_FS_CACHE . $filename;
  32. $success = false;
  33. // try to open the file
  34. if ($fp = @fopen($filename, 'w')) {
  35. // obtain a file lock to stop corruptions occuring
  36. flock($fp, 2); // LOCK_EX
  37. // write serialized data
  38. fputs($fp, serialize($var));
  39. // release the file lock
  40. flock($fp, 3); // LOCK_UN
  41. fclose($fp);
  42. $success = true;
  43. }
  44. return $success;
  45. }
  46. ////
  47. //! Read in seralized data.
  48. // read_cache reads the serialized data in $filename and
  49. // fills $var using unserialize().
  50. // $var - The variable to be filled.
  51. // $filename - The name of the file to read.
  52. function read_cache(&$var, $filename, $auto_expire = false){
  53. $filename = DIR_FS_CACHE . $filename;
  54. $success = false;
  55. if (($auto_expire == true) && file_exists($filename)) {
  56. $now = time();
  57. $filetime = filemtime($filename);
  58. $difference = $now - $filetime;
  59. if ($difference >= $auto_expire) {
  60. return false;
  61. }
  62. }
  63. // try to open file
  64. if ($fp = @fopen($filename, 'r')) {
  65. // read in serialized data
  66. $szdata = fread($fp, filesize($filename));
  67. fclose($fp);
  68. // unserialze the data
  69. $var = unserialize($szdata);
  70. $success = true;
  71. }
  72. return $success;
  73. }
  74. ////
  75. //! Get data from the cache or the database.
  76. // get_db_cache checks the cache for cached SQL data in $filename
  77. // or retreives it from the database is the cache is not present.
  78. // $SQL - The SQL query to exectue if needed.
  79. // $filename - The name of the cache file.
  80. // $var - The variable to be filled.
  81. // $refresh - Optional. If true, do not read from the cache.
  82. // function get_db_cache($sql, &$var, $filename, $refresh = false){
  83. // $var = array();
  84. // check for the refresh flag and try to the data
  85. // if (($refresh == true)|| !read_cache($var, $filename)) {
  86. // Didn' get cache so go to the database.
  87. // $conn = mysql_connect("localhost", "apachecon", "apachecon");
  88. // $res = $db->Execute($sql);
  89. // if ($err = mysql_error()) trigger_error($err, E_USER_ERROR);
  90. // loop through the results and add them to an array
  91. // while ($rec = zen_db_fetch_array($res)) {
  92. // $var[] = $rec;
  93. // }
  94. // write the data to the file
  95. // write_cache($var, $filename);
  96. // }
  97. // }
  98. ////
  99. //! Cache the categories box
  100. // Cache the categories box
  101. function zen_cache_categories_box($auto_expire = false, $refresh = false) {
  102. global $cPath, $foo, $id, $categories_string;
  103. if (($refresh == true) || !read_cache($cache_output, 'categories_box-' . $_SESSION['language'] . '.cache' . $cPath, $auto_expire)) {
  104. ob_start();
  105. include(DIR_WS_BOXES . 'categories.php');
  106. $cache_output = ob_get_contents();
  107. ob_end_clean();
  108. write_cache($cache_output, 'categories_box-' . $_SESSION['language'] . '.cache' . $cPath);
  109. }
  110. return $cache_output;
  111. }
  112. ////
  113. //! Cache the manufacturers box
  114. // Cache the manufacturers box
  115. function zen_cache_manufacturers_box($auto_expire = false, $refresh = false) {
  116. if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-' . $_SESSION['language'] . '.cache' . $_GET['manufacturers_id'], $auto_expire)) {
  117. ob_start();
  118. include(DIR_WS_BOXES . 'manufacturers.php');
  119. $cache_output = ob_get_contents();
  120. ob_end_clean();
  121. write_cache($cache_output, 'manufacturers_box-' . $_SESSION['language'] . '.cache' . $_GET['manufacturers_id']);
  122. }
  123. return $cache_output;
  124. }
  125. ////
  126. //! Cache the also purchased module
  127. // Cache the also purchased module
  128. function zen_cache_also_purchased($auto_expire = false, $refresh = false) {
  129. if (($refresh == true) || !read_cache($cache_output, 'also_purchased-' . $_SESSION['language'] . '.cache' . $_GET['products_id'], $auto_expire)) {
  130. ob_start();
  131. include(DIR_WS_MODULES . zen_get_module_directory(FILENAME_ALSO_PURCHASED_PRODUCTS));
  132. $cache_output = ob_get_contents();
  133. ob_end_clean();
  134. write_cache($cache_output, 'also_purchased-' . $_SESSION['language'] . '.cache' . $_GET['products_id']);
  135. }
  136. return $cache_output;
  137. }
  138. ?>