Blame view

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