Its_Patchi

parser

Aug 13th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.    
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9
  12. 10
  13. 11
  14. 12
  15. 13
  16. 14
  17. 15
  18. 16
  19. 17
  20. 18
  21. 19
  22. 20
  23. 21
  24. 22
  25. 23
  26. 24
  27. 25
  28. 26
  29. 27
  30. 28
  31. 29
  32. 30
  33. 31
  34. 32
  35. 33
  36. 34
  37. 35
  38. 36
  39. 37
  40. 38
  41. 39
  42. 40
  43. 41
  44. 42
  45. 43
  46. 44
  47. 45
  48. 46
  49. 47
  50. 48
  51. 49
  52. 50
  53. 51
  54. 52
  55. 53
  56. 54
  57. 55
  58. 56
  59. 57
  60. 58
  61. 59
  62. 60
  63. 61
  64. 62
  65. 63
  66. 64
  67. 65
  68. 66
  69. 67
  70. 68
  71. 69
  72. 70
  73. 71
  74. 72
  75. 73
  76. 74
  77. 75
  78. 76
  79. 77
  80. 78
  81. 79
  82. 80
  83. 81
  84. 82
  85. 83
  86. 84
  87. 85
  88. 86
  89. 87
  90. 88
  91. 89
  92. 90
  93. 91
  94. 92
  95. 93
  96. 94
  97. 95
  98. 96
  99. 97
  100. 98
  101. 99
  102. 100
  103. 101
  104. 102
  105. 103
  106. 104
  107. 105
  108. 106
  109. 107
  110. 108
  111. 109
  112. 110
  113. 111
  114. 112
  115. 113
  116. 114
  117. 115
  118. 116
  119. 117
  120. 118
  121. 119
  122. 120
  123. 121
  124. 122
  125. 123
  126. 124
  127. 125
  128. 126
  129. 127
  130. 128
  131. 129
  132. 130
  133. 131
  134. 132
  135. 133
  136. 134
  137. 135
  138. 136
  139. 137
  140. 138
  141. 139
  142. 140
  143. 141
  144. 142
  145. 143
  146. 144
  147. 145
  148. 146
  149. 147
  150. 148
  151. 149
  152. 150
  153. 151
  154. 152
  155. 153
  156. 154
  157. 155
  158. 156
  159. 157
  160. 158
  161. 159
  162. 160
  163. 161
  164. 162
  165. 163
  166. <?php
  167. require("colorparser.php");
  168.  
  169. function makeClickableLinks($s)
  170. {
  171.     return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
  172. }
  173.  
  174. function parseLine($line)
  175. {
  176.     /**
  177.      * "Normal" lines.
  178.      */
  179.     if (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] <(?<nick>\S+)> (?<line>.+)$/', $line, $matches)) {
  180.         $line = "[$matches[time]] <$matches[nick]> $matches[line]";
  181.         /**
  182.          * "Join" lines.
  183.          */
  184.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* Joins: (?<nick>\S+) \((?<host>\S+)\)$/', $line, $matches)) {
  185.         $line = "<font color=\"green\">[$matches[time]] *** Joins: $matches[nick] ($matches[host])</font>";
  186.         /**
  187.          * "Quit" lines.
  188.          */
  189.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* Quits: (?<nick>\S+) \((?<host>\S+)\) \((?<reason>.*\))$/', $line, $matches)) {
  190.         $line = "<font color=\"red\">[$matches[time]] *** Quits: $matches[nick] ($matches[host]) ($matches[reason])</font>";
  191.         /**
  192.          * "Mode" lines.
  193.          */
  194.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* (?<nick_performing>\S+) sets mode: (?<modes>[-+][ov]+([-+][ov]+)?) (?<nicks_undergoing>\S+( \S+)*)$/', $line, $matches)) {
  195.         $modenum          = 0;
  196.         $nicks_undergoing = explode(' ', $matches['nicks_undergoing']);
  197.         for ($i = 0, $j = strlen($matches['modes']); $i < $j; $i++) {
  198.             $mode = substr($matches['modes'], $i, 1);
  199.             if ($mode === '-' || $mode === '+') {
  200.                 $modesign = $mode;
  201.             } else {
  202.                 $line = "<font color=\"darkgrey\">[$matches[time]] $matches[nick_performing] sets mode: $modesign$mode on $nicks_undergoing[$modenum]</font>";
  203.                 $modenum++;
  204.             }
  205.         }
  206.         //Actions
  207.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \* (?<line>(?<nick_performing>\S+) ((?<slap>[sS][lL][aA][pP][sS]( (?<nick_undergoing>\S+)( .+)?)?)|(.+)))$/', $line, $matches)) {
  208.         $line = "<font color=\"purple\">[$matches[time]] * $matches[line]</font>";
  209.         //"Nickchange" lines.
  210.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* (?<nick_performing>\S+) is now known as (?<nick_undergoing>\S+)$/', $line, $matches)) {
  211.         $line = "<font color=\"blue\">[$matches[time]] *** $matches[nick_performing] is now known as $matches[nick_undergoing]</font>";
  212.         //"Part" lines.
  213.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* Parts: (?<nick>\S+) \(\S+\)( \(.*\))?$/', $line, $matches)) {
  214.     //$line = "<font color=\"red\">
  215.         /**$this->set_part($matches['time'], $matches['nick']);
  216.          * "Topic" lines.
  217.          */
  218.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* (?<nick>\S+) changes topic to \'(?<line>.+)\'$/', $line, $matches)) {
  219.         /**if ($matches['line'] !== ' ') {
  220.         $this->set_topic($matches['time'], $matches['nick'], $matches['line']);
  221.         }
  222.         * "Kick" lines.
  223.         */
  224.     } elseif (preg_match('/^\[(?<time>\d{2}:\d{2}(:\d{2})?)\] \*\*\* (?<line>(?<nick_undergoing>\S+) was kicked by (?<nick_performing>\S+) \(.*\))$/', $line, $matches)) {
  225.         /** $this->set_kick($matches['time'], $matches['nick_performing'], $matches['nick_undergoing'], $matches['line']); */
  226.     }
  227.     return $line;
  228. }
  229.  
  230. $escaped = preg_replace('/[^A-Za-z0-9_\-]\./', '_', $_GET['log']);
  231.  
  232. $buffer = ob_get_clean();
  233. $tidy   = new tidy();
  234. if (!isset($_GET['plain'])) {
  235. $buffer = "<head>
  236. <title>OCLogs: $escaped</title>
  237.    <script type=\"text/javascript\"
  238.    src=\"//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js\">
  239.    </script>
  240.     <script type=\"text/javascript\" language=\"javascript\">
  241.         $(function () {
  242.             $('#scrlBotm').click(function () {
  243.                 $('html, body').animate({
  244.                     scrollTop: $(document).height()
  245.                 },
  246.                 1500);
  247.                 return false;
  248.             });
  249.  
  250.             $('#scrlTop').click(function () {
  251.                 $('html, body').animate({
  252.                     scrollTop: '0px'
  253.                 },
  254.                 1500);
  255.                 return false;
  256.             });
  257.         });
  258.    </script>
  259. </head>
  260. </body>";
  261. }
  262. $mcp    = new MircColorParser();
  263. $linenum = 1;
  264. if ($escaped != "") {
  265.     $handle = fopen($escaped, "r");
  266.     if (!isset($_GET['plain'])) {
  267.             date_default_timezone_set(timezone_name_from_abbr("CST"));
  268.             $yesterday = date('Y-m-d', strtotime( str_replace(".log", "", $escaped) . ' -1 day' ));
  269.             $tomorrow = date('Y-m-d', strtotime( str_replace(".log", "", $escaped) . ' +1 day' ));
  270.             if (file_exists($yesterday . ".log")) {
  271.                 $buffer .= "<a href=\"parser?log=$yesterday.log\"><<Prev</a> ";
  272.             }
  273.             if (file_exists($tomorrow . ".log")) {
  274.                 $buffer .= "<a href=\"parser?log=$tomorrow.log\">Next>></a> ";
  275.             }
  276.             $buffer .= "<a id=\"scrlBotm\" href=\"#\">Scroll to Bottom</a> <a href=\"parser?log=$escaped&refresh#bottom\">Auto Refresh</a><br>";
  277.     }
  278.     if ($handle) {
  279.         while (($line = fgets($handle)) !== false) {
  280.             $line = htmlspecialchars($line);
  281.             if (!isset($_GET['plain'])) {
  282.                 //$line = parseToHTML($line);
  283.                 $line = $mcp->colorize($line);
  284.                 $line = parseLine($line);
  285.             }
  286.             if (isset($_GET['linenum'])) {
  287.                 $line = "$linenum: " . $line;
  288.             }
  289.             $line = makeClickableLinks($line);
  290.             $buffer .= $line . "<br>";
  291.             $linenum++;
  292.         }
  293.         fclose($handle);
  294.     } else {
  295.         // error opening the file.
  296.     }
  297.     if (!isset($_GET['plain'])) {
  298.             date_default_timezone_set(timezone_name_from_abbr("CST"));
  299.             $yesterday = date('Y-m-d', strtotime( str_replace(".log", "", $escaped) . ' -1 day' ));
  300.             $tomorrow = date('Y-m-d', strtotime( str_replace(".log", "", $escaped) . ' +1 day' ));
  301.             if (file_exists($yesterday . ".log")) {
  302.                 $buffer .= "<a href=\"parser?log=$yesterday.log\"><<Prev</a> ";
  303.             }
  304.             if (file_exists($tomorrow . ".log")) {
  305.                 $buffer .= "<a href=\"parser?log=$tomorrow.log\">Next>></a> ";
  306.             }
  307.             $buffer .= "<a id=\"scrlTop\" href=\"#\">Scroll to Top</a> ";
  308.             if (!isset($_GET['refresh'])) {
  309.                 $buffer .= "<a href=\"parser?log=$escaped&refresh#bottom\">Auto Refresh</a> ";
  310.             } else {
  311.                 $buffer .= "<a href=\"parser?log=$escaped#bottom\">Stop Auto Refresh</a> ";
  312.                 $buffer .= "<script type=\"text/javascript\">
  313.                 window.setInterval(function() {
  314.                     window.location.href = '\parser?log=$escaped&refresh#bottom';
  315.                     location.reload();
  316.                 }, 5000);
  317.                 </script>";
  318.             }
  319.             $buffer .= "<a name=\"bottom\">&nbsp;</a></body></html>";
  320.             $buffer = $tidy->repairString($buffer, array(
  321.                 'preserve-entities' => true
  322.                 ), "utf8");
  323.     }
  324.     echo $buffer;
  325. } else {
  326.     echo "No log file specified";
  327. }
  328. ?>
Advertisement
Add Comment
Please, Sign In to add comment