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

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