Capoeirista

Untitled

May 22nd, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * This plugin is only for Smarty2 BC
  5.  *
  6.  * @package    Smarty
  7.  * @subpackage PluginsFunction
  8.  */
  9.  
  10. /**
  11.  * Smarty {math} function plugin
  12.  * Type:     function<br>
  13.  * Name:     math<br>
  14.  * Purpose:  handle math computations in template
  15.  *
  16.  * @link     http://www.smarty.net/manual/en/language.function.math.php {math}
  17.  *           (Smarty online manual)
  18.  * @author   Monte Ohrt <monte at ohrt dot com>
  19.  *
  20.  * @param array                    $params   parameters
  21.  * @param Smarty
  22.  *
  23.  * @return string|null
  24.  */
  25. function smarty_function_math($params, &$smarty)
  26. {
  27.     static $_allowed_funcs =
  28.         array('int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
  29.               'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true, 'rand' => true,
  30.               'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true);
  31.     // be sure equation parameter is present
  32.     if (empty($params[ 'equation' ])) {
  33.         trigger_error("math: missing equation parameter", E_USER_WARNING);
  34.  
  35.         return;
  36.     }
  37.  
  38.     $equation = $params[ 'equation' ];
  39.  
  40.     // make sure parenthesis are balanced
  41.     if (substr_count($equation, "(") != substr_count($equation, ")")) {
  42.         trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
  43.  
  44.         return;
  45.     }
  46.  
  47.     // disallow backticks
  48.     if (strpos($equation, '`') !== false) {
  49.         trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
  50.  
  51.         return;
  52.     }
  53.  
  54.     // also disallow dollar signs
  55.     if (strpos($equation, '$') !== false) {
  56.         trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
  57.  
  58.         return;
  59.     }
  60.  
  61.     foreach ($params as $key => $val) {
  62.         if ($key != "equation" && $key != "format" && $key != "assign") {
  63.             // make sure value is not empty
  64.             if (strlen($val) == 0) {
  65.                 trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
  66.  
  67.                 return;
  68.             }
  69.             if (!is_numeric($val)) {
  70.                 trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
  71.  
  72.                 return;
  73.             }
  74.         }
  75.     }
  76.  
  77.     // match all vars in equation, make sure all are passed
  78.     preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
  79.  
  80.     foreach ($match[ 1 ] as $curr_var) {
  81.         if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
  82.             trigger_error("math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'", E_USER_WARNING);
  83.  
  84.             return;
  85.         }
  86.     }
  87.  
  88.     foreach ($params as $key => $val) {
  89.         if ($key != "equation" && $key != "format" && $key != "assign") {
  90.             $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  91.         }
  92.     }
  93.     $smarty_math_result = null;
  94.     eval("\$smarty_math_result = " . $equation . ";");
  95.  
  96.     if (empty($params[ 'format' ])) {
  97.         if (empty($params[ 'assign' ])) {
  98.             return $smarty_math_result;
  99.         } else {
  100.             $smarty->assign($params[ 'assign' ], $smarty_math_result);
  101.         }
  102.     } else {
  103.         if (empty($params[ 'assign' ])) {
  104.             printf($params[ 'format' ], $smarty_math_result);
  105.         } else {
  106.             $smarty->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));
  107.         }
  108.     }
  109. }
Add Comment
Please, Sign In to add comment