Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {math} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     math
Chris PeBenito 696b41
 * Purpose:  handle math computations in template
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.math.php {math}
Chris PeBenito 696b41
 *          (Smarty online manual)
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_math($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    // be sure equation parameter is present
Chris PeBenito 696b41
    if (empty($params['equation'])) {
Chris PeBenito 696b41
        $smarty->trigger_error("math: missing equation parameter");
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $equation = $params['equation'];
Chris PeBenito 696b41
Chris PeBenito 696b41
    // make sure parenthesis are balanced
Chris PeBenito 696b41
    if (substr_count($equation,"(") != substr_count($equation,")")) {
Chris PeBenito 696b41
        $smarty->trigger_error("math: unbalanced parenthesis");
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    // match all vars in equation, make sure all are passed
Chris PeBenito 696b41
    preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]+)!",$equation, $match);
Chris PeBenito 696b41
    $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
Chris PeBenito 696b41
                           'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    foreach($match[1] as $curr_var) {
Chris PeBenito 696b41
        if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
Chris PeBenito 696b41
            $smarty->trigger_error("math: function call $curr_var not allowed");
Chris PeBenito 696b41
            return;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    foreach($params as $key => $val) {
Chris PeBenito 696b41
        if ($key != "equation" && $key != "format" && $key != "assign") {
Chris PeBenito 696b41
            // make sure value is not empty
Chris PeBenito 696b41
            if (strlen($val)==0) {
Chris PeBenito 696b41
                $smarty->trigger_error("math: parameter $key is empty");
Chris PeBenito 696b41
                return;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            if (!is_numeric($val)) {
Chris PeBenito 696b41
                $smarty->trigger_error("math: parameter $key: is not numeric");
Chris PeBenito 696b41
                return;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $equation = preg_replace("/\b$key\b/",$val, $equation);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    eval("\$smarty_math_result = ".$equation.";");
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (empty($params['format'])) {
Chris PeBenito 696b41
        if (empty($params['assign'])) {
Chris PeBenito 696b41
            return $smarty_math_result;
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $smarty->assign($params['assign'],$smarty_math_result);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        if (empty($params['assign'])){
Chris PeBenito 696b41
            printf($params['format'],$smarty_math_result);
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $smarty->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>