1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Mail supervisor |
---|
4 | Version: auto |
---|
5 | Description: Mail supervisor surveille l'envoie des mails. |
---|
6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=315 |
---|
7 | Author: cljosse |
---|
8 | Author URI:http://cljosse.free.fr |
---|
9 | */ |
---|
10 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
11 | if (!defined('mail_superv_DIR')) define('mail_superv_DIR' , basename(dirname(__FILE__))); |
---|
12 | if (!defined('MAIL_SUPERV_PATH')) define('MAIL_SUPERV_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/'); |
---|
13 | |
---|
14 | $MAIL_SUPERV_PATH_ABS = str_replace('\\','/',dirname(__FILE__) ); |
---|
15 | if (!defined('MAIL_SUPERV_PATH_ABS')) define( 'MAIL_SUPERV_PATH_ABS', $MAIL_SUPERV_PATH_ABS."/"); |
---|
16 | |
---|
17 | global $conf,$mails_options ; |
---|
18 | include_once (MAIL_SUPERV_PATH.'include/constants.php'); |
---|
19 | include_once(MAIL_SUPERV_PATH.'mail_super.inc.php'); |
---|
20 | |
---|
21 | $mail_supervisor = new mail_supervisor(); |
---|
22 | add_event_handler('get_admin_plugin_menu_links', array(&$mail_supervisor,'admin_menu') ); |
---|
23 | |
---|
24 | mail_supervisor::Get_Options($mails_options,"mail_superv"); |
---|
25 | |
---|
26 | /* EVENT_HANDLER_PRIORITY_NEUTRAL */ |
---|
27 | // |
---|
28 | // |
---|
29 | include_once (PHPWG_ROOT_PATH.'include/functions_mail.inc.php' ); |
---|
30 | include_once (MAIL_SUPERV_PATH.'include/fonctions.php'); |
---|
31 | //===============loc_begin_index |
---|
32 | //==== loc_begin_page_header |
---|
33 | //========== loc_after_page_header |
---|
34 | add_event_handler('loc_after_page_header', array(&$mail_supervisor,'init') ); |
---|
35 | add_event_handler('loc_begin_page_tail', array(&$mail_supervisor,'affiche_message') ); |
---|
36 | |
---|
37 | include_once(MAIL_SUPERV_PATH.'mail_send.inc.php'); |
---|
38 | $clj = new clj(); |
---|
39 | |
---|
40 | add_event_handler('send_mail',array(&$clj,'send_mail'),EVENT_HANDLER_PRIORITY_NEUTRAL - 1, 6); |
---|
41 | add_event_handler('send_mail_to',array(&$clj,'send_mail_to') ); |
---|
42 | add_event_handler('send_mail_subject',array(&$clj,'send_mail_subject') ); |
---|
43 | add_event_handler('send_mail_content',array(&$clj,'send_mail_content') ); |
---|
44 | add_event_handler('send_mail_headers',array(&$clj,'send_mail_headers'),EVENT_HANDLER_PRIORITY_NEUTRAL,6 ); |
---|
45 | |
---|
46 | //========================================================================================== |
---|
47 | //add_event_handler('send_mail', 'clj_send_mail',EVENT_HANDLER_PRIORITY_NEUTRAL - 10, 6); |
---|
48 | |
---|
49 | function clj_send_mail($result, $mailto, $subject, $content, $headers, $args) { |
---|
50 | global $conf, $user, $lang_info; |
---|
51 | global $conf_mail,$mails_options,$mails_donnees ; |
---|
52 | global $template,$page ,$erreur_message,$infos_message, $lang; |
---|
53 | global $errors; |
---|
54 | // lecture config table ------------- |
---|
55 | |
---|
56 | |
---|
57 | include_once (MAIL_SUPERV_PATH.'include/fonctions.php'); |
---|
58 | load_language('plugin.lang', MAIL_SUPERV_PATH); |
---|
59 | |
---|
60 | $arg_trigg= array($mailto, $subject, $content, $headers, $args); |
---|
61 | |
---|
62 | trigger_event('mail_supervisor_clj_mail',$arg_trigg); |
---|
63 | |
---|
64 | // remove_event_handler('send_mail', 'pwg_send_mail'); |
---|
65 | $message =""; |
---|
66 | if (!isset($infos_message)) { $infos_message = ""; } |
---|
67 | if (!isset($erreur_message)) { $erreur_message = ''; } |
---|
68 | |
---|
69 | // $mails_options = explode("," , $conf['mail_superv']); |
---|
70 | $envoie_ok=true ; |
---|
71 | //======== verification si envoie de mail ou blocage connexion ===================== |
---|
72 | |
---|
73 | if ($mails_options['no_mail_list']=='on' || $mails_options['no_connect'] == 'on' ) { |
---|
74 | if (kill_list()) { |
---|
75 | $nb_destinataires=0; ; |
---|
76 | $envoie_ok= false ; |
---|
77 | $erreur_message .= "<BR />".l10n('Sv_is_a_spam') ; |
---|
78 | $infos_message = l10n('Sv_mail_not_send')."<BR />"; |
---|
79 | if ($mails_options['no_connect'] == 'on' ){ |
---|
80 | if ($user['status'] == 'webmaster'){ |
---|
81 | //---------- continuer si web master ---------- |
---|
82 | }else{ |
---|
83 | trigger_event('mail_supervisor_send_mail', "DIE(KILL) : " . $ip . ' = ' . $user['status']); |
---|
84 | if (!isset($_GET['admin'])) { |
---|
85 | die('Blacklist : <br />' .$erreur_message. '<br /> Hacking attempt!'); |
---|
86 | }elseif ($_GET['admin'] != 'piwigo') |
---|
87 | { |
---|
88 | die('blacklist Hacking attempt!'); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | trigger_event('mail_supervisor_send_mail', "Kill : " . $ip . ' = ' . $user['status'] . "noconnect==off"); |
---|
93 | return true; |
---|
94 | } |
---|
95 | } |
---|
96 | // lecture donnees ------------- |
---|
97 | $mails_donnees = mail_supervisor::Get_Datas(); |
---|
98 | $nb_mails = $mails_donnees['nb_mails'] ; |
---|
99 | $next_day = $mails_donnees['date_mail'] ; |
---|
100 | $reste = $mails_donnees['reste'] ; |
---|
101 | $rest_mail = $mails_donnees['rest_mail'] ; |
---|
102 | if ($rest_mail <=0 || $mails_donnees['nb_mails'] ==0 ){ |
---|
103 | $mails_donnees['nb_mails'] =0 ; |
---|
104 | $mails_donnees['date_mail'] = time(); |
---|
105 | } |
---|
106 | $rest_spam = $mails_donnees['rest_spam'] ; |
---|
107 | if ($rest_spam <=0 || $mails_donnees['nb_spams'] ==0 ){ |
---|
108 | $mails_donnees['nb_spams'] =0 ; |
---|
109 | $mails_donnees['date_spam'] = time(); |
---|
110 | } |
---|
111 | $alerte = ( $mails_donnees['reste'] >0 ) ? true : false ; |
---|
112 | $alerte_mail = ( $mails_donnees['nb_mails'] > $mails_donnees['nb_mails_maxi']-1 ) ? true : false ; |
---|
113 | $alerte_spam = ( $mails_donnees['nb_spams'] > $mails_donnees['nb_spams_maxi']-1 ) ? true : false ; |
---|
114 | |
---|
115 | if ( ( !$alerte && !$alerte_spam && !$alerte_mail ) ) { |
---|
116 | $nb_destinataires=0; |
---|
117 | //==================================================== |
---|
118 | $match_email = $conf_mail['email_webmaster']; |
---|
119 | |
---|
120 | $new_args=array(); |
---|
121 | if( isset($args['Bcc'])) |
---|
122 | foreach($args['Bcc'] as $key => $val){ |
---|
123 | if (!preg_match('/'. $match_email.'/', $val)){ |
---|
124 | array_push($new_args, $val); |
---|
125 | }else{ |
---|
126 | $mailto = $match_email; |
---|
127 | } |
---|
128 | } |
---|
129 | $args['Bcc']=$new_args; |
---|
130 | //======================================================================= |
---|
131 | $nb_destinataires=0; |
---|
132 | if ($mailto<>"" ) $nb_destinataires +=1; |
---|
133 | if (!empty($args['Cc'])) { |
---|
134 | $nb_destinataires += count($args['Cc']); |
---|
135 | |
---|
136 | } |
---|
137 | if (!empty($args['Bcc'])) { |
---|
138 | $nb_destinataires += count($args['Bcc']); |
---|
139 | } |
---|
140 | if ($mailto == "" && $nb_destinataires==0 ) { |
---|
141 | $mailto = $conf_mail['email_webmaster']; |
---|
142 | $nb_destinataires ++; |
---|
143 | } |
---|
144 | |
---|
145 | if ($mails_options['check_mailto'] == 'on') { |
---|
146 | if ($mailto == "" && $nb_destinataires > 1 ) $mailto = $conf_mail['email_webmaster']; |
---|
147 | } |
---|
148 | |
---|
149 | $infos_message .= "nb_destinataires : $nb_destinataires Theme :" . ($args['theme'] ) ."{". $lang_info['language_name'] ."}"."<br />"; |
---|
150 | //================= réecriture de l'entete =============================== |
---|
151 | $match=array("<",">"); |
---|
152 | $string=array("<",">") ; |
---|
153 | if ( $nb_destinataires == 1 ) { |
---|
154 | if ($mailto == "" ) { |
---|
155 | if (!empty($args['Bcc'])){ |
---|
156 | $mailto = get_strict_email_list(implode(',', $args['Bcc'])); |
---|
157 | $args['Bcc']=array(); |
---|
158 | }else{ |
---|
159 | if (!empty($args['Cc']) ){ $mailto=get_strict_email_list(implode(',', $args['Cc'])); |
---|
160 | $args['Cc']=array(); |
---|
161 | }; |
---|
162 | } |
---|
163 | } |
---|
164 | if ($mailto == "" ) {$nb_destinataires = 0 ;} |
---|
165 | $mailto = $mailto ." \n" ; |
---|
166 | $headers = preg_replace('/.*Bcc(.*).\n/i', '', $headers); |
---|
167 | $headers = preg_replace('/.*Cc(.*).\n/i', '', $headers); |
---|
168 | $infos_message .="Destinataire:" .$mailto ; |
---|
169 | //============================================================================= |
---|
170 | |
---|
171 | }else{ |
---|
172 | //=== plusieurs destinataires =========== |
---|
173 | $cl_list_mail=""; |
---|
174 | if ($mailto !="") $infos_message .="To: " . $mailto . " "."<br />" ; |
---|
175 | if (!empty($args['Cc'])) { |
---|
176 | $ret= corrige_header('Cc',$headers,$args,$cl_list_mail,$mailto); |
---|
177 | $headers = $ret->headers; |
---|
178 | if($ret->list_mail!="") $infos_message .="Cc: " . ( $ret->list_mail)." <br />" ; |
---|
179 | |
---|
180 | } |
---|
181 | if (!empty($args['Bcc'])){ |
---|
182 | $ret = corrige_header('Bcc',$headers,$args,$cl_list_mail,$mailto); |
---|
183 | $headers = $ret->headers ; |
---|
184 | if($ret->list_mail!="") $infos_message .="Bcc: " . ( $ret->list_mail) ." <br />"; |
---|
185 | |
---|
186 | } |
---|
187 | $infos_message .= " " ; |
---|
188 | } |
---|
189 | |
---|
190 | //====================================================== |
---|
191 | // si adresse avec ip , certaine boite aux lettres traite ces messages |
---|
192 | // en spam (orange+windowsmail) |
---|
193 | //====================================================== |
---|
194 | if ( $mails_options['no_ipaddress'] == 'on' ){ |
---|
195 | $pattern ="/(http:\/\/)(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]|[1-9])\.){1}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9])\.){2}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]){1}))/" ; |
---|
196 | if(preg_match($pattern, $content)){ |
---|
197 | $replacement = "http://localhost"; |
---|
198 | $content = preg_replace( $pattern, $replacement, $content ); |
---|
199 | } |
---|
200 | } |
---|
201 | //====================================================== |
---|
202 | // Format TEXTE |
---|
203 | //====================================================== |
---|
204 | if($args['content_format'] == 'text/plain' && $args['email_format'] == 'text/plain'){ |
---|
205 | $pattern ="/" . preg_quote("Content-Type: multipart/alternative", '/')."/" ; |
---|
206 | $headers= preg_replace($pattern, "Content-type: text/plain",$headers); //format text |
---|
207 | $content=$args['content']; |
---|
208 | $cache_key = $args['email_format'].'-'.$lang_info['code'].'-'.$args['theme']; |
---|
209 | $content.= $conf_mail[$cache_key]['footer']; |
---|
210 | |
---|
211 | } |
---|
212 | //====================================================== |
---|
213 | // drop LF barre |
---|
214 | //====================================================== |
---|
215 | $pattern ="/" . preg_quote($pattern, '/')."/" ; |
---|
216 | $pattern="/free.fr|club/"; |
---|
217 | if( preg_match($pattern, $mailto) || preg_match($pattern, $headers) ){ |
---|
218 | $content= preg_replace('#(?<!\r)\n#si', "\r\n", $content); //drop LF barre |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | //===================================================================== |
---|
223 | if (!preg_match('/'. $conf_mail['boundary_key'] .'/', $headers)){ |
---|
224 | $erreur_message .= "<br />ERREUR manque : boundary_key dans le headers <br />"; |
---|
225 | $infos_message = l10n('Sv_mail_not_send'); |
---|
226 | $envoie_ok= false ; |
---|
227 | |
---|
228 | |
---|
229 | } |
---|
230 | //===================================================================== |
---|
231 | $ret="false"; |
---|
232 | ob_start(); |
---|
233 | //======== Non mise en quarantaine ============= |
---|
234 | //echo "warning spam" ; |
---|
235 | // echo " Warning: mail() has been disabled for security reasons"; |
---|
236 | |
---|
237 | if ($nb_destinataires > 0) { |
---|
238 | if( $envoie_ok) { |
---|
239 | $ret = mail($mailto, $subject,$content, $headers); |
---|
240 | } |
---|
241 | }else{ |
---|
242 | $message .= l10n('Sv_no_receiver_available'); |
---|
243 | } |
---|
244 | $message .= ob_get_contents(); |
---|
245 | ob_end_clean(); |
---|
246 | $arg_trigg= array("Nb destinataires: " . $nb_destinataires,"Mail to: ".$mailto,"content: ".$content, " Reponse : " . $message); |
---|
247 | trigger_event('mail_supervisor_mail', $arg_trigg ); |
---|
248 | $mails_donnees['message']=$message; |
---|
249 | $ret= Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto, $subject,$content); |
---|
250 | } else { |
---|
251 | //================== Est en quarantaine ======================= |
---|
252 | |
---|
253 | $erreur_message .= l10n('Sv_To_day_is')."<br />"; |
---|
254 | if ($alerte_spam ) { |
---|
255 | $type="Spams"; |
---|
256 | $d1=$mails_donnees['nb_spams'] . ' \\ ' . $mails_donnees['nb_spams_maxi']; |
---|
257 | $d2=$mails_donnees['nb_spams']; |
---|
258 | $d3= $mails_donnees['rest_spam']; |
---|
259 | $d4=time()+$d3 ; |
---|
260 | } |
---|
261 | if ($alerte_mail) { |
---|
262 | $type="Mails"; |
---|
263 | $d1=$mails_donnees['nb_mails']. ' \\ ' . $mails_donnees['nb_mails_maxi'];; |
---|
264 | $d2=$mails_donnees['date_mail']; |
---|
265 | $d3= $mails_donnees['rest_mail']; |
---|
266 | $d4=time()+$d3 ; |
---|
267 | } |
---|
268 | if ($alerte) { |
---|
269 | $type="Spams(Quarantaine)"; |
---|
270 | $d1=$mails_donnees['nb_spams'] . ' \\ ' . $mails_donnees['nb_spams_maxi']; |
---|
271 | $d2=$mails_donnees['date_quarantaine']; |
---|
272 | $d3= $mails_donnees['reste']; |
---|
273 | $d4=time()+$d3 ; |
---|
274 | } |
---|
275 | $d2=date(l10n('Sv_formatdate'),$d2); |
---|
276 | $d3=int_to_heure($d3) ; |
---|
277 | $d4=date(l10n('Sv_formatdate'),$d4); |
---|
278 | if ($alerte_mail) { |
---|
279 | $erreur_message .='<br />'.sprintf(l10n('Sv_alerte_mail %s %s %s %s %s'),$type,$d1,$d2,$d3,$d4).'<br />'; |
---|
280 | }else{ |
---|
281 | $erreur_message .='<br />'.sprintf(l10n('Sv_mise_en_quarantaine %s %s %s %s %s'),$type,$d1,$d2,$d3,$d4).'<br />'; |
---|
282 | } |
---|
283 | $ret = "Error ". $type." : "; |
---|
284 | $ret .= l10n('Sv_mail_not_send'); |
---|
285 | |
---|
286 | $infos_message .= l10n('Sv_mail_not_send'); |
---|
287 | } |
---|
288 | $infos_message .= "<hr>"; |
---|
289 | trigger_event('mail_supervisor_message', $infos_message ); |
---|
290 | mail_supervisor::sauve_donnees(); |
---|
291 | mail_supervisor::affiche_message(); |
---|
292 | |
---|
293 | return $ret ;// |
---|
294 | } |
---|
295 | //=========================================================================================== |
---|
296 | function Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto, $subject,$content){ |
---|
297 | global $conf, $user, $conf_mail,$mails_options,$mails_donnees , $template,$page ,$infos_message,$erreur_message,$args,$lang, $lang_info; |
---|
298 | $match=array("<",">"); |
---|
299 | $string=array("<",">") ; |
---|
300 | // $headers=str_replace( $match,$string, $headers); |
---|
301 | // Warning: mail() has been disabled for security reasons |
---|
302 | $est_un_spam =!(strpos(strtolower($message), 'spam') === false); |
---|
303 | if ( ($ret==false) || ($est_un_spam==true) || ($nb_destinataires == 0) ) { |
---|
304 | //=============== La fonction mail retourne une erreur ========================= |
---|
305 | $ret="Error"; |
---|
306 | // Bad recipient address syntax |
---|
307 | $pos1 = strpos(strtolower($message), 'bad recipient address syntax'); |
---|
308 | if ( !($pos1 === false) ){ |
---|
309 | $erreur_message .= l10n('Sv_detection_de').' Bad recipient address syntax .'; |
---|
310 | $erreur_message .= l10n('Sv_verifiez_les_destinataires').' (Bcc,Cc,To)<br />'; |
---|
311 | } |
---|
312 | $erreur_message .=$ret . " : <br />".sprintf(l10n('Sv_debug_message'), |
---|
313 | ($ret==true)? "true" : "false", |
---|
314 | htmlspecialchars($message) , |
---|
315 | htmlspecialchars( $mailto) , |
---|
316 | htmlspecialchars($subject) , |
---|
317 | htmlspecialchars($headers) , |
---|
318 | htmlspecialchars($content) , |
---|
319 | $nb_destinataires ); |
---|
320 | |
---|
321 | Mail_supervisor_log($mailto,$erreur_message) ; |
---|
322 | |
---|
323 | }else{ |
---|
324 | if( isset($mails_options['debug_mail']) && $mails_options['debug_mail']=="on") { |
---|
325 | $infos_message .="DEBUG : <br />" .sprintf(l10n('Sv_debug_message'), |
---|
326 | ($ret==true)? "true" : "false", |
---|
327 | htmlspecialchars( $message) , |
---|
328 | htmlspecialchars( $mailto) , |
---|
329 | htmlspecialchars($subject) , |
---|
330 | htmlspecialchars($headers) , |
---|
331 | htmlspecialchars($content) , |
---|
332 | $nb_destinataires ); |
---|
333 | |
---|
334 | Mail_supervisor_log($mailto,$infos_message) ; |
---|
335 | } |
---|
336 | } |
---|
337 | //================================================================= |
---|
338 | |
---|
339 | |
---|
340 | if ($est_un_spam == false){ |
---|
341 | if ($mails_donnees['nb_mails']==0) { $mails_donnees['date_mail'] = time(); } |
---|
342 | $mails_donnees['nb_mails'] += $nb_destinataires; |
---|
343 | } else { |
---|
344 | $ret="Error spam"; |
---|
345 | $mails_donnees['nb_spams'] += $nb_destinataires; |
---|
346 | //--- Detection de spam ------------------ |
---|
347 | if ($mails_donnees['nb_spams']==0) {$mails_donnees['date_spam'] = time(); } |
---|
348 | |
---|
349 | $trop_de_spam =!(strpos(strtolower($message), 'trop de spam') === false); |
---|
350 | if ( $trop_de_spam ) { |
---|
351 | $mails_donnees['nb_spams_maxi']=$mails_donnees['nb_spams']; |
---|
352 | |
---|
353 | $mails_donnees['date_quarantaine']= time() ; |
---|
354 | $mails_donnees['quarantaine']= true ; |
---|
355 | $infos_message .= mail_supervisor::Memo_Var($mails_donnees); |
---|
356 | } |
---|
357 | } |
---|
358 | return $ret ; |
---|
359 | } |
---|
360 | //============================================================ |
---|
361 | global $ms_file_log; |
---|
362 | $dir=$conf['local_data_dir'].'/Mail_supervisor_log'; |
---|
363 | if (!is_dir($dir)){ |
---|
364 | $umask = umask(0); |
---|
365 | $mkd = @mkdir($dir, 0755, true ); |
---|
366 | umask($umask); |
---|
367 | |
---|
368 | if ($mkd==false){ |
---|
369 | fatal_error( "$dir ".l10n('no write access')); |
---|
370 | return false; |
---|
371 | } |
---|
372 | $file = $dir.'/.htaccess'; |
---|
373 | @file_put_contents( $file, 'allow from all' ); |
---|
374 | |
---|
375 | } |
---|
376 | if($ms_file_log=="") $ms_file_log=$dir."/"."pw1.html"; |
---|
377 | |
---|
378 | |
---|
379 | //============================================================ |
---|
380 | function Mail_supervisor_log($label_,$lines){ |
---|
381 | global $ms_file_log,$conf; |
---|
382 | $nb_lignes=300 ; |
---|
383 | $t= var_export( $lines, true ); |
---|
384 | $s = htmlspecialchars( $lines); |
---|
385 | $s = ( $lines); |
---|
386 | if (!file_exists($ms_file_log)) { |
---|
387 | $handle = fopen($ms_file_log, 'w'); |
---|
388 | fwrite($handle, "\n"); |
---|
389 | fclose($handle); |
---|
390 | } |
---|
391 | $tableau=file($ms_file_log); |
---|
392 | $handle = fopen($ms_file_log, 'a'); |
---|
393 | $nb=count ($tableau); |
---|
394 | if($nb>$nb_lignes) { |
---|
395 | $handle = fopen($ms_file_log, 'w'); |
---|
396 | fwrite($handle, $nb. " > " . $nb_lignes . " RAZ " ."\n"); |
---|
397 | } |
---|
398 | //rewind ($handle); // Go back to the beginning |
---|
399 | //fwrite ($handle, sprintf("%5d ", $nb)); // Don't forget to increment the counter |
---|
400 | $s="\n".date('Y m d H:m:s ')."\n".$s; |
---|
401 | |
---|
402 | |
---|
403 | $s= nl2br($s); |
---|
404 | |
---|
405 | if( $s !="") fwrite($handle,$s ); |
---|
406 | fclose($handle); |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | |
---|
411 | ?> |
---|