bootstrap.php 10 KB

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