2012-07-04 07:23:08 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Smarty plugin
|
|
|
|
* This plugin is only for Smarty2 BC
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
|
|
|
* @package Smarty
|
2012-07-04 07:23:08 +02:00
|
|
|
* @subpackage PluginsFunction
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty {math} function plugin
|
|
|
|
* Type: function<br>
|
|
|
|
* Name: math<br>
|
|
|
|
* Purpose: handle math computations in template
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @link http://www.smarty.net/manual/en/language.function.math.php {math}
|
|
|
|
* (Smarty online manual)
|
2012-07-04 07:23:08 +02:00
|
|
|
* @author Monte Ohrt <monte at ohrt dot com>
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @param array $params parameters
|
|
|
|
* @param Smarty_Internal_Template $template template object
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
function smarty_function_math($params, $template)
|
|
|
|
{
|
|
|
|
static $_allowed_funcs = array(
|
2014-09-07 13:38:28 +02:00
|
|
|
'int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
|
|
|
|
'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true,
|
|
|
|
'rand' => true, 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true
|
2012-07-04 07:23:08 +02:00
|
|
|
);
|
|
|
|
// be sure equation parameter is present
|
|
|
|
if (empty($params['equation'])) {
|
2014-09-07 13:38:28 +02:00
|
|
|
trigger_error("math: missing equation parameter", E_USER_WARNING);
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$equation = $params['equation'];
|
|
|
|
|
|
|
|
// make sure parenthesis are balanced
|
2014-09-07 13:38:28 +02:00
|
|
|
if (substr_count($equation, "(") != substr_count($equation, ")")) {
|
|
|
|
trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// match all vars in equation, make sure all are passed
|
2014-09-07 13:38:28 +02:00
|
|
|
preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!", $equation, $match);
|
2012-07-04 07:23:08 +02:00
|
|
|
|
2014-09-07 13:38:28 +02:00
|
|
|
foreach ($match[1] as $curr_var) {
|
2012-07-04 07:23:08 +02:00
|
|
|
if ($curr_var && !isset($params[$curr_var]) && !isset($_allowed_funcs[$curr_var])) {
|
2014-09-07 13:38:28 +02:00
|
|
|
trigger_error("math: function call $curr_var not allowed", E_USER_WARNING);
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 13:38:28 +02:00
|
|
|
foreach ($params as $key => $val) {
|
2012-07-04 07:23:08 +02:00
|
|
|
if ($key != "equation" && $key != "format" && $key != "assign") {
|
|
|
|
// make sure value is not empty
|
2014-09-07 13:38:28 +02:00
|
|
|
if (strlen($val) == 0) {
|
|
|
|
trigger_error("math: parameter $key is empty", E_USER_WARNING);
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!is_numeric($val)) {
|
2014-09-07 13:38:28 +02:00
|
|
|
trigger_error("math: parameter $key: is not numeric", E_USER_WARNING);
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$smarty_math_result = null;
|
2014-09-07 13:38:28 +02:00
|
|
|
eval("\$smarty_math_result = " . $equation . ";");
|
2012-07-04 07:23:08 +02:00
|
|
|
|
|
|
|
if (empty($params['format'])) {
|
|
|
|
if (empty($params['assign'])) {
|
|
|
|
return $smarty_math_result;
|
|
|
|
} else {
|
2014-09-07 13:38:28 +02:00
|
|
|
$template->assign($params['assign'], $smarty_math_result);
|
2012-07-04 07:23:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-07 13:38:28 +02:00
|
|
|
if (empty($params['assign'])) {
|
|
|
|
printf($params['format'], $smarty_math_result);
|
2012-07-04 07:23:08 +02:00
|
|
|
} else {
|
2014-09-07 13:38:28 +02:00
|
|
|
$template->assign($params['assign'], sprintf($params['format'], $smarty_math_result));
|
2012-07-04 07:23:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|