MyBootstrap.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. /**
  3. * Basic bootstrap stuff
  4. *
  5. * Note: Do not use App::uses() to include this file.
  6. * Use App::import('Lib', 'Tools.Bootstrap/MyBootstrap'); as noted in the readme.
  7. */
  8. App::uses('Utility', 'Tools.Utility');
  9. # You can also use FULL_BASE_URL (cake) instead of HTTP_BASE
  10. if (!empty($_SERVER['HTTP_HOST'])) {
  11. define('HTTP_HOST', $_SERVER['HTTP_HOST']);
  12. define('HTTP_BASE', 'http://' . HTTP_HOST); //FULL_BASE_URL
  13. } else {
  14. define('HTTP_HOST', '');
  15. define('HTTP_BASE', '');
  16. }
  17. if (!empty($_SERVER['PHP_SELF'])) {
  18. define('HTTP_SELF', '' . $_SERVER['PHP_SELF']);
  19. }
  20. if (!empty($_SERVER['REQUEST_URI'])) {
  21. define('HTTP_URI', '' . $_SERVER['REQUEST_URI']);
  22. }
  23. if (!empty($_SERVER['HTTP_REFERER'])) {
  24. define('HTTP_REF', '' . $_SERVER['HTTP_REFERER']);
  25. }
  26. define('CHOWN_PUBLIC', 0770);
  27. # Useful when putting a string together that needs some "pretty" html-doc. source layouting
  28. # Only visible in SOURCE code (not in html layout in the browser)
  29. //define('LF',''); // line feed (depending on the system)
  30. define('LF', PHP_EOL); // line feed (depending on the system): \n or \n\r etc.
  31. # Alternativly NL,CR:
  32. define('NL', "\n"); // new line
  33. define('CR', "\r"); // carriage return
  34. define('TB', "\t"); // tabulator
  35. # Useful for html layouting
  36. # Visible in the Html Layout in the Browser
  37. define('BR', '<br />'); // line break
  38. # Make the app and l10n play nice with Windows.
  39. if (substr(PHP_OS, 0, 3) === 'WIN') { // || strpos(@php_uname(), 'ARCH')
  40. define('WINDOWS', true);
  41. } else {
  42. define('WINDOWS', false);
  43. }
  44. define('FORMAT_DB_DATETIME', 'Y-m-d H:i:s'); // date(...)
  45. define('FORMAT_DB_DATE', 'Y-m-d');
  46. define('FORMAT_DB_TIME', 'H:i:s');
  47. define('DEFAULT_DATETIME', '0000-00-00 00:00:00');
  48. define('DEFAULT_DATE', '0000-00-00');
  49. define('DEFAULT_TIME', '00:00:00');
  50. # workpaths
  51. define('FILES', APP . 'files' . DS);
  52. define('LOCALE', APP . 'locale' . DS);
  53. if (!defined('CLASS_USER')) {
  54. define('CLASS_USER', 'User');
  55. }
  56. # Validation ## (minus should be "hyphen")
  57. /** Valid characters: letters only */
  58. define('VALID_ALPHA', '/^[a-zA-Z]+$/');
  59. /** Valid characters: letters,underscores only */
  60. define('VALID_ALPHA_UNDERSCORES', '/^[a-zA-Z_]+$/');
  61. /** Valid characters: letters,underscores,minus only */
  62. define('VALID_ALPHA_MINUS_UNDERSCORES', '/^[a-zA-Z_-]+$/');
  63. /** Valid characters: letters,spaces only */
  64. define('VALID_ALPHA_WHITESPACES', '/^[a-zA-Z ]+$/');
  65. /** Valid characters: letters,numbers,underscores only */
  66. define('VALID_ALPHANUMERIC_UNDERSCORES', '/^[\da-zA-Z_]+$/');
  67. /** Valid characters: letters,numbers,underscores,minus only */
  68. define('VALID_ALPHANUMERIC_MINUS_UNDERSCORES', '/^[\da-zA-Z_-]+$/');
  69. /** Valid characters: letters,numbers,spaces only */
  70. define('VALID_ALPHANUMERIC_WHITESPACES', '/^[\da-zA-Z ]+$/');
  71. /** Valid characters: letters,numbers,spaces,underscores only */
  72. define('VALID_ALPHANUMERIC_WHITESPACES_UNDERSCORES', '/^[\da-zA-Z _]+$/');
  73. /** Valid characters: numbers,underscores only */
  74. define('VALID_NUMERIC_UNDERSCORES', '/^[\d_]+$/');
  75. /** Valid characters: numbers,spaces only */
  76. define('VALID_NUMERIC_WHITESPACES', '/^[\d ]+$/');
  77. /** Valid characters: numbers,spaces,underscores only */
  78. define('VALID_NUMERIC_WHITESPACES_UNDERSCORES', '/^[\d _]+$/');
  79. /** Valid integers: > 0 */
  80. define('VALID_INTEGERS', '/^[\d]+$/'); //??
  81. if (!defined('FORMAT_NICE_YMDHMS')) {
  82. define('FORMAT_NICE_YMDHMS','d.m.Y, H:i:s');
  83. define('FORMAT_NICE_YMDHM','d.m.Y, H:i');
  84. define('FORMAT_NICE_YM','m.Y');
  85. define('FORMAT_NICE_YMD','d.m.Y');
  86. define('FORMAT_NICE_MD','d.m.');
  87. define('FORMAT_NICE_D','d'); # xx
  88. define('FORMAT_NICE_W_NUM','w'); # xx (0=sunday to 6=saturday)
  89. define('FORMAT_NICE_W_ABBR','D'); # needs manual translation
  90. define('FORMAT_NICE_W_FULL','l'); # needs manual translation
  91. define('FORMAT_NICE_M','m'); # xx
  92. define('FORMAT_NICE_M_ABBR','M'); # needs manual translation
  93. define('FORMAT_NICE_M_FULL','F'); # needs manual translation
  94. define('FORMAT_NICE_Y_ABBR','y'); # xx
  95. define('FORMAT_NICE_Y','Y'); # xxxx
  96. define('FORMAT_NICE_HM','H:i');
  97. define('FORMAT_NICE_HMS','H:i:s');
  98. # localDate strings
  99. define('FORMAT_LOCAL_WA_YMDHMS','%a, %d.%m.%Y, %H:%M:%S');
  100. define('FORMAT_LOCAL_WF_YMDHMS','%A, %d.%m.%Y, %H:%M:%S');
  101. define('FORMAT_LOCAL_WA_YMDHM','%a, %d.%m.%Y, %H:%M');
  102. define('FORMAT_LOCAL_WF_YMDHM','%A, %d.%m.%Y, %H:%M');
  103. define('FORMAT_LOCAL_YMDHMS','%d.%m.%Y, %H:%M:%S');
  104. define('FORMAT_LOCAL_YMDHM','%d.%m.%Y, %H:%M');
  105. define('FORMAT_LOCAL_YMD','%d.%m.%Y');
  106. define('FORMAT_LOCAL_MD','%d.%m.');
  107. define('FORMAT_LOCAL_D','%d'); # xx
  108. define('FORMAT_LOCAL_W_NUM','%w'); # xx (0=sunday to 6=saturday)
  109. define('FORMAT_LOCAL_W_ABBR','%a'); # needs translation
  110. define('FORMAT_LOCAL_W_FULL','%A'); # needs translation
  111. define('FORMAT_LOCAL_M','%m'); # xx
  112. define('FORMAT_LOCAL_M_ABBR','%b'); # needs translation
  113. define('FORMAT_LOCAL_M_FULL','%B'); # needs translation
  114. define('FORMAT_LOCAL_Y_ABBR','%y'); # xx
  115. define('FORMAT_LOCAL_Y','%Y'); # xxxx
  116. define('FORMAT_LOCAL_H','%H');
  117. define('FORMAT_LOCAL_S','%S');
  118. define('FORMAT_LOCAL_HM','%H:%i');
  119. define('FORMAT_LOCAL_HMS','%H:%M:%S');
  120. }
  121. /*** chars ***/
  122. /* see http://www.htmlcodetutorial.com/characterentities_famsupp_69.html */
  123. define('CHAR_LESS', '&lt;'); # <
  124. define('CHAR_GREATER', '&gt;'); # >
  125. define('CHAR_QUOTE', '&quot;'); # "
  126. define('CHAR_APOSTROPHE', '&#39'); # '
  127. define('CHAR_ARROWS', '&raquo;'); # »
  128. define('CHAR_ARROWS_R', '&#187;'); # »
  129. define('CHAR_ARROWS_L', '&#171;'); # «
  130. define('CHAR_AVERAGE', '&#216;'); # Ø
  131. define('CHAR_INFIN', '&infin;'); # 8
  132. define('CHAR_MILL', '&#137;'); # ‰ (per mille) / or &permil;
  133. define('CHAR_PLUSMN', '&plusmn;'); # 8
  134. define('CHAR_HELLIP', '&#8230;'); # … (horizontal ellipsis = three dot leader)
  135. define('CHAR_CIRCA', '&asymp;'); # ˜ (almost equal to)
  136. define('CHAR_CHECKBOX_EMPTY', '&#9744;]'); #
  137. define('CHAR_CHECKBOX_MAKRED', '&#9745'); #
  138. define('CHAR_CHECKMARK', '&#10003;');
  139. define('CHAR_CHECKMARK_BOLD', '&#10004;');
  140. define('CHAR_BALLOT', '&#10007;');
  141. define('CHAR_BALLOT_BOLD', '&#10008;');
  142. define('CHAR_ABOUT','&asymp;'); # … (horizontal ellipsis = three dot leader)
  143. /* not very often used */
  144. define('CHAR_RPIME', '&#8242;'); # ' (minutes)
  145. define('CHAR_DOUBLE_RPIME', '&#8243;'); # ? (seconds)
  146. /** BASIC FUNCTIONS **/
  147. /**
  148. * Own slug function - containing extra char replacement
  149. *
  150. * //TODO move to TextLib?
  151. *
  152. * @return string
  153. * 2010-11-07 ms
  154. */
  155. function slug($string, $separator = null, $low = true) {
  156. $additionalSlugElements = array(
  157. '/º|°/' => 0,
  158. '/¹/' => 1,
  159. '/²/' => 2,
  160. '/³/' => 3,
  161. # new utf8 char "capitel ß" still missing here! '/.../' => 'SS', (TODO in 2009)
  162. '/@/' => 'at',
  163. '/æ/' => 'ae',
  164. '/©/' => 'C',
  165. '/ç|¢/' => 'c',
  166. '/Ð/' => 'D',
  167. '/€/' => 'EUR',
  168. '/™/' => 'TM',
  169. # more missing?
  170. );
  171. if ($separator === null) {
  172. $separator = defined('SEO_SEPARATOR') ? SEO_SEPARATOR : '-';
  173. }
  174. $res = Inflector::slug($string, $separator, $additionalSlugElements);
  175. if ($low) {
  176. $res = strtolower($res);
  177. }
  178. return $res;
  179. }
  180. /**
  181. * Since nl2br doesn't remove the line breaks when adding in the <br /> tags,
  182. * it is necessary to strip those off before you convert all of the tags, otherwise you will get double spacing
  183. *
  184. * //TODO: move to TextLib?
  185. *
  186. * @param string $str
  187. * @return string
  188. * 2010-11-07 ms
  189. */
  190. function br2nl($str) {
  191. $str = preg_replace("/(\r\n|\r|\n)/", "", $str);
  192. return preg_replace("=<br */?>=i", "\n", $str);
  193. }
  194. /**
  195. * Replaces CRLF with spaces
  196. *
  197. * //TODO: move to TextLib?
  198. *
  199. * @param string $text Any text
  200. * @return string Safe string without new lines
  201. * 2010-11-14 ms
  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 boolean Result
  215. * 2009-06-15 ms
  216. */
  217. function isEmpty($var = null) {
  218. if (empty($var)) {
  219. return true;
  220. }
  221. return false;
  222. }
  223. /**
  224. * Return of what type the specific value is
  225. *
  226. * //TODO: use Debugger::exportVar() instead?
  227. *
  228. * @param mixed $value
  229. * @return type (NULL, array, bool, float, int, string, object, unknown) + value
  230. * 2009-03-03 ms
  231. */
  232. function returns($value) {
  233. if ($value === null) {
  234. return 'NULL';
  235. } elseif (is_array($value)) {
  236. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  237. } elseif ($value === true) {
  238. return '(bool)TRUE';
  239. } elseif ($value === false) {
  240. return '(bool)FALSE';
  241. } elseif (is_numeric($value) && is_float($value)) {
  242. return '(float)' . $value;
  243. } elseif (is_numeric($value) && is_int($value)) {
  244. return '(int)' . $value;
  245. } elseif (is_string($value)) {
  246. return '(string)' . $value;
  247. } elseif (is_object($value)) {
  248. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  249. '</pre>';
  250. } else {
  251. return '(unknown)' . $value;
  252. }
  253. }
  254. /**
  255. * Quickly dump a $var
  256. *
  257. * @param mixed $var
  258. * @return void
  259. */
  260. function dump($var) {
  261. if (class_exists('Debugger')) {
  262. App::uses('Debugger', 'Utility');
  263. }
  264. return Debugger::dump($var);
  265. }
  266. /**
  267. * Returns htmlentities - string
  268. *
  269. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  270. * ENT_QUOTES = Will convert both double and single quotes. !!!
  271. * ENT_NOQUOTES= Will leave both double and single quotes unconverted.
  272. */
  273. function ent($text) {
  274. return (!empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '');
  275. }
  276. /**
  277. * Convenience method for htmlspecialchars_decode
  278. *
  279. * @param string $text Text to wrap through htmlspecialchars_decode
  280. * @return string Wrapped text
  281. * 2011-04-03 ms
  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 Wrapped text
  294. * 2011-04-03 ms
  295. */
  296. function entDec($text, $quoteStyle = ENT_QUOTES) {
  297. if (is_array($text)) {
  298. return array_map('entDec', $text);
  299. }
  300. return (!empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '');
  301. }
  302. /**
  303. * Focus is on the filename (without path)
  304. *
  305. * //TODO: switch parameters!!!
  306. *
  307. * @return mixed
  308. * 2011-06-02 ms
  309. */
  310. function extractFileInfo($type = null, $filename) {
  311. if ($info = extractPathInfo($type, $filename)) {
  312. return $info;
  313. }
  314. $pos = strrpos($filename, '.');
  315. $res = '';
  316. switch ($type) {
  317. case 'extension':
  318. case 'ext':
  319. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  320. break;
  321. case 'filename':
  322. case 'file':
  323. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  324. break;
  325. default:
  326. break;
  327. }
  328. return $res;
  329. }
  330. /**
  331. * Uses native PHP function to retrieve infos about a filename etc.
  332. * Improves it by not returning non-file-name characters from url files if specified.
  333. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  334. *
  335. * //TODO: switch parameters!!!
  336. *
  337. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  338. * @param string filename to check on
  339. * @return mixed
  340. * 2009-01-22 ms
  341. */
  342. function extractPathInfo($type = null, $filename, $fromUrl = false) {
  343. switch ($type) {
  344. case 'extension':
  345. case 'ext':
  346. $infoType = PATHINFO_EXTENSION;
  347. break;
  348. case 'filename':
  349. case 'file':
  350. $infoType = PATHINFO_FILENAME;
  351. break;
  352. case 'basename':
  353. case 'base':
  354. $infoType = PATHINFO_BASENAME;
  355. break;
  356. case 'dirname':
  357. case 'dir':
  358. $infoType = PATHINFO_DIRNAME;
  359. break;
  360. default:
  361. $infoType = null;
  362. }
  363. $result = pathinfo($filename, $infoType);
  364. if ($fromUrl) {
  365. if (($pos = strpos($result, '#')) !== false) {
  366. $result = substr($result, 0, $pos);
  367. }
  368. if (($pos = strpos($result, '?')) !== false) {
  369. $result = substr($result, 0, $pos);
  370. }
  371. }
  372. return $result;
  373. }
  374. /**
  375. * Shows pr() messages, even with debug=0
  376. *
  377. * @param mixed $content
  378. * @param boolean $collapsedAndExpandable
  379. * @param array $options
  380. * - class, showHtml, showFrom, jquery, returns, debug
  381. * @return string HTML
  382. * 2011-01-19 ms
  383. */
  384. function pre($var, $collapsedAndExpandable = false, $options = array()) {
  385. $defaults = array(
  386. 'class' => 'cake-debug',
  387. 'showHtml' => false, # escape < and > (or manually escape with h() prior to calling this function)
  388. 'showFrom' => false, # display file + line
  389. 'jquery' => null, # auto - use jQuery (true/false to manually decide),
  390. 'returns' => false, # returns(),
  391. 'debug' => false # showOnlyOnDebug
  392. );
  393. $options = array_merge($defaults, $options);
  394. if ($options['debug'] && !Configure::read('debug')) {
  395. return '';
  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 boolean
  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 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. *
  453. * @param string $haystack Input string.
  454. * @param string $needle Needed char or string
  455. * @return boolean
  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. }
  463. /**
  464. * base64 encode and replace chars base64 uses that would mess up the url
  465. * only needed for named params to be safely passed (if urlencode is not used)
  466. *
  467. * @deprecated Use Utility::urlEncode().
  468. * @return string or NULL
  469. */
  470. function base64UrlEncode($fieldContent) {
  471. if (empty($fieldContent)) {
  472. return null;
  473. }
  474. $tmp = base64_encode($fieldContent);
  475. return str_replace(array('/', '='), array('-', '_'), $tmp);
  476. }
  477. /**
  478. * base64 decode and undo replacing of chars base64 uses that would mess up the url
  479. * only needed for named params to be safely passed (if urlencode is not used)
  480. *
  481. * @deprecated Use Utility::urlDecode().
  482. * @return string or NULL
  483. */
  484. function base64UrlDecode($fieldContent) {
  485. if (empty($fieldContent)) {
  486. return null;
  487. }
  488. $tmp = str_replace(array('-', '_'), array('/', '='), $fieldContent);
  489. return base64_decode($tmp);
  490. }
  491. /**
  492. * prettyJson
  493. *
  494. * @link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
  495. * @param string $json - the original JSON string
  496. * @param string $ind - the string to indent with
  497. * @return string
  498. */
  499. function prettyJson($json, $ind = "\t") {
  500. // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
  501. $tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);
  502. $indent = 0;
  503. $result = '';
  504. $quotemarks_counter = 0;
  505. $next_token_use_prefix = true;
  506. foreach ($tokens as $token) {
  507. $quotemarks_counter = $quotemarks_counter + (count(explode('"', $token)) - 1);
  508. if ($token === '') {
  509. continue;
  510. }
  511. if ($next_token_use_prefix) {
  512. $prefix = str_repeat($ind, $indent);
  513. } else {
  514. $prefix = null;
  515. }
  516. // Determine if the quote marks are open or closed
  517. if ($quotemarks_counter & 1) {
  518. // odd - thus quotemarks open
  519. $next_token_use_prefix = false;
  520. $new_line = null;
  521. } else {
  522. // even - thus quotemarks closed
  523. $next_token_use_prefix = true;
  524. $new_line = "\n";
  525. }
  526. if ($token === "{" || $token === "[") {
  527. $indent++;
  528. $result .= $token . $new_line;
  529. } elseif ($token === "}" || $token === "]") {
  530. $indent--;
  531. if ($indent >= 0) {
  532. $prefix = str_repeat($ind, $indent);
  533. }
  534. if ($next_token_use_prefix) {
  535. $result .= $new_line . $prefix . $token;
  536. } else {
  537. $result .= $new_line . $token;
  538. }
  539. } elseif ($token === ",") {
  540. $result .= $token . $new_line;
  541. } else {
  542. $result .= $prefix . $token;
  543. }
  544. }
  545. $result = str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
  546. return $result;
  547. }