Update admin tool smarty lib
parent
3f0bacd374
commit
a188c6b38a
@ -1,85 +1,109 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Smarty plugin
|
* Smarty plugin
|
||||||
* @package Smarty
|
* This plugin is only for Smarty2 BC
|
||||||
* @subpackage plugins
|
*
|
||||||
|
* @package Smarty
|
||||||
|
* @subpackage PluginsFunction
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Smarty {math} function plugin
|
* Smarty {math} function plugin
|
||||||
*
|
|
||||||
* Type: function<br>
|
* Type: function<br>
|
||||||
* Name: math<br>
|
* Name: math<br>
|
||||||
* Purpose: handle math computations in template<br>
|
* Purpose: handle math computations in template
|
||||||
* @link http://smarty.php.net/manual/en/language.function.math.php {math}
|
*
|
||||||
* (Smarty online manual)
|
* @link http://www.smarty.net/manual/en/language.function.math.php {math}
|
||||||
|
* (Smarty online manual)
|
||||||
* @author Monte Ohrt <monte at ohrt dot com>
|
* @author Monte Ohrt <monte at ohrt dot com>
|
||||||
* @param array
|
*
|
||||||
|
* @param array $params parameters
|
||||||
* @param Smarty
|
* @param Smarty
|
||||||
* @return string
|
*
|
||||||
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
function smarty_function_math($params, &$smarty)
|
function smarty_function_math($params, &$smarty)
|
||||||
{
|
{
|
||||||
|
static $_allowed_funcs =
|
||||||
|
array('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);
|
||||||
// be sure equation parameter is present
|
// be sure equation parameter is present
|
||||||
if (empty($params['equation'])) {
|
if (empty($params[ 'equation' ])) {
|
||||||
$smarty->trigger_error("math: missing equation parameter");
|
trigger_error("math: missing equation parameter", E_USER_WARNING);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip out backticks, not necessary for math
|
$equation = $params[ 'equation' ];
|
||||||
$equation = str_replace('`','',$params['equation']);
|
|
||||||
|
|
||||||
// make sure parenthesis are balanced
|
// make sure parenthesis are balanced
|
||||||
if (substr_count($equation,"(") != substr_count($equation,")")) {
|
if (substr_count($equation, "(") != substr_count($equation, ")")) {
|
||||||
$smarty->trigger_error("math: unbalanced parenthesis");
|
trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// match all vars in equation, make sure all are passed
|
// disallow backticks
|
||||||
preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match);
|
if (strpos($equation, '`') !== false) {
|
||||||
$allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
|
trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
|
||||||
'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
|
|
||||||
|
return;
|
||||||
foreach($match[1] as $curr_var) {
|
}
|
||||||
if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
|
|
||||||
$smarty->trigger_error("math: function call $curr_var not allowed");
|
// also disallow dollar signs
|
||||||
return;
|
if (strpos($equation, '$') !== false) {
|
||||||
}
|
trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
if ($key != "equation" && $key != "format" && $key != "assign") {
|
if ($key != "equation" && $key != "format" && $key != "assign") {
|
||||||
// make sure value is not empty
|
// make sure value is not empty
|
||||||
if (strlen($val)==0) {
|
if (strlen($val) == 0) {
|
||||||
$smarty->trigger_error("math: parameter $key is empty");
|
trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!is_numeric($val)) {
|
if (!is_numeric($val)) {
|
||||||
$smarty->trigger_error("math: parameter $key: is not numeric");
|
trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eval("\$smarty_math_result = ".$equation.";");
|
// match all vars in equation, make sure all are passed
|
||||||
|
preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
|
||||||
|
|
||||||
|
foreach ($match[ 1 ] as $curr_var) {
|
||||||
|
if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
|
||||||
|
trigger_error("math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'", E_USER_WARNING);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($params as $key => $val) {
|
||||||
|
if ($key != "equation" && $key != "format" && $key != "assign") {
|
||||||
|
$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[ 'format' ])) {
|
||||||
if (empty($params['assign'])) {
|
if (empty($params[ 'assign' ])) {
|
||||||
return $smarty_math_result;
|
return $smarty_math_result;
|
||||||
} else {
|
} else {
|
||||||
$smarty->assign($params['assign'],$smarty_math_result);
|
$smarty->assign($params[ 'assign' ], $smarty_math_result);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (empty($params['assign'])){
|
if (empty($params[ 'assign' ])) {
|
||||||
printf($params['format'],$smarty_math_result);
|
printf($params[ 'format' ], $smarty_math_result);
|
||||||
} else {
|
} else {
|
||||||
$smarty->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
|
$smarty->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vim: set expandtab: */
|
|
||||||
|
|
||||||
?>
|
|
Loading…
Reference in New Issue