function ParseFile($filename, $instancename = null) { events("Parsing {$filename}, instance={$instancename}"); $f = explode("\n", @file_get_contents($filename)); $q = new mysql_postfix_builder(); @mkdir("/var/log/artica-postfix/Postfix-sql-error", 0755, true); while (list($num, $line) = each($f)) { if (trim($line) == null) { continue; } usleep(500); if (preg_match("#(.+?)\\s+([0-9\\:]+)\\s+(.+?)\\s+(.+?)\\s+(.+)\\s+(.*?)\\s+SMTP\\s+-\\s+([0-9]+)\\s+([0-9]+)#i", $line, $re)) { while (list($num, $line) = each($re)) { $line = str_replace("'", "", $line); $line = mysql_escape_string2($line); $re[$num] = $line; } $md5 = md5(@implode("", $re)); $zDate = "{$re[1]} {$re[2]}"; $time = strtotime($zDate); $year = date("Y", $time); if ($year != date("Y")) { $zDate = date("Y") . "-" . date("m-d", $time) . " " . date("H:i:s", $time); $time = strtotime($zDate); } $month = date("m", $time); if ($month != date("m")) { $zDate = date("Y") . "-" . date("m") . "-" . date("d", $time) . " " . date("H:i:s", $time); $time = strtotime($zDate); } $table_hour = date("YmdH", $time) . "_hour"; if ($re[3] == "<>") { $re[3] = "unknown"; } if ($re[4] == "<>") { $re[4] = "unknown"; } if ($GLOBALS["VERBOSE"]) { echo "{$table_hour}:{$time} From:{$re[3]} to {$re[4]}\n"; } $from = $re[3]; $to = $re[4]; $from_domain = null; $to_domain = null; if (strpos($from, "@") > 0) { $tb = explode("@", $from); $from_domain = $tb[1]; } if (strpos($to, "@") > 0) { $tb = explode("@", $to); $to_domain = $tb[1]; } $hour = date("H", $time); $senderHost = $re[5]; $recipientHost = $re[6]; $SMTPCode = $re[7]; $MailSize = $re[8]; $from = strtolower(str_replace("'", "", $from)); $to = strtolower(str_replace("'", "", $to)); $from_domain = strtolower(str_replace("'", "", $from_domain)); $to_domain = strtolower(str_replace("'", "", $to_domain)); $SQL_ARRAY[$table_hour][] = "('{$md5}','{$zDate}','{$hour}','{$from}','{$to}','{$from_domain}','{$to_domain}','{$senderHost}','{$recipientHost}','{$MailSize}','{$SMTPCode}','{$instancename}')"; if (count($SQL_ARRAY[$table_hour]) > 5000) { if (!$q->BuildHourTable($table_hour)) { echo "Unable to build table {$table_hour} {$q->mysql_error}\n"; return; } $sql = "INSERT IGNORE INTO `{$table_hour}` (zmd5,ztime,zhour,mailfrom,mailto,domainfrom,domainto,senderhost,recipienthost,mailsize,smtpcode,instancename) VALUES " . @implode(",", $SQL_ARRAY[$table_hour]); if (!$q->QUERY_SQL($sql)) { echo $q->mysql_error . "\n"; @file_put_contents("/var/log/artica-postfix/Postfix-sql-error/" . md5($sql), $sql); return; } $SQL_ARRAY[$table_hour] = array(); } continue; } if (preg_match("#(.+?)\\s+([0-9\\:]+)\\s+(.+?)\\s+(.+?)\\s+(.+?)\\s+(.*?)\\s+SMTP\\s+-\\s+([0-9]+)\\s+\\?#i", $line, $re)) { while (list($num, $line) = each($re)) { $line = str_replace("'", "", $line); $line = mysql_escape_string2($line); $re[$num] = $line; } $md5 = md5(@implode("", $re)); $zDate = "{$re[1]} {$re[2]}"; $time = strtotime($zDate); $year = date("Y", $time); if ($year != date("Y")) { $zDate = date("Y") . "-" . date("m-d", $time) . " " . date("H:i:s", $time); $time = strtotime($zDate); } $month = date("m", $time); if ($month != date("m")) { $zDate = date("Y") . "-" . date("m") . "-" . date("d", $time) . " " . date("H:i:s", $time); $time = strtotime($zDate); } $table_hour = date("YmdH", $time) . "_hour"; $hour = date("H", $time); $from = $re[3]; $tb = explode("@", $from); $from_domain = $tb[1]; $to = $re[4]; $tb = explode("@", $to); $to_domain = $tb[1]; $senderHost = $re[5]; $SMTPCode = $re[7]; $recipientHost = null; $from = strtolower(str_replace("'", "", $from)); $to = strtolower(str_replace("'", "", $to)); $from_domain = strtolower(str_replace("'", "", $from_domain)); $to_domain = strtolower(str_replace("'", "", $to_domain)); $MailSize = 0; $SQL_ARRAY[$table_hour][] = "('{$md5}','{$zDate}','{$hour}','{$from}','{$to}','{$from_domain}','{$to_domain}','{$senderHost}','{$recipientHost}','{$MailSize}','{$SMTPCode}','{$instancename}')"; if (count($SQL_ARRAY[$table_hour]) > 5000) { if (!$q->BuildHourTable($table_hour)) { echo "Unable to build table {$table_hour} {$q->mysql_error}\n"; return; } $sql = "INSERT IGNORE INTO `{$table_hour}` (zmd5,ztime,zhour,mailfrom,mailto,domainfrom,domainto,senderhost,recipienthost,mailsize,smtpcode,instancename) VALUES " . @implode(",", $SQL_ARRAY[$table_hour]); if (!$q->QUERY_SQL($sql)) { events($q->mysql_error); @file_put_contents("/var/log/artica-postfix/Postfix-sql-error/" . md5($sql), $sql); return; } $SQL_ARRAY[$table_hour] = array(); } continue; } } while (list($table_hour, $s) = each($SQL_ARRAY)) { if (count($s) > 0) { if (!$q->BuildHourTable($table_hour)) { events("Unable to build table {$table_hour} {$q->mysql_error}"); return; } $count = $q->COUNT_ROWS($table_hour); $sql = "INSERT IGNORE INTO `{$table_hour}` (zmd5,ztime,zhour,mailfrom,mailto,domainfrom,domainto,senderhost,recipienthost,mailsize,smtpcode,instancename) VALUES " . @implode(",", $s); if (!$q->QUERY_SQL($sql)) { @file_put_contents("/var/log/artica-postfix/Postfix-sql-error/" . md5($sql), $sql); events($q->mysql_error); return; } $count2 = $q->COUNT_ROWS($table_hour); $Allelements = count($s); $AddedElements = $count2 - $count; events("{$table_hour} {$Allelements} elements ({$AddedElements} new added elements)"); } } events("removing {$filename}"); @unlink($filename); }