irclogs.php
changeset 5 fcc1eac04772
equal deleted inserted replaced
4:8f62a406d9d9 5:fcc1eac04772
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * EnanoBot - copyright (C) 2008 Dan Fuhry
       
     5  * All rights reserved.
       
     6  */
       
     7 
       
     8 /*****************************************************************
       
     9  * YOU NEED TO SET THE PATH TO THE REST OF THE EnanoBot FILES HERE.
       
    10  * Include a trailing slash.
       
    11  * This script MUST be placed in an Enano installation directory.
       
    12  *****************************************************************/
       
    13 
       
    14 define('ENANOBOT_ROOT', './');
       
    15 
       
    16 // We're authed.
       
    17 // Load config
       
    18 require(ENANOBOT_ROOT . 'config.php');
       
    19 
       
    20 // check config
       
    21 if ( empty($mysql_host) || empty($mysql_user) || empty($mysql_dbname) )
       
    22 {
       
    23   die("Bad config file - have a look at config-sample.php.\n");
       
    24 }
       
    25 
       
    26 $title_append = '';
       
    27 if ( isset($_GET['channel']) )
       
    28   $title_append .= ' - #' . $_GET['channel'];
       
    29 
       
    30 $title = "Enano IRC logs$title_append";
       
    31 require('includes/common.php');
       
    32 
       
    33 // unset($template);
       
    34 // $template = new template();
       
    35 // $template->load_theme('oxygen', 'bleu');
       
    36 
       
    37 $template->header();
       
    38 
       
    39 $q = $db->sql_query('USE `' . $mysql_dbname . '`;');
       
    40 if ( !$q )
       
    41   $db->_die();
       
    42 
       
    43 $days_in_month = array(
       
    44     1 => 31,
       
    45     2 => ( intval(date('Y')) % 4 == 0 ? 29 : 28 ),
       
    46     3 => 31,
       
    47     4 => 30,
       
    48     5 => 31,
       
    49     6 => 30,
       
    50     7 => 31,
       
    51     8 => 31,
       
    52     9 => 30,
       
    53     10 => 31,
       
    54     11 => 30,
       
    55     12 => 31
       
    56   );
       
    57 
       
    58 function irc_make_calendar($year = 0, $month = 0, $days_to_link = array())
       
    59 {
       
    60   global $session;
       
    61   global $days_in_month, $channel;
       
    62   
       
    63   if ( $year < 1970 || $month < 1 || $month > 12 )
       
    64   {
       
    65     $year = intval(date('Y'));
       
    66     $month = intval(date('n'));
       
    67   }
       
    68   
       
    69   $month = intval($month);
       
    70   $year = intval($year);
       
    71   
       
    72   $this_month = mktime(0, 0, 1, $month, 1, $year);
       
    73   
       
    74   $next_month = mktime(0, 0, 1, ( $month + 1 ), 1, $year);
       
    75   $next_year = mktime(0, 0, 1, $month, 1, ( $year + 1 ) );
       
    76   $prev_month = mktime(0, 0, 1, ( $month - 1 ), 1, $year);
       
    77   $prev_year = mktime(0, 0, 1, $month, 1, ( $year - 1 ) );
       
    78   
       
    79   $a_next_month = '<a href="' . htmlspecialchars($session->append_sid(scriptPath . '/irclogs.php?year=' . date('Y', $next_month) . '&month=' . date('n', $next_month) . '&channel=' . $channel)) . '">&gt;</a>';
       
    80   $a_next_year = '<a href="' . htmlspecialchars($session->append_sid(scriptPath . '/irclogs.php?year=' . date('Y', $next_year) . '&month=' . $month . '&channel=' . $channel)) . '">&gt;&gt;</a>';
       
    81   $a_prev_month = ( $year == 1970 && $month == 1 ) ? '&lt;' : '<a href="' . htmlspecialchars($session->append_sid(scriptPath . '/irclogs.php?year=' . date('Y', $prev_month) . '&month=' . date('n', $prev_month) . '&channel=' . $channel)) . '">&lt;</a>';
       
    82   $a_prev_year  = ( $year == 1970 ) ? '&lt;&lt;' : '<a href="' . htmlspecialchars($session->append_sid(scriptPath . '/irclogs.php?year=' . date('Y', $prev_year) . '&month=' . $month . '&channel=' . $channel)) . '">&lt;&lt;</a>';
       
    83   
       
    84   $dow = intval(date('w', $this_month));
       
    85   
       
    86   $return = '';
       
    87   
       
    88   $return .= '<div class="tblholder" style="display: table; text-align: center;">
       
    89           <table border="0" cellspacing="1" cellpadding="6">
       
    90             <tr>
       
    91               <th colspan="7">' . "$a_prev_year $a_prev_month " . date('F', $this_month) . ' ' . $year . " $a_next_month $a_next_year" . '</th>
       
    92             </tr>';
       
    93   
       
    94   $return .= '<tr>';
       
    95   $class = 'row1';
       
    96   for ( $i = 0; $i < $dow; $i++ )
       
    97   {
       
    98     $class = ( $class == 'row1' ) ? 'row3' : 'row1';
       
    99     $return .= '<td class="' . $class . '"></td>';
       
   100   }
       
   101   
       
   102   if ( $month == 2 )
       
   103   {
       
   104     $days_in_this_month = ( $year % 4 == 0 ) ? 29 : 28;
       
   105   }
       
   106   else
       
   107   {
       
   108     $days_in_this_month = $days_in_month[$month];
       
   109   }
       
   110   
       
   111   for ( $i = 1; $i <= $days_in_this_month; $i++ )
       
   112   {
       
   113     if ( $dow == 7 )
       
   114     {
       
   115       $return .= '</tr>';
       
   116       if ( $i < $days_in_this_month )
       
   117         $return .= '<tr>';
       
   118       $dow = 0;
       
   119     }
       
   120     $dow++;
       
   121     $class = ( $class == 'row1' ) ? 'row3' : 'row1';
       
   122     $a = "<span style=\"color: #808080;\">$i</span>";
       
   123     if ( in_array($i, $days_to_link) )
       
   124     {
       
   125       $a = '<a class="wikilink-nonexistent" href="' . htmlspecialchars($session->append_sid(scriptPath . '/irclogs.php?year=' . $year . '&month=' . $month . '&day=' . $i . '&channel=' . $channel)) . '"><b>' . $i . '</b></a>';
       
   126     }
       
   127     $return .= "<td class=\"$class\">$a</td>";
       
   128   }
       
   129   
       
   130   while ( $dow < 7 )
       
   131   {
       
   132     $class = ( $class == 'row1' ) ? 'row3' : 'row1';
       
   133     $return .= "<td class=\"$class\"></td>";
       
   134     $dow++;
       
   135   }
       
   136 
       
   137   $return .= '</table></div>';
       
   138   return $return;
       
   139 }
       
   140 
       
   141 $get_valid_year = isset($_GET['year']);
       
   142 $get_valid_month = ( isset($_GET['month']) && intval($_GET['month']) > 0 && intval($_GET['month']) < 13 );
       
   143 
       
   144 $year = ( $get_valid_year ) ? intval($_GET['year']) : intval(date('Y'));
       
   145 $month = ( $get_valid_month ) ? intval($_GET['month']) : intval(date('n'));
       
   146 
       
   147 function make_nick_color($username)
       
   148 {
       
   149   if ( $username == '' )
       
   150     return '';
       
   151   $hash = substr(sha1($username), 0, 6);
       
   152   $hash = enano_str_split($hash);
       
   153   for ( $i = 0; $i < count($hash); $i++ )
       
   154   {
       
   155     if ( $i % 2 == 1 )
       
   156       continue;
       
   157     
       
   158     // call this a cheap hack or whatever, but intval() doesn't accept 0x????
       
   159     $digit = eval("return 0x{$hash[$i]};");
       
   160     if ( $digit > 0x9 )
       
   161       $digit = "9";
       
   162     else
       
   163       $digit = strval($digit);
       
   164     $hash[$i] = $digit;
       
   165   }
       
   166   $color = implode('', $hash);
       
   167   $span = "<span style=\"color: #$color;\">" . htmlspecialchars($username) . "</span>";
       
   168   return $span;
       
   169 }
       
   170 
       
   171 function irclog_autoparse_links($text)
       
   172 {
       
   173   $sid = md5(microtime());
       
   174   preg_match_all('/((https?|ftp|irc):\/\/([^@\s\]"\':]+)?((([a-z0-9-]+\.)*)[a-z0-9-]+)(\/[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*(\?(([a-' 
       
   175                . 'z0-9_-]+)(=[A-z0-9_%\|~`\!@#\$\^&\*\(\):;\.,\/-\[\]]*)?((&([a-z0-9_-]+)(=[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*)?)*))'
       
   176                . '?)?)?)/', $text, $matches);
       
   177   foreach ( $matches[0] as $i => $match )
       
   178   {
       
   179     $text = str_replace_once($match, "{AUTOLINK:$sid:$i}", $text);
       
   180   }
       
   181   $text = htmlspecialchars($text);
       
   182   foreach ( $matches[0] as $i => $match )
       
   183   {
       
   184     $match_short = $match;
       
   185     if ( strlen($match) > 75 )
       
   186     {
       
   187       $match_short = htmlspecialchars(substr($match, 0, 25)) . '...' . htmlspecialchars(substr($match, -25));
       
   188     }
       
   189     $match = htmlspecialchars($match);
       
   190     $text = str_replace_once("{AUTOLINK:$sid:$i}", "<a href=\"$match\">$match_short</a>", $text);
       
   191   }
       
   192   return $text;
       
   193 }
       
   194 
       
   195 function irclog_protect_emails($text)
       
   196 {
       
   197   global $email;
       
   198   preg_match_all('/([a-z0-9_-]+@(([a-z0-9-]+\.)*)[a-z0-9-]+)/', $text, $matches);
       
   199   foreach ( $matches[0] as $match )
       
   200   {
       
   201     $text = str_replace_once($match, $email->encryptEmail($match), $text);
       
   202   }
       
   203   return $text;
       
   204 }
       
   205 
       
   206 function irclog_format_row($_, $row)
       
   207 {
       
   208   static $class = 'row1';
       
   209   $class = ( $class == 'row1' ) ? 'row3' : 'row1';
       
   210   
       
   211   $time = date('H:i', $row['timestamp']);
       
   212   $nick = make_nick_color($row['nick']);
       
   213   
       
   214   $message = irclog_autoparse_links($row['line']);
       
   215   $message = irclog_protect_emails($message);
       
   216   $message = RenderMan::smilieyize($message);
       
   217   if ( $row['nick'] == '' )
       
   218     $message = "<span style=\"color: #808080;\">" . $message . "</span>";
       
   219   return "              <tr>
       
   220             <td class=\"$class\">$time</td>
       
   221             <td class=\"$class\">$nick</td>
       
   222             <td class=\"$class\">$message</td>
       
   223           </tr>\n";
       
   224 }
       
   225 
       
   226 if ( $get_valid_year && $get_valid_month && isset($_GET['day']) && isset($_GET['channel']) )
       
   227 {
       
   228   $days_in_this_month = $days_in_month[$month];
       
   229   if ( $month == 2 && $year !== intval(date('n')) )
       
   230     $days_in_this_month = ( $year % 4 == 0 ) ? 29 : 28;
       
   231   $day = intval($_GET['day']);
       
   232   if ( $day < 1 || $day > $days_in_this_month )
       
   233   {
       
   234     $day = intval(date('j'));
       
   235   }
       
   236   // mode is view logs, and we have the date on which to display them
       
   237   $channel = $db->escape($_GET['channel']);
       
   238   if ( !preg_match('/^[a-z0-9_-]+$/i', $channel) )
       
   239     die('Channel contains XSS attempt');
       
   240   
       
   241   $datekey = $year . '-' . 
       
   242       ( $month < 10 ? "0$month" : $month ) . '-' . 
       
   243       ( $day < 10 ? "0$day" : $day );
       
   244   
       
   245   $q = $db->sql_query("SELECT * FROM irclog WHERE day='$datekey' AND channel = '#$channel' ORDER BY timestamp ASC;");
       
   246   if ( !$q )
       
   247     $db->_die();
       
   248   
       
   249   echo '<p><a href="' . htmlspecialchars($session->append_sid(scriptPath . "/irclogs.php?year=$year&month=$month&channel=$channel")) . '">&lt; Back to date listings</a></p>';
       
   250   
       
   251   if ( $db->numrows() < 1 )
       
   252   {
       
   253     echo '<p>No chat logs for today.</p>';
       
   254   }
       
   255   else
       
   256   {
       
   257     $count = $db->numrows();
       
   258     
       
   259     $start = ( isset($_GET['start']) ) ? intval($_GET['start']) : 0;
       
   260     
       
   261     // ($q, $tpl_text, $num_results, $result_url, $start = 0, $perpage = 10, $callers = Array(), $header = '', $footer = '')
       
   262     $html = paginate($q, '{id}', $count, $session->append_sid(scriptPath . "/irclogs.php?year=$year&month=$month&day=$day&channel=$channel&start=%s"), $start, 100, array('id' => 'irclog_format_row'), '<p>All times are UTC.</p><div class="tblholder"><table border="0" cellspacing="1" cellpadding="4">', '</table></div>');
       
   263     
       
   264     echo $html;
       
   265   }
       
   266 }
       
   267 else if ( isset($_GET['channel']) )
       
   268 {
       
   269   // show log calendar
       
   270   $channel = $db->escape($_GET['channel']);
       
   271   if ( !preg_match('/^[a-z0-9_-]+$/i', $channel) )
       
   272     die('Channel contains XSS attempt');
       
   273   
       
   274   echo "<h3>Chat logs for #$channel</h3>";
       
   275   
       
   276   echo '<p><a href="' . htmlspecialchars($session->append_sid(scriptPath . "/irclogs.php")) . '">&lt; Back to channel list</a></p>';
       
   277 
       
   278   $year = strval($year);
       
   279   $month = strval($month);
       
   280   
       
   281   if ( $month < 10 )
       
   282     $month = "0" . $month;
       
   283   
       
   284   $q = $db->sql_query("SELECT day FROM irclog WHERE day LIKE '$year-$month-__' AND channel = '#$channel' GROUP BY day ORDER BY timestamp ASC;");
       
   285   if ( !$q )
       
   286     $db->_die();
       
   287   
       
   288   $days = array();
       
   289   while ( $row = $db->fetchrow() )
       
   290   {
       
   291     if ( !preg_match('/^[0-9]+-[0-9]+-([0-9]+)$/', $row['day'], $match) )
       
   292     {
       
   293       continue;
       
   294     }
       
   295     $days[] = intval($match[1]);
       
   296   }
       
   297   
       
   298   echo irc_make_calendar($year, $month, $days);
       
   299 }
       
   300 else
       
   301 {
       
   302   // list channels
       
   303   $q = $db->sql_query("SELECT channel FROM irclog GROUP BY channel;");
       
   304   if ( !$q )
       
   305     $db->_die();
       
   306   echo '<h3>List of available channels</h3>';
       
   307   if ( $row = $db->fetchrow() )
       
   308   {
       
   309     echo '<p>';
       
   310     do
       
   311     {
       
   312       $channel = preg_replace('/^#/', '', $row['channel']);
       
   313       echo '<a href="' . htmlspecialchars($session->append_sid(scriptPath . "/irclogs.php?channel={$channel}")) . '">' . $row['channel'] . '</a><br />';
       
   314     }
       
   315     while ( $row = $db->fetchrow() );
       
   316     echo '</p>';
       
   317   }
       
   318   else
       
   319   {
       
   320     echo '<p>No channels logged.</p>';
       
   321   }
       
   322 }
       
   323 
       
   324 $q = $db->sql_query('USE enano_www;');
       
   325 if ( !$q )
       
   326   $db->_die();
       
   327 
       
   328 $template->footer();
       
   329