1 |
torben |
2125 |
#!/usr/bin/php |
2 |
|
|
<?php |
3 |
|
|
|
4 |
|
|
require("/var/www/inc/mysql.php"); |
5 |
|
|
|
6 |
|
|
function parsefile($file) { |
7 |
|
|
$content = file_get_contents( $file ); |
8 |
|
|
$tmp = explode( "\n\n", $content, 2); |
9 |
|
|
|
10 |
|
|
$headers = $tmp[0]; |
11 |
|
|
$body = trim( $tmp[1] ); |
12 |
|
|
|
13 |
|
|
$ret = array(); |
14 |
|
|
$ret["body"] = $body; |
15 |
|
|
$ret["header"] = array(); |
16 |
|
|
|
17 |
|
|
$tmp = explode("\n", $headers); |
18 |
|
|
foreach($tmp as $line) { |
19 |
|
|
$line = trim($line); |
20 |
|
|
if ($line == "") |
21 |
|
|
continue; |
22 |
|
|
|
23 |
|
|
$parts = explode(":", $line, 2); |
24 |
|
|
$key = trim( $parts[0] ); |
25 |
|
|
$val = trim( $parts[1]); |
26 |
|
|
|
27 |
|
|
$ret["header"][$key] = $val; |
28 |
|
|
} |
29 |
|
|
return $ret; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
$action = $argv[1]; |
35 |
|
|
$file = $argv[2]; |
36 |
|
|
|
37 |
|
|
$ret = parsefile($file); |
38 |
|
|
|
39 |
|
|
$sql = "INSERT INTO log (action,file,actiontime) values ('{$action}', '{$file}', now() )"; |
40 |
|
|
echo $sql; |
41 |
|
|
squery($sql); |
42 |
|
|
|
43 |
|
|
if ($action == "RECEIVED") { |
44 |
|
|
$body = mysql_real_escape_string($ret["body"] ); |
45 |
|
|
$from = $ret["header"]["From"]; |
46 |
|
|
$recv = $ret["header"]["Received"]; |
47 |
|
|
$sql = "insert into inbox (sender, receivetime, body) values ('{$from}', '{$recv}', '{$body}') "; |
48 |
|
|
squery($sql); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if ($action == "SENT" || $action == "FAILED") { |
52 |
|
|
$filename = pathinfo($file, PATHINFO_FILENAME); //file name without extension |
53 |
|
|
$parts = explode("_", $filename); |
54 |
|
|
$id = $parts[1]; |
55 |
|
|
|
56 |
|
|
$sql = "UPDATE outbox SET status='$action', statustime=now() WHERE id=$id"; |
57 |
|
|
squery($sql); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
?> |