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