4  * Project:     Smarty: the PHP compiling template engine
 
   5  * File:        smarty_internal_utility.php
 
   8  * This library is free software; you can redistribute it and/or
 
   9  * modify it under the terms of the GNU Lesser General Public
 
  10  * License as published by the Free Software Foundation; either
 
  11  * version 2.1 of the License, or (at your option) any later version.
 
  13  * This library is distributed in the hope that it will be useful,
 
  14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
  16  * Lesser General Public License for more details.
 
  18  * You should have received a copy of the GNU Lesser General Public
 
  19  * License along with this library; if not, write to the Free Software
 
  20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  22  * For questions, help, comments, discussion, etc., please join the
 
  23  * Smarty mailing list. Send a blank e-mail to
 
  24  * smarty-discussion-subscribe@googlegroups.com
 
  26  * @link http://www.smarty.net/
 
  27  * @copyright 2008 New Digital Group, Inc.
 
  28  * @author Monte Ohrt <monte at ohrt dot com> 
 
  31  * @subpackage PluginsInternal
 
  32  * @version 3-SVN$Rev: 3286 $
 
  35 class Smarty_Internal_Utility {
 
  38     function __construct($smarty)
 
  40         $this->smarty = $smarty;
 
  44      * Compile all template files
 
  46      * @param string $extension file extension
 
  47      * @param bool $force_compile force all to recompile
 
  48      * @param int $time_limit 
 
  49      * @param int $max_errors 
 
  50      * @return integer number of template files recompiled
 
  52     function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
 
  54         // switch off time limit
 
  55         if (function_exists('set_time_limit')) {
 
  56             @set_time_limit($time_limit);
 
  58         $this->smarty->force_compile = $force_compile;
 
  61         // loop over array of template directories
 
  62         foreach((array)$this->smarty->template_dir as $_dir) {
 
  63             if (strpos('/\\', substr($_dir, -1)) === false) {
 
  66             $_compileDirs = new RecursiveDirectoryIterator($_dir);
 
  67             $_compile = new RecursiveIteratorIterator($_compileDirs);
 
  68             foreach ($_compile as $_fileinfo) {
 
  69                 if (strpos($_fileinfo, '.svn') !== false) continue;
 
  70                 $_file = $_fileinfo->getFilename();
 
  71                 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
 
  72                 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
 
  73                    $_template_file = $_file;
 
  75                    $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
 
  77                 echo '<br>', $_dir, '---', $_template_file;
 
  79                 $_start_time = microtime(true);
 
  81                     $_tpl = $this->smarty->createTemplate($_template_file,null,null,null,false);
 
  82                     if ($_tpl->mustCompile()) {
 
  83                         $_tpl->compileTemplateSource();
 
  84                         echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
 
  87                         echo ' is up to date';
 
  91                 catch (Exception $e) {
 
  92                     echo 'Error: ', $e->getMessage(), "<br><br>";
 
  96                 $this->smarty->template_objects = array();
 
  97                 $_tpl->smarty->template_objects = array();
 
  99                 if ($max_errors !== null && $_error_count == $max_errors) {
 
 100                     echo '<br><br>too many errors';
 
 109      * Compile all config files
 
 111      * @param string $extension file extension
 
 112      * @param bool $force_compile force all to recompile
 
 113      * @param int $time_limit 
 
 114      * @param int $max_errors 
 
 115      * @return integer number of template files recompiled
 
 117     function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
 
 119         // switch off time limit
 
 120         if (function_exists('set_time_limit')) {
 
 121             @set_time_limit($time_limit);
 
 123         $this->smarty->force_compile = $force_compile;
 
 126         // loop over array of template directories
 
 127         foreach((array)$this->smarty->config_dir as $_dir) {
 
 128             if (strpos('/\\', substr($_dir, -1)) === false) {
 
 131             $_compileDirs = new RecursiveDirectoryIterator($_dir);
 
 132             $_compile = new RecursiveIteratorIterator($_compileDirs);
 
 133             foreach ($_compile as $_fileinfo) {
 
 134                 if (strpos($_fileinfo, '.svn') !== false) continue;
 
 135                 $_file = $_fileinfo->getFilename();
 
 136                 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
 
 137                 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
 
 138                     $_config_file = $_file;
 
 140                     $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
 
 142                 echo '<br>', $_dir, '---', $_config_file;
 
 144                 $_start_time = microtime(true);
 
 146                     $_config = new Smarty_Internal_Config($_config_file, $this->smarty);
 
 147                     if ($_config->mustCompile()) {
 
 148                         $_config->compileConfigSource();
 
 149                         echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
 
 152                         echo ' is up to date';
 
 156                 catch (Exception $e) {
 
 157                     echo 'Error: ', $e->getMessage(), "<br><br>";
 
 160                 if ($max_errors !== null && $_error_count == $max_errors) {
 
 161                     echo '<br><br>too many errors';
 
 170      * Delete compiled template file
 
 172      * @param string $resource_name template name
 
 173      * @param string $compile_id compile id
 
 174      * @param integer $exp_time expiration time
 
 175      * @return integer number of template files deleted
 
 177     function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
 
 179         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
 
 180         $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
 
 181         if (isset($resource_name)) {
 
 182             $_resource_part_1 = $resource_name . '.php';
 
 183             $_resource_part_2 = $resource_name . '.cache' . '.php';
 
 185             $_resource_part = '';
 
 187         $_dir = $this->smarty->compile_dir;
 
 188         if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
 
 189             $_dir .= $_compile_id . $_dir_sep;
 
 191         if (isset($_compile_id)) {
 
 192             $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
 
 195         $_compileDirs = new RecursiveDirectoryIterator($_dir);
 
 196         $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
 
 197         foreach ($_compile as $_file) {
 
 198             if (strpos($_file, '.svn') !== false) continue;
 
 199             if ($_file->isDir()) {
 
 200                 if (!$_compile->isDot()) {
 
 201                     // delete folder if empty
 
 202                     @rmdir($_file->getPathname());
 
 205                 if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
 
 206                         (!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) ||
 
 207                             (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
 
 208                     if (isset($exp_time)) {
 
 209                         if (time() - @filemtime($_file) >= $exp_time) {
 
 210                             $_count += @unlink((string) $_file) ? 1 : 0;
 
 213                         $_count += @unlink((string) $_file) ? 1 : 0;
 
 222      * Return array of tag/attributes of all tags used by an template
 
 224      * @param object $templae template object
 
 225      * @return array of tag/attributes
 
 227         function getTags(Smarty_Internal_Template $template) 
 
 229                 $template->smarty->get_used_tags = true;
 
 230                 $template->compileTemplateSource();
 
 231                 return $template->compiler_object->used_tags;
 
 234     function testInstall()
 
 238         echo "Smarty Installation test...\n";
 
 240         echo "Testing template directory...\n";
 
 242         foreach((array)$this->smarty->template_dir as $template_dir) {
 
 243             if (!is_dir($template_dir))
 
 244                 echo "FAILED: $template_dir is not a directory.\n";
 
 245             elseif (!is_readable($template_dir))
 
 246                 echo "FAILED: $template_dir is not readable.\n";
 
 248                 echo "$template_dir is OK.\n";
 
 251         echo "Testing compile directory...\n";
 
 253         if (!is_dir($this->smarty->compile_dir))
 
 254             echo "FAILED: {$this->smarty->compile_dir} is not a directory.\n";
 
 255         elseif (!is_readable($this->smarty->compile_dir))
 
 256             echo "FAILED: {$this->smarty->compile_dir} is not readable.\n";
 
 257         elseif (!is_writable($this->smarty->compile_dir))
 
 258             echo "FAILED: {$this->smarty->compile_dir} is not writable.\n";
 
 260             echo "{$this->smarty->compile_dir} is OK.\n";
 
 262         echo "Testing plugins directory...\n";
 
 264         foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
 
 265             if (!is_dir($plugin_dir))
 
 266                 echo "FAILED: $plugin_dir is not a directory.\n";
 
 267             elseif (!is_readable($plugin_dir))
 
 268                 echo "FAILED: $plugin_dir is not readable.\n";
 
 270                 echo "$plugin_dir is OK.\n";
 
 273         echo "Testing cache directory...\n";
 
 275         if (!is_dir($this->smarty->cache_dir))
 
 276             echo "FAILED: {$this->smarty->cache_dir} is not a directory.\n";
 
 277         elseif (!is_readable($this->smarty->cache_dir))
 
 278             echo "FAILED: {$this->smarty->cache_dir} is not readable.\n";
 
 279         elseif (!is_writable($this->smarty->cache_dir))
 
 280             echo "FAILED: {$this->smarty->cache_dir} is not writable.\n";
 
 282             echo "{$this->smarty->cache_dir} is OK.\n";
 
 284         echo "Testing configs directory...\n";
 
 286         if (!is_dir($this->smarty->config_dir))
 
 287             echo "FAILED: {$this->smarty->config_dir} is not a directory.\n";
 
 288         elseif (!is_readable($this->smarty->config_dir))
 
 289             echo "FAILED: {$this->smarty->config_dir} is not readable.\n";
 
 291             echo "{$this->smarty->config_dir} is OK.\n";
 
 293         echo "Tests complete.\n";