diff -r 8f62a406d9d9 -r fcc1eac04772 irclogs.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/irclogs.php Wed May 07 23:56:26 2008 -0400 @@ -0,0 +1,329 @@ +load_theme('oxygen', 'bleu'); + +$template->header(); + +$q = $db->sql_query('USE `' . $mysql_dbname . '`;'); +if ( !$q ) + $db->_die(); + +$days_in_month = array( + 1 => 31, + 2 => ( intval(date('Y')) % 4 == 0 ? 29 : 28 ), + 3 => 31, + 4 => 30, + 5 => 31, + 6 => 30, + 7 => 31, + 8 => 31, + 9 => 30, + 10 => 31, + 11 => 30, + 12 => 31 + ); + +function irc_make_calendar($year = 0, $month = 0, $days_to_link = array()) +{ + global $session; + global $days_in_month, $channel; + + if ( $year < 1970 || $month < 1 || $month > 12 ) + { + $year = intval(date('Y')); + $month = intval(date('n')); + } + + $month = intval($month); + $year = intval($year); + + $this_month = mktime(0, 0, 1, $month, 1, $year); + + $next_month = mktime(0, 0, 1, ( $month + 1 ), 1, $year); + $next_year = mktime(0, 0, 1, $month, 1, ( $year + 1 ) ); + $prev_month = mktime(0, 0, 1, ( $month - 1 ), 1, $year); + $prev_year = mktime(0, 0, 1, $month, 1, ( $year - 1 ) ); + + $a_next_month = '>'; + $a_next_year = '>>'; + $a_prev_month = ( $year == 1970 && $month == 1 ) ? '<' : '<'; + $a_prev_year = ( $year == 1970 ) ? '<<' : '<<'; + + $dow = intval(date('w', $this_month)); + + $return = ''; + + $return .= '
+ + + + '; + + $return .= ''; + $class = 'row1'; + for ( $i = 0; $i < $dow; $i++ ) + { + $class = ( $class == 'row1' ) ? 'row3' : 'row1'; + $return .= ''; + } + + if ( $month == 2 ) + { + $days_in_this_month = ( $year % 4 == 0 ) ? 29 : 28; + } + else + { + $days_in_this_month = $days_in_month[$month]; + } + + for ( $i = 1; $i <= $days_in_this_month; $i++ ) + { + if ( $dow == 7 ) + { + $return .= ''; + if ( $i < $days_in_this_month ) + $return .= ''; + $dow = 0; + } + $dow++; + $class = ( $class == 'row1' ) ? 'row3' : 'row1'; + $a = "$i"; + if ( in_array($i, $days_to_link) ) + { + $a = '' . $i . ''; + } + $return .= ""; + } + + while ( $dow < 7 ) + { + $class = ( $class == 'row1' ) ? 'row3' : 'row1'; + $return .= ""; + $dow++; + } + + $return .= '
' . "$a_prev_year $a_prev_month " . date('F', $this_month) . ' ' . $year . " $a_next_month $a_next_year" . '
$a
'; + return $return; +} + +$get_valid_year = isset($_GET['year']); +$get_valid_month = ( isset($_GET['month']) && intval($_GET['month']) > 0 && intval($_GET['month']) < 13 ); + +$year = ( $get_valid_year ) ? intval($_GET['year']) : intval(date('Y')); +$month = ( $get_valid_month ) ? intval($_GET['month']) : intval(date('n')); + +function make_nick_color($username) +{ + if ( $username == '' ) + return ''; + $hash = substr(sha1($username), 0, 6); + $hash = enano_str_split($hash); + for ( $i = 0; $i < count($hash); $i++ ) + { + if ( $i % 2 == 1 ) + continue; + + // call this a cheap hack or whatever, but intval() doesn't accept 0x???? + $digit = eval("return 0x{$hash[$i]};"); + if ( $digit > 0x9 ) + $digit = "9"; + else + $digit = strval($digit); + $hash[$i] = $digit; + } + $color = implode('', $hash); + $span = "" . htmlspecialchars($username) . ""; + return $span; +} + +function irclog_autoparse_links($text) +{ + $sid = md5(microtime()); + preg_match_all('/((https?|ftp|irc):\/\/([^@\s\]"\':]+)?((([a-z0-9-]+\.)*)[a-z0-9-]+)(\/[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*(\?(([a-' + . 'z0-9_-]+)(=[A-z0-9_%\|~`\!@#\$\^&\*\(\):;\.,\/-\[\]]*)?((&([a-z0-9_-]+)(=[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*)?)*))' + . '?)?)?)/', $text, $matches); + foreach ( $matches[0] as $i => $match ) + { + $text = str_replace_once($match, "{AUTOLINK:$sid:$i}", $text); + } + $text = htmlspecialchars($text); + foreach ( $matches[0] as $i => $match ) + { + $match_short = $match; + if ( strlen($match) > 75 ) + { + $match_short = htmlspecialchars(substr($match, 0, 25)) . '...' . htmlspecialchars(substr($match, -25)); + } + $match = htmlspecialchars($match); + $text = str_replace_once("{AUTOLINK:$sid:$i}", "$match_short", $text); + } + return $text; +} + +function irclog_protect_emails($text) +{ + global $email; + preg_match_all('/([a-z0-9_-]+@(([a-z0-9-]+\.)*)[a-z0-9-]+)/', $text, $matches); + foreach ( $matches[0] as $match ) + { + $text = str_replace_once($match, $email->encryptEmail($match), $text); + } + return $text; +} + +function irclog_format_row($_, $row) +{ + static $class = 'row1'; + $class = ( $class == 'row1' ) ? 'row3' : 'row1'; + + $time = date('H:i', $row['timestamp']); + $nick = make_nick_color($row['nick']); + + $message = irclog_autoparse_links($row['line']); + $message = irclog_protect_emails($message); + $message = RenderMan::smilieyize($message); + if ( $row['nick'] == '' ) + $message = "" . $message . ""; + return " + $time + $nick + $message + \n"; +} + +if ( $get_valid_year && $get_valid_month && isset($_GET['day']) && isset($_GET['channel']) ) +{ + $days_in_this_month = $days_in_month[$month]; + if ( $month == 2 && $year !== intval(date('n')) ) + $days_in_this_month = ( $year % 4 == 0 ) ? 29 : 28; + $day = intval($_GET['day']); + if ( $day < 1 || $day > $days_in_this_month ) + { + $day = intval(date('j')); + } + // mode is view logs, and we have the date on which to display them + $channel = $db->escape($_GET['channel']); + if ( !preg_match('/^[a-z0-9_-]+$/i', $channel) ) + die('Channel contains XSS attempt'); + + $datekey = $year . '-' . + ( $month < 10 ? "0$month" : $month ) . '-' . + ( $day < 10 ? "0$day" : $day ); + + $q = $db->sql_query("SELECT * FROM irclog WHERE day='$datekey' AND channel = '#$channel' ORDER BY timestamp ASC;"); + if ( !$q ) + $db->_die(); + + echo '

< Back to date listings

'; + + if ( $db->numrows() < 1 ) + { + echo '

No chat logs for today.

'; + } + else + { + $count = $db->numrows(); + + $start = ( isset($_GET['start']) ) ? intval($_GET['start']) : 0; + + // ($q, $tpl_text, $num_results, $result_url, $start = 0, $perpage = 10, $callers = Array(), $header = '', $footer = '') + $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'), '

All times are UTC.

', '
'); + + echo $html; + } +} +else if ( isset($_GET['channel']) ) +{ + // show log calendar + $channel = $db->escape($_GET['channel']); + if ( !preg_match('/^[a-z0-9_-]+$/i', $channel) ) + die('Channel contains XSS attempt'); + + echo "

Chat logs for #$channel

"; + + echo '

< Back to channel list

'; + + $year = strval($year); + $month = strval($month); + + if ( $month < 10 ) + $month = "0" . $month; + + $q = $db->sql_query("SELECT day FROM irclog WHERE day LIKE '$year-$month-__' AND channel = '#$channel' GROUP BY day ORDER BY timestamp ASC;"); + if ( !$q ) + $db->_die(); + + $days = array(); + while ( $row = $db->fetchrow() ) + { + if ( !preg_match('/^[0-9]+-[0-9]+-([0-9]+)$/', $row['day'], $match) ) + { + continue; + } + $days[] = intval($match[1]); + } + + echo irc_make_calendar($year, $month, $days); +} +else +{ + // list channels + $q = $db->sql_query("SELECT channel FROM irclog GROUP BY channel;"); + if ( !$q ) + $db->_die(); + echo '

List of available channels

'; + if ( $row = $db->fetchrow() ) + { + echo '

'; + do + { + $channel = preg_replace('/^#/', '', $row['channel']); + echo '' . $row['channel'] . '
'; + } + while ( $row = $db->fetchrow() ); + echo '

'; + } + else + { + echo '

No channels logged.

'; + } +} + +$q = $db->sql_query('USE enano_www;'); +if ( !$q ) + $db->_die(); + +$template->footer(); +