basics.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: basics_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Basic Cake functionality.
00005  *
00006  * Core functions for including other source files, loading models and so forth.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake
00023  * @since           CakePHP(tm) v 0.2.9
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Basic defines for timing functions.
00031  */
00032     define('SECOND', 1);
00033     define('MINUTE', 60 * SECOND);
00034     define('HOUR', 60 * MINUTE);
00035     define('DAY', 24 * HOUR);
00036     define('WEEK', 7 * DAY);
00037     define('MONTH', 30 * DAY);
00038     define('YEAR', 365 * DAY);
00039 /**
00040  * Patch for PHP < 5.0
00041  */
00042 if (!function_exists('clone')) {
00043     if (version_compare(phpversion(), '5.0') < 0) {
00044         eval ('
00045         function clone($object)
00046         {
00047             return $object;
00048         }');
00049     }
00050 }
00051 /**
00052  * Loads configuration files. Receives a set of configuration files
00053  * to load.
00054  * Example:
00055  * <code>
00056  * config('config1', 'config2');
00057  * </code>
00058  *
00059  * @return boolean Success
00060  */
00061     function config() {
00062         $args = func_get_args();
00063         foreach ($args as $arg) {
00064             if (('database' == $arg) && file_exists(CONFIGS . $arg . '.php')) {
00065                 include_once(CONFIGS . $arg . '.php');
00066             } elseif (file_exists(CONFIGS . $arg . '.php')) {
00067                 include_once(CONFIGS . $arg . '.php');
00068 
00069                 if (count($args) == 1) {
00070                     return true;
00071                 }
00072             } else {
00073                 if (count($args) == 1) {
00074                     return false;
00075                 }
00076             }
00077         }
00078         return true;
00079     }
00080 /**
00081  * Loads component/components from LIBS. Takes optional number of parameters.
00082  *
00083  * Example:
00084  * <code>
00085  * uses('flay', 'time');
00086  * </code>
00087  *
00088  * @param string $name Filename without the .php part
00089  */
00090     function uses() {
00091         $args = func_get_args();
00092         foreach ($args as $file) {
00093             require_once(LIBS . strtolower($file) . '.php');
00094         }
00095     }
00096 /**
00097  * Prints out debug information about given variable.
00098  *
00099  * Only runs if debug level is non-zero.
00100  *
00101  * @param boolean $var Variable to show debug information for.
00102  * @param boolean $showHtml If set to true, the method prints the debug data in a screen-friendly way.
00103  * @param boolean $showFrom If set to true, the method prints from where the function was called.
00104  */
00105     function debug($var = false, $showHtml = false, $showFrom = true) {
00106         if (Configure::read() > 0) {
00107             if ($showFrom) {
00108                 $calledFrom = debug_backtrace();
00109                 print "<strong>".substr(r(ROOT, "", $calledFrom[0]['file']), 1)."</strong> (line <strong>".$calledFrom[0]['line']."</strong>)";
00110             }
00111             print "\n<pre class=\"cake-debug\">\n";
00112             $var = print_r($var, true);
00113 
00114             if ($showHtml) {
00115                 $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
00116             }
00117             print "{$var}\n</pre>\n";
00118         }
00119     }
00120     if (!function_exists('getMicrotime')) {
00121 /**
00122  * Returns microtime for execution time checking
00123  *
00124  * @return float Microtime
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  * Sorts given $array by key $sortby.
00134  *
00135  * @param array $array Array to sort
00136  * @param string $sortby Sort by this key
00137  * @param string $order  Sort order asc/desc (ascending or descending).
00138  * @param integer $type Type of sorting to perform
00139  * @return mixed Sorted array
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  * Combines given identical arrays by using the first array's values as keys,
00165  * and the second one's values as values. (Implemented for back-compatibility with PHP4)
00166  *
00167  * @param array $a1 Array to use for keys
00168  * @param array $a2 Array to use for values
00169  * @return mixed Outputs either combined array or false.
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 
00184             $output=array();
00185             for ($i = 0; $i < $c1; $i++) {
00186                 $output[$a1[$i]] = $a2[$i];
00187             }
00188             return $output;
00189         }
00190     }
00191 /**
00192  * Convenience method for htmlspecialchars.
00193  *
00194  * @param string $text Text to wrap through htmlspecialchars
00195  * @param string $charset Character set to use when escaping.  Defaults to config value in 'App.encoding' or 'UTF-8'
00196  * @return string Wrapped text
00197  */
00198     function h($text, $charset = null) {
00199         if (is_array($text)) {
00200             return array_map('h', $text);
00201         }
00202         if (empty($charset)) {
00203             $charset = Configure::read('App.encoding');
00204         }
00205         if (empty($charset)) {
00206             $charset = 'UTF-8';
00207         }
00208         return htmlspecialchars($text, ENT_QUOTES, $charset);
00209     }
00210 /**
00211  * Returns an array of all the given parameters.
00212  *
00213  * Example:
00214  * <code>
00215  * a('a', 'b')
00216  * </code>
00217  *
00218  * Would return:
00219  * <code>
00220  * array('a', 'b')
00221  * </code>
00222  *
00223  * @return array Array of given parameters
00224  */
00225     function a() {
00226         $args = func_get_args();
00227         return $args;
00228     }
00229 /**
00230  * Constructs associative array from pairs of arguments.
00231  *
00232  * Example:
00233  * <code>
00234  * aa('a','b')
00235  * </code>
00236  *
00237  * Would return:
00238  * <code>
00239  * array('a'=>'b')
00240  * </code>
00241  *
00242  * @return array Associative array
00243  */
00244     function aa() {
00245         $args = func_get_args();
00246         for ($l = 0, $c = count($args); $l < $c; $l++) {
00247             if ($l + 1 < count($args)) {
00248                 $a[$args[$l]] = $args[$l + 1];
00249             } else {
00250                 $a[$args[$l]] = null;
00251             }
00252             $l++;
00253         }
00254         return $a;
00255     }
00256 /**
00257  * Convenience method for echo().
00258  *
00259  * @param string $text String to echo
00260  */
00261     function e($text) {
00262         echo $text;
00263     }
00264 /**
00265  * Convenience method for strtolower().
00266  *
00267  * @param string $str String to lowercase
00268  * @return string Lowercased string
00269  */
00270     function low($str) {
00271         return strtolower($str);
00272     }
00273 /**
00274  * Convenience method for strtoupper().
00275  *
00276  * @param string $str String to uppercase
00277  * @return string Uppercased string
00278  */
00279     function up($str) {
00280         return strtoupper($str);
00281     }
00282 /**
00283  * Convenience method for str_replace().
00284  *
00285  * @param string $search String to be replaced
00286  * @param string $replace String to insert
00287  * @param string $subject String to search
00288  * @return string Replaced string
00289  */
00290     function r($search, $replace, $subject) {
00291         return str_replace($search, $replace, $subject);
00292     }
00293 /**
00294  * Print_r convenience function, which prints out <PRE> tags around
00295  * the output of given array. Similar to debug().
00296  *
00297  * @see debug()
00298  * @param array $var Variable to print out
00299  * @param boolean $showFrom If set to true, the method prints from where the function was called
00300  */
00301     function pr($var) {
00302         if (Configure::read() > 0) {
00303             echo "<pre>";
00304             print_r($var);
00305             echo "</pre>";
00306         }
00307     }
00308 /**
00309  * Display parameter
00310  *
00311  * @param mixed $p Parameter as string or array
00312  * @return string
00313  */
00314     function params($p) {
00315         if (!is_array($p) || count($p) == 0) {
00316             return null;
00317         } else {
00318             if (is_array($p[0]) && count($p) == 1) {
00319                 return $p[0];
00320             } else {
00321                 return $p;
00322             }
00323         }
00324     }
00325 /**
00326  * Merge a group of arrays
00327  *
00328  * @param array First array
00329  * @param array Second array
00330  * @param array Third array
00331  * @param array Etc...
00332  * @return array All array parameters merged into one
00333  */
00334     function am() {
00335         $r = array();
00336         foreach (func_get_args()as $a) {
00337             if (!is_array($a)) {
00338                 $a = array($a);
00339             }
00340             $r = array_merge($r, $a);
00341         }
00342         return $r;
00343     }
00344 /**
00345  * Gets an environment variable from available sources, and provides emulation
00346  * for unsupported or inconsisten environment variables (i.e. DOCUMENT_ROOT on
00347  * IIS, or SCRIPT_NAME in CGI mode).  Also exposes some additional custom
00348  * environment information.
00349  *
00350  * @param  string $key Environment variable name.
00351  * @return string Environment variable setting.
00352  */
00353     function env($key) {
00354         if ($key == 'HTTPS') {
00355             if (isset($_SERVER) && !empty($_SERVER)) {
00356                 return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
00357             } else {
00358                 return (strpos(env('SCRIPT_URI'), 'https://') === 0);
00359             }
00360         }
00361 
00362         if ($key == 'SCRIPT_NAME') {
00363             if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
00364                 $key = 'SCRIPT_URL';
00365             }
00366         }
00367 
00368         $val = null;
00369         if (isset($_SERVER[$key])) {
00370             $val = $_SERVER[$key];
00371         } elseif (isset($_ENV[$key])) {
00372             $val = $_ENV[$key];
00373         } elseif (getenv($key) !== false) {
00374             $val = getenv($key);
00375         }
00376 
00377         if ($key == 'REMOTE_ADDR' && $val == env('SERVER_ADDR')) {
00378             $addr = env('HTTP_PC_REMOTE_ADDR');
00379             if ($addr != null) {
00380                 $val = $addr;
00381             }
00382         }
00383 
00384         if ($val !== null) {
00385             return $val;
00386         }
00387 
00388         switch ($key) {
00389             case 'SCRIPT_FILENAME':
00390                 if (defined('SERVER_IIS') && SERVER_IIS === true){
00391                     return str_replace('\\\\', '\\', env('PATH_TRANSLATED') );
00392                 }
00393             break;
00394             case 'DOCUMENT_ROOT':
00395                 $offset = 0;
00396                 if (!strpos(env('SCRIPT_NAME'), '.php')) {
00397                     $offset = 4;
00398                 }
00399                 return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
00400             break;
00401             case 'PHP_SELF':
00402                 return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
00403             break;
00404             case 'CGI_MODE':
00405                 return (substr(php_sapi_name(), 0, 3) == 'cgi');
00406             break;
00407             case 'HTTP_BASE':
00408                 return preg_replace ('/^([^.])*/i', null, env('HTTP_HOST'));
00409             break;
00410         }
00411         return null;
00412     }
00413     if (!function_exists('file_put_contents')) {
00414 /**
00415  * Writes data into file.
00416  *
00417  * If file exists, it will be overwritten. If data is an array, it will be join()ed with an empty string.
00418  *
00419  * @param string $fileName File name.
00420  * @param mixed  $data String or array.
00421  * @return boolean Success
00422  */
00423         function file_put_contents($fileName, $data) {
00424             if (is_array($data)) {
00425                 $data = join('', $data);
00426             }
00427             $res = @fopen($fileName, 'w+b');
00428             if ($res) {
00429                 $write = @fwrite($res, $data);
00430                 if ($write === false) {
00431                     return false;
00432                 } else {
00433                     @fclose($res);
00434                     return $write;
00435                 }
00436             }
00437             return false;
00438         }
00439     }
00440 /**
00441  * Reads/writes temporary data to cache files or session.
00442  *
00443  * @param  string $path File path within /tmp to save the file.
00444  * @param  mixed  $data The data to save to the temporary file.
00445  * @param  mixed  $expires A valid strtotime string when the data expires.
00446  * @param  string $target  The target of the cached data; either 'cache' or 'public'.
00447  * @return mixed  The contents of the temporary file.
00448  * @deprecated Please use Cache::write() instead
00449  */
00450     function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
00451         if (Configure::read('Cache.disable')) {
00452             return null;
00453         }
00454         $now = time();
00455 
00456         if (!is_numeric($expires)) {
00457             $expires = strtotime($expires, $now);
00458         }
00459 
00460         switch(low($target)) {
00461             case 'cache':
00462                 $filename = CACHE . $path;
00463             break;
00464             case 'public':
00465                 $filename = WWW_ROOT . $path;
00466             break;
00467             case 'tmp':
00468                 $filename = TMP . $path;
00469             break;
00470         }
00471         $timediff = $expires - $now;
00472         $filetime = false;
00473 
00474         if (file_exists($filename)) {
00475             $filetime = @filemtime($filename);
00476         }
00477 
00478         if ($data === null) {
00479             if (file_exists($filename) && $filetime !== false) {
00480                 if ($filetime + $timediff < $now) {
00481                     @unlink($filename);
00482                 } else {
00483                     $data = @file_get_contents($filename);
00484                 }
00485             }
00486         } elseif (is_writable(dirname($filename))) {
00487             @file_put_contents($filename, $data);
00488         }
00489         return $data;
00490     }
00491 /**
00492  * Used to delete files in the cache directories, or clear contents of cache directories
00493  *
00494  * @param mixed $params As String name to be searched for deletion, if name is a directory all files in directory will be deleted.
00495  *              If array, names to be searched for deletion.
00496  *              If clearCache() without params, all files in app/tmp/cache/views will be deleted
00497  *
00498  * @param string $type Directory in tmp/cache defaults to view directory
00499  * @param string $ext The file extension you are deleting
00500  * @return true if files found and deleted false otherwise
00501  */
00502     function clearCache($params = null, $type = 'views', $ext = '.php') {
00503         if (is_string($params) || $params === null) {
00504             $params = preg_replace('/\/\//', '/', $params);
00505             $cache = CACHE . $type . DS . $params;
00506 
00507             if (is_file($cache . $ext)) {
00508                 @unlink($cache . $ext);
00509                 return true;
00510             } elseif (is_dir($cache)) {
00511                 $files = glob("$cache*");
00512 
00513                 if ($files === false) {
00514                     return false;
00515                 }
00516 
00517                 foreach ($files as $file) {
00518                     if (is_file($file)) {
00519                         @unlink($file);
00520                     }
00521                 }
00522                 return true;
00523             } else {
00524                 $cache = CACHE . $type . DS . '*' . $params . $ext;
00525                 $files = glob($cache);
00526 
00527                 $cache = CACHE . $type . DS . '*' . $params . '_*' . $ext;
00528                                 
00529                 if ($files === false) {
00530                     return false;
00531                 }
00532                 
00533                 $files = array_merge($files, glob($cache));             
00534 
00535                 foreach ($files as $file) {
00536                     if (is_file($file)) {
00537                         @unlink($file);
00538                     }
00539                 }
00540                 return true;
00541             }
00542         } elseif (is_array($params)) {
00543             foreach ($params as $key => $file) {
00544                 clearCache($file, $type, $ext);
00545             }
00546             return true;
00547         }
00548         return false;
00549     }
00550 /**
00551  * Recursively strips slashes from all values in an array
00552  *
00553  * @param array $value Array of values to strip slashes
00554  * @return mixed What is returned from calling stripslashes
00555  */
00556     function stripslashes_deep($value) {
00557         if (is_array($value)) {
00558             $return = array_map('stripslashes_deep', $value);
00559             return $return;
00560         } else {
00561             $return = stripslashes($value);
00562             return $return ;
00563         }
00564     }
00565 /**
00566  * Returns a translated string if one is found, or the submitted message if not found.
00567  *
00568  * @param string $singular Text to translate
00569  * @param boolean $return Set to true to return translated string, or false to echo
00570  * @return mixed translated string if $return is false string will be echoed
00571  */
00572     function __($singular, $return = false) {
00573         if (!$singular) {
00574             return;
00575         }
00576         if (!class_exists('I18n')) {
00577             App::import('Core', 'i18n');
00578         }
00579 
00580         if ($return === false) {
00581             echo I18n::translate($singular);
00582         } else {
00583             return I18n::translate($singular);
00584         }
00585     }
00586 /**
00587  * Returns correct plural form of message identified by $singular and $plural for count $count.
00588  * Some languages have more than one form for plural messages dependent on the count.
00589  *
00590  * @param string $singular Singular text to translate
00591  * @param string $plural Plural text
00592  * @param integer $count Count
00593  * @param boolean $return true to return, false to echo
00594  * @return mixed plural form of translated string if $return is false string will be echoed
00595  */
00596     function __n($singular, $plural, $count, $return = false) {
00597         if (!$singular) {
00598             return;
00599         }
00600         if (!class_exists('I18n')) {
00601             App::import('Core', 'i18n');
00602         }
00603 
00604         if ($return === false) {
00605             echo I18n::translate($singular, $plural, null, 5, $count);
00606         } else {
00607             return I18n::translate($singular, $plural, null, 5, $count);
00608         }
00609     }
00610 /**
00611  * Allows you to override the current domain for a single message lookup.
00612  *
00613  * @param string $domain Domain
00614  * @param string $msg String to translate
00615  * @param string $return true to return, false to echo
00616  * @return translated string if $return is false string will be echoed
00617  */
00618     function __d($domain, $msg, $return = false) {
00619         if (!$msg) {
00620             return;
00621         }
00622         if (!class_exists('I18n')) {
00623             App::import('Core', 'i18n');
00624         }
00625 
00626         if ($return === false) {
00627             echo I18n::translate($msg, null, $domain);
00628         } else {
00629             return I18n::translate($msg, null, $domain);
00630         }
00631     }
00632 /**
00633  * Allows you to override the current domain for a single plural message lookup
00634  * Returns correct plural form of message identified by $singular and $plural for count $count
00635  * from domain $domain
00636  *
00637  * @param string $domain Domain
00638  * @param string $singular Singular string to translate
00639  * @param string $plural Plural
00640  * @param integer $count Count
00641  * @param boolean $return true to return, false to echo
00642  * @return plural form of translated string if $return is false string will be echoed
00643  */
00644     function __dn($domain, $singular, $plural, $count, $return = false) {
00645         if (!$singular) {
00646             return;
00647         }
00648         if (!class_exists('I18n')) {
00649             App::import('Core', 'i18n');
00650         }
00651 
00652         if ($return === false) {
00653             echo I18n::translate($singular, $plural, $domain, 5, $count);
00654         } else {
00655             return I18n::translate($singular, $plural, $domain, 5, $count);
00656         }
00657     }
00658 /**
00659  * Allows you to override the current domain for a single message lookup.
00660  * It also allows you to specify a category.
00661  *
00662  * The category argument allows a specific category of the locale settings to be used for fetching a message.
00663  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
00664  *
00665  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
00666  * LC_CTYPE     0
00667  * LC_NUMERIC   1
00668  * LC_TIME      2
00669  * LC_COLLATE   3
00670  * LC_MONETARY  4
00671  * LC_MESSAGES  5
00672  * LC_ALL       6
00673  *
00674  * @param string $domain Domain
00675  * @param string $msg Message to translate
00676  * @param integer $category Category
00677  * @param boolean $return true to return, false to echo
00678  * @return translated string if $return is false string will be echoed
00679  */
00680     function __dc($domain, $msg, $category, $return = false) {
00681         if (!$msg) {
00682             return;
00683         }
00684         if (!class_exists('I18n')) {
00685             App::import('Core', 'i18n');
00686         }
00687 
00688         if ($return === false) {
00689             echo I18n::translate($msg, null, $domain, $category);
00690         } else {
00691             return I18n::translate($msg, null, $domain, $category);
00692         }
00693     }
00694 /**
00695  * Allows you to override the current domain for a single plural message lookup.
00696  * It also allows you to specify a category.
00697  * Returns correct plural form of message identified by $singular and $plural for count $count
00698  * from domain $domain
00699  *
00700  * The category argument allows a specific category of the locale settings to be used for fetching a message.
00701  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
00702  *
00703  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
00704  * LC_CTYPE     0
00705  * LC_NUMERIC   1
00706  * LC_TIME      2
00707  * LC_COLLATE   3
00708  * LC_MONETARY  4
00709  * LC_MESSAGES  5
00710  * LC_ALL       6
00711  *
00712  * @param string $domain Domain
00713  * @param string $singular Singular string to translate
00714  * @param string $plural Plural
00715  * @param integer $count Count
00716  * @param integer $category Category
00717  * @param boolean $return true to return, false to echo
00718  * @return plural form of translated string if $return is false string will be echoed
00719  */
00720     function __dcn($domain, $singular, $plural, $count, $category, $return = false) {
00721         if (!$singular) {
00722             return;
00723         }
00724         if (!class_exists('I18n')) {
00725             App::import('Core', 'i18n');
00726         }
00727 
00728         if ($return === false) {
00729             echo I18n::translate($singular, $plural, $domain, $category, $count);
00730         } else {
00731             return I18n::translate($singular, $plural, $domain, $category, $count);
00732         }
00733     }
00734 /**
00735  * The category argument allows a specific category of the locale settings to be used for fetching a message.
00736  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
00737  *
00738  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
00739  * LC_CTYPE     0
00740  * LC_NUMERIC   1
00741  * LC_TIME      2
00742  * LC_COLLATE   3
00743  * LC_MONETARY  4
00744  * LC_MESSAGES  5
00745  * LC_ALL       6
00746  *
00747  * @param string $msg String to translate
00748  * @param integer $category Category
00749  * @param string $return true to return, false to echo
00750  * @return translated string if $return is false string will be echoed
00751  */
00752     function __c($msg, $category, $return = false) {
00753         if (!$msg) {
00754             return;
00755         }
00756         if (!class_exists('I18n')) {
00757             App::import('Core', 'i18n');
00758         }
00759 
00760         if ($return === false) {
00761             echo I18n::translate($msg, null, null, $category);
00762         } else {
00763             return I18n::translate($msg, null, null, $category);
00764         }
00765     }
00766 /**
00767  * Computes the difference of arrays using keys for comparison
00768  *
00769  * @param array First array
00770  * @param array Second array
00771  * @return array Array with different keys
00772  */
00773     if (!function_exists('array_diff_key')) {
00774         function array_diff_key() {
00775             $valuesDiff = array();
00776 
00777             if (func_num_args() < 2) {
00778                 return false;
00779             }
00780 
00781             foreach (func_get_args() as $param) {
00782                 if (!is_array($param)) {
00783                     return false;
00784                 }
00785             }
00786 
00787             $args = func_get_args();
00788             foreach ($args[0] as $valueKey => $valueData) {
00789                 for ($i = 1; $i < func_num_args(); $i++) {
00790                     if (isset($args[$i][$valueKey])) {
00791                         continue 2;
00792                     }
00793                 }
00794                 $valuesDiff[$valueKey] = $valueData;
00795             }
00796             return $valuesDiff;
00797         }
00798     }
00799 /**
00800  * Computes the intersection of arrays using keys for comparison
00801  *
00802  * @param array First array
00803  * @param array Second array
00804  * @return array Array with interesected keys
00805  */
00806     if (!function_exists('array_intersect_key')) {
00807         function array_intersect_key($arr1, $arr2) {
00808             $res = array();
00809             foreach ($arr1 as $key=>$value) {
00810                 if (array_key_exists($key, $arr2)) {
00811                     $res[$key] = $arr1[$key];
00812                 }
00813             }
00814             return $res;
00815         }
00816     }
00817 /**
00818  * Shortcut to Log::write.
00819  *
00820  * @param string $message Message to write to log
00821  */
00822     function LogError($message) {
00823         if (!class_exists('CakeLog')) {
00824             App::import('Core', 'CakeLog');
00825         }
00826         $bad = array("\n", "\r", "\t");
00827         $good = ' ';
00828         CakeLog::write('error', str_replace($bad, $good, $message));
00829     }
00830 /**
00831  * Searches include path for files
00832  *
00833  * @param string $file File to look for
00834  * @return Full path to file if exists, otherwise false
00835  */
00836     function fileExistsInPath($file) {
00837         $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
00838         foreach ($paths as $path) {
00839             $fullPath = $path . DIRECTORY_SEPARATOR . $file;
00840 
00841             if (file_exists($fullPath)) {
00842                 return $fullPath;
00843             } elseif (file_exists($file)) {
00844                 return $file;
00845             }
00846         }
00847         return false;
00848     }
00849 /**
00850  * Convert forward slashes to underscores and removes first and last underscores in a string
00851  *
00852  * @param string String to convert
00853  * @return string with underscore remove from start and end of string
00854  */
00855     function convertSlash($string) {
00856         $string = trim($string,"/");
00857         $string = preg_replace('/\/\//', '/', $string);
00858         $string = str_replace('/', '_', $string);
00859         return $string;
00860     }
00861 /**
00862  * Implements http_build_query for PHP4.
00863  *
00864  * @param string $data Data to set in query string
00865  * @param string $prefix If numeric indices, prepend this to index for elements in base array.
00866  * @param string $argSep String used to separate arguments
00867  * @param string $baseKey Base key
00868  * @return string URL encoded query string
00869  * @see http://php.net/http_build_query
00870  */
00871     if (!function_exists('http_build_query')) {
00872         function http_build_query($data, $prefix = null, $argSep = null, $baseKey = null) {
00873             if (empty($argSep)) {
00874                 $argSep = ini_get('arg_separator.output');
00875             }
00876             if (is_object($data)) {
00877                 $data = get_object_vars($data);
00878             }
00879             $out = array();
00880 
00881             foreach ((array)$data as $key => $v) {
00882                 if (is_numeric($key) && !empty($prefix)) {
00883                     $key = $prefix . $key;
00884                 }
00885                 $key = urlencode($key);
00886 
00887                 if (!empty($baseKey)) {
00888                     $key = $baseKey . '[' . $key . ']';
00889                 }
00890 
00891                 if (is_array($v) || is_object($v)) {
00892                     $out[] = http_build_query($v, $prefix, $argSep, $key);
00893                 } else {
00894                     $out[] = $key . '=' . urlencode($v);
00895                 }
00896             }
00897             return implode($argSep, $out);
00898         }
00899     }
00900 /**
00901  * Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2.
00902  * Don't use for isset() conditions, or wrap your variable with @ operator:
00903  * Example:
00904  * <code>
00905  * ife(isset($variable), @$variable, 'default');
00906  * </code>
00907  *
00908  * @param mixed $condition Conditional expression
00909  * @param mixed $val1 Value to return in case condition matches
00910  * @param mixed $val2 Value to return if condition doesn't match
00911  * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
00912  */
00913     function ife($condition, $val1 = null, $val2 = null) {
00914         if (!empty($condition)) {
00915             return $val1;
00916         }
00917         return $val2;
00918     }
00919 /**
00920  * @deprecated
00921  * @see App::import('View', 'ViewName');
00922  */
00923     function loadView($name) {
00924         trigger_error('loadView is deprecated see App::import(\'View\', \'ViewName\');', E_USER_WARNING);
00925         return App::import('View', $name);
00926     }
00927 /**
00928  * @deprecated
00929  * @see App::import('Model', 'ModelName');
00930  */
00931     function loadModel($name = null) {
00932         trigger_error('loadModel is deprecated see App::import(\'Model\', \'ModelName\');', E_USER_WARNING);
00933         return App::import('Model', $name);
00934     }
00935 /**
00936  * @deprecated
00937  * @see App::import('Controller', 'ControllerName');
00938  */
00939     function loadController($name) {
00940         trigger_error('loadController is deprecated see App::import(\'Controller\', \'ControllerName\');', E_USER_WARNING);
00941         return App::import('Controller', $name);
00942     }
00943 /**
00944  * @deprecated
00945  * @see App::import('Helper', 'HelperName');
00946  */
00947     function loadHelper($name) {
00948         trigger_error('loadHelper is deprecated see App::import(\'Helper\', \'PluginName.HelperName\');', E_USER_WARNING);
00949         return App::import('Helper', $name);
00950     }
00951 /**
00952  * @deprecated
00953  * @see App::import('Helper', 'PluginName.HelperName');
00954  */
00955     function loadPluginHelper($plugin, $helper) {
00956         trigger_error('loadPluginHelper is deprecated see App::import(\'Helper\', \'PluginName.HelperName\');', E_USER_WARNING);
00957         return App::import('Helper', $plugin . '.' . $helper);
00958     }
00959 /**
00960  * @deprecated
00961  * @see App::import('Component', 'ComponentName');
00962  */
00963     function loadComponent($name) {
00964         trigger_error('loadComponent is deprecated see App::import(\'Component\', \'ComponentName\');', E_USER_WARNING);
00965         return App::import('Component', $name);
00966     }
00967 /**
00968  * @deprecated
00969  * @see App::import('Component', 'PluginName.ComponentName');
00970  */
00971     function loadPluginComponent($plugin, $component) {
00972         trigger_error('loadPluginComponent is deprecated see App::import(\'Component\', \'PluginName.ComponentName\');', E_USER_WARNING);
00973         return App::import('Component', $plugin . '.' . $component);
00974     }
00975 /**
00976  * @deprecated
00977  * @see App::import('Behavior', 'BehaviorrName');
00978  */
00979     function loadBehavior($name) {
00980         trigger_error('loadBehavior is deprecated see App::import(\'Behavior\', $name);', E_USER_WARNING);
00981         return App::import('Behavior', $name);
00982     }
00983 /**
00984  * @deprecated
00985  * @see $model = Configure::listObjects('model'); and App::import('Model', $models);
00986  *      or App::import('Model', array(List of Models));
00987  */
00988     function loadModels() {
00989         $loadModels = array();
00990         if (func_num_args() > 0) {
00991             $args = func_get_args();
00992             foreach($args as $arg) {
00993                 if (is_array($arg)) {
00994                     $loadModels = am($loadModels, $arg);
00995                 } else {
00996                     $loadModels[] = $arg;
00997                 }
00998             }
00999         }
01000 
01001         if (empty($loadModels)) {
01002             $loadModels = Configure::listObjects('model');
01003         }
01004         App::import('Model', $loadModels);
01005         trigger_error('loadModels is deprecated see $model = Configure::listObjects(\'model\'); and App::import(\'Model\', $models);', E_USER_WARNING);
01006         return $loadModels;
01007     }
01008 /**
01009  * @deprecated
01010  * @see App::import('Model', 'PluginName.PluginModel');
01011  */
01012     function loadPluginModels($plugin) {
01013         if (!class_exists('AppModel')) {
01014             loadModel();
01015         }
01016         $plugin = Inflector::underscore($plugin);
01017         $pluginAppModel = Inflector::camelize($plugin . '_app_model');
01018         $pluginAppModelFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_model.php';
01019 
01020         if (!class_exists($pluginAppModel)) {
01021             if (file_exists($pluginAppModelFile)) {
01022                 require($pluginAppModelFile);
01023                 Overloadable::overload($pluginAppModel);
01024             }
01025         }
01026 
01027         $pluginModelDir = APP . 'plugins' . DS . $plugin . DS . 'models' . DS;
01028         if (is_dir($pluginModelDir)) {
01029             foreach (listClasses($pluginModelDir)as $modelFileName) {
01030                 list($name) = explode('.', $modelFileName);
01031                 $className = Inflector::camelize($name);
01032 
01033                 if (!class_exists($className)) {
01034                     require($pluginModelDir . $modelFileName);
01035                     Overloadable::overload($className);
01036                 }
01037             }
01038         }
01039         trigger_error('loadPluginModels is deprecated see App::import(\'Model\', \'PluginName.PluginModel\');', E_USER_WARNING);
01040     }
01041 /**
01042  * @deprecated
01043  * @see $controllers = Configure::listObjects('controller'); and App::import('Controller', $controllers);
01044  *      or App::import('Controller', array(List of Controllers);
01045  */
01046     function loadControllers() {
01047         $loadControllers = array();
01048         if (func_num_args() > 0) {
01049             $args = func_get_args();
01050             foreach($args as $arg) {
01051                 if (is_array($arg)) {
01052                     $loadControllers = am($loadControllers, $arg);
01053                 } else {
01054                     $loadControllers[] = $arg;
01055                 }
01056             }
01057         }
01058 
01059         if (empty($loadControllers)) {
01060             $loadControllers = Configure::listObjects('controller');
01061         }
01062         App::import('Controller', $loadControllers);
01063         trigger_error('loadControllers is deprecated see $controllers = Configure::listObjects(\'controller\'); and App::import(\'Controller\', $controllers);', E_USER_WARNING);
01064         return $loadControllers;
01065     }
01066 /**
01067  * @deprecated
01068  * @see Configure::listObjects('file', $path);
01069  */
01070     function listClasses($path ) {
01071         trigger_error('listClasses is deprecated see Configure::listObjects(\'file\', $path);', E_USER_WARNING);
01072         return Configure::listObjects('file', $path);
01073     }
01074 /**
01075  * @deprecated
01076  * @see Configure::corePaths();
01077  */
01078     function paths() {
01079         $directories = Configure::getInstance();
01080         $paths = array();
01081 
01082         foreach ($directories->modelPaths as $path) {
01083             $paths['Models'][] = $path;
01084         }
01085         foreach ($directories->behaviorPaths as $path) {
01086             $paths['Behaviors'][] = $path;
01087         }
01088         foreach ($directories->controllerPaths as $path) {
01089             $paths['Controllers'][] = $path;
01090         }
01091         foreach ($directories->componentPaths as $path) {
01092             $paths['Components'][] = $path;
01093         }
01094         foreach ($directories->helperPaths as $path) {
01095             $paths['Helpers'][] = $path;
01096         }
01097 
01098         if (!class_exists('Folder')) {
01099             App::import('Core', 'Folder');
01100         }
01101 
01102         $folder =& new Folder(APP.'plugins'.DS);
01103         $plugins = $folder->ls();
01104         $classPaths = array('models', 'models'.DS.'behaviors',  'controllers', 'controllers'.DS.'components', 'views'.DS.'helpers');
01105 
01106         foreach ($plugins[0] as $plugin) {
01107             foreach ($classPaths as $path) {
01108                 if (strpos($path, DS) !== false) {
01109                     $key = explode(DS, $path);
01110                     $key = $key[1];
01111                 } else {
01112                     $key = $path;
01113                 }
01114                 $folder->path = APP.'plugins'.DS.$plugin.DS.$path;
01115                 $paths[Inflector::camelize($plugin)][Inflector::camelize($key)][] = $folder->path;
01116             }
01117         }
01118         return $paths;
01119     }
01120 /**
01121  * @deprecated
01122  */
01123     function vendor() {
01124         trigger_error('(vendor) Deprecated, see App::import(\'Vendor\', \'...\');', E_USER_WARNING);
01125         $args = func_get_args();
01126         $c = func_num_args();
01127 
01128         for ($i = 0; $i < $c; $i++) {
01129             $arg = $args[$i];
01130 
01131             if (strpos($arg, '.') !== false) {
01132                 $file = explode('.', $arg);
01133                 $plugin = Inflector::underscore($file[0]);
01134                 unset($file[0]);
01135                 $file = implode('.', $file);
01136                 if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'vendors' . DS . $file . '.php')) {
01137                     require_once(APP . 'plugins' . DS . $plugin . DS . 'vendors' . DS . $file . '.php');
01138                     continue;
01139                 }
01140             }
01141 
01142             if (file_exists(APP . 'vendors' . DS . $arg . '.php')) {
01143                 require_once(APP . 'vendors' . DS . $arg . '.php');
01144             } elseif (file_exists(VENDORS . $arg . '.php')) {
01145                 require_once(VENDORS . $arg . '.php');
01146             } else {
01147                 return false;
01148             }
01149         }
01150         return true;
01151     }
01152 /**
01153  * @deprecated
01154  * @see Dispatcher::uri();
01155  */
01156     function setUri() {
01157         return null;
01158     }
01159 /**
01160  * @deprecated
01161  * @see Dispatcher::getUrl();
01162  */
01163     function setUrl() {
01164         return null;
01165     }
01166 ?>