functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. use Cake\Core\Configure;
  16. if (!defined('DS')) {
  17. /**
  18. * Define DS as short form of DIRECTORY_SEPARATOR.
  19. */
  20. define('DS', DIRECTORY_SEPARATOR);
  21. }
  22. if (!function_exists('h')) {
  23. /**
  24. * Convenience method for htmlspecialchars.
  25. *
  26. * @param mixed $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
  27. * Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
  28. * implement a `__toString` method. Otherwise the class name will be used.
  29. * Other scalar types will be returned unchanged.
  30. * @param bool $double Encode existing html entities.
  31. * @param string|null $charset Character set to use when escaping. Defaults to config value in `mb_internal_encoding()`
  32. * or 'UTF-8'.
  33. * @return mixed Wrapped text.
  34. * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#h
  35. */
  36. function h($text, $double = true, $charset = null)
  37. {
  38. if (is_string($text)) {
  39. //optimize for strings
  40. } elseif (is_array($text)) {
  41. $texts = [];
  42. foreach ($text as $k => $t) {
  43. $texts[$k] = h($t, $double, $charset);
  44. }
  45. return $texts;
  46. } elseif (is_object($text)) {
  47. if (method_exists($text, '__toString')) {
  48. $text = (string)$text;
  49. } else {
  50. $text = '(object)' . get_class($text);
  51. }
  52. } elseif ($text === null || is_scalar($text)) {
  53. return $text;
  54. }
  55. static $defaultCharset = false;
  56. if ($defaultCharset === false) {
  57. $defaultCharset = mb_internal_encoding();
  58. if ($defaultCharset === null) {
  59. $defaultCharset = 'UTF-8';
  60. }
  61. }
  62. if (is_string($double)) {
  63. deprecationWarning(
  64. 'Passing charset string for 2nd argument is deprecated. ' .
  65. 'Use the 3rd argument instead.'
  66. );
  67. $charset = $double;
  68. $double = true;
  69. }
  70. return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, $charset ?: $defaultCharset, $double);
  71. }
  72. }
  73. if (!function_exists('pluginSplit')) {
  74. /**
  75. * Splits a dot syntax plugin name into its plugin and class name.
  76. * If $name does not have a dot, then index 0 will be null.
  77. *
  78. * Commonly used like
  79. * ```
  80. * list($plugin, $name) = pluginSplit($name);
  81. * ```
  82. *
  83. * @param string $name The name you want to plugin split.
  84. * @param bool $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  85. * @param string|null $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  86. * @return array Array with 2 indexes. 0 => plugin name, 1 => class name.
  87. * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pluginSplit
  88. */
  89. function pluginSplit($name, $dotAppend = false, $plugin = null)
  90. {
  91. if (strpos($name, '.') !== false) {
  92. $parts = explode('.', $name, 2);
  93. if ($dotAppend) {
  94. $parts[0] .= '.';
  95. }
  96. return $parts;
  97. }
  98. return [$plugin, $name];
  99. }
  100. }
  101. if (!function_exists('namespaceSplit')) {
  102. /**
  103. * Split the namespace from the classname.
  104. *
  105. * Commonly used like `list($namespace, $className) = namespaceSplit($class);`.
  106. *
  107. * @param string $class The full class name, ie `Cake\Core\App`.
  108. * @return array Array with 2 indexes. 0 => namespace, 1 => classname.
  109. */
  110. function namespaceSplit($class)
  111. {
  112. $pos = strrpos($class, '\\');
  113. if ($pos === false) {
  114. return ['', $class];
  115. }
  116. return [substr($class, 0, $pos), substr($class, $pos + 1)];
  117. }
  118. }
  119. if (!function_exists('pr')) {
  120. /**
  121. * print_r() convenience function.
  122. *
  123. * In terminals this will act similar to using print_r() directly, when not run on cli
  124. * print_r() will also wrap <pre> tags around the output of given variable. Similar to debug().
  125. *
  126. * This function returns the same variable that was passed.
  127. *
  128. * @param mixed $var Variable to print out.
  129. * @return mixed the same $var that was passed to this function
  130. * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pr
  131. * @see debug()
  132. */
  133. function pr($var)
  134. {
  135. if (!Configure::read('debug')) {
  136. return $var;
  137. }
  138. $template = (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') ? '<pre class="pr">%s</pre>' : "\n%s\n\n";
  139. printf($template, trim(print_r($var, true)));
  140. return $var;
  141. }
  142. }
  143. if (!function_exists('pj')) {
  144. /**
  145. * json pretty print convenience function.
  146. *
  147. * In terminals this will act similar to using json_encode() with JSON_PRETTY_PRINT directly, when not run on cli
  148. * will also wrap <pre> tags around the output of given variable. Similar to pr().
  149. *
  150. * This function returns the same variable that was passed.
  151. *
  152. * @param mixed $var Variable to print out.
  153. * @return mixed the same $var that was passed to this function
  154. * @see pr()
  155. * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pj
  156. */
  157. function pj($var)
  158. {
  159. if (!Configure::read('debug')) {
  160. return $var;
  161. }
  162. $template = (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') ? '<pre class="pj">%s</pre>' : "\n%s\n\n";
  163. printf($template, trim(json_encode($var, JSON_PRETTY_PRINT)));
  164. return $var;
  165. }
  166. }
  167. if (!function_exists('env')) {
  168. /**
  169. * Gets an environment variable from available sources, and provides emulation
  170. * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
  171. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  172. * environment information.
  173. *
  174. * @param string $key Environment variable name.
  175. * @param string|null $default Specify a default value in case the environment variable is not defined.
  176. * @return string|bool|null Environment variable setting.
  177. * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#env
  178. */
  179. function env($key, $default = null)
  180. {
  181. if ($key === 'HTTPS') {
  182. if (isset($_SERVER['HTTPS'])) {
  183. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  184. }
  185. return (strpos((string)env('SCRIPT_URI'), 'https://') === 0);
  186. }
  187. if ($key === 'SCRIPT_NAME' && env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  188. $key = 'SCRIPT_URL';
  189. }
  190. $val = null;
  191. if (isset($_SERVER[$key])) {
  192. $val = $_SERVER[$key];
  193. } elseif (isset($_ENV[$key])) {
  194. $val = $_ENV[$key];
  195. } elseif (getenv($key) !== false) {
  196. $val = getenv($key);
  197. }
  198. if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
  199. $addr = env('HTTP_PC_REMOTE_ADDR');
  200. if ($addr !== null) {
  201. $val = $addr;
  202. }
  203. }
  204. if ($val !== null) {
  205. return $val;
  206. }
  207. switch ($key) {
  208. case 'DOCUMENT_ROOT':
  209. $name = env('SCRIPT_NAME');
  210. $filename = env('SCRIPT_FILENAME');
  211. $offset = 0;
  212. if (!strpos($name, '.php')) {
  213. $offset = 4;
  214. }
  215. return substr($filename, 0, -(strlen($name) + $offset));
  216. case 'PHP_SELF':
  217. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  218. case 'CGI_MODE':
  219. return (PHP_SAPI === 'cgi');
  220. }
  221. return $default;
  222. }
  223. }
  224. if (!function_exists('triggerWarning')) {
  225. /**
  226. * Triggers an E_USER_WARNING.
  227. *
  228. * @param string $message The warning message.
  229. * @return void
  230. */
  231. function triggerWarning($message)
  232. {
  233. $stackFrame = 1;
  234. $trace = debug_backtrace();
  235. if (isset($trace[$stackFrame])) {
  236. $frame = $trace[$stackFrame];
  237. $frame += ['file' => '[internal]', 'line' => '??'];
  238. $message = sprintf(
  239. '%s - %s, line: %s',
  240. $message,
  241. $frame['file'],
  242. $frame['line']
  243. );
  244. }
  245. trigger_error($message, E_USER_WARNING);
  246. }
  247. }
  248. if (!function_exists('deprecationWarning')) {
  249. /**
  250. * Helper method for outputting deprecation warnings
  251. *
  252. * @param string $message The message to output as a deprecation warning.
  253. * @param int $stackFrame The stack frame to include in the error. Defaults to 1
  254. * as that should point to application/plugin code.
  255. * @return void
  256. */
  257. function deprecationWarning($message, $stackFrame = 1)
  258. {
  259. if (!(error_reporting() & E_USER_DEPRECATED)) {
  260. return;
  261. }
  262. $trace = debug_backtrace();
  263. if (isset($trace[$stackFrame])) {
  264. $frame = $trace[$stackFrame];
  265. $frame += ['file' => '[internal]', 'line' => '??'];
  266. $message = sprintf(
  267. '%s - %s, line: %s' . "\n" .
  268. ' You can disable deprecation warnings by setting `Error.errorLevel` to' .
  269. ' `E_ALL & ~E_USER_DEPRECATED` in your config/app.php.',
  270. $message,
  271. $frame['file'],
  272. $frame['line']
  273. );
  274. }
  275. trigger_error($message, E_USER_DEPRECATED);
  276. }
  277. }
  278. if (!function_exists('getTypeName')) {
  279. /**
  280. * Returns the objects class or var type of it's not an object
  281. *
  282. * @param mixed $var Variable to check
  283. * @return string Returns the class name or variable type
  284. */
  285. function getTypeName($var)
  286. {
  287. return is_object($var) ? get_class($var) : gettype($var);
  288. }
  289. }