#freeze
**スクリプト [#qf377e91]
#contents
----
***URLデコード [#abcd3be1]
暫定版。~
URLデコードした状態で解析すると、どうもURL長いよ警告が出ているらしく~
Wikiなどの長いURLの解析をスキップしている様子。~
そこで、UTF-8以外の検索文字列を(今のところキメ打ちで)~
UTF-8に直した上でURLエンコードし、再度WebalizerにURLデコードさせる。~
ついでにごにょごにょと。
#!/usr/local/bin/php
<?php
$infile = fopen('/tmp/access_log',"r");
$outfile = fopen('/tmp/access_log.tmp',"w");
//検索文字列(Webalizer定義)
//Shift_JISで化けた文字用にチルダ入れる
$query= '/(query|q|keyword|p|MT|search|qr|text)=([\w\d%+\-\.\~]*)/';
//リファラ
$ref = '/\[.*?\] ".*?" [0-9]*? [0-9]*? "(.*?)"/';
//クエリ内ハッシュ
$hash = '/[\?&](q=.*#)/';
//Googleキャッシュ
$cache = '/[\?&]q=(cache:[\w\d\-]{12}:.*?\+)/';
if( $infile ) {
while( !feof( $infile ) ) {
$line = fgets( $infile );
if ( !empty($line) ) {
//GoogleSuggest機能?のハッシュ文字対策
if( preg_match($hash, $line, $hash_matches) != 0 ) {
$line = str_replace($hash_matches[1], '', $line);
}
//Googleキャッシュの検索文字列
if( preg_match($cache, $line, $cache_matches) != 0) {
$line = str_replace($cache_matches[1], '', $line);
}
//EzWebのSJISのURLエンコードをUTF-8のURLエンコードに変換
if( strpos($line, '//ezsch.ezweb.ne.jp/search/') !== FALSE ) {
preg_match($ref, $line, $ref_matches);
preg_match($query, $ref_matches[1], $matches);
$q_str = urlencode( mb_convert_encoding( urldecode( $matches[2] ), "utf-8", "auto"));
$line = str_replace($matches[2], $q_str, $line);
}
//InputEncodeがUTF-8以外のURLエンコード文字列をUTF-8のURLエンコードに変換
//Googleのケータイ用とLunascapeとgoo用。他にあれば追加
if( stripos($line, 'IE=Shift_JIS' ) !== FALSE ||
stripos($line, 'IE=SJIS' ) !== FALSE ||
stripos($line, 'IE=EUC-JP' ) !== FALSE ) {
preg_match($ref, $line, $ref_matches);
preg_match( $query, $ref_matches[1], $matches );
$q_str = urlencode( mb_convert_encoding( urldecode( $matches[2] ), "utf-8", "auto"));
$line = str_replace($matches[2], $q_str, $line);
}
// fwrite($outfile, mb_convert_encoding( urldecode( $line ), "utf-8", "auto"));
// print ( mb_convert_encoding( urldecode( $line ), "utf-8", "auto" ));
//「%」が二重にエンコードされてる謎を直す
$line = preg_replace('/%25([0-9A-F]{2})/', "%$1", $line);
//全角空白を半角(+)に直す
$line = str_replace('%E3%80%80', '+', $line);
// print $line;
fwrite($outfile, $line);
}
}
}
?>
***GeoIP DB更新 [#of6cfc83]
毎月初に更新されるので対応
#!/bin/bash
cd /tmp
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
if [ $? == '0' ]; then
gunzip GeoIP.dat.gz
fi
mv -f GeoIP.dat /usr/local/share/GeoIP/
crontabに登録
0 0 3 * * sh /usr/local/src/GeoIPDBUpdate.sh
マージン取って毎月3日に更新してみる
***URLデコード(旧) [#n77b6c87]
暫定版~
URLエンコードされている検索文字列を~
GoogleCacheでの検索文字列も拾って~
UTF-8に変換する~
変換したあとにwebalizerで解析する
#!/usr/local/bin/php
<?php
$infile = fopen('/tmp/access_log',"r");
$outfile = fopen('/tmp/access_log.tmp',"w");
if( $infile ) {
while( !feof( $infile ) ) {
$line = fgets( $infile );
if ( !empty($line) ) {
if( strpos($line,'search?q=cache:') ) {
$line = preg_replace('/cache:.+?\+/','',$line);
};
fwrite($outfile, mb_convert_encoding( urldecode( $line ), "utf-8", "auto"));
// print ( mb_convert_encoding( urldecode( $line ), "utf-8", "auto" ));
}
}
}
?>