bootstrap.php 9.2 KB

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