Advertisement
AsviS

PHP JSON DECODE FORMATTER

Aug 25th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /* =============== DESCRIPTION FILE ======================================== */
  4. /*                                                                           */
  5. /*   Module  : ZEROC0D3 JSON DECODE FORMATTER                                */
  6. /*   Type    : JSON DECODER TOOLS                                            */
  7. /*   Author  : ZEROC0D3 Team                                                 */
  8. /*   Date    : July 2015                                                     */
  9. /*                                                                           */
  10. /*  __________                  _________ _______       .___________         */
  11. /*  \____    /___________  ____ \_   ___ \\   _  \    __| _/\_____  \ Team   */
  12. /*    /     // __ \_  __ \/  _ \/    \  \//  /_\  \  / __ |   _(__  <        */  
  13. /*   /     /\  ___/|  | \(  <_> )     \___\  \_/   \/ /_/ |  /       \       */
  14. /*  /_______ \___  >__|   \____/ \______  /\_____  /\____ | /______  /       */
  15. /*          \/   \/                     \/       \/      \/        \/        */
  16. /*                                                                           */
  17. /*  ZeroC0d3 Team                                                            */  
  18. /*  [ N0th1ng Imp0ss1bl3, Grey Hat Coder ]                                   */
  19. /*  --------------------------------------------------------                 */
  20. /*                                                                           */
  21. /* ========================================================================= */
  22.  
  23. global $zver;
  24. global $snap_date, $start, $end;
  25. global $str_plain;
  26. global $json, $result, $str_format;
  27. global $style1, $style2, $style3;
  28.  
  29. $GLOBALS['zver']  = "ver 1.2.1";
  30.  
  31. date_default_timezone_set('Asia/Jakarta');
  32. error_reporting(0);
  33. @ini_set('display_errors', 'Off');
  34. @ini_set('log_errors', 'Off');
  35. @ini_set('html_errors', 'Off');
  36. @ini_set('safe_mode', 'Off');
  37. @ini_set('register_globals', 'Off');
  38. @ini_set('post_max_size', '128M');
  39. @ini_set('memory_limit', '128M');
  40. @set_time_limit(0);
  41.  
  42. function cssFile()
  43. {
  44.     echo "<style>";
  45.     echo ".block { width: 270px; display: inline-block; height: 170px; padding-left: 30px; padding-bottom: 30px; text-align: center; vertical-align: top; position: relative; }  .block2 { margin: 0 auto; text-align: center; }  textarea, input, select { border: #777 1px solid; padding: 5px 0; }  .hiddenInput { display: inline-block; width: 240px; }  .code { display: none; }  .attn { color: #c00; } .button50 { width: 40%; height: 40px; border-width: 1px; border-color: #000000; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: bold; font-color: #FFFFFF; color: #FFFFFF; background-color: #000000; vertical-align: middle; }  .button100 { width: 90%; height: 40px; border-width: 1px; border-color: #000000; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: bold; font-color: #FFFFFF; color: #FFFFFF; background-color: #000000; vertical-align: middle; }  .no-print { display: none; }  .disable { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: bold; background: #DFDFDF; color: #000000; }  .disable1 { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; font-weight: bold; background: #1B1B1B; color: #000000; }  .title { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 16pt; font-weight: bold; color: #1B1B1B; vertical-align: middle; text-align: center; height: 50px; }  .title1 { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 14pt; font-weight: bold; font-color: #000000; background-color: #AFAFAF; vertical-align: middle; text-align: center; height: 30px; }  .style0 { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 9pt; font-weight: bold; color: #1B1B1B; vertical-align: middle; text-align: left; }  .style0:hover { background-color: #FFFF99; }  .style1 { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 7pt; color: #1B1B1B; padding-left: 5px; padding-right: 5px; vertical-align: middle; text-align: left; }  .style1:hover { background-color: #FFFF99; }  .style2 { font-family: \"Monospace\", \"Ubuntu Mono\", \"Courier New\", Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 9pt; font-weight: bold; color: #1B1B1B; vertical-align: middle; text-align: left; }  .style2:hover { background-color: #FFFF99; }";
  46.     echo "</style>";
  47. }
  48.  
  49. function formatJSON($json)
  50. {
  51.     global $snap_date, $start, $end;
  52.     global $str_plain;
  53.     global $json, $result, $str_format;
  54.     global $style1, $style2, $style3;
  55.  
  56.     if (!is_string($json)) {
  57.         if (phpversion() && phpversion() >= 5.4) {
  58.             return json_encode($json, JSON_PRETTY_PRINT);
  59.         }
  60.         $json = json_encode($json);
  61.     }
  62.     $result = '';
  63.     $pos = 0; // indentation level
  64.     $strLen = strlen($json);
  65.     $indentStr = "\t";
  66.     $newLine = "\n";
  67.     $prevChar = '';
  68.     $outOfQuotes = true;
  69.     for ($i = 0; $i < $strLen; $i++) {
  70.         // Speedup: copy blocks of input which don't matter re string detection and formatting.
  71.         $copyLen = strcspn($json, $outOfQuotes ? " \t\r\n\",:[{}]" : "\\\"", $i);
  72.         if ($copyLen >= 1) {
  73.             $copyStr = substr($json, $i, $copyLen);
  74.             // Also reset the tracker for escapes: we won't be hitting any right now
  75.             // and the next round is the first time an 'escape' character can be seen again at the input.
  76.             $prevChar = '';
  77.             $result .= $copyStr;
  78.             $i += $copyLen - 1; // correct for the for(;;) loop
  79.             continue;
  80.         }
  81.         // Grab the next character in the string
  82.         $char = substr($json, $i, 1);
  83.         // Are we inside a quoted string encountering an escape sequence?
  84.         if (!$outOfQuotes && $prevChar === '\\') {
  85.         // Add the escaped character to the result string and ignore it for the string enter/exit detection:
  86.             $result .= $char;
  87.             $prevChar = '';
  88.             continue;
  89.         }
  90.         // Are we entering/exiting a quoted string?
  91.         if ($char === '"' && $prevChar !== '\\') {
  92.             $outOfQuotes = !$outOfQuotes;
  93.         }
  94.         // If this character is the end of an element,
  95.         // output a new line and indent the next line
  96.         else if ($outOfQuotes && ($char === '}' || $char === ']')) {
  97.             $result .= $newLine;
  98.             $pos--;
  99.             for ($j = 0; $j < $pos; $j++) {
  100.                 $result .= $indentStr;
  101.             }
  102.         } // eat all non-essential whitespace in the input as we do our own here and it would only mess up our process
  103.         else if ($outOfQuotes && false !== strpos(" \t\r\n", $char)) {
  104.             continue;
  105.         }
  106.         // Add the character to the result string
  107.         $result .= $char;
  108.         // always add a space after a field colon:
  109.         if ($outOfQuotes && $char === ':') {
  110.             $result .= ' ';
  111.         }
  112.         // If the last character was the beginning of an element,
  113.         // output a new line and indent the next line
  114.         else if ($outOfQuotes && ($char === ',' || $char === '{' || $char === '[')) {
  115.             $result .= $newLine;
  116.             if ($char === '{' || $char === '[') {
  117.                 $pos++;
  118.             }
  119.             for ($j = 0; $j < $pos; $j++) {
  120.                 $result .= $indentStr;
  121.             }
  122.         }
  123.         $prevChar = $char;
  124.     }
  125.     $str_format = $result;
  126.     return $result;
  127. }
  128.  
  129. if ($_GET['action'] == "format_json") {
  130.     if (isset($_POST['json_unformat'])) {
  131.         $json_unformat = $_POST['json_unformat'];
  132.         $start = microtime(true);
  133.         formatJSON($json = $json_unformat);
  134.         $style1 = $str_format;
  135.         $end = microtime(true);
  136.         $snap_date = date("D, Y-m-d H:i");      
  137.     }
  138. }
  139.  
  140. ?>
  141.  
  142. <html>
  143. <head>
  144.     <title>ZeroC0d3 JSON DECODE FORMATER</title>
  145.     <?php echo cssFile(); ?>
  146. </head>
  147. <body>
  148. <table width="780" border="1" align="center">
  149.     <tr>
  150.         <th colspan="3" class="title" scope="col" bgcolor="#AFAFAF">&nbsp;<strong>ZeroC0d3 JSON DECODE FORMATER (<?php echo $GLOBALS['zver']; ?>)</strong></th>
  151.     </tr>
  152.     <form id="form1" name="form1" method="post" action="<?php echo basename($_SERVER['PHP_SELF']); ?>?action=format_json">
  153.         <tr>
  154.             <th scope="row" class="style0" width="250px">&nbsp;JSON (Unformat)&nbsp;</th>
  155.             <td class="style0" style="text-align:center" width="5">
  156.                 <center>&nbsp;:&nbsp;</center>
  157.             </td>
  158.             <td>
  159.                 <textarea name="json_unformat" cols="80" rows="5"><?php if ($start > 0) {
  160.                    echo $json_unformat . "\n"; } ?></textarea>
  161.             </td>
  162.         </tr>
  163.         <tr>
  164.             <th colspan="3" style="text-align:left" scope="row">
  165.                 <center>
  166.                     <input type="submit" name="json_text" id="  " value="FORMAT JSON" class="button100"/>
  167.                 </center>
  168.             </th>
  169.         </tr>
  170.     </form>
  171.     <tr>
  172.         <th colspan="3" class="title1" scope="col">&nbsp;<strong>JSON FORMATED :: RESULTS</strong></th>
  173.     </tr>
  174.     <tr>
  175.         <th scope="row" class="style0" width="250px">&nbsp;Visual Decoded (Style-1)&nbsp;</th>
  176.         <td class="style0" style="text-align:center" width="5">
  177.             <center>&nbsp;:&nbsp;</center>
  178.         </td>
  179.         <td>
  180.             <textarea name="vstyle1" cols="80" rows="30" readonly><?php if ($start > 0) {
  181.                     echo $style1 . "\n";
  182.                 } ?></textarea>
  183.         </td>
  184.     </tr>
  185.     <tr>
  186.         <th scope="row" class="style0" width="250px">&nbsp;Visual Decoded (Style-2)&nbsp;</th>
  187.         <td class="style0" style="text-align:center" width="5">
  188.             <center>&nbsp;:&nbsp;</center>
  189.         </td>
  190.         <td>
  191.             <textarea name="vstyle2" cols="80" rows="10" readonly><?php if ($start > 0) {
  192.                    $style2 = var_dump(json_decode($style1, true));
  193.                    echo $style2 . "\n";
  194.                 } ?></textarea>
  195.         </td>
  196.     </tr>
  197.     <tr>
  198.         <th scope="row" class="style0" width="250px">&nbsp;Visual Decoded (Style-3)&nbsp;</th>
  199.         <td class="style0" style="text-align:center" width="5">
  200.             <center>&nbsp;:&nbsp;</center>
  201.         </td>
  202.         <td>
  203.             <textarea name="vstyle3" cols="80" rows="10" readonly><?php if ($start > 0) {
  204.                     $style3 = var_dump(json_decode($style1, false, 512, JSON_BIGINT_AS_STRING));
  205.                     echo $style3 . "\n";
  206.                 } ?></textarea>
  207.         </td>
  208.     </tr>
  209.     <tr>
  210.         <th colspan="3" class="style1" scope="row">&nbsp;Execution Time (s)
  211.             : <?php echo "<strong>" . ($end - $start) . " | Last Activities : " . $snap_date . " </strong>"; ?></th>
  212.     </tr>
  213. </table>
  214. </body>
  215. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement