functions.php 11 KB

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