cakephp2-php8/cake/basics.php

417 lines
7.9 KiB
PHP
Raw Normal View History

<?php
/* SVN FILE: $Id$ */
/**
* Basic Cake functionality.
*
* Core functions for including other source files, loading models and so forth.
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2005, CakePHP Authors/Developers
*
* Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com>
* Larry E. Masters aka PhpNut <nut@phpnut.com>
* Kamil Dzielinski aka Brego <brego.dk@gmail.com>
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @author CakePHP Authors/Developers
* @copyright Copyright (c) 2005, CakePHP Authors/Developers
* @link https://trac.cakephp.org/wiki/Authors Authors/Developers
* @package cake
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
* @subpackage cake.cake
* @since CakePHP v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Basic defines for timing functions.
*/
define('SECOND', 1);
define('MINUTE', 60 * SECOND);
define('HOUR', 60 * MINUTE);
define('DAY', 24 * HOUR);
define('WEEK', 7 * DAY);
define('MONTH', 30 * DAY);
define('YEAR', 365 * DAY);
/**
* Loads all models.
*
* @uses listModules()
* @uses APP
* @uses MODELS
*/
function loadModels ()
{
if(file_exists(APP.'app_model.php'))
{
require_once(APP.'app_model.php');
}
else
{
require_once(CAKE.'app_model.php');
}
foreach (listClasses(MODELS) as $model_fn)
{
require_once (MODELS.$model_fn);
}
}
/**
* Loads all controllers.
*
* @uses APP
* @uses listModules()
* @uses HELPERS
* @uses CONTROLLERS
*/
function loadControllers ()
{
if(file_exists(APP.'app_controller.php'))
{
require_once(APP.'app_controller.php');
}
else
{
require_once(CAKE.'app_controller.php');
}
foreach (listClasses(CONTROLLERS) as $controller)
{
require_once (CONTROLLERS.$controller.'.php');
}
}
/**
* Loads a controller and its helper libraries.
*
* @param string $name Name of controller
* @return boolean Success
*/
function loadController ($name)
{
if(file_exists(CONTROLLERS.Inflector::underscore($name).'_controller.php'))
{
$controller_fn = CONTROLLERS.Inflector::underscore($name).'_controller.php';
}
elseif(file_exists(LIBS.'controller'.DS.Inflector::underscore($name).'_controller.php'))
{
$controller_fn = LIBS.'controller'.DS.Inflector::underscore($name).'_controller.php';
}
else
{
$controller_fn = false;
}
if(file_exists(APP.'app_controller.php'))
{
require_once(APP.'app_controller.php');
}
else
{
require_once(CAKE.'app_controller.php');
}
return file_exists($controller_fn)? require_once($controller_fn): false;
}
/**
* Lists PHP files in given directory.
*
* @param string $path Path to scan for files
* @return array List of files in directory
*/
function listClasses($path)
{
$modules = new Folder($path);
return $modules->find('(.+)\.php');
}
/**
* Loads configuration files
*
* @return boolean Success
*/
function config ()
{
$args = func_get_args();
foreach ($args as $arg)
{
if (('database' == $arg) && file_exists(CONFIGS.$arg.'.php'))
{
include_once(CONFIGS.$arg.'.php');
}
elseif (file_exists(CONFIGS.$arg.'.php'))
{
include_once (CONFIGS.$arg.'.php');
if (count($args) == 1) return true;
}
else
{
if (count($args) == 1) return false;
}
}
return true;
}
/**
* Loads component/components from LIBS.
*
* Example:
* <code>
* uses('flay', 'time');
* </code>
*
* @uses LIBS
*/
function uses ()
{
$args = func_get_args();
foreach ($args as $arg)
{
require_once(LIBS.strtolower($arg).'.php');
}
}
/**
* Require given files in the VENDORS directory. Takes optional number of parameters.
*
* @param string $name Filename without the .php part.
*
*/
function vendor($name)
{
$args = func_get_args();
foreach ($args as $arg)
{
require_once(VENDORS.$arg.'.php');
}
}
/**
* Print out debug information about given variable.
*
* Only runs if DEBUG level is non-zero.
*
* @param boolean $var Variable to show debug information for.
* @param boolean $show_html If set to true, the method prints the debug data in a screen-friendly way.
*/
function debug($var = false, $show_html = false)
{
if (DEBUG)
{
print "\n<pre>\n";
if ($show_html) $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
print_r($var);
print "\n</pre>\n";
}
}
if (!function_exists('getMicrotime'))
{
/**
* Returns microtime for execution time checking.
*
* @return integer
*/
function getMicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
if (!function_exists('sortByKey'))
{
/**
* Sorts given $array by key $sortby.
*
* @param array $array
* @param string $sortby
* @param string $order Sort order asc/desc (ascending or descending).
* @param integer $type
* @return mixed
*/
function sortByKey(&$array, $sortby, $order='asc', $type=SORT_NUMERIC)
{
if (!is_array($array))
return null;
foreach ($array as $key => $val)
{
$sa[$key] = $val[$sortby];
}
$order == 'asc'
? asort($sa, $type)
: arsort($sa, $type);
foreach ($sa as $key=>$val)
{
$out[] = $array[$key];
}
return $out;
}
}
if (!function_exists('array_combine'))
{
/**
* Combines given identical arrays by using the first array's values as keys,
* and the second one's values as values. (Implemented for back-compatibility with PHP4.)
*
* @param array $a1
* @param array $a2
* @return mixed Outputs either combined array or false.
*/
function array_combine($a1, $a2)
{
$a1 = array_values($a1);
$a2 = array_values($a2);
$c1 = count($a1);
$c2 = count($a2);
if ($c1 != $c2) return false; // different lenghts
if ($c1 <= 0) return false; // arrays are the same and both are empty
$output = array();
for ($i = 0; $i < $c1; $i++)
{
$output[$a1[$i]] = $a2[$i];
}
return $output;
}
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @param unknown_type $text
* @return unknown
*/
function h($text)
{
return htmlspecialchars($text);
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @return unknown
*/
function a()
{
$args = func_get_args();
return $args;
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @return unknown
*/
function ha()
{
$args = func_get_args();
for($l=0 ; $l<count($args) ; $l++)
{
$a[$args[$l]] = $l+1<count($args) ? $args[$l+1] : null;
$l++;
}
return $a;
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @param unknown_type $text
*/
function e($text)
{
echo $text;
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @param unknown_type $var
*/
function pr($var)
{
if(DEBUG > 0)
{
echo "<pre>";
print_r($var);
echo "</pre>";
}
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @param unknown_type $p
* @return unknown
*/
function params($p)
{
if(!is_array($p) || count($p) == 0)
{
return null;
}
else
{
if(is_array($p[0]) && count($p) == 1)
{
return $p[0];
}
else
{
return $p;
}
}
}
Merging from sandboxes [1079] Merged [1005] committed by nate but not added to core prior to release. Merged [1078] prior to modifying all developers sandboxes. [1081] adding view and template directories [1082] adding base files for view generator [1083] correcting all package and sub package tags for in doc blocks. Making sure every file in the core has doc block in them [1084] renaming working copy of latest release [1093] Added fix for associations using underscores if var $useTable is set in the associated models. This closes ticket #11. [1094] Fix for Ticket #24. The problem was tracked to a variable in View::_render(); $loadedHelpers was being assigned a reference when it when it should not have been. [1096] Initial work on controller components needs testing. Also added a work around for the basics.php uses(). Using the define DS where the files from the original version are now located in deeper libs directories. [1097] committing a few typos in the code I added [1098] reformatting code in component.php [1104] changed the test route and corrected a regex in inflector. [1111] removing the contructor from dispatcher, it is not needed [1112] Changes made for errors when a file is not present in webroot. Fixed the regex used in Router::parse(). Change the error layout template. [1113] Changes to Folder class to allow setting the permissions mode when constructing. This class needs to be refactored and move everything that is in the contructor out. The constructor should set the vars for use in other Folder::"methods"(). Will work on this at a later time. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1114 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-09 01:56:21 +00:00
/**
* Enter description here...
*
* @return unknown
*/
function setUri() {
if (isset($_SERVER['REQUEST_URI']))
{
$uri = $_SERVER['REQUEST_URI'];
}
else
{
if (isset($_SERVER['argv']))
{
$uri = $_SERVER['PHP_SELF'] .'/'. $_SERVER['argv'][0];
}
else
{
$uri = $_SERVER['PHP_SELF'] .'/'. $_SERVER['QUERY_STRING'];
}
}
return $uri;
}
?>