basics.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: basics.php 8281 2009-08-03 19:24:38Z mark_story $ */
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. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake
00021  * @since         CakePHP(tm) v 0.2.9
00022  * @version       $Revision: 8281 $
00023  * @modifiedby    $LastChangedBy: mark_story $
00024  * @lastmodified  $Date: 2009-08-03 15:24:38 -0400 (Mon, 03 Aug 2009) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Basic defines for timing functions.
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  * Patch for PHP < 5.0
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  * Loads configuration files. Receives a set of configuration files
00051  * to load.
00052  * Example:
00053  * <code>
00054  * config('config1', 'config2');
00055  * </code>
00056  *
00057  * @return boolean Success
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  * Loads component/components from LIBS. Takes optional number of parameters.
00080  *
00081  * Example:
00082  * <code>
00083  * uses('flay', 'time');
00084  * </code>
00085  *
00086  * @param string $name Filename without the .php part
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  * Prints out debug information about given variable.
00096  *
00097  * Only runs if debug level is greater than zero.
00098  *
00099  * @param boolean $var Variable to show debug information for.
00100  * @param boolean $showHtml If set to true, the method prints the debug data in a screen-friendly way.
00101  * @param boolean $showFrom If set to true, the method prints from where the function was called.
00102  * @link http://book.cakephp.org/view/458/Basic-Debugging
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('<', '&lt;', str_replace('>', '&gt;', $var));
00116             }
00117             echo $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 backwards 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         $output = array();
00184 
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  * @link http://book.cakephp.org/view/703/h
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  * Returns an array of all the given parameters.
00213  *
00214  * Example:
00215  * <code>
00216  * a('a', 'b')
00217  * </code>
00218  *
00219  * Would return:
00220  * <code>
00221  * array('a', 'b')
00222  * </code>
00223  *
00224  * @return array Array of given parameters
00225  * @link http://book.cakephp.org/view/694/a
00226  */
00227     function a() {
00228         $args = func_get_args();
00229         return $args;
00230     }
00231 /**
00232  * Constructs associative array from pairs of arguments.
00233  *
00234  * Example:
00235  * <code>
00236  * aa('a','b')
00237  * </code>
00238  *
00239  * Would return:
00240  * <code>
00241  * array('a'=>'b')
00242  * </code>
00243  *
00244  * @return array Associative array
00245  * @link http://book.cakephp.org/view/695/aa
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  * Convenience method for echo().
00262  *
00263  * @param string $text String to echo
00264  * @link http://book.cakephp.org/view/700/e
00265  */
00266     function e($text) {
00267         echo $text;
00268     }
00269 /**
00270  * Convenience method for strtolower().
00271  *
00272  * @param string $str String to lowercase
00273  * @return string Lowercased string
00274  * @link http://book.cakephp.org/view/705/low
00275  */
00276     function low($str) {
00277         return strtolower($str);
00278     }
00279 /**
00280  * Convenience method for strtoupper().
00281  *
00282  * @param string $str String to uppercase
00283  * @return string Uppercased string
00284  * @link http://book.cakephp.org/view/710/up
00285  */
00286     function up($str) {
00287         return strtoupper($str);
00288     }
00289 /**
00290  * Convenience method for str_replace().
00291  *
00292  * @param string $search String to be replaced
00293  * @param string $replace String to insert
00294  * @param string $subject String to search
00295  * @return string Replaced string
00296  * @link http://book.cakephp.org/view/708/r
00297  */
00298     function r($search, $replace, $subject) {
00299         return str_replace($search, $replace, $subject);
00300     }
00301 /**
00302  * Print_r convenience function, which prints out <PRE> tags around
00303  * the output of given array. Similar to debug().
00304  *
00305  * @see debug()
00306  * @param array $var Variable to print out
00307  * @param boolean $showFrom If set to true, the method prints from where the function was called
00308  * @link http://book.cakephp.org/view/707/pr
00309  */
00310     function pr($var) {
00311         if (Configure::read() > 0) {
00312             echo '<pre>';
00313             print_r($var);
00314             echo '</pre>';
00315         }
00316     }
00317 /**
00318  * Display parameters.
00319  *
00320  * @param mixed $p Parameter as string or array
00321  * @return string
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  * Merge a group of arrays
00334  *
00335  * @param array First array
00336  * @param array Second array
00337  * @param array Third array
00338  * @param array Etc...
00339  * @return array All array parameters merged into one
00340  * @link http://book.cakephp.org/view/696/am
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  * Gets an environment variable from available sources, and provides emulation
00355  * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
00356  * IIS, or SCRIPT_NAME in CGI mode).  Also exposes some additional custom
00357  * environment information.
00358  *
00359  * @param  string $key Environment variable name.
00360  * @return string Environment variable setting.
00361  * @link http://book.cakephp.org/view/701/env
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://') === 0);
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  * Writes data into file.
00431  *
00432  * If file exists, it will be overwritten. If data is an array, it will be join()ed with an empty string.
00433  *
00434  * @param string $fileName File name.
00435  * @param mixed  $data String or array.
00436  * @return boolean Success
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  * Reads/writes temporary data to cache files or session.
00458  *
00459  * @param  string $path File path within /tmp to save the file.
00460  * @param  mixed  $data The data to save to the temporary file.
00461  * @param  mixed  $expires A valid strtotime string when the data expires.
00462  * @param  string $target  The target of the cached data; either 'cache' or 'public'.
00463  * @return mixed  The contents of the temporary file.
00464  * @deprecated Please use Cache::write() instead
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  * Used to delete files in the cache directories, or clear contents of cache directories
00509  *
00510  * @param mixed $params As String name to be searched for deletion, if name is a directory all files in directory will be deleted.
00511  *              If array, names to be searched for deletion.
00512  *              If clearCache() without params, all files in app/tmp/cache/views will be deleted
00513  *
00514  * @param string $type Directory in tmp/cache defaults to view directory
00515  * @param string $ext The file extension you are deleting
00516  * @return true if files found and deleted false otherwise
00517  */
00518     function clearCache($params = null, $type = 'views', $ext = '.php') {
00519         if (is_string($params) || $params === null) {
00520             $params = preg_replace('/\/\//', '/', $params);
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  * Recursively strips slashes from all values in an array
00571  *
00572  * @param array $values Array of values to strip slashes
00573  * @return mixed What is returned from calling stripslashes
00574  * @link http://book.cakephp.org/view/709/stripslashes_deep
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  * Returns a translated string if one is found; Otherwise, the submitted message.
00588  *
00589  * @param string $singular Text to translate
00590  * @param boolean $return Set to true to return translated string, or false to echo
00591  * @return mixed translated string if $return is false string will be echoed
00592  * @link http://book.cakephp.org/view/693/__
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  * Returns correct plural form of message identified by $singular and $plural for count $count.
00610  * Some languages have more than one form for plural messages dependent on the count.
00611  *
00612  * @param string $singular Singular text to translate
00613  * @param string $plural Plural text
00614  * @param integer $count Count
00615  * @param boolean $return true to return, false to echo
00616  * @return mixed plural form of translated string if $return is false string will be echoed
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  * Allows you to override the current domain for a single message lookup.
00634  *
00635  * @param string $domain Domain
00636  * @param string $msg String to translate
00637  * @param string $return true to return, false to echo
00638  * @return translated string if $return is false string will be echoed
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  * Allows you to override the current domain for a single plural message lookup.
00656  * Returns correct plural form of message identified by $singular and $plural for count $count
00657  * from domain $domain.
00658  *
00659  * @param string $domain Domain
00660  * @param string $singular Singular string to translate
00661  * @param string $plural Plural
00662  * @param integer $count Count
00663  * @param boolean $return true to return, false to echo
00664  * @return plural form of translated string if $return is false string will be echoed
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  * Allows you to override the current domain for a single message lookup.
00682  * It also allows you to specify a category.
00683  *
00684  * The category argument allows a specific category of the locale settings to be used for fetching a message.
00685  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
00686  *
00687  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
00688  * LC_CTYPE     0
00689  * LC_NUMERIC   1
00690  * LC_TIME      2
00691  * LC_COLLATE   3
00692  * LC_MONETARY  4
00693  * LC_MESSAGES  5
00694  * LC_ALL       6
00695  *
00696  * @param string $domain Domain
00697  * @param string $msg Message to translate
00698  * @param integer $category Category
00699  * @param boolean $return true to return, false to echo
00700  * @return translated string if $return is false string will be echoed
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  * Allows you to override the current domain for a single plural message lookup.
00718  * It also allows you to specify a category.
00719  * Returns correct plural form of message identified by $singular and $plural for count $count
00720  * from domain $domain.
00721  *
00722  * The category argument allows a specific category of the locale settings to be used for fetching a message.
00723  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
00724  *
00725  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
00726  * LC_ALL       0
00727  * LC_COLLATE   1
00728  * LC_CTYPE     2
00729  * LC_MONETARY  3
00730  * LC_NUMERIC   4
00731  * LC_TIME      5
00732  * LC_MESSAGES  6
00733  *
00734  * @param string $domain Domain
00735  * @param string $singular Singular string to translate
00736  * @param string $plural Plural
00737  * @param integer $count Count
00738  * @param integer $category Category
00739  * @param boolean $return true to return, false to echo
00740  * @return plural form of translated string if $return is false string will be echoed
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  * The category argument allows a specific category of the locale settings to be used for fetching a message.
00758  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
00759  *
00760  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
00761  * LC_CTYPE     0
00762  * LC_NUMERIC   1
00763  * LC_TIME      2
00764  * LC_COLLATE   3
00765  * LC_MONETARY  4
00766  * LC_MESSAGES  5
00767  * LC_ALL       6
00768  *
00769  * @param string $msg String to translate
00770  * @param integer $category Category
00771  * @param string $return true to return, false to echo
00772  * @return translated string if $return is false string will be echoed
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  * Computes the difference of arrays using keys for comparison.
00790  *
00791  * @param array First array
00792  * @param array Second array
00793  * @return array Array with different keys
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  * Computes the intersection of arrays using keys for comparison
00824  *
00825  * @param array First array
00826  * @param array Second array
00827  * @return array Array with interesected keys
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  * Shortcut to Log::write.
00842  *
00843  * @param string $message Message to write to log
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  * Searches include path for files.
00855  *
00856  * @param string $file File to look for
00857  * @return Full path to file if exists, otherwise false
00858  * @link http://book.cakephp.org/view/702/fileExistsInPath
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  * Convert forward slashes to underscores and removes first and last underscores in a string
00875  *
00876  * @param string String to convert
00877  * @return string with underscore remove from start and end of string
00878  * @link http://book.cakephp.org/view/697/convertSlash
00879  */
00880     function convertSlash($string) {
00881         $string = trim($string, '/');
00882         $string = preg_replace('/\/\//', '/', $string);
00883         $string = str_replace('/', '_', $string);
00884         return $string;
00885     }
00886 /**
00887  * Implements http_build_query for PHP4.
00888  *
00889  * @param string $data Data to set in query string
00890  * @param string $prefix If numeric indices, prepend this to index for elements in base array.
00891  * @param string $argSep String used to separate arguments
00892  * @param string $baseKey Base key
00893  * @return string URL encoded query string
00894  * @see http://php.net/http_build_query
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  * Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2.
00927  * Don't use for isset() conditions, or wrap your variable with @ operator:
00928  * Example:
00929  * <code>
00930  * ife(isset($variable), @$variable, 'default');
00931  * </code>
00932  *
00933  * @param mixed $condition Conditional expression
00934  * @param mixed $val1 Value to return in case condition matches
00935  * @param mixed $val2 Value to return if condition doesn't match
00936  * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
00937  * @link http://book.cakephp.org/view/704/ife
00938  */
00939     function ife($condition, $val1 = null, $val2 = null) {
00940         if (!empty($condition)) {
00941             return $val1;
00942         }
00943         return $val2;
00944     }
00945 ?>

Generated on Sun Nov 22 00:30:50 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7