Source for file Adapter.php

Documentation is available at Adapter.php

  1. <?php
  2.  
  3. if (!defined('DIR_SEP')) {
  4.     define('DIR_SEP'DIRECTORY_SEPARATOR);
  5. }
  6.  
  7. if (!defined('SMARTY_PHP_PASSTHRU')) {
  8.     define('SMARTY_PHP_PASSTHRU',   0);
  9.     define('SMARTY_PHP_QUOTE',      1);
  10.     define('SMARTY_PHP_REMOVE',     2);
  11.     define('SMARTY_PHP_ALLOW',      3);
  12. }
  13.  
  14. if (class_exists('Dwoo_Compiler'false=== false{
  15.     require 'Dwoo/Compiler.php';
  16. }
  17.  
  18. /**
  19.  * a Smarty compatibility layer for Dwoo
  20.  *
  21.  * This software is provided 'as-is', without any express or implied warranty.
  22.  * In no event will the authors be held liable for any damages arising from the use of this software.
  23.  *
  24.  * This file is released under the LGPL
  25.  * "GNU Lesser General Public License"
  26.  * More information can be found here:
  27.  * {@link http://www.gnu.org/copyleft/lesser.html}
  28.  *
  29.  * @author     Jordi Boggiano <[email protected]>
  30.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  31.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  32.  * @link       http://dwoo.org/
  33.  * @version    0.9.1
  34.  * @date       2008-05-30
  35.  * @package    Dwoo
  36.  */
  37. class Dwoo_Smarty__Adapter extends Dwoo
  38. {
  39.     // magic get/set/call functions that handle unsupported features
  40.     public function __set($p$v)
  41.     {
  42.         if ($p==='scope'{
  43.             $this->scope = $v;
  44.             return;
  45.         }
  46.         if (array_key_exists($p$this->compat['properties']!== false{
  47.             if ($this->show_compat_errors{
  48.                 $this->triggerError('Property '.$p.' is not available in the Dwoo_Smarty_Adapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.'E_USER_NOTICE);
  49.             }
  50.             $this->compat['properties'][$p$v;
  51.         else {
  52.             if ($this->show_compat_errors{
  53.                 $this->triggerError('Property '.$p.' is not available in the Dwoo_Smarty_Adapter, but it is not listed as such, so you might want to tell me about it at [email protected]'E_USER_NOTICE);
  54.             }
  55.         }
  56.     }
  57.  
  58.     public function __get($p)
  59.     {
  60.         if (array_key_exists($p$this->compat['properties']!== false{
  61.             if ($this->show_compat_errors{
  62.                 $this->triggerError('Property '.$p.' is not available in the Dwoo_Smarty_Adapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.'E_USER_NOTICE);
  63.             }
  64.             return $this->compat['properties'][$p];
  65.         else {
  66.             if ($this->show_compat_errors{
  67.                 $this->triggerError('Property '.$p.' is not available in the Dwoo_Smarty_Adapter, but it is not listed as such, so you might want to tell me about it at [email protected]'E_USER_NOTICE);
  68.             }
  69.         }
  70.     }
  71.  
  72.     public function __call($m$a)
  73.     {
  74.         if (method_exists($this->dataProvider$m)) {
  75.             call_user_func_array(array($this->dataProvider$m)$a);
  76.         elseif ($this->show_compat_errors{
  77.             if (array_search($m$this->compat['methods']!== false{
  78.                 $this->triggerError('Method '.$m.' is not available in the Dwoo_Smarty_Adapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.'E_USER_NOTICE);
  79.             else {
  80.                 $this->triggerError('Method '.$m.' is not available in the Dwoo_Smarty_Adapter, but it is not listed as such, so you might want to tell me about it at [email protected]'E_USER_NOTICE);
  81.             }
  82.         }
  83.     }
  84.  
  85.     // list of unsupported properties and methods
  86.     protected $compat = array
  87.     (
  88.         'methods' => array
  89.         (
  90.             'register_resource''unregister_resource''load_filter''clear_compiled_tpl',
  91.             'clear_config''get_config_vars''config_load'
  92.         ),
  93.         'properties' => array
  94.         (
  95.             'cache_handler_func' => null,
  96.             'debugging' => false,
  97.             'error_reporting' => null,
  98.             'debugging_ctrl' => 'NONE',
  99.             'request_vars_order' => 'EGPCS',
  100.             'request_use_auto_globals' => true,
  101.             'use_sub_dirs' => false,
  102.             'autoload_filters' => array(),
  103.             'default_template_handler_func' => '',
  104.             'debug_tpl' => '',
  105.             'cache_modified_check' => false,
  106.             'default_modifiers' => array(),
  107.             'default_resource_type' => 'file',
  108.             'config_overwrite' => true,
  109.             'config_booleanize' => true,
  110.             'config_read_hidden' => false,
  111.             'config_fix_newlines' => true,
  112.             'config_class' => 'Config_File',
  113.         ),
  114.     );
  115.  
  116.     // security vars
  117.     public $security = false;
  118.     public $trusted_dir = array();
  119.     public $secure_dir = array();
  120.     public $php_handling = SMARTY_PHP_PASSTHRU;
  121.     public $security_settings = array
  122.     (
  123.         'PHP_HANDLING'    => false,
  124.         'IF_FUNCS'        => array
  125.         (
  126.             'list''empty''count''sizeof',
  127.             'in_array''is_array',
  128.         ),
  129.         'INCLUDE_ANY'     => false,
  130.         'PHP_TAGS'        => false,
  131.         'MODIFIER_FUNCS'  => array(),
  132.         'ALLOW_CONSTANTS'  => false
  133.     );
  134.  
  135.     // paths
  136.     public $template_dir = 'templates';
  137.     public $compile_dir = 'templates_c';
  138.     public $config_dir = 'configs';
  139.     public $cache_dir = 'cache';
  140.     public $plugins_dir = array();
  141.  
  142.     // misc options
  143.     public $left_delimiter = '{';
  144.     public $right_delimiter = '}';
  145.     public $compile_check = true;
  146.     public $force_compile = false;
  147.     public $caching = 0;
  148.     public $cache_lifetime = 3600;
  149.     public $compile_id = null;
  150.     public $compiler_file = null;
  151.     public $compiler_class = null;
  152.  
  153.     // dwoo/smarty compat layer
  154.     public $show_compat_errors = false;
  155.     protected $dataProvider;
  156.     protected $_filters = array('pre'=>array()'post'=>array()'output'=>array());
  157.     protected static $tplCache array();
  158.     protected $compiler;
  159.  
  160.     public function __construct()
  161.     {
  162.         parent::__construct();
  163.         $this->charset = 'iso-8859-1';
  164.         $this->dataProvider = new Dwoo_Data();
  165.         $this->compiler = new Dwoo_Compiler();
  166.         $this->compiler->smartyCompat true;
  167.     }
  168.  
  169.     public function display($filename$cacheId=null$compileId=null)
  170.     {
  171.         $this->fetch($filename$cacheId$compileIdtrue);
  172.     }
  173.  
  174.     public function fetch($filename$cacheId=null$compileId=null$display=false)
  175.     {
  176.         if ($this->security{
  177.             $policy new Dwoo_Security_Policy();
  178.             $policy->addPhpFunction(array_merge($this->security_settings['IF_FUNCS']$this->security_settings['MODIFIER_FUNCS']));
  179.  
  180.             $phpTags $this->security_settings['PHP_HANDLING'SMARTY_PHP_ALLOW $this->php_handling;
  181.             if ($this->security_settings['PHP_TAGS']{
  182.                 $phpTags SMARTY_PHP_ALLOW;
  183.             }
  184.             switch($phpTags{
  185.                 case SMARTY_PHP_ALLOW:
  186.                 case SMARTY_PHP_PASSTHRU:
  187.                     $phpTags Dwoo_Security_Policy::PHP_ALLOW;
  188.                     break;
  189.                 case SMARTY_PHP_QUOTE:
  190.                     $phpTags Dwoo_Security_Policy::PHP_ENCODE;
  191.                     break;
  192.                 case SMARTY_PHP_REMOVE:
  193.                 default:
  194.                     $phpTags Dwoo_Security_Policy::PHP_REMOVE;
  195.                     break;
  196.             }
  197.             $policy->setPhpHandling($phpTags);
  198.  
  199.             $policy->setConstantHandling($this->security_settings['ALLOW_CONSTANTS']);
  200.  
  201.             if ($this->security_settings['INCLUDE_ANY']{
  202.                 $policy->allowDirectory(preg_replace('{^((?:[a-z]:)?[\\\\/]).*}i''$1'__FILE__));
  203.             else {
  204.                 $policy->allowDirectory($this->secure_dir);
  205.             }
  206.  
  207.             $this->setSecurityPolicy($policy);
  208.         }
  209.  
  210.         if (!empty($this->plugins_dir)) {
  211.             foreach ($this->plugins_dir as $dir{
  212.                 $this->getLoader()->addDirectory(rtrim($dir'\\/'));
  213.             }
  214.         }
  215.  
  216.         $tpl $this->makeTemplate($filename$cacheId$compileId);
  217.         if ($this->force_compile{
  218.             $tpl->forceCompilation();
  219.         }
  220.  
  221.         if ($this->caching > 0{
  222.             $this->cacheTime = $this->cache_lifetime;
  223.         else {
  224.             $this->cacheTime = 0;
  225.         }
  226.  
  227.         if ($this->compiler_class !== null{
  228.             if ($this->compiler_file !== null && !class_exists($this->compiler_classfalse)) {
  229.                 include $this->compiler_file;
  230.             }
  231.             $this->compiler = new $this->compiler_class;
  232.         else {
  233.             $this->compiler->addPreProcessor('smarty_compat'true);
  234.             $this->compiler->setLooseOpeningHandling(true);
  235.         }
  236.  
  237.         $this->compiler->setDelimiters($this->left_delimiter$this->right_delimiter);
  238.  
  239.         return $this->get($tpl$this->dataProvider$this->compiler$display===true);
  240.     }
  241.  
  242.     public function register_function($name$callback$cacheable=true$cache_attrs=null)
  243.     {
  244.         if (isset($this->plugins[$name]&& $this->plugins[$name][0!== self::SMARTY_FUNCTION{
  245.             throw new Dwoo_Exception('Multiple plugins of different types can not share the same name');
  246.         }
  247.         $this->plugins[$namearray('type'=>self::SMARTY_FUNCTION'callback'=>$callback);
  248.     }
  249.  
  250.     public function unregister_function($name)
  251.     {
  252.         unset($this->plugins[$name]);
  253.     }
  254.  
  255.     public function register_block($name$callback$cacheable=true$cache_attrs=null)
  256.     {
  257.         if (isset($this->plugins[$name]&& $this->plugins[$name][0!== self::SMARTY_BLOCK{
  258.             throw new Dwoo_Exception('Multiple plugins of different types can not share the same name');
  259.         }
  260.         $this->plugins[$namearray('type'=>self::SMARTY_BLOCK'callback'=>$callback);
  261.     }
  262.  
  263.     public function unregister_block($name)
  264.     {
  265.         unset($this->plugins[$name]);
  266.     }
  267.  
  268.     public function register_modifier($name$callback)
  269.     {
  270.         if (isset($this->plugins[$name]&& $this->plugins[$name][0!== self::SMARTY_MODIFIER{
  271.             throw new Dwoo_Exception('Multiple plugins of different types can not share the same name');
  272.         }
  273.         $this->plugins[$namearray('type'=>self::SMARTY_MODIFIER'callback'=>$callback);
  274.     }
  275.  
  276.     public function unregister_modifier($name)
  277.     {
  278.         unset($this->plugins[$name]);
  279.     }
  280.  
  281.     public function register_prefilter($callback)
  282.     {
  283.         $processor new Dwoo_SmartyProcessorAdapter($this->compiler);
  284.         $processor->registerCallback($callback);
  285.         $this->_filters['pre'][$processor;
  286.         $this->compiler->addPreProcessor($processor);
  287.     }
  288.  
  289.     public function unregister_prefilter($callback)
  290.     {
  291.         foreach ($this->_filters['pre'as $index => $processor)
  292.             if ($processor->callback === $callback{
  293.                 $this->compiler->removePostProcessor($processor);
  294.                 unset($this->_filters['pre'][$index]);
  295.             }
  296.     }
  297.  
  298.     public function register_postfilter($callback)
  299.     {
  300.         $processor new Dwoo_SmartyProcessorAdapter($this->compiler);
  301.         $processor->registerCallback($callback);
  302.         $this->_filters['post'][$processor;
  303.         $this->compiler->addPostProcessor($processor);
  304.     }
  305.  
  306.     public function unregister_postfilter($callback)
  307.     {
  308.         foreach ($this->_filters['post'as $index => $processor)
  309.             if ($processor->callback === $callback{
  310.                 $this->compiler->removePostProcessor($processor);
  311.                 unset($this->_filters['post'][$index]);
  312.             }
  313.     }
  314.  
  315.     public function register_outputfilter($callback)
  316.     {
  317.         $filter new Dwoo_SmartyFilterAdapter($this);
  318.         $filter->registerCallback($callback);
  319.         $this->_filters['output'][$filter;
  320.         $this->addFilter($filter);
  321.     }
  322.  
  323.     public function unregister_outputfilter($callback)
  324.     {
  325.         foreach ($this->_filters['output'as $index => $filter)
  326.             if ($filter->callback === $callback{
  327.                 $this->removeOutputFilter($filter);
  328.                 unset($this->_filters['output'][$index]);
  329.             }
  330.     }
  331.  
  332.     function register_object($object$object_impl$allowed array()$smarty_args false$block_methods array())
  333.     {
  334.         settype($allowed'array');
  335.         settype($block_methods'array');
  336.         settype($smarty_args'boolean');
  337.  
  338.         if (!empty($allowed&& $this->show_compat_errors{
  339.             $this->triggerError('You can register objects but can not restrict the method/properties used, this is PHP5, you have proper OOP access restrictions so use them.'E_USER_NOTICE);
  340.         }
  341.  
  342.         if ($smarty_args{
  343.             $this->triggerError('You can register objects but methods will be called using method($arg1, $arg2, $arg3), not as an argument array like smarty did.'E_USER_NOTICE);
  344.         }
  345.  
  346.         if (!empty($block_methods)) {
  347.             $this->triggerError('You can register objects but can not use methods as being block methods, you have to build a plugin for that.'E_USER_NOTICE);
  348.         }
  349.  
  350.         $this->dataProvider->assign($object$object_impl);
  351.     }
  352.  
  353.     function unregister_object($object)
  354.     {
  355.         $this->dataProvider->clear($object);
  356.     }
  357.  
  358.     function get_registered_object($name{
  359.         $data $this->dataProvider->getData();
  360.         if (isset($data[$name]&& is_object($data[$name])) {
  361.             return $data[$name];
  362.         else {
  363.             trigger_error('Dwoo_Compiler: object "'.$name.'" was not registered or is not an object'E_USER_ERROR);
  364.         }
  365.     }
  366.  
  367.     public function template_exists($filename)
  368.     {
  369.         return file_exists($this->template_dir.DIRECTORY_SEPARATOR.$filename);
  370.     }
  371.  
  372.        public function is_cached($tpl$cacheId null$compileId null)
  373.        {
  374.            return $this->isCached($this->makeTemplate($tpl$cacheId$compileId));
  375.        }
  376.  
  377.        public function append_by_ref($var&$value$merge=false)
  378.        {
  379.            $this->dataProvider->appendByRef($var$value$merge);
  380.        }
  381.  
  382.     public function assign_by_ref($name&$val)
  383.     {
  384.         $this->dataProvider->assignByRef($name$val);
  385.     }
  386.  
  387.        public function clear_assign($var)
  388.        {
  389.            $this->dataProvider->clear($var);
  390.        }
  391.  
  392.        public function clear_all_assign()
  393.        {
  394.            $this->dataProvider->clear();
  395.        }
  396.  
  397.     public function get_template_vars($name=null)
  398.     {
  399.         if ($this->show_compat_errors{
  400.             trigger_error('get_template_vars does not return values by reference, if you try to modify the data that way you should modify your code.'E_USER_NOTICE);
  401.         }
  402.  
  403.         $data $this->dataProvider->getData();
  404.            if ($name === null)
  405.                return $data;
  406.            elseif (isset($data[$name]))
  407.                return $data[$name];
  408.            return null;
  409.        }
  410.  
  411.        public function clear_all_cache($olderThan 0)
  412.        {
  413.            $this->clearCache($olderThan);
  414.        }
  415.  
  416.        public function clear_cache($template$cacheId null$compileId null$olderThan 0)
  417.        {
  418.            $this->makeTemplate($template$cacheId$compileId)->clearCache($olderThan);
  419.        }
  420.  
  421.     public function trigger_error($error_msg$error_type E_USER_WARNING)
  422.     {
  423.         $this->triggerError($error_msg$error_type);
  424.     }
  425.  
  426.     protected function initGlobals(Dwoo_ITemplate $tpl)
  427.     {
  428.         parent::initGlobals($tpl);
  429.         $this->globals['ldelim''{';
  430.         $this->globals['rdelim''}';
  431.     }
  432.  
  433.     protected function makeTemplate($file$cacheId$compileId)
  434.     {
  435.         $this->setCacheDir($this->cache_dir);
  436.         $this->setCompileDir($this->compile_dir);
  437.  
  438.            if ($compileId === null)
  439.                $compileId $this->compile_id;
  440.  
  441.         $hash bin2hex(md5($file.$cacheId.$compileIdtrue));
  442.         if (!isset(self::$tplCache[$hash])) {
  443.             // abs path
  444.             if (substr($file01=== '/' || substr($file11=== ':'{
  445.                 self::$tplCache[$hashnew Dwoo_Template_File($filenull$cacheId$compileId);
  446.             elseif (is_string($this->template_dir|| is_array($this->template_dir)) {
  447.                 self::$tplCache[$hashnew Dwoo_Template_File($filenull$cacheId$compileId$this->template_dir);
  448.             else {
  449.                 throw new Exception('Unable to load "'.$file.'", check the template_dir');
  450.             }
  451.         }
  452.         return self::$tplCache[$hash];
  453.     }
  454. }
  455.  
  456. {
  457.     public $callback;
  458.  
  459.     public function process($input)
  460.     {
  461.         return call_user_func($this->callback$input);
  462.     }
  463.  
  464.     public function registerCallback($callback)
  465.     {
  466.         $this->callback = $callback;
  467.     }
  468. }
  469.  
  470. {
  471.     public $callback;
  472.  
  473.     public function process($input)
  474.     {
  475.         return call_user_func($this->callback$input);
  476.     }
  477.  
  478.     public function registerCallback($callback)
  479.     {
  480.         $this->callback = $callback;
  481.     }
  482. }
  483.  
  484. // cloaks the adapter if possible with the smarty name to fool type-hinted plugins
  485. if (class_exists('Smarty'false=== false)
  486. {
  487.     interface Smarty {}
  488.     class Dwoo_Smarty_Adapter extends Dwoo_Smarty__Adapter implements Smarty {}
  489. }
  490. else
  491. {
  492.     class Dwoo_Smarty_Adapter extends Dwoo_Smarty__Adapter {}
  493. }

Documentation generated on Sun, 03 Aug 2008 15:12:16 +0200 by phpDocumentor 1.4.0