/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
<?php
2
function GetTmpFile($prefix, $ext = false) {
3
    return tempnam(sys_get_temp_dir(), $prefix) . ($ext?".$ext":"");
4
}
5
6
function dsPrintSelectOptions($config, $selected=false) {
7
    foreach ($config as $opt => $value) {
8
	if (($selected)&&(strcmp($value,$selected))) $selected = " selected=\"1\"";
9
	else $selected = "";
10
	
11
	print "<option value=\"$value\"$selected>$opt</option>";
12
    }
13
}
14
516 by Suren A. Chilingaryan
Adding generalized data functions to VIEW
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
1 by Suren A. Chilingaryan
Initial import
86
function dsMathPreciseSubstract($a, $b) {
199 by Suren A. Chilingaryan
Correctly show legend on negative timestamps
87
    if ($a < $b) return -dsMathPreciseSubstract($b, $a);
88
    
1 by Suren A. Chilingaryan
Initial import
89
    $pos = strpos($a, ".");
90
    if ($pos === false) {
91
	$ra = 0;
92
	$ia = (int)$a;
93
    } else {
94
        $ia = (int)floor($a);
95
96
	if (is_float($a)) $ra = $a - $ia;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
97
	else if ($ia < 0) $ra = 1 - ("0." . substr($a, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
98
	else $ra = "0." . substr($a, $pos + 1);
99
    }
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
100
    
1 by Suren A. Chilingaryan
Initial import
101
    $pos = strpos($b, ".");
102
    if ($pos === false) {
103
	$rb = 0;
104
	$ib = (int)$b;
105
    } else {
106
        $ib = (int)floor($b);
107
108
	if (is_float($b)) $rb = $b - $ib;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
109
	else if ($ib < 0) $rb = 1 - ("0." . substr($b, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
110
	else $rb = "0." . substr($b, $pos + 1);
111
    }
112
113
    if (($ra)||($rb)) {
114
	$r = $ra - $rb;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
115
116
#	if ($a < 0) echo "$a,$b   $ia,$ib,    $ra,$rb   = $r\n";
117
118
	if ($r < 0) {
350 by Suren A. Chilingaryan
Increase precision of dsPrecise math operations to 24 numbers after decimal point
119
	    if ($ia > $ib) return ($ia - $ib - 1) . strstr(sprintf("%.24F", ($r+1)), ".");
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
120
	    else return 0;
121
	} else if ($r > 0) {
122
	    if ($ia < $ib) return 0;
350 by Suren A. Chilingaryan
Increase precision of dsPrecise math operations to 24 numbers after decimal point
123
	    else return ($ia - $ib) . strstr(sprintf("%.24F", $r), ".");
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
124
	} else {
125
	    if ($ia > $ib) return ($ia - $ib);
126
	    else return 0;
127
	}
1 by Suren A. Chilingaryan
Initial import
128
    } else return $ia - $ib;
129
}
130
131
function dsMathPreciseAdd($a, $b) {
132
    $pos = strpos($a, ".");
133
    if ($pos === false) {
134
	$ra = 0;
135
	$ia = (int)$a;
136
    } else {
137
        $ia = (int)floor($a);
138
139
	if (is_float($a)) $ra = $a - $ia;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
140
	else if ($ia < 0) $ra = 1 - ("0." . substr($a, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
141
	else $ra = "0." . substr($a, $pos + 1);
142
    }
143
144
    $pos = strpos($b, ".");
145
    if ($pos === false) {
146
	$rb = 0;
147
	$ib = (int)$b;
148
    } else {
149
        $ib = (int)floor($b);
150
151
	if (is_float($b)) $rb = $b - $ib;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
152
	else if ($ib < 0) $rb = 1 - ("0." . substr($b, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
153
	else $rb = "0." . substr($b, $pos + 1);
154
    }
155
156
    if (($ra)||($rb)) {
157
	$r = $ra + $rb;
350 by Suren A. Chilingaryan
Increase precision of dsPrecise math operations to 24 numbers after decimal point
158
	if ($r > 1) return ($ia + $ib + 1) . strstr(sprintf("%.24F", $r), ".");
159
	else if ($r < 1) return ($ia + $ib) . strstr(sprintf("%.24F", $r), ".");
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
160
	else  return ($ia + $ib + 1);
1 by Suren A. Chilingaryan
Initial import
161
    } else return $ia + $ib;
162
}
163
164
function dsMathPreciseCompare($a, $b) {
165
    $pos = strpos($a, ".");
166
    if ($pos === false) {
167
	$ra = 0;
168
	$ia = (int)$a;
169
    } else {
170
        $ia = (int)floor($a);
171
172
	if (is_float($a)) $ra = $a - $ia;
173
	else $ra = "0." . substr($a, $pos + 1);
174
    }
175
176
    $pos = strpos($b, ".");
177
    if ($pos === false) {
178
	$rb = 0;
179
	$ib = (int)$b;
180
    } else {
181
        $ib = (int)floor($b);
182
183
	if (is_float($b)) $rb = $b - $ib;
184
	else $rb = "0." . substr($b, $pos + 1);
185
    }
186
187
    if (($ra)||($rb)) {
188
	if ($ia > $ib) return 1;
189
	if ($ia < $ib) return -1;
190
	return ($ra == $rb)?0:(($ra<$rb)?-1:1);
191
    } 
192
    return ($ia == $ib)?0:(($ia<$ib)?-1:1);
193
}
194
195
23 by Suren A. Chilingaryan
Administrative interface and better handling of missing group channels
196
function dsPrintSize($size) {
197
    $lvl = 0;
198
199
    while (($lvl<4)&&($size > 5119)) {
200
	$lvl++;
201
	$size = (int)ceil($size / 1024.);
202
    }
203
    
204
    switch ($lvl) {
205
	case 0:
206
	    $size .= " bytes";
207
	    break;
208
	case 1:
209
	    $size .= " KB";
210
	    break;
211
	case 2:
212
	    $size .= " MB";
213
	    break;
214
	case 3:
215
	    $size .= " GB";
216
	    break;
217
	case 4:
218
	    $size .= " TB";
219
	    break;
220
    }
221
    
222
    return $size;
223
}
224
225
1 by Suren A. Chilingaryan
Initial import
226
function xml_escape($message) {
58 by Suren A. Chilingaryan
Bringing back double click support
227
	/* HTMLSpecialChars will return empty string if non-unicode message
228
	is passed. */
229
    $msg = htmlspecialchars($message, ENT_COMPAT, "UTF-8");
230
    if ($msg) return $msg;
509 by Suren A. Chilingaryan
Just skip invalid characters in xml_escape if wrong encoding is configured
231
    return htmlspecialchars($message, ENT_COMPAT|ENT_SUBSTITUTE);
1 by Suren A. Chilingaryan
Initial import
232
}
233
234
function translate($string) {
235
    $arg = array();
236
    for($i = 1 ; $i < func_num_args(); $i++)
237
	$arg[] = func_get_arg($i);
238
239
    return vsprintf(gettext($string), $arg);
240
}
241
16 by Suren A. Chilingaryan
Tracing and monitoring timing information
242
function log_message($message) {
243
    echo "$message\n";
244
}
245
1 by Suren A. Chilingaryan
Initial import
246
?>