From d861a733273bf10d6aa2e16477a3d06afa6ab218 Mon Sep 17 00:00:00 2001 From: AD7six Date: Thu, 28 Apr 2011 15:16:55 +0200 Subject: [PATCH] update basics to match code standards global functions are not indented --- lib/Cake/basics.php | 747 +++++++++++++++++++++++--------------------- 1 file changed, 385 insertions(+), 362 deletions(-) diff --git a/lib/Cake/basics.php b/lib/Cake/basics.php index 74f3690b3..4bea45d86 100644 --- a/lib/Cake/basics.php +++ b/lib/Cake/basics.php @@ -1,4 +1,5 @@ 0) { - $file = ''; - $line = ''; - if ($showFrom) { - $calledFrom = debug_backtrace(); - $file = substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1); - $line = $calledFrom[0]['line']; - } - $html = << 0) { + $file = ''; + $line = ''; + if ($showFrom) { + $calledFrom = debug_backtrace(); + $file = substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1); + $line = $calledFrom[0]['line']; + } + $html = <<%s (line %s)
 %s
@@ -94,32 +95,32 @@ HTML;
 ###########################
 
 TEXT;
-			$template = $html;
-			if (php_sapi_name() == 'cli') {
-				$template = $text;
-			}
-			if ($showHtml === null && $template !== $text) {
-				$showHtml = true;
-			}
-			$var = print_r($var, true);
-			if ($showHtml) {
-				$var = str_replace(array('<', '>'), array('<', '>'), $var);
-			}
-			printf($template, $file, $line, $var);
+		$template = $html;
+		if (php_sapi_name() == 'cli') {
+			$template = $text;
 		}
+		if ($showHtml === null && $template !== $text) {
+			$showHtml = true;
+		}
+		$var = print_r($var, true);
+		if ($showHtml) {
+			$var = str_replace(array('<', '>'), array('<', '>'), $var);
+		}
+		printf($template, $file, $line, $var);
 	}
+}
 
 if (!function_exists('sortByKey')) {
 
-/**
- * Sorts given $array by key $sortby.
- *
- * @param array $array Array to sort
- * @param string $sortby Sort by this key
- * @param string $order  Sort order asc/desc (ascending or descending).
- * @param integer $type Type of sorting to perform
- * @return mixed Sorted array
- */
+	/**
+	 * Sorts given $array by key $sortby.
+	 *
+	 * @param array $array Array to sort
+	 * @param string $sortby Sort by this key
+	 * @param string $order  Sort order asc/desc (ascending or descending).
+	 * @param integer $type Type of sorting to perform
+	 * @return mixed Sorted array
+	 */
 	function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
 		if (!is_array($array)) {
 			return null;
@@ -151,28 +152,28 @@ if (!function_exists('sortByKey')) {
  * @return string Wrapped text
  * @link http://book.cakephp.org/view/1132/h
  */
-	function h($text, $double = true, $charset = null) {
-		if (is_array($text)) {
-			$texts = array();
-			foreach ($text as $k => $t) {
-				$texts[$k] = h($t, $double, $charset);
-			}
-			return $texts;
+function h($text, $double = true, $charset = null) {
+	if (is_array($text)) {
+		$texts = array();
+		foreach ($text as $k => $t) {
+			$texts[$k] = h($t, $double, $charset);
 		}
-
-		static $defaultCharset = false;
-		if ($defaultCharset === false) {
-			$defaultCharset = Configure::read('App.encoding');
-			if ($defaultCharset === null) {
-				$defaultCharset = 'UTF-8';
-			}
-		}
-		if (is_string($double)) {
-			$charset = $double;
-		}
-		return htmlspecialchars($text, ENT_QUOTES, ($charset) ? $charset : $defaultCharset, $double);
+		return $texts;
 	}
 
+	static $defaultCharset = false;
+	if ($defaultCharset === false) {
+		$defaultCharset = Configure::read('App.encoding');
+		if ($defaultCharset === null) {
+			$defaultCharset = 'UTF-8';
+		}
+	}
+	if (is_string($double)) {
+		$charset = $double;
+	}
+	return htmlspecialchars($text, ENT_QUOTES, ($charset) ? $charset : $defaultCharset, $double);
+}
+
 /**
  * Splits a dot syntax plugin name into its plugin and classname.
  * If $name does not have a dot, then index 0 will be null.
@@ -184,16 +185,16 @@ if (!function_exists('sortByKey')) {
  * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  * @return array Array with 2 indexes.  0 => plugin name, 1 => classname
  */
-	function pluginSplit($name, $dotAppend = false, $plugin = null) {
-		if (strpos($name, '.') !== false) {
-			$parts = explode('.', $name, 2);
-			if ($dotAppend) {
-				$parts[0] .= '.';
-			}
-			return $parts;
+function pluginSplit($name, $dotAppend = false, $plugin = null) {
+	if (strpos($name, '.') !== false) {
+		$parts = explode('.', $name, 2);
+		if ($dotAppend) {
+			$parts[0] .= '.';
 		}
-		return array($plugin, $name);
+		return $parts;
 	}
+	return array($plugin, $name);
+}
 
 /**
  * Print_r convenience function, which prints out 
 tags around
@@ -203,13 +204,13 @@ if (!function_exists('sortByKey')) {
  * @param array $var Variable to print out
  * @link http://book.cakephp.org/view/1136/pr
  */
-	function pr($var) {
-		if (Configure::read('debug') > 0) {
-			echo '
';
-			print_r($var);
-			echo '
'; - } +function pr($var) { + if (Configure::read('debug') > 0) { + echo '
';
+		print_r($var);
+		echo '
'; } +} /** * Merge a group of arrays @@ -221,17 +222,17 @@ if (!function_exists('sortByKey')) { * @return array All array parameters merged into one * @link http://book.cakephp.org/view/1124/am */ - function am() { - $r = array(); - $args = func_get_args(); - foreach ($args as $a) { - if (!is_array($a)) { - $a = array($a); - } - $r = array_merge($r, $a); +function am() { + $r = array(); + $args = func_get_args(); + foreach ($args as $a) { + if (!is_array($a)) { + $a = array($a); } - return $r; + $r = array_merge($r, $a); } + return $r; +} /** * Gets an environment variable from available sources, and provides emulation @@ -243,83 +244,105 @@ if (!function_exists('sortByKey')) { * @return string Environment variable setting. * @link http://book.cakephp.org/view/1130/env */ - function env($key) { - if ($key === 'HTTPS') { - if (isset($_SERVER['HTTPS'])) { - return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'); - } - return (strpos(env('SCRIPT_URI'), 'https://') === 0); +function env($key) { + if ($key === 'HTTPS') { + if (isset($_SERVER['HTTPS'])) { + return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'); } - - if ($key === 'SCRIPT_NAME') { - if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) { - $key = 'SCRIPT_URL'; - } - } - - $val = null; - if (isset($_SERVER[$key])) { - $val = $_SERVER[$key]; - } elseif (isset($_ENV[$key])) { - $val = $_ENV[$key]; - } elseif (getenv($key) !== false) { - $val = getenv($key); - } - - if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) { - $addr = env('HTTP_PC_REMOTE_ADDR'); - if ($addr !== null) { - $val = $addr; - } - } - - if ($val !== null) { - return $val; - } - - switch ($key) { - case 'SCRIPT_FILENAME': - if (defined('SERVER_IIS') && SERVER_IIS === true) { - return str_replace('\\\\', '\\', env('PATH_TRANSLATED')); - } - break; - case 'DOCUMENT_ROOT': - $name = env('SCRIPT_NAME'); - $filename = env('SCRIPT_FILENAME'); - $offset = 0; - if (!strpos($name, '.php')) { - $offset = 4; - } - return substr($filename, 0, strlen($filename) - (strlen($name) + $offset)); - break; - case 'PHP_SELF': - return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME')); - break; - case 'CGI_MODE': - return (PHP_SAPI === 'cgi'); - break; - case 'HTTP_BASE': - $host = env('HTTP_HOST'); - $parts = explode('.', $host); - $count = count($parts); - - if ($count === 1) { - return '.' . $host; - } elseif ($count === 2) { - return '.' . $host; - } elseif ($count === 3) { - $gTLD = array('aero', 'asia', 'biz', 'cat', 'com', 'coop', 'edu', 'gov', 'info', 'int', 'jobs', 'mil', 'mobi', 'museum', 'name', 'net', 'org', 'pro', 'tel', 'travel', 'xxx'); - if (in_array($parts[1], $gTLD)) { - return '.' . $host; - } - } - array_shift($parts); - return '.' . implode('.', $parts); - break; - } - return null; + return (strpos(env('SCRIPT_URI'), 'https://') === 0); } + if ($key === 'SCRIPT_NAME') { + if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) { + $key = 'SCRIPT_URL'; + } + } + + $val = null; + if (isset($_SERVER[$key])) { + $val = $_SERVER[$key]; + } elseif (isset($_ENV[$key])) { + $val = $_ENV[$key]; + } elseif (getenv($key) !== false) { + $val = getenv($key); + } + + if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) { + $addr = env('HTTP_PC_REMOTE_ADDR'); + if ($addr !== null) { + $val = $addr; + } + } + + if ($val !== null) { + return $val; + } + + switch ($key) { + case 'SCRIPT_FILENAME': + if (defined('SERVER_IIS') && SERVER_IIS === true) { + return str_replace('\\\\', '\\', env('PATH_TRANSLATED')); + } + break; + case 'DOCUMENT_ROOT': + $name = env('SCRIPT_NAME'); + $filename = env('SCRIPT_FILENAME'); + $offset = 0; + if (!strpos($name, '.php')) { + $offset = 4; + } + return substr($filename, 0, strlen($filename) - (strlen($name) + $offset)); + break; + case 'PHP_SELF': + return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME')); + break; + case 'CGI_MODE': + return (PHP_SAPI === 'cgi'); + break; + case 'HTTP_BASE': + $host = env('HTTP_HOST'); + $parts = explode('.', $host); + $count = count($parts); + + if ($count === 1) { + return '.' . $host; + } elseif ($count === 2) { + return '.' . $host; + } elseif ($count === 3) { + $gTLD = array( + 'aero', + 'asia', + 'biz', + 'cat', + 'com', + 'coop', + 'edu', + 'gov', + 'info', + 'int', + 'jobs', + 'mil', + 'mobi', + 'museum', + 'name', + 'net', + 'org', + 'pro', + 'tel', + 'travel', + 'xxx' + ); + if (in_array($parts[1], $gTLD)) { + return '.' . $host; + } + } + array_shift($parts); + return '.' . implode('.', $parts); + break; + } + return null; +} + /** * Reads/writes temporary data to cache files or session. * @@ -330,47 +353,47 @@ if (!function_exists('sortByKey')) { * @return mixed The contents of the temporary file. * @deprecated Please use Cache::write() instead */ - function cache($path, $data = null, $expires = '+1 day', $target = 'cache') { - if (Configure::read('Cache.disable')) { - return null; - } - $now = time(); - - if (!is_numeric($expires)) { - $expires = strtotime($expires, $now); - } - - switch (strtolower($target)) { - case 'cache': - $filename = CACHE . $path; - break; - case 'public': - $filename = WWW_ROOT . $path; - break; - case 'tmp': - $filename = TMP . $path; - break; - } - $timediff = $expires - $now; - $filetime = false; - - if (file_exists($filename)) { - $filetime = @filemtime($filename); - } - - if ($data === null) { - if (file_exists($filename) && $filetime !== false) { - if ($filetime + $timediff < $now) { - @unlink($filename); - } else { - $data = @file_get_contents($filename); - } - } - } elseif (is_writable(dirname($filename))) { - @file_put_contents($filename, $data); - } - return $data; +function cache($path, $data = null, $expires = '+1 day', $target = 'cache') { + if (Configure::read('Cache.disable')) { + return null; } + $now = time(); + + if (!is_numeric($expires)) { + $expires = strtotime($expires, $now); + } + + switch (strtolower($target)) { + case 'cache': + $filename = CACHE . $path; + break; + case 'public': + $filename = WWW_ROOT . $path; + break; + case 'tmp': + $filename = TMP . $path; + break; + } + $timediff = $expires - $now; + $filetime = false; + + if (file_exists($filename)) { + $filetime = @filemtime($filename); + } + + if ($data === null) { + if (file_exists($filename) && $filetime !== false) { + if ($filetime + $timediff < $now) { + @unlink($filename); + } else { + $data = @file_get_contents($filename); + } + } + } elseif (is_writable(dirname($filename))) { + @file_put_contents($filename, $data); + } + return $data; +} /** * Used to delete files in the cache directories, or clear contents of cache directories @@ -382,57 +405,57 @@ if (!function_exists('sortByKey')) { * @param string $ext The file extension you are deleting * @return true if files found and deleted false otherwise */ - function clearCache($params = null, $type = 'views', $ext = '.php') { - if (is_string($params) || $params === null) { - $params = preg_replace('/\/\//', '/', $params); - $cache = CACHE . $type . DS . $params; +function clearCache($params = null, $type = 'views', $ext = '.php') { + if (is_string($params) || $params === null) { + $params = preg_replace('/\/\//', '/', $params); + $cache = CACHE . $type . DS . $params; - if (is_file($cache . $ext)) { - @unlink($cache . $ext); - return true; - } elseif (is_dir($cache)) { - $files = glob($cache . '*'); + if (is_file($cache . $ext)) { + @unlink($cache . $ext); + return true; + } elseif (is_dir($cache)) { + $files = glob($cache . '*'); - if ($files === false) { - return false; - } - - foreach ($files as $file) { - if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) { - @unlink($file); - } - } - return true; - } else { - $cache = array( - CACHE . $type . DS . '*' . $params . $ext, - CACHE . $type . DS . '*' . $params . '_*' . $ext - ); - $files = array(); - while ($search = array_shift($cache)) { - $results = glob($search); - if ($results !== false) { - $files = array_merge($files, $results); - } - } - if (empty($files)) { - return false; - } - foreach ($files as $file) { - if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) { - @unlink($file); - } - } - return true; + if ($files === false) { + return false; } - } elseif (is_array($params)) { - foreach ($params as $file) { - clearCache($file, $type, $ext); + + foreach ($files as $file) { + if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) { + @unlink($file); + } + } + return true; + } else { + $cache = array( + CACHE . $type . DS . '*' . $params . $ext, + CACHE . $type . DS . '*' . $params . '_*' . $ext + ); + $files = array(); + while ($search = array_shift($cache)) { + $results = glob($search); + if ($results !== false) { + $files = array_merge($files, $results); + } + } + if (empty($files)) { + return false; + } + foreach ($files as $file) { + if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) { + @unlink($file); + } } return true; } - return false; + } elseif (is_array($params)) { + foreach ($params as $file) { + clearCache($file, $type, $ext); + } + return true; } + return false; +} /** * Recursively strips slashes from all values in an array @@ -441,16 +464,16 @@ if (!function_exists('sortByKey')) { * @return mixed What is returned from calling stripslashes * @link http://book.cakephp.org/view/1138/stripslashes_deep */ - function stripslashes_deep($values) { - if (is_array($values)) { - foreach ($values as $key => $value) { - $values[$key] = stripslashes_deep($value); - } - } else { - $values = stripslashes($values); +function stripslashes_deep($values) { + if (is_array($values)) { + foreach ($values as $key => $value) { + $values[$key] = stripslashes_deep($value); } - return $values; + } else { + $values = stripslashes($values); } + return $values; +} /** * Returns a translated string if one is found; Otherwise, the submitted message. @@ -460,21 +483,21 @@ if (!function_exists('sortByKey')) { * @return mixed translated string * @link http://book.cakephp.org/view/1121/__ */ - function __($singular, $args = null) { - if (!$singular) { - return; - } - - App::uses('I18n', 'I18n'); - $translated = I18n::translate($singular); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 1); - } - return vsprintf($translated, $args); +function __($singular, $args = null) { + if (!$singular) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($singular); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 1); + } + return vsprintf($translated, $args); +} + /** * Returns correct plural form of message identified by $singular and $plural for count $count. * Some languages have more than one form for plural messages dependent on the count. @@ -485,21 +508,21 @@ if (!function_exists('sortByKey')) { * @param mixed $args Array with arguments or multiple arguments in function * @return mixed plural form of translated string */ - function __n($singular, $plural, $count, $args = null) { - if (!$singular) { - return; - } - - App::uses('I18n', 'I18n'); - $translated = I18n::translate($singular, $plural, null, 6, $count); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 3); - } - return vsprintf($translated, $args); +function __n($singular, $plural, $count, $args = null) { + if (!$singular) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($singular, $plural, null, 6, $count); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 3); + } + return vsprintf($translated, $args); +} + /** * Allows you to override the current domain for a single message lookup. * @@ -508,19 +531,19 @@ if (!function_exists('sortByKey')) { * @param mixed $args Array with arguments or multiple arguments in function * @return translated string */ - function __d($domain, $msg, $args = null) { - if (!$msg) { - return; - } - App::uses('I18n', 'I18n'); - $translated = I18n::translate($msg, null, $domain); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 2); - } - return vsprintf($translated, $args); +function __d($domain, $msg, $args = null) { + if (!$msg) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($msg, null, $domain); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 2); + } + return vsprintf($translated, $args); +} /** * Allows you to override the current domain for a single plural message lookup. @@ -534,19 +557,19 @@ if (!function_exists('sortByKey')) { * @param mixed $args Array with arguments or multiple arguments in function * @return plural form of translated string */ - function __dn($domain, $singular, $plural, $count, $args = null) { - if (!$singular) { - return; - } - App::uses('I18n', 'I18n'); - $translated = I18n::translate($singular, $plural, $domain, 6, $count); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 4); - } - return vsprintf($translated, $args); +function __dn($domain, $singular, $plural, $count, $args = null) { + if (!$singular) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($singular, $plural, $domain, 6, $count); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 4); + } + return vsprintf($translated, $args); +} /** * Allows you to override the current domain for a single message lookup. @@ -571,19 +594,19 @@ if (!function_exists('sortByKey')) { * @param mixed $args Array with arguments or multiple arguments in function * @return translated string */ - function __dc($domain, $msg, $category, $args = null) { - if (!$msg) { - return; - } - App::uses('I18n', 'I18n'); - $translated = I18n::translate($msg, null, $domain, $category); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 3); - } - return vsprintf($translated, $args); +function __dc($domain, $msg, $category, $args = null) { + if (!$msg) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($msg, null, $domain, $category); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 3); + } + return vsprintf($translated, $args); +} /** * Allows you to override the current domain for a single plural message lookup. @@ -612,19 +635,19 @@ if (!function_exists('sortByKey')) { * @param mixed $args Array with arguments or multiple arguments in function * @return plural form of translated string */ - function __dcn($domain, $singular, $plural, $count, $category, $args = null) { - if (!$singular) { - return; - } - App::uses('I18n', 'I18n'); - $translated = I18n::translate($singular, $plural, $domain, $category, $count); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 5); - } - return vsprintf($translated, $args); +function __dcn($domain, $singular, $plural, $count, $category, $args = null) { + if (!$singular) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($singular, $plural, $domain, $category, $count); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 5); + } + return vsprintf($translated, $args); +} /** * The category argument allows a specific category of the locale settings to be used for fetching a message. @@ -645,31 +668,31 @@ if (!function_exists('sortByKey')) { * @param mixed $args Array with arguments or multiple arguments in function * @return translated string */ - function __c($msg, $category, $args = null) { - if (!$msg) { - return; - } - App::uses('I18n', 'I18n'); - $translated = I18n::translate($msg, null, null, $category); - if ($args === null) { - return $translated; - } elseif (!is_array($args)) { - $args = array_slice(func_get_args(), 2); - } - return vsprintf($translated, $args); +function __c($msg, $category, $args = null) { + if (!$msg) { + return; } + App::uses('I18n', 'I18n'); + $translated = I18n::translate($msg, null, null, $category); + if ($args === null) { + return $translated; + } elseif (!is_array($args)) { + $args = array_slice(func_get_args(), 2); + } + return vsprintf($translated, $args); +} /** * Shortcut to Log::write. * * @param string $message Message to write to log */ - function LogError($message) { - App::uses('CakeLog', 'Log'); - $bad = array("\n", "\r", "\t"); - $good = ' '; - CakeLog::write('error', str_replace($bad, $good, $message)); - } +function LogError($message) { + App::uses('CakeLog', 'Log'); + $bad = array("\n", "\r", "\t"); + $good = ' '; + CakeLog::write('error', str_replace($bad, $good, $message)); +} /** * Searches include path for files. @@ -678,19 +701,19 @@ if (!function_exists('sortByKey')) { * @return Full path to file if exists, otherwise false * @link http://book.cakephp.org/view/1131/fileExistsInPath */ - function fileExistsInPath($file) { - $paths = explode(PATH_SEPARATOR, ini_get('include_path')); - foreach ($paths as $path) { - $fullPath = $path . DS . $file; +function fileExistsInPath($file) { + $paths = explode(PATH_SEPARATOR, ini_get('include_path')); + foreach ($paths as $path) { + $fullPath = $path . DS . $file; - if (file_exists($fullPath)) { - return $fullPath; - } elseif (file_exists($file)) { - return $file; - } + if (file_exists($fullPath)) { + return $fullPath; + } elseif (file_exists($file)) { + return $file; } - return false; } + return false; +} /** * Convert forward slashes to underscores and removes first and last underscores in a string @@ -699,9 +722,9 @@ if (!function_exists('sortByKey')) { * @return string with underscore remove from start and end of string * @link http://book.cakephp.org/view/1126/convertSlash */ - function convertSlash($string) { - $string = trim($string, '/'); - $string = preg_replace('/\/\//', '/', $string); - $string = str_replace('/', '_', $string); - return $string; - } +function convertSlash($string) { + $string = trim($string, '/'); + $string = preg_replace('/\/\//', '/', $string); + $string = str_replace('/', '_', $string); + return $string; +}