MyBootstrap.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 trim(htmlspecialchars_decode($text, $quoteStyle));
  296. }
  297. /**
  298. * Convenience method for html_entity_decode
  299. *
  300. * @param string $text Text to wrap through htmlspecialchars_decode
  301. * @return string Wrapped text
  302. * 2011-04-03 ms
  303. */
  304. function entDec($text, $quoteStyle = ENT_QUOTES) {
  305. if (is_array($text)) {
  306. return array_map('entDec', $text);
  307. }
  308. return (!empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '');
  309. }
  310. /**
  311. * focus is on the filename (without path)
  312. * 2011-06-02 ms
  313. */
  314. function extractFileInfo($type = null, $filename) {
  315. if ($info = extractPathInfo($type, $filename)) {
  316. return $info;
  317. }
  318. $pos = strrpos($filename, '.');
  319. $res = '';
  320. switch ($type) {
  321. case 'extension':
  322. case 'ext':
  323. $res = ($pos !== false) ? substr($filename, $pos+1) : '';
  324. break;
  325. case 'filename':
  326. case 'file':
  327. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  328. break;
  329. default:
  330. break;
  331. }
  332. return $res;
  333. }
  334. /**
  335. * uses native PHP function to retrieve infos about a filename etc.
  336. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  337. * @param string filename to check on
  338. * //TODO: switch parameters!!!
  339. * 2009-01-22 ms
  340. */
  341. function extractPathInfo($type = null, $filename) {
  342. switch ($type) {
  343. case 'extension':
  344. case 'ext':
  345. $infoType = PATHINFO_EXTENSION;
  346. break;
  347. case 'filename':
  348. case 'file':
  349. $infoType = PATHINFO_FILENAME;
  350. break;
  351. case 'basename':
  352. case 'base':
  353. $infoType = PATHINFO_BASENAME;
  354. break;
  355. case 'dirname':
  356. case 'dir':
  357. $infoType = PATHINFO_DIRNAME;
  358. break;
  359. default:
  360. $infoType = null;
  361. }
  362. return pathinfo($filename, $infoType);
  363. }
  364. /**
  365. * Shows pr() messages, even with debug=0
  366. *
  367. * @param mixed $content
  368. * @param bool $collapsedAndExpandable
  369. * @param array $options
  370. * - class, showHtml, showFrom, jquery, returns, debug
  371. * 2011-01-19 ms
  372. */
  373. function pre($var, $collapsedAndExpandable = false, $options = array()) {
  374. $defaults = array(
  375. 'class' => 'cake-debug',
  376. 'showHtml' => false, # escape < and > (or manually escape with h() prior to calling this function)
  377. 'showFrom' => false, # display file + line
  378. 'jquery' => null, # auto - use jQuery (true/false to manually decide),
  379. 'returns' => false, # returns(),
  380. 'debug' => false # showOnlyOnDebug
  381. );
  382. $options = array_merge($defaults, $options);
  383. if ($options['debug'] && !Configure::read('debug')) {
  384. return '';
  385. }
  386. $res = '<div class="'.$options['class'].'">';
  387. $pre = '';
  388. if ($collapsedAndExpandable) {
  389. $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\'}';
  390. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  391. if ($options['jquery'] === true) {
  392. $js = $jsJquery;
  393. } elseif ($options['jquery'] !== false) {
  394. # auto
  395. $js = 'if (typeof jQuery == \'undefined\') {'.$js.'} else {'.$jsJquery.'}';
  396. }
  397. $res .= '<span onclick="'.$js.'" style="cursor: pointer; font-weight: bold">Debug</span>';
  398. if ($options['showFrom']) {
  399. $calledFrom = debug_backtrace();
  400. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  401. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  402. $res .= '<div>'.$from.'</div>';
  403. }
  404. $pre = ' style="display: none"';
  405. }
  406. if ($options['returns']) {
  407. $var = returns($var);
  408. } else {
  409. $var = print_r($var, true);
  410. }
  411. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  412. $res .= '</div>';
  413. return $res;
  414. }
  415. /**
  416. * Checks if the string [$haystack] contains [$needle]
  417. * @param string $haystack Input string.
  418. * @param string $needle Needed char or string.
  419. * @return boolean
  420. */
  421. function contains($haystack, $needle, $caseSensitive = false) {
  422. return (!$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle))
  423. !== false;
  424. }
  425. /**
  426. * Can compare two float values
  427. * @deprecated use NumberLib::isFloatEqual
  428. * @link http://php.net/manual/en/language.types.float.php
  429. * @return boolean
  430. */
  431. function isFloatEqual($x, $y, $precision = 0.0000001) {
  432. trigger_error('deprecated - use NumberLib::isFloatEqual instead');
  433. return ($x+$precision >= $y) && ($x-$precision <= $y);
  434. }
  435. /**
  436. * Checks if the string [$haystack] starts with [$needle]
  437. * @param string $haystack Input string.
  438. * @param string $needle Needed char or string.
  439. * @return boolean
  440. */
  441. function startsWith($haystack, $needle, $caseSensitive = false) {
  442. if ($caseSensitive) {
  443. return (mb_strpos($haystack, $needle) === 0);
  444. }
  445. return (mb_stripos($haystack, $needle) === 0);
  446. }
  447. /**
  448. * Checks if the String [$haystack] ends with [$needle]
  449. * @param string $haystack Input string.
  450. * @param string $needle Needed char or string
  451. * @return boolean
  452. */
  453. function endsWith($haystack, $needle, $caseSensitive = false) {
  454. if ($caseSensitive) {
  455. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  456. }
  457. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  458. }
  459. /* deprecated? */
  460. function isLoggedIn() {
  461. return isset($_SESSION) && !empty($_SESSION['Auth']['User']['id']);
  462. }
  463. /* deprecated? */
  464. function uid($default = null) {
  465. return (isset($_SESSION) && !empty($_SESSION['Auth']['User']['id'])) ? $_SESSION['Auth']['User']['id'] :
  466. $default;
  467. }
  468. /**
  469. * own shutdown function - also logs fatal errors (necessary until cake2.2)
  470. * 2010-10-17 ms
  471. */
  472. function shutDownFunction() {
  473. $error = error_get_last();
  474. if (empty($error)) {
  475. return;
  476. }
  477. $matching = array(
  478. E_ERROR =>'E_ERROR',
  479. E_WARNING => 'E_WARNING',
  480. E_PARSE => 'E_',
  481. E_NOTICE => 'E_',
  482. E_CORE_ERROR => 'E_',
  483. E_COMPILE_ERROR => 'E_',
  484. E_COMPILE_WARNING => 'E_',
  485. E_STRICT => 'E_STRICT',
  486. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  487. E_DEPRECATED => 'E_DEPRECATED',
  488. );
  489. App::uses('CakeLog', 'Log');
  490. if (in_array($error['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR))) {
  491. $error['type_name'] = 'Fatal Error';
  492. $type = 'error';
  493. } elseif (Configure::read('Debug.log') && isset($matching[$error['type']])) {
  494. $error['type_name'] = 'Error';
  495. $type = 'notice';
  496. }
  497. if (!isset($type)) {
  498. return;
  499. }
  500. App::uses('Debugger', 'Utility');
  501. $trace = Debugger::trace(array('start' => 1, 'format' => 'log', 'args'=>true));
  502. $path = Debugger::trimPath($error['file']);
  503. $message = $error['type_name'].' '.$matching[$error['type']].' in '.$path. ' (line '.$error['line'].'): ' . $error['message'];
  504. $message .= PHP_EOL . $trace;
  505. App::uses('MyErrorHandler', 'Tools.Error');
  506. $message .= MyErrorHandler::traceDetails();
  507. CakeLog::write($type, $message);
  508. }
  509. /*** < PHP5.3 ***/
  510. /**
  511. * until PHP5.3 is the PHP version in use
  512. * //BUGGY
  513. * 2010-06-21 ms
  514. */
  515. if (!function_exists('lcfirst')) {
  516. function lcfirst($str) {
  517. return (string) (mb_strtolower(mb_substr($str, 0, 1)) . mb_substr($str, 1));
  518. }
  519. }
  520. class DebugTab {
  521. public static $content = array();
  522. public static $groups = array();
  523. }
  524. function debugTab($var = false, $display = false, $key = null) {
  525. if (is_string($display)) {
  526. $key = $display;
  527. $display = true;
  528. }
  529. if (Configure::read('debug') > 0) {
  530. $calledFrom = debug_backtrace();
  531. if (is_string($key)) {
  532. if (!isset(debugTab::$groups[$key])) {
  533. DebugTab::$groups[$key] = array();
  534. }
  535. DebugTab::$groups[$key][] = array(
  536. 'debug' => print_r($var, true),
  537. 'file' => substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1),
  538. 'line' => $calledFrom[0]['line'],
  539. 'display' => $display
  540. );
  541. } else {
  542. DebugTab::$content[] = array(
  543. 'debug' => print_r($var, true),
  544. 'file' => substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1),
  545. 'line' => $calledFrom[0]['line'],
  546. 'display' => $display
  547. );
  548. }
  549. }
  550. return true;
  551. }
  552. /**
  553. * base64 encode and replace chars base64 uses that would mess up the url
  554. * @return string or NULL
  555. */
  556. function base64UrlEncode($fieldContent) {
  557. if (empty($fieldContent)) {
  558. return null;
  559. }
  560. $tmp = base64_encode($fieldContent);
  561. return str_replace(array('/', '='), array('-', '_'), $tmp);
  562. }
  563. /**
  564. * base64 decode and undo replacing of chars base64 uses that would mess up the url
  565. * @return string or NULL
  566. */
  567. function base64UrlDecode($fieldContent) {
  568. if (empty($fieldContent)) {
  569. return null;
  570. }
  571. $tmp = str_replace(array('-', '_'), array('/', '='), $fieldContent);
  572. return base64_decode($tmp);
  573. }
  574. /**
  575. * pretty_json
  576. *
  577. * @link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
  578. * @param string $json - the original JSON string
  579. * @param string $ind - the string to indent with
  580. * @return string
  581. */
  582. function pretty_json($json, $ind = "\t") {
  583. // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
  584. $tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);
  585. $indent = 0;
  586. $result = "";
  587. $quotemarks_counter = 0;
  588. $next_token_use_prefix = true;
  589. foreach ($tokens as $token) {
  590. $quotemarks_counter = $quotemarks_counter + (count(explode('"', $token)) - 1);
  591. if ($token == "") {
  592. continue;
  593. }
  594. if ($next_token_use_prefix) {
  595. $prefix = str_repeat($ind, $indent);
  596. } else {
  597. $prefix = null;
  598. }
  599. // Determine if the quote marks are open or closed
  600. if ($quotemarks_counter & 1) {
  601. // odd - thus quotemarks open
  602. $next_token_use_prefix = false;
  603. $new_line = null;
  604. } else {
  605. // even - thus quotemarks closed
  606. $next_token_use_prefix = true;
  607. $new_line = "\n";
  608. }
  609. if ($token == "{" || $token == "[") {
  610. $indent++;
  611. $result .= $token . $new_line;
  612. } elseif ($token == "}" || $token == "]") {
  613. $indent--;
  614. if ($indent >= 0) {
  615. $prefix = str_repeat($ind, $indent);
  616. }
  617. if ($next_token_use_prefix) {
  618. $result .= $new_line . $prefix . $token;
  619. } else {
  620. $result .= $new_line . $token;
  621. }
  622. } elseif ($token == ",") {
  623. $result .= $token . $new_line;
  624. } else {
  625. $result .= $prefix . $token;
  626. }
  627. }
  628. $result = str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
  629. return $result;
  630. }
  631. /*** > PHP5.3 ***/
  632. /**
  633. * replacement since it is deprecated in PHP5.3.3 (needs testing!!!)
  634. *
  635. * TODO: Write cool MimeLib to do this fucking Mime stuff in a better way
  636. * and also build a mime type WITH the charset of the file/strig like:
  637. * text/plain; charset=utf-8
  638. * @deprecated This function has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way
  639. **/
  640. if (!function_exists('mime_content_type')) {
  641. function mime_content_type($file, $method = 0) {
  642. if (WINDOWS) {
  643. return false;
  644. }
  645. if ($method == 0) {
  646. ob_start();
  647. system('/usr/bin/file -i -b ' . realpath($file));
  648. $type = ob_get_clean();
  649. $parts = explode(';', $type);
  650. return trim($parts[0]);
  651. } elseif ($method == 1) {
  652. // another method here
  653. }
  654. }
  655. }