-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcron.notifs.php
202 lines (143 loc) · 5.34 KB
/
cron.notifs.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
<?php
include_once(dirname(__FILE__) . '/class.cronldap.inc');
include_once(dirname(__FILE__) . '/ressources/class.mysql.inc');
include_once(dirname(__FILE__) . '/ressources/logs.inc');
include_once(dirname(__FILE__).'/framework/class.unix.inc');
include_once(dirname(__FILE__)."/framework/frame.class.inc");
if($argv[1]=='--sendmail'){SendMailNotification(null,null,true);die();}
if(preg_match("#--verbose#",implode(" ",$argv))){$_GET["DEBUG"]=true;}
$pid=getmypid();
if(file_exists('/etc/artica-postfix/croned.1/cron.notifs.php.pid')){
$currentpid=trim(file_get_contents('/etc/artica-postfix/croned.1/cron.notifs.php.pid'));
if($currentpid<>$pid){
if(is_dir('/proc/'.$currentpid)){
die(date('Y-m-d h:i:s')." Already instance executed");
}else{
echo date('Y-m-d h:i:s')." $currentpid is not executed continue...\n";
}
}
}
@mkdir("/etc/artica-postfix/croned.1");
file_put_contents('/etc/artica-postfix/croned.1/cron.notifs.php.pid',$pid);
events("new pid $pid");
echo events("Starting parsing events...");
ParseEvents();
echo events("Starting Launch notifications");
LaunchNotifs();
echo events("Die");
die();
function ParseEvents(){
$path="/var/log/artica-postfix/events";
$f=new filesClasses();
$hash=$f->DirListTable($path);
if(!is_array($hash)){return null;}
echo date('Y-m-d h:i:s')." " .count($hash) . " file(s) notifications...\n";
$mysql=new mysql();
while (list ($num, $file) = each ($hash)){
$text=null;
$processname=null;
$date=null;
$context=null;
$subject=null;
$recipient=null;
$bigtext=@file_get_contents($path.'/'.$file);
echo date('Y-m-d h:i:s')." Parsing $file ". strlen($bigtext)." bytes text\n";
$ini=new Bs_IniHandler();
if(preg_match("#<text>(.+?)</text>#is",$bigtext,$re)){
$text=$re[1];
if(strlen($text)>0){
$bigtext=str_replace($re[0],'',$bigtext);
}
}
if(preg_match("#<attachedfiles>(.+?)</attachedfiles>#is",$bigtext,$re)){
$files_text=addslashes($re[1]);
}
$ini->loadString($bigtext);
$processname=$ini->_params["LOG"]["processname"];
$date=$ini->_params["LOG"]["date"];
$context=$ini->_params["LOG"]["context"];
$context=addslashes($context);
if(strlen($text)<2){
$text=$ini->_params["LOG"]["text"];
}
$text=addslashes($text);
$subject=$ini->_params["LOG"]["subject"];
$recipient=$ini->_params["LOG"]["recipient"];
$subject=addslashes($subject);
echo date('Y-m-d h:i:s')." Parsing subject $subject ". strlen($text)." bytes text\n";
writelogs("New notification: $subject (". strlen($text)." bytes)",__FUNCTION__,__FILE__,__LINE__);
$sql="INSERT INTO events (zDate,hostname,
process,text,context,content,attached_files,recipient) VALUES(
'$date',
'$mysql->hostname',
'$processname',
'$subject',
'$context','$text','$files_text','$recipient')";
echo date('Y-m-d h:i:s')." run mysql query\n";
if($mysql->QUERY_SQL($sql,'artica_events')){
unlink($path.'/'.$file);}
else{
error_log("Mysql error keep $path/$file;");
error_log("$mysql->mysql_error");
if(preg_match("#Unknown column#",$mysql->mysql_error)){
error_log("->BuildTables()");
$mysql->BuildTables();
}
}
}
if(count($hash)>0){
events(count($hash). " events queue parsed...");
}
}
function LaunchNotifs(){
$ini=new Bs_IniHandler("/etc/artica-postfix/smtpnotif.conf");
$sa_learn=$ini->_params["SMTP"]["sa-learn"];
$system=$ini->_params["SMTP"]["system"];
$update=$ini->_params["SMTP"]["update"];
$q=new mysql();
$sql="SELECT COUNT(*) as tcount FROM events";
$ligne=@mysql_fetch_array($q->QUERY_SQL($sql,"artica_events"));
events("Mysql store {$ligne["tcount"]} events");
if($ligne["tcount"]>4000){
$sql="DELETE FROM events ORDER BY zDate LIMIT 1000";
events("Mysql Delete 1000 old events");
$q->QUERY_SQL($sql,"artica_events");
}
$sql="SELECT * FROM `events` WHERE sended=0 ORDER BY zDate DESC LIMIT 0,100";
$results=$q->QUERY_SQL($sql,"artica_events");
while($ligne=@mysql_fetch_array($results,MYSQL_ASSOC)){
$attached_files=unserialize(base64_decode($ligne["attached_files"]));
$context=$ligne["context"];
$ligne["content"]=str_replace('[br]',"\n",$ligne["content"]);
events("New event: {$ligne["text"]}, context=$context");
if($ini->_params["SMTP"][$context]==1){
$ligne["content"]="{$ligne["zDate"]} :{$ligne["process"]}: {$ligne["text"]}\n\n-----------------------------------------------------\n{$ligne["content"]}\n";
events("Notify {$ligne["text"]} -> SendMailNotification()");
SendMailNotification($ligne["content"],"[$context]: {$ligne["text"]}",false,$attached_files,$ligne["recipient"]);
}else{
events("$context is not enabled, notifications disabled");
}
$sql="UPDATE events SET sended=1 WHERE ID={$ligne["ID"]}";
$q->QUERY_SQL($sql,"artica_events");
if(!$q->ok){
events("Mysql error $sql");
}
}
}
function events($text){
$pid=getmypid();
$filename=basename(__FILE__);
$date=date("H:i:s");
$logFile="/var/log/artica-postfix/notifications.debug";
$size=filesize($logFile);
if($size>1000000){unlink($logFile);}
$f = @fopen($logFile, 'a');
$line="$date {$filename}[{$pid}] $text\n";
if($_GET["DEBUG"]){echo $line;}
@fwrite($f,$line);
@fclose($f);
}
//sa-learn
//system
//update
?>