bootstrap.php 10 KB

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