bootstrap.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. // For date()
  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. /**
  56. * Convenience function to check on "empty()"
  57. *
  58. * @param mixed $var
  59. * @return bool Result
  60. */
  61. function isEmpty($var = null) {
  62. if (empty($var)) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * Return of what type the specific value is
  69. *
  70. * //TODO: use Debugger::exportVar() instead?
  71. *
  72. * @param mixed $value
  73. * @return mixed Type (NULL, array, bool, float, int, string, object, unknown) + value
  74. */
  75. function returns($value) {
  76. if ($value === null) {
  77. return 'NULL';
  78. }
  79. if (is_array($value)) {
  80. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  81. }
  82. if ($value === true) {
  83. return '(bool)TRUE';
  84. }
  85. if ($value === false) {
  86. return '(bool)FALSE';
  87. }
  88. if (is_numeric($value) && is_float($value)) {
  89. return '(float)' . $value;
  90. }
  91. if (is_numeric($value) && is_int($value)) {
  92. return '(int)' . $value;
  93. }
  94. if (is_string($value)) {
  95. return '(string)' . $value;
  96. }
  97. if (is_object($value)) {
  98. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  99. '</pre>';
  100. }
  101. return '(unknown)' . $value;
  102. }
  103. /**
  104. * Returns htmlentities - string
  105. *
  106. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  107. * ENT_QUOTES = Will convert both double and single quotes. !!!
  108. * ENT_NOQUOTES = Will leave both double and single quotes unconverted.
  109. *
  110. * @param string $text
  111. * @return string Converted text
  112. */
  113. function ent($text) {
  114. return !empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '';
  115. }
  116. /**
  117. * Convenience method for htmlspecialchars_decode
  118. *
  119. * @param string $text Text to wrap through htmlspecialchars_decode
  120. * @param int $quoteStyle
  121. * @return string Converted text
  122. */
  123. function hDec($text, $quoteStyle = ENT_QUOTES) {
  124. if (is_array($text)) {
  125. return array_map('hDec', $text);
  126. }
  127. return trim(htmlspecialchars_decode($text, $quoteStyle));
  128. }
  129. /**
  130. * Convenience method for html_entity_decode
  131. *
  132. * @param string $text Text to wrap through htmlspecialchars_decode
  133. * @param int $quoteStyle
  134. * @return string Converted text
  135. */
  136. function entDec($text, $quoteStyle = ENT_QUOTES) {
  137. if (is_array($text)) {
  138. return array_map('entDec', $text);
  139. }
  140. return !empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '';
  141. }
  142. /**
  143. * Focus is on the filename (without path)
  144. *
  145. * @param string $filename to check on
  146. * @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
  147. * @return mixed
  148. */
  149. function extractFileInfo($filename, $type = null) {
  150. if ($info = extractPathInfo($filename, $type)) {
  151. return $info;
  152. }
  153. $pos = strrpos($filename, '.');
  154. $res = '';
  155. switch ($type) {
  156. case 'extension':
  157. case 'ext':
  158. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  159. break;
  160. case 'filename':
  161. case 'file':
  162. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  163. break;
  164. default:
  165. break;
  166. }
  167. return $res;
  168. }
  169. /**
  170. * Uses native PHP function to retrieve infos about a filename etc.
  171. * Improves it by not returning non-file-name characters from url files if specified.
  172. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  173. *
  174. * @param string $filename to check on
  175. * @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
  176. * @param bool $fromUrl
  177. * @return mixed
  178. */
  179. function extractPathInfo($filename, $type = null, $fromUrl = false) {
  180. switch ($type) {
  181. case 'extension':
  182. case 'ext':
  183. $infoType = PATHINFO_EXTENSION;
  184. break;
  185. case 'filename':
  186. case 'file':
  187. $infoType = PATHINFO_FILENAME;
  188. break;
  189. case 'basename':
  190. case 'base':
  191. $infoType = PATHINFO_BASENAME;
  192. break;
  193. case 'dirname':
  194. case 'dir':
  195. $infoType = PATHINFO_DIRNAME;
  196. break;
  197. default:
  198. $infoType = $type;
  199. }
  200. $result = pathinfo($filename, $infoType);
  201. if ($fromUrl) {
  202. if (($pos = strpos($result, '#')) !== false) {
  203. $result = substr($result, 0, $pos);
  204. }
  205. if (($pos = strpos($result, '?')) !== false) {
  206. $result = substr($result, 0, $pos);
  207. }
  208. }
  209. return $result;
  210. }
  211. /**
  212. * Shows pr() messages, even with debug = 0.
  213. * Also allows additional customization.
  214. *
  215. * @param mixed $var
  216. * @param bool $collapsedAndExpandable
  217. * @param array $options
  218. * - class, showHtml, showFrom, jquery, returns, debug
  219. * @return string HTML
  220. */
  221. function pre($var, $collapsedAndExpandable = false, $options = []) {
  222. $defaults = [
  223. 'class' => 'cake-debug',
  224. 'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
  225. 'showFrom' => false, // Display file + line
  226. 'jquery' => null, // null => Auto - use jQuery (true/false to manually decide),
  227. 'debug' => false // Show only with debug > 0
  228. ];
  229. $options += $defaults;
  230. if ($options['debug'] && !Configure::read('debug')) {
  231. return '';
  232. }
  233. if (PHP_SAPI === 'cli') {
  234. return sprintf("\n%s\n", print_r($var, true));
  235. }
  236. $res = '<div class="' . $options['class'] . '">';
  237. $pre = '';
  238. if ($collapsedAndExpandable) {
  239. $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\'}';
  240. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  241. if ($options['jquery'] === true) {
  242. $js = $jsJquery;
  243. } elseif ($options['jquery'] !== false) {
  244. // auto
  245. $js = 'if (typeof jQuery == \'undefined\') {' . $js . '} else {' . $jsJquery . '}';
  246. }
  247. $res .= '<span onclick="' . $js . '" style="cursor: pointer; font-weight: bold">Debug</span>';
  248. if ($options['showFrom']) {
  249. $calledFrom = debug_backtrace();
  250. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  251. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  252. $res .= '<div>' . $from . '</div>';
  253. }
  254. $pre = ' style="display: none"';
  255. }
  256. $var = print_r($var, true);
  257. if (!$options['showHtml']) {
  258. $var = h($var);
  259. }
  260. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  261. $res .= '</div>';
  262. return $res;
  263. }
  264. /**
  265. * Checks if the string [$haystack] contains [$needle]
  266. *
  267. * @param string $haystack Input string.
  268. * @param string $needle Needed char or string.
  269. * @param bool $caseSensitive
  270. * @return bool
  271. */
  272. function contains($haystack, $needle, $caseSensitive = false) {
  273. $result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);
  274. return $result !== false;
  275. }
  276. /**
  277. * Checks if the string [$haystack] starts with [$needle]
  278. *
  279. * @param string $haystack Input string.
  280. * @param string $needle Needed char or string.
  281. * @param bool $caseSensitive
  282. * @return bool
  283. */
  284. function startsWith($haystack, $needle, $caseSensitive = false) {
  285. if ($caseSensitive) {
  286. return mb_strpos($haystack, $needle) === 0;
  287. }
  288. return mb_stripos($haystack, $needle) === 0;
  289. }
  290. /**
  291. * Checks if the String [$haystack] ends with [$needle]
  292. *
  293. * @param string $haystack Input string.
  294. * @param string $needle Needed char or string
  295. * @param bool $caseSensitive
  296. * @return bool
  297. */
  298. function endsWith($haystack, $needle, $caseSensitive = false) {
  299. if ($caseSensitive) {
  300. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  301. }
  302. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  303. }