functions.php 10 KB

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