/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk

« back to all changes in this revision

Viewing changes to tools.php

  • Committer: Suren A. Chilingaryan
  • Date: 2018-07-15 01:53:01 UTC
  • Revision ID: csa@suren.me-20180715015301-s17qbq19snb3wlr5
Adding generalized data functions to VIEW

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
    }
13
13
}
14
14
 
 
15
function dsMathParseExpression($expression, $args) {
 
16
    static $function_map = array(
 
17
        'floor'     => 'floor',
 
18
        'ceil'      => 'ceil',
 
19
        'round'     => 'round',         
 
20
        'sin'       => 'sin',
 
21
        'cos'       => 'cos',
 
22
        'tan'       => 'tan',           
 
23
        'asin'      => 'asin',
 
24
        'acos'      => 'acos',
 
25
        'atan'      => 'atan',          
 
26
        'abs'       => 'abs',
 
27
        'log'       => 'log',           
 
28
        'pi'        => 'pi',
 
29
        'exp'       => 'exp',
 
30
        'min'       => 'min',
 
31
        'max'       => 'max',
 
32
        'rand'      => 'rand',
 
33
        'fmod'      => 'fmod',
 
34
        'sqrt'      => 'sqrt',
 
35
        'deg2rad'   => 'deg2rad',
 
36
        'rad2deg'   => 'rad2deg',
 
37
        'and'       => '&&',
 
38
        'or'        => '||',
 
39
    );
 
40
    
 
41
    $args = explode(",", $args);
 
42
    foreach ($args as $arg) {
 
43
        if (substr($arg,0,1) == '$')
 
44
            $function_map[substr($arg, 1)] = $arg;
 
45
        else
 
46
            $function_map[$arg] = '$' . $arg;
 
47
    }
 
48
 
 
49
    // Illegal function
 
50
    $expression = preg_replace_callback('~\b[a-z]\w*\b~', function($match) use($function_map) {
 
51
        $function = $match[0];
 
52
        if (!isset($function_map[$function])) {
 
53
            throw new ADEIException(translate("Illegal function '{$match[0]}'"));
 
54
        }
 
55
        return $function_map[$function];
 
56
    }, $expression);
 
57
 
 
58
    // Remove any whitespace
 
59
    $expression = strtolower(preg_replace('~\s+~', '', $expression));
 
60
 
 
61
    // Empty expression
 
62
    if ($expression === '') {
 
63
        throw new ADEIException(translate('Empty expression'));
 
64
    }
 
65
 
 
66
 
 
67
    // Invalid function calls
 
68
    if (preg_match('~(?![$\w])[a-z]\w*(?![\(\w])~', $expression, $match) > 0) {
 
69
        throw new ADEIException(translate("Invalid function call '{$match[0]}'"));
 
70
    }
 
71
 
 
72
    // Legal characters
 
73
    if (preg_match('~[^-+/%*&|<>!=.()0-9a-z,$]~', $expression, $match) > 0) {
 
74
        throw new ADEIException(translate("Illegal character '{$match[0]}'"));
 
75
    }       
 
76
 
 
77
    
 
78
    return "return({$expression});";
 
79
}
 
80
 
 
81
function dsMathCreateFunction($args, $expr) {
 
82
    $expr = dsMathParseExpression($expr, $args);
 
83
    return create_function($args, $expr);
 
84
}
 
85
 
15
86
function dsMathPreciseSubstract($a, $b) {
16
87
    if ($a < $b) return -dsMathPreciseSubstract($b, $a);
17
88