Source for file File.php

Documentation is available at File.php

  1. <?php
  2.  
  3. /**
  4.  * represents a Dwoo template contained in a file
  5.  *
  6.  * This software is provided 'as-is', without any express or implied warranty.
  7.  * In no event will the authors be held liable for any damages arising from the use of this software.
  8.  *
  9.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  10.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  11.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  12.  * @link       http://dwoo.org/
  13.  * @version    1.0.0
  14.  * @date       2008-10-23
  15.  * @package    Dwoo
  16.  */
  17. {
  18.     /**
  19.      * template filename
  20.      *
  21.      * @var string 
  22.      */
  23.     protected $file;
  24.  
  25.     /**
  26.      * include path(s) to look into to find this template
  27.      *
  28.      * @var array 
  29.      */
  30.     protected $includePath = null;
  31.  
  32.     /**
  33.      * resolved path cache when looking for a file in multiple include paths
  34.      *
  35.      * this is reset when the include path is changed
  36.      *
  37.      * @var string 
  38.      */
  39.     protected $resolvedPath = null;
  40.  
  41.     /**
  42.      * creates a template from a file
  43.      *
  44.      * @param string $file the path to the template file, make sure it exists
  45.      * @param int $cacheTime duration of the cache validity for this template,
  46.      *                           if null it defaults to the Dwoo instance that will
  47.      *                           render this template
  48.      * @param string $cacheId the unique cache identifier of this page or anything else that
  49.      *                            makes this template's content unique, if null it defaults
  50.      *                            to the current url
  51.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  52.      *                              template from others, if null it defaults to the filename+bits of the path
  53.      * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths
  54.      */
  55.     public function __construct($file$cacheTime null$cacheId null$compileId null$includePath null)
  56.     {
  57.         $this->file = $file;
  58.         $this->name = basename($file);
  59.         $this->cacheTime = $cacheTime;
  60.  
  61.         if ($compileId !== null{
  62.             $this->compileId = str_replace('../''__'strtr($compileId'\\%?=!:;'.PATH_SEPARATOR'/-------'));
  63.         }
  64.  
  65.         if ($cacheId !== null{
  66.             $this->cacheId = str_replace('../''__'strtr($cacheId'\\%?=!:;'.PATH_SEPARATOR'/-------'));
  67.         }
  68.  
  69.         if (is_string($includePath)) {
  70.             $this->includePath = array($includePath);
  71.         elseif (is_array($includePath)) {
  72.             $this->includePath = $includePath;
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * sets the include path(s) to where the given template filename must be looked up
  78.      *
  79.      * @param mixed $paths the path to look into, can be string for a single path or an array of paths
  80.      */
  81.     public function setIncludePath($paths)
  82.     {
  83.         if (is_array($paths=== false{
  84.             $paths array($paths);
  85.         }
  86.  
  87.         $this->includePath = $paths;
  88.         $this->resolvedPath = null;
  89.     }
  90.  
  91.     /**
  92.      * return the current include path(s)
  93.      *
  94.      * @return array 
  95.      */
  96.     public function getIncludePath()
  97.     {
  98.         return $this->includePath;
  99.     }
  100.     
  101.  
  102.     
  103.     /**
  104.      * Checks if compiled file is valid (exists and it's the modification is greater or
  105.      * equal to the modification time of the template file)
  106.      * 
  107.      * @param string file
  108.      * @return boolean True cache file existance and it's modification time
  109.      */
  110.     protected function isValidCompiledFile($file{
  111.         return parent::isValidCompiledFile($file&& (int)$this->getUid(<= filemtime($file);
  112.     }
  113.  
  114.     /**
  115.      * returns the template source of this template
  116.      *
  117.      * @return string 
  118.      */
  119.     public function getSource()
  120.     {
  121.         return file_get_contents($this->getResourceIdentifier());
  122.     }
  123.  
  124.     /**
  125.      * returns the resource name for this template class
  126.      *
  127.      * @return string 
  128.      */
  129.     public function getResourceName()
  130.     {
  131.         return 'file';
  132.     }
  133.  
  134.     /**
  135.      * returns this template's source filename
  136.      *
  137.      * @return string 
  138.      */
  139.     public function getResourceIdentifier()
  140.     {
  141.         if ($this->resolvedPath !== null{
  142.             return $this->resolvedPath;
  143.         elseif ($this->includePath === null{
  144.             return $this->file;
  145.         else {
  146.             foreach ($this->includePath as $path{
  147.                 if (file_exists($path.DIRECTORY_SEPARATOR.$this->file=== true{
  148.                     $this->resolvedPath = $path DIRECTORY_SEPARATOR $this->file;
  149.                     return $this->resolvedPath;
  150.                 }
  151.             }
  152.  
  153.             throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)');
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * returns an unique value identifying the current version of this template,
  159.      * in this case it's the unix timestamp of the last modification
  160.      *
  161.      * @return string 
  162.      */
  163.     public function getUid()
  164.     {
  165.         return (string) filemtime($this->getResourceIdentifier());
  166.     }
  167.  
  168.     /**
  169.      * returns a new template object from the given include name, null if no include is
  170.      * possible (resource not found), or false if include is not permitted by this resource type
  171.      *
  172.      * @param Dwoo $dwoo the dwoo instance requiring it
  173.      * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
  174.      * @param int $cacheTime duration of the cache validity for this template,
  175.      *                           if null it defaults to the Dwoo instance that will
  176.      *                           render this template
  177.      * @param string $cacheId the unique cache identifier of this page or anything else that
  178.      *                            makes this template's content unique, if null it defaults
  179.      *                            to the current url
  180.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  181.      *                              template from others, if null it defaults to the filename+bits of the path
  182.      * @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
  183.      *                                              an include, extends or any other plugin)
  184.      * @return Dwoo_Template_File|null
  185.      */
  186.     public static function templateFactory(Dwoo $dwoo$resourceId$cacheTime null$cacheId null$compileId nullDwoo_ITemplate $parentTemplate null)
  187.     {
  188.         if (DIRECTORY_SEPARATOR === '\\'{
  189.             $resourceId str_replace(array("\t""\n""\r""\f""\v")array('\\t''\\n''\\r''\\f''\\v')$resourceId);
  190.         }
  191.         $resourceId strtr($resourceId'\\''/');
  192.  
  193.         $includePath null;
  194.  
  195.         if (file_exists($resourceId=== false{
  196.             if ($parentTemplate === null{
  197.                 $parentTemplate $dwoo->getTemplate();
  198.             }
  199.             if ($parentTemplate instanceof Dwoo_Template_File{
  200.                 if ($includePath $parentTemplate->getIncludePath()) {
  201.                     if (strstr($resourceId'../')) {
  202.                         throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)');
  203.                     }
  204.                 else {
  205.                     $resourceId dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
  206.                     if (file_exists($resourceId=== false{
  207.                         return null;
  208.                     }
  209.                 }
  210.             else {
  211.                 return null;
  212.             }
  213.         }
  214.  
  215.         if ($policy $dwoo->getSecurityPolicy()) {
  216.             while (true{
  217.                 if (preg_match('{^([a-z]+?)://}i'$resourceId)) {
  218.                     throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.');
  219.                 }
  220.  
  221.                 if ($includePath{
  222.                     break;
  223.                 }
  224.  
  225.                 $resourceId realpath($resourceId);
  226.                 $dirs $policy->getAllowedDirectories();
  227.                 foreach ($dirs as $dir=>$dummy{
  228.                     if (strpos($resourceId$dir=== 0{
  229.                         break 2;
  230.                     }
  231.                 }
  232.                 throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>');
  233.             }
  234.         }
  235.  
  236.         return new Dwoo_Template_File($resourceId$cacheTime$cacheId$compileId$includePath);
  237.     }
  238.  
  239.     /**
  240.      * returns the full compiled file name and assigns a default value to it if
  241.      * required
  242.      *
  243.      * @param Dwoo $dwoo the dwoo instance that requests the file name
  244.      * @return string the full path to the compiled file
  245.      */
  246.     protected function getCompiledFilename(Dwoo $dwoo)
  247.     {
  248.         // no compile id was provided, set default
  249.         if ($this->compileId===null{
  250.             $this->compileId = str_replace('../''__'strtr($this->getResourceIdentifier()'\\:''/-'));
  251.         }
  252.         return $dwoo->getCompileDir($this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
  253.     }
  254. }

Documentation generated on Wed, 24 Dec 2008 02:13:28 +0100 by phpDocumentor 1.4.0