bootstrap.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 (cake) 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. define('DEFAULT_DATETIME', '0000-00-00 00:00:00');
  51. define('DEFAULT_DATE', '0000-00-00');
  52. define('DEFAULT_TIME', '00:00:00');
  53. # workpaths
  54. define('FILES', APP . 'files' . DS);
  55. define('LOCALE', APP . 'locale' . DS);
  56. if (!defined('CLASS_USER')) {
  57. define('CLASS_USER', 'User');
  58. }
  59. # Validation ## (minus should be "hyphen")
  60. /** Valid characters: letters only */
  61. define('VALID_ALPHA', '/^[a-zA-Z]+$/');
  62. /** Valid characters: letters,underscores only */
  63. define('VALID_ALPHA_UNDERSCORES', '/^[a-zA-Z_]+$/');
  64. /** Valid characters: letters,underscores,minus only */
  65. define('VALID_ALPHA_MINUS_UNDERSCORES', '/^[a-zA-Z_-]+$/');
  66. /** Valid characters: letters,spaces only */
  67. define('VALID_ALPHA_WHITESPACES', '/^[a-zA-Z ]+$/');
  68. /** Valid characters: letters,numbers,underscores only */
  69. define('VALID_ALPHANUMERIC_UNDERSCORES', '/^[\da-zA-Z_]+$/');
  70. /** Valid characters: letters,numbers,underscores,minus only */
  71. define('VALID_ALPHANUMERIC_MINUS_UNDERSCORES', '/^[\da-zA-Z_-]+$/');
  72. /** Valid characters: letters,numbers,spaces only */
  73. define('VALID_ALPHANUMERIC_WHITESPACES', '/^[\da-zA-Z ]+$/');
  74. /** Valid characters: letters,numbers,spaces,underscores only */
  75. define('VALID_ALPHANUMERIC_WHITESPACES_UNDERSCORES', '/^[\da-zA-Z _]+$/');
  76. /** Valid characters: numbers,underscores only */
  77. define('VALID_NUMERIC_UNDERSCORES', '/^[\d_]+$/');
  78. /** Valid characters: numbers,spaces only */
  79. define('VALID_NUMERIC_WHITESPACES', '/^[\d ]+$/');
  80. /** Valid characters: numbers,spaces,underscores only */
  81. define('VALID_NUMERIC_WHITESPACES_UNDERSCORES', '/^[\d _]+$/');
  82. /** Valid integers: > 0 */
  83. define('VALID_INTEGERS', '/^[\d]+$/'); //??
  84. if (!defined('FORMAT_NICE_YMDHMS')) {
  85. define('FORMAT_NICE_YMDHMS', 'd.m.Y, H:i:s');
  86. define('FORMAT_NICE_YMDHM', 'd.m.Y, H:i');
  87. define('FORMAT_NICE_YM', 'm.Y');
  88. define('FORMAT_NICE_YMD', 'd.m.Y');
  89. define('FORMAT_NICE_MD', 'd.m.');
  90. define('FORMAT_NICE_D', 'd'); # xx
  91. define('FORMAT_NICE_W_NUM', 'w'); # xx (0=sunday to 6=saturday)
  92. define('FORMAT_NICE_W_ABBR', 'D'); # needs manual translation
  93. define('FORMAT_NICE_W_FULL', 'l'); # needs manual translation
  94. define('FORMAT_NICE_M', 'm'); # xx
  95. define('FORMAT_NICE_M_ABBR', 'M'); # needs manual translation
  96. define('FORMAT_NICE_M_FULL', 'F'); # needs manual translation
  97. define('FORMAT_NICE_Y_ABBR', 'y'); # xx
  98. define('FORMAT_NICE_Y', 'Y'); # xxxx
  99. define('FORMAT_NICE_HM', 'H:i');
  100. define('FORMAT_NICE_HMS', 'H:i:s');
  101. // localDate strings
  102. define('FORMAT_LOCAL_WA_YMDHMS', '%a, %d.%m.%Y, %H:%M:%S');
  103. define('FORMAT_LOCAL_WF_YMDHMS', '%A, %d.%m.%Y, %H:%M:%S');
  104. define('FORMAT_LOCAL_WA_YMDHM', '%a, %d.%m.%Y, %H:%M');
  105. define('FORMAT_LOCAL_WF_YMDHM', '%A, %d.%m.%Y, %H:%M');
  106. define('FORMAT_LOCAL_YMDHMS', '%d.%m.%Y, %H:%M:%S');
  107. define('FORMAT_LOCAL_YMDHM', '%d.%m.%Y, %H:%M');
  108. define('FORMAT_LOCAL_YMD', '%d.%m.%Y');
  109. define('FORMAT_LOCAL_MD', '%d.%m.');
  110. define('FORMAT_LOCAL_D', '%d'); # xx
  111. define('FORMAT_LOCAL_W_NUM', '%w'); # xx (0=sunday to 6=saturday)
  112. define('FORMAT_LOCAL_W_ABBR', '%a'); # needs translation
  113. define('FORMAT_LOCAL_W_FULL', '%A'); # needs translation
  114. define('FORMAT_LOCAL_M', '%m'); # xx
  115. define('FORMAT_LOCAL_M_ABBR', '%b'); # needs translation
  116. define('FORMAT_LOCAL_M_FULL', '%B'); # needs translation
  117. define('FORMAT_LOCAL_Y_ABBR', '%y'); # xx
  118. define('FORMAT_LOCAL_Y', '%Y'); # xxxx
  119. define('FORMAT_LOCAL_H', '%H');
  120. define('FORMAT_LOCAL_S', '%S');
  121. define('FORMAT_LOCAL_HM', '%H:%M');
  122. define('FORMAT_LOCAL_HMS', '%H:%M:%S');
  123. }
  124. /*** chars ***/
  125. /* see http://www.htmlcodetutorial.com/characterentities_famsupp_69.html */
  126. define('CHAR_LESS', '&lt;'); # <
  127. define('CHAR_GREATER', '&gt;'); # >
  128. define('CHAR_QUOTE', '&quot;'); # "
  129. define('CHAR_APOSTROPHE', '&#39'); # '
  130. define('CHAR_ARROWS', '&raquo;'); # »
  131. define('CHAR_ARROWS_R', '&#187;'); # »
  132. define('CHAR_ARROWS_L', '&#171;'); # «
  133. define('CHAR_AVERAGE', '&#216;'); # Ø
  134. define('CHAR_INFIN', '&infin;'); # 8
  135. define('CHAR_MILL', '&#137;'); # ‰ (per mille) / or &permil;
  136. define('CHAR_PLUSMN', '&plusmn;'); # 8
  137. define('CHAR_HELLIP', '&#8230;'); # … (horizontal ellipsis = three dot leader)
  138. define('CHAR_CIRCA', '&asymp;'); # ˜ (almost equal to)
  139. define('CHAR_CHECKBOX_EMPTY', '&#9744;]'); #
  140. define('CHAR_CHECKBOX_MAKRED', '&#9745'); #
  141. define('CHAR_CHECKMARK', '&#10003;');
  142. define('CHAR_CHECKMARK_BOLD', '&#10004;');
  143. define('CHAR_BALLOT', '&#10007;');
  144. define('CHAR_BALLOT_BOLD', '&#10008;');
  145. define('CHAR_ABOUT', '&asymp;'); # … (horizontal ellipsis = three dot leader)
  146. /* not very often used */
  147. define('CHAR_RPIME', '&#8242;'); # ' (minutes)
  148. define('CHAR_DOUBLE_RPIME', '&#8243;'); # ? (seconds)
  149. /** BASIC FUNCTIONS **/
  150. /**
  151. * Own slug function - containing extra char replacement
  152. *
  153. * //TODO move to TextLib?
  154. *
  155. * @return string
  156. */
  157. function slug($string, $separator = null, $low = true) {
  158. $additionalSlugElements = array(
  159. '/º|°/' => 0,
  160. '/¹/' => 1,
  161. '/²/' => 2,
  162. '/³/' => 3,
  163. // new utf8 char "capitel ß" still missing here! '/.../' => 'SS', (TODO in 2009)
  164. '/@/' => 'at',
  165. '/æ/' => 'ae',
  166. '/©/' => 'C',
  167. '/ç|¢/' => 'c',
  168. '/Ð/' => 'D',
  169. '/€/' => 'EUR',
  170. '/™/' => 'TM',
  171. // more missing?
  172. );
  173. if ($separator === null) {
  174. $separator = defined('SEO_SEPARATOR') ? SEO_SEPARATOR : '-';
  175. }
  176. $res = Inflector::slug($string, $separator, $additionalSlugElements);
  177. if ($low) {
  178. $res = strtolower($res);
  179. }
  180. return $res;
  181. }
  182. /**
  183. * Since nl2br doesn't remove the line breaks when adding in the <br /> tags,
  184. * it is necessary to strip those off before you convert all of the tags, otherwise you will get double spacing
  185. *
  186. * //TODO: move to TextLib?
  187. *
  188. * @param string $str
  189. * @return string
  190. */
  191. function br2nl($str) {
  192. $str = preg_replace("/(\r\n|\r|\n)/", "", $str);
  193. return preg_replace("=<br */?>=i", "\n", $str);
  194. }
  195. /**
  196. * Replaces CRLF with spaces
  197. *
  198. * //TODO: move to TextLib?
  199. *
  200. * @param string $text Any text
  201. * @return string Safe string without new lines
  202. */
  203. function safenl($str) {
  204. //$str = str_replace(chr(13).chr(10), " ", $str); # \r\n
  205. //$str = str_replace(chr(13), " ", $str); # \r
  206. //$str = str_replace(chr(10), " ", $str); # \n
  207. $str = preg_replace("/(\r\n|\r|\n)/", " ", $str);
  208. return $str;
  209. }
  210. /**
  211. * Convenience function to check on "empty()"
  212. *
  213. * @param mixed $var
  214. * @return bool Result
  215. */
  216. function isEmpty($var = null) {
  217. if (empty($var)) {
  218. return true;
  219. }
  220. return false;
  221. }
  222. /**
  223. * Return of what type the specific value is
  224. *
  225. * //TODO: use Debugger::exportVar() instead?
  226. *
  227. * @param mixed $value
  228. * @return type (NULL, array, bool, float, int, string, object, unknown) + value
  229. */
  230. function returns($value) {
  231. if ($value === null) {
  232. return 'NULL';
  233. } elseif (is_array($value)) {
  234. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  235. } elseif ($value === true) {
  236. return '(bool)TRUE';
  237. } elseif ($value === false) {
  238. return '(bool)FALSE';
  239. } elseif (is_numeric($value) && is_float($value)) {
  240. return '(float)' . $value;
  241. } elseif (is_numeric($value) && is_int($value)) {
  242. return '(int)' . $value;
  243. } elseif (is_string($value)) {
  244. return '(string)' . $value;
  245. } elseif (is_object($value)) {
  246. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  247. '</pre>';
  248. } else {
  249. return '(unknown)' . $value;
  250. }
  251. }
  252. /**
  253. * Quickly dump a $var
  254. *
  255. * @param mixed $var
  256. * @return void
  257. */
  258. function dump($var) {
  259. if (!class_exists('Debugger')) {
  260. App::uses('Debugger', 'Utility');
  261. }
  262. return Debugger::dump($var);
  263. }
  264. /**
  265. * Returns htmlentities - string
  266. *
  267. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  268. * ENT_QUOTES = Will convert both double and single quotes. !!!
  269. * ENT_NOQUOTES = Will leave both double and single quotes unconverted.
  270. *
  271. * @param string $text
  272. * @return string Converted text
  273. */
  274. function ent($text) {
  275. return (!empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '');
  276. }
  277. /**
  278. * Convenience method for htmlspecialchars_decode
  279. *
  280. * @param string $text Text to wrap through htmlspecialchars_decode
  281. * @return string Converted text
  282. */
  283. function hDec($text, $quoteStyle = ENT_QUOTES) {
  284. if (is_array($text)) {
  285. return array_map('hDec', $text);
  286. }
  287. return trim(htmlspecialchars_decode($text, $quoteStyle));
  288. }
  289. /**
  290. * Convenience method for html_entity_decode
  291. *
  292. * @param string $text Text to wrap through htmlspecialchars_decode
  293. * @return string Converted text
  294. */
  295. function entDec($text, $quoteStyle = ENT_QUOTES) {
  296. if (is_array($text)) {
  297. return array_map('entDec', $text);
  298. }
  299. return (!empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '');
  300. }
  301. /**
  302. * Focus is on the filename (without path)
  303. *
  304. * //TODO: switch parameters!!!
  305. *
  306. * @return mixed
  307. */
  308. function extractFileInfo($type = null, $filename) {
  309. if ($info = extractPathInfo($type, $filename)) {
  310. return $info;
  311. }
  312. $pos = strrpos($filename, '.');
  313. $res = '';
  314. switch ($type) {
  315. case 'extension':
  316. case 'ext':
  317. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  318. break;
  319. case 'filename':
  320. case 'file':
  321. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  322. break;
  323. default:
  324. break;
  325. }
  326. return $res;
  327. }
  328. /**
  329. * Uses native PHP function to retrieve infos about a filename etc.
  330. * Improves it by not returning non-file-name characters from url files if specified.
  331. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  332. *
  333. * //TODO: switch parameters!!!
  334. *
  335. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  336. * @param string filename to check on
  337. * @return mixed
  338. */
  339. function extractPathInfo($type = null, $filename, $fromUrl = false) {
  340. switch ($type) {
  341. case 'extension':
  342. case 'ext':
  343. $infoType = PATHINFO_EXTENSION;
  344. break;
  345. case 'filename':
  346. case 'file':
  347. $infoType = PATHINFO_FILENAME;
  348. break;
  349. case 'basename':
  350. case 'base':
  351. $infoType = PATHINFO_BASENAME;
  352. break;
  353. case 'dirname':
  354. case 'dir':
  355. $infoType = PATHINFO_DIRNAME;
  356. break;
  357. default:
  358. $infoType = null;
  359. }
  360. $result = pathinfo($filename, $infoType);
  361. if ($fromUrl) {
  362. if (($pos = strpos($result, '#')) !== false) {
  363. $result = substr($result, 0, $pos);
  364. }
  365. if (($pos = strpos($result, '?')) !== false) {
  366. $result = substr($result, 0, $pos);
  367. }
  368. }
  369. return $result;
  370. }
  371. /**
  372. * Shows pr() messages, even with debug = 0.
  373. * Also allows additional customization.
  374. *
  375. * @param mixed $content
  376. * @param bool $collapsedAndExpandable
  377. * @param array $options
  378. * - class, showHtml, showFrom, jquery, returns, debug
  379. * @return string HTML
  380. */
  381. function pre($var, $collapsedAndExpandable = false, $options = array()) {
  382. $defaults = array(
  383. 'class' => 'cake-debug',
  384. 'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
  385. 'showFrom' => false, // Display file + line
  386. 'jquery' => null, // null => Auto - use jQuery (true/false to manually decide),
  387. 'returns' => false, // Use returns(),
  388. 'debug' => false // Show only with debug > 0
  389. );
  390. $options = array_merge($defaults, $options);
  391. if ($options['debug'] && !Configure::read('debug')) {
  392. return '';
  393. }
  394. if (php_sapi_name() === 'cli') {
  395. return sprintf("\n%s\n", $options['returns'] ? returns($var) : print_r($var, true));
  396. }
  397. $res = '<div class="' . $options['class'] . '">';
  398. $pre = '';
  399. if ($collapsedAndExpandable) {
  400. $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\'}';
  401. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  402. if ($options['jquery'] === true) {
  403. $js = $jsJquery;
  404. } elseif ($options['jquery'] !== false) {
  405. // auto
  406. $js = 'if (typeof jQuery == \'undefined\') {' . $js . '} else {' . $jsJquery . '}';
  407. }
  408. $res .= '<span onclick="' . $js . '" style="cursor: pointer; font-weight: bold">Debug</span>';
  409. if ($options['showFrom']) {
  410. $calledFrom = debug_backtrace();
  411. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  412. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  413. $res .= '<div>' . $from . '</div>';
  414. }
  415. $pre = ' style="display: none"';
  416. }
  417. if ($options['returns']) {
  418. $var = returns($var);
  419. } else {
  420. $var = print_r($var, true);
  421. }
  422. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  423. $res .= '</div>';
  424. return $res;
  425. }
  426. /**
  427. * Checks if the string [$haystack] contains [$needle]
  428. *
  429. * @param string $haystack Input string.
  430. * @param string $needle Needed char or string.
  431. * @return bool
  432. */
  433. function contains($haystack, $needle, $caseSensitive = false) {
  434. $result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);
  435. return ($result !== false);
  436. }
  437. /**
  438. * Checks if the string [$haystack] starts with [$needle]
  439. *
  440. * @param string $haystack Input string.
  441. * @param string $needle Needed char or string.
  442. * @return bool
  443. */
  444. function startsWith($haystack, $needle, $caseSensitive = false) {
  445. if ($caseSensitive) {
  446. return (mb_strpos($haystack, $needle) === 0);
  447. }
  448. return (mb_stripos($haystack, $needle) === 0);
  449. }
  450. /**
  451. * Checks if the String [$haystack] ends with [$needle]
  452. *
  453. * @param string $haystack Input string.
  454. * @param string $needle Needed char or string
  455. * @return bool
  456. */
  457. function endsWith($haystack, $needle, $caseSensitive = false) {
  458. if ($caseSensitive) {
  459. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  460. }
  461. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  462. }