PEEL Shopping
Open source ecommerce : PEEL Shopping
function.math.php
Go to the documentation of this file.
1 <?php
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 ?>
smarty_function_math($params, $template)
Smarty {math} function plugin.
$match
Definition: search.php:86

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:16 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.