require( "fnc_allow.php" );
//カウントディレクトリ
$dir = "cntdir";
if ( !is_dir( "$dir" ) ) {
umask( 0 );
mkdir ("$dir", 0777);
}
//設定ファイル読み込み
$setup = file( 'cntsetup.txt' );
//URIの分解
$uri = explode( "/", strtolower( $_SERVER["REQUEST_URI"] ) );
//管理
if ( $uri[2] === 'admin' ) {
if ( chkAllow() ) {
dispAdmin( $setup );
exit;
}
else {
exit( "Thank you." );
}
}
//設定ファイルの確認
foreach( $setup as $val ) {
$val = preg_replace( "/\r\n|\r|\n/", "", $val );
list( $id, $url, $outfile ) = explode( ",", $val );
if ( $id === $uri[3] ) {
$logfile = "$dir/$outfile";
break;
}
}
if ( !$logfile ) exit( 'ページ指定に誤りがあります' );
switch( $uri[2] ) {
case 'rd':
redirectURL( "$logfile", $url );
break;
case 'dl':
if ( chkAllow() ) {
downloadFile( "$logfile", "$id.csv" );
}
else {
exit( "Thank you." );
}
break;
case 'total':
if ( chkAllow() ) {
totalCalc( "$logfile" );
foreach( $cntmon as $key => $val ) {
print "$key : $val
";
}
}
else {
exit( "Thank you." );
}
break;
case 'daytotal':
if ( chkAllow() ) {
totalCalc( "$logfile", $uri[4] );
foreach( $cntday as $key => $val ) {
print "$key : $val
";
}
}
else {
exit( "Thank you." );
}
break;
case 'dltotal':
if ( chkAllow() ) {
$cnt = totalAll( "$logfile" );
foreach( $cnt as $key => $val ) {
$out .= "$key,$val\n";
}
$file = fopen( "tmp.csv", "w" ) or die( '書き込みエラー
' );
flock( $file, LOCK_EX );
fputs( $file, $out );
flock( $file, LOCK_UN );
fclose( $file );
downloadFile( "tmp.csv", "$id.csv" );
}
else {
exit( "Thank you." );
}
break;
default:
exit( '指定に誤りがあります' );
break;
}
function dispAdmin( $setup ) {
/*
if ( $handle = opendir( $aDir ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( $file != "." && $file != ".." ) {
$option .= "\n";
}
}
closedir($handle);
}
*/
foreach( $setup as $val ) {
$val = preg_replace( "/\r\n|\r|\n/", "", $val );
if ( !preg_match( "/^#/", $val ) ) {
list( $id, $url, $outfile ) = explode( ",", $val );
$option .= "\n";
}
}
$host = $_SERVER['HTTP_HOST'];
print <<