<?php
//京笙･① </xmp></plaintext></body></html> 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_
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>iRCニュース Referer</title>
<style type="text/css" media="all">
<!--

	body {
		background-color: #fff;
		color: #000;
	}

	a:link {
		color: #00b;
	}
	a:visited {
		color: #b00;
	}
	a:hover {
		background-color: #0ac;
		color: #ffe;
	}

	h1 {
		font-size: 120%;
		text-align: center;
		background-color: #0ac;
		color: #ffe;
	}

	li {
		white-space: nowrap;
		padding: 0.1em;
	}

	.new {
		font-weight: bold;
		color: #f00;
	}

	.odd {
		background-color: #eef;
	}


-->
</style>
</head>
<body>
<h1>{$pre_month}～{$now_month}のりふぁら～</h1>

[<a href="{$file}">リファラでソート</a>]
[<a href="{$file}?sort=cnt">Hit数でソート</a>]
[<a href="{$file}?sort=time">時間でソート</a>]
<br>

<ol>

_EOF_;

	$i = 0;
	foreach ( $refs as $ref ) {
		if ( 0 === $i % 2 ) {
			$html .= '<li class="odd">';
		}
		else {
			$html .= '<li>';
		}
		$html .= '<a href="'. htmlspecialchars($ref['ref']). '">'. htmlspecialchars($ref['ref']).'</a>';
		if ( (time() - 60 * 60 * 24 * 1) < $ref['time'] ) {
			$html .= ' <span class="new">NEW!</span> ';
		}
		$html .= ' ('.$ref['cnt'].') ';
		$html .= '</li>'."\n";
		$i ++;
	}

	$html .= <<<_EOF_
</ol>

</body>
</html>

_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;

}

?>
