Subversion Repositories logviewer

Rev

Rev 4 | Rev 9 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * ViaThinkSoft LogViewer
  5.  * Copyright 2018-2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. require_once __DIR__.'/config.inc.php';
  21.  
  22. $hostname = trim(file_get_contents('/etc/hostname'));
  23.  
  24. if (file_exists(__DIR__."/db_$hostname.inc.php")) {
  25.         require_once __DIR__."/db_$hostname.inc.php";
  26. } else {
  27.         require_once __DIR__.'/db.inc.php';
  28. }
  29.  
  30. if (file_exists(__DIR__."/auth_$hostname.inc.php")) {
  31.         require_once __DIR__."/auth_$hostname.inc.php";
  32. } else {
  33.         require_once __DIR__.'/auth.inc.php';
  34. }
  35.  
  36. try {
  37.         logviewer_check_access();
  38. } catch (Exception $e) {
  39.         die('<h1>ViaThinkSoft LogViewer - Error</h1><p>'.$e->getMessage().'</p>');
  40. }
  41.  
  42. $sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
  43. if ($sort == '') $sort = 'anzahl';
  44. if (($sort != 'anzahl') && ($sort != 'letzter') && ($sort != 'random')) die('Sort falsch');
  45.  
  46. # Please keep this code synchronized with ajax_cmd.php
  47. $add_filters = logviewer_additional_filter();
  48. $hardcoded_filters = empty($add_filters) ? '' : "and ($add_filters)";
  49. $hardcoded_filters .= " and (letzter >= DATE_SUB(NOW(),INTERVAL ".MAXYEARS." YEAR))";
  50. # Please keep this code synchronized with ajax_cmd.php
  51.  
  52. if (isset($_REQUEST['solveall']) && logviewer_allow_solvemark()) {
  53.         db_query("update vts_fehlerlog set anzahlsolved = anzahl where text like '%".db_real_escape_string($_REQUEST['solveall'])."%'");
  54.         header('location:?sort='.urlencode($sort)); // avoid F5
  55.         die();
  56. }
  57.  
  58. $filter_add = '';
  59. if (isset($_REQUEST['filter'])) {
  60.         $ary = explode(' ', $_REQUEST['filter']);
  61.         foreach ($ary as $a) {
  62.                 $a = trim($a);
  63.                 if ($a == '') continue;
  64.  
  65.                 if (substr($a,0,1) == '-') {
  66.                         $negate = "NOT ";
  67.                         $a = substr($a, 1); // remove "-"
  68.                 } else {
  69.                         $negate = " ";
  70.                 }
  71.  
  72.                 $filter_add .= " and text $negate like '".db_real_escape_string('%'.$a.'%')."' ";
  73.         }
  74. }
  75.  
  76. ?>
  77. <html>
  78.  
  79. <head>
  80.         <title>ViaThinkSoft LogViewer</title>
  81.         <script src="ajax.js"></script>
  82.         <link href="style<?php if (file_exists("style_$hostname.css")) echo "_$hostname"; ?>.css" rel="stylesheet" type="text/css">
  83. </head>
  84.  
  85. <body>
  86.  
  87. <h1>ViaThinkSoft LogViewer</h1>
  88.  
  89. <form method="GET" action="index.php">
  90. <input type="hidden" name="sort" value="<?php echo htmlentities($sort); ?>">
  91. <p>Filter: <input style="width:300px" type="text" name="filter" value="<?php echo htmlentities(isset($_REQUEST['filter']) ? $_REQUEST['filter'] : ''); ?>"> <input type="submit" value="Filter"><?php
  92. if (isset($_REQUEST['filter'])) {
  93.         echo ' <a href="?sort='.htmlentities($sort).'">Clear filter</a>';
  94.         if (logviewer_allow_solvemark()) {
  95.                 echo ' | <a href="?sort='.htmlentities($sort).'&solveall='.urlencode($_REQUEST['filter']).'" onclick="return confirm(\'Are you sure?\');">Solve all</a>';
  96.         }
  97. }
  98. ?></p>
  99. <p><font size="-3">Search terms divided with whitespace. Prepend hyphen to exclude a search term. Only field "Message" will be searched.</font></p>
  100. </form>
  101.  
  102. <?php
  103. if (!empty($add_filters)) {
  104.         echo '<span class="filter_hint">Showing max. '.COUNT.' results of max. '.MAXYEARS.' years; Hardcoded filter: '.htmlentities($add_filters).'</span>';
  105. } else {
  106.         echo '<span class="filter_hint">Showing max. '.COUNT.' results of max. '.MAXYEARS.' years</span>';
  107. }
  108. ?>
  109.  
  110. <div id="sort">Sort by: <?php
  111.  
  112. if ($sort == 'anzahl') {
  113.         echo '<span class="selected_menu">Occurrences</span>';
  114. } else {
  115.         echo '<a href="?sort=anzahl'.((isset($_REQUEST['filter'])) ? '&filter='.urlencode($_REQUEST['filter']) : '').'">Occurrences</a>';
  116. }
  117.  
  118. ?> | <?php
  119.  
  120. if ($sort == 'letzter') {
  121.         echo '<span class="selected_menu">Last occurrence</span>';
  122. } else {
  123.         echo '<a href="?sort=letzter'.((isset($_REQUEST['filter'])) ? '&filter='.urlencode($_REQUEST['filter']) : '').'">Last occurrence</a>';
  124. }
  125.  
  126. ?> | <?php
  127.  
  128. if ($sort == 'random') {
  129.         echo '<span class="selected_menu">Random order</span>';
  130. } else {
  131.         echo '<a href="?sort=random'.((isset($_REQUEST['filter'])) ? '&filter='.urlencode($_REQUEST['filter']) : '').'">Random order</a>';
  132. }
  133.  
  134. ?> (<span id="count"><?php
  135.  
  136. $res = db_query("select count(*) as cnt from vts_fehlerlog where (anzahl > anzahlsolved) ".$filter_add." ".$hardcoded_filters.";");
  137. $row = db_fetch_object($res);
  138. echo $row->cnt;
  139.  
  140. ?></span>)</div>
  141.  
  142. <table border="1" width="100%">
  143. <thead>
  144. <tr>
  145. <?php
  146. if (SOURCE_STYLE == 0) {
  147.         // nothing
  148. } else if (SOURCE_STYLE == 1) {
  149. ?>
  150.         <td>Source</td>
  151.         <td>Module</td>
  152. <?php
  153. } else if (SOURCE_STYLE == 2) {
  154. ?>
  155.         <td>User</td>
  156. <?php
  157. }
  158. ?>
  159.         <?php if (logviewer_allow_solvemark()) { ?><td>Mark&nbsp;as...</td><?php } ?>
  160.         <td>Count</td>
  161.         <td>Last&nbsp;occurrence</td>
  162.         <td>Message</td>
  163. </tr>
  164. </thead>
  165. <tbody>
  166. <tr>
  167. <?php
  168.  
  169. $odd = true;
  170.  
  171. if ($sort == 'letzter') {
  172.         $res = db_query("select * from vts_fehlerlog where (anzahl > anzahlsolved) ".$filter_add." ".$hardcoded_filters." order by letzter desc, anzahl desc, id asc limit ".COUNT);
  173. } else if ($sort == 'anzahl') {
  174.         $res = db_query("select * from vts_fehlerlog where (anzahl > anzahlsolved) ".$filter_add." ".$hardcoded_filters." order by anzahl desc, letzter desc, id asc limit ".COUNT);
  175. } else if ($sort == 'random') {
  176.         $res = db_query("select * from vts_fehlerlog where (anzahl > anzahlsolved) ".$filter_add." ".$hardcoded_filters." order by RAND() limit ".COUNT);
  177. }
  178. while ($row = db_fetch_object($res)) {
  179.         $text = htmlentities($row->text);
  180.         $text = preg_replace('@ ([^ ]{2,}) on line@ismU', ' <a href="?sort='.urlencode($sort).'&filter=\1">\1</a> on line', $text); // TODO: urlencode \1
  181.         $text = preg_replace('@(at|in) ([^ ]{2,}):(\d+)@ismU', '\1 <a href="?sort='.urlencode($sort).'&filter=\2">\2</a>:\3', $text); // TODO: urlencode \2
  182.  
  183.         $anzahl = htmlentities($row->anzahl);
  184.         if ($row->anzahlsolved != 0) $anzahl .= '<br>('.$row->anzahlsolved.')';
  185.  
  186.         $class = $odd ? 'tr_odd' : 'tr_even';
  187.         $odd = !$odd;
  188.  
  189.         echo '<tr id="line'.$row->id.'" class="'.$class.'">';
  190.         if (SOURCE_STYLE == 0) {
  191.                 // nothing
  192.         } else if (SOURCE_STYLE == 1) {
  193.                 echo '<td>'.htmlentities($row->logfile).'</td>';
  194.                 echo '<td>'.htmlentities($row->modul).'</td>';
  195.         } else {
  196.                 $user = preg_match('@/home/(.+)/@sU', $row->logfile, $m) ? $m[1] : ((strpos($row->logfile, '/root/') === 0) ? 'root' : '');
  197.                 echo '<td>'.htmlentities($user).'</td>';
  198.         }
  199.         if (logviewer_allow_solvemark()) echo '<td><a href="javascript:_solve('.$row->id.')">Solved</a></td>';
  200.         echo '<td>'.$anzahl.'</td>';
  201.         echo '<td>'.htmlentities($row->letzter).'</td>';
  202.         echo '<td>'.$text.'</td>';
  203.         echo '</tr>';
  204.         flush();
  205. }
  206. ?>
  207. </tr>
  208. </tbody>
  209. </table>
  210.  
  211. </body>
  212.  
  213. </html>
  214.