@@ -56,6 +56,10 @@ discard block |
||
56 | 56 | return $head; |
57 | 57 | } |
58 | 58 | |
59 | +/** |
|
60 | + * @param null|string $prob |
|
61 | + * @param null|string $conf |
|
62 | + */ |
|
59 | 63 | function dspamLevel($prob, $conf) { |
60 | 64 | /* Calculate DSPAM Level as the Spamassassin Plugin */ |
61 | 65 | if (is_null($prob) or is_null($conf)) return '-'; |
@@ -63,6 +67,9 @@ discard block |
||
63 | 67 | return round(($t_prob + ($conf*100)) / 2); |
64 | 68 | } |
65 | 69 | |
70 | +/** |
|
71 | + * @param string $classSpam |
|
72 | + */ |
|
66 | 73 | function dspamType($classSpam) { |
67 | 74 | switch($classSpam) { |
68 | 75 | case 'HAM': |
@@ -3,13 +3,13 @@ discard block |
||
3 | 3 | function imapFolder($cf, $username) { |
4 | 4 | $return = array(); |
5 | 5 | $open='{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'; |
6 | - $m_mail = imap_open($open, $username, $cf['authpassword'], OP_READONLY) |
|
7 | - or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
|
8 | - if ( !$m_mail ) exit(254); |
|
6 | + $m_mail = imap_open($open, $username, $cf['authpassword'], OP_READONLY) |
|
7 | + or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
|
8 | + if ( !$m_mail ) exit(254); |
|
9 | 9 | |
10 | 10 | |
11 | - syslog (LOG_INFO,$cf['user'].': Successfully connected to <'.$cf['mailhost'].'>; Listing folders of account <'.$username.'>...'); |
|
12 | - //get all folder |
|
11 | + syslog (LOG_INFO,$cf['user'].': Successfully connected to <'.$cf['mailhost'].'>; Listing folders of account <'.$username.'>...'); |
|
12 | + //get all folder |
|
13 | 13 | $list = imap_list($m_mail, $open, "*"); |
14 | 14 | imap_close($m_mail); |
15 | 15 | if (is_array($list)) |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | function imapFind ($cf, $username, $folder) { |
24 | 24 | $head=array(); |
25 | 25 | $m_mail = imap_open('{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'.$folder, $username,$cf['authpassword'], OP_READONLY) |
26 | - or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
|
26 | + or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
|
27 | 27 | if ( !$m_mail ) exit(254); |
28 | 28 | |
29 | 29 | |
@@ -36,12 +36,12 @@ discard block |
||
36 | 36 | // Order results starting from newest message |
37 | 37 | if ( empty($m_search) ) { |
38 | 38 | syslog (LOG_INFO,$cf['user'].": No suitable mail found in <$folder> folder."); |
39 | - if ( $ierr = imap_errors() ) |
|
40 | - foreach ( $ierr as $thiserr ) |
|
41 | - syslog (LOG_ERR, $cf['user'].": IMAP Error: $thiserr"); |
|
42 | - if ( $ierr = imap_alerts() ) |
|
43 | - foreach ( $ierr as $thiserr ) |
|
44 | - syslog (LOG_ALERT, $cf['user'].": IMAP Alert: $thiserr"); |
|
39 | + if ( $ierr = imap_errors() ) |
|
40 | + foreach ( $ierr as $thiserr ) |
|
41 | + syslog (LOG_ERR, $cf['user'].": IMAP Error: $thiserr"); |
|
42 | + if ( $ierr = imap_alerts() ) |
|
43 | + foreach ( $ierr as $thiserr ) |
|
44 | + syslog (LOG_ALERT, $cf['user'].": IMAP Alert: $thiserr"); |
|
45 | 45 | imap_close( $m_mail ); |
46 | 46 | return FALSE; |
47 | 47 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | syslog (LOG_INFO,$cf['user'].": Found $nmes mail in <$folder> folder."); |
50 | 50 | if ($nmes>0) rsort($m_search); |
51 | 51 | |
52 | - // loop for each message |
|
52 | + // loop for each message |
|
53 | 53 | foreach ($m_search as $onem) |
54 | 54 | $head[] = imap_fetchheader($m_mail, $onem ); |
55 | 55 | imap_close($m_mail); |
@@ -80,52 +80,52 @@ discard block |
||
80 | 80 | /* Each line must end with /r/n */ |
81 | 81 | |
82 | 82 | $result = array( |
83 | - 'date' => NULL, |
|
84 | - 'from' => NULL, |
|
85 | - 'messageid' => NULL, |
|
83 | + 'date' => NULL, |
|
84 | + 'from' => NULL, |
|
85 | + 'messageid' => NULL, |
|
86 | 86 | 'dmarc' => array( |
87 | 87 | 'result' => NULL, |
88 | 88 | 'dom' => NULL |
89 | 89 | ), |
90 | 90 | 'spf' => array( |
91 | - 'result' => NULL, |
|
92 | - 'dom' => NULL |
|
93 | - ), |
|
91 | + 'result' => NULL, |
|
92 | + 'dom' => NULL |
|
93 | + ), |
|
94 | 94 | 'dkim' => array( |
95 | - 'result' => NULL, |
|
96 | - 'dom' => NULL |
|
97 | - ), |
|
98 | - 'spam' => array( |
|
99 | - 'status' => NULL, |
|
100 | - 'score' => NULL, |
|
101 | - 'th' => NULL, |
|
102 | - ), |
|
103 | - 'dspam' => array( |
|
104 | - 'type' => NULL, |
|
105 | - 'level' => NULL, |
|
95 | + 'result' => NULL, |
|
96 | + 'dom' => NULL |
|
97 | + ), |
|
98 | + 'spam' => array( |
|
99 | + 'status' => NULL, |
|
100 | + 'score' => NULL, |
|
101 | + 'th' => NULL, |
|
102 | + ), |
|
103 | + 'dspam' => array( |
|
104 | + 'type' => NULL, |
|
105 | + 'level' => NULL, |
|
106 | 106 | 'learn' => NULL |
107 | - ), |
|
107 | + ), |
|
108 | 108 | 'warn' => NULL |
109 | - ); |
|
109 | + ); |
|
110 | 110 | |
111 | 111 | |
112 | - if ( preg_match_all ('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dmarc=(?<dmarc>\w+)\s+\(p=\w+\s+dis=\w+\)\s+header\.from=(?<DMARCfrom>[\w\.]+)/m',$header,$received) ) { |
|
112 | + if ( preg_match_all ('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dmarc=(?<dmarc>\w+)\s+\(p=\w+\s+dis=\w+\)\s+header\.from=(?<DMARCfrom>[\w\.]+)/m',$header,$received) ) { |
|
113 | 113 | $k=0; |
114 | - for ($i = count($received[0])-1;$i>=0;$i--) { |
|
115 | - foreach ($ARhosts as $mx) { |
|
114 | + for ($i = count($received[0])-1;$i>=0;$i--) { |
|
115 | + foreach ($ARhosts as $mx) { |
|
116 | 116 | if ($mx == $received['host'][$i]) { |
117 | 117 | /* This is a trusted AR result */ |
118 | 118 | $result['dmarc']['result']=$received['dmarc'][$i]; |
119 | 119 | $result['dmarc']['dom'] = $received['DMARCfrom'][$i]; |
120 | 120 | $k++; |
121 | 121 | } |
122 | - } |
|
122 | + } |
|
123 | + } |
|
123 | 124 | } |
124 | - } |
|
125 | 125 | $received=NULL; |
126 | 126 | if ($k>1) $result['warn'][] = 'The trusted DMARC AR Headers are present more than once. Something wrong.'; |
127 | 127 | |
128 | - if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)spf=(?<spf>\w+)\s+smtp\.(?:mailfrom|helo)=(?<SPFfrom>[\w\.]+)/m',$header,$received) ) { |
|
128 | + if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)spf=(?<spf>\w+)\s+smtp\.(?:mailfrom|helo)=(?<SPFfrom>[\w\.]+)/m',$header,$received) ) { |
|
129 | 129 | $k=0; |
130 | 130 | for ($i = count($received[0])-1;$i>=0;$i--) { |
131 | 131 | foreach ($ARhosts as $mx) { |
@@ -134,25 +134,25 @@ discard block |
||
134 | 134 | $result['spf']['result']=$received['spf'][$i]; |
135 | 135 | $result['spf']['dom'] = $received['SPFfrom'][$i]; |
136 | 136 | $k++; |
137 | - } |
|
138 | - } |
|
139 | - } |
|
137 | + } |
|
138 | + } |
|
139 | + } |
|
140 | 140 | } |
141 | 141 | $received=NULL; |
142 | 142 | if ($k>1) $result['warn'][] = 'The trusted SPF AR Headers are present more than once. Something wrong.'; |
143 | 143 | |
144 | 144 | $k=0; |
145 | - if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dkim=(?<dkim>\w+)\s+[\w\s\(\)\-]+header\.d=(?<DKIMdom>[\w\.]+)/m',$header,$received) ) { |
|
145 | + if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dkim=(?<dkim>\w+)\s+[\w\s\(\)\-]+header\.d=(?<DKIMdom>[\w\.]+)/m',$header,$received) ) { |
|
146 | 146 | for ($i = count($received[0])-1;$i>=0;$i--) { |
147 | - foreach ($ARhosts as $mx) { |
|
148 | - if ($mx == $received['host'][$i]) { |
|
149 | - /* This is a trusted AR result */ |
|
150 | - $result['dkim']['result']=$received['dkim'][$i]; |
|
151 | - $result['dkim']['dom'] = $received['DKIMdom'][$i]; |
|
147 | + foreach ($ARhosts as $mx) { |
|
148 | + if ($mx == $received['host'][$i]) { |
|
149 | + /* This is a trusted AR result */ |
|
150 | + $result['dkim']['result']=$received['dkim'][$i]; |
|
151 | + $result['dkim']['dom'] = $received['DKIMdom'][$i]; |
|
152 | 152 | $k++; |
153 | - } |
|
154 | - } |
|
155 | - } |
|
153 | + } |
|
154 | + } |
|
155 | + } |
|
156 | 156 | } |
157 | 157 | $received=NULL; |
158 | 158 | if ($k>1) $result['warn'][] = 'The trusted DKIM AR Headers are present more than once. Something wrong.'; |
@@ -160,59 +160,59 @@ discard block |
||
160 | 160 | if ($dpl) { /* Use Spamassassin Plugin */ |
161 | 161 | if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*DSPAM_(?P<dtype>SPAM|HAM)_(?P<dlevel>\d\d)(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
162 | 162 | $result['spam']['status']=$received['spamstatus'][0]; |
163 | - $result['spam']['score'] = $received['score'][0]; |
|
163 | + $result['spam']['score'] = $received['score'][0]; |
|
164 | 164 | $result['spam']['th'] = $received['th'][0]; |
165 | 165 | $result['dspam']['type'] = dspamType($received['dtype'][0]); |
166 | 166 | $result['dspam']['level'] =$received['dlevel'][0]; |
167 | - } |
|
168 | - if (count($received[0])>1) $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
167 | + } |
|
168 | + if (count($received[0])>1) $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
169 | 169 | } |
170 | 170 | else { /* Parse apart all DSPAM Header and calculate a level */ |
171 | 171 | if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
172 | - $result['spam']['status']=$received['spamstatus'][0]; |
|
173 | - $result['spam']['score'] = $received['score'][0]; |
|
174 | - $result['spam']['th'] = $received['th'][0]; |
|
172 | + $result['spam']['status']=$received['spamstatus'][0]; |
|
173 | + $result['spam']['score'] = $received['score'][0]; |
|
174 | + $result['spam']['th'] = $received['th'][0]; |
|
175 | 175 | if (count($received[0])>1) |
176 | 176 | $result['warn'][]= 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
177 | 177 | } |
178 | 178 | if ( preg_match ('/\r\nX\-DSPAM\-Result:\s(?P<result>.*)\r\n/',$header,$received) != 1) |
179 | - $result['warn'] = 'DSPAM Result invalid, not present or present more than once.'; |
|
180 | - else |
|
181 | - $result['dspam']['type']=$received['result']; |
|
179 | + $result['warn'] = 'DSPAM Result invalid, not present or present more than once.'; |
|
180 | + else |
|
181 | + $result['dspam']['type']=$received['result']; |
|
182 | 182 | $prob = NULL; |
183 | 183 | $conf = NULL; |
184 | - if ( preg_match ('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/',$header,$received) != 1) |
|
185 | - $result['warn'][] = 'DSPAM Probability invalid, not present or present more than once.'; |
|
184 | + if ( preg_match ('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/',$header,$received) != 1) |
|
185 | + $result['warn'][] = 'DSPAM Probability invalid, not present or present more than once.'; |
|
186 | 186 | else |
187 | 187 | $prob = $received['prob']; |
188 | 188 | if ( preg_match ('/\r\nX\-DSPAM\-Confidence:\s(?P<conf>.*)\r\n/',$header,$received) != 1) |
189 | - $result['warn'][] = 'DSPAM Confidence invalid, not present or present more than once.'; |
|
189 | + $result['warn'][] = 'DSPAM Confidence invalid, not present or present more than once.'; |
|
190 | 190 | else |
191 | 191 | $conf = $received['conf']; |
192 | 192 | $result['dspam']['level'] = dspamLevel($prob,$conf); |
193 | 193 | } |
194 | 194 | $received=NULL; |
195 | 195 | if ( preg_match ('/\r\nFrom:\s(?P<from>.*)\r\n/',$header,$received) != 1) |
196 | - $result['warn'][] = 'From header invalid or not present'; |
|
197 | - else |
|
198 | - $result['from'] = $received['from']; |
|
196 | + $result['warn'][] = 'From header invalid or not present'; |
|
197 | + else |
|
198 | + $result['from'] = $received['from']; |
|
199 | 199 | |
200 | - if ( preg_match ('/\r\nDate:\s(?P<date>.*)\r\n/',$header,$received) != 1) |
|
201 | - $result['warn'][] = 'Date header invalid or not present'; |
|
200 | + if ( preg_match ('/\r\nDate:\s(?P<date>.*)\r\n/',$header,$received) != 1) |
|
201 | + $result['warn'][] = 'Date header invalid or not present'; |
|
202 | 202 | else |
203 | 203 | $result['date'] = $received['date']; |
204 | 204 | |
205 | 205 | $received=NULL; |
206 | - if ( preg_match ('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/',$header,$received) != 1) |
|
207 | - $result['warn'][] = 'Message-ID invalid, not present or present more than once.'; |
|
206 | + if ( preg_match ('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/',$header,$received) != 1) |
|
207 | + $result['warn'][] = 'Message-ID invalid, not present or present more than once.'; |
|
208 | 208 | else |
209 | 209 | $result['messageid']=$received['mid']; |
210 | 210 | |
211 | - $received=NULL; |
|
211 | + $received=NULL; |
|
212 | 212 | |
213 | - switch ($learn) { |
|
213 | + switch ($learn) { |
|
214 | 214 | case 'dspamc': |
215 | - if ( preg_match ('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/',$header,$received) != 1) |
|
215 | + if ( preg_match ('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/',$header,$received) != 1) |
|
216 | 216 | $result['warn'] = 'DSPAM Signature invalid, not present or present more than once.'; |
217 | 217 | else |
218 | 218 | $result['dspam']['learn']=$received['sig']; |
@@ -223,23 +223,23 @@ discard block |
||
223 | 223 | syslog (LOG_INFO,$user.': Error in "learn" imap configuration value. Please, set "dspamc" or "false".'); |
224 | 224 | } |
225 | 225 | |
226 | - return $result; |
|
226 | + return $result; |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | |
230 | 230 | |
231 | 231 | |
232 | 232 | function printTableHeader($title,$content,$footer=FALSE,$fcontent) { |
233 | - print <<<END |
|
233 | + print <<<END |
|
234 | 234 | <caption>$title</caption> |
235 | 235 | <thead> |
236 | 236 | <tr> |
237 | 237 | END; |
238 | 238 | $kcontent = array_keys($content); |
239 | - $cols = count($kcontent); |
|
240 | - for ($i=0; $i<$cols; $i++) { |
|
239 | + $cols = count($kcontent); |
|
240 | + for ($i=0; $i<$cols; $i++) { |
|
241 | 241 | $key = $kcontent[$i]; |
242 | - printf ('<th colspan="%d" rowspan="%d">%s</th>', |
|
242 | + printf ('<th colspan="%d" rowspan="%d">%s</th>', |
|
243 | 243 | !is_array($content[$key]) ?: |
244 | 244 | count(array_keys($content[$key])) ?: '1', |
245 | 245 | !is_array($content[$key]) ?: |
@@ -255,13 +255,13 @@ discard block |
||
255 | 255 | } |
256 | 256 | } |
257 | 257 | |
258 | - print '</tr></thead>'; |
|
259 | - if ($footer) { |
|
260 | - print '<tfoot><tr>'; |
|
261 | - print "<th colspan=\"$cols\">".$fcontent.'</th>'; |
|
262 | - print '</tr></tfoot>'; |
|
263 | - } |
|
264 | - return TRUE; |
|
258 | + print '</tr></thead>'; |
|
259 | + if ($footer) { |
|
260 | + print '<tfoot><tr>'; |
|
261 | + print "<th colspan=\"$cols\">".$fcontent.'</th>'; |
|
262 | + print '</tr></tfoot>'; |
|
263 | + } |
|
264 | + return TRUE; |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | } |
337 | 337 | /* Spamassassin format */ |
338 | 338 | if (isset($row['status'])) |
339 | - switch($row['status']) { |
|
339 | + switch($row['status']) { |
|
340 | 340 | case 'No': |
341 | 341 | $color = 'rgba(0,255,0, %.1f)'; |
342 | 342 | break; |
@@ -2,56 +2,56 @@ discard block |
||
2 | 2 | |
3 | 3 | function imapFolder($cf, $username) { |
4 | 4 | $return = array(); |
5 | - $open='{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'; |
|
5 | + $open = '{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'; |
|
6 | 6 | $m_mail = imap_open($open, $username, $cf['authpassword'], OP_READONLY) |
7 | - or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
|
8 | - if ( !$m_mail ) exit(254); |
|
7 | + or syslog(LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: '.imap_last_error()); |
|
8 | + if (!$m_mail) exit(254); |
|
9 | 9 | |
10 | 10 | |
11 | - syslog (LOG_INFO,$cf['user'].': Successfully connected to <'.$cf['mailhost'].'>; Listing folders of account <'.$username.'>...'); |
|
11 | + syslog(LOG_INFO, $cf['user'].': Successfully connected to <'.$cf['mailhost'].'>; Listing folders of account <'.$username.'>...'); |
|
12 | 12 | //get all folder |
13 | 13 | $list = imap_list($m_mail, $open, "*"); |
14 | 14 | imap_close($m_mail); |
15 | 15 | if (is_array($list)) |
16 | 16 | foreach ($list as $mbox) |
17 | - $return[] = explode($open,$mbox,2)[1]; |
|
17 | + $return[] = explode($open, $mbox, 2)[1]; |
|
18 | 18 | else |
19 | - syslog (LOG_INFO,$cf['user'] . ': imap_list failed: ' . imap_last_error() ); |
|
19 | + syslog(LOG_INFO, $cf['user'].': imap_list failed: '.imap_last_error()); |
|
20 | 20 | return $return; |
21 | 21 | } |
22 | 22 | |
23 | -function imapFind ($cf, $username, $folder) { |
|
24 | - $head=array(); |
|
25 | - $m_mail = imap_open('{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'.$folder, $username,$cf['authpassword'], OP_READONLY) |
|
26 | - or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
|
27 | - if ( !$m_mail ) exit(254); |
|
23 | +function imapFind($cf, $username, $folder) { |
|
24 | + $head = array(); |
|
25 | + $m_mail = imap_open('{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'.$folder, $username, $cf['authpassword'], OP_READONLY) |
|
26 | + or syslog(LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: '.imap_last_error()); |
|
27 | + if (!$m_mail) exit(254); |
|
28 | 28 | |
29 | 29 | |
30 | - syslog (LOG_INFO,$cf['user'].': Successfully connected to <'.$cf['mailhost'].">; Reading <$folder> messages of last ".$cf['oldestday'].' days on account <'.$username.'>...'); |
|
30 | + syslog(LOG_INFO, $cf['user'].': Successfully connected to <'.$cf['mailhost'].">; Reading <$folder> messages of last ".$cf['oldestday'].' days on account <'.$username.'>...'); |
|
31 | 31 | //get all messages |
32 | - $dateTh = date ( "d-M-Y", strToTime ( '-'.$cf['oldestday'].' days' ) ); |
|
33 | - $m_search=imap_search ($m_mail, "SINCE \"$dateTh\" TEXT \"Authentication-Results: \"" ); |
|
32 | + $dateTh = date("d-M-Y", strToTime('-'.$cf['oldestday'].' days')); |
|
33 | + $m_search = imap_search($m_mail, "SINCE \"$dateTh\" TEXT \"Authentication-Results: \""); |
|
34 | 34 | |
35 | 35 | |
36 | 36 | // Order results starting from newest message |
37 | - if ( empty($m_search) ) { |
|
38 | - syslog (LOG_INFO,$cf['user'].": No suitable mail found in <$folder> folder."); |
|
39 | - if ( $ierr = imap_errors() ) |
|
40 | - foreach ( $ierr as $thiserr ) |
|
41 | - syslog (LOG_ERR, $cf['user'].": IMAP Error: $thiserr"); |
|
42 | - if ( $ierr = imap_alerts() ) |
|
43 | - foreach ( $ierr as $thiserr ) |
|
44 | - syslog (LOG_ALERT, $cf['user'].": IMAP Alert: $thiserr"); |
|
45 | - imap_close( $m_mail ); |
|
37 | + if (empty($m_search)) { |
|
38 | + syslog(LOG_INFO, $cf['user'].": No suitable mail found in <$folder> folder."); |
|
39 | + if ($ierr = imap_errors()) |
|
40 | + foreach ($ierr as $thiserr) |
|
41 | + syslog(LOG_ERR, $cf['user'].": IMAP Error: $thiserr"); |
|
42 | + if ($ierr = imap_alerts()) |
|
43 | + foreach ($ierr as $thiserr) |
|
44 | + syslog(LOG_ALERT, $cf['user'].": IMAP Alert: $thiserr"); |
|
45 | + imap_close($m_mail); |
|
46 | 46 | return FALSE; |
47 | 47 | } |
48 | - $nmes = count ($m_search); |
|
49 | - syslog (LOG_INFO,$cf['user'].": Found $nmes mail in <$folder> folder."); |
|
48 | + $nmes = count($m_search); |
|
49 | + syslog(LOG_INFO, $cf['user'].": Found $nmes mail in <$folder> folder."); |
|
50 | 50 | if ($nmes>0) rsort($m_search); |
51 | 51 | |
52 | 52 | // loop for each message |
53 | 53 | foreach ($m_search as $onem) |
54 | - $head[] = imap_fetchheader($m_mail, $onem ); |
|
54 | + $head[] = imap_fetchheader($m_mail, $onem); |
|
55 | 55 | imap_close($m_mail); |
56 | 56 | return $head; |
57 | 57 | } |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | function dspamLevel($prob, $conf) { |
60 | 60 | /* Calculate DSPAM Level as the Spamassassin Plugin */ |
61 | 61 | if (is_null($prob) or is_null($conf)) return '-'; |
62 | - $t_prob = abs((($prob - 0.5) * 2) * 100); |
|
63 | - return round(($t_prob + ($conf*100)) / 2); |
|
62 | + $t_prob = abs((($prob-0.5) * 2) * 100); |
|
63 | + return round(($t_prob+($conf * 100)) / 2); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | function dspamType($classSpam) { |
67 | - switch($classSpam) { |
|
67 | + switch ($classSpam) { |
|
68 | 68 | case 'HAM': |
69 | 69 | return 'Innocent'; |
70 | 70 | case 'SPAM': |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | } |
76 | 76 | } |
77 | 77 | |
78 | -function imapInfo($user,$header,$ARhosts,$dpl=false, $learn=false) { |
|
78 | +function imapInfo($user, $header, $ARhosts, $dpl = false, $learn = false) { |
|
79 | 79 | /* Get relevant Info from header's mail */ |
80 | 80 | /* Each line must end with /r/n */ |
81 | 81 | |
@@ -109,118 +109,118 @@ discard block |
||
109 | 109 | ); |
110 | 110 | |
111 | 111 | |
112 | - if ( preg_match_all ('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dmarc=(?<dmarc>\w+)\s+\(p=\w+\s+dis=\w+\)\s+header\.from=(?<DMARCfrom>[\w\.]+)/m',$header,$received) ) { |
|
113 | - $k=0; |
|
114 | - for ($i = count($received[0])-1;$i>=0;$i--) { |
|
112 | + if (preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dmarc=(?<dmarc>\w+)\s+\(p=\w+\s+dis=\w+\)\s+header\.from=(?<DMARCfrom>[\w\.]+)/m', $header, $received)) { |
|
113 | + $k = 0; |
|
114 | + for ($i = count($received[0])-1; $i>=0; $i--) { |
|
115 | 115 | foreach ($ARhosts as $mx) { |
116 | 116 | if ($mx == $received['host'][$i]) { |
117 | 117 | /* This is a trusted AR result */ |
118 | - $result['dmarc']['result']=$received['dmarc'][$i]; |
|
118 | + $result['dmarc']['result'] = $received['dmarc'][$i]; |
|
119 | 119 | $result['dmarc']['dom'] = $received['DMARCfrom'][$i]; |
120 | 120 | $k++; |
121 | 121 | } |
122 | 122 | } |
123 | 123 | } |
124 | 124 | } |
125 | - $received=NULL; |
|
125 | + $received = NULL; |
|
126 | 126 | if ($k>1) $result['warn'][] = 'The trusted DMARC AR Headers are present more than once. Something wrong.'; |
127 | 127 | |
128 | - if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)spf=(?<spf>\w+)\s+smtp\.(?:mailfrom|helo)=(?<SPFfrom>[\w\.]+)/m',$header,$received) ) { |
|
129 | - $k=0; |
|
130 | - for ($i = count($received[0])-1;$i>=0;$i--) { |
|
128 | + if (preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)spf=(?<spf>\w+)\s+smtp\.(?:mailfrom|helo)=(?<SPFfrom>[\w\.]+)/m', $header, $received)) { |
|
129 | + $k = 0; |
|
130 | + for ($i = count($received[0])-1; $i>=0; $i--) { |
|
131 | 131 | foreach ($ARhosts as $mx) { |
132 | 132 | if ($mx == $received['host'][$i]) { |
133 | 133 | /* This is a trusted AR result */ |
134 | - $result['spf']['result']=$received['spf'][$i]; |
|
134 | + $result['spf']['result'] = $received['spf'][$i]; |
|
135 | 135 | $result['spf']['dom'] = $received['SPFfrom'][$i]; |
136 | 136 | $k++; |
137 | 137 | } |
138 | 138 | } |
139 | 139 | } |
140 | 140 | } |
141 | - $received=NULL; |
|
141 | + $received = NULL; |
|
142 | 142 | if ($k>1) $result['warn'][] = 'The trusted SPF AR Headers are present more than once. Something wrong.'; |
143 | 143 | |
144 | - $k=0; |
|
145 | - if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dkim=(?<dkim>\w+)\s+[\w\s\(\)\-]+header\.d=(?<DKIMdom>[\w\.]+)/m',$header,$received) ) { |
|
146 | - for ($i = count($received[0])-1;$i>=0;$i--) { |
|
144 | + $k = 0; |
|
145 | + if (preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dkim=(?<dkim>\w+)\s+[\w\s\(\)\-]+header\.d=(?<DKIMdom>[\w\.]+)/m', $header, $received)) { |
|
146 | + for ($i = count($received[0])-1; $i>=0; $i--) { |
|
147 | 147 | foreach ($ARhosts as $mx) { |
148 | 148 | if ($mx == $received['host'][$i]) { |
149 | 149 | /* This is a trusted AR result */ |
150 | - $result['dkim']['result']=$received['dkim'][$i]; |
|
150 | + $result['dkim']['result'] = $received['dkim'][$i]; |
|
151 | 151 | $result['dkim']['dom'] = $received['DKIMdom'][$i]; |
152 | 152 | $k++; |
153 | 153 | } |
154 | 154 | } |
155 | 155 | } |
156 | 156 | } |
157 | - $received=NULL; |
|
157 | + $received = NULL; |
|
158 | 158 | if ($k>1) $result['warn'][] = 'The trusted DKIM AR Headers are present more than once. Something wrong.'; |
159 | 159 | |
160 | 160 | if ($dpl) { /* Use Spamassassin Plugin */ |
161 | - if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*DSPAM_(?P<dtype>SPAM|HAM)_(?P<dlevel>\d\d)(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
|
162 | - $result['spam']['status']=$received['spamstatus'][0]; |
|
161 | + if (preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*DSPAM_(?P<dtype>SPAM|HAM)_(?P<dlevel>\d\d)(?:.|\r\n\s+)*\]/m', $header, $received)) { |
|
162 | + $result['spam']['status'] = $received['spamstatus'][0]; |
|
163 | 163 | $result['spam']['score'] = $received['score'][0]; |
164 | 164 | $result['spam']['th'] = $received['th'][0]; |
165 | 165 | $result['dspam']['type'] = dspamType($received['dtype'][0]); |
166 | - $result['dspam']['level'] =$received['dlevel'][0]; |
|
166 | + $result['dspam']['level'] = $received['dlevel'][0]; |
|
167 | 167 | } |
168 | 168 | if (count($received[0])>1) $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
169 | 169 | } |
170 | 170 | else { /* Parse apart all DSPAM Header and calculate a level */ |
171 | - if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
|
172 | - $result['spam']['status']=$received['spamstatus'][0]; |
|
171 | + if (preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*\]/m', $header, $received)) { |
|
172 | + $result['spam']['status'] = $received['spamstatus'][0]; |
|
173 | 173 | $result['spam']['score'] = $received['score'][0]; |
174 | 174 | $result['spam']['th'] = $received['th'][0]; |
175 | 175 | if (count($received[0])>1) |
176 | - $result['warn'][]= 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
176 | + $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
177 | 177 | } |
178 | - if ( preg_match ('/\r\nX\-DSPAM\-Result:\s(?P<result>.*)\r\n/',$header,$received) != 1) |
|
178 | + if (preg_match('/\r\nX\-DSPAM\-Result:\s(?P<result>.*)\r\n/', $header, $received) != 1) |
|
179 | 179 | $result['warn'] = 'DSPAM Result invalid, not present or present more than once.'; |
180 | 180 | else |
181 | - $result['dspam']['type']=$received['result']; |
|
181 | + $result['dspam']['type'] = $received['result']; |
|
182 | 182 | $prob = NULL; |
183 | 183 | $conf = NULL; |
184 | - if ( preg_match ('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/',$header,$received) != 1) |
|
184 | + if (preg_match('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/', $header, $received) != 1) |
|
185 | 185 | $result['warn'][] = 'DSPAM Probability invalid, not present or present more than once.'; |
186 | 186 | else |
187 | 187 | $prob = $received['prob']; |
188 | - if ( preg_match ('/\r\nX\-DSPAM\-Confidence:\s(?P<conf>.*)\r\n/',$header,$received) != 1) |
|
188 | + if (preg_match('/\r\nX\-DSPAM\-Confidence:\s(?P<conf>.*)\r\n/', $header, $received) != 1) |
|
189 | 189 | $result['warn'][] = 'DSPAM Confidence invalid, not present or present more than once.'; |
190 | 190 | else |
191 | 191 | $conf = $received['conf']; |
192 | - $result['dspam']['level'] = dspamLevel($prob,$conf); |
|
192 | + $result['dspam']['level'] = dspamLevel($prob, $conf); |
|
193 | 193 | } |
194 | - $received=NULL; |
|
195 | - if ( preg_match ('/\r\nFrom:\s(?P<from>.*)\r\n/',$header,$received) != 1) |
|
194 | + $received = NULL; |
|
195 | + if (preg_match('/\r\nFrom:\s(?P<from>.*)\r\n/', $header, $received) != 1) |
|
196 | 196 | $result['warn'][] = 'From header invalid or not present'; |
197 | 197 | else |
198 | 198 | $result['from'] = $received['from']; |
199 | 199 | |
200 | - if ( preg_match ('/\r\nDate:\s(?P<date>.*)\r\n/',$header,$received) != 1) |
|
200 | + if (preg_match('/\r\nDate:\s(?P<date>.*)\r\n/', $header, $received) != 1) |
|
201 | 201 | $result['warn'][] = 'Date header invalid or not present'; |
202 | 202 | else |
203 | 203 | $result['date'] = $received['date']; |
204 | 204 | |
205 | - $received=NULL; |
|
206 | - if ( preg_match ('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/',$header,$received) != 1) |
|
205 | + $received = NULL; |
|
206 | + if (preg_match('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/', $header, $received) != 1) |
|
207 | 207 | $result['warn'][] = 'Message-ID invalid, not present or present more than once.'; |
208 | 208 | else |
209 | - $result['messageid']=$received['mid']; |
|
209 | + $result['messageid'] = $received['mid']; |
|
210 | 210 | |
211 | - $received=NULL; |
|
211 | + $received = NULL; |
|
212 | 212 | |
213 | 213 | switch ($learn) { |
214 | 214 | case 'dspamc': |
215 | - if ( preg_match ('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/',$header,$received) != 1) |
|
215 | + if (preg_match('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/', $header, $received) != 1) |
|
216 | 216 | $result['warn'] = 'DSPAM Signature invalid, not present or present more than once.'; |
217 | 217 | else |
218 | - $result['dspam']['learn']=$received['sig']; |
|
218 | + $result['dspam']['learn'] = $received['sig']; |
|
219 | 219 | break; |
220 | 220 | case false: |
221 | 221 | break; |
222 | 222 | default: |
223 | - syslog (LOG_INFO,$user.': Error in "learn" imap configuration value. Please, set "dspamc" or "false".'); |
|
223 | + syslog(LOG_INFO, $user.': Error in "learn" imap configuration value. Please, set "dspamc" or "false".'); |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | return $result; |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | |
230 | 230 | |
231 | 231 | |
232 | -function printTableHeader($title,$content,$footer=FALSE,$fcontent) { |
|
232 | +function printTableHeader($title, $content, $footer = FALSE, $fcontent) { |
|
233 | 233 | print <<<END |
234 | 234 | <caption>$title</caption> |
235 | 235 | <thead> |
@@ -237,21 +237,19 @@ discard block |
||
237 | 237 | END; |
238 | 238 | $kcontent = array_keys($content); |
239 | 239 | $cols = count($kcontent); |
240 | - for ($i=0; $i<$cols; $i++) { |
|
240 | + for ($i = 0; $i<$cols; $i++) { |
|
241 | 241 | $key = $kcontent[$i]; |
242 | - printf ('<th colspan="%d" rowspan="%d">%s</th>', |
|
243 | - !is_array($content[$key]) ?: |
|
244 | - count(array_keys($content[$key])) ?: '1', |
|
245 | - !is_array($content[$key]) ?: |
|
246 | - empty(array_keys($content[$key])) ? '2' : '1', |
|
242 | + printf('<th colspan="%d" rowspan="%d">%s</th>', |
|
243 | + !is_array($content[$key]) ?: count(array_keys($content[$key])) ?: '1', |
|
244 | + !is_array($content[$key]) ?: empty(array_keys($content[$key])) ? '2' : '1', |
|
247 | 245 | $kcontent[$i]); |
248 | 246 | } |
249 | 247 | print '</tr><tr>'; |
250 | - for ($i=0; $i<$cols; $i++) { |
|
248 | + for ($i = 0; $i<$cols; $i++) { |
|
251 | 249 | $key = $kcontent[$i]; |
252 | - if (is_array($content[$key])&&($hs = array_keys($content[$key]))) { |
|
250 | + if (is_array($content[$key]) && ($hs = array_keys($content[$key]))) { |
|
253 | 251 | foreach ($hs as $h) |
254 | - printf('<th>%s</th>',$h); |
|
252 | + printf('<th>%s</th>', $h); |
|
255 | 253 | } |
256 | 254 | } |
257 | 255 | |
@@ -267,7 +265,7 @@ discard block |
||
267 | 265 | |
268 | 266 | function formatVal($val, $learn) { |
269 | 267 | foreach (array_keys($val) as $key) { |
270 | - if (is_array($val["$key"]) and ($key!='warn')) |
|
268 | + if (is_array($val["$key"]) and ($key != 'warn')) |
|
271 | 269 | $val["$key"] = formatVal($val["$key"], $learn); |
272 | 270 | else { |
273 | 271 | switch ($key) { |
@@ -275,7 +273,7 @@ discard block |
||
275 | 273 | if (empty($val["$key"])) |
276 | 274 | $val["$key"] = '-'; |
277 | 275 | else |
278 | - $val["$key"] = sprintf('<div title="%s">Y</div>',implode($val["$key"],"\n")); |
|
276 | + $val["$key"] = sprintf('<div title="%s">Y</div>', implode($val["$key"], "\n")); |
|
279 | 277 | break; |
280 | 278 | case 'learn': |
281 | 279 | $val["$key"] = formLearn($learn, $val); |
@@ -297,26 +295,26 @@ discard block |
||
297 | 295 | $par['class'] = $class; |
298 | 296 | $val["$class"] = sprintf('dspamc --user dspam --deliver=summary --class=%s --source=error --signature=%s', |
299 | 297 | strtolower($class), $par['learn']); |
300 | - if (($class != $par['type'])||($par['level']<99)) |
|
298 | + if (($class != $par['type']) || ($par['level']<99)) |
|
301 | 299 | $return .= sprintf(file_get_contents('formLearnDSPAM.htm'), |
302 | - $class,$class,$val["$class"],base64_encode(json_encode($par)),$class); |
|
300 | + $class, $class, $val["$class"], base64_encode(json_encode($par)), $class); |
|
303 | 301 | } |
304 | 302 | default: |
305 | 303 | return $return; |
306 | 304 | } |
307 | 305 | } |
308 | 306 | |
309 | -function printTableRow($row, $learn, $init=true) { |
|
307 | +function printTableRow($row, $learn, $init = true) { |
|
310 | 308 | $color = 'inherit'; |
311 | 309 | if ($init) |
312 | - $row=formatVal($row,$learn); |
|
313 | - foreach( $row as $key => $val) { |
|
310 | + $row = formatVal($row, $learn); |
|
311 | + foreach ($row as $key => $val) { |
|
314 | 312 | if (is_array($val)) |
315 | 313 | printTableRow($val, $learn, false); |
316 | 314 | else { |
317 | 315 | /* DSPAM format */ |
318 | 316 | if (isset($row['type'])) |
319 | - switch($row['type']) { |
|
317 | + switch ($row['type']) { |
|
320 | 318 | case 'Innocent': |
321 | 319 | case 'HAM': |
322 | 320 | $color = 'rgba(0,255,0, %.1f)'; |
@@ -327,7 +325,7 @@ discard block |
||
327 | 325 | } |
328 | 326 | /* DMARC, DKIM, SPF format */ |
329 | 327 | if (isset($row['result'])) |
330 | - switch($row['result']) { |
|
328 | + switch ($row['result']) { |
|
331 | 329 | case 'pass': |
332 | 330 | $color = 'rgba(0,255,0, %.1f)'; |
333 | 331 | break; |
@@ -336,7 +334,7 @@ discard block |
||
336 | 334 | } |
337 | 335 | /* Spamassassin format */ |
338 | 336 | if (isset($row['status'])) |
339 | - switch($row['status']) { |
|
337 | + switch ($row['status']) { |
|
340 | 338 | case 'No': |
341 | 339 | $color = 'rgba(0,255,0, %.1f)'; |
342 | 340 | break; |
@@ -344,9 +342,9 @@ discard block |
||
344 | 342 | $color = 'rgba(255,0,0,%.1f)'; |
345 | 343 | } |
346 | 344 | |
347 | - $alpha = (is_numeric($val)AND($key=='type')) ? round($val/100,1) : 1.0; |
|
345 | + $alpha = (is_numeric($val) AND ($key == 'type')) ? round($val / 100, 1) : 1.0; |
|
348 | 346 | $bg = sprintf(" style=\"background-color: $color\"", $alpha); |
349 | - printf ('<td%s>%s</td>',$bg, $val); |
|
347 | + printf('<td%s>%s</td>', $bg, $val); |
|
350 | 348 | } |
351 | 349 | } |
352 | 350 | } |
@@ -5,18 +5,21 @@ discard block |
||
5 | 5 | $open='{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'; |
6 | 6 | $m_mail = imap_open($open, $username, $cf['authpassword'], OP_READONLY) |
7 | 7 | or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
8 | - if ( !$m_mail ) exit(254); |
|
8 | + if ( !$m_mail ) { |
|
9 | + exit(254); |
|
10 | + } |
|
9 | 11 | |
10 | 12 | |
11 | 13 | syslog (LOG_INFO,$cf['user'].': Successfully connected to <'.$cf['mailhost'].'>; Listing folders of account <'.$username.'>...'); |
12 | 14 | //get all folder |
13 | 15 | $list = imap_list($m_mail, $open, "*"); |
14 | 16 | imap_close($m_mail); |
15 | - if (is_array($list)) |
|
16 | - foreach ($list as $mbox) |
|
17 | + if (is_array($list)) { |
|
18 | + foreach ($list as $mbox) |
|
17 | 19 | $return[] = explode($open,$mbox,2)[1]; |
18 | - else |
|
19 | - syslog (LOG_INFO,$cf['user'] . ': imap_list failed: ' . imap_last_error() ); |
|
20 | + } else { |
|
21 | + syslog (LOG_INFO,$cf['user'] . ': imap_list failed: ' . imap_last_error() ); |
|
22 | + } |
|
20 | 23 | return $return; |
21 | 24 | } |
22 | 25 | |
@@ -24,7 +27,9 @@ discard block |
||
24 | 27 | $head=array(); |
25 | 28 | $m_mail = imap_open('{'.$cf['mailhost'].':143/imap/novalidate-cert/authuser='.$cf['authuser'].'}'.$folder, $username,$cf['authpassword'], OP_READONLY) |
26 | 29 | or syslog (LOG_EMERG, $cf['user'].': Error in IMAP connection to <'.$cf['mailhost'].'>: ' . imap_last_error()); |
27 | - if ( !$m_mail ) exit(254); |
|
30 | + if ( !$m_mail ) { |
|
31 | + exit(254); |
|
32 | + } |
|
28 | 33 | |
29 | 34 | |
30 | 35 | syslog (LOG_INFO,$cf['user'].': Successfully connected to <'.$cf['mailhost'].">; Reading <$folder> messages of last ".$cf['oldestday'].' days on account <'.$username.'>...'); |
@@ -36,29 +41,36 @@ discard block |
||
36 | 41 | // Order results starting from newest message |
37 | 42 | if ( empty($m_search) ) { |
38 | 43 | syslog (LOG_INFO,$cf['user'].": No suitable mail found in <$folder> folder."); |
39 | - if ( $ierr = imap_errors() ) |
|
40 | - foreach ( $ierr as $thiserr ) |
|
44 | + if ( $ierr = imap_errors() ) { |
|
45 | + foreach ( $ierr as $thiserr ) |
|
41 | 46 | syslog (LOG_ERR, $cf['user'].": IMAP Error: $thiserr"); |
42 | - if ( $ierr = imap_alerts() ) |
|
43 | - foreach ( $ierr as $thiserr ) |
|
47 | + } |
|
48 | + if ( $ierr = imap_alerts() ) { |
|
49 | + foreach ( $ierr as $thiserr ) |
|
44 | 50 | syslog (LOG_ALERT, $cf['user'].": IMAP Alert: $thiserr"); |
51 | + } |
|
45 | 52 | imap_close( $m_mail ); |
46 | 53 | return FALSE; |
47 | 54 | } |
48 | 55 | $nmes = count ($m_search); |
49 | 56 | syslog (LOG_INFO,$cf['user'].": Found $nmes mail in <$folder> folder."); |
50 | - if ($nmes>0) rsort($m_search); |
|
57 | + if ($nmes>0) { |
|
58 | + rsort($m_search); |
|
59 | + } |
|
51 | 60 | |
52 | 61 | // loop for each message |
53 | - foreach ($m_search as $onem) |
|
54 | - $head[] = imap_fetchheader($m_mail, $onem ); |
|
62 | + foreach ($m_search as $onem) { |
|
63 | + $head[] = imap_fetchheader($m_mail, $onem ); |
|
64 | + } |
|
55 | 65 | imap_close($m_mail); |
56 | 66 | return $head; |
57 | 67 | } |
58 | 68 | |
59 | 69 | function dspamLevel($prob, $conf) { |
60 | 70 | /* Calculate DSPAM Level as the Spamassassin Plugin */ |
61 | - if (is_null($prob) or is_null($conf)) return '-'; |
|
71 | + if (is_null($prob) or is_null($conf)) { |
|
72 | + return '-'; |
|
73 | + } |
|
62 | 74 | $t_prob = abs((($prob - 0.5) * 2) * 100); |
63 | 75 | return round(($t_prob + ($conf*100)) / 2); |
64 | 76 | } |
@@ -123,7 +135,9 @@ discard block |
||
123 | 135 | } |
124 | 136 | } |
125 | 137 | $received=NULL; |
126 | - if ($k>1) $result['warn'][] = 'The trusted DMARC AR Headers are present more than once. Something wrong.'; |
|
138 | + if ($k>1) { |
|
139 | + $result['warn'][] = 'The trusted DMARC AR Headers are present more than once. Something wrong.'; |
|
140 | + } |
|
127 | 141 | |
128 | 142 | if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)spf=(?<spf>\w+)\s+smtp\.(?:mailfrom|helo)=(?<SPFfrom>[\w\.]+)/m',$header,$received) ) { |
129 | 143 | $k=0; |
@@ -139,7 +153,9 @@ discard block |
||
139 | 153 | } |
140 | 154 | } |
141 | 155 | $received=NULL; |
142 | - if ($k>1) $result['warn'][] = 'The trusted SPF AR Headers are present more than once. Something wrong.'; |
|
156 | + if ($k>1) { |
|
157 | + $result['warn'][] = 'The trusted SPF AR Headers are present more than once. Something wrong.'; |
|
158 | + } |
|
143 | 159 | |
144 | 160 | $k=0; |
145 | 161 | if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dkim=(?<dkim>\w+)\s+[\w\s\(\)\-]+header\.d=(?<DKIMdom>[\w\.]+)/m',$header,$received) ) { |
@@ -155,7 +171,9 @@ discard block |
||
155 | 171 | } |
156 | 172 | } |
157 | 173 | $received=NULL; |
158 | - if ($k>1) $result['warn'][] = 'The trusted DKIM AR Headers are present more than once. Something wrong.'; |
|
174 | + if ($k>1) { |
|
175 | + $result['warn'][] = 'The trusted DKIM AR Headers are present more than once. Something wrong.'; |
|
176 | + } |
|
159 | 177 | |
160 | 178 | if ($dpl) { /* Use Spamassassin Plugin */ |
161 | 179 | if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*DSPAM_(?P<dtype>SPAM|HAM)_(?P<dlevel>\d\d)(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
@@ -165,57 +183,66 @@ discard block |
||
165 | 183 | $result['dspam']['type'] = dspamType($received['dtype'][0]); |
166 | 184 | $result['dspam']['level'] =$received['dlevel'][0]; |
167 | 185 | } |
168 | - if (count($received[0])>1) $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
169 | - } |
|
170 | - else { /* Parse apart all DSPAM Header and calculate a level */ |
|
186 | + if (count($received[0])>1) { |
|
187 | + $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
188 | + } |
|
189 | + } else { /* Parse apart all DSPAM Header and calculate a level */ |
|
171 | 190 | if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
172 | 191 | $result['spam']['status']=$received['spamstatus'][0]; |
173 | 192 | $result['spam']['score'] = $received['score'][0]; |
174 | 193 | $result['spam']['th'] = $received['th'][0]; |
175 | - if (count($received[0])>1) |
|
176 | - $result['warn'][]= 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
194 | + if (count($received[0])>1) { |
|
195 | + $result['warn'][]= 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
|
196 | + } |
|
177 | 197 | } |
178 | - if ( preg_match ('/\r\nX\-DSPAM\-Result:\s(?P<result>.*)\r\n/',$header,$received) != 1) |
|
179 | - $result['warn'] = 'DSPAM Result invalid, not present or present more than once.'; |
|
180 | - else |
|
181 | - $result['dspam']['type']=$received['result']; |
|
198 | + if ( preg_match ('/\r\nX\-DSPAM\-Result:\s(?P<result>.*)\r\n/',$header,$received) != 1) { |
|
199 | + $result['warn'] = 'DSPAM Result invalid, not present or present more than once.'; |
|
200 | + } else { |
|
201 | + $result['dspam']['type']=$received['result']; |
|
202 | + } |
|
182 | 203 | $prob = NULL; |
183 | 204 | $conf = NULL; |
184 | - if ( preg_match ('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/',$header,$received) != 1) |
|
185 | - $result['warn'][] = 'DSPAM Probability invalid, not present or present more than once.'; |
|
186 | - else |
|
187 | - $prob = $received['prob']; |
|
188 | - if ( preg_match ('/\r\nX\-DSPAM\-Confidence:\s(?P<conf>.*)\r\n/',$header,$received) != 1) |
|
189 | - $result['warn'][] = 'DSPAM Confidence invalid, not present or present more than once.'; |
|
190 | - else |
|
191 | - $conf = $received['conf']; |
|
205 | + if ( preg_match ('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/',$header,$received) != 1) { |
|
206 | + $result['warn'][] = 'DSPAM Probability invalid, not present or present more than once.'; |
|
207 | + } else { |
|
208 | + $prob = $received['prob']; |
|
209 | + } |
|
210 | + if ( preg_match ('/\r\nX\-DSPAM\-Confidence:\s(?P<conf>.*)\r\n/',$header,$received) != 1) { |
|
211 | + $result['warn'][] = 'DSPAM Confidence invalid, not present or present more than once.'; |
|
212 | + } else { |
|
213 | + $conf = $received['conf']; |
|
214 | + } |
|
192 | 215 | $result['dspam']['level'] = dspamLevel($prob,$conf); |
193 | 216 | } |
194 | 217 | $received=NULL; |
195 | - if ( preg_match ('/\r\nFrom:\s(?P<from>.*)\r\n/',$header,$received) != 1) |
|
196 | - $result['warn'][] = 'From header invalid or not present'; |
|
197 | - else |
|
198 | - $result['from'] = $received['from']; |
|
218 | + if ( preg_match ('/\r\nFrom:\s(?P<from>.*)\r\n/',$header,$received) != 1) { |
|
219 | + $result['warn'][] = 'From header invalid or not present'; |
|
220 | + } else { |
|
221 | + $result['from'] = $received['from']; |
|
222 | + } |
|
199 | 223 | |
200 | - if ( preg_match ('/\r\nDate:\s(?P<date>.*)\r\n/',$header,$received) != 1) |
|
201 | - $result['warn'][] = 'Date header invalid or not present'; |
|
202 | - else |
|
203 | - $result['date'] = $received['date']; |
|
224 | + if ( preg_match ('/\r\nDate:\s(?P<date>.*)\r\n/',$header,$received) != 1) { |
|
225 | + $result['warn'][] = 'Date header invalid or not present'; |
|
226 | + } else { |
|
227 | + $result['date'] = $received['date']; |
|
228 | + } |
|
204 | 229 | |
205 | 230 | $received=NULL; |
206 | - if ( preg_match ('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/',$header,$received) != 1) |
|
207 | - $result['warn'][] = 'Message-ID invalid, not present or present more than once.'; |
|
208 | - else |
|
209 | - $result['messageid']=$received['mid']; |
|
231 | + if ( preg_match ('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/',$header,$received) != 1) { |
|
232 | + $result['warn'][] = 'Message-ID invalid, not present or present more than once.'; |
|
233 | + } else { |
|
234 | + $result['messageid']=$received['mid']; |
|
235 | + } |
|
210 | 236 | |
211 | 237 | $received=NULL; |
212 | 238 | |
213 | 239 | switch ($learn) { |
214 | 240 | case 'dspamc': |
215 | - if ( preg_match ('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/',$header,$received) != 1) |
|
216 | - $result['warn'] = 'DSPAM Signature invalid, not present or present more than once.'; |
|
217 | - else |
|
218 | - $result['dspam']['learn']=$received['sig']; |
|
241 | + if ( preg_match ('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/',$header,$received) != 1) { |
|
242 | + $result['warn'] = 'DSPAM Signature invalid, not present or present more than once.'; |
|
243 | + } else { |
|
244 | + $result['dspam']['learn']=$received['sig']; |
|
245 | + } |
|
219 | 246 | break; |
220 | 247 | case false: |
221 | 248 | break; |
@@ -250,8 +277,9 @@ discard block |
||
250 | 277 | for ($i=0; $i<$cols; $i++) { |
251 | 278 | $key = $kcontent[$i]; |
252 | 279 | if (is_array($content[$key])&&($hs = array_keys($content[$key]))) { |
253 | - foreach ($hs as $h) |
|
254 | - printf('<th>%s</th>',$h); |
|
280 | + foreach ($hs as $h) { |
|
281 | + printf('<th>%s</th>',$h); |
|
282 | + } |
|
255 | 283 | } |
256 | 284 | } |
257 | 285 | |
@@ -267,15 +295,16 @@ discard block |
||
267 | 295 | |
268 | 296 | function formatVal($val, $learn) { |
269 | 297 | foreach (array_keys($val) as $key) { |
270 | - if (is_array($val["$key"]) and ($key!='warn')) |
|
271 | - $val["$key"] = formatVal($val["$key"], $learn); |
|
272 | - else { |
|
298 | + if (is_array($val["$key"]) and ($key!='warn')) { |
|
299 | + $val["$key"] = formatVal($val["$key"], $learn); |
|
300 | + } else { |
|
273 | 301 | switch ($key) { |
274 | 302 | case 'warn': |
275 | - if (empty($val["$key"])) |
|
276 | - $val["$key"] = '-'; |
|
277 | - else |
|
278 | - $val["$key"] = sprintf('<div title="%s">Y</div>',implode($val["$key"],"\n")); |
|
303 | + if (empty($val["$key"])) { |
|
304 | + $val["$key"] = '-'; |
|
305 | + } else { |
|
306 | + $val["$key"] = sprintf('<div title="%s">Y</div>',implode($val["$key"],"\n")); |
|
307 | + } |
|
279 | 308 | break; |
280 | 309 | case 'learn': |
281 | 310 | $val["$key"] = formLearn($learn, $val); |
@@ -297,9 +326,10 @@ discard block |
||
297 | 326 | $par['class'] = $class; |
298 | 327 | $val["$class"] = sprintf('dspamc --user dspam --deliver=summary --class=%s --source=error --signature=%s', |
299 | 328 | strtolower($class), $par['learn']); |
300 | - if (($class != $par['type'])||($par['level']<99)) |
|
301 | - $return .= sprintf(file_get_contents('formLearnDSPAM.htm'), |
|
329 | + if (($class != $par['type'])||($par['level']<99)) { |
|
330 | + $return .= sprintf(file_get_contents('formLearnDSPAM.htm'), |
|
302 | 331 | $class,$class,$val["$class"],base64_encode(json_encode($par)),$class); |
332 | + } |
|
303 | 333 | } |
304 | 334 | default: |
305 | 335 | return $return; |
@@ -308,37 +338,41 @@ discard block |
||
308 | 338 | |
309 | 339 | function printTableRow($row, $learn, $init=true) { |
310 | 340 | $color = 'inherit'; |
311 | - if ($init) |
|
312 | - $row=formatVal($row,$learn); |
|
341 | + if ($init) { |
|
342 | + $row=formatVal($row,$learn); |
|
343 | + } |
|
313 | 344 | foreach( $row as $key => $val) { |
314 | - if (is_array($val)) |
|
315 | - printTableRow($val, $learn, false); |
|
316 | - else { |
|
345 | + if (is_array($val)) { |
|
346 | + printTableRow($val, $learn, false); |
|
347 | + } else { |
|
317 | 348 | /* DSPAM format */ |
318 | - if (isset($row['type'])) |
|
319 | - switch($row['type']) { |
|
349 | + if (isset($row['type'])) { |
|
350 | + switch($row['type']) { |
|
320 | 351 | case 'Innocent': |
321 | 352 | case 'HAM': |
322 | 353 | $color = 'rgba(0,255,0, %.1f)'; |
354 | + } |
|
323 | 355 | break; |
324 | 356 | case 'Spam': |
325 | 357 | case 'SPAM': |
326 | 358 | $color = 'rgba(255,0,0,%.1f)'; |
327 | 359 | } |
328 | 360 | /* DMARC, DKIM, SPF format */ |
329 | - if (isset($row['result'])) |
|
330 | - switch($row['result']) { |
|
361 | + if (isset($row['result'])) { |
|
362 | + switch($row['result']) { |
|
331 | 363 | case 'pass': |
332 | 364 | $color = 'rgba(0,255,0, %.1f)'; |
365 | + } |
|
333 | 366 | break; |
334 | 367 | case 'fail': |
335 | 368 | $color = 'rgba(255,0,0,%.1f)'; |
336 | 369 | } |
337 | 370 | /* Spamassassin format */ |
338 | - if (isset($row['status'])) |
|
339 | - switch($row['status']) { |
|
371 | + if (isset($row['status'])) { |
|
372 | + switch($row['status']) { |
|
340 | 373 | case 'No': |
341 | 374 | $color = 'rgba(0,255,0, %.1f)'; |
375 | + } |
|
342 | 376 | break; |
343 | 377 | case 'Yes': |
344 | 378 | $color = 'rgba(255,0,0,%.1f)'; |
@@ -26,12 +26,12 @@ |
||
26 | 26 | if ( preg_match ('/^X-DSPAM-Result:\s+(?P<user>[\w\.\@]+);\s+result="(?P<result>\w+)";\s+class="(?P<class>\w+)";\s+probability=(?P<prob>[\d\.]+);\s+confidence=(?P<conf>[\d\.]+);\s+signature=(?P<sig>[\w\,]+)$/',$out[0],$received) != 1) { |
27 | 27 | $err = 'DSPAM Client returned an unparseable result.'; |
28 | 28 | syslog(LOG_ERR, $username.': Learn Error: '.$err); |
29 | - exit (sprintf('<p>%s</p>',htmlentities($err))); |
|
29 | + exit (sprintf('<p>%s</p>',htmlentities($err))); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | printf('<p>Message learned successfully with following result:</p><ul><li>Owner: <b>%s</b></li><li>Result: <b>%s</b></li><li>Class: <b>%s</b></li></ul>', |
33 | 33 | htmlentities($received['user']), |
34 | - $received['result'], $received['class']); |
|
34 | + $received['result'], $received['class']); |
|
35 | 35 | closelog(); |
36 | 36 | ?> |
37 | 37 | </div> |