Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving core related global functions to a separate file
- Loading branch information
Showing
3 changed files
with
238 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since 3.0.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
use Cake\Core\Configure; | ||
|
||
if (!function_exists('h')) { | ||
|
||
/** | ||
* Convenience method for htmlspecialchars. | ||
* | ||
* @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects. | ||
* Arrays will be mapped and have all their elements escaped. Objects will be string cast if they | ||
* implement a `__toString` method. Otherwise the class name will be used. | ||
* @param bool $double Encode existing html entities | ||
* @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8' | ||
* @return string Wrapped text | ||
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#h | ||
*/ | ||
function h($text, $double = true, $charset = null) { | ||
if (is_string($text)) { | ||
//optimize for strings | ||
} elseif (is_array($text)) { | ||
$texts = array(); | ||
foreach ($text as $k => $t) { | ||
$texts[$k] = h($t, $double, $charset); | ||
} | ||
return $texts; | ||
} elseif (is_object($text)) { | ||
if (method_exists($text, '__toString')) { | ||
$text = (string)$text; | ||
} else { | ||
$text = '(object)' . get_class($text); | ||
} | ||
} elseif (is_bool($text)) { | ||
return $text; | ||
} | ||
|
||
static $defaultCharset = false; | ||
if ($defaultCharset === false) { | ||
$defaultCharset = mb_internal_encoding(); | ||
if ($defaultCharset === null) { | ||
$defaultCharset = 'UTF-8'; | ||
} | ||
} | ||
if (is_string($double)) { | ||
$charset = $double; | ||
} | ||
return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, ($charset) ? $charset : $defaultCharset, $double); | ||
} | ||
|
||
} | ||
|
||
if (!function_exists('pluginSplit')) { | ||
|
||
/** | ||
* Splits a dot syntax plugin name into its plugin and class name. | ||
* If $name does not have a dot, then index 0 will be null. | ||
* | ||
* Commonly used like `list($plugin, $name) = pluginSplit($name);` | ||
* | ||
* @param string $name The name you want to plugin split. | ||
* @param bool $dotAppend Set to true if you want the plugin to have a '.' appended to it. | ||
* @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 => class name | ||
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pluginSplit | ||
*/ | ||
function pluginSplit($name, $dotAppend = false, $plugin = null) { | ||
if (strpos($name, '.') !== false) { | ||
$parts = explode('.', $name, 2); | ||
if ($dotAppend) { | ||
$parts[0] .= '.'; | ||
} | ||
return $parts; | ||
} | ||
return array($plugin, $name); | ||
} | ||
|
||
} | ||
|
||
if (!function_exists('namespaceSplit')) { | ||
|
||
/** | ||
* Split the namespace from the classname. | ||
* | ||
* Commonly used like `list($namespace, $className) = namespaceSplit($class);` | ||
* | ||
* @param string $class The full class name, ie `Cake\Core\App` | ||
* @return array Array with 2 indexes. 0 => namespace, 1 => classname | ||
*/ | ||
function namespaceSplit($class) { | ||
$pos = strrpos($class, '\\'); | ||
if ($pos === false) { | ||
return array('', $class); | ||
} | ||
return array(substr($class, 0, $pos), substr($class, $pos + 1)); | ||
} | ||
|
||
} | ||
|
||
if (!function_exists('pr')) { | ||
|
||
/** | ||
* print_r() convenience function | ||
* | ||
* In terminals this will act the same as using print_r() directly, when not run on cli | ||
* print_r() will wrap <PRE> tags around the output of given array. Similar to debug(). | ||
* | ||
* @param mixed $var Variable to print out | ||
* @return void | ||
* @see debug() | ||
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pr | ||
* @see debug() | ||
*/ | ||
function pr($var) { | ||
if (Configure::read('debug')) { | ||
$template = php_sapi_name() !== 'cli' ? '<pre>%s</pre>' : "\n%s\n"; | ||
printf($template, print_r($var, true)); | ||
} | ||
} | ||
|
||
} | ||
|
||
if (!function_exists('env')) { | ||
|
||
/** | ||
* Gets an environment variable from available sources, and provides emulation | ||
* for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on | ||
* IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom | ||
* environment information. | ||
* | ||
* @param string $key Environment variable name. | ||
* @return string Environment variable setting. | ||
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#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); | ||
} | ||
|
||
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 'DOCUMENT_ROOT': | ||
$name = env('SCRIPT_NAME'); | ||
$filename = env('SCRIPT_FILENAME'); | ||
$offset = 0; | ||
if (!strpos($name, '.php')) { | ||
$offset = 4; | ||
} | ||
return substr($filename, 0, -(strlen($name) + $offset)); | ||
case 'PHP_SELF': | ||
return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME')); | ||
case 'CGI_MODE': | ||
return (PHP_SAPI === 'cgi'); | ||
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); | ||
} | ||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.