forked from poppyred/1.6.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.last.100.mails.php
executable file
·371 lines (287 loc) · 11.4 KB
/
exec.last.100.mails.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
include_once(dirname(__FILE__) . '/ressources/class.users.menus.inc');
include_once(dirname(__FILE__) . '/ressources/class.mysql.inc');
include_once(dirname(__FILE__) . '/ressources/class.user.inc');
include_once(dirname(__FILE__) . '/ressources/class.ini.inc');
include_once(dirname(__FILE__) . '/ressources/class.templates.inc');
include_once(dirname(__FILE__) . '/ressources/class.rtmm.tools.inc');
include_once(dirname(__FILE__) . '/ressources/class.system.network.inc');
include_once(dirname(__FILE__).'/ressources/class.os.system.inc');
include_once(dirname(__FILE__)."/framework/class.unix.inc");
include_once(dirname(__FILE__)."/framework/frame.class.inc");
if(preg_match("#--force#",@implode(" ",$argv))){$GLOBALS["FORCE"]=true;}
if(preg_match("#--verbose#",@implode(" ",$argv))){$GLOBALS["VERBOSE"]=true;}
RTMevents("start realtime monitor");
include_once(dirname(__FILE__).'/framework/class.unix.inc');
$sock=new sockets();
$EnableArticaSMTPStatistics=$sock->GET_INFO("EnableArticaSMTPStatistics");
if(!is_numeric($EnableArticaSMTPStatistics)){$EnableArticaSMTPStatistics=1;}
if($EnableArticaSMTPStatistics==0){die();}
if(!$GLOBALS["FORCE"]){
if(!Build_pid_func(__FILE__,"MAIN")){
writelogs(basename(__FILE__).":Already executed.. aborting the process",basename(__FILE__),__FILE__,__LINE__);
die();
}
if(system_is_overloaded()){RTMevents("die, overloaded");die();}
@mkdir("/etc/artica-postfix/croned.2",null,true);
$RTMMailSchedule=intval($sock->GET_INFO('RTMMailSchedule'));
if($RTMMailSchedule<1){$RTMMailSchedule=35;}
$timef=file_get_time_min("/etc/artica-postfix/croned.2/".md5(__FILE__));
if($timef<$RTMMailSchedule){
RTMevents("die, {$RTMMailSchedule}mn minimal current {$timef}mn");die();
}
}
RTMevents("current time {$timef}mn");
$array["Content scanner malfunction"]="Content scanner malfunction";
$array["Discard"]="Discard";
$array["DNS Error"]="DNS Error";
$array["Domain not found"]="Domain not found";
$array["Error"]="Error";
$array["Greylisting"]="Greylisting";
$array["hostname not found"]="hostname not found";
$array["RBL"]="RBL";
$array["Relay access denied"]="Relay access denied";
$array["Sended"]="Sended";
$array["SPAM"]="SPAM";
$array["SPAMMY"]="SPAMMY";
$array["Mailbox unknown"]="Mailbox unknown";
$array["User unknown in relay recipient table"]="User unknown in relay recipient table";
$array[null]="{all}";
$ini=new Bs_IniHandler();
if(is_file("/etc/artica-postfix/settings/Daemons/RTMMailConfig")){$ini->loadFile("/etc/artica-postfix/settings/Daemons/RTMMailConfig");}
if($ini->_params["ENGINE"]["LIMIT"]==null){$ini->_params["ENGINE"]["LIMIT"]="100";}
$maxmail=$ini->_params["ENGINE"]["LIMIT"];
$FILTERBY=$ini->_params["ENGINE"]["FILTERBY"];
$_GET["INI"]=$ini->_params;
RTMevents("Building $file filter by $FILTERBY");
switch ($FILTERBY) {
case "SPAM":
$sql="SELECT * FROM smtp_logs WHERE LENGTH(delivery_user)>0 AND SPAM='1' ORDER BY time_connect DESC LIMIT 0,{$ini->_params["ENGINE"]["LIMIT"]}";
$file="last-100-mails-".md5($FILTERBY).".html";
break;
case "SPAMMY":
$sql="SELECT * FROM smtp_logs WHERE LENGTH(delivery_user)>0 AND spammy='1' ORDER BY time_connect DESC LIMIT 0,{$ini->_params["ENGINE"]["LIMIT"]}";
$file="last-100-mails-".md5($FILTERBY).".html";
break;
case "Sended":
$sql="SELECT * FROM smtp_logs WHERE LENGTH(delivery_user)>0 AND bounce_error='$num' AND SPAM=0 AND spammy=0 ORDER BY time_connect DESC LIMIT 0,{$ini->_params["ENGINE"]["LIMIT"]}";
$file="last-100-mails-".md5($FILTERBY).".html";
break;
case null:
$sql="SELECT * FROM smtp_logs WHERE LENGTH(delivery_user)>0 ORDER BY time_connect DESC LIMIT 0,{$ini->_params["ENGINE"]["LIMIT"]}";
$file="last-100-mails.html";
break;
default:
$sql="SELECT * FROM smtp_logs WHERE LENGTH(delivery_user)>0 AND bounce_error='$num' ORDER BY time_connect DESC LIMIT 0,{$ini->_params["ENGINE"]["LIMIT"]}";
$file="last-100-mails-".md5($FILTERBY).".html";
break;
}
BuildHeader($sql,$file,$maxmail,$FILTERBY);
RTMevents("RTMMailConfig OK");
UpdateDiscards();
RTMevents("UpdateDiscards() OK");
UpdateGeoip();
RTMevents("UpdateGeoip() OK");
@unlink("/etc/artica-postfix/croned.2/".md5(__FILE__));
@file_put_contents("/etc/artica-postfix/croned.2/".md5(__FILE__),date('Y-m-d H:i:s'));
die();
function BuildHeader($sql_query,$html_file="last-100-mails.html",$maxmail,$subtitle=null){
$date_start=time();
$q=new mysql();
$results=$q->QUERY_SQL($sql_query,"artica_events");
if(!$q->ok){
RTMevents("Wrong sql query $q->mysql_error");
return null;
}
$count=0;
while($ligne=mysql_fetch_array($results,MYSQL_ASSOC)){
$count=$count+1;
$tr=$tr.format_line($ligne);
}
if($_GET["COUNT_MAILS"]==null){
$sql="SELECT count(*) as tcount FROM smtp_logs";
$ligne=@mysql_fetch_array($q->QUERY_SQL($sql,"artica_events"));
$count_table=$ligne["tcount"];
$count_table=number_format($count_table, 0, ',', ' ');
$_GET["COUNT_MAILS"]=$count_table;
}else{
$count_table=$_GET["COUNT_MAILS"];
}
if($subtitle==null){$subtitle="{all}";}
$date_end=time();
$calculate=distanceOfTimeInWords($date_start,$date_end);
RTMevents("$calculate");
$html="
<table style='width:100%'>
<tr>
<td><H3 style='font-weight:bold;border-bottom:1px solid #CCCCCC;margin-bottom:4px;'>{last_mails} ($maxmail)</H3>
<p class=caption>{filter}:<strong>$subtitle ($calculate)</strong> - $count_table eMails {total}</p>
</td>
<td>
<table style='width:100%'>
<td>
" . postfixlogger_progress()."</td>
</tr>
</table>
</td>
</tr>
</table>
<div style='width:100%;height:300px;overflow:auto'>
<table style='width:100%'>
$tr
</table>
</div>
";
RTMevents("BuildHeader():: Saving usr/share/artica-postfix/ressources/logs/$html_file ($count)");
file_put_contents("/usr/share/artica-postfix/ressources/logs/$html_file",$tr);
file_put_contents("/usr/share/artica-postfix/ressources/logs/" . str_replace("-",".",$html_file),$html);
@chmod("/usr/share/artica-postfix/ressources/logs/$html_file",0755);
@chmod("/usr/share/artica-postfix/ressources/logs/" . str_replace("-",".",$html_file),0755);
}
function postfixlogger_progress(){
$user=new usersMenus();
if(is_file("/usr/share/artica-postfix/ressources/logs/postfix-logger.ini")){
$ini=new Bs_IniHandler("/usr/share/artica-postfix/ressources/logs/postfix-logger.ini");
}else{
$ini=new Bs_IniHandler();
}
$max=$ini->get("PROGRESS","max");
$current=$ini->get("PROGRESS","current");
$type=$ini->get("PROGRESS","type");
$endtime=$ini->get("PROGRESS","time");
if($current==null){$current=0;}
if($type==null){$type="{scheduled}";}
if($endtime==null){$endtime=date("H:i:s");}
if($max<>null){
$pourc=round($current/$max,2)*100;
}else{
$pourc="0";
}
$queue=$user->POSTFIX_QUEUE["RTM"];
$endtime=str_replace("Y-m-d","",$endtime);
$color="#5DD13D";
$html="
<table style='width:100%'>
<tr>
<td valign='top'>
<table style='width:100%'>
<tr>
<td align='center' colspan=2><strong style='font-size:11px'>«$type» $current/$max/$queue ({end}:$endtime) </td>
</tr>
<tr>
<td width=1%><span id='wait'>$img</span>
</td>
<td width=99%>
<table style='width:100%'>
<tr>
<td>
<div style='width:100%;background-color:white;padding-left:0px;border:1px solid $color'>
<div id='progression_postfix'>
<div style='width:{$pourc}%;text-align:center;color:white;padding-top:3px;padding-bottom:3px;background-color:$color'>
<strong style='color:#BCF3D6;font-size:12px;font-weight:bold'>{$pourc}%</strong></center>
</div>
</div>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td valign='top' width=1%>
<div style='padding:2px;border:1px solid #CCCCCC'>" . imgtootltip("rouage-32.png","{parameters}","Loadjs('RTMMailConfig.php')")."</div>
</td>
<td valign='top' width=1%>
<div style='padding:2px;border:1px solid #CCCCCC'>" . imgtootltip("loupe-32.png","{detach}","Loadjs('RTMMail.php')")."</div>
</td>
</tr>
</table>";
return $html;
}
function UpdateDiscards(){
$q=new mysql();
$sql="UPDATE smtp_logs SET bounce_error='Error' WHERE bounce_error LIKE '%4.7.1 Service unavailable%'";
$q->QUERY_SQL($sql,"artica_events");
$sql="UPDATE smtp_logs SET bounce_error='Error' WHERE bounce_error LIKE '%4.5.0 Failure%'";
$q->QUERY_SQL($sql,"artica_events");
$sql="UPDATE smtp_logs SET bounce_error='Content scanner malfunction' WHERE bounce_error LIKE '%4.6.0 Content scanner malfunction%'";
$q->QUERY_SQL($sql,"artica_events");
$sql="UPDATE smtp_logs SET bounce_error='Sended' WHERE LENGTH(bounce_error)=0";
$q->QUERY_SQL($sql,"artica_events");
$sql="UPDATE smtp_logs SET bounce_error='Mailbox unknown' WHERE bounce_error LIKE '%mailbox unavailable%'";
$q->QUERY_SQL($sql,"artica_events");
$sql="UPDATE smtp_logs SET bounce_error='DNS Error' WHERE bounce_error LIKE '%delivery temporarily suspended: Host or domain name not found%'";
$q->QUERY_SQL($sql,"artica_events");
}
function RTMevents($text){
$f=new debuglogs();
$f->events(basename(__FILE__)." $text","/var/log/artica-postfix/artica-status.debug");
}
function UpdateGeoip(){
$database="/usr/share/GeoIP/GeoIP.dat";
if(!is_file($database)){
RTMevents("Unable to stat /usr/share/GeoIP/GeoIP.dat");
installgeoip();
return null;
}
if(!is_file("/usr/local/share/GeoIP/GeoIPCity.dat")){
if(is_file("/usr/local/share/GeoIP/GeoLiteCity.dat")){
RTMevents("Linking /usr/local/share/GeoIP/GeoLiteCity.dat");
system('/bin/ln -s /usr/local/share/GeoIP/GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat >/dev/null 2>&1');
}
}
if(!is_file("/usr/share/GeoIP/GeoIPCity.dat")){
if(is_file("/usr/share/GeoIP/GeoLiteCity.dat")){
RTMevents("Linking /usr/share/GeoIP/GeoLiteCity.dat");
system('/bin/ln -s /usr/share/GeoIP/GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat >/dev/null 2>&1');
}
}
if(!function_exists("geoip_record_by_name")){
RTMevents("Unable to sat geoip_record_by_name() function");
installgeoip();
return null;
}
$db_info=geoip_database_info(GEOIP_COUNTRY_EDITION);
RTMevents("Using $db_info");
$q=new mysql();
$sql="SELECT smtp_sender FROM `smtp_logs` WHERE LENGTH(smtp_sender)>0 AND smtp_sender!='127.0.0.1' AND (Country IS NULL or Country='undefined')";
$results=$q->QUERY_SQL($sql,"artica_events");
if(!$q->ok){
RTMevents("Wrong sql query $q->mysql_error");
return null;
}
while($ligne=mysql_fetch_array($results,MYSQL_ASSOC)){
$smtp_sender=$ligne["smtp_sender"];
if($_GET["smtp_cache"][$smtp_sender]){continue;}
$record = geoip_record_by_name($smtp_sender);
if (!$record) {
RTMevents("unable to locate this IP $smtp_sender");
$_GET["smtp_cache"][$smtp_sender]=true;
continue;
}
$Country=$record["country_name"];
$_GET["smtp_cache"][$smtp_sender]=true;
RTMevents("$smtp_sender = $Country");
updategeo($smtp_sender,$Country);
}
}
function updategeo($ip,$country){
$sql="UPDATE smtp_logs SET Country='$country' WHERE smtp_sender='$ip'";
$q=new mysql();
$q->QUERY_SQL($sql,"artica_events");
}
function installgeoip(){
$unix=new unix();
if(is_file("/etc/artica-postfix/FROM_ISO")){$time=$unix->file_time_min("/etc/artica-postfix/FROM_ISO");if($time<60){return;}}
if(is_file('/usr/bin/pecl')){
if(!is_file("/etc/artica-postfix/php-geoip-checked")){
system('/usr/share/artica-postfix/bin/setup-ubuntu --check-base-system');
system('/usr/bin/pecl install geoip');
system('/etc/init.d/artica-postfix restart apache');
system('/bin/touch /etc/artica-postfix/php-geoip-checked');
}
}
}
?>