MyBootstrap.php 20 KB

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