1 | <?php |
---|
2 | /** |
---|
3 | * Base utility class |
---|
4 | * @author Carlos Eduardo Nogueira Goncalves |
---|
5 | * @author Marcos Pont |
---|
6 | * @version 1.0 |
---|
7 | * @link http://workflow.celepar.parana/doc-workflow/classes/utils Complete reference |
---|
8 | * @package Workflow |
---|
9 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
10 | */ |
---|
11 | class Utils |
---|
12 | { |
---|
13 | /** |
---|
14 | * Include File |
---|
15 | * @param string $filePath |
---|
16 | * @param bool $return |
---|
17 | * @access public |
---|
18 | * @return bool |
---|
19 | */ |
---|
20 | function includeFile($filePath, $return=FALSE) |
---|
21 | { |
---|
22 | if ($return === TRUE) { |
---|
23 | return (include($filePath)); |
---|
24 | } |
---|
25 | else |
---|
26 | { |
---|
27 | if (!@include($filePath)) |
---|
28 | { |
---|
29 | return FALSE; |
---|
30 | } |
---|
31 | else |
---|
32 | { |
---|
33 | return TRUE; |
---|
34 | } |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Println |
---|
40 | * @param $str |
---|
41 | * @param $nl |
---|
42 | * @access public |
---|
43 | * @return void |
---|
44 | */ |
---|
45 | function println($str, $nl='<br>') { |
---|
46 | echo $str . $nl; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Dump the $var Variable on screen |
---|
51 | * @param mixed $var |
---|
52 | */ |
---|
53 | function dumpVariable($var) { |
---|
54 | print '<pre>'; |
---|
55 | var_dump($var); |
---|
56 | print '</pre>'; |
---|
57 | } |
---|
58 | /** |
---|
59 | * DumpArray |
---|
60 | * @param $arr |
---|
61 | * @param $stringLimit |
---|
62 | * @param deep |
---|
63 | */ |
---|
64 | function dumpArray($arr, $return=TRUE, $stringLimit=200, $deep=FALSE) { |
---|
65 | $r = array(); |
---|
66 | foreach ($arr as $k => $v) { |
---|
67 | if (is_string($v)) { |
---|
68 | $r[] = $k . "=>'" . (strlen($v) > $stringLimit ? substr($v, 0, $stringLimit) . "...(" . strlen($v) . ")" : $v) . "'"; |
---|
69 | } elseif ($deep && (is_array($v) || is_object($v))) { |
---|
70 | (is_object($v)) && ($v = get_object_vars($v)); |
---|
71 | $r[] = $k . '=>' . dumpArray($v, TRUE, $stringLimit, TRUE); |
---|
72 | } else { |
---|
73 | $r[] = $k . '=>' . $v; |
---|
74 | } |
---|
75 | } |
---|
76 | if ($return) |
---|
77 | return "[" . implode(", ", $r) . "]"; |
---|
78 | print "[" . implode(", ", $r) . "]"; |
---|
79 | return TRUE; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Export Variable |
---|
84 | * @param $var |
---|
85 | * @param bool $formatted |
---|
86 | * @access public |
---|
87 | * @return string |
---|
88 | */ |
---|
89 | function exportVariable($var, $formatted=FALSE) { |
---|
90 | if (is_object($var) && !$this->isPHP(5) && method_exists($var, '__tostring')) |
---|
91 | $export = $var->__toString(); |
---|
92 | else |
---|
93 | $export = var_export($var, TRUE); |
---|
94 | if ($formatted) |
---|
95 | return '<pre>' . $export . '</pre>'; |
---|
96 | else |
---|
97 | return $export; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * |
---|
103 | * @param $var |
---|
104 | * @param bool $formatted |
---|
105 | * @access public |
---|
106 | * @return string |
---|
107 | */ |
---|
108 | function consumeArray(&$array, $key) { |
---|
109 | if (is_array($array)) { |
---|
110 | if (array_key_exists($key, $array)) { |
---|
111 | $return = $array[$key]; |
---|
112 | unset($array[$key]); |
---|
113 | return $return; |
---|
114 | } |
---|
115 | } |
---|
116 | return NULL; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Find Array path |
---|
121 | * @param array $arr |
---|
122 | * @param string $formatted |
---|
123 | * @param string $separator |
---|
124 | * @param string $fallback |
---|
125 | * @access public |
---|
126 | * @return string |
---|
127 | */ |
---|
128 | function findArrayPath($arr, $path, $separator='.', $fallback=NULL) { |
---|
129 | if (!is_array($arr)) |
---|
130 | return $fallback; |
---|
131 | $parts = explode($separator, $path); |
---|
132 | if (sizeof($parts) == 1) { |
---|
133 | return (isset($arr[$path]) ? $arr[$path] : $fallback); |
---|
134 | } else { |
---|
135 | $i = 0; |
---|
136 | $base = $arr; |
---|
137 | $size = sizeof($parts); |
---|
138 | while ($i < $size) { |
---|
139 | if (!isset($base[$parts[$i]])) |
---|
140 | return $fallback; |
---|
141 | else |
---|
142 | $base = $base[$parts[$i]]; |
---|
143 | if ($i < ($size-1) && !is_array($base)) |
---|
144 | return $fallback; |
---|
145 | $i++; |
---|
146 | } |
---|
147 | return $base; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * isPHP |
---|
153 | * @param int $num |
---|
154 | * @access public |
---|
155 | * @return float |
---|
156 | */ |
---|
157 | function isPHP($num=5) { |
---|
158 | return (floatval(PHP_VERSION) >= $num); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * uri |
---|
163 | * @param uri |
---|
164 | * @bool |
---|
165 | * |
---|
166 | * @access public |
---|
167 | * @return string |
---|
168 | */ |
---|
169 | function uri($full=TRUE) { |
---|
170 | if ($full) { |
---|
171 | $protocol = $this->protocol(); |
---|
172 | $port = $this->get('SERVER_PORT'); |
---|
173 | if (($protocol == 'http' && $port != '80') || ($protocol == 'https' && $port != '443')) |
---|
174 | $base = "{$protocol}://" . $this->serverName() . ":{$port}"; |
---|
175 | else |
---|
176 | $base = "{$protocol}://" . $this->serverName(); |
---|
177 | } else { |
---|
178 | $base = ''; |
---|
179 | } |
---|
180 | if (!$requestUri = $this->get('REQUEST_URI')) { |
---|
181 | if ($queryString = $this->get('QUERY_STRING')) |
---|
182 | return $base . $this->scriptName() . '?' . $queryString; |
---|
183 | else |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | $requestUri = preg_replace('#[?|&]' . session_name() . '=[^&|?]*#', '', $requestUri); |
---|
187 | return $base . $requestUri; |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | /** |
---|
192 | * Return the protocol |
---|
193 | * @return string if isSecure returns true https else return http |
---|
194 | * @access public |
---|
195 | */ |
---|
196 | function protocol() { |
---|
197 | if ($this->isSecure()) |
---|
198 | return 'https'; |
---|
199 | else |
---|
200 | return 'http'; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Return if secure connection is required |
---|
205 | * @return bool https required or not |
---|
206 | * @access public |
---|
207 | */ |
---|
208 | function isSecure() { |
---|
209 | return (strtolower($this->get('HTTPS')) == 'on' || $this->has('SSL_PROTOCOL_VERSION')); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Return the script name |
---|
214 | * @return string script name |
---|
215 | * @access public |
---|
216 | */ |
---|
217 | function scriptName() { |
---|
218 | return $this->get('SCRIPT_NAME'); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Return the server name |
---|
223 | * @return string server Name |
---|
224 | * @access public |
---|
225 | */ |
---|
226 | function serverName() { |
---|
227 | return $this->get('SERVER_NAME'); |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * Return Environment variable |
---|
232 | * @return enviroment variable |
---|
233 | * @access public |
---|
234 | */ |
---|
235 | function get($key) { |
---|
236 | if (isset($_SERVER[$key])) { |
---|
237 | return $_SERVER[$key]; |
---|
238 | } |
---|
239 | if (@getenv($key)) |
---|
240 | return getenv($key); |
---|
241 | else |
---|
242 | return NULL; |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Check if the value of variable is NULL |
---|
247 | * @param mixed $value value |
---|
248 | * @param bool $strict strict true compares the type and value |
---|
249 | * @access public |
---|
250 | */ |
---|
251 | function isNull($value, $strict = FALSE) { |
---|
252 | return ($strict) ? (NULL === $value) : (NULL == $value); |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Get the string value of variable |
---|
257 | * @param mixed $value |
---|
258 | * @access public |
---|
259 | * @return string |
---|
260 | */ |
---|
261 | function parseString($value) { |
---|
262 | return strval($value); |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * Checks if $value is false |
---|
267 | * @param mixed $value |
---|
268 | * @return bool true if is a boolean and false |
---|
269 | */ |
---|
270 | function isFalse($value) { |
---|
271 | return ($value === FALSE); |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * Checks if $value is false |
---|
276 | * @param string $str |
---|
277 | * @param $chars |
---|
278 | * @return string |
---|
279 | * @access public |
---|
280 | * |
---|
281 | */ |
---|
282 | function left($str, $chars = 0) { |
---|
283 | if (!$this->isInteger($chars)) { |
---|
284 | return $str; |
---|
285 | } else if ($chars == 0) { |
---|
286 | return ''; |
---|
287 | } else { |
---|
288 | return substr($str, 0, $chars); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Checks if $value is a integer |
---|
294 | * @param string $value |
---|
295 | * @param $chars |
---|
296 | * @return string |
---|
297 | * @access public |
---|
298 | */ |
---|
299 | function isInteger(&$value, $strict=FALSE) { |
---|
300 | $exp = "/^\-?[0-9]+$/"; |
---|
301 | if (preg_match($exp, $value)) { |
---|
302 | if (!$strict && !is_int($value)) { |
---|
303 | $value = $this->parseInteger($value); |
---|
304 | } |
---|
305 | return TRUE; |
---|
306 | } else { |
---|
307 | return FALSE; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * Get the integer value of variable |
---|
313 | * @param $value integer value |
---|
314 | * @access public |
---|
315 | */ |
---|
316 | function parseInteger($value) { |
---|
317 | return intval($value); |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * Return the positive integer value of variable |
---|
322 | * @param mixed $value |
---|
323 | * @return int |
---|
324 | * @access public |
---|
325 | */ |
---|
326 | function parseIntegerPositive($value) { |
---|
327 | return abs(intval($value)); |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Returns a random number |
---|
332 | * @param int $rangeMin |
---|
333 | * @param int $rangeMax |
---|
334 | * @return string |
---|
335 | * @access public |
---|
336 | */ |
---|
337 | function randomize($rangeMin, $rangeMax) { |
---|
338 | if ($rangeMax > $rangeMin && is_numeric($rangeMin) && is_numeric($rangeMax)) { |
---|
339 | return rand($rangeMin, $rangeMax); |
---|
340 | } else { |
---|
341 | return NULL; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * Return a $default value if the first parameter is NULL else returns the first parameter |
---|
347 | * |
---|
348 | * @param mixed $value first parameter |
---|
349 | * @param mixed $default default value to return if $value == NULL |
---|
350 | * @access public |
---|
351 | * @return mixed |
---|
352 | */ |
---|
353 | |
---|
354 | function ifNull($value, $default = NULL) { |
---|
355 | if ($value === NULL) |
---|
356 | return $default; |
---|
357 | return $value; |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | * Return the javascript code for a anchor |
---|
362 | * |
---|
363 | * @param string $url base url |
---|
364 | * @param string $text |
---|
365 | * @param string $statusBarText |
---|
366 | * @param string $cssClass |
---|
367 | * @param array $jsEvents |
---|
368 | * @param string $target |
---|
369 | * @param string $name |
---|
370 | * @param string $id |
---|
371 | * @param string $rel |
---|
372 | * @param string $accessKey |
---|
373 | * |
---|
374 | * @param array $jsEvents |
---|
375 | * |
---|
376 | * @return string javascript code |
---|
377 | * @access public |
---|
378 | */ |
---|
379 | function anchor($url, $text, $statusBarText='', $cssClass='', $jsEvents=array(), $target='', $name='', $id='', $rel='', $accessKey='') |
---|
380 | { |
---|
381 | if (empty($url)) |
---|
382 | $url = "javascript:void(0);"; |
---|
383 | $scriptStr = ''; |
---|
384 | if (!empty($jsEvents) && $statusBarText != "") { |
---|
385 | $jsEvents['onMouseOver'] = (isset($jsEvents['onMouseOver']) ? $jsEvents['onMouseOver'] . "window.status='$statusBarText';return true;" : "window.status='$statusBarText';return true;"); |
---|
386 | $jsEvents['onMouseOut'] = (isset($jsEvents['onMouseOut']) ? $jsEvents['onMouseOut'] . "window.status='';return true;" : "window.status='';return true;"); |
---|
387 | } else if ($statusBarText) { |
---|
388 | $scriptStr .= "onMouseOver=\"window.status='$statusBarText';return true;\" onMouseOut=\"window.status='';return true;\""; |
---|
389 | } |
---|
390 | foreach ($jsEvents as $event => $action) |
---|
391 | $scriptStr .= " $event=\"" . preg_replace("/\"/", "'", $action) . "\""; |
---|
392 | return sprintf("<a href=\"%s\"%s%s%s%s%s%s%s%s>%s</a>", htmlentities($url), |
---|
393 | (!empty($name) ? " name=\"{$name}\"" : ""), |
---|
394 | (!empty($id) ? " id=\"{$id}\"" : ""), |
---|
395 | (!empty($rel) ? " rel=\"{$rel}\"" : ""), |
---|
396 | (!empty($accessKey) ? " accesskey=\"{$accessKey}\"" : ""), |
---|
397 | (!empty($target) ? " target=\"{$target}\"" : ""), |
---|
398 | (!empty($cssClass) ? " class=\"{$cssClass}\"" : ""), |
---|
399 | (!empty($statusBarText) ? " title=\"{$statusBarText}\"" : ""), |
---|
400 | (!empty($scriptStr) ? " {$scriptStr}" : ""), |
---|
401 | $text); |
---|
402 | } |
---|
403 | |
---|
404 | /** |
---|
405 | * Return a error message |
---|
406 | * @param string $msg |
---|
407 | * @param string $file |
---|
408 | * @param string $line |
---|
409 | * |
---|
410 | * @return void |
---|
411 | * @access public |
---|
412 | */ |
---|
413 | function raiseError($msg = 'Erro', $file = __FILE__, $line = __LINE__) |
---|
414 | { |
---|
415 | ini_set('display_errors', true); |
---|
416 | error_reporting(E_ALL); |
---|
417 | trigger_error("$file ($line): $msg", E_USER_ERROR); |
---|
418 | } |
---|
419 | } |
---|
420 | ?> |
---|