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.  * This file is released under the LGPL
  10.  * "GNU Lesser General Public License"
  11.  * More information can be found here:
  12.  * {@link http://www.gnu.org/copyleft/lesser.html}
  13.  *
  14.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  15.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  16.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  17.  * @link       http://dwoo.org/
  18.  * @version    0.9.1
  19.  * @date       2008-05-30
  20.  * @package    Dwoo
  21.  */
  22. {
  23.     /**
  24.      * template filename
  25.      *
  26.      * @var string 
  27.      */
  28.     protected $file;
  29.  
  30.     /**
  31.      * include path(s) to look into to find this template
  32.      *
  33.      * @var array 
  34.      */
  35.     protected $includePath = null;
  36.  
  37.     /**
  38.      * resolved path cache when looking for a file in multiple include paths
  39.      *
  40.      * this is reset when the include path is changed
  41.      *
  42.      * @var string 
  43.      */
  44.     protected $resolvedPath = null;
  45.  
  46.     /**
  47.      * creates a template from a file
  48.      *
  49.      * @param string $file the path to the template file, make sure it exists
  50.      * @param int $cacheTime duration of the cache validity for this template,
  51.      *                           if null it defaults to the Dwoo instance that will
  52.      *                           render this template
  53.      * @param string $cacheId the unique cache identifier of this page or anything else that
  54.      *                            makes this template's content unique, if null it defaults
  55.      *                            to the current url
  56.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  57.      *                              template from others, if null it defaults to the filename+bits of the path
  58.      * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths
  59.      */
  60.     public function __construct($file$cacheTime null$cacheId null$compileId null$includePath null)
  61.     {
  62.         $this->file = $file;
  63.         $this->name = basename($file);
  64.         $this->cacheTime = $cacheTime;
  65.  
  66.         if ($compileId !== null{
  67.             $this->compileId = str_replace('../''__'strtr($compileId'\\%?=!:;'.PATH_SEPARATOR'/-------'));
  68.         }
  69.  
  70.         if ($cacheId !== null{
  71.             $this->cacheId = str_replace('../''__'strtr($cacheId'\\%?=!:;'.PATH_SEPARATOR'/-------'));
  72.         }
  73.  
  74.         if (is_string($includePath)) {
  75.             $this->includePath = array($includePath);
  76.         elseif (is_array($includePath)) {
  77.             $this->includePath = $includePath;
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * sets the include path(s) to where the given template filename must be looked up
  83.      *
  84.      * @param mixed $paths the path to look into, can be string for a single path or an array of paths
  85.      */
  86.     public function setIncludePath($paths)
  87.     {
  88.         if (is_array($paths=== false{
  89.             $paths array($paths);
  90.         }
  91.  
  92.         $this->includePath = $paths;
  93.         $this->resolvedPath = null;
  94.     }
  95.  
  96.     /**
  97.      * return the current include path(s)
  98.      *
  99.      * @return array 
  100.      */
  101.     public function getIncludePath()
  102.     {
  103.         return $this->includePath;
  104.     }
  105.  
  106.     /**
  107.      * returns the compiled template file name
  108.      *
  109.      * @param Dwoo $dwoo the dwoo instance that requests it
  110.      * @param Dwoo_ICompiler $compiler the compiler that must be used
  111.      * @return string 
  112.      */
  113.     public function getCompiledTemplate(Dwoo $dwooDwoo_ICompiler $compiler null)
  114.     {
  115.         $compiledFile $this->getCompiledFilename($dwoo);
  116.  
  117.         if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]=== true{
  118.             // already checked, return compiled file
  119.         elseif ($this->compilationEnforced !== true && file_exists($compiledFile)===true && (int)$this->getUid(<= filemtime($compiledFile)) {
  120.             // template is compiled and has not been modified since the compilation
  121.             self::$cache['compiled'][$this->compileIdtrue;
  122.         else {
  123.             // compiles the template
  124.             $this->compilationEnforced = false;
  125.  
  126.             if ($compiler === null{
  127.                 $compiler $dwoo->getDefaultCompilerFactory('string');
  128.  
  129.                 if ($compiler === null || $compiler === array('Dwoo_Compiler''compilerFactory')) {
  130.                     if (class_exists('Dwoo_Compiler'false=== false{
  131.                         include DWOO_DIRECTORY 'Dwoo/Compiler.php';
  132.                     }
  133.                     $compiler Dwoo_Compiler::compilerFactory();
  134.                 else {
  135.                     $compiler call_user_func($compiler);
  136.                 }
  137.             }
  138.  
  139.             $this->compiler = $compiler;
  140.  
  141.             $compiler->setCustomPlugins($dwoo->getCustomPlugins());
  142.             $compiler->setSecurityPolicy($dwoo->getSecurityPolicy());
  143.             $this->makeDirectory(dirname($compiledFile));
  144.             file_put_contents($compiledFile$compiler->compile($dwoo$this));
  145.             if ($this->chmod !== null{
  146.                 chmod($compiledFile$this->chmod);
  147.             }
  148.  
  149.             self::$cache['compiled'][$this->compileIdtrue;
  150.         }
  151.  
  152.         return $compiledFile;
  153.     }
  154.  
  155.     /**
  156.      * returns the template source of this template
  157.      *
  158.      * @return string 
  159.      */
  160.     public function getSource()
  161.     {
  162.         return file_get_contents($this->getResourceIdentifier());
  163.     }
  164.  
  165.     /**
  166.      * returns the resource name for this template class
  167.      *
  168.      * @return string 
  169.      */
  170.     public function getResourceName()
  171.     {
  172.         return 'file';
  173.     }
  174.  
  175.     /**
  176.      * returns this template's source filename
  177.      *
  178.      * @return string 
  179.      */
  180.     public function getResourceIdentifier()
  181.     {
  182.         if ($this->resolvedPath !== null{
  183.             return $this->resolvedPath;
  184.         elseif ($this->includePath === null{
  185.             return $this->file;
  186.         else {
  187.             foreach ($this->includePath as $path{
  188.                 if (file_exists($path.DIRECTORY_SEPARATOR.$this->file=== true{
  189.                     $this->resolvedPath = $path DIRECTORY_SEPARATOR $this->file;
  190.                     return $this->resolvedPath;
  191.                 }
  192.             }
  193.  
  194.             throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)');
  195.         }
  196.     }
  197.  
  198.     /**
  199.      * returns an unique value identifying the current version of this template,
  200.      * in this case it's the unix timestamp of the last modification
  201.      *
  202.      * @return string 
  203.      */
  204.     public function getUid()
  205.     {
  206.         return (string) filemtime($this->getResourceIdentifier());
  207.     }
  208.  
  209.     /**
  210.      * returns a new template object from the given include name, null if no include is
  211.      * possible (resource not found), or false if include is not permitted by this resource type
  212.      *
  213.      * @param Dwoo $dwoo the dwoo instance requiring it
  214.      * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
  215.      * @param int $cacheTime duration of the cache validity for this template,
  216.      *                           if null it defaults to the Dwoo instance that will
  217.      *                           render this template
  218.      * @param string $cacheId the unique cache identifier of this page or anything else that
  219.      *                            makes this template's content unique, if null it defaults
  220.      *                            to the current url
  221.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  222.      *                              template from others, if null it defaults to the filename+bits of the path
  223.      * @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
  224.      *                                              an include, extends or any other plugin)
  225.      * @return Dwoo_Template_File|null
  226.      */
  227.     public static function templateFactory(Dwoo $dwoo$resourceId$cacheTime null$cacheId null$compileId nullDwoo_ITemplate $parentTemplate null)
  228.     {
  229.         if (DIRECTORY_SEPARATOR === '\\'{
  230.             $resourceId str_replace(array("\t""\n""\r""\f""\v")array('\\t''\\n''\\r''\\f''\\v')$resourceId);
  231.         }
  232.         $resourceId strtr($resourceId'\\''/');
  233.  
  234.         $includePath null;
  235.  
  236.         if (file_exists($resourceId=== false{
  237.             if ($parentTemplate === null{
  238.                 $parentTemplate $dwoo->getTemplate();
  239.             }
  240.             if ($parentTemplate instanceof Dwoo_Template_File{
  241.                 if ($includePath $parentTemplate->getIncludePath()) {
  242.                     if (strstr($resourceId'../')) {
  243.                         throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)');
  244.                     }
  245.                 else {
  246.                     $resourceId dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
  247.                     if (file_exists($resourceId=== false{
  248.                         return null;
  249.                     }
  250.                 }
  251.             else {
  252.                 return null;
  253.             }
  254.         }
  255.  
  256.         if ($policy $dwoo->getSecurityPolicy()) {
  257.             while (true{
  258.                 if (preg_match('{^([a-z]+?)://}i'$resourceId)) {
  259.                     throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.');
  260.                 }
  261.  
  262.                 if ($includePath{
  263.                     break;
  264.                 }
  265.  
  266.                 $resourceId realpath($resourceId);
  267.                 $dirs $policy->getAllowedDirectories();
  268.                 foreach ($dirs as $dir=>$dummy{
  269.                     if (strpos($resourceId$dir=== 0{
  270.                         break 2;
  271.                     }
  272.                 }
  273.                 throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>');
  274.             }
  275.         }
  276.  
  277.         return new Dwoo_Template_File($resourceId$cacheTime$cacheId$compileId$includePath);
  278.     }
  279.  
  280.     /**
  281.      * returns the full compiled file name and assigns a default value to it if
  282.      * required
  283.      *
  284.      * @param Dwoo $dwoo the dwoo instance that requests the file name
  285.      * @return string the full path to the compiled file
  286.      */
  287.     protected function getCompiledFilename(Dwoo $dwoo)
  288.     {
  289.         // no compile id was provided, set default
  290.         if ($this->compileId===null{
  291.             $this->compileId = str_replace('../''__'strtr($this->getResourceIdentifier()'\\:''/-'));
  292.         }
  293.         return $dwoo->getCompileDir($this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
  294.     }
  295. }

Documentation generated on Sun, 07 Sep 2008 23:57:47 +0200 by phpDocumentor 1.4.0