<?php
$link = mysqli_connect('localhost', 'root', '');
mysqli_select_db($link, 'laravel');
$logfile = [
'access_log.20220801','access_log.20220802','access_log.20220803'
];
for($i=0; $i<count($logfile); $i++){
echo date("Y-m-d H:i:s"). " $logfile[$i]\n";
if(($fp = fopen($logfile[$i], "r"))){
while(!feof($fp)){
$buffer = fgets($fp);
if(!empty($buffer)){
/*
* $info[0]: IPアドレス
* $info[1]: アクセス時間
* $info[2]: アクセスメソッド
* $info[3]: アクセスパス
*/
$info = explode(",", preg_replace('/^([\S]+)[^\[]+\[([^\]]+)\]\s+"(\S+)\s(\S+).*/', "$1,$2,$3,$4,", $buffer));
$date = date("Y-m-d H:i:s", strtotime($info[1])); // unix時間に変換してフォーマット
$method = $info[2];
$path = $info[3];
$sql = '';
if($method == 'PUT' && preg_match("/\/api\/building\/(\d{8})/", $path, $b_code)){
// echo "$date $method $path $b_code[1]\n";
$sql = "INSERT INTO `building_floor_history_logs` (`b_code`, `f_code`, `history_message`, `history_message_kind`, `check_date`) VALUES ('$b_code[1]', NULL, 'ビル情報を更新しました。', 100, '$date');\n";
}
if(!empty($sql)){
mysqli_query($link, $sql);
}
}
}
fclose($fp);
}
}
mysqli_close($link);