bootstrap.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. return trim(htmlspecialchars_decode($text, $quoteStyle));
  146. }
  147. }
  148. if (!function_exists('entDec')) {
  149. /**
  150. * Convenience method for html_entity_decode
  151. *
  152. * @param string $text Text to wrap through htmlspecialchars_decode
  153. * @param int $quoteStyle
  154. * @return string Converted text
  155. */
  156. function entDec(string $text, int $quoteStyle = ENT_QUOTES): string {
  157. return !empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '';
  158. }
  159. }
  160. if (!function_exists('extractFileInfo')) {
  161. /**
  162. * Focus is on the filename (without path)
  163. *
  164. * @deprecated Use native method instead
  165. *
  166. * @param string $filename to check on
  167. * @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
  168. * @return mixed
  169. */
  170. function extractFileInfo(string $filename, ?string $type = null) {
  171. $info = extractPathInfo($filename, $type);
  172. if ($info) {
  173. return $info;
  174. }
  175. $pos = strrpos($filename, '.');
  176. $res = '';
  177. switch ($type) {
  178. case 'extension':
  179. case 'ext':
  180. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  181. break;
  182. case 'filename':
  183. case 'file':
  184. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  185. break;
  186. default:
  187. break;
  188. }
  189. return $res;
  190. }
  191. }
  192. if (!function_exists('extractPathInfo')) {
  193. /**
  194. * Uses native PHP function to retrieve infos about a filename etc.
  195. * Improves it by not returning non-file-name characters from url files if specified.
  196. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  197. *
  198. * @deprecated Use native method instead
  199. *
  200. * @param string $filename to check on
  201. * @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
  202. * @param bool $fromUrl
  203. * @return mixed
  204. */
  205. function extractPathInfo(string $filename, ?string $type = null, bool $fromUrl = false) {
  206. switch ($type) {
  207. case 'extension':
  208. case 'ext':
  209. $infoType = PATHINFO_EXTENSION;
  210. break;
  211. case 'filename':
  212. case 'file':
  213. $infoType = PATHINFO_FILENAME;
  214. break;
  215. case 'basename':
  216. case 'base':
  217. $infoType = PATHINFO_BASENAME;
  218. break;
  219. case 'dirname':
  220. case 'dir':
  221. $infoType = PATHINFO_DIRNAME;
  222. break;
  223. default:
  224. $infoType = $type;
  225. }
  226. $result = pathinfo($filename, $infoType);
  227. if ($fromUrl) {
  228. $pos = strpos($result, '#');
  229. if ($pos !== false) {
  230. $result = substr($result, 0, $pos);
  231. }
  232. $pos = strpos($result, '?');
  233. if ($pos !== false) {
  234. $result = substr($result, 0, $pos);
  235. }
  236. }
  237. return $result;
  238. }
  239. }
  240. if (!function_exists('pre')) {
  241. /**
  242. * Shows pr() messages, even with debug = 0.
  243. * Also allows additional customization.
  244. *
  245. * @param mixed $var
  246. * @param bool $collapsedAndExpandable
  247. * @param array<string, mixed> $options
  248. * - class, showHtml, showFrom, jquery, returns, debug
  249. * @return string HTML
  250. */
  251. function pre($var, bool $collapsedAndExpandable = false, array $options = []): string {
  252. $defaults = [
  253. 'class' => 'cake-debug',
  254. 'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
  255. 'showFrom' => false, // Display file + line
  256. 'jquery' => null, // null => Auto - use jQuery (true/false to manually decide),
  257. 'debug' => false, // Show only with debug > 0
  258. ];
  259. $options += $defaults;
  260. if ($options['debug'] && !Configure::read('debug')) {
  261. return '';
  262. }
  263. if (PHP_SAPI === 'cli') {
  264. return sprintf("\n%s\n", print_r($var, true));
  265. }
  266. $res = '<div class="' . $options['class'] . '">';
  267. $pre = '';
  268. if ($collapsedAndExpandable) {
  269. $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\'}';
  270. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  271. if ($options['jquery'] === true) {
  272. $js = $jsJquery;
  273. } elseif ($options['jquery'] !== false) {
  274. // auto
  275. $js = 'if (typeof jQuery == \'undefined\') {' . $js . '} else {' . $jsJquery . '}';
  276. }
  277. $res .= '<span onclick="' . $js . '" style="cursor: pointer; font-weight: bold">Debug</span>';
  278. if ($options['showFrom']) {
  279. $calledFrom = debug_backtrace();
  280. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  281. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  282. $res .= '<div>' . $from . '</div>';
  283. }
  284. $pre = ' style="display: none"';
  285. }
  286. $var = print_r($var, true);
  287. if (!$options['showHtml']) {
  288. $var = h($var);
  289. }
  290. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  291. $res .= '</div>';
  292. return $res;
  293. }
  294. }
  295. if (!function_exists('contains')) {
  296. /**
  297. * Checks if the string [$haystack] contains [$needle]
  298. *
  299. * @param string $haystack Input string.
  300. * @param string $needle Needed char or string.
  301. * @param bool $caseSensitive
  302. * @return bool
  303. */
  304. function contains(string $haystack, string $needle, bool $caseSensitive = false): bool {
  305. $result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);
  306. return $result !== false;
  307. }
  308. }
  309. if (!function_exists('startsWith')) {
  310. /**
  311. * Checks if the string [$haystack] starts with [$needle]
  312. *
  313. * @param string $haystack Input string.
  314. * @param string $needle Needed char or string.
  315. * @param bool $caseSensitive
  316. * @return bool
  317. */
  318. function startsWith(string $haystack, string $needle, bool $caseSensitive = false): bool {
  319. if ($caseSensitive) {
  320. return mb_strpos($haystack, $needle) === 0;
  321. }
  322. return mb_stripos($haystack, $needle) === 0;
  323. }
  324. }
  325. if (!function_exists('endsWith')) {
  326. /**
  327. * Checks if the String [$haystack] ends with [$needle]
  328. *
  329. * @param string $haystack Input string.
  330. * @param string $needle Needed char or string
  331. * @param bool $caseSensitive
  332. * @return bool
  333. */
  334. function endsWith(string $haystack, string $needle, bool $caseSensitive = false): bool {
  335. if ($caseSensitive) {
  336. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  337. }
  338. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  339. }
  340. }