bootstrap.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * Basic bootstrap stuff.
  4. *
  5. * Note: Use
  6. *
  7. * CakePlugin::load('Tools', array('bootstrap' => true));
  8. *
  9. * to include this bootstrap file.
  10. */
  11. App::uses('Utility', 'Tools.Utility');
  12. // You can also use FULL_BASE_URL instead of HTTP_BASE
  13. if (!empty($_SERVER['HTTP_HOST'])) {
  14. define('HTTP_HOST', $_SERVER['HTTP_HOST']);
  15. define('HTTP_BASE', 'http://' . HTTP_HOST); //FULL_BASE_URL
  16. } else {
  17. define('HTTP_HOST', '');
  18. define('HTTP_BASE', '');
  19. }
  20. if (!empty($_SERVER['PHP_SELF'])) {
  21. define('HTTP_SELF', '' . $_SERVER['PHP_SELF']);
  22. }
  23. if (!empty($_SERVER['REQUEST_URI'])) {
  24. define('HTTP_URI', '' . $_SERVER['REQUEST_URI']);
  25. }
  26. if (!empty($_SERVER['HTTP_REFERER'])) {
  27. define('HTTP_REF', '' . $_SERVER['HTTP_REFERER']);
  28. }
  29. define('CHOWN_PUBLIC', 0770);
  30. # Useful when putting a string together that needs some "pretty" html-doc. source layouting
  31. # Only visible in SOURCE code (not in html layout in the browser)
  32. //define('LF',''); // line feed (depending on the system)
  33. define('LF', PHP_EOL); // line feed (depending on the system): \n or \n\r etc.
  34. # Alternativly NL,CR:
  35. define('NL', "\n"); // new line
  36. define('CR', "\r"); // carriage return
  37. define('TB', "\t"); // tabulator
  38. # Useful for html layouting
  39. # Visible in the Html Layout in the Browser
  40. define('BR', '<br />'); // line break
  41. # Make the app and l10n play nice with Windows.
  42. if (substr(PHP_OS, 0, 3) === 'WIN') { // || strpos(@php_uname(), 'ARCH')
  43. define('WINDOWS', true);
  44. } else {
  45. define('WINDOWS', false);
  46. }
  47. define('FORMAT_DB_DATETIME', 'Y-m-d H:i:s'); // date(...)
  48. define('FORMAT_DB_DATE', 'Y-m-d');
  49. define('FORMAT_DB_TIME', 'H:i:s');
  50. // @deprecated Use NULL instead for nullish date(time)s
  51. define('DEFAULT_DATETIME', '0000-00-00 00:00:00');
  52. define('DEFAULT_DATE', '0000-00-00');
  53. define('DEFAULT_TIME', '00:00:00');
  54. // Workpaths
  55. define('FILES', APP . 'files' . DS);
  56. define('LOCALE', APP . 'locale' . DS);
  57. if (!defined('CLASS_USER')) {
  58. define('CLASS_USER', 'User');
  59. }
  60. # Validation ## (minus should be "hyphen")
  61. /** Valid characters: letters only */
  62. define('VALID_ALPHA', '/^[a-zA-Z]+$/');
  63. /** Valid characters: letters,underscores only */
  64. define('VALID_ALPHA_UNDERSCORES', '/^[a-zA-Z_]+$/');
  65. /** Valid characters: letters,underscores,minus only */
  66. define('VALID_ALPHA_MINUS_UNDERSCORES', '/^[a-zA-Z_-]+$/');
  67. /** Valid characters: letters,spaces only */
  68. define('VALID_ALPHA_WHITESPACES', '/^[a-zA-Z ]+$/');
  69. /** Valid characters: letters,numbers,underscores only */
  70. define('VALID_ALPHANUMERIC_UNDERSCORES', '/^[\da-zA-Z_]+$/');
  71. /** Valid characters: letters,numbers,underscores,minus only */
  72. define('VALID_ALPHANUMERIC_MINUS_UNDERSCORES', '/^[\da-zA-Z_-]+$/');
  73. /** Valid characters: letters,numbers,spaces only */
  74. define('VALID_ALPHANUMERIC_WHITESPACES', '/^[\da-zA-Z ]+$/');
  75. /** Valid characters: letters,numbers,spaces,underscores only */
  76. define('VALID_ALPHANUMERIC_WHITESPACES_UNDERSCORES', '/^[\da-zA-Z _]+$/');
  77. /** Valid characters: numbers,underscores only */
  78. define('VALID_NUMERIC_UNDERSCORES', '/^[\d_]+$/');
  79. /** Valid characters: numbers,spaces only */
  80. define('VALID_NUMERIC_WHITESPACES', '/^[\d ]+$/');
  81. /** Valid characters: numbers,spaces,underscores only */
  82. define('VALID_NUMERIC_WHITESPACES_UNDERSCORES', '/^[\d _]+$/');
  83. /** Valid integers: > 0 */
  84. define('VALID_INTEGERS', '/^[\d]+$/'); //??
  85. if (!defined('FORMAT_NICE_YMDHMS')) {
  86. define('FORMAT_NICE_YMDHMS', 'd.m.Y, H:i:s');
  87. define('FORMAT_NICE_YMDHM', 'd.m.Y, H:i');
  88. define('FORMAT_NICE_YM', 'm.Y');
  89. define('FORMAT_NICE_YMD', 'd.m.Y');
  90. define('FORMAT_NICE_MD', 'd.m.');
  91. define('FORMAT_NICE_D', 'd'); # xx
  92. define('FORMAT_NICE_W_NUM', 'w'); # xx (0=sunday to 6=saturday)
  93. define('FORMAT_NICE_W_ABBR', 'D'); # needs manual translation
  94. define('FORMAT_NICE_W_FULL', 'l'); # needs manual translation
  95. define('FORMAT_NICE_M', 'm'); # xx
  96. define('FORMAT_NICE_M_ABBR', 'M'); # needs manual translation
  97. define('FORMAT_NICE_M_FULL', 'F'); # needs manual translation
  98. define('FORMAT_NICE_Y_ABBR', 'y'); # xx
  99. define('FORMAT_NICE_Y', 'Y'); # xxxx
  100. define('FORMAT_NICE_HM', 'H:i');
  101. define('FORMAT_NICE_HMS', 'H:i:s');
  102. // localDate strings
  103. define('FORMAT_LOCAL_WA_YMDHMS', '%a, %d.%m.%Y, %H:%M:%S');
  104. define('FORMAT_LOCAL_WF_YMDHMS', '%A, %d.%m.%Y, %H:%M:%S');
  105. define('FORMAT_LOCAL_WA_YMDHM', '%a, %d.%m.%Y, %H:%M');
  106. define('FORMAT_LOCAL_WF_YMDHM', '%A, %d.%m.%Y, %H:%M');
  107. define('FORMAT_LOCAL_YMDHMS', '%d.%m.%Y, %H:%M:%S');
  108. define('FORMAT_LOCAL_YMDHM', '%d.%m.%Y, %H:%M');
  109. define('FORMAT_LOCAL_YMD', '%d.%m.%Y');
  110. define('FORMAT_LOCAL_MD', '%d.%m.');
  111. define('FORMAT_LOCAL_D', '%d'); # xx
  112. define('FORMAT_LOCAL_W_NUM', '%w'); # xx (0=sunday to 6=saturday)
  113. define('FORMAT_LOCAL_W_ABBR', '%a'); # needs translation
  114. define('FORMAT_LOCAL_W_FULL', '%A'); # needs translation
  115. define('FORMAT_LOCAL_M', '%m'); # xx
  116. define('FORMAT_LOCAL_M_ABBR', '%b'); # needs translation
  117. define('FORMAT_LOCAL_M_FULL', '%B'); # needs translation
  118. define('FORMAT_LOCAL_Y_ABBR', '%y'); # xx
  119. define('FORMAT_LOCAL_YMS_ABBR', '%d.%m.%y');
  120. define('FORMAT_LOCAL_Y', '%Y'); # xxxx
  121. define('FORMAT_LOCAL_H', '%H');
  122. define('FORMAT_LOCAL_S', '%S');
  123. define('FORMAT_LOCAL_HM', '%H:%M');
  124. define('FORMAT_LOCAL_HMS', '%H:%M:%S');
  125. }
  126. /*** chars ***/
  127. /* see http://www.htmlcodetutorial.com/characterentities_famsupp_69.html */
  128. define('CHAR_LESS', '&lt;'); # <
  129. define('CHAR_GREATER', '&gt;'); # >
  130. define('CHAR_QUOTE', '&quot;'); # "
  131. define('CHAR_APOSTROPHE', '&#39'); # '
  132. define('CHAR_ARROWS', '&raquo;'); # »
  133. define('CHAR_ARROWS_R', '&#187;'); # »
  134. define('CHAR_ARROWS_L', '&#171;'); # «
  135. define('CHAR_AVERAGE', '&#216;'); # Ø
  136. define('CHAR_INFIN', '&infin;'); # 8
  137. define('CHAR_MILL', '&#137;'); # ‰ (per mille) / or &permil;
  138. define('CHAR_PLUSMN', '&plusmn;'); # 8
  139. define('CHAR_HELLIP', '&#8230;'); # … (horizontal ellipsis = three dot leader)
  140. define('CHAR_CIRCA', '&asymp;'); # ˜ (almost equal to)
  141. define('CHAR_CHECKBOX_EMPTY', '&#9744;]'); #
  142. define('CHAR_CHECKBOX_MAKRED', '&#9745'); #
  143. define('CHAR_CHECKMARK', '&#10003;');
  144. define('CHAR_CHECKMARK_BOLD', '&#10004;');
  145. define('CHAR_BALLOT', '&#10007;');
  146. define('CHAR_BALLOT_BOLD', '&#10008;');
  147. define('CHAR_ABOUT', '&asymp;'); # … (horizontal ellipsis = three dot leader)
  148. /* not very often used */
  149. define('CHAR_RPIME', '&#8242;'); # ' (minutes)
  150. define('CHAR_DOUBLE_RPIME', '&#8243;'); # ? (seconds)
  151. /** BASIC FUNCTIONS **/
  152. if (!function_exists('isEmpty')) {
  153. /**
  154. * Convenience function to check on "empty()"
  155. *
  156. * @param mixed $var
  157. * @return bool Result
  158. */
  159. function isEmpty($var = null) {
  160. if (empty($var)) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. }
  166. /**
  167. * Return of what type the specific value is
  168. *
  169. * //TODO: use Debugger::exportVar() instead?
  170. *
  171. * @param mixed $value
  172. * @return type (NULL, array, bool, float, int, string, object, unknown) + value
  173. */
  174. function returns($value) {
  175. if ($value === null) {
  176. return 'NULL';
  177. } elseif (is_array($value)) {
  178. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  179. } elseif ($value === true) {
  180. return '(bool)TRUE';
  181. } elseif ($value === false) {
  182. return '(bool)FALSE';
  183. } elseif (is_numeric($value) && is_float($value)) {
  184. return '(float)' . $value;
  185. } elseif (is_numeric($value) && is_int($value)) {
  186. return '(int)' . $value;
  187. } elseif (is_string($value)) {
  188. return '(string)' . $value;
  189. } elseif (is_object($value)) {
  190. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  191. '</pre>';
  192. } else {
  193. return '(unknown)' . $value;
  194. }
  195. }
  196. /**
  197. * Quickly dump a $var
  198. *
  199. * @param mixed $var
  200. * @return void
  201. */
  202. function dump($var) {
  203. if (!class_exists('Debugger')) {
  204. App::uses('Debugger', 'Utility');
  205. }
  206. return Debugger::dump($var);
  207. }
  208. /**
  209. * Returns htmlentities - string
  210. *
  211. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  212. * ENT_QUOTES = Will convert both double and single quotes. !!!
  213. * ENT_NOQUOTES = Will leave both double and single quotes unconverted.
  214. *
  215. * @param string $text
  216. * @return string Converted text
  217. */
  218. function ent($text) {
  219. return (!empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '');
  220. }
  221. /**
  222. * Convenience method for htmlspecialchars_decode
  223. *
  224. * @param string $text Text to wrap through htmlspecialchars_decode
  225. * @return string Converted text
  226. */
  227. function hDec($text, $quoteStyle = ENT_QUOTES) {
  228. if (is_array($text)) {
  229. return array_map('hDec', $text);
  230. }
  231. return trim(htmlspecialchars_decode($text, $quoteStyle));
  232. }
  233. /**
  234. * Convenience method for html_entity_decode
  235. *
  236. * @param string $text Text to wrap through htmlspecialchars_decode
  237. * @return string Converted text
  238. */
  239. function entDec($text, $quoteStyle = ENT_QUOTES) {
  240. if (is_array($text)) {
  241. return array_map('entDec', $text);
  242. }
  243. return (!empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '');
  244. }
  245. /**
  246. * Focus is on the filename (without path)
  247. *
  248. * //TODO: switch parameters!!!
  249. *
  250. * @return mixed
  251. */
  252. function extractFileInfo($type = null, $filename) {
  253. if ($info = extractPathInfo($type, $filename)) {
  254. return $info;
  255. }
  256. $pos = strrpos($filename, '.');
  257. $res = '';
  258. switch ($type) {
  259. case 'extension':
  260. case 'ext':
  261. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  262. break;
  263. case 'filename':
  264. case 'file':
  265. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  266. break;
  267. default:
  268. break;
  269. }
  270. return $res;
  271. }
  272. /**
  273. * Uses native PHP function to retrieve infos about a filename etc.
  274. * Improves it by not returning non-file-name characters from url files if specified.
  275. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  276. *
  277. * //TODO: switch parameters!!!
  278. *
  279. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  280. * @param string filename to check on
  281. * @return mixed
  282. */
  283. function extractPathInfo($type = null, $filename, $fromUrl = false) {
  284. switch ($type) {
  285. case 'extension':
  286. case 'ext':
  287. $infoType = PATHINFO_EXTENSION;
  288. break;
  289. case 'filename':
  290. case 'file':
  291. $infoType = PATHINFO_FILENAME;
  292. break;
  293. case 'basename':
  294. case 'base':
  295. $infoType = PATHINFO_BASENAME;
  296. break;
  297. case 'dirname':
  298. case 'dir':
  299. $infoType = PATHINFO_DIRNAME;
  300. break;
  301. default:
  302. $infoType = null;
  303. }
  304. $result = pathinfo($filename, $infoType);
  305. if ($fromUrl) {
  306. if (($pos = strpos($result, '#')) !== false) {
  307. $result = substr($result, 0, $pos);
  308. }
  309. if (($pos = strpos($result, '?')) !== false) {
  310. $result = substr($result, 0, $pos);
  311. }
  312. }
  313. return $result;
  314. }
  315. /**
  316. * Shows pr() messages, even with debug = 0.
  317. * Also allows additional customization.
  318. *
  319. * @param mixed $content
  320. * @param bool $collapsedAndExpandable
  321. * @param array $options
  322. * - class, showHtml, showFrom, jquery, returns, debug
  323. * @return string HTML
  324. */
  325. function pre($var, $collapsedAndExpandable = false, $options = []) {
  326. $defaults = [
  327. 'class' => 'cake-debug',
  328. 'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
  329. 'showFrom' => false, // Display file + line
  330. 'jquery' => null, // null => Auto - use jQuery (true/false to manually decide),
  331. 'returns' => false, // Use returns(),
  332. 'debug' => false // Show only with debug > 0
  333. ];
  334. $options += $defaults;
  335. if ($options['debug'] && !Configure::read('debug')) {
  336. return '';
  337. }
  338. if (php_sapi_name() === 'cli') {
  339. return sprintf("\n%s\n", $options['returns'] ? returns($var) : print_r($var, true));
  340. }
  341. $res = '<div class="' . $options['class'] . '">';
  342. $pre = '';
  343. if ($collapsedAndExpandable) {
  344. $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\'}';
  345. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  346. if ($options['jquery'] === true) {
  347. $js = $jsJquery;
  348. } elseif ($options['jquery'] !== false) {
  349. // auto
  350. $js = 'if (typeof jQuery == \'undefined\') {' . $js . '} else {' . $jsJquery . '}';
  351. }
  352. $res .= '<span onclick="' . $js . '" style="cursor: pointer; font-weight: bold">Debug</span>';
  353. if ($options['showFrom']) {
  354. $calledFrom = debug_backtrace();
  355. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  356. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  357. $res .= '<div>' . $from . '</div>';
  358. }
  359. $pre = ' style="display: none"';
  360. }
  361. if ($options['returns']) {
  362. $var = returns($var);
  363. } else {
  364. $var = print_r($var, true);
  365. }
  366. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  367. $res .= '</div>';
  368. return $res;
  369. }
  370. /**
  371. * Checks if the string [$haystack] contains [$needle]
  372. *
  373. * @param string $haystack Input string.
  374. * @param string $needle Needed char or string.
  375. * @return bool
  376. */
  377. function contains($haystack, $needle, $caseSensitive = false) {
  378. $result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);
  379. return ($result !== false);
  380. }
  381. /**
  382. * Checks if the string [$haystack] starts with [$needle]
  383. *
  384. * @param string $haystack Input string.
  385. * @param string $needle Needed char or string.
  386. * @return bool
  387. */
  388. function startsWith($haystack, $needle, $caseSensitive = false) {
  389. if ($caseSensitive) {
  390. return (mb_strpos($haystack, $needle) === 0);
  391. }
  392. return (mb_stripos($haystack, $needle) === 0);
  393. }
  394. /**
  395. * Checks if the String [$haystack] ends with [$needle]
  396. *
  397. * @param string $haystack Input string.
  398. * @param string $needle Needed char or string
  399. * @return bool
  400. */
  401. function endsWith($haystack, $needle, $caseSensitive = false) {
  402. if ($caseSensitive) {
  403. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  404. }
  405. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  406. }