[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/Smarty/libs/plugins/ -> function.math.php (source)

   1  <?php
   2  /**
   3   * Smarty plugin
   4   *
   5   * This plugin is only for Smarty2 BC
   6   * @package Smarty
   7   * @subpackage PluginsFunction
   8   */
   9  
  10  /**
  11   * Smarty {math} function plugin
  12   *
  13   * Type:     function<br>
  14   * Name:     math<br>
  15   * Purpose:  handle math computations in template
  16   *
  17   * @link http://www.smarty.net/manual/en/language.function.math.php {math}
  18   *          (Smarty online manual)
  19   * @author   Monte Ohrt <monte at ohrt dot com>
  20   * @param array                    $params   parameters
  21   * @param Smarty_Internal_Template $template template object
  22   * @return string|null
  23   */
  24  function smarty_function_math($params, $template)
  25  {
  26      static $_allowed_funcs = array(
  27          'int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
  28          'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true,
  29          'rand' => true, 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true ,'tan' => true
  30      );
  31      // be sure equation parameter is present
  32      if (empty($params['equation'])) {
  33          trigger_error("math: missing equation parameter",E_USER_WARNING);
  34          return;
  35      }
  36  
  37      $equation = $params['equation'];
  38  
  39      // make sure parenthesis are balanced
  40      if (substr_count($equation,"(") != substr_count($equation,")")) {
  41          trigger_error("math: unbalanced parenthesis",E_USER_WARNING);
  42          return;
  43      }
  44  
  45      // match all vars in equation, make sure all are passed
  46      preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match);
  47  
  48      foreach($match[1] as $curr_var) {
  49          if ($curr_var && !isset($params[$curr_var]) && !isset($_allowed_funcs[$curr_var])) {
  50              trigger_error("math: function call $curr_var not allowed",E_USER_WARNING);
  51              return;
  52          }
  53      }
  54  
  55      foreach($params as $key => $val) {
  56          if ($key != "equation" && $key != "format" && $key != "assign") {
  57              // make sure value is not empty
  58              if (strlen($val)==0) {
  59                  trigger_error("math: parameter $key is empty",E_USER_WARNING);
  60                  return;
  61              }
  62              if (!is_numeric($val)) {
  63                  trigger_error("math: parameter $key: is not numeric",E_USER_WARNING);
  64                  return;
  65              }
  66              $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  67          }
  68      }
  69      $smarty_math_result = null;
  70      eval("\$smarty_math_result = ".$equation.";");
  71  
  72      if (empty($params['format'])) {
  73          if (empty($params['assign'])) {
  74              return $smarty_math_result;
  75          } else {
  76              $template->assign($params['assign'],$smarty_math_result);
  77          }
  78      } else {
  79          if (empty($params['assign'])){
  80              printf($params['format'],$smarty_math_result);
  81          } else {
  82              $template->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
  83          }
  84      }
  85  }
  86  
  87  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1