bootstrap.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. if (!defined('FORMAT_DB_DATETIME')) {
  3. define('FORMAT_DB_DATETIME', 'Y-m-d H:i:s');
  4. }
  5. if (!defined('FORMAT_DB_DATE')) {
  6. define('FORMAT_DB_DATE', 'Y-m-d');
  7. }
  8. if (!defined('FORMAT_DB_TIME')) {
  9. define('FORMAT_DB_TIME', 'H:i:s');
  10. }
  11. if (!defined('SECOND')) {
  12. define('SECOND', 1);
  13. define('MINUTE', 60);
  14. define('HOUR', 3600);
  15. define('DAY', 86400);
  16. define('WEEK', 604800);
  17. define('MONTH', 2592000);
  18. define('YEAR', 31536000);
  19. }
  20. if (!defined('FORMAT_NICE_YMDHMS')) {
  21. define('FORMAT_NICE_YMDHMS', 'd.m.Y, H:i:s');
  22. define('FORMAT_NICE_YMDHM', 'd.m.Y, H:i');
  23. define('FORMAT_NICE_YM', 'm.Y');
  24. define('FORMAT_NICE_YMD', 'd.m.Y');
  25. define('FORMAT_NICE_MD', 'd.m.');
  26. define('FORMAT_NICE_D', 'd'); # xx
  27. define('FORMAT_NICE_W_NUM', 'w'); # xx (0=sunday to 6=saturday)
  28. define('FORMAT_NICE_W_ABBR', 'D'); # needs manual translation
  29. define('FORMAT_NICE_W_FULL', 'l'); # needs manual translation
  30. define('FORMAT_NICE_M', 'm'); # xx
  31. define('FORMAT_NICE_M_ABBR', 'M'); # needs manual translation
  32. define('FORMAT_NICE_M_FULL', 'F'); # needs manual translation
  33. define('FORMAT_NICE_Y_ABBR', 'y'); # xx
  34. define('FORMAT_NICE_Y', 'Y'); # xxxx
  35. define('FORMAT_NICE_HM', 'H:i');
  36. define('FORMAT_NICE_HMS', 'H:i:s');
  37. // localDate strings DEPRECATED PHP 8.1+
  38. define('FORMAT_LOCAL_WA_YMDHMS', '%a, %d.%m.%Y, %H:%M:%S');
  39. define('FORMAT_LOCAL_WF_YMDHMS', '%A, %d.%m.%Y, %H:%M:%S');
  40. define('FORMAT_LOCAL_WA_YMDHM', '%a, %d.%m.%Y, %H:%M');
  41. define('FORMAT_LOCAL_WF_YMDHM', '%A, %d.%m.%Y, %H:%M');
  42. define('FORMAT_LOCAL_YMDHMS', '%d.%m.%Y, %H:%M:%S');
  43. define('FORMAT_LOCAL_YMDHM', '%d.%m.%Y, %H:%M');
  44. define('FORMAT_LOCAL_YMD', '%d.%m.%Y');
  45. define('FORMAT_LOCAL_MD', '%d.%m.');
  46. define('FORMAT_LOCAL_D', '%d'); # xx
  47. define('FORMAT_LOCAL_W_NUM', '%w'); # xx (0=sunday to 6=saturday)
  48. define('FORMAT_LOCAL_W_ABBR', '%a'); # needs translation
  49. define('FORMAT_LOCAL_W_FULL', '%A'); # needs translation
  50. define('FORMAT_LOCAL_M', '%m'); # xx
  51. define('FORMAT_LOCAL_M_ABBR', '%b'); # needs translation
  52. define('FORMAT_LOCAL_M_FULL', '%B'); # needs translation
  53. define('FORMAT_LOCAL_Y_ABBR', '%y'); # xx
  54. define('FORMAT_LOCAL_YMS_ABBR', '%d.%m.%y');
  55. define('FORMAT_LOCAL_Y', '%Y'); # xxxx
  56. define('FORMAT_LOCAL_H', '%H');
  57. define('FORMAT_LOCAL_S', '%S');
  58. define('FORMAT_LOCAL_HM', '%H:%M');
  59. define('FORMAT_LOCAL_HMS', '%H:%M:%S');
  60. }
  61. // Make the app and L10n play nice with Windows.
  62. if (!defined('WINDOWS')) {
  63. if (DS === '\\' || substr(PHP_OS, 0, 3) === 'WIN') {
  64. define('WINDOWS', true);
  65. } else {
  66. define('WINDOWS', false);
  67. }
  68. }
  69. if (!function_exists('isEmpty')) {
  70. /**
  71. * Convenience function to check on "empty()"
  72. *
  73. * @param mixed $var
  74. * @return bool Result
  75. */
  76. function isEmpty($var = null): bool {
  77. if (empty($var)) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. }
  83. if (!function_exists('returns')) {
  84. /**
  85. * Returns of what type the specific value is
  86. *
  87. * //TODO: use Debugger::exportVar() instead?
  88. *
  89. * @param mixed $value
  90. * @return mixed Type (NULL, array, bool, float, int, string, object, unknown) + value
  91. */
  92. function returns($value) {
  93. if ($value === null) {
  94. return 'NULL';
  95. }
  96. if (is_array($value)) {
  97. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  98. }
  99. if ($value === true) {
  100. return '(bool)TRUE';
  101. }
  102. if ($value === false) {
  103. return '(bool)FALSE';
  104. }
  105. if (is_numeric($value) && is_float($value)) {
  106. return '(float)' . $value;
  107. }
  108. if (is_numeric($value) && is_int($value)) {
  109. return '(int)' . $value;
  110. }
  111. if (is_string($value)) {
  112. return '(string)' . $value;
  113. }
  114. if (is_object($value)) {
  115. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  116. '</pre>';
  117. }
  118. return '(unknown)' . $value;
  119. }
  120. }
  121. if (!function_exists('ent')) {
  122. /**
  123. * Returns htmlentities - string
  124. *
  125. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  126. * ENT_QUOTES = Will convert both double and single quotes. !!!
  127. * ENT_NOQUOTES = Will leave both double and single quotes unconverted.
  128. *
  129. * @param string $text
  130. * @return string Converted text
  131. */
  132. function ent(string $text): string {
  133. return htmlentities($text, ENT_QUOTES, 'UTF-8');
  134. }
  135. }
  136. if (!function_exists('hDec')) {
  137. /**
  138. * Convenience method for htmlspecialchars_decode
  139. *
  140. * @param string $text Text to wrap through htmlspecialchars_decode
  141. * @param int $quoteStyle
  142. * @return string Converted text
  143. */
  144. function hDec(string $text, int $quoteStyle = ENT_QUOTES): string {
  145. if (is_array($text)) {
  146. return array_map('hDec', $text);
  147. }
  148. return trim(htmlspecialchars_decode($text, $quoteStyle));
  149. }
  150. }
  151. if (!function_exists('entDec')) {
  152. /**
  153. * Convenience method for html_entity_decode
  154. *
  155. * @param string $text Text to wrap through htmlspecialchars_decode
  156. * @param int $quoteStyle
  157. * @return string Converted text
  158. */
  159. function entDec(string $text, int $quoteStyle = ENT_QUOTES): string {
  160. if (is_array($text)) {
  161. return array_map('entDec', $text);
  162. }
  163. return !empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '';
  164. }
  165. }
  166. if (!function_exists('extractFileInfo')) {
  167. /**
  168. * Focus is on the filename (without path)
  169. *
  170. * @deprecated Use native method instead
  171. *
  172. * @param string $filename to check on
  173. * @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
  174. * @return mixed
  175. */
  176. function extractFileInfo(string $filename, ?string $type = null) {
  177. $info = extractPathInfo($filename, $type);
  178. if ($info) {
  179. return $info;
  180. }
  181. $pos = strrpos($filename, '.');
  182. $res = '';
  183. switch ($type) {
  184. case 'extension':
  185. case 'ext':
  186. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  187. break;
  188. case 'filename':
  189. case 'file':
  190. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  191. break;
  192. default:
  193. break;
  194. }
  195. return $res;
  196. }
  197. }
  198. if (!function_exists('extractPathInfo')) {
  199. /**
  200. * Uses native PHP function to retrieve infos about a filename etc.
  201. * Improves it by not returning non-file-name characters from url files if specified.
  202. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  203. *
  204. * @deprecated Use native method instead
  205. *
  206. * @param string $filename to check on
  207. * @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
  208. * @param bool $fromUrl
  209. * @return mixed
  210. */
  211. function extractPathInfo(string $filename, ?string $type = null, bool $fromUrl = false) {
  212. switch ($type) {
  213. case 'extension':
  214. case 'ext':
  215. $infoType = PATHINFO_EXTENSION;
  216. break;
  217. case 'filename':
  218. case 'file':
  219. $infoType = PATHINFO_FILENAME;
  220. break;
  221. case 'basename':
  222. case 'base':
  223. $infoType = PATHINFO_BASENAME;
  224. break;
  225. case 'dirname':
  226. case 'dir':
  227. $infoType = PATHINFO_DIRNAME;
  228. break;
  229. default:
  230. $infoType = $type;
  231. }
  232. $result = pathinfo($filename, $infoType);
  233. if ($fromUrl) {
  234. $pos = strpos($result, '#');
  235. if ($pos !== false) {
  236. $result = substr($result, 0, $pos);
  237. }
  238. $pos = strpos($result, '?');
  239. if ($pos !== false) {
  240. $result = substr($result, 0, $pos);
  241. }
  242. }
  243. return $result;
  244. }
  245. }
  246. if (!function_exists('pre')) {
  247. /**
  248. * Shows pr() messages, even with debug = 0.
  249. * Also allows additional customization.
  250. *
  251. * @param mixed $var
  252. * @param bool $collapsedAndExpandable
  253. * @param array<string, mixed> $options
  254. * - class, showHtml, showFrom, jquery, returns, debug
  255. * @return string HTML
  256. */
  257. function pre($var, bool $collapsedAndExpandable = false, array $options = []): string {
  258. $defaults = [
  259. 'class' => 'cake-debug',
  260. 'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
  261. 'showFrom' => false, // Display file + line
  262. 'jquery' => null, // null => Auto - use jQuery (true/false to manually decide),
  263. 'debug' => false, // Show only with debug > 0
  264. ];
  265. $options += $defaults;
  266. if ($options['debug'] && !Configure::read('debug')) {
  267. return '';
  268. }
  269. if (PHP_SAPI === 'cli') {
  270. return sprintf("\n%s\n", print_r($var, true));
  271. }
  272. $res = '<div class="' . $options['class'] . '">';
  273. $pre = '';
  274. if ($collapsedAndExpandable) {
  275. $js = 'if (this.parentNode.getElementsByTagName(\'pre\')[0].style.display==\'block\') {this.parentNode.getElementsByTagName(\'pre\')[0].style.display=\'none\'} else {this.parentNode.getElementsByTagName(\'pre\')[0].style.display=\'block\'}';
  276. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  277. if ($options['jquery'] === true) {
  278. $js = $jsJquery;
  279. } elseif ($options['jquery'] !== false) {
  280. // auto
  281. $js = 'if (typeof jQuery == \'undefined\') {' . $js . '} else {' . $jsJquery . '}';
  282. }
  283. $res .= '<span onclick="' . $js . '" style="cursor: pointer; font-weight: bold">Debug</span>';
  284. if ($options['showFrom']) {
  285. $calledFrom = debug_backtrace();
  286. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  287. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  288. $res .= '<div>' . $from . '</div>';
  289. }
  290. $pre = ' style="display: none"';
  291. }
  292. $var = print_r($var, true);
  293. if (!$options['showHtml']) {
  294. $var = h($var);
  295. }
  296. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  297. $res .= '</div>';
  298. return $res;
  299. }
  300. }
  301. if (!function_exists('contains')) {
  302. /**
  303. * Checks if the string [$haystack] contains [$needle]
  304. *
  305. * @param string $haystack Input string.
  306. * @param string $needle Needed char or string.
  307. * @param bool $caseSensitive
  308. * @return bool
  309. */
  310. function contains(string $haystack, string $needle, bool $caseSensitive = false): bool {
  311. $result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);
  312. return $result !== false;
  313. }
  314. }
  315. if (!function_exists('startsWith')) {
  316. /**
  317. * Checks if the string [$haystack] starts with [$needle]
  318. *
  319. * @param string $haystack Input string.
  320. * @param string $needle Needed char or string.
  321. * @param bool $caseSensitive
  322. * @return bool
  323. */
  324. function startsWith(string $haystack, string $needle, bool $caseSensitive = false): bool {
  325. if ($caseSensitive) {
  326. return mb_strpos($haystack, $needle) === 0;
  327. }
  328. return mb_stripos($haystack, $needle) === 0;
  329. }
  330. }
  331. if (!function_exists('endsWith')) {
  332. /**
  333. * Checks if the String [$haystack] ends with [$needle]
  334. *
  335. * @param string $haystack Input string.
  336. * @param string $needle Needed char or string
  337. * @param bool $caseSensitive
  338. * @return bool
  339. */
  340. function endsWith(string $haystack, string $needle, bool $caseSensitive = false): bool {
  341. if ($caseSensitive) {
  342. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  343. }
  344. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  345. }
  346. }