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