PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Smarty/internals/core.write_cache_file.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 121 lines | 53 code | 16 blank | 52 comment | 14 complexity | 3347fa035bd907e7ea032ef4b745c0da MD5 | raw file
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  5. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  6. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  7. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  8. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  9. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  10. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  11. r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
  12. */
  13. /**
  14. * Smarty plugin
  15. * @package Smarty
  16. * @subpackage plugins
  17. */
  18. /**
  19. * Prepend the cache information to the cache file
  20. * and write it
  21. *
  22. * @param string $tpl_file
  23. * @param string $cache_id
  24. * @param string $compile_id
  25. * @param string $results
  26. * @return true|null
  27. */
  28. // $tpl_file, $cache_id, $compile_id, $results
  29. function smarty_core_write_cache_file($params, &$smarty)
  30. {
  31. // put timestamp in cache header
  32. $smarty->_cache_info['timestamp'] = time();
  33. if ($smarty->cache_lifetime > -1){
  34. // expiration set
  35. $smarty->_cache_info['expires'] = $smarty->_cache_info['timestamp'] + $smarty->cache_lifetime;
  36. } else {
  37. // cache will never expire
  38. $smarty->_cache_info['expires'] = -1;
  39. }
  40. // collapse nocache.../nocache-tags
  41. if (preg_match_all('!\{(/?)nocache\:[0-9a-f]{32}#\d+\}!', $params['results'], $match, PREG_PATTERN_ORDER)) {
  42. // remove everything between every pair of outermost noache.../nocache-tags
  43. // and replace it by a single nocache-tag
  44. // this new nocache-tag will be replaced by dynamic contents in
  45. // smarty_core_process_compiled_includes() on a cache-read
  46. $match_count = count($match[0]);
  47. $results = preg_split('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!', $params['results'], -1, PREG_SPLIT_DELIM_CAPTURE);
  48. $level = 0;
  49. $j = 0;
  50. for ($i=0, $results_count = count($results); $i < $results_count && $j < $match_count; $i++) {
  51. if ($results[$i] == $match[0][$j]) {
  52. // nocache tag
  53. if ($match[1][$j]) { // closing tag
  54. $level--;
  55. unset($results[$i]);
  56. } else { // opening tag
  57. if ($level++ > 0) unset($results[$i]);
  58. }
  59. $j++;
  60. } elseif ($level > 0) {
  61. unset($results[$i]);
  62. }
  63. }
  64. $params['results'] = implode('', $results);
  65. }
  66. $smarty->_cache_info['cache_serials'] = $smarty->_cache_serials;
  67. // prepend the cache header info into cache file
  68. $_cache_info = serialize($smarty->_cache_info);
  69. $params['results'] = strlen($_cache_info) . "\n" . $_cache_info . $params['results'];
  70. if (!empty($smarty->cache_handler_func)) {
  71. // use cache_handler function
  72. call_user_func_array($smarty->cache_handler_func,
  73. array('write', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null));
  74. } else {
  75. // use local cache file
  76. if(!@is_writable($smarty->cache_dir)) {
  77. // cache_dir not writable, see if it exists
  78. if(!@is_dir($smarty->cache_dir)) {
  79. $smarty->trigger_error('the $cache_dir \'' . $smarty->cache_dir . '\' does not exist, or is not a directory.', E_USER_ERROR);
  80. return false;
  81. }
  82. $smarty->trigger_error('unable to write to $cache_dir \'' . realpath($smarty->cache_dir) . '\'. Be sure $cache_dir is writable by the web server user.', E_USER_ERROR);
  83. return false;
  84. }
  85. $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']);
  86. $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id);
  87. $_params = array('filename' => $_cache_file, 'contents' => $params['results'], 'create_dirs' => true);
  88. require_once(SMARTY_CORE_DIR . 'core.write_file.php');
  89. smarty_core_write_file($_params, $smarty);
  90. return true;
  91. }
  92. }
  93. /* vim: set expandtab: */
  94. ?>