MyBootstrap.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <?php
  2. App::uses('Utility', 'Tools.Utility');
  3. /** BASIC STUFF **/
  4. //use FULL_BASE_URL (cake) instead of http_base?
  5. if (!empty($_SERVER['HTTP_HOST'])) {
  6. define('HTTP_HOST', $_SERVER['HTTP_HOST']);
  7. define('HTTP_BASE', 'http://' . HTTP_HOST); //FULL_BASE_URL
  8. } else {
  9. define('HTTP_HOST', '');
  10. define('HTTP_BASE', '');
  11. }
  12. if (!empty($_SERVER['PHP_SELF'])) {
  13. define('HTTP_SELF', '' . $_SERVER['PHP_SELF']);
  14. }
  15. if (!empty($_SERVER['REQUEST_URI'])) {
  16. define('HTTP_URI', '' . $_SERVER['REQUEST_URI']);
  17. }
  18. if (!empty($_SERVER['HTTP_REFERER'])) {
  19. define('HTTP_REF', '' . $_SERVER['HTTP_REFERER']);
  20. }
  21. define('CHOWN_PUBLIC', 0770);
  22. # Useful when putting a string together that needs some "pretty" html-doc. source layouting
  23. # Only visible in SOURCE code (not in html layout in the browser)
  24. //define('LF',''); // line feed (depending on the system)
  25. define('LF', PHP_EOL); // line feed (depending on the system): \n or \n\r etc.
  26. # Alternativly NL,CR:
  27. define('NL', "\n"); // new line
  28. define('CR', "\r"); // carriage return
  29. define('TB', "\t"); // tabulator
  30. # Useful for html layouting
  31. # Visible in the Html Layout in the Browser
  32. define('BR', '<br />'); // line break
  33. # Make the app and l10n play nice with Windows.
  34. if (substr(PHP_OS, 0, 3) == 'WIN') { // || strpos(@php_uname(), 'ARCH')
  35. define('WINDOWS', true);
  36. } else {
  37. define('WINDOWS', false);
  38. }
  39. define('FORMAT_DB_DATETIME', 'Y-m-d H:i:s'); // date(...)
  40. define('FORMAT_DB_DATE', 'Y-m-d');
  41. define('FORMAT_DB_TIME', 'H:i:s');
  42. define('DEFAULT_DATETIME', '0000-00-00 00:00:00');
  43. define('DEFAULT_DATE', '0000-00-00');
  44. define('DEFAULT_TIME', '00:00:00');
  45. # deprecated (could be wrong, if timezone is modified)
  46. define('CURRENT_YEAR', date('Y'));
  47. define('CURRENT_MONTH', date('m'));
  48. define('CURRENT_DAY', date('d'));
  49. # workpaths
  50. define('FILES', APP . 'files' . DS);
  51. define('LOCALE', APP . 'locale' . DS);
  52. # Validation ## (minus should be "hyphen")
  53. /** Valid characters: letters only */
  54. define('VALID_ALPHA', '/^[a-zA-Z]+$/');
  55. /** Valid characters: letters,underscores only */
  56. define('VALID_ALPHA_UNDERSCORES', '/^[a-zA-Z_]+$/');
  57. /** Valid characters: letters,underscores,minus only */
  58. define('VALID_ALPHA_MINUS_UNDERSCORES', '/^[a-zA-Z_-]+$/');
  59. /** Valid characters: letters,spaces only */
  60. define('VALID_ALPHA_WHITESPACES', '/^[a-zA-Z ]+$/');
  61. /** Valid characters: letters,numbers,underscores only */
  62. define('VALID_ALPHANUMERIC_UNDERSCORES', '/^[\da-zA-Z_]+$/');
  63. /** Valid characters: letters,numbers,underscores,minus only */
  64. define('VALID_ALPHANUMERIC_MINUS_UNDERSCORES', '/^[\da-zA-Z_-]+$/');
  65. /** Valid characters: letters,numbers,spaces only */
  66. define('VALID_ALPHANUMERIC_WHITESPACES', '/^[\da-zA-Z ]+$/');
  67. /** Valid characters: letters,numbers,spaces,underscores only */
  68. define('VALID_ALPHANUMERIC_WHITESPACES_UNDERSCORES', '/^[\da-zA-Z _]+$/');
  69. /** Valid characters: numbers,underscores only */
  70. define('VALID_NUMERIC_UNDERSCORES', '/^[\d_]+$/');
  71. /** Valid characters: numbers,spaces only */
  72. define('VALID_NUMERIC_WHITESPACES', '/^[\d ]+$/');
  73. /** Valid characters: numbers,spaces,underscores only */
  74. define('VALID_NUMERIC_WHITESPACES_UNDERSCORES', '/^[\d _]+$/');
  75. /** Valid integers: > 0 */
  76. define('VALID_INTEGERS', '/^[\d]+$/'); //??
  77. if (!defined('FORMAT_NICE_YMDHMS')) {
  78. define('FORMAT_NICE_YMDHMS','d.m.Y, H:i:s');
  79. define('FORMAT_NICE_YMDHM','d.m.Y, H:i');
  80. define('FORMAT_NICE_YM','m.Y');
  81. define('FORMAT_NICE_YMD','d.m.Y');
  82. define('FORMAT_NICE_MD','d.m.');
  83. define('FORMAT_NICE_D','d'); # xx
  84. define('FORMAT_NICE_W_NUM','w'); # xx (0=sunday to 6=saturday)
  85. define('FORMAT_NICE_W_ABBR','D'); # needs manual translation
  86. define('FORMAT_NICE_W_FULL','l'); # needs manual translation
  87. define('FORMAT_NICE_M','m'); # xx
  88. define('FORMAT_NICE_M_ABBR','M'); # needs manual translation
  89. define('FORMAT_NICE_M_FULL','F'); # needs manual translation
  90. define('FORMAT_NICE_Y_ABBR','y'); # xx
  91. define('FORMAT_NICE_Y','Y'); # xxxx
  92. define('FORMAT_NICE_HM','H:i');
  93. define('FORMAT_NICE_HMS','H:i:s');
  94. # localDate strings
  95. define('FORMAT_LOCAL_WA_YMDHMS','%a, %d.%m.%Y, %H:%M:%S');
  96. define('FORMAT_LOCAL_WF_YMDHMS','%A, %d.%m.%Y, %H:%M:%S');
  97. define('FORMAT_LOCAL_WA_YMDHM','%a, %d.%m.%Y, %H:%M');
  98. define('FORMAT_LOCAL_WF_YMDHM','%A, %d.%m.%Y, %H:%M');
  99. define('FORMAT_LOCAL_YMDHMS','%d.%m.%Y, %H:%M:%S');
  100. define('FORMAT_LOCAL_YMDHM','%d.%m.%Y, %H:%M');
  101. define('FORMAT_LOCAL_YMD','%d.%m.%Y');
  102. define('FORMAT_LOCAL_MD','%d.%m.');
  103. define('FORMAT_LOCAL_D','%d'); # xx
  104. define('FORMAT_LOCAL_W_NUM','%w'); # xx (0=sunday to 6=saturday)
  105. define('FORMAT_LOCAL_W_ABBR','%a'); # needs translation
  106. define('FORMAT_LOCAL_W_FULL','%A'); # needs translation
  107. define('FORMAT_LOCAL_M','%m'); # xx
  108. define('FORMAT_LOCAL_M_ABBR','%b'); # needs translation
  109. define('FORMAT_LOCAL_M_FULL','%B'); # needs translation
  110. define('FORMAT_LOCAL_Y_ABBR','%y'); # xx
  111. define('FORMAT_LOCAL_Y','%Y'); # xxxx
  112. define('FORMAT_LOCAL_H','%H');
  113. define('FORMAT_LOCAL_S','%S');
  114. define('FORMAT_LOCAL_HM','%H:%i');
  115. define('FORMAT_LOCAL_HMS','%H:%M:%S');
  116. }
  117. /*** chars ***/
  118. /* see http://www.htmlcodetutorial.com/characterentities_famsupp_69.html */
  119. define('CHAR_LESS', '&lt;'); # <
  120. define('CHAR_GREATER', '&gt;'); # >
  121. define('CHAR_QUOTE', '&quot;'); # "
  122. define('CHAR_APOSTROPHE', '&#39'); # '
  123. define('CHAR_ARROWS', '&raquo;'); # »
  124. define('CHAR_ARROWS_R', '&#187;'); # »
  125. define('CHAR_ARROWS_L', '&#171;'); # «
  126. define('CHAR_AVERAGE', '&#216;'); # Ø
  127. define('CHAR_INFIN', '&infin;'); # 8
  128. define('CHAR_MILL', '&#137;'); # ‰ (per mille) / or &permil;
  129. define('CHAR_PLUSMN', '&plusmn;'); # 8
  130. define('CHAR_HELLIP', '&#8230;'); # … (horizontal ellipsis = three dot leader)
  131. define('CHAR_CIRCA', '&asymp;'); # ˜ (almost equal to)
  132. define('CHAR_CHECKBOX_EMPTY', '&#9744;]'); #
  133. define('CHAR_CHECKBOX_MAKRED', '&#9745'); #
  134. define('CHAR_CHECKMARK', '&#10003;');
  135. define('CHAR_CHECKMARK_BOLD', '&#10004;');
  136. define('CHAR_BALLOT', '&#10007;');
  137. define('CHAR_BALLOT_BOLD', '&#10008;');
  138. define('CHAR_ABOUT','&asymp;'); # … (horizontal ellipsis = three dot leader)
  139. /* not very often used */
  140. define('CHAR_RPIME', '&#8242;'); # ' (minutes)
  141. define('CHAR_DOUBLE_RPIME', '&#8243;'); # ? (seconds)
  142. /** BASIC FUNCTIONS **/
  143. /**
  144. * own slug function
  145. * 2010-11-07 ms
  146. */
  147. function slug($string, $separator = null, $low = true) {
  148. $additionalSlugElements = array(
  149. '/º|°/' => 0,
  150. '/¹/' => 1,
  151. '/²/' => 2,
  152. '/³/' => 3,
  153. # new utf8 char "capitel ß" still missing here! '/.../' => 'SS', (TODO in 2009)
  154. '/@/' => 'at',
  155. '/æ/' => 'ae',
  156. '/©/' => 'C',
  157. '/ç|¢/' => 'c',
  158. '/Ð/' => 'D',
  159. '/€/' => 'EUR',
  160. '/™/' => 'TM',
  161. # more missing?
  162. );
  163. if ($separator === null) {
  164. $separator = defined('SEO_SEPARATOR') ? SEO_SEPARATOR : '-';
  165. }
  166. $res = Inflector::slug($string, $separator, $additionalSlugElements);
  167. if ($low) {
  168. $res = strtolower($res);
  169. }
  170. return $res;
  171. }
  172. /**
  173. * Since nl2br doesn't remove the line breaks when adding in the <br /> tags,
  174. * it is necessary to strip those off before you convert all of the tags, otherwise you will get double spacing
  175. * @param string $str
  176. * @return string
  177. * 2010-11-07 ms
  178. */
  179. function br2nl($str) {
  180. $str = preg_replace("/(\r\n|\r|\n)/", "", $str);
  181. return preg_replace("=<br */?>=i", "\n", $str);
  182. }
  183. /**
  184. * Replaces CRLF with spaces
  185. *
  186. * @param string $text Any text
  187. * @return string Safe string without new lines
  188. * 2010-11-14 ms
  189. */
  190. function safenl($str) {
  191. //$str = str_replace(chr(13).chr(10), " ", $str); # \r\n
  192. //$str = str_replace(chr(13), " ", $str); # \r
  193. //$str = str_replace(chr(10), " ", $str); # \n
  194. $str = preg_replace("/(\r\n|\r|\n)/", " ", $str);
  195. return $str;
  196. }
  197. /**
  198. * @param array $keyValuePairs
  199. * @return string $key
  200. * like array_shift() only for keys and not values
  201. * 2011-01-22 ms
  202. */
  203. function arrayShiftKeys(&$array) {
  204. trigger_error('deprecated - use Tools.Utility instead');
  205. //TODO: improve?
  206. foreach ($array as $key => $value) {
  207. unset($array[$key]);
  208. return $key;
  209. }
  210. }
  211. /**
  212. * Flattens an array, or returns FALSE on fail.
  213. * 2011-07-02 ms
  214. */
  215. function arrayFlatten($array) {
  216. trigger_error('deprecated - use Tools.Utility instead');
  217. if (!is_array($array)) {
  218. return false;
  219. }
  220. $result = array();
  221. foreach ($array as $key => $value) {
  222. if (is_array($value)) {
  223. $result = array_merge($result, arrayFlatten($value));
  224. } else {
  225. $result[$key] = $value;
  226. }
  227. }
  228. return $result;
  229. }
  230. /**
  231. * convenience function to check on "empty()"
  232. * 2009-06-15 ms
  233. */
  234. function isEmpty($var = null) {
  235. if (empty($var)) {
  236. return true;
  237. }
  238. return false;
  239. }
  240. /**
  241. * //TODO: use Debugger::exportVar() instead?
  242. * of what type is the specific value
  243. * @return type (NULL, array, bool, float, int, string, object, unknown) + value
  244. * 2009-03-03 ms
  245. */
  246. function returns($value) {
  247. if ($value === null) {
  248. return 'NULL';
  249. } elseif (is_array($value)) {
  250. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  251. } elseif ($value === true) {
  252. return '(bool)TRUE';
  253. } elseif ($value === false) {
  254. return '(bool)FALSE';
  255. } elseif (is_numeric($value) && is_float($value)) {
  256. return '(float)' . $value;
  257. } elseif (is_numeric($value) && is_int($value)) {
  258. return '(int)' . $value;
  259. } elseif (is_string($value)) {
  260. return '(string)' . $value;
  261. } elseif (is_object($value)) {
  262. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  263. '</pre>';
  264. } else {
  265. return '(unknown)' . $value;
  266. }
  267. }
  268. function dump($var) {
  269. if (class_exists('Debugger')) {
  270. App::import('Core', 'Debugger');
  271. }
  272. return Debugger::dump($var);
  273. }
  274. /**
  275. * Returns htmlentities - string
  276. *
  277. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  278. * ENT_QUOTES = Will convert both double and single quotes. !!!
  279. * ENT_NOQUOTES= Will leave both double and single quotes unconverted.
  280. */
  281. function ent($text) {
  282. return (!empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '');
  283. }
  284. /**
  285. * Convenience method for htmlspecialchars_decode
  286. *
  287. * @param string $text Text to wrap through htmlspecialchars_decode
  288. * @return string Wrapped text
  289. * 2011-04-03 ms
  290. */
  291. function hDec($text, $quoteStyle = ENT_QUOTES) {
  292. if (is_array($text)) {
  293. return array_map('hDec', $text);
  294. }
  295. return htmlspecialchars_decode($text, $quoteStyle);
  296. }
  297. function entDec($text, $quoteStyle = ENT_QUOTES) {
  298. return (!empty($text) ? html_entity_decode($text, $quoteStyle, 'UTF-8') :
  299. '');
  300. }
  301. /**
  302. * focus is on the filename (without path)
  303. * 2011-06-02 ms
  304. */
  305. function extractFileInfo($type = null, $filename) {
  306. if ($info = extractPathInfo($type, $filename)) {
  307. return $info;
  308. }
  309. $pos = strrpos($filename, '.');
  310. $res = '';
  311. switch ($type) {
  312. case 'extension':
  313. case 'ext':
  314. $res = ($pos !== false) ? substr($filename, $pos+1) : '';
  315. break;
  316. case 'filename':
  317. case 'file':
  318. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  319. break;
  320. default:
  321. break;
  322. }
  323. return $res;
  324. }
  325. /**
  326. * uses native PHP function to retrieve infos about a filename etc.
  327. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  328. * @param string filename to check on
  329. * //TODO: switch parameters!!!
  330. * 2009-01-22 ms
  331. */
  332. function extractPathInfo($type = null, $filename) {
  333. switch ($type) {
  334. case 'extension':
  335. case 'ext':
  336. $infoType = PATHINFO_EXTENSION;
  337. break;
  338. case 'filename':
  339. case 'file':
  340. $infoType = PATHINFO_FILENAME;
  341. break;
  342. case 'basename':
  343. case 'base':
  344. $infoType = PATHINFO_BASENAME;
  345. break;
  346. case 'dirname':
  347. case 'dir':
  348. $infoType = PATHINFO_DIRNAME;
  349. break;
  350. default:
  351. $infoType = null;
  352. }
  353. return pathinfo($filename, $infoType);
  354. }
  355. /**
  356. * Shows pr() messages, even with debug=0
  357. *
  358. * @param mixed $content
  359. * @param bool $collapsedAndExpandable
  360. * @param array $options
  361. * - class, showHtml, showFrom, jquery, returns, debug
  362. * 2011-01-19 ms
  363. */
  364. function pre($var, $collapsedAndExpandable = false, $options = array()) {
  365. $defaults = array(
  366. 'class' => 'cake-debug',
  367. 'showHtml' => false, # escape < and > (or manually escape with h() prior to calling this function)
  368. 'showFrom' => false, # display file + line
  369. 'jquery' => null, # auto - use jQuery (true/false to manually decide),
  370. 'returns' => false, # returns(),
  371. 'debug' => false # showOnlyOnDebug
  372. );
  373. $options = array_merge($defaults, $options);
  374. if ($options['debug'] && !Configure::read('debug')) {
  375. return '';
  376. }
  377. $res = '<div class="'.$options['class'].'">';
  378. $pre = '';
  379. if ($collapsedAndExpandable) {
  380. $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\'}';
  381. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  382. if ($options['jquery'] === true) {
  383. $js = $jsJquery;
  384. } elseif ($options['jquery'] !== false) {
  385. # auto
  386. $js = 'if (typeof jQuery == \'undefined\') {'.$js.'} else {'.$jsJquery.'}';
  387. }
  388. $res .= '<span onclick="'.$js.'" style="cursor: pointer; font-weight: bold">Debug</span>';
  389. if ($options['showFrom']) {
  390. $calledFrom = debug_backtrace();
  391. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  392. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  393. $res .= '<div>'.$from.'</div>';
  394. }
  395. $pre = ' style="display: none"';
  396. }
  397. if ($options['returns']) {
  398. $var = returns($var);
  399. } else {
  400. $var = print_r($var, true);
  401. }
  402. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  403. $res .= '</div>';
  404. return $res;
  405. }
  406. /**
  407. * Checks if the string [$haystack] contains [$needle]
  408. * @param string $haystack Input string.
  409. * @param string $needle Needed char or string.
  410. * @return boolean
  411. */
  412. function contains($haystack, $needle, $caseSensitive = false) {
  413. return (!$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle))
  414. !== false;
  415. }
  416. /**
  417. * Can compare two float values
  418. * @deprecated use NumberLib::isFloatEqual
  419. * @link http://php.net/manual/en/language.types.float.php
  420. * @return boolean
  421. */
  422. function isFloatEqual($x, $y, $precision = 0.0000001) {
  423. trigger_error('deprecated - use NumberLib::isFloatEqual instead');
  424. return ($x+$precision >= $y) && ($x-$precision <= $y);
  425. }
  426. /**
  427. * Checks if the string [$haystack] starts with [$needle]
  428. * @param string $haystack Input string.
  429. * @param string $needle Needed char or string.
  430. * @return boolean
  431. */
  432. function startsWith($haystack, $needle, $caseSensitive = false) {
  433. if ($caseSensitive) {
  434. return (mb_strpos($haystack, $needle) === 0);
  435. }
  436. return (mb_stripos($haystack, $needle) === 0);
  437. }
  438. /**
  439. * Checks if the String [$haystack] ends with [$needle]
  440. * @param string $haystack Input string.
  441. * @param string $needle Needed char or string
  442. * @return boolean
  443. */
  444. function endsWith($haystack, $needle, $caseSensitive = false) {
  445. if ($caseSensitive) {
  446. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  447. }
  448. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  449. }
  450. /* deprecated? */
  451. function isLoggedIn() {
  452. return isset($_SESSION) && !empty($_SESSION['Auth']['User']['id']);
  453. }
  454. /* deprecated? */
  455. function uid($default = null) {
  456. return (isset($_SESSION) && !empty($_SESSION['Auth']['User']['id'])) ? $_SESSION['Auth']['User']['id'] :
  457. $default;
  458. }
  459. register_shutdown_function('shutdownFunction');
  460. /**
  461. * own shutdown function - also logs fatal errors (necessary until cake2.2)
  462. * 2010-10-17 ms
  463. */
  464. function shutDownFunction() {
  465. $error = error_get_last();
  466. if (empty($error)) {
  467. return;
  468. }
  469. $matching = array(
  470. E_ERROR =>'E_ERROR',
  471. E_WARNING => 'E_WARNING',
  472. E_PARSE => 'E_',
  473. E_NOTICE => 'E_',
  474. E_CORE_ERROR => 'E_',
  475. E_COMPILE_ERROR => 'E_',
  476. E_COMPILE_WARNING => 'E_',
  477. E_STRICT => 'E_STRICT',
  478. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  479. E_DEPRECATED => 'E_DEPRECATED',
  480. );
  481. App::uses('CakeLog', 'Log');
  482. if (in_array($error['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR))) {
  483. $error['type_name'] = 'Fatal Error';
  484. $type = 'error';
  485. } elseif (Configure::read('Debug.log') && isset($matching[$error['type']])) {
  486. $error['type_name'] = 'Error';
  487. $type = 'notice';
  488. }
  489. if (!isset($type)) {
  490. return;
  491. }
  492. App::uses('Debugger', 'Utility');
  493. $trace = Debugger::trace(array('start' => 1, 'format' => 'log', 'args'=>true));
  494. $path = Debugger::trimPath($error['file']);
  495. $message = $error['type_name'].' '.$matching[$error['type']].' in '.$path. ' (line '.$error['line'].'): ' . $error['message'];
  496. $message .= PHP_EOL . $trace;
  497. App::uses('MyErrorHandler', 'Tools.Error');
  498. $message .= MyErrorHandler::traceDetails();
  499. CakeLog::write($type, $message);
  500. }
  501. /*** < PHP5.3 ***/
  502. /**
  503. * until PHP5.3 is the PHP version in use
  504. * //BUGGY
  505. * 2010-06-21 ms
  506. */
  507. if (!function_exists('lcfirst')) {
  508. function lcfirst($str) {
  509. return (string) (mb_strtolower(mb_substr($str, 0, 1)) . mb_substr($str, 1));
  510. }
  511. }
  512. class DebugTab {
  513. public static $content = array();
  514. public static $groups = array();
  515. }
  516. function debugTab($var = false, $display = false, $key = null) {
  517. if (is_string($display)) {
  518. $key = $display;
  519. $display = true;
  520. }
  521. if (Configure::read('debug') > 0) {
  522. $calledFrom = debug_backtrace();
  523. if (is_string($key)) {
  524. if (!isset(debugTab::$groups[$key])) {
  525. DebugTab::$groups[$key] = array();
  526. }
  527. DebugTab::$groups[$key][] = array(
  528. 'debug' => print_r($var, true),
  529. 'file' => substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1),
  530. 'line' => $calledFrom[0]['line'],
  531. 'display' => $display
  532. );
  533. } else {
  534. DebugTab::$content[] = array(
  535. 'debug' => print_r($var, true),
  536. 'file' => substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1),
  537. 'line' => $calledFrom[0]['line'],
  538. 'display' => $display
  539. );
  540. }
  541. }
  542. return true;
  543. }
  544. /**
  545. * pretty_json
  546. *
  547. * @link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
  548. * @param string $json - the original JSON string
  549. * @param string $ind - the string to indent with
  550. * @return string
  551. */
  552. function pretty_json($json, $ind = "\t") {
  553. // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
  554. $tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);
  555. $indent = 0;
  556. $result = "";
  557. $quotemarks_counter = 0;
  558. $next_token_use_prefix = true;
  559. foreach ($tokens as $token) {
  560. $quotemarks_counter = $quotemarks_counter + (count(explode('"', $token)) - 1);
  561. if ($token == "") {
  562. continue;
  563. }
  564. if ($next_token_use_prefix) {
  565. $prefix = str_repeat($ind, $indent);
  566. } else {
  567. $prefix = null;
  568. }
  569. // Determine if the quote marks are open or closed
  570. if ($quotemarks_counter & 1) {
  571. // odd - thus quotemarks open
  572. $next_token_use_prefix = false;
  573. $new_line = null;
  574. } else {
  575. // even - thus quotemarks closed
  576. $next_token_use_prefix = true;
  577. $new_line = "\n";
  578. }
  579. if ($token == "{" || $token == "[") {
  580. $indent++;
  581. $result .= $token . $new_line;
  582. } elseif ($token == "}" || $token == "]") {
  583. $indent--;
  584. if ($indent >= 0) {
  585. $prefix = str_repeat($ind, $indent);
  586. }
  587. if ($next_token_use_prefix) {
  588. $result .= $new_line . $prefix . $token;
  589. } else {
  590. $result .= $new_line . $token;
  591. }
  592. } elseif ($token == ",") {
  593. $result .= $token . $new_line;
  594. } else {
  595. $result .= $prefix . $token;
  596. }
  597. }
  598. $result = str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
  599. return $result;
  600. }
  601. /*** > PHP5.3 ***/
  602. /**
  603. * replacement since it is deprecated in PHP5.3.3 (needs testing!!!)
  604. *
  605. * TODO: Write cool MimeLib to do this fucking Mime stuff in a better way
  606. * and also build a mime type WITH the charset of the file/strig like:
  607. * text/plain; charset=utf-8
  608. * @deprecated This function has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way
  609. **/
  610. if (!function_exists('mime_content_type')) {
  611. function mime_content_type($file, $method = 0) {
  612. if (WINDOWS) {
  613. return false;
  614. }
  615. if ($method == 0) {
  616. ob_start();
  617. system('/usr/bin/file -i -b ' . realpath($file));
  618. $type = ob_get_clean();
  619. $parts = explode(';', $type);
  620. return trim($parts[0]);
  621. } elseif ($method == 1) {
  622. // another method here
  623. }
  624. }
  625. }