1 | <?php |
---|
2 | /** |
---|
3 | * Basic Cake functionality. |
---|
4 | * |
---|
5 | * Core functions for including other source files, loading models and so forth. |
---|
6 | * |
---|
7 | * PHP versions 4 and 5 |
---|
8 | * |
---|
9 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
10 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
11 | * |
---|
12 | * Licensed under The MIT License |
---|
13 | * Redistributions of files must retain the above copyright notice. |
---|
14 | * |
---|
15 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
16 | * @link http://cakephp.org CakePHP(tm) Project |
---|
17 | * @package cake |
---|
18 | * @subpackage cake.cake |
---|
19 | * @since CakePHP(tm) v 0.2.9 |
---|
20 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * Basic defines for timing functions. |
---|
25 | */ |
---|
26 | define('SECOND', 1); |
---|
27 | define('MINUTE', 60); |
---|
28 | define('HOUR', 3600); |
---|
29 | define('DAY', 86400); |
---|
30 | define('WEEK', 604800); |
---|
31 | define('MONTH', 2592000); |
---|
32 | define('YEAR', 31536000); |
---|
33 | |
---|
34 | /** |
---|
35 | * Patch old versions of PHP4. |
---|
36 | */ |
---|
37 | if (!defined('PHP_EOL')) { |
---|
38 | switch (strtoupper(substr(PHP_OS, 0, 3))) { |
---|
39 | case 'WIN': |
---|
40 | define('PHP_EOL', "\r\n"); |
---|
41 | break; |
---|
42 | default: |
---|
43 | define('PHP_EOL', "\n"); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Patch PHP4 and PHP5.0 |
---|
49 | */ |
---|
50 | if (!defined('DATE_RFC2822')) { |
---|
51 | define('DATE_RFC2822', 'D, d M Y H:i:s O'); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Patch for PHP < 5.0 |
---|
56 | */ |
---|
57 | if (!function_exists('clone')) { |
---|
58 | if (version_compare(PHP_VERSION, '5.0') < 0) { |
---|
59 | eval (' |
---|
60 | function clone($object) |
---|
61 | { |
---|
62 | return $object; |
---|
63 | }'); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Loads configuration files. Receives a set of configuration files |
---|
69 | * to load. |
---|
70 | * Example: |
---|
71 | * |
---|
72 | * `config('config1', 'config2');` |
---|
73 | * |
---|
74 | * @return boolean Success |
---|
75 | * @link http://book.cakephp.org/view/1125/config |
---|
76 | */ |
---|
77 | function config() { |
---|
78 | $args = func_get_args(); |
---|
79 | foreach ($args as $arg) { |
---|
80 | if ($arg === 'database' && file_exists(CONFIGS . 'database.php')) { |
---|
81 | include_once(CONFIGS . $arg . '.php'); |
---|
82 | } elseif (file_exists(CONFIGS . $arg . '.php')) { |
---|
83 | include_once(CONFIGS . $arg . '.php'); |
---|
84 | |
---|
85 | if (count($args) == 1) { |
---|
86 | return true; |
---|
87 | } |
---|
88 | } else { |
---|
89 | if (count($args) == 1) { |
---|
90 | return false; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | return true; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Loads component/components from LIBS. Takes optional number of parameters. |
---|
99 | * |
---|
100 | * Example: |
---|
101 | * |
---|
102 | * `uses('flay', 'time');` |
---|
103 | * |
---|
104 | * @param string $name Filename without the .php part |
---|
105 | * @deprecated Will be removed in 2.0 |
---|
106 | * @link http://book.cakephp.org/view/1140/uses |
---|
107 | */ |
---|
108 | function uses() { |
---|
109 | $args = func_get_args(); |
---|
110 | foreach ($args as $file) { |
---|
111 | require_once(LIBS . strtolower($file) . '.php'); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Prints out debug information about given variable. |
---|
117 | * |
---|
118 | * Only runs if debug level is greater than zero. |
---|
119 | * |
---|
120 | * @param boolean $var Variable to show debug information for. |
---|
121 | * @param boolean $showHtml If set to true, the method prints the debug data in a screen-friendly way. |
---|
122 | * @param boolean $showFrom If set to true, the method prints from where the function was called. |
---|
123 | * @link http://book.cakephp.org/view/1190/Basic-Debugging |
---|
124 | * @link http://book.cakephp.org/view/1128/debug |
---|
125 | */ |
---|
126 | function debug($var = false, $showHtml = false, $showFrom = true) { |
---|
127 | if (Configure::read() > 0) { |
---|
128 | if ($showFrom) { |
---|
129 | $calledFrom = debug_backtrace(); |
---|
130 | echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>'; |
---|
131 | echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)'; |
---|
132 | } |
---|
133 | echo "\n<pre class=\"cake-debug\">\n"; |
---|
134 | |
---|
135 | $var = print_r($var, true); |
---|
136 | if ($showHtml) { |
---|
137 | $var = str_replace('<', '<', str_replace('>', '>', $var)); |
---|
138 | } |
---|
139 | echo $var . "\n</pre>\n"; |
---|
140 | } |
---|
141 | } |
---|
142 | if (!function_exists('getMicrotime')) { |
---|
143 | |
---|
144 | /** |
---|
145 | * Returns microtime for execution time checking |
---|
146 | * |
---|
147 | * @return float Microtime |
---|
148 | */ |
---|
149 | function getMicrotime() { |
---|
150 | list($usec, $sec) = explode(' ', microtime()); |
---|
151 | return ((float)$usec + (float)$sec); |
---|
152 | } |
---|
153 | } |
---|
154 | if (!function_exists('sortByKey')) { |
---|
155 | |
---|
156 | /** |
---|
157 | * Sorts given $array by key $sortby. |
---|
158 | * |
---|
159 | * @param array $array Array to sort |
---|
160 | * @param string $sortby Sort by this key |
---|
161 | * @param string $order Sort order asc/desc (ascending or descending). |
---|
162 | * @param integer $type Type of sorting to perform |
---|
163 | * @return mixed Sorted array |
---|
164 | */ |
---|
165 | function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) { |
---|
166 | if (!is_array($array)) { |
---|
167 | return null; |
---|
168 | } |
---|
169 | |
---|
170 | foreach ($array as $key => $val) { |
---|
171 | $sa[$key] = $val[$sortby]; |
---|
172 | } |
---|
173 | |
---|
174 | if ($order == 'asc') { |
---|
175 | asort($sa, $type); |
---|
176 | } else { |
---|
177 | arsort($sa, $type); |
---|
178 | } |
---|
179 | |
---|
180 | foreach ($sa as $key => $val) { |
---|
181 | $out[] = $array[$key]; |
---|
182 | } |
---|
183 | return $out; |
---|
184 | } |
---|
185 | } |
---|
186 | if (!function_exists('array_combine')) { |
---|
187 | |
---|
188 | /** |
---|
189 | * Combines given identical arrays by using the first array's values as keys, |
---|
190 | * and the second one's values as values. (Implemented for backwards compatibility with PHP4) |
---|
191 | * |
---|
192 | * @param array $a1 Array to use for keys |
---|
193 | * @param array $a2 Array to use for values |
---|
194 | * @return mixed Outputs either combined array or false. |
---|
195 | * @deprecated Will be removed in 2.0 |
---|
196 | */ |
---|
197 | function array_combine($a1, $a2) { |
---|
198 | $a1 = array_values($a1); |
---|
199 | $a2 = array_values($a2); |
---|
200 | $c1 = count($a1); |
---|
201 | $c2 = count($a2); |
---|
202 | |
---|
203 | if ($c1 != $c2) { |
---|
204 | return false; |
---|
205 | } |
---|
206 | if ($c1 <= 0) { |
---|
207 | return false; |
---|
208 | } |
---|
209 | $output = array(); |
---|
210 | |
---|
211 | for ($i = 0; $i < $c1; $i++) { |
---|
212 | $output[$a1[$i]] = $a2[$i]; |
---|
213 | } |
---|
214 | return $output; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Convenience method for htmlspecialchars. |
---|
220 | * |
---|
221 | * @param string $text Text to wrap through htmlspecialchars |
---|
222 | * @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8' |
---|
223 | * @return string Wrapped text |
---|
224 | * @link http://book.cakephp.org/view/1132/h |
---|
225 | */ |
---|
226 | function h($text, $charset = null) { |
---|
227 | if (is_array($text)) { |
---|
228 | return array_map('h', $text); |
---|
229 | } |
---|
230 | |
---|
231 | static $defaultCharset = false; |
---|
232 | if ($defaultCharset === false) { |
---|
233 | $defaultCharset = Configure::read('App.encoding'); |
---|
234 | if ($defaultCharset === null) { |
---|
235 | $defaultCharset = 'UTF-8'; |
---|
236 | } |
---|
237 | } |
---|
238 | if ($charset) { |
---|
239 | return htmlspecialchars($text, ENT_QUOTES, $charset); |
---|
240 | } else { |
---|
241 | return htmlspecialchars($text, ENT_QUOTES, $defaultCharset); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Splits a dot syntax plugin name into its plugin and classname. |
---|
247 | * If $name does not have a dot, then index 0 will be null. |
---|
248 | * |
---|
249 | * Commonly used like `list($plugin, $name) = pluginSplit($name);` |
---|
250 | * |
---|
251 | * @param string $name The name you want to plugin split. |
---|
252 | * @param boolean $dotAppend Set to true if you want the plugin to have a '.' appended to it. |
---|
253 | * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null. |
---|
254 | * @return array Array with 2 indexes. 0 => plugin name, 1 => classname |
---|
255 | */ |
---|
256 | function pluginSplit($name, $dotAppend = false, $plugin = null) { |
---|
257 | if (strpos($name, '.') !== false) { |
---|
258 | $parts = explode('.', $name, 2); |
---|
259 | if ($dotAppend) { |
---|
260 | $parts[0] .= '.'; |
---|
261 | } |
---|
262 | return $parts; |
---|
263 | } |
---|
264 | return array($plugin, $name); |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * Returns an array of all the given parameters. |
---|
269 | * |
---|
270 | * Example: |
---|
271 | * |
---|
272 | * `a('a', 'b')` |
---|
273 | * |
---|
274 | * Would return: |
---|
275 | * |
---|
276 | * `array('a', 'b')` |
---|
277 | * |
---|
278 | * @return array Array of given parameters |
---|
279 | * @link http://book.cakephp.org/view/1122/a |
---|
280 | * @deprecated Will be removed in 2.0 |
---|
281 | */ |
---|
282 | function a() { |
---|
283 | $args = func_get_args(); |
---|
284 | return $args; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * Constructs associative array from pairs of arguments. |
---|
289 | * |
---|
290 | * Example: |
---|
291 | * |
---|
292 | * `aa('a','b')` |
---|
293 | * |
---|
294 | * Would return: |
---|
295 | * |
---|
296 | * `array('a'=>'b')` |
---|
297 | * |
---|
298 | * @return array Associative array |
---|
299 | * @link http://book.cakephp.org/view/1123/aa |
---|
300 | * @deprecated Will be removed in 2.0 |
---|
301 | */ |
---|
302 | function aa() { |
---|
303 | $args = func_get_args(); |
---|
304 | $argc = count($args); |
---|
305 | for ($i = 0; $i < $argc; $i++) { |
---|
306 | if ($i + 1 < $argc) { |
---|
307 | $a[$args[$i]] = $args[$i + 1]; |
---|
308 | } else { |
---|
309 | $a[$args[$i]] = null; |
---|
310 | } |
---|
311 | $i++; |
---|
312 | } |
---|
313 | return $a; |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * Convenience method for echo(). |
---|
318 | * |
---|
319 | * @param string $text String to echo |
---|
320 | * @link http://book.cakephp.org/view/1129/e |
---|
321 | * @deprecated Will be removed in 2.0 |
---|
322 | */ |
---|
323 | function e($text) { |
---|
324 | echo $text; |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * Convenience method for strtolower(). |
---|
329 | * |
---|
330 | * @param string $str String to lowercase |
---|
331 | * @return string Lowercased string |
---|
332 | * @link http://book.cakephp.org/view/1134/low |
---|
333 | * @deprecated Will be removed in 2.0 |
---|
334 | */ |
---|
335 | function low($str) { |
---|
336 | return strtolower($str); |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * Convenience method for strtoupper(). |
---|
341 | * |
---|
342 | * @param string $str String to uppercase |
---|
343 | * @return string Uppercased string |
---|
344 | * @link http://book.cakephp.org/view/1139/up |
---|
345 | * @deprecated Will be removed in 2.0 |
---|
346 | */ |
---|
347 | function up($str) { |
---|
348 | return strtoupper($str); |
---|
349 | } |
---|
350 | |
---|
351 | /** |
---|
352 | * Convenience method for str_replace(). |
---|
353 | * |
---|
354 | * @param string $search String to be replaced |
---|
355 | * @param string $replace String to insert |
---|
356 | * @param string $subject String to search |
---|
357 | * @return string Replaced string |
---|
358 | * @link http://book.cakephp.org/view/1137/r |
---|
359 | * @deprecated Will be removed in 2.0 |
---|
360 | */ |
---|
361 | function r($search, $replace, $subject) { |
---|
362 | return str_replace($search, $replace, $subject); |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * Print_r convenience function, which prints out <PRE> tags around |
---|
367 | * the output of given array. Similar to debug(). |
---|
368 | * |
---|
369 | * @see debug() |
---|
370 | * @param array $var Variable to print out |
---|
371 | * @link http://book.cakephp.org/view/1136/pr |
---|
372 | */ |
---|
373 | function pr($var) { |
---|
374 | if (Configure::read() > 0) { |
---|
375 | echo '<pre>'; |
---|
376 | print_r($var); |
---|
377 | echo '</pre>'; |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | /** |
---|
382 | * Display parameters. |
---|
383 | * |
---|
384 | * @param mixed $p Parameter as string or array |
---|
385 | * @return string |
---|
386 | * @deprecated Will be removed in 2.0 |
---|
387 | */ |
---|
388 | function params($p) { |
---|
389 | if (!is_array($p) || count($p) == 0) { |
---|
390 | return null; |
---|
391 | } |
---|
392 | if (is_array($p[0]) && count($p) == 1) { |
---|
393 | return $p[0]; |
---|
394 | } |
---|
395 | return $p; |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | * Merge a group of arrays |
---|
400 | * |
---|
401 | * @param array First array |
---|
402 | * @param array Second array |
---|
403 | * @param array Third array |
---|
404 | * @param array Etc... |
---|
405 | * @return array All array parameters merged into one |
---|
406 | * @link http://book.cakephp.org/view/1124/am |
---|
407 | */ |
---|
408 | function am() { |
---|
409 | $r = array(); |
---|
410 | $args = func_get_args(); |
---|
411 | foreach ($args as $a) { |
---|
412 | if (!is_array($a)) { |
---|
413 | $a = array($a); |
---|
414 | } |
---|
415 | $r = array_merge($r, $a); |
---|
416 | } |
---|
417 | return $r; |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * Gets an environment variable from available sources, and provides emulation |
---|
422 | * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on |
---|
423 | * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom |
---|
424 | * environment information. |
---|
425 | * |
---|
426 | * @param string $key Environment variable name. |
---|
427 | * @return string Environment variable setting. |
---|
428 | * @link http://book.cakephp.org/view/1130/env |
---|
429 | */ |
---|
430 | function env($key) { |
---|
431 | if ($key == 'HTTPS') { |
---|
432 | if (isset($_SERVER['HTTPS'])) { |
---|
433 | return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'); |
---|
434 | } |
---|
435 | return (strpos(env('SCRIPT_URI'), 'https://') === 0); |
---|
436 | } |
---|
437 | |
---|
438 | if ($key == 'SCRIPT_NAME') { |
---|
439 | if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) { |
---|
440 | $key = 'SCRIPT_URL'; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | $val = null; |
---|
445 | if (isset($_SERVER[$key])) { |
---|
446 | $val = $_SERVER[$key]; |
---|
447 | } elseif (isset($_ENV[$key])) { |
---|
448 | $val = $_ENV[$key]; |
---|
449 | } elseif (getenv($key) !== false) { |
---|
450 | $val = getenv($key); |
---|
451 | } |
---|
452 | |
---|
453 | if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) { |
---|
454 | $addr = env('HTTP_PC_REMOTE_ADDR'); |
---|
455 | if ($addr !== null) { |
---|
456 | $val = $addr; |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | if ($val !== null) { |
---|
461 | return $val; |
---|
462 | } |
---|
463 | |
---|
464 | switch ($key) { |
---|
465 | case 'SCRIPT_FILENAME': |
---|
466 | if (defined('SERVER_IIS') && SERVER_IIS === true) { |
---|
467 | return str_replace('\\\\', '\\', env('PATH_TRANSLATED')); |
---|
468 | } |
---|
469 | break; |
---|
470 | case 'DOCUMENT_ROOT': |
---|
471 | $name = env('SCRIPT_NAME'); |
---|
472 | $filename = env('SCRIPT_FILENAME'); |
---|
473 | $offset = 0; |
---|
474 | if (!strpos($name, '.php')) { |
---|
475 | $offset = 4; |
---|
476 | } |
---|
477 | return substr($filename, 0, strlen($filename) - (strlen($name) + $offset)); |
---|
478 | break; |
---|
479 | case 'PHP_SELF': |
---|
480 | return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME')); |
---|
481 | break; |
---|
482 | case 'CGI_MODE': |
---|
483 | return (PHP_SAPI === 'cgi'); |
---|
484 | break; |
---|
485 | case 'HTTP_BASE': |
---|
486 | $host = env('HTTP_HOST'); |
---|
487 | $parts = explode('.', $host); |
---|
488 | $count = count($parts); |
---|
489 | |
---|
490 | if ($count === 1) { |
---|
491 | return '.' . $host; |
---|
492 | } elseif ($count === 2) { |
---|
493 | return '.' . $host; |
---|
494 | } elseif ($count === 3) { |
---|
495 | $gTLD = array('aero', 'asia', 'biz', 'cat', 'com', 'coop', 'edu', 'gov', 'info', 'int', 'jobs', 'mil', 'mobi', 'museum', 'name', 'net', 'org', 'pro', 'tel', 'travel', 'xxx'); |
---|
496 | if (in_array($parts[1], $gTLD)) { |
---|
497 | return '.' . $host; |
---|
498 | } |
---|
499 | } |
---|
500 | array_shift($parts); |
---|
501 | return '.' . implode('.', $parts); |
---|
502 | break; |
---|
503 | } |
---|
504 | return null; |
---|
505 | } |
---|
506 | if (!function_exists('file_put_contents')) { |
---|
507 | |
---|
508 | /** |
---|
509 | * Writes data into file. |
---|
510 | * |
---|
511 | * If file exists, it will be overwritten. If data is an array, it will be implode()ed with an empty string. |
---|
512 | * |
---|
513 | * @param string $fileName File name. |
---|
514 | * @param mixed $data String or array. |
---|
515 | * @return boolean Success |
---|
516 | * @deprecated Will be removed in 2.0 |
---|
517 | */ |
---|
518 | function file_put_contents($fileName, $data) { |
---|
519 | if (is_array($data)) { |
---|
520 | $data = implode('', $data); |
---|
521 | } |
---|
522 | $res = @fopen($fileName, 'w+b'); |
---|
523 | |
---|
524 | if ($res) { |
---|
525 | $write = @fwrite($res, $data); |
---|
526 | if ($write === false) { |
---|
527 | return false; |
---|
528 | } else { |
---|
529 | @fclose($res); |
---|
530 | return $write; |
---|
531 | } |
---|
532 | } |
---|
533 | return false; |
---|
534 | } |
---|
535 | } |
---|
536 | |
---|
537 | /** |
---|
538 | * Reads/writes temporary data to cache files or session. |
---|
539 | * |
---|
540 | * @param string $path File path within /tmp to save the file. |
---|
541 | * @param mixed $data The data to save to the temporary file. |
---|
542 | * @param mixed $expires A valid strtotime string when the data expires. |
---|
543 | * @param string $target The target of the cached data; either 'cache' or 'public'. |
---|
544 | * @return mixed The contents of the temporary file. |
---|
545 | * @deprecated Please use Cache::write() instead |
---|
546 | */ |
---|
547 | function cache($path, $data = null, $expires = '+1 day', $target = 'cache') { |
---|
548 | if (Configure::read('Cache.disable')) { |
---|
549 | return null; |
---|
550 | } |
---|
551 | $now = time(); |
---|
552 | |
---|
553 | if (!is_numeric($expires)) { |
---|
554 | $expires = strtotime($expires, $now); |
---|
555 | } |
---|
556 | |
---|
557 | switch (strtolower($target)) { |
---|
558 | case 'cache': |
---|
559 | $filename = CACHE . $path; |
---|
560 | break; |
---|
561 | case 'public': |
---|
562 | $filename = WWW_ROOT . $path; |
---|
563 | break; |
---|
564 | case 'tmp': |
---|
565 | $filename = TMP . $path; |
---|
566 | break; |
---|
567 | } |
---|
568 | $timediff = $expires - $now; |
---|
569 | $filetime = false; |
---|
570 | |
---|
571 | if (file_exists($filename)) { |
---|
572 | $filetime = @filemtime($filename); |
---|
573 | } |
---|
574 | |
---|
575 | if ($data === null) { |
---|
576 | if (file_exists($filename) && $filetime !== false) { |
---|
577 | if ($filetime + $timediff < $now) { |
---|
578 | @unlink($filename); |
---|
579 | } else { |
---|
580 | $data = @file_get_contents($filename); |
---|
581 | } |
---|
582 | } |
---|
583 | } elseif (is_writable(dirname($filename))) { |
---|
584 | @file_put_contents($filename, $data); |
---|
585 | } |
---|
586 | return $data; |
---|
587 | } |
---|
588 | |
---|
589 | /** |
---|
590 | * Used to delete files in the cache directories, or clear contents of cache directories |
---|
591 | * |
---|
592 | * @param mixed $params As String name to be searched for deletion, if name is a directory all files in |
---|
593 | * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params, |
---|
594 | * all files in app/tmp/cache/views will be deleted |
---|
595 | * @param string $type Directory in tmp/cache defaults to view directory |
---|
596 | * @param string $ext The file extension you are deleting |
---|
597 | * @return true if files found and deleted false otherwise |
---|
598 | */ |
---|
599 | function clearCache($params = null, $type = 'views', $ext = '.php') { |
---|
600 | if (is_string($params) || $params === null) { |
---|
601 | $params = preg_replace('/\/\//', '/', $params); |
---|
602 | $cache = CACHE . $type . DS . $params; |
---|
603 | |
---|
604 | if (is_file($cache . $ext)) { |
---|
605 | @unlink($cache . $ext); |
---|
606 | return true; |
---|
607 | } elseif (is_dir($cache)) { |
---|
608 | $files = glob($cache . '*'); |
---|
609 | |
---|
610 | if ($files === false) { |
---|
611 | return false; |
---|
612 | } |
---|
613 | |
---|
614 | foreach ($files as $file) { |
---|
615 | if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) { |
---|
616 | @unlink($file); |
---|
617 | } |
---|
618 | } |
---|
619 | return true; |
---|
620 | } else { |
---|
621 | $cache = array( |
---|
622 | CACHE . $type . DS . '*' . $params . $ext, |
---|
623 | CACHE . $type . DS . '*' . $params . '_*' . $ext |
---|
624 | ); |
---|
625 | $files = array(); |
---|
626 | while ($search = array_shift($cache)) { |
---|
627 | $results = glob($search); |
---|
628 | if ($results !== false) { |
---|
629 | $files = array_merge($files, $results); |
---|
630 | } |
---|
631 | } |
---|
632 | if (empty($files)) { |
---|
633 | return false; |
---|
634 | } |
---|
635 | foreach ($files as $file) { |
---|
636 | if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) { |
---|
637 | @unlink($file); |
---|
638 | } |
---|
639 | } |
---|
640 | return true; |
---|
641 | } |
---|
642 | } elseif (is_array($params)) { |
---|
643 | foreach ($params as $file) { |
---|
644 | clearCache($file, $type, $ext); |
---|
645 | } |
---|
646 | return true; |
---|
647 | } |
---|
648 | return false; |
---|
649 | } |
---|
650 | |
---|
651 | /** |
---|
652 | * Recursively strips slashes from all values in an array |
---|
653 | * |
---|
654 | * @param array $values Array of values to strip slashes |
---|
655 | * @return mixed What is returned from calling stripslashes |
---|
656 | * @link http://book.cakephp.org/view/1138/stripslashes_deep |
---|
657 | */ |
---|
658 | function stripslashes_deep($values) { |
---|
659 | if (is_array($values)) { |
---|
660 | foreach ($values as $key => $value) { |
---|
661 | $values[$key] = stripslashes_deep($value); |
---|
662 | } |
---|
663 | } else { |
---|
664 | $values = stripslashes($values); |
---|
665 | } |
---|
666 | return $values; |
---|
667 | } |
---|
668 | |
---|
669 | /** |
---|
670 | * Returns a translated string if one is found; Otherwise, the submitted message. |
---|
671 | * |
---|
672 | * @param string $singular Text to translate |
---|
673 | * @param boolean $return Set to true to return translated string, or false to echo |
---|
674 | * @return mixed translated string if $return is false string will be echoed |
---|
675 | * @link http://book.cakephp.org/view/1121/__ |
---|
676 | */ |
---|
677 | function __($singular, $return = false) { |
---|
678 | if (!$singular) { |
---|
679 | return; |
---|
680 | } |
---|
681 | if (!class_exists('I18n')) { |
---|
682 | App::import('Core', 'i18n'); |
---|
683 | } |
---|
684 | |
---|
685 | if ($return === false) { |
---|
686 | echo I18n::translate($singular); |
---|
687 | } else { |
---|
688 | return I18n::translate($singular); |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | /** |
---|
693 | * Returns correct plural form of message identified by $singular and $plural for count $count. |
---|
694 | * Some languages have more than one form for plural messages dependent on the count. |
---|
695 | * |
---|
696 | * @param string $singular Singular text to translate |
---|
697 | * @param string $plural Plural text |
---|
698 | * @param integer $count Count |
---|
699 | * @param boolean $return true to return, false to echo |
---|
700 | * @return mixed plural form of translated string if $return is false string will be echoed |
---|
701 | */ |
---|
702 | function __n($singular, $plural, $count, $return = false) { |
---|
703 | if (!$singular) { |
---|
704 | return; |
---|
705 | } |
---|
706 | if (!class_exists('I18n')) { |
---|
707 | App::import('Core', 'i18n'); |
---|
708 | } |
---|
709 | |
---|
710 | if ($return === false) { |
---|
711 | echo I18n::translate($singular, $plural, null, 6, $count); |
---|
712 | } else { |
---|
713 | return I18n::translate($singular, $plural, null, 6, $count); |
---|
714 | } |
---|
715 | } |
---|
716 | |
---|
717 | /** |
---|
718 | * Allows you to override the current domain for a single message lookup. |
---|
719 | * |
---|
720 | * @param string $domain Domain |
---|
721 | * @param string $msg String to translate |
---|
722 | * @param string $return true to return, false to echo |
---|
723 | * @return translated string if $return is false string will be echoed |
---|
724 | */ |
---|
725 | function __d($domain, $msg, $return = false) { |
---|
726 | if (!$msg) { |
---|
727 | return; |
---|
728 | } |
---|
729 | if (!class_exists('I18n')) { |
---|
730 | App::import('Core', 'i18n'); |
---|
731 | } |
---|
732 | |
---|
733 | if ($return === false) { |
---|
734 | echo I18n::translate($msg, null, $domain); |
---|
735 | } else { |
---|
736 | return I18n::translate($msg, null, $domain); |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | /** |
---|
741 | * Allows you to override the current domain for a single plural message lookup. |
---|
742 | * Returns correct plural form of message identified by $singular and $plural for count $count |
---|
743 | * from domain $domain. |
---|
744 | * |
---|
745 | * @param string $domain Domain |
---|
746 | * @param string $singular Singular string to translate |
---|
747 | * @param string $plural Plural |
---|
748 | * @param integer $count Count |
---|
749 | * @param boolean $return true to return, false to echo |
---|
750 | * @return plural form of translated string if $return is false string will be echoed |
---|
751 | */ |
---|
752 | function __dn($domain, $singular, $plural, $count, $return = false) { |
---|
753 | if (!$singular) { |
---|
754 | return; |
---|
755 | } |
---|
756 | if (!class_exists('I18n')) { |
---|
757 | App::import('Core', 'i18n'); |
---|
758 | } |
---|
759 | |
---|
760 | if ($return === false) { |
---|
761 | echo I18n::translate($singular, $plural, $domain, 6, $count); |
---|
762 | } else { |
---|
763 | return I18n::translate($singular, $plural, $domain, 6, $count); |
---|
764 | } |
---|
765 | } |
---|
766 | |
---|
767 | /** |
---|
768 | * Allows you to override the current domain for a single message lookup. |
---|
769 | * It also allows you to specify a category. |
---|
770 | * |
---|
771 | * The category argument allows a specific category of the locale settings to be used for fetching a message. |
---|
772 | * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL. |
---|
773 | * |
---|
774 | * Note that the category must be specified with a numeric value, instead of the constant name. The values are: |
---|
775 | * |
---|
776 | * - LC_ALL 0 |
---|
777 | * - LC_COLLATE 1 |
---|
778 | * - LC_CTYPE 2 |
---|
779 | * - LC_MONETARY 3 |
---|
780 | * - LC_NUMERIC 4 |
---|
781 | * - LC_TIME 5 |
---|
782 | * - LC_MESSAGES 6 |
---|
783 | * |
---|
784 | * @param string $domain Domain |
---|
785 | * @param string $msg Message to translate |
---|
786 | * @param integer $category Category |
---|
787 | * @param boolean $return true to return, false to echo |
---|
788 | * @return translated string if $return is false string will be echoed |
---|
789 | */ |
---|
790 | function __dc($domain, $msg, $category, $return = false) { |
---|
791 | if (!$msg) { |
---|
792 | return; |
---|
793 | } |
---|
794 | if (!class_exists('I18n')) { |
---|
795 | App::import('Core', 'i18n'); |
---|
796 | } |
---|
797 | |
---|
798 | if ($return === false) { |
---|
799 | echo I18n::translate($msg, null, $domain, $category); |
---|
800 | } else { |
---|
801 | return I18n::translate($msg, null, $domain, $category); |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | /** |
---|
806 | * Allows you to override the current domain for a single plural message lookup. |
---|
807 | * It also allows you to specify a category. |
---|
808 | * Returns correct plural form of message identified by $singular and $plural for count $count |
---|
809 | * from domain $domain. |
---|
810 | * |
---|
811 | * The category argument allows a specific category of the locale settings to be used for fetching a message. |
---|
812 | * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL. |
---|
813 | * |
---|
814 | * Note that the category must be specified with a numeric value, instead of the constant name. The values are: |
---|
815 | * |
---|
816 | * - LC_ALL 0 |
---|
817 | * - LC_COLLATE 1 |
---|
818 | * - LC_CTYPE 2 |
---|
819 | * - LC_MONETARY 3 |
---|
820 | * - LC_NUMERIC 4 |
---|
821 | * - LC_TIME 5 |
---|
822 | * - LC_MESSAGES 6 |
---|
823 | * |
---|
824 | * @param string $domain Domain |
---|
825 | * @param string $singular Singular string to translate |
---|
826 | * @param string $plural Plural |
---|
827 | * @param integer $count Count |
---|
828 | * @param integer $category Category |
---|
829 | * @param boolean $return true to return, false to echo |
---|
830 | * @return plural form of translated string if $return is false string will be echoed |
---|
831 | */ |
---|
832 | function __dcn($domain, $singular, $plural, $count, $category, $return = false) { |
---|
833 | if (!$singular) { |
---|
834 | return; |
---|
835 | } |
---|
836 | if (!class_exists('I18n')) { |
---|
837 | App::import('Core', 'i18n'); |
---|
838 | } |
---|
839 | |
---|
840 | if ($return === false) { |
---|
841 | echo I18n::translate($singular, $plural, $domain, $category, $count); |
---|
842 | } else { |
---|
843 | return I18n::translate($singular, $plural, $domain, $category, $count); |
---|
844 | } |
---|
845 | } |
---|
846 | |
---|
847 | /** |
---|
848 | * The category argument allows a specific category of the locale settings to be used for fetching a message. |
---|
849 | * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL. |
---|
850 | * |
---|
851 | * Note that the category must be specified with a numeric value, instead of the constant name. The values are: |
---|
852 | * |
---|
853 | * - LC_ALL 0 |
---|
854 | * - LC_COLLATE 1 |
---|
855 | * - LC_CTYPE 2 |
---|
856 | * - LC_MONETARY 3 |
---|
857 | * - LC_NUMERIC 4 |
---|
858 | * - LC_TIME 5 |
---|
859 | * - LC_MESSAGES 6 |
---|
860 | * |
---|
861 | * @param string $msg String to translate |
---|
862 | * @param integer $category Category |
---|
863 | * @param string $return true to return, false to echo |
---|
864 | * @return translated string if $return is false string will be echoed |
---|
865 | */ |
---|
866 | function __c($msg, $category, $return = false) { |
---|
867 | if (!$msg) { |
---|
868 | return; |
---|
869 | } |
---|
870 | if (!class_exists('I18n')) { |
---|
871 | App::import('Core', 'i18n'); |
---|
872 | } |
---|
873 | |
---|
874 | if ($return === false) { |
---|
875 | echo I18n::translate($msg, null, null, $category); |
---|
876 | } else { |
---|
877 | return I18n::translate($msg, null, null, $category); |
---|
878 | } |
---|
879 | } |
---|
880 | |
---|
881 | /** |
---|
882 | * Computes the difference of arrays using keys for comparison. |
---|
883 | * |
---|
884 | * @param array First array |
---|
885 | * @param array Second array |
---|
886 | * @return array Array with different keys |
---|
887 | * @deprecated Will be removed in 2.0 |
---|
888 | */ |
---|
889 | if (!function_exists('array_diff_key')) { |
---|
890 | function array_diff_key() { |
---|
891 | $valuesDiff = array(); |
---|
892 | |
---|
893 | $argc = func_num_args(); |
---|
894 | if ($argc < 2) { |
---|
895 | return false; |
---|
896 | } |
---|
897 | |
---|
898 | $args = func_get_args(); |
---|
899 | foreach ($args as $param) { |
---|
900 | if (!is_array($param)) { |
---|
901 | return false; |
---|
902 | } |
---|
903 | } |
---|
904 | |
---|
905 | foreach ($args[0] as $valueKey => $valueData) { |
---|
906 | for ($i = 1; $i < $argc; $i++) { |
---|
907 | if (array_key_exists($valueKey, $args[$i])) { |
---|
908 | continue 2; |
---|
909 | } |
---|
910 | } |
---|
911 | $valuesDiff[$valueKey] = $valueData; |
---|
912 | } |
---|
913 | return $valuesDiff; |
---|
914 | } |
---|
915 | } |
---|
916 | |
---|
917 | /** |
---|
918 | * Computes the intersection of arrays using keys for comparison |
---|
919 | * |
---|
920 | * @param array First array |
---|
921 | * @param array Second array |
---|
922 | * @return array Array with interesected keys |
---|
923 | * @deprecated Will be removed in 2.0 |
---|
924 | */ |
---|
925 | if (!function_exists('array_intersect_key')) { |
---|
926 | function array_intersect_key($arr1, $arr2) { |
---|
927 | $res = array(); |
---|
928 | foreach ($arr1 as $key => $value) { |
---|
929 | if (array_key_exists($key, $arr2)) { |
---|
930 | $res[$key] = $arr1[$key]; |
---|
931 | } |
---|
932 | } |
---|
933 | return $res; |
---|
934 | } |
---|
935 | } |
---|
936 | |
---|
937 | /** |
---|
938 | * Shortcut to Log::write. |
---|
939 | * |
---|
940 | * @param string $message Message to write to log |
---|
941 | */ |
---|
942 | function LogError($message) { |
---|
943 | if (!class_exists('CakeLog')) { |
---|
944 | App::import('Core', 'CakeLog'); |
---|
945 | } |
---|
946 | $bad = array("\n", "\r", "\t"); |
---|
947 | $good = ' '; |
---|
948 | CakeLog::write('error', str_replace($bad, $good, $message)); |
---|
949 | } |
---|
950 | |
---|
951 | /** |
---|
952 | * Searches include path for files. |
---|
953 | * |
---|
954 | * @param string $file File to look for |
---|
955 | * @return Full path to file if exists, otherwise false |
---|
956 | * @link http://book.cakephp.org/view/1131/fileExistsInPath |
---|
957 | */ |
---|
958 | function fileExistsInPath($file) { |
---|
959 | $paths = explode(PATH_SEPARATOR, ini_get('include_path')); |
---|
960 | foreach ($paths as $path) { |
---|
961 | $fullPath = $path . DS . $file; |
---|
962 | |
---|
963 | if (file_exists($fullPath)) { |
---|
964 | return $fullPath; |
---|
965 | } elseif (file_exists($file)) { |
---|
966 | return $file; |
---|
967 | } |
---|
968 | } |
---|
969 | return false; |
---|
970 | } |
---|
971 | |
---|
972 | /** |
---|
973 | * Convert forward slashes to underscores and removes first and last underscores in a string |
---|
974 | * |
---|
975 | * @param string String to convert |
---|
976 | * @return string with underscore remove from start and end of string |
---|
977 | * @link http://book.cakephp.org/view/1126/convertSlash |
---|
978 | */ |
---|
979 | function convertSlash($string) { |
---|
980 | $string = trim($string, '/'); |
---|
981 | $string = preg_replace('/\/\//', '/', $string); |
---|
982 | $string = str_replace('/', '_', $string); |
---|
983 | return $string; |
---|
984 | } |
---|
985 | |
---|
986 | /** |
---|
987 | * Implements http_build_query for PHP4. |
---|
988 | * |
---|
989 | * @param string $data Data to set in query string |
---|
990 | * @param string $prefix If numeric indices, prepend this to index for elements in base array. |
---|
991 | * @param string $argSep String used to separate arguments |
---|
992 | * @param string $baseKey Base key |
---|
993 | * @return string URL encoded query string |
---|
994 | * @see http://php.net/http_build_query |
---|
995 | * @deprecated Will be removed in 2.0 |
---|
996 | */ |
---|
997 | if (!function_exists('http_build_query')) { |
---|
998 | function http_build_query($data, $prefix = null, $argSep = null, $baseKey = null) { |
---|
999 | if (empty($argSep)) { |
---|
1000 | $argSep = ini_get('arg_separator.output'); |
---|
1001 | } |
---|
1002 | if (is_object($data)) { |
---|
1003 | $data = get_object_vars($data); |
---|
1004 | } |
---|
1005 | $out = array(); |
---|
1006 | |
---|
1007 | foreach ((array)$data as $key => $v) { |
---|
1008 | if (is_numeric($key) && !empty($prefix)) { |
---|
1009 | $key = $prefix . $key; |
---|
1010 | } |
---|
1011 | $key = urlencode($key); |
---|
1012 | |
---|
1013 | if (!empty($baseKey)) { |
---|
1014 | $key = $baseKey . '[' . $key . ']'; |
---|
1015 | } |
---|
1016 | |
---|
1017 | if (is_array($v) || is_object($v)) { |
---|
1018 | $out[] = http_build_query($v, $prefix, $argSep, $key); |
---|
1019 | } else { |
---|
1020 | $out[] = $key . '=' . urlencode($v); |
---|
1021 | } |
---|
1022 | } |
---|
1023 | return implode($argSep, $out); |
---|
1024 | } |
---|
1025 | } |
---|
1026 | |
---|
1027 | /** |
---|
1028 | * Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2. |
---|
1029 | * Don't use for isset() conditions, or wrap your variable with @ operator: |
---|
1030 | * Example: |
---|
1031 | * |
---|
1032 | * `ife(isset($variable), @$variable, 'default');` |
---|
1033 | * |
---|
1034 | * @param mixed $condition Conditional expression |
---|
1035 | * @param mixed $val1 Value to return in case condition matches |
---|
1036 | * @param mixed $val2 Value to return if condition doesn't match |
---|
1037 | * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression. |
---|
1038 | * @link http://book.cakephp.org/view/1133/ife |
---|
1039 | * @deprecated Will be removed in 2.0 |
---|
1040 | */ |
---|
1041 | function ife($condition, $val1 = null, $val2 = null) { |
---|
1042 | if (!empty($condition)) { |
---|
1043 | return $val1; |
---|
1044 | } |
---|
1045 | return $val2; |
---|
1046 | } |
---|