Exemple #1
0
/**
 * Inserts another template into the current one
 * <pre>
 *  * file : the resource name of the template
 *  * cache_time : cache length in seconds
 *  * cache_id : cache identifier for the included template
 *  * compile_id : compilation identifier for the included template
 *  * data : data to feed into the included template, it can be any array and will default to $_root (the current data)
 *  * assign : if set, the output of the included template will be saved in this variable instead of being output
 *  * rest : any additional parameter/value provided will be added to the data array
 * </pre>
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the use of this software.
 *
 * @author     Jordi Boggiano <*****@*****.**>
 * @copyright  Copyright (c) 2008, Jordi Boggiano
 * @license    http://dwoo.org/LICENSE   Modified BSD License
 * @link       http://dwoo.org/
 * @version    1.1.0
 * @date       2009-07-18
 * @package    Dwoo
 */
function Dwoo_Plugin_include(Dwoo_Core $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
{
    if ($file === '') {
        return;
    }
    if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) {
        // resource:identifier given, extract them
        $resource = $m[1];
        $identifier = $m[2];
    } else {
        // get the current template's resource
        $resource = $dwoo->getTemplate()->getResourceName();
        $identifier = $file;
    }
    try {
        $include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
    } catch (Dwoo_Security_Exception $e) {
        return $dwoo->triggerError('Include : Security restriction : ' . $e->getMessage(), E_USER_WARNING);
    } catch (Dwoo_Exception $e) {
        return $dwoo->triggerError('Include : ' . $e->getMessage(), E_USER_WARNING);
    }
    if ($include === null) {
        return $dwoo->triggerError('Include : Resource "' . $resource . ':' . $identifier . '" not found.', E_USER_WARNING);
    } elseif ($include === false) {
        return $dwoo->triggerError('Include : Resource "' . $resource . '" does not support includes.', E_USER_WARNING);
    }
    if (is_string($data)) {
        $vars = $dwoo->readVar($data);
    } else {
        $vars = $data;
    }
    if (count($rest)) {
        $vars = $rest + $vars;
    }
    $clone = clone $dwoo;
    $out = $clone->get($include, $vars);
    if ($assign !== null) {
        $dwoo->assignInScope($out, $assign);
    }
    foreach ($clone->getReturnValues() as $name => $value) {
        $dwoo->assignInScope($value, $name);
    }
    if ($assign === null) {
        return $out;
    }
}
Exemple #2
0
 /**
  * returns a new template object from the given include name, null if no include is
  * possible (resource not found), or false if include is not permitted by this resource type
  *
  * @param Dwoo_Core $dwoo the dwoo instance requiring it
  * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
  * @param int $cacheTime duration of the cache validity for this template,
  * 						 if null it defaults to the Dwoo instance that will
  * 						 render this template
  * @param string $cacheId the unique cache identifier of this page or anything else that
  * 						  makes this template's content unique, if null it defaults
  * 						  to the current url
  * @param string $compileId the unique compiled identifier, which is used to distinguish this
  * 							template from others, if null it defaults to the filename+bits of the path
  * @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
  * 											an include, extends or any other plugin)
  * @return Dwoo_Template_File|null
  */
 public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
 {
     if (DIRECTORY_SEPARATOR === '\\') {
         $resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId);
     }
     $resourceId = strtr($resourceId, '\\', '/');
     $includePath = null;
     if (file_exists($resourceId) === false) {
         if ($parentTemplate === null) {
             $parentTemplate = $dwoo->getTemplate();
         }
         if ($parentTemplate instanceof Dwoo_Template_File) {
             if ($includePath = $parentTemplate->getIncludePath()) {
                 if (strstr($resourceId, '../')) {
                     throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)');
                 }
             } else {
                 $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId;
                 if (file_exists($resourceId) === false) {
                     return null;
                 }
             }
         } else {
             return null;
         }
     }
     if ($policy = $dwoo->getSecurityPolicy()) {
         while (true) {
             if (preg_match('{^([a-z]+?)://}i', $resourceId)) {
                 throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>' . $resourceId . '</em>.');
             }
             if ($includePath) {
                 break;
             }
             $resourceId = realpath($resourceId);
             $dirs = $policy->getAllowedDirectories();
             foreach ($dirs as $dir => $dummy) {
                 if (strpos($resourceId, $dir) === 0) {
                     break 2;
                 }
             }
             throw new Dwoo_Security_Exception('The security policy prevents you to read <em>' . $resourceId . '</em>');
         }
     }
     $class = 'Dwoo_Template_File';
     if ($parentTemplate) {
         $class = get_class($parentTemplate);
     }
     return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath);
 }