gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

smarty_internal_data.php (13720B)


      1 <?php
      2 /**
      3  * Smarty Internal Plugin Data
      4  * This file contains the basic classes and methods for template and variable creation
      5  *
      6  * @package    Smarty
      7  * @subpackage Template
      8  * @author     Uwe Tews
      9  */
     10 
     11 /**
     12  * Base class with template and variable methods
     13  *
     14  * @package    Smarty
     15  * @subpackage Template
     16  */
     17 class Smarty_Internal_Data
     18 {
     19     /**
     20      * name of class used for templates
     21      *
     22      * @var string
     23      */
     24     public $template_class = 'Smarty_Internal_Template';
     25     /**
     26      * template variables
     27      *
     28      * @var array
     29      */
     30     public $tpl_vars = array();
     31     /**
     32      * parent template (if any)
     33      *
     34      * @var Smarty_Internal_Template
     35      */
     36     public $parent = null;
     37     /**
     38      * configuration settings
     39      *
     40      * @var array
     41      */
     42     public $config_vars = array();
     43 
     44     /**
     45      * assigns a Smarty variable
     46      *
     47      * @param  array|string $tpl_var the template variable name(s)
     48      * @param  mixed        $value   the value to assign
     49      * @param  boolean      $nocache if true any output of this variable will be not cached
     50      *
     51      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
     52      */
     53     public function assign($tpl_var, $value = null, $nocache = false)
     54     {
     55         if (is_array($tpl_var)) {
     56             foreach ($tpl_var as $_key => $_val) {
     57                 if ($_key != '') {
     58                     $this->tpl_vars[$_key] = new Smarty_Variable($_val, $nocache);
     59                 }
     60             }
     61         } else {
     62             if ($tpl_var != '') {
     63                 $this->tpl_vars[$tpl_var] = new Smarty_Variable($value, $nocache);
     64             }
     65         }
     66 
     67         return $this;
     68     }
     69 
     70     /**
     71      * assigns a global Smarty variable
     72      *
     73      * @param  string  $varname the global variable name
     74      * @param  mixed   $value   the value to assign
     75      * @param  boolean $nocache if true any output of this variable will be not cached
     76      *
     77      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
     78      */
     79     public function assignGlobal($varname, $value = null, $nocache = false)
     80     {
     81         if ($varname != '') {
     82             Smarty::$global_tpl_vars[$varname] = new Smarty_Variable($value, $nocache);
     83             $ptr = $this;
     84             while ($ptr instanceof Smarty_Internal_Template) {
     85                 $ptr->tpl_vars[$varname] = clone Smarty::$global_tpl_vars[$varname];
     86                 $ptr = $ptr->parent;
     87             }
     88         }
     89 
     90         return $this;
     91     }
     92 
     93     /**
     94      * assigns values to template variables by reference
     95      *
     96      * @param string   $tpl_var the template variable name
     97      * @param          $value
     98      * @param  boolean $nocache if true any output of this variable will be not cached
     99      *
    100      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    101      */
    102     public function assignByRef($tpl_var, &$value, $nocache = false)
    103     {
    104         if ($tpl_var != '') {
    105             $this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
    106             $this->tpl_vars[$tpl_var]->value = &$value;
    107         }
    108 
    109         return $this;
    110     }
    111 
    112     /**
    113      * appends values to template variables
    114      *
    115      * @param  array|string $tpl_var the template variable name(s)
    116      * @param  mixed        $value   the value to append
    117      * @param  boolean      $merge   flag if array elements shall be merged
    118      * @param  boolean      $nocache if true any output of this variable will be not cached
    119      *
    120      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    121      */
    122     public function append($tpl_var, $value = null, $merge = false, $nocache = false)
    123     {
    124         if (is_array($tpl_var)) {
    125             // $tpl_var is an array, ignore $value
    126             foreach ($tpl_var as $_key => $_val) {
    127                 if ($_key != '') {
    128                     if (!isset($this->tpl_vars[$_key])) {
    129                         $tpl_var_inst = $this->getVariable($_key, null, true, false);
    130                         if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
    131                             $this->tpl_vars[$_key] = new Smarty_Variable(null, $nocache);
    132                         } else {
    133                             $this->tpl_vars[$_key] = clone $tpl_var_inst;
    134                         }
    135                     }
    136                     if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
    137                         settype($this->tpl_vars[$_key]->value, 'array');
    138                     }
    139                     if ($merge && is_array($_val)) {
    140                         foreach ($_val as $_mkey => $_mval) {
    141                             $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
    142                         }
    143                     } else {
    144                         $this->tpl_vars[$_key]->value[] = $_val;
    145                     }
    146                 }
    147             }
    148         } else {
    149             if ($tpl_var != '' && isset($value)) {
    150                 if (!isset($this->tpl_vars[$tpl_var])) {
    151                     $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
    152                     if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
    153                         $this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
    154                     } else {
    155                         $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
    156                     }
    157                 }
    158                 if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
    159                     settype($this->tpl_vars[$tpl_var]->value, 'array');
    160                 }
    161                 if ($merge && is_array($value)) {
    162                     foreach ($value as $_mkey => $_mval) {
    163                         $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
    164                     }
    165                 } else {
    166                     $this->tpl_vars[$tpl_var]->value[] = $value;
    167                 }
    168             }
    169         }
    170 
    171         return $this;
    172     }
    173 
    174     /**
    175      * appends values to template variables by reference
    176      *
    177      * @param  string  $tpl_var the template variable name
    178      * @param  mixed   &$value  the referenced value to append
    179      * @param  boolean $merge   flag if array elements shall be merged
    180      *
    181      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    182      */
    183     public function appendByRef($tpl_var, &$value, $merge = false)
    184     {
    185         if ($tpl_var != '' && isset($value)) {
    186             if (!isset($this->tpl_vars[$tpl_var])) {
    187                 $this->tpl_vars[$tpl_var] = new Smarty_Variable();
    188             }
    189             if (!is_array($this->tpl_vars[$tpl_var]->value)) {
    190                 settype($this->tpl_vars[$tpl_var]->value, 'array');
    191             }
    192             if ($merge && is_array($value)) {
    193                 foreach ($value as $_key => $_val) {
    194                     $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
    195                 }
    196             } else {
    197                 $this->tpl_vars[$tpl_var]->value[] = &$value;
    198             }
    199         }
    200 
    201         return $this;
    202     }
    203 
    204     /**
    205      * Returns a single or all template variables
    206      *
    207      * @param  string  $varname        variable name or null
    208      * @param  object  $_ptr           optional pointer to data object
    209      * @param  boolean $search_parents include parent templates?
    210      *
    211      * @return string  variable value or or array of variables
    212      */
    213     public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
    214     {
    215         if (isset($varname)) {
    216             $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
    217             if (is_object($_var)) {
    218                 return $_var->value;
    219             } else {
    220                 return null;
    221             }
    222         } else {
    223             $_result = array();
    224             if ($_ptr === null) {
    225                 $_ptr = $this;
    226             }
    227             while ($_ptr !== null) {
    228                 foreach ($_ptr->tpl_vars AS $key => $var) {
    229                     if (!array_key_exists($key, $_result)) {
    230                         $_result[$key] = $var->value;
    231                     }
    232                 }
    233                 // not found, try at parent
    234                 if ($search_parents) {
    235                     $_ptr = $_ptr->parent;
    236                 } else {
    237                     $_ptr = null;
    238                 }
    239             }
    240             if ($search_parents && isset(Smarty::$global_tpl_vars)) {
    241                 foreach (Smarty::$global_tpl_vars AS $key => $var) {
    242                     if (!array_key_exists($key, $_result)) {
    243                         $_result[$key] = $var->value;
    244                     }
    245                 }
    246             }
    247 
    248             return $_result;
    249         }
    250     }
    251 
    252     /**
    253      * clear the given assigned template variable.
    254      *
    255      * @param  string|array $tpl_var the template variable(s) to clear
    256      *
    257      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    258      */
    259     public function clearAssign($tpl_var)
    260     {
    261         if (is_array($tpl_var)) {
    262             foreach ($tpl_var as $curr_var) {
    263                 unset($this->tpl_vars[$curr_var]);
    264             }
    265         } else {
    266             unset($this->tpl_vars[$tpl_var]);
    267         }
    268 
    269         return $this;
    270     }
    271 
    272     /**
    273      * clear all the assigned template variables.
    274      *
    275      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    276      */
    277     public function clearAllAssign()
    278     {
    279         $this->tpl_vars = array();
    280 
    281         return $this;
    282     }
    283 
    284     /**
    285      * load a config file, optionally load just selected sections
    286      *
    287      * @param  string $config_file filename
    288      * @param  mixed  $sections    array of section names, single section or null
    289      *
    290      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    291      */
    292     public function configLoad($config_file, $sections = null)
    293     {
    294         // load Config class
    295         Smarty_Internal_Extension_Config::configLoad($this, $config_file, $sections);
    296         return $this;
    297     }
    298 
    299     /**
    300      * gets the object of a Smarty variable
    301      *
    302      * @param  string  $variable       the name of the Smarty variable
    303      * @param  object  $_ptr           optional pointer to data object
    304      * @param  boolean $search_parents search also in parent data
    305      * @param bool     $error_enable
    306      *
    307      * @return object  the object of the variable
    308      */
    309     public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
    310     {
    311         if ($_ptr === null) {
    312             $_ptr = $this;
    313         }
    314         while ($_ptr !== null) {
    315             if (isset($_ptr->tpl_vars[$variable])) {
    316                 // found it, return it
    317                 return $_ptr->tpl_vars[$variable];
    318             }
    319             // not found, try at parent
    320             if ($search_parents) {
    321                 $_ptr = $_ptr->parent;
    322             } else {
    323                 $_ptr = null;
    324             }
    325         }
    326         if (isset(Smarty::$global_tpl_vars[$variable])) {
    327             // found it, return it
    328             return Smarty::$global_tpl_vars[$variable];
    329         }
    330         $smarty = isset($this->smarty) ? $this->smarty : $this;
    331         if ($smarty->error_unassigned && $error_enable) {
    332             // force a notice
    333             $x = $$variable;
    334         }
    335 
    336         return new Smarty_Undefined_Variable;
    337     }
    338 
    339     /**
    340      * gets  a config variable
    341      *
    342      * @param  string $variable the name of the config variable
    343      * @param bool    $error_enable
    344      *
    345      * @return mixed  the value of the config variable
    346      */
    347     public function getConfigVariable($variable, $error_enable = true)
    348     {
    349         return Smarty_Internal_Extension_Config::getConfigVariable($this, $variable, $error_enable = true);
    350     }
    351 
    352     /**
    353      * Returns a single or all config variables
    354      *
    355      * @param  string $varname variable name or null
    356      * @param bool    $search_parents
    357      *
    358      * @return string variable value or or array of variables
    359      */
    360     public function getConfigVars($varname = null, $search_parents = true)
    361     {
    362         return Smarty_Internal_Extension_Config::getConfigVars($this, $varname, $search_parents);
    363     }
    364 
    365     /**
    366      * Deassigns a single or all config variables
    367      *
    368      * @param  string $varname variable name or null
    369      *
    370      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
    371      */
    372     public function clearConfig($varname = null)
    373     {
    374         return Smarty_Internal_Extension_Config::clearConfig($this, $varname);
    375     }
    376 
    377     /**
    378      * gets  a stream variable
    379      *
    380      * @param  string $variable the stream of the variable
    381      *
    382      * @throws SmartyException
    383      * @return mixed  the value of the stream variable
    384      */
    385     public function getStreamVariable($variable)
    386     {
    387         $_result = '';
    388         $fp = fopen($variable, 'r+');
    389         if ($fp) {
    390             while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
    391                 $_result .= $current_line;
    392             }
    393             fclose($fp);
    394 
    395             return $_result;
    396         }
    397         $smarty = isset($this->smarty) ? $this->smarty : $this;
    398         if ($smarty->error_unassigned) {
    399             throw new SmartyException('Undefined stream variable "' . $variable . '"');
    400         } else {
    401             return null;
    402         }
    403     }
    404 }