die(); mb_internal_encoding('utf-8'); /* ========================================================= ========================================================= */ # スクリプトを置くファイルの相対PATH #$dir = '/home/webstats/logs/'; $dir = 'logs/'; $bad_ref = '^http://news\.scenecritique\.com'; $files = get_ref_files(); $refs = get_ref_line($files); ob_start(); header('Content-Type: text/html; charset=Shift_JIS'); $html = render_html($refs); $html = mb_convert_encoding($html, 'sjis-win', 'utf-8'); if ( header_gzip() ) { $html = gzencode($html, 3); } echo $html; header('Content-Length: '. ob_get_length()); ob_end_flush(); exit(); /* ========================================================= render_html ========================================================= */ function render_html(&$refs) { $pre_month = date('Ym', strtotime('-1 month')); $now_month = date('Ym', time()); $file = basename(__FILE__); $html = <<<_EOF_ iRCニュース Referer

{$pre_month}~{$now_month}のりふぁら~

[リファラでソート] [Hit数でソート] [時間でソート]
    _EOF_; $i = 0; foreach ( $refs as $ref ) { if ( 0 === $i % 2 ) { $html .= '
  1. '; } else { $html .= '
  2. '; } $html .= ''. htmlspecialchars($ref['ref']).''; if ( (time() - 60 * 60 * 24 * 1) < $ref['time'] ) { $html .= ' NEW! '; } $html .= ' ('.$ref['cnt'].') '; $html .= '
  3. '."\n"; $i ++; } $html .= <<<_EOF_
_EOF_; return $html; } /* ========================================================= get_ref_line ========================================================= */ function get_ref_line(&$files) { global $bad_ref; $refs = array(); $cnt = array(); $url_regex = 'https?://[\w.!~*\'();/?:@&=+$,%#-]+'; foreach ( $files as $file ) { $fh = fopen($file, 'rb') or die($file.' not found'); while ( !feof($fh) ) { $line = fgets($fh, 4096); if ( preg_match('<\[([^]]+)].+"([^"]*)" "[^"]*"$>', $line, $m) ) { $time = $m[1]; $ref = $m[2]; if ( ! preg_match('<^'.$url_regex.'$>', $ref) ) { continue; } if ( preg_match('<'. $bad_ref. '>i', $ref) ) { continue; } if ( ! isset($cnt[$ref]) ) { $cnt[$ref] = array(); $cnt[$ref]['cnt'] = 1; $cnt[$ref]['time'] = str2time($time); } else { $cnt[$ref]['cnt']++; $time = str2time($time); if ( $time > $cnt[$ref]['time']) { $cnt[$ref]['time'] = $time; } } } } fclose($fh); } $new = array(); foreach ( $cnt as $ref => $val ) { $new[] = array( 'ref' => $ref, 'cnt' => $val['cnt'], 'time' => $val['time'], ); } unset($cnt); foreach ( $new as $key => $row ) { $ref_arr[$key] = $row['ref']; $cnt_arr[$key] = $row['cnt']; $time_arr[$key] = $row['time']; } if ( isset($_GET['sort']) && 'cnt' == $_GET['sort'] ) { array_multisort($cnt_arr, SORT_NUMERIC, SORT_DESC, $new); return $new; } elseif ( isset($_GET['sort']) && 'time' == $_GET['sort'] ) { array_multisort($time_arr, SORT_NUMERIC, SORT_DESC, $new); return $new; } else { array_multisort($ref_arr, SORT_STRING, SORT_ASC, $new); return $new; } } /* ========================================================= get_ref_files ========================================================= */ function get_ref_files() { global $dir; $pre_month = date('Ym', strtotime('-1 month')); $now_month = date('Ym', time()); $prefix = 'news.scenecritique.com-access_log.'; if (! is_dir($dir)) { die($dir. ' is not directory!'); } $files = array(); $now_month_file = $prefix. $now_month; $pre_month_file = $prefix. $pre_month; $dh = opendir($dir); while ( FALSE !== ($file = readdir($dh)) ) { if ( FALSE !== strpos($file, $now_month_file) ) { array_push($files, $dir.$file); } elseif ( FALSE !== strpos($file, $pre_month_file) ) { array_push($files, $dir.$file); } } closedir($dh); if ( empty($files) ) { die('no data file!'); } return $files; } /* ========================================================= get_ref_files ========================================================= */ function str2time($s) { $month_arr = array( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12', ); preg_match('#^(\d\d)/(\w{3})/(\d{4}):(\d\d):(\d\d):(\d\d)#', $s, $match); $d = $match[1]; $m = $month_arr[ $match[2] ]; $Y = $match[3]; $H = $match[4]; $i = $match[5]; $s = $match[6]; $str = $Y.'-'.$m.'-'.$d.' '.$H.':'.$i.':'.$s; return strtotime($str); } /* ========================================================= header_gzip ========================================================= */ function header_gzip() { // use gzencode ! if ( headers_sent() ) { return FALSE; } if ( ! empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) { if ( preg_match('{((?:x-)?gzip)}i', $_SERVER['HTTP_ACCEPT_ENCODING'], $m) ) { $gzip = $m[1]; header('Content-Encoding: ' . $gzip); return TRUE; } } return FALSE; } ?>