forked from dtouzeau/artica
/
exec.fetmaillog.php
106 lines (90 loc) · 3.12 KB
/
exec.fetmaillog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
include_once(dirname(__FILE__).'/ressources/class.templates.inc');
include_once(dirname(__FILE__).'/ressources/class.ini.inc');
include_once(dirname(__FILE__).'/ressources/class.users.menus.inc');
include_once(dirname(__FILE__)."/framework/frame.class.inc");
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
include_once(dirname(__FILE__).'/framework/class.unix.inc');
if(!Build_pid_func(__FILE__,"MAIN")){
writelogs(basename(__FILE__).":Already executed.. aborting the process",basename(__FILE__),__FILE__,__LINE__);
die();
}
$pid=getmypid();
$pidfile="/etc/artica-postfix/".basename(__FILE__).".pid";
echo "save $pid in file $pidfile\n";
file_put_contents($pidfile,$pid);
$users=new usersMenus();
$_GET["server"]=$users->hostname;
$pipe = fopen("php://stdin", "r");
while(!feof($pipe)){
$buffer .= fgets($pipe, 4096);
Parseline($buffer);
$buffer=null;
}
fclose($pipe);
function Parseline($buffer){
if(preg_match("#ignor.+?non.+?limin.+?#",$buffer)){return null;}
if(preg_match("#connection to (.+?):(.+?)\s+\[(.+?)\] failed: Connection timed out.#",$buffer,$re)){
$server=$re[1];
$port=$re[2];
$ip=$re[2];
fetchtimeout($server,$port,$ip,$buffer);
return null;
}
if(preg_match("#reading message (.+?)@(.+?)@(.+?):.+?\(([0-9]+).+?\(([0-9]+)#",$buffer,$re)){
$user="{$re[1]}@{$re[2]}";
$server=$re[3];
$octets=$re[4]+$re[5];
AddFetchEv($user,$server,$octets);
return null;
}
if(preg_match("#lecture du message (.+?)@(.+?)@(.+?):.+?\(([0-9]+).+?\(([0-9]+)#",$buffer,$re)){
$user="{$re[1]}@{$re[2]}";
$server=$re[3];
$octets=$re[4]+$re[5];
AddFetchEv($user,$server,$octets);
return null;
}
if(preg_match("#lecture du message\s+(.+?)@(.+?):.+\(([0-9]+).+?limin.+?#",$buffer)){
$user=$re[1];
$server=$re[2];
$octets=$re[3];
AddFetchEv($user,$server,$octets);
return null;
}
if(preg_match("#reading message\s+(.+?)@(.+?):.+\(([0-9]+).+?flushed#",$buffer)){
$user=$re[1];
$server=$re[2];
$octets=$re[3];
AddFetchEv($user,$server,$octets);
return null;
}
fetchevents("Not Filtered:\"".trim($buffer)."\"");
}
function fetchtimeout($server,$port,$ip,$buffer){
$file="/etc/artica-postfix/cron.1/".md5(__FILE__)."-".md5("$server,$port,$ip");
if(file_time_min($file)<15){return null;}
send_email_events("fetchmail network error on $server $port","fetchmail claim \"$buffer\", please set the right server for fetching messages",'system');
THREAD_COMMAND_SET("/etc/init.d/artica-postfix restart postfix");
@unlink($file);
@file_put_contents("#",$file);
}
function AddFetchEv($user,$server,$octets){
@mkdir("/var/log/artica-postfix/fetchmail",0755,true);
$date=date('Y-m-d H:i:s');
$md5=md5("$user,$server,$octets,$date");
$file="/var/log/artica-postfix/fetchmail/$md5.sql";
$sql="INSERT INTO fetchmail_events (zDate,server,account,size) VALUES('$date','$server','$user','$octets')";
@file_put_contents($file,$sql);
}
function fetchevents($text){
$pid=getmypid();
$date=date("H:i:s");
$logFile="/var/log/artica-postfix/fetchmail-logger.debug";
$size=@filesize($logFile);
if($size>1000000){@unlink($logFile);}
$f = @fopen($logFile, 'a');
@fwrite($f, "[$pid] fetchmail: $date $text\n");
@fclose($f);
}
?>