/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_utility.php

http://zoop.googlecode.com/ · PHP · 223 lines · 149 code · 22 blank · 52 comment · 37 complexity · a74fbd6fe75cdc3e3e2b0e9a6753d48a MD5 · raw file

  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: smarty_internal_utility.php
  5. * SVN: $Id: $
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * For questions, help, comments, discussion, etc., please join the
  22. * Smarty mailing list. Send a blank e-mail to
  23. * smarty-discussion-subscribe@googlegroups.com
  24. *
  25. * @link http://www.smarty.net/
  26. * @copyright 2008 New Digital Group, Inc.
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author Uwe Tews
  29. * @package Smarty
  30. * @subpackage PluginsInternal
  31. * @version 3-SVN$Rev: 3286 $
  32. */
  33. class Smarty_Internal_Utility {
  34. protected $smarty;
  35. function __construct($smarty) {
  36. $this->smarty = $smarty;
  37. }
  38. /**
  39. * Compile all template files
  40. *
  41. * @param string $extension file extension
  42. * @param bool $force_compile force all to recompile
  43. * @param int $time_limit
  44. * @param int $max_errors
  45. * @return integer number of template files recompiled
  46. */
  47. function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  48. {
  49. function _get_time()
  50. {
  51. $_mtime = microtime();
  52. $_mtime = explode(" ", $_mtime);
  53. return (double)($_mtime[1]) + (double)($_mtime[0]);
  54. }
  55. // set default directory
  56. if ($dir_name === null) {
  57. $dir_name = $this->smarty->template_dir;
  58. }
  59. // switch off time limit
  60. if (function_exists('set_time_limit')) {
  61. @set_time_limit($time_limit);
  62. }
  63. $this->smarty->force_compile = $force_compile;
  64. $_count = 0;
  65. $_error_count = 0;
  66. // loop over array of template directories
  67. foreach((array)$this->smarty->template_dir as $_dir) {
  68. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  69. $_compile = new RecursiveIteratorIterator($_compileDirs);
  70. foreach ($_compile as $_fileinfo) {
  71. if (strpos($_fileinfo, '.svn') !== false) continue;
  72. $_file = $_fileinfo->getFilename();
  73. if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
  74. if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
  75. $_template_file = $_file;
  76. } else {
  77. $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . '\\' . $_file;
  78. }
  79. echo '<br>', $_dir, '---', $_template_file;
  80. flush();
  81. $_start_time = _get_time();
  82. try {
  83. $_tpl = $this->smarty->createTemplate($_template_file);
  84. $_tpl->getCompiledTemplate();
  85. }
  86. catch (Exception $e) {
  87. echo 'Error: ', $e->getMessage(), "<br><br>";
  88. $_error_count++;
  89. }
  90. echo ' done in ', _get_time() - $_start_time, ' seconds';
  91. if ($max_errors !== null && $_error_count == $max_errors) {
  92. echo '<br><br>too many errors';
  93. exit();
  94. }
  95. }
  96. }
  97. return $_count;
  98. }
  99. /**
  100. * Delete compiled template file
  101. *
  102. * @param string $resource_name template name
  103. * @param string $compile_id compile id
  104. * @param integer $exp_time expiration time
  105. * @return integer number of template files deleted
  106. */
  107. function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  108. {
  109. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!','_',$compile_id) : null;
  110. $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
  111. if (isset($resource_name)) {
  112. $_resource_part_1 = $resource_name . '.php';
  113. $_resource_part_2 = $resource_name . '.cache' . '.php';
  114. } else {
  115. $_resource_part = '';
  116. }
  117. $_dir = $this->smarty->compile_dir;
  118. if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
  119. $_dir .= $_compile_id . $_dir_sep;
  120. }
  121. if (isset($_compile_id)) {
  122. $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
  123. }
  124. $_count = 0;
  125. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  126. $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
  127. foreach ($_compile as $_file) {
  128. if (strpos($_file, '.svn') !== false) continue;
  129. if ($_file->isDir()) {
  130. if (!$_compile->isDot()) {
  131. // delete folder if empty
  132. @rmdir($_file->getPathname());
  133. }
  134. } else {
  135. if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
  136. (!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) ||
  137. (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
  138. if (isset($exp_time)) {
  139. if (time() - @filemtime($_file) >= $exp_time) {
  140. $_count += @unlink((string) $_file) ? 1 : 0;
  141. }
  142. } else {
  143. $_count += @unlink((string) $_file) ? 1 : 0;
  144. }
  145. }
  146. }
  147. }
  148. return $_count;
  149. }
  150. function testInstall()
  151. {
  152. echo "<PRE>\n";
  153. echo "Smarty Installation test...\n";
  154. echo "Testing template directory...\n";
  155. foreach((array)$this->smarty->template_dir as $template_dir) {
  156. if (!is_dir($template_dir))
  157. echo "FAILED: $template_dir is not a directory.\n";
  158. elseif (!is_readable($template_dir))
  159. echo "FAILED: $template_dir is not readable.\n";
  160. else
  161. echo "$template_dir is OK.\n";
  162. }
  163. echo "Testing compile directory...\n";
  164. if (!is_dir($this->smarty->compile_dir))
  165. echo "FAILED: $this->smarty->compile_dir is not a directory.\n";
  166. elseif (!is_readable($this->smarty->compile_dir))
  167. echo "FAILED: $this->smarty->compile_dir is not readable.\n";
  168. elseif (!is_writable($this->smarty->compile_dir))
  169. echo "FAILED: $this->smarty->compile_dir is not writable.\n";
  170. else
  171. echo "{$this->smarty->compile_dir} is OK.\n";
  172. echo "Testing plugins directory...\n";
  173. foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
  174. if (!is_dir($plugin_dir))
  175. echo "FAILED: $plugin_dir is not a directory.\n";
  176. elseif (!is_readable($plugin_dir))
  177. echo "FAILED: $plugin_dir is not readable.\n";
  178. else
  179. echo "$plugin_dir is OK.\n";
  180. }
  181. echo "Testing cache directory...\n";
  182. if (!is_dir($this->smarty->cache_dir))
  183. echo "FAILED: $this->smarty->cache_dir is not a directory.\n";
  184. elseif (!is_readable($this->smarty->cache_dir))
  185. echo "FAILED: $this->smarty->cache_dir is not readable.\n";
  186. elseif (!is_writable($this->smarty->cache_dir))
  187. echo "FAILED: $this->smarty->cache_dir is not writable.\n";
  188. else
  189. echo "{$this->smarty->cache_dir} is OK.\n";
  190. echo "Testing configs directory...\n";
  191. if (!is_dir($this->smarty->config_dir))
  192. echo "FAILED: $this->smarty->config_dir is not a directory.\n";
  193. elseif (!is_readable($this->smarty->config_dir))
  194. echo "FAILED: $this->smarty->config_dir is not readable.\n";
  195. else
  196. echo "{$this->smarty->config_dir} is OK.\n";
  197. echo "Tests complete.\n";
  198. echo "</PRE>\n";
  199. return true;
  200. }
  201. }