00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 define('SECOND', 1);
00031 define('MINUTE', 60 * SECOND);
00032 define('HOUR', 60 * MINUTE);
00033 define('DAY', 24 * HOUR);
00034 define('WEEK', 7 * DAY);
00035 define('MONTH', 30 * DAY);
00036 define('YEAR', 365 * DAY);
00037
00038
00039
00040 if (!function_exists('clone')) {
00041 if (version_compare(PHP_VERSION, '5.0') < 0) {
00042 eval ('
00043 function clone($object)
00044 {
00045 return $object;
00046 }');
00047 }
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 function config() {
00060 $args = func_get_args();
00061 foreach ($args as $arg) {
00062 if ($arg === 'database' && file_exists(CONFIGS . 'database.php')) {
00063 include_once(CONFIGS . $arg . '.php');
00064 } elseif (file_exists(CONFIGS . $arg . '.php')) {
00065 include_once(CONFIGS . $arg . '.php');
00066
00067 if (count($args) == 1) {
00068 return true;
00069 }
00070 } else {
00071 if (count($args) == 1) {
00072 return false;
00073 }
00074 }
00075 }
00076 return true;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 function uses() {
00089 $args = func_get_args();
00090 foreach ($args as $file) {
00091 require_once(LIBS . strtolower($file) . '.php');
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 function debug($var = false, $showHtml = false, $showFrom = true) {
00105 if (Configure::read() > 0) {
00106 if ($showFrom) {
00107 $calledFrom = debug_backtrace();
00108 echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
00109 echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
00110 }
00111 echo "\n<pre class=\"cake-debug\">\n";
00112
00113 $var = print_r($var, true);
00114 if ($showHtml) {
00115 $var = str_replace('<', '<', str_replace('>', '>', $var));
00116 }
00117 echo $var . "\n</pre>\n";
00118 }
00119 }
00120 if (!function_exists('getMicrotime')) {
00121
00122
00123
00124
00125
00126 function getMicrotime() {
00127 list($usec, $sec) = explode(' ', microtime());
00128 return ((float)$usec + (float)$sec);
00129 }
00130 }
00131 if (!function_exists('sortByKey')) {
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
00142 if (!is_array($array)) {
00143 return null;
00144 }
00145
00146 foreach ($array as $key => $val) {
00147 $sa[$key] = $val[$sortby];
00148 }
00149
00150 if ($order == 'asc') {
00151 asort($sa, $type);
00152 } else {
00153 arsort($sa, $type);
00154 }
00155
00156 foreach ($sa as $key => $val) {
00157 $out[] = $array[$key];
00158 }
00159 return $out;
00160 }
00161 }
00162 if (!function_exists('array_combine')) {
00163
00164
00165
00166
00167
00168
00169
00170
00171 function array_combine($a1, $a2) {
00172 $a1 = array_values($a1);
00173 $a2 = array_values($a2);
00174 $c1 = count($a1);
00175 $c2 = count($a2);
00176
00177 if ($c1 != $c2) {
00178 return false;
00179 }
00180 if ($c1 <= 0) {
00181 return false;
00182 }
00183 $output = array();
00184
00185 for ($i = 0; $i < $c1; $i++) {
00186 $output[$a1[$i]] = $a2[$i];
00187 }
00188 return $output;
00189 }
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199 function h($text, $charset = null) {
00200 if (is_array($text)) {
00201 return array_map('h', $text);
00202 }
00203 if (empty($charset)) {
00204 $charset = Configure::read('App.encoding');
00205 }
00206 if (empty($charset)) {
00207 $charset = 'UTF-8';
00208 }
00209 return htmlspecialchars($text, ENT_QUOTES, $charset);
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 function a() {
00228 $args = func_get_args();
00229 return $args;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 function aa() {
00248 $args = func_get_args();
00249 $argc = count($args);
00250 for ($i = 0; $i < $argc; $i++) {
00251 if ($i + 1 < $argc) {
00252 $a[$args[$i]] = $args[$i + 1];
00253 } else {
00254 $a[$args[$i]] = null;
00255 }
00256 $i++;
00257 }
00258 return $a;
00259 }
00260
00261
00262
00263
00264
00265
00266 function e($text) {
00267 echo $text;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276 function low($str) {
00277 return strtolower($str);
00278 }
00279
00280
00281
00282
00283
00284
00285
00286 function up($str) {
00287 return strtoupper($str);
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 function r($search, $replace, $subject) {
00299 return str_replace($search, $replace, $subject);
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 function pr($var) {
00311 if (Configure::read() > 0) {
00312 echo '<pre>';
00313 print_r($var);
00314 echo '</pre>';
00315 }
00316 }
00317
00318
00319
00320
00321
00322
00323 function params($p) {
00324 if (!is_array($p) || count($p) == 0) {
00325 return null;
00326 }
00327 if (is_array($p[0]) && count($p) == 1) {
00328 return $p[0];
00329 }
00330 return $p;
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 function am() {
00343 $r = array();
00344 $args = func_get_args();
00345 foreach ($args as $a) {
00346 if (!is_array($a)) {
00347 $a = array($a);
00348 }
00349 $r = array_merge($r, $a);
00350 }
00351 return $r;
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 function env($key) {
00364 if ($key == 'HTTPS') {
00365 if (isset($_SERVER['HTTPS'])) {
00366 return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
00367 }
00368 return (strpos(env('SCRIPT_URI'), 'https:
00369 }
00370
00371 if ($key == 'SCRIPT_NAME') {
00372 if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
00373 $key = 'SCRIPT_URL';
00374 }
00375 }
00376
00377 $val = null;
00378 if (isset($_SERVER[$key])) {
00379 $val = $_SERVER[$key];
00380 } elseif (isset($_ENV[$key])) {
00381 $val = $_ENV[$key];
00382 } elseif (getenv($key) !== false) {
00383 $val = getenv($key);
00384 }
00385
00386 if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
00387 $addr = env('HTTP_PC_REMOTE_ADDR');
00388 if ($addr !== null) {
00389 $val = $addr;
00390 }
00391 }
00392
00393 if ($val !== null) {
00394 return $val;
00395 }
00396
00397 switch ($key) {
00398 case 'SCRIPT_FILENAME':
00399 if (defined('SERVER_IIS') && SERVER_IIS === true) {
00400 return str_replace('\\\\', '\\', env('PATH_TRANSLATED'));
00401 }
00402 break;
00403 case 'DOCUMENT_ROOT':
00404 $name = env('SCRIPT_NAME');
00405 $filename = env('SCRIPT_FILENAME');
00406 $offset = 0;
00407 if (!strpos($name, '.php')) {
00408 $offset = 4;
00409 }
00410 return substr($filename, 0, strlen($filename) - (strlen($name) + $offset));
00411 break;
00412 case 'PHP_SELF':
00413 return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
00414 break;
00415 case 'CGI_MODE':
00416 return (PHP_SAPI === 'cgi');
00417 break;
00418 case 'HTTP_BASE':
00419 $host = env('HTTP_HOST');
00420 if (substr_count($host, '.') !== 1) {
00421 return preg_replace('/^([^.])*/i', null, env('HTTP_HOST'));
00422 }
00423 return '.' . $host;
00424 break;
00425 }
00426 return null;
00427 }
00428 if (!function_exists('file_put_contents')) {
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 function file_put_contents($fileName, $data) {
00439 if (is_array($data)) {
00440 $data = join('', $data);
00441 }
00442 $res = @fopen($fileName, 'w+b');
00443
00444 if ($res) {
00445 $write = @fwrite($res, $data);
00446 if ($write === false) {
00447 return false;
00448 } else {
00449 @fclose($res);
00450 return $write;
00451 }
00452 }
00453 return false;
00454 }
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
00467 if (Configure::read('Cache.disable')) {
00468 return null;
00469 }
00470 $now = time();
00471
00472 if (!is_numeric($expires)) {
00473 $expires = strtotime($expires, $now);
00474 }
00475
00476 switch (low($target)) {
00477 case 'cache':
00478 $filename = CACHE . $path;
00479 break;
00480 case 'public':
00481 $filename = WWW_ROOT . $path;
00482 break;
00483 case 'tmp':
00484 $filename = TMP . $path;
00485 break;
00486 }
00487 $timediff = $expires - $now;
00488 $filetime = false;
00489
00490 if (file_exists($filename)) {
00491 $filetime = @filemtime($filename);
00492 }
00493
00494 if ($data === null) {
00495 if (file_exists($filename) && $filetime !== false) {
00496 if ($filetime + $timediff < $now) {
00497 @unlink($filename);
00498 } else {
00499 $data = @file_get_contents($filename);
00500 }
00501 }
00502 } elseif (is_writable(dirname($filename))) {
00503 @file_put_contents($filename, $data);
00504 }
00505 return $data;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 function clearCache($params = null, $type = 'views', $ext = '.php') {
00519 if (is_string($params) || $params === null) {
00520 $params = preg_replace('/\/\
00521 $cache = CACHE . $type . DS . $params;
00522
00523 if (is_file($cache . $ext)) {
00524 @unlink($cache . $ext);
00525 return true;
00526 } elseif (is_dir($cache)) {
00527 $files = glob($cache . '*');
00528
00529 if ($files === false) {
00530 return false;
00531 }
00532
00533 foreach ($files as $file) {
00534 if (is_file($file)) {
00535 @unlink($file);
00536 }
00537 }
00538 return true;
00539 } else {
00540 $cache = array(
00541 CACHE . $type . DS . '*' . $params . $ext,
00542 CACHE . $type . DS . '*' . $params . '_*' . $ext
00543 );
00544 $files = array();
00545 while ($search = array_shift($cache)) {
00546 $results = glob($search);
00547 if ($results !== false) {
00548 $files = array_merge($files, $results);
00549 }
00550 }
00551 if (empty($files)) {
00552 return false;
00553 }
00554 foreach ($files as $file) {
00555 if (is_file($file)) {
00556 @unlink($file);
00557 }
00558 }
00559 return true;
00560 }
00561 } elseif (is_array($params)) {
00562 foreach ($params as $file) {
00563 clearCache($file, $type, $ext);
00564 }
00565 return true;
00566 }
00567 return false;
00568 }
00569
00570
00571
00572
00573
00574
00575
00576 function stripslashes_deep($values) {
00577 if (is_array($values)) {
00578 foreach ($values as $key => $value) {
00579 $values[$key] = stripslashes_deep($value);
00580 }
00581 } else {
00582 $values = stripslashes($values);
00583 }
00584 return $values;
00585 }
00586
00587
00588
00589
00590
00591
00592
00593
00594 function __($singular, $return = false) {
00595 if (!$singular) {
00596 return;
00597 }
00598 if (!class_exists('I18n')) {
00599 App::import('Core', 'i18n');
00600 }
00601
00602 if ($return === false) {
00603 echo I18n::translate($singular);
00604 } else {
00605 return I18n::translate($singular);
00606 }
00607 }
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 function __n($singular, $plural, $count, $return = false) {
00619 if (!$singular) {
00620 return;
00621 }
00622 if (!class_exists('I18n')) {
00623 App::import('Core', 'i18n');
00624 }
00625
00626 if ($return === false) {
00627 echo I18n::translate($singular, $plural, null, 6, $count);
00628 } else {
00629 return I18n::translate($singular, $plural, null, 6, $count);
00630 }
00631 }
00632
00633
00634
00635
00636
00637
00638
00639
00640 function __d($domain, $msg, $return = false) {
00641 if (!$msg) {
00642 return;
00643 }
00644 if (!class_exists('I18n')) {
00645 App::import('Core', 'i18n');
00646 }
00647
00648 if ($return === false) {
00649 echo I18n::translate($msg, null, $domain);
00650 } else {
00651 return I18n::translate($msg, null, $domain);
00652 }
00653 }
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666 function __dn($domain, $singular, $plural, $count, $return = false) {
00667 if (!$singular) {
00668 return;
00669 }
00670 if (!class_exists('I18n')) {
00671 App::import('Core', 'i18n');
00672 }
00673
00674 if ($return === false) {
00675 echo I18n::translate($singular, $plural, $domain, 6, $count);
00676 } else {
00677 return I18n::translate($singular, $plural, $domain, 6, $count);
00678 }
00679 }
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702 function __dc($domain, $msg, $category, $return = false) {
00703 if (!$msg) {
00704 return;
00705 }
00706 if (!class_exists('I18n')) {
00707 App::import('Core', 'i18n');
00708 }
00709
00710 if ($return === false) {
00711 echo I18n::translate($msg, null, $domain, $category);
00712 } else {
00713 return I18n::translate($msg, null, $domain, $category);
00714 }
00715 }
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 function __dcn($domain, $singular, $plural, $count, $category, $return = false) {
00743 if (!$singular) {
00744 return;
00745 }
00746 if (!class_exists('I18n')) {
00747 App::import('Core', 'i18n');
00748 }
00749
00750 if ($return === false) {
00751 echo I18n::translate($singular, $plural, $domain, $category, $count);
00752 } else {
00753 return I18n::translate($singular, $plural, $domain, $category, $count);
00754 }
00755 }
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774 function __c($msg, $category, $return = false) {
00775 if (!$msg) {
00776 return;
00777 }
00778 if (!class_exists('I18n')) {
00779 App::import('Core', 'i18n');
00780 }
00781
00782 if ($return === false) {
00783 echo I18n::translate($msg, null, null, $category);
00784 } else {
00785 return I18n::translate($msg, null, null, $category);
00786 }
00787 }
00788
00789
00790
00791
00792
00793
00794
00795 if (!function_exists('array_diff_key')) {
00796 function array_diff_key() {
00797 $valuesDiff = array();
00798
00799 $argc = func_num_args();
00800 if ($argc < 2) {
00801 return false;
00802 }
00803
00804 $args = func_get_args();
00805 foreach ($args as $param) {
00806 if (!is_array($param)) {
00807 return false;
00808 }
00809 }
00810
00811 foreach ($args[0] as $valueKey => $valueData) {
00812 for ($i = 1; $i < $argc; $i++) {
00813 if (array_key_exists($valueKey, $args[$i])) {
00814 continue 2;
00815 }
00816 }
00817 $valuesDiff[$valueKey] = $valueData;
00818 }
00819 return $valuesDiff;
00820 }
00821 }
00822
00823
00824
00825
00826
00827
00828
00829 if (!function_exists('array_intersect_key')) {
00830 function array_intersect_key($arr1, $arr2) {
00831 $res = array();
00832 foreach ($arr1 as $key => $value) {
00833 if (array_key_exists($key, $arr2)) {
00834 $res[$key] = $arr1[$key];
00835 }
00836 }
00837 return $res;
00838 }
00839 }
00840
00841
00842
00843
00844
00845 function LogError($message) {
00846 if (!class_exists('CakeLog')) {
00847 App::import('Core', 'CakeLog');
00848 }
00849 $bad = array("\n", "\r", "\t");
00850 $good = ' ';
00851 CakeLog::write('error', str_replace($bad, $good, $message));
00852 }
00853
00854
00855
00856
00857
00858
00859
00860 function fileExistsInPath($file) {
00861 $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
00862 foreach ($paths as $path) {
00863 $fullPath = $path . DS . $file;
00864
00865 if (file_exists($fullPath)) {
00866 return $fullPath;
00867 } elseif (file_exists($file)) {
00868 return $file;
00869 }
00870 }
00871 return false;
00872 }
00873
00874
00875
00876
00877
00878
00879
00880 function convertSlash($string) {
00881 $string = trim($string, '/');
00882 $string = preg_replace('/\/\
00883 $string = str_replace('/', '_', $string);
00884 return $string;
00885 }
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896 if (!function_exists('http_build_query')) {
00897 function http_build_query($data, $prefix = null, $argSep = null, $baseKey = null) {
00898 if (empty($argSep)) {
00899 $argSep = ini_get('arg_separator.output');
00900 }
00901 if (is_object($data)) {
00902 $data = get_object_vars($data);
00903 }
00904 $out = array();
00905
00906 foreach ((array)$data as $key => $v) {
00907 if (is_numeric($key) && !empty($prefix)) {
00908 $key = $prefix . $key;
00909 }
00910 $key = urlencode($key);
00911
00912 if (!empty($baseKey)) {
00913 $key = $baseKey . '[' . $key . ']';
00914 }
00915
00916 if (is_array($v) || is_object($v)) {
00917 $out[] = http_build_query($v, $prefix, $argSep, $key);
00918 } else {
00919 $out[] = $key . '=' . urlencode($v);
00920 }
00921 }
00922 return implode($argSep, $out);
00923 }
00924 }
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939 function ife($condition, $val1 = null, $val2 = null) {
00940 if (!empty($condition)) {
00941 return $val1;
00942 }
00943 return $val2;
00944 }
00945 ?>