Utility.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. namespace Tools\Utility;
  3. use Cake\Routing\Router;
  4. use Cake\Utility\Hash;
  5. /**
  6. * Main class for all app-wide utility methods
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  10. */
  11. class Utility {
  12. /**
  13. * Clean implementation of inArray to avoid false positives.
  14. *
  15. * in_array itself has some PHP flaws regarding cross-type comparison:
  16. * - in_array('50x', array(40, 50, 60)) would be true!
  17. * - in_array(50, array('40x', '50x', '60x')) would be true!
  18. *
  19. * @param mixed $needle
  20. * @param array $haystack
  21. * @return bool Success
  22. */
  23. public static function inArray($needle, $haystack) {
  24. $strict = !is_numeric($needle);
  25. return in_array((string)$needle, $haystack, $strict);
  26. }
  27. /**
  28. * Tokenizes a string using $separator.
  29. *
  30. * Options
  31. * - clean: true/false (defaults to true and removes empty tokens and whitespace)
  32. *
  33. * @param string $data The data to tokenize
  34. * @param string $separator The token to split the data on.
  35. * @param array $options
  36. * @return void
  37. */
  38. public static function tokenize($data, $separator = ',', $options = array()) {
  39. $defaults = array(
  40. 'clean' => true
  41. );
  42. $options += $defaults;
  43. if (empty($data)) {
  44. return array();
  45. }
  46. $tokens = explode($separator, $data);
  47. if (empty($tokens) || !$options['clean']) {
  48. return $tokens;
  49. }
  50. $tokens = array_map('trim', $tokens);
  51. foreach ($tokens as $key => $token) {
  52. if ($token === '') {
  53. unset($tokens[$key]);
  54. }
  55. }
  56. return $tokens;
  57. }
  58. /**
  59. * Multibyte analogue of preg_match_all() function. Only that this returns the result.
  60. * By default this works properly with UTF8 strings.
  61. *
  62. * Do not forget to use preg_quote() first on strings that could potentially contain
  63. * unescaped characters.
  64. *
  65. * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
  66. *
  67. * Example: /some(.*)pattern/u
  68. *
  69. * @param string $pattern The pattern to use.
  70. * @param string $subject The string to match.
  71. * @param int $flags
  72. * @param int $offset
  73. * @return array Result
  74. */
  75. public static function pregMatchAll($pattern, $subject, $flags = PREG_SET_ORDER, $offset = null) {
  76. $pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
  77. preg_match_all($pattern, $subject, $matches, $flags, $offset);
  78. return $matches;
  79. }
  80. /**
  81. * Multibyte analogue of preg_match() function. Only that this returns the result.
  82. * By default this works properly with UTF8 strings.
  83. *
  84. * Do not forget to use preg_quote() first on strings that could potentially contain
  85. * unescaped characters.
  86. *
  87. * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
  88. *
  89. * Example: /some(.*)pattern/u
  90. *
  91. * @param string $pattern The pattern to use.
  92. * @param string $subject The string to match.
  93. * @param int $flags
  94. * @param int $offset
  95. * @return array Result
  96. */
  97. public static function pregMatch($pattern, $subject, $flags = null, $offset = null) {
  98. $pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
  99. preg_match($pattern, $subject, $matches, $flags, $offset);
  100. return $matches;
  101. }
  102. /**
  103. * Multibyte analogue of str_split() function.
  104. * By default this works properly with UTF8 strings.
  105. *
  106. * @param string $text
  107. * @param int $length
  108. * @return array Result
  109. */
  110. public static function strSplit($str, $length = 1) {
  111. if ($length < 1) {
  112. return false;
  113. }
  114. $result = array();
  115. $c = mb_strlen($str);
  116. for ($i = 0; $i < $c; $i += $length) {
  117. $result[] = mb_substr($str, $i, $length);
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Get the current IP address.
  123. *
  124. * @param bool $safe
  125. * @return string IP address
  126. */
  127. public static function getClientIp($safe = true) {
  128. if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
  129. $ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
  130. } else {
  131. if (env('HTTP_CLIENT_IP')) {
  132. $ipaddr = env('HTTP_CLIENT_IP');
  133. } else {
  134. $ipaddr = env('REMOTE_ADDR');
  135. }
  136. }
  137. if (env('HTTP_CLIENTADDRESS')) {
  138. $tmpipaddr = env('HTTP_CLIENTADDRESS');
  139. if (!empty($tmpipaddr)) {
  140. $ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr);
  141. }
  142. }
  143. return trim($ipaddr);
  144. }
  145. /**
  146. * Get the current referrer if available.
  147. *
  148. * @param bool $full (defaults to false and leaves the url untouched)
  149. * @return string referer (local or foreign)
  150. */
  151. public static function getReferer($full = false) {
  152. $ref = env('HTTP_REFERER');
  153. $forwarded = env('HTTP_X_FORWARDED_HOST');
  154. if ($forwarded) {
  155. $ref = $forwarded;
  156. }
  157. if (empty($ref)) {
  158. return $ref;
  159. }
  160. if ($full) {
  161. $ref = Router::url($ref, $full);
  162. }
  163. return $ref;
  164. }
  165. /**
  166. * Remove unnessary stuff + add http:// for external urls
  167. * TODO: protocol to lower!
  168. *
  169. * @param string $url
  170. * @return string Cleaned Url
  171. */
  172. public static function cleanUrl($url, $headerRedirect = false) {
  173. if ($url === '' || $url === 'http://' || $url === 'http://www' || $url === 'http://www.') {
  174. $url = '';
  175. } else {
  176. $url = static::autoPrefixUrl($url, 'http://');
  177. }
  178. if ($headerRedirect && !empty($url)) {
  179. $headers = static::getHeaderFromUrl($url);
  180. if ($headers !== false) {
  181. $headerString = implode("\n", $headers);
  182. if ((bool)preg_match('#^HTTP/.*\s+[(301)]+\s#i', $headerString)) {
  183. foreach ($headers as $header) {
  184. if (mb_strpos($header, 'Location:') === 0) {
  185. $url = trim(hDec(mb_substr($header, 9))); // rawurldecode/urldecode ?
  186. }
  187. }
  188. }
  189. }
  190. }
  191. $length = mb_strlen($url);
  192. while (!empty($url) && mb_strrpos($url, '/') === $length - 1) {
  193. $url = mb_substr($url, 0, $length - 1);
  194. $length--;
  195. }
  196. return $url;
  197. }
  198. /**
  199. * A more robust wrapper around for file_exists() which easily
  200. * fails to return true for existent remote files.
  201. * Per default it allows http/https images to be looked up via urlExists()
  202. * for a better result.
  203. *
  204. * @param string $file File
  205. * @return bool Success
  206. */
  207. public static function fileExists($file, $pattern = '~^https?://~i') {
  208. if (!preg_match($pattern, $file)) {
  209. return file_exists($file);
  210. }
  211. return static::urlExists($file);
  212. }
  213. /**
  214. * file_exists() does not always work with URLs.
  215. * So if you check on strpos(http) === 0 you can use this
  216. * to check for URLs instead.
  217. *
  218. * @param string $url Absolute URL
  219. * @return bool Success
  220. */
  221. public static function urlExists($url) {
  222. $headers = @get_headers($url);
  223. if ($headers && preg_match('|\b200\b|', $headers[0])) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. /**
  229. * Parse headers from a specific URL content.
  230. *
  231. * @param string $url
  232. * @return mixed array of headers or FALSE on failure
  233. */
  234. public static function getHeaderFromUrl($url) {
  235. $url = @parse_url($url);
  236. if (empty($url)) {
  237. return false;
  238. }
  239. $url = array_map('trim', $url);
  240. $url['port'] = (!isset($url['port'])) ? '' : (':' . (int)$url['port']);
  241. $path = (isset($url['path'])) ? $url['path'] : '';
  242. if (empty($path)) {
  243. $path = '/';
  244. }
  245. $path .= (isset($url['query'])) ? "?$url[query]" : '';
  246. $defaults = array(
  247. 'http' => array(
  248. 'header' => "Accept: text/html\r\n" .
  249. "Connection: Close\r\n" .
  250. "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64)\r\n",
  251. )
  252. );
  253. stream_context_get_default($defaults);
  254. if (isset($url['host']) && $url['host'] !== gethostbyname($url['host'])) {
  255. $url = "$url[scheme]://$url[host]$url[port]$path";
  256. $headers = get_headers($url);
  257. if (is_array($headers)) {
  258. return $headers;
  259. }
  260. }
  261. return false;
  262. }
  263. /**
  264. * Add protocol prefix if necessary (and possible)
  265. *
  266. * @param string $url
  267. */
  268. public static function autoPrefixUrl($url, $prefix = null) {
  269. if ($prefix === null) {
  270. $prefix = 'http://';
  271. }
  272. if (($pos = strpos($url, '.')) !== false) {
  273. if (strpos(substr($url, 0, $pos), '//') === false) {
  274. $url = $prefix . $url;
  275. }
  276. }
  277. return $url;
  278. }
  279. /**
  280. * Encode strings with base64_encode and also
  281. * replace chars base64 uses that would mess up the url.
  282. *
  283. * Do not use this for querystrings. Those will escape automatically.
  284. * This is only useful for named or passed params.
  285. *
  286. * @deprecated Use query strings instead
  287. * @param string $string Unsafe string
  288. * @return string Encoded string
  289. */
  290. public static function urlEncode($string) {
  291. return str_replace(array('/', '='), array('-', '_'), base64_encode($string));
  292. }
  293. /**
  294. * Decode strings with base64_encode and also
  295. * replace back chars base64 uses that would mess up the url.
  296. *
  297. * Do not use this for querystrings. Those will escape automatically.
  298. * This is only useful for named or passed params.
  299. *
  300. * @deprecated Use query strings instead
  301. * @param string $string Safe string
  302. * @return string Decoded string
  303. */
  304. public static function urlDecode($string) {
  305. return base64_decode(str_replace(array('-', '_'), array('/', '='), $string));
  306. }
  307. /**
  308. * Returns true only if all values are true.
  309. * //TODO: maybe move to bootstrap?
  310. *
  311. * @param array $array
  312. * @return bool Result
  313. */
  314. public static function logicalAnd($array) {
  315. if (empty($array)) {
  316. return false;
  317. }
  318. foreach ($array as $result) {
  319. if (!$result) {
  320. return false;
  321. }
  322. }
  323. return true;
  324. }
  325. /**
  326. * Returns true if at least one value is true.
  327. * //TODO: maybe move to bootstrap?
  328. *
  329. * @param array $array
  330. * @return bool Result
  331. *
  332. */
  333. public static function logicalOr($array) {
  334. foreach ($array as $result) {
  335. if ($result) {
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. /**
  342. * On non-transaction db connections it will return a deep array of bools instead of bool.
  343. * So we need to call this method inside the modified saveAll() method to return the expected single bool there, too.
  344. *
  345. * @param array
  346. * @return bool
  347. * @deprecated Not sure this is useful for CakePHP 3.0
  348. */
  349. public static function isValidSaveAll($array) {
  350. if (empty($array)) {
  351. return false;
  352. }
  353. $ret = true;
  354. foreach ($array as $key => $val) {
  355. if (is_array($val)) {
  356. $ret = $ret & Utility::logicalAnd($val);
  357. } else {
  358. $ret = $ret & $val;
  359. }
  360. }
  361. return (bool)$ret;
  362. }
  363. /**
  364. * Convenience function for automatic casting in form methods etc.
  365. * //TODO: maybe move to bootstrap?
  366. *
  367. * @param mixed $value
  368. * @param string $type
  369. * @return safe value for DB query, or NULL if type was not a valid one
  370. */
  371. public static function typeCast($value, $type) {
  372. switch ($type) {
  373. case 'int':
  374. $value = (int)$value;
  375. break;
  376. case 'float':
  377. $value = (float)$value;
  378. break;
  379. case 'double':
  380. $value = (double)$value;
  381. break;
  382. case 'array':
  383. $value = (array)$value;
  384. break;
  385. case 'bool':
  386. $value = (bool)$value;
  387. break;
  388. case 'string':
  389. $value = (string)$value;
  390. break;
  391. default:
  392. return null;
  393. }
  394. return $value;
  395. }
  396. /**
  397. * Trim recursivly
  398. *
  399. */
  400. public static function trimDeep($value) {
  401. $value = is_array($value) ? array_map('self::trimDeep', $value) : trim($value);
  402. return $value;
  403. }
  404. /**
  405. * H() recursivly
  406. *
  407. */
  408. public static function specialcharsDeep($value) {
  409. $value = is_array($value) ? array_map('self::specialcharsDeep', $value) : htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  410. return $value;
  411. }
  412. /**
  413. * Removes all except A-Z,a-z,0-9 and allowedChars (allowedChars array) recursivly
  414. *
  415. */
  416. public static function paranoidDeep($value) {
  417. $value = is_array($value) ? array_map('self::paranoidDeep', $value) : Sanatize::paranoid($value, $this->allowedChars);
  418. return $value;
  419. }
  420. /**
  421. * Transfers/removes all < > from text (remove TRUE/FALSE)
  422. *
  423. */
  424. public static function htmlDeep($value) {
  425. $value = is_array($value) ? array_map('self::htmlDeep', $value) : Sanatize::html($value, $this->removeChars);
  426. return $value;
  427. }
  428. /**
  429. * Main deep method
  430. *
  431. */
  432. public static function deep($function, $value) {
  433. $value = is_array($value) ? array_map('self::' . $function, $value) : $function($value);
  434. return $value;
  435. }
  436. /**
  437. * Counts the dimensions of an array. If $all is set to false (which is the default) it will
  438. * only consider the dimension of the first element in the array.
  439. *
  440. * @param array $array Array to count dimensions on
  441. * @param bool $all Set to true to count the dimension considering all elements in array
  442. * @param int $count Start the dimension count at this number
  443. * @return int The number of dimensions in $array
  444. */
  445. public static function countDim($array, $all = false, $count = 0) {
  446. if ($all) {
  447. $depth = array($count);
  448. if (is_array($array) && reset($array) !== false) {
  449. foreach ($array as $value) {
  450. $depth[] = static::countDim($value, true, $count + 1);
  451. }
  452. }
  453. $return = max($depth);
  454. } else {
  455. if (is_array(reset($array))) {
  456. $return = static::countDim(reset($array)) + 1;
  457. } else {
  458. $return = 1;
  459. }
  460. }
  461. return $return;
  462. }
  463. /**
  464. * Expands the values of an array of strings into a deep array.
  465. * Opposite of flattenList().
  466. *
  467. * It needs at least a single separator to be present in all values
  468. * as the key would otherwise be undefined. If data can contain such key-less
  469. * rows, use $undefinedKey to avoid an exception being thrown. But it will
  470. * effectivly collide with other values in that same key then.
  471. *
  472. * So `Some.Deep.Value` becomes `array('Some' => array('Deep' => array('Value')))`.
  473. *
  474. * @param array $data
  475. * @param string $separator
  476. * @param string $undefinedKey
  477. * @return array
  478. */
  479. public static function expandList(array $data, $separator = '.', $undefinedKey = null) {
  480. $result = array();
  481. foreach ($data as $value) {
  482. $keys = explode($separator, $value);
  483. $value = array_pop($keys);
  484. $keys = array_reverse($keys);
  485. if (!isset($keys[0])) {
  486. if ($undefinedKey === null) {
  487. throw new \RuntimeException('Key-less values are not supported without $undefinedKey.');
  488. }
  489. $keys[0] = $undefinedKey;
  490. }
  491. $child = array($keys[0] => array($value));
  492. array_shift($keys);
  493. foreach ($keys as $k) {
  494. $child = array(
  495. $k => $child
  496. );
  497. }
  498. $result = Hash::merge($result, $child);
  499. }
  500. return $result;
  501. }
  502. /**
  503. * Flattens a deep array into an array of strings.
  504. * Opposite of expandList().
  505. *
  506. * So `array('Some' => array('Deep' => array('Value')))` becomes `Some.Deep.Value`.
  507. *
  508. * Note that primarily only string should be used.
  509. * However, boolean values are casted to int and thus
  510. * both boolean and integer values also supported.
  511. *
  512. * @param array $data
  513. * @return array
  514. */
  515. public static function flattenList(array $data, $separator = '.') {
  516. $result = array();
  517. $stack = array();
  518. $path = null;
  519. reset($data);
  520. while (!empty($data)) {
  521. $key = key($data);
  522. $element = $data[$key];
  523. unset($data[$key]);
  524. if (is_array($element) && !empty($element)) {
  525. if (!empty($data)) {
  526. $stack[] = array($data, $path);
  527. }
  528. $data = $element;
  529. reset($data);
  530. $path .= $key . $separator;
  531. } else {
  532. if (is_bool($element)) {
  533. $element = (int)$element;
  534. }
  535. $result[] = $path . $element;
  536. }
  537. if (empty($data) && !empty($stack)) {
  538. list($data, $path) = array_pop($stack);
  539. reset($data);
  540. }
  541. }
  542. return $result;
  543. }
  544. /**
  545. * Force-flattens an array.
  546. *
  547. * Careful with this method. It can lose information.
  548. * The keys will not be changed, thus possibly overwrite each other.
  549. *
  550. * //TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
  551. *
  552. * @param array $array to flatten
  553. * @param bool $perserveKeys
  554. * @return array
  555. */
  556. public static function arrayFlatten($array, $preserveKeys = false) {
  557. if ($preserveKeys) {
  558. return static::_arrayFlatten($array);
  559. }
  560. if (!$array) {
  561. return array();
  562. }
  563. $result = array();
  564. foreach ($array as $key => $value) {
  565. if (is_array($value)) {
  566. $result = array_merge($result, static::arrayFlatten($value));
  567. } else {
  568. $result[$key] = $value;
  569. }
  570. }
  571. return $result;
  572. }
  573. /**
  574. * Force-flattens an array and preserves the keys.
  575. *
  576. * Careful with this method. It can lose information.
  577. * The keys will not be changed, thus possibly overwrite each other.
  578. *
  579. * //TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
  580. *
  581. * @param array $a
  582. * @param array $f
  583. * @return array
  584. */
  585. protected static function _arrayFlatten($a, $f = array()) {
  586. if (!$a) {
  587. return array();
  588. }
  589. foreach ($a as $k => $v) {
  590. if (is_array($v)) {
  591. $f = static::_arrayFlatten($v, $f);
  592. } else {
  593. $f[$k] = $v;
  594. }
  595. }
  596. return $f;
  597. }
  598. /**
  599. * Similar to array_shift but on the keys of the array
  600. * like array_shift() only for keys and not values
  601. *
  602. * @param array $keyValuePairs
  603. * @return string key
  604. */
  605. public static function arrayShiftKeys(&$array) {
  606. foreach ($array as $key => $value) {
  607. unset($array[$key]);
  608. return $key;
  609. }
  610. }
  611. protected static $_counterStartTime;
  612. /**
  613. * Returns microtime as float value
  614. * (to be subtracted right away)
  615. *
  616. * @param int $precision
  617. * @return float
  618. */
  619. public static function microtime($precision = 8) {
  620. return round(microtime(true), $precision);
  621. }
  622. /**
  623. * @return void
  624. */
  625. public static function startClock() {
  626. static::$_counterStartTime = static::microtime();
  627. }
  628. /**
  629. * @param int $precision
  630. * @param bool $restartClock
  631. * @return float
  632. */
  633. public static function returnElapsedTime($precision = 8, $restartClock = false) {
  634. $startTime = static::$_counterStartTime;
  635. if ($restartClock) {
  636. static::startClock();
  637. }
  638. return static::calcElapsedTime($startTime, static::microtime(), $precision);
  639. }
  640. /**
  641. * Returns microtime as float value
  642. * (to be subtracted right away)
  643. *
  644. * @param int $start
  645. * @param int $end
  646. * @param int $precision
  647. * @return float
  648. */
  649. public static function calcElapsedTime($start, $end, $precision = 8) {
  650. $elapsed = $end - $start;
  651. return round($elapsed, $precision);
  652. }
  653. /**
  654. * Returns pretty JSON
  655. *
  656. * @link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
  657. * @param string $json The original JSON string
  658. * @param string $ind The string to indent with
  659. * @return string
  660. */
  661. public static function prettyJson($json, $indString = "\t") {
  662. // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
  663. $tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);
  664. $indent = 0;
  665. $result = '';
  666. $quotemarksCounter = 0;
  667. $nextTokenUsePrefix = true;
  668. foreach ($tokens as $token) {
  669. $quotemarksCounter = $quotemarksCounter + (count(explode('"', $token)) - 1);
  670. if ($token === '') {
  671. continue;
  672. }
  673. if ($nextTokenUsePrefix) {
  674. $prefix = str_repeat($indString, $indent);
  675. } else {
  676. $prefix = null;
  677. }
  678. // Determine if the quote marks are open or closed
  679. if ($quotemarksCounter & 1) {
  680. // odd - thus quotemarks open
  681. $nextTokenUsePrefix = false;
  682. $newLine = null;
  683. } else {
  684. // even - thus quotemarks closed
  685. $nextTokenUsePrefix = true;
  686. $newLine = "\n";
  687. }
  688. if ($token === "{" || $token === "[") {
  689. $indent++;
  690. $result .= $token . $newLine;
  691. } elseif ($token === "}" || $token === "]") {
  692. $indent--;
  693. if ($indent >= 0) {
  694. $prefix = str_repeat($indString, $indent);
  695. }
  696. if ($nextTokenUsePrefix) {
  697. $result .= $newLine . $prefix . $token;
  698. } else {
  699. $result .= $newLine . $token;
  700. }
  701. } elseif ($token === ",") {
  702. $result .= $token . $newLine;
  703. } else {
  704. $result .= $prefix . $token;
  705. }
  706. }
  707. return str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
  708. }
  709. }