basics.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: basics_8php-source.html 675 2008-12-26 00:27:14Z 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: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 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 < 4.3
00041  */
00042     if (!function_exists("ob_get_clean")) {
00043         function ob_get_clean() {
00044             $ob_contents = ob_get_contents();
00045             ob_end_clean();
00046             return $ob_contents;
00047         }
00048     }
00049 /**
00050  * Patch for PHP < 4.3
00051  */
00052     if (version_compare(phpversion(), '5.0') < 0) {
00053         eval ('
00054         function clone($object) {
00055         return $object;
00056         }');
00057     }
00058 /**
00059  * Computes the difference of arrays using keys for comparison
00060  *
00061  * @param array
00062  * @param array
00063  * @return array
00064  */
00065     if (!function_exists('array_diff_key')) {
00066         function array_diff_key() {
00067             $valuesDiff = array();
00068 
00069             if (func_num_args() < 2) {
00070                 return false;
00071             }
00072 
00073             foreach (func_get_args() as $param) {
00074                 if (!is_array($param)) {
00075                     return false;
00076                 }
00077             }
00078 
00079             $args = func_get_args();
00080             foreach ($args[0] as $valueKey => $valueData) {
00081                 for ($i = 1; $i < func_num_args(); $i++) {
00082                     if (isset($args[$i][$valueKey])) {
00083                         continue 2;
00084                     }
00085                 }
00086                 $valuesDiff[$valueKey] = $valueData;
00087             }
00088             return $valuesDiff;
00089         }
00090     }
00091 /**
00092  * Computes the intersection of arrays using keys for comparison
00093  *
00094  * @param array
00095  * @param array
00096  * @return array
00097  */
00098     if (!function_exists('array_intersect_key')) {
00099         function array_intersect_key($arr1, $arr2) {
00100             $res = array();
00101             foreach ($arr1 as $key=>$value) {
00102                 if (array_key_exists($key, $arr2)) {
00103                     $res[$key] = $arr1[$key];
00104                 }
00105             }
00106             return $res;
00107         }
00108     }
00109 /**
00110  * Loads all models.
00111  */
00112     function loadModels() {
00113         if (!class_exists('Model')) {
00114             require LIBS . 'model' . DS . 'model.php';
00115         }
00116         $path = Configure::getInstance();
00117         if (!class_exists('AppModel')) {
00118             if (file_exists(APP . 'app_model.php')) {
00119                 require(APP . 'app_model.php');
00120             } else {
00121                 require(CAKE . 'app_model.php');
00122             }
00123             if (phpversion() < 5 && function_exists("overload")) {
00124                 overload('AppModel');
00125             }
00126         }
00127 
00128         foreach ($path->modelPaths as $path) {
00129             foreach (listClasses($path)as $model_fn) {
00130                 list($name) = explode('.', $model_fn);
00131                 $className = Inflector::camelize($name);
00132 
00133                 if (!class_exists($className)) {
00134                     require($path . $model_fn);
00135 
00136                     if (phpversion() < 5 && function_exists("overload")) {
00137                         overload($className);
00138                     }
00139                 }
00140             }
00141         }
00142     }
00143 /**
00144  * Loads all plugin models.
00145  *
00146  * @param  string  $plugin Name of plugin
00147  * @return
00148  */
00149     function loadPluginModels($plugin) {
00150         if (!class_exists('AppModel')) {
00151             loadModel();
00152         }
00153         $pluginAppModel = Inflector::camelize($plugin . '_app_model');
00154         $pluginAppModelFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_model.php';
00155         if (!class_exists($pluginAppModel)) {
00156             if (file_exists($pluginAppModelFile)) {
00157                 require($pluginAppModelFile);
00158             } else {
00159                 die('Plugins must have a class named ' . $pluginAppModel);
00160             }
00161             if (phpversion() < 5 && function_exists("overload")) {
00162                 overload($pluginAppModel);
00163             }
00164         }
00165 
00166         $pluginModelDir = APP . 'plugins' . DS . $plugin . DS . 'models' . DS;
00167 
00168         foreach (listClasses($pluginModelDir)as $modelFileName) {
00169             list($name) = explode('.', $modelFileName);
00170             $className = Inflector::camelize($name);
00171 
00172             if (!class_exists($className)) {
00173                 require($pluginModelDir . $modelFileName);
00174 
00175                 if (phpversion() < 5 && function_exists("overload")) {
00176                     overload($className);
00177                 }
00178             }
00179         }
00180     }
00181 /**
00182  * Loads custom view class.
00183  *
00184  */
00185     function loadView($viewClass) {
00186         if (!class_exists($viewClass . 'View')) {
00187             $paths = Configure::getInstance();
00188             $file = Inflector::underscore($viewClass) . '.php';
00189 
00190             foreach ($paths->viewPaths as $path) {
00191                 if (file_exists($path . $file)) {
00192                     return require($path . $file);
00193                 }
00194             }
00195 
00196             if ($viewFile = fileExistsInPath(LIBS . 'view' . DS . $file)) {
00197                 if (file_exists($viewFile)) {
00198                     require($viewFile);
00199                     return true;
00200                 } else {
00201                     return false;
00202                 }
00203             }
00204         }
00205     }
00206 /**
00207  * Loads a model by CamelCase name.
00208  */
00209     function loadModel($name = null) {
00210         if (!class_exists('Model')) {
00211             require LIBS . 'model' . DS . 'model.php';
00212         }
00213         if (!class_exists('AppModel')) {
00214             if (file_exists(APP . 'app_model.php')) {
00215                 require(APP . 'app_model.php');
00216             } else {
00217                 require(CAKE . 'app_model.php');
00218             }
00219             if (phpversion() < 5 && function_exists("overload")) {
00220                 overload('AppModel');
00221             }
00222         }
00223 
00224         if (!is_null($name) && !class_exists($name)) {
00225             $className = $name;
00226             $name = Inflector::underscore($name);
00227             $paths = Configure::getInstance();
00228 
00229             foreach ($paths->modelPaths as $path) {
00230                 if (file_exists($path . $name . '.php')) {
00231                     require($path . $name . '.php');
00232                     if (phpversion() < 5 && function_exists("overload")) {
00233                         overload($className);
00234                     }
00235                     return true;
00236                 }
00237             }
00238             return false;
00239         } else {
00240             return true;
00241         }
00242     }
00243 /**
00244  * Loads all controllers.
00245  */
00246     function loadControllers() {
00247         $paths = Configure::getInstance();
00248         if (!class_exists('AppController')) {
00249             if (file_exists(APP . 'app_controller.php')) {
00250                 require(APP . 'app_controller.php');
00251             } else {
00252                 require(CAKE . 'app_controller.php');
00253             }
00254         }
00255         $loadedControllers = array();
00256 
00257         foreach ($paths->controllerPaths as $path) {
00258             foreach (listClasses($path) as $controller) {
00259                 list($name) = explode('.', $controller);
00260                 $className = Inflector::camelize(str_replace('_controller', '', $name));
00261 
00262                 if (loadController($className)) {
00263                     $loadedControllers[$controller] = $className;
00264                 }
00265             }
00266         }
00267         return $loadedControllers;
00268     }
00269 /**
00270  * Loads a controller and its helper libraries.
00271  *
00272  * @param  string  $name Name of controller
00273  * @return boolean Success
00274  */
00275     function loadController($name) {
00276         $paths = Configure::getInstance();
00277         if (!class_exists('AppController')) {
00278             if (file_exists(APP . 'app_controller.php')) {
00279                 require(APP . 'app_controller.php');
00280             } else {
00281                 require(CAKE . 'app_controller.php');
00282             }
00283         }
00284 
00285         if ($name === null) {
00286             return true;
00287         }
00288 
00289         if (!class_exists($name . 'Controller')) {
00290             $name = Inflector::underscore($name);
00291 
00292             foreach ($paths->controllerPaths as $path) {
00293                 if (file_exists($path . $name . '_controller.php')) {
00294                     require($path . $name . '_controller.php');
00295                     return true;
00296                 }
00297             }
00298 
00299             if ($controller_fn = fileExistsInPath(LIBS . 'controller' . DS . $name . '_controller.php')) {
00300                 if (file_exists($controller_fn)) {
00301                     require($controller_fn);
00302                     return true;
00303                 } else {
00304                     return false;
00305                 }
00306             }
00307         } else {
00308             return false;
00309         }
00310     }
00311 /**
00312  * Loads a plugin's controller.
00313  *
00314  * @param  string  $plugin Name of plugin
00315  * @param  string  $controller Name of controller to load
00316  * @return boolean Success
00317  */
00318     function loadPluginController($plugin, $controller) {
00319         $pluginAppController = Inflector::camelize($plugin . '_app_controller');
00320         $pluginAppControllerFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_controller.php';
00321         if (!class_exists($pluginAppController)) {
00322             if (file_exists($pluginAppControllerFile)) {
00323                 require($pluginAppControllerFile);
00324             } else {
00325                 return false;
00326             }
00327         }
00328 
00329         if (empty($controller)) {
00330             if (!class_exists($plugin . 'Controller')) {
00331                 if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
00332                     require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
00333                     return true;
00334                 }
00335             }
00336         }
00337 
00338         if (!class_exists($controller . 'Controller')) {
00339             $controller = Inflector::underscore($controller);
00340             $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $controller . '_controller.php';
00341 
00342             if (file_exists($file)) {
00343                 require($file);
00344                 return true;
00345             }  elseif (!class_exists(Inflector::camelize($plugin . '_controller'))) {
00346                 if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
00347                     require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
00348                     return true;
00349                 } else {
00350                     return false;
00351                 }
00352             }
00353         }
00354         return true;
00355     }
00356 /**
00357  * Loads a helper
00358  *
00359  * @param  string  $name Name of helper
00360  * @return boolean Success
00361  */
00362     function loadHelper($name) {
00363         $paths = Configure::getInstance();
00364 
00365         if ($name === null) {
00366             return true;
00367         }
00368 
00369         if (!class_exists($name . 'Helper')) {
00370             $name=Inflector::underscore($name);
00371 
00372             foreach ($paths->helperPaths as $path) {
00373                 if (file_exists($path . $name . '.php')) {
00374                     require($path . $name . '.php');
00375                     return true;
00376                 }
00377             }
00378 
00379             if ($helper_fn = fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $name . '.php')) {
00380                 if (file_exists($helper_fn)) {
00381                     require($helper_fn);
00382                     return true;
00383                 } else {
00384                     return false;
00385                 }
00386             }
00387         } else {
00388             return true;
00389         }
00390     }
00391 /**
00392  * Loads a plugin's helper
00393  *
00394  * @param  string  $plugin Name of plugin
00395  * @param  string  $helper Name of helper to load
00396  * @return boolean Success
00397  */
00398     function loadPluginHelper($plugin, $helper) {
00399         if (!class_exists($helper . 'Helper')) {
00400             $helper = Inflector::underscore($helper);
00401             $file = APP . 'plugins' . DS . $plugin . DS . 'views' . DS . 'helpers' . DS . $helper . '.php';
00402             if (file_exists($file)) {
00403                 require($file);
00404                 return true;
00405             } else {
00406                 return false;
00407             }
00408         } else {
00409             return true;
00410         }
00411     }
00412 /**
00413  * Loads a component
00414  *
00415  * @param  string  $name Name of component
00416  * @return boolean Success
00417  */
00418     function loadComponent($name) {
00419         $paths = Configure::getInstance();
00420 
00421         if ($name === null) {
00422             return true;
00423         }
00424 
00425         if (!class_exists($name . 'Component')) {
00426             $name=Inflector::underscore($name);
00427 
00428             foreach ($paths->componentPaths as $path) {
00429                 if (file_exists($path . $name . '.php')) {
00430                     require($path . $name . '.php');
00431                     return true;
00432                 }
00433             }
00434 
00435             if ($component_fn = fileExistsInPath(LIBS . 'controller' . DS . 'components' . DS . $name . '.php')) {
00436                 if (file_exists($component_fn)) {
00437                     require($component_fn);
00438                     return true;
00439                 } else {
00440                     return false;
00441                 }
00442             }
00443         } else {
00444             return true;
00445         }
00446     }
00447 /**
00448  * Loads a plugin's component
00449  *
00450  * @param  string  $plugin Name of plugin
00451  * @param  string  $helper Name of component to load
00452  * @return boolean Success
00453  */
00454     function loadPluginComponent($plugin, $component) {
00455         if (!class_exists($component . 'Component')) {
00456             $component = Inflector::underscore($component);
00457             $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . 'components' . DS . $component . '.php';
00458             if (file_exists($file)) {
00459                 require($file);
00460                 return true;
00461             } else {
00462                 return false;
00463             }
00464         } else {
00465             return true;
00466         }
00467     }
00468 /**
00469  * Returns an array of filenames of PHP files in given directory.
00470  *
00471  * @param  string $path Path to scan for files
00472  * @return array  List of files in directory
00473  */
00474     function listClasses($path) {
00475         $dir = opendir($path);
00476         $classes=array();
00477         while (false !== ($file = readdir($dir))) {
00478             if ((substr($file, -3, 3) == 'php') && substr($file, 0, 1) != '.') {
00479                 $classes[] = $file;
00480             }
00481         }
00482         closedir($dir);
00483         return $classes;
00484     }
00485 /**
00486  * Loads configuration files
00487  *
00488  * @return boolean Success
00489  */
00490     function config() {
00491         $args = func_get_args();
00492         foreach ($args as $arg) {
00493             if (('database' == $arg) && file_exists(CONFIGS . $arg . '.php')) {
00494                 include_once(CONFIGS . $arg . '.php');
00495             } elseif (file_exists(CONFIGS . $arg . '.php')) {
00496                 include_once(CONFIGS . $arg . '.php');
00497 
00498                 if (count($args) == 1) {
00499                     return true;
00500                 }
00501             } else {
00502                 if (count($args) == 1) {
00503                     return false;
00504                 }
00505             }
00506         }
00507         return true;
00508     }
00509 /**
00510  * Loads component/components from LIBS.
00511  *
00512  * Example:
00513  * <code>
00514  * uses('flay', 'time');
00515  * </code>
00516  *
00517  * @uses LIBS
00518  */
00519     function uses() {
00520         $args = func_get_args();
00521         foreach ($args as $arg) {
00522             require_once(LIBS . strtolower($arg) . '.php');
00523         }
00524     }
00525 /**
00526  * Require given files in the VENDORS directory. Takes optional number of parameters.
00527  *
00528  * @param string $name Filename without the .php part.
00529  *
00530  */
00531     function vendor($name) {
00532         $args = func_get_args();
00533         foreach ($args as $arg) {
00534             if (file_exists(APP . 'vendors' . DS . $arg . '.php')) {
00535                 require_once(APP . 'vendors' . DS . $arg . '.php');
00536             } else {
00537                 require_once(VENDORS . $arg . '.php');
00538             }
00539         }
00540     }
00541 /**
00542  * Prints out debug information about given variable.
00543  *
00544  * Only runs if DEBUG level is non-zero.
00545  *
00546  * @param boolean $var      Variable to show debug information for.
00547  * @param boolean $show_html    If set to true, the method prints the debug data in a screen-friendly way.
00548  */
00549     function debug($var = false, $showHtml = false) {
00550         if (Configure::read() > 0) {
00551             print "\n<pre class=\"cake_debug\">\n";
00552             ob_start();
00553             print_r($var);
00554             $var = ob_get_clean();
00555 
00556             if ($showHtml) {
00557                 $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
00558             }
00559             print "{$var}\n</pre>\n";
00560         }
00561     }
00562 /**
00563  * Returns microtime for execution time checking
00564  *
00565  * @return integer
00566  */
00567     if (!function_exists('getMicrotime')) {
00568         function getMicrotime() {
00569             list($usec, $sec) = explode(" ", microtime());
00570             return ((float)$usec + (float)$sec);
00571         }
00572     }
00573 /**
00574  * Sorts given $array by key $sortby.
00575  *
00576  * @param  array    $array
00577  * @param  string  $sortby
00578  * @param  string  $order  Sort order asc/desc (ascending or descending).
00579  * @param  integer $type
00580  * @return mixed
00581  */
00582     if (!function_exists('sortByKey')) {
00583         function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
00584             if (!is_array($array)) {
00585                 return null;
00586             }
00587 
00588             foreach ($array as $key => $val) {
00589                 $sa[$key] = $val[$sortby];
00590             }
00591 
00592             if ($order == 'asc') {
00593                 asort($sa, $type);
00594             } else {
00595                 arsort($sa, $type);
00596             }
00597 
00598             foreach ($sa as $key => $val) {
00599                 $out[] = $array[$key];
00600             }
00601             return $out;
00602         }
00603     }
00604 /**
00605  * Combines given identical arrays by using the first array's values as keys,
00606  * and the second one's values as values. (Implemented for back-compatibility with PHP4)
00607  *
00608  * @param  array $a1
00609  * @param  array $a2
00610  * @return mixed Outputs either combined array or false.
00611  */
00612     if (!function_exists('array_combine')) {
00613         function array_combine($a1, $a2) {
00614             $a1 = array_values($a1);
00615             $a2 = array_values($a2);
00616             $c1 = count($a1);
00617             $c2 = count($a2);
00618 
00619             if ($c1 != $c2) {
00620                 return false;
00621             }
00622             if ($c1 <= 0) {
00623                 return false;
00624             }
00625 
00626             $output=array();
00627             for ($i = 0; $i < $c1; $i++) {
00628                 $output[$a1[$i]] = $a2[$i];
00629             }
00630             return $output;
00631         }
00632     }
00633 /**
00634  * Convenience method for htmlspecialchars.
00635  *
00636  * @param string $text
00637  * @return string
00638  */
00639     function h($text) {
00640         if (is_array($text)) {
00641             return array_map('h', $text);
00642         }
00643         return htmlspecialchars($text);
00644     }
00645 /**
00646  * Returns an array of all the given parameters.
00647  *
00648  * Example:
00649  * <code>
00650  * a('a', 'b')
00651  * </code>
00652  *
00653  * Would return:
00654  * <code>
00655  * array('a', 'b')
00656  * </code>
00657  *
00658  * @return array
00659  */
00660     function a() {
00661         $args = func_get_args();
00662         return $args;
00663     }
00664 /**
00665  * Constructs associative array from pairs of arguments.
00666  *
00667  * Example:
00668  * <code>
00669  * aa('a','b')
00670  * </code>
00671  *
00672  * Would return:
00673  * <code>
00674  * array('a'=>'b')
00675  * </code>
00676  *
00677  * @return array
00678  */
00679     function aa() {
00680         $args = func_get_args();
00681         for ($l = 0, $c = count($args); $l < $c; $l++) {
00682             if ($l + 1 < count($args)) {
00683                 $a[$args[$l]] = $args[$l + 1];
00684             } else {
00685                 $a[$args[$l]] = null;
00686             }
00687             $l++;
00688         }
00689         return $a;
00690     }
00691 /**
00692  * Convenience method for echo().
00693  *
00694  * @param string $text String to echo
00695  */
00696     function e($text) {
00697         echo $text;
00698     }
00699 /**
00700  * Convenience method for strtolower().
00701  *
00702  * @param string $str String to lowercase
00703  */
00704     function low($str) {
00705         return strtolower($str);
00706     }
00707 /**
00708  * Convenience method for strtoupper().
00709  *
00710  * @param string $str String to uppercase
00711  */
00712     function up($str) {
00713         return strtoupper($str);
00714     }
00715 /**
00716  * Convenience method for str_replace().
00717  *
00718  * @param string $search String to be replaced
00719  * @param string $replace String to insert
00720  * @param string $subject String to search
00721  */
00722     function r($search, $replace, $subject) {
00723         return str_replace($search, $replace, $subject);
00724     }
00725 /**
00726  * Print_r convenience function, which prints out <PRE> tags around
00727  * the output of given array. Similar to debug().
00728  *
00729  * @see debug()
00730  * @param array $var
00731  */
00732     function pr($var) {
00733         if (Configure::read() > 0) {
00734             echo "<pre>";
00735             print_r($var);
00736             echo "</pre>";
00737         }
00738     }
00739 /**
00740  * Display parameter
00741  *
00742  * @param  mixed  $p Parameter as string or array
00743  * @return string
00744  */
00745     function params($p) {
00746         if (!is_array($p) || count($p) == 0) {
00747             return null;
00748         } else {
00749             if (is_array($p[0]) && count($p) == 1) {
00750                 return $p[0];
00751             } else {
00752                 return $p;
00753             }
00754         }
00755     }
00756 /**
00757  * Merge a group of arrays
00758  *
00759  * @param array First array
00760  * @param array Second array
00761  * @param array Third array
00762  * @param array Etc...
00763  * @return array All array parameters merged into one
00764  */
00765     function am() {
00766         $r = array();
00767         foreach (func_get_args()as $a) {
00768             if (!is_array($a)) {
00769                 $a = array($a);
00770             }
00771             $r = array_merge($r, $a);
00772         }
00773         return $r;
00774     }
00775 /**
00776  * Returns the REQUEST_URI from the server environment, or, failing that,
00777  * constructs a new one, using the PHP_SELF constant and other variables.
00778  *
00779  * @return string URI
00780  */
00781     function setUri() {
00782         if (env('HTTP_X_REWRITE_URL')) {
00783             $uri = env('HTTP_X_REWRITE_URL');
00784         } elseif (env('REQUEST_URI')) {
00785             $uri = env('REQUEST_URI');
00786         } else {
00787             if (env('argv')) {
00788                 $uri = env('argv');
00789 
00790                 if (defined('SERVER_IIS')) {
00791                     $uri = BASE_URL . $uri[0];
00792                 } else {
00793                     $uri = env('PHP_SELF') . '/' . $uri[0];
00794                 }
00795             } else {
00796                 $uri = env('PHP_SELF') . '/' . env('QUERY_STRING');
00797             }
00798         }
00799         return $uri;
00800     }
00801 /**
00802  * Gets an environment variable from available sources.
00803  * Used as a backup if $_SERVER/$_ENV are disabled.
00804  *
00805  * @param  string $key Environment variable name.
00806  * @return string Environment variable setting.
00807  */
00808     function env($key) {
00809 
00810         if ($key == 'HTTPS') {
00811             if (isset($_SERVER) && !empty($_SERVER)) {
00812                 return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
00813             } else {
00814                 return (strpos(env('SCRIPT_URI'), 'https://') === 0);
00815             }
00816         }
00817 
00818         if (isset($_SERVER[$key])) {
00819             return $_SERVER[$key];
00820         } elseif (isset($_ENV[$key])) {
00821             return $_ENV[$key];
00822         } elseif (getenv($key) !== false) {
00823             return getenv($key);
00824         }
00825 
00826         if ($key == 'SCRIPT_FILENAME' && defined('SERVER_IIS') && SERVER_IIS === true) {
00827             return str_replace('\\\\', '\\', env('PATH_TRANSLATED') );
00828         }
00829 
00830         if ($key == 'DOCUMENT_ROOT') {
00831             $offset = 0;
00832             if (!strpos(env('SCRIPT_NAME'), '.php')) {
00833                 $offset = 4;
00834             }
00835             return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
00836         }
00837         if ($key == 'PHP_SELF') {
00838             return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
00839         }
00840         return null;
00841     }
00842 /**
00843  * Returns contents of a file as a string.
00844  *
00845  * @param  string  $fileName        Name of the file.
00846  * @param  boolean $useIncludePath Wheter the function should use the include path or not.
00847  * @return mixed    Boolean false or contents of required file.
00848  */
00849     if (!function_exists('file_get_contents')) {
00850         function file_get_contents($fileName, $useIncludePath = false) {
00851             $res=fopen($fileName, 'rb', $useIncludePath);
00852 
00853             if ($res === false) {
00854                 trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
00855                 return false;
00856             }
00857             clearstatcache();
00858 
00859             if ($fileSize = @filesize($fileName)) {
00860                 $data = fread($res, $fileSize);
00861             } else {
00862                 $data = '';
00863 
00864                 while (!feof($res)) {
00865                     $data .= fread($res, 8192);
00866                 }
00867             }
00868             return "$data\n";
00869         }
00870     }
00871 /**
00872  * Writes data into file.
00873  *
00874  * If file exists, it will be overwritten. If data is an array, it will be join()ed with an empty string.
00875  *
00876  * @param string $fileName File name.
00877  * @param mixed  $data String or array.
00878  */
00879     if (!function_exists('file_put_contents')) {
00880         function file_put_contents($fileName, $data) {
00881             if (is_array($data)) {
00882                 $data = join('', $data);
00883             }
00884             $res = @fopen($fileName, 'w+b');
00885             if ($res) {
00886                 $write = @fwrite($res, $data);
00887                 if ($write === false) {
00888                     return false;
00889                 } else {
00890                     return $write;
00891                 }
00892             }
00893         }
00894     }
00895 /**
00896  * Reads/writes temporary data to cache files or session.
00897  *
00898  * @param  string $path File path within /tmp to save the file.
00899  * @param  mixed  $data The data to save to the temporary file.
00900  * @param  mixed  $expires A valid strtotime string when the data expires.
00901  * @param  string $target  The target of the cached data; either 'cache' or 'public'.
00902  * @return mixed  The contents of the temporary file.
00903  */
00904     function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
00905         $now = time();
00906         if (!is_numeric($expires)) {
00907             $expires = strtotime($expires, $now);
00908         }
00909 
00910         switch(strtolower($target)) {
00911             case 'cache':
00912                 $filename = CACHE . $path;
00913             break;
00914             case 'public':
00915                 $filename = WWW_ROOT . $path;
00916             break;
00917         }
00918 
00919         $timediff = $expires - $now;
00920         $filetime = false;
00921         if (file_exists($filename)) {
00922             $filetime = @filemtime($filename);
00923         }
00924 
00925         if ($data === null) {
00926             // Read data from file
00927             if (file_exists($filename) && $filetime !== false) {
00928                 if ($filetime + $timediff < $now) {
00929                     // File has expired
00930                     @unlink($filename);
00931                 } else {
00932                     $data = file_get_contents($filename);
00933                 }
00934             }
00935         } else {
00936             file_put_contents($filename, $data);
00937         }
00938         return $data;
00939     }
00940 /**
00941  * Used to delete files in the cache directories, or clear contents of cache directories
00942  *
00943  * @param mixed $params As String name to be searched for deletion, if name is a directory all files in directory will be deleted.
00944  *              If array, names to be searched for deletion.
00945  *              If clearCache() without params, all files in app/tmp/cache/views will be deleted
00946  *
00947  * @param string $type Directory in tmp/cache defaults to view directory
00948  * @param string $ext The file extension you are deleting
00949  * @return true if files found and deleted false otherwise
00950  */
00951     function clearCache($params = null, $type = 'views', $ext = '.php') {
00952         if (is_string($params) || $params === null) {
00953             $params = preg_replace('/\/\//', '/', $params);
00954             $cache = CACHE . $type . DS . $params;
00955 
00956             if (is_file($cache . $ext)) {
00957                 @unlink($cache . $ext);
00958                 return true;
00959             } elseif (is_dir($cache)) {
00960                 $files = glob("$cache*");
00961 
00962                 if ($files === false) {
00963                     return false;
00964                 }
00965 
00966                 foreach ($files as $file) {
00967                     if (is_file($file)) {
00968                         @unlink($file);
00969                     }
00970                 }
00971                 return true;
00972             } else {
00973                 $cache = CACHE . $type . DS . '*' . $params . $ext;
00974                 $files = glob($cache);
00975 
00976                 $cache = CACHE . $type . DS . '*' . $params . '_*' . $ext;
00977                 $files = array_merge($files, glob($cache));
00978 
00979                 if ($files === false) {
00980                     return false;
00981                 }
00982 
00983                 foreach ($files as $file) {
00984                     if (is_file($file)) {
00985                         @unlink($file);
00986                     }
00987                 }
00988                 return true;
00989             }
00990         } elseif (is_array($params)) {
00991             foreach ($params as $key => $file) {
00992                 clearCache($file, $type, $ext);
00993             }
00994             return true;
00995         }
00996         return false;
00997     }
00998 /**
00999  * Recursively strips slashes from all values in an array
01000  *
01001  * @param unknown_type $value
01002  * @return unknown
01003  */
01004     function stripslashes_deep($value) {
01005         if (is_array($value)) {
01006             $return = array_map('stripslashes_deep', $value);
01007             return $return;
01008         } else {
01009             $return = stripslashes($value);
01010             return $return ;
01011         }
01012     }
01013 /**
01014  * Returns a translated string if one is found,
01015  * or the submitted message if not found.
01016  *
01017  * @param  unknown_type $msg
01018  * @param  unknown_type $return
01019  * @return unknown
01020  * @todo Not implemented fully till 2.0
01021  */
01022     function __($msg, $return = null) {
01023         if (is_null($return)) {
01024             echo($msg);
01025         } else {
01026             return $msg;
01027         }
01028     }
01029 /**
01030  * Counts the dimensions of an array
01031  *
01032  * @param array $array
01033  * @return int The number of dimensions in $array
01034  */
01035     function countdim($array) {
01036         if (is_array(reset($array))) {
01037             $return = countdim(reset($array)) + 1;
01038         } else {
01039             $return = 1;
01040         }
01041         return $return;
01042     }
01043 /**
01044  * Shortcut to Log::write.
01045  */
01046     function LogError($message) {
01047         if (!class_exists('CakeLog')) {
01048             uses('cake_log');
01049         }
01050         $bad = array("\n", "\r", "\t");
01051         $good = ' ';
01052         CakeLog::write('error', str_replace($bad, $good, $message));
01053     }
01054 /**
01055  * Searches include path for files
01056  *
01057  * @param string $file
01058  * @return Full path to file if exists, otherwise false
01059  */
01060     function fileExistsInPath($file) {
01061         $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
01062         foreach ($paths as $path) {
01063             $fullPath = $path . DIRECTORY_SEPARATOR . $file;
01064 
01065             if (file_exists($fullPath)) {
01066                 return $fullPath;
01067             } elseif (file_exists($file)) {
01068                 return $file;
01069             }
01070         }
01071         return false;
01072     }
01073 /**
01074  * Convert forward slashes to underscores and removes first and last underscores in a string
01075  *
01076  * @param string
01077  * @return string with underscore remove from start and end of string
01078  */
01079     function convertSlash($string) {
01080         $string = trim($string,"/");
01081         $string = preg_replace('/\/\//', '/', $string);
01082         $string = str_replace('/', '_', $string);
01083         return $string;
01084     }
01085 
01086 /**
01087  * chmod recursively on a directory
01088  *
01089  * @param string $path
01090  * @param int $mode
01091  * @return boolean
01092  */
01093     function chmodr($path, $mode = 0755) {
01094         if (!is_dir($path)) {
01095             return chmod($path, $mode);
01096         }
01097         $dir = opendir($path);
01098 
01099         while ($file = readdir($dir)) {
01100             if ($file != '.' && $file != '..') {
01101                 $fullpath = $path . '/' . $file;
01102 
01103                 if (!is_dir($fullpath)) {
01104                     if (!chmod($fullpath, $mode)) {
01105                         return false;
01106                     }
01107                 } else {
01108                     if (!chmodr($fullpath, $mode)) {
01109                         return false;
01110                     }
01111                 }
01112             }
01113         }
01114         closedir($dir);
01115 
01116         if (chmod($path, $mode)) {
01117             return true;
01118         } else {
01119             return false;
01120         }
01121     }
01122 /**
01123  * removed the plugin name from the base url
01124  *
01125  * @param string $base
01126  * @param string $plugin
01127  * @return base url with plugin name removed if present
01128  */
01129     function strip_plugin($base, $plugin) {
01130         if ($plugin != null) {
01131             $base = preg_replace('/' . $plugin . '/', '', $base);
01132             $base = str_replace('//', '', $base);
01133             $pos1 = strrpos($base, '/');
01134             $char = strlen($base) - 1;
01135 
01136             if ($pos1 == $char) {
01137                 $base = substr($base, 0, $char);
01138             }
01139         }
01140         return $base;
01141     }
01142 /**
01143  * Wraps ternary operations.  If $condition is a non-empty value, $val1 is returned, otherwise $val2.
01144  *
01145  * @param mixed $condition Conditional expression
01146  * @param mixed $val1
01147  * @param mixed $val2
01148  * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
01149  */
01150     function ife($condition, $val1 = null, $val2 = null) {
01151         if (!empty($condition)) {
01152             return $val1;
01153         }
01154         return $val2;
01155     }
01156 ?>