source: extensions/Mail_supervisor/main.inc.php @ 4323

Last change on this file since 4323 was 4323, checked in by cljosse, 14 years ago

[mail_supervisor] Improvement of the management of the spammers.

File size: 10.7 KB
Line 
1<?php
2/*
3Plugin Name: Mail supervisor
4Version: 1.2.7.a
5Description: Mail supervisor surveille l'envoie des mails.
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=315
7Author: cljosse
8Author URI:http://cljosse.free.fr
9*/
10if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
11if (!defined('MAIL_SUPERV_DIR')) define('MAIL_SUPERV_DIR' , basename(dirname(__FILE__)));
12if (!defined('MAIL_SUPERV_PATH')) define('MAIL_SUPERV_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
13include_once (MAIL_SUPERV_PATH.'include/constants.php'); 
14
15/* EVENT_HANDLER_PRIORITY_NEUTRAL  */
16
17
18
19global  $superv_champs , $superv_type ,$superv_bl_champs , $superv_bl_type ; 
20          $superv_champs = array(
21                       "id",
22                                        "nb_mails",
23                                        "date_mail",
24                                        "nb_mails_maxi",               
25                                        "nb_mails_periode",     
26                                        "nb_spams",
27                                        "date_spam",   
28                                        "nb_spams_maxi",       
29                                        "nb_spams_periode",     
30                                        "quarantaine",
31                                        "date_quarantaine",
32                                        "quarantaine_periode", 
33                                        "message", 
34                                        "message_test", 
35                                    "header_text",
36                               
37                                        );
38                                       
39        $superv_type = array(
40                        " SMALLINT( 5 ) NOT NULL DEFAULT '0'"      ,
41                                        "INT NOT NULL DEFAULT '0'"  ,
42                                        "INT NOT NULL DEFAULT '0'"  ,
43                                        "INT NOT NULL DEFAULT '2000'",         
44                                        "TEXT"  ,       
45                                        "INT NOT NULL DEFAULT '0'",
46                                        "INT NOT NULL DEFAULT '0'",     
47                                        "INT NOT NULL DEFAULT '10'",   
48                                        "TEXT"  ,       
49                                        "TEXT" ,
50                                        "INT NOT NULL DEFAULT '0'",
51                                        "TEXT"  ,       
52                                        "TEXT"  , 
53                                        "TEXT"  , 
54                                        "TEXT"  ,
55                                       
56                                        );             
57                                       
58 
59          $superv_bl_champs = array(
60                                        "id",
61                                        "ip",
62                                        "pays",
63                                        "region",               
64                                        "ville",       
65                                        "adresse",
66                                        "fai", 
67                                        "nb"
68                                        );
69                                       
70        $superv_bl_type = array(
71                        " smallint(5) UNSIGNED NOT NULL auto_increment ",
72                                        "TEXT"  ,
73                                        "TEXT"  ,
74                                        "TEXT"  ,
75                                        "TEXT"  ,
76                                        "TEXT"  ,
77                                        "TEXT"  ,
78                                        " smallint(5) UNSIGNED "
79                                        );                                                                                                                                     
80
81
82add_event_handler('send_mail', 'clj_mail',EVENT_HANDLER_PRIORITY_NEUTRAL - 1 , 6);
83
84function clj_mail($result, $mailto, $subject, $content, $headers, $args)
85{
86    global $conf, $user, $lang_info,  $conf_mail,$mails_options,$mails_données , $template,$page ,$erreur_message,$infos_message, $lang; 
87        // lecture config table -------------
88        include_once (MAIL_SUPERV_PATH.'include/fonctions.php'); 
89 load_language('plugin.lang', MAIL_SUPERV_PATH);
90 
91        $mails_options = explode("," , $conf['mail_superv']);
92
93        if ($mails_options[3]=='on' || $mails_options[4] == 'on' )
94        {
95                if (kill_list())
96                 {
97                  $erreur_message .= "<BR />".l10n('is_a_spam') ;
98                 if ($mails_options[4] == 'on' ){
99                        if ($user['status'] != 'webmaster'  )
100                                if (!isset($_GET['admin'])) {
101                                                 die('Blacklist : <br />' .$erreur_message. '<br /> Hacking attempt!');
102                                }elseif   ($_GET['admin'] != 'piwigo')
103                                        {
104                                                 die('blacklist Hacking attempt!');
105                                        }
106                  }
107                        return true;
108                 }
109        }
110       
111        $message ="";
112        if (!isset($infos_message))
113        {
114        $infos_message = "";
115        }
116if (!isset($erreur_message))
117        {
118        $erreur_message = '';
119        }
120        // lecture données       -------------
121    $mails_données = Get_Datas();
122
123       
124        $nb_mails         =     $mails_données['nb_mails'] ;
125        $next_day  =  $mails_données['date_mail'] ;
126       
127        $reste =    $mails_données['reste'] ;
128       
129       
130        $rest_mail =    $mails_données['rest_mail'] ;
131        if ($rest_mail <=0 || $mails_données['nb_mails'] ==0 ){
132           $mails_données['nb_mails'] =0 ;
133           $mails_données['date_mail'] = time();
134        }
135       
136        $rest_spam =    $mails_données['rest_spam'] ;
137        if ($rest_spam <=0 || $mails_données['nb_spams'] ==0 ){
138           $mails_données['nb_spams'] =0 ;
139           $mails_données['date_spam'] = time();
140        }
141       
142        $alerte =   ( $mails_données['reste'] >0 ) ? true : false ; 
143       
144        $alerte_mail =    ( $mails_données['nb_mails'] >   $mails_données['nb_mails_maxi']-1 ) ? true : false ;
145        $alerte_spam =    (  $mails_données['nb_spams'] >   $mails_données['nb_spams_maxi']-1 ) ? true : false ;
146       
147 
148 if ( ( !$alerte  && !$alerte_spam  && !$alerte_mail ) ) 
149        {
150                 
151  $nb_destinataires=0;
152                if ($mails_options[0] == 'on') {
153                        if ($mailto == "" )     $mailto = $conf_mail['email_webmaster'];
154                }   
155 if ($mailto<>"" )            $nb_destinataires +=1; 
156
157                if (!empty($args['Bcc']))  { 
158                        $nb_destinataires += count($args['Bcc']); 
159                        } 
160                if (!empty($args['Cc']))  {   
161                        $nb_destinataires += count($args['Cc']); 
162                        }
163   //================= réécriture de l'entête ===============================
164if ( $nb_destinataires  == 1 )
165        {
166                if ($mailto == "" ) 
167                  {
168                          if (!empty($args['Cc']) and count($args['Cc']) > 0 )  $mailto  = get_strict_email_list(implode(',', $args['Cc'])) ;
169                          if (!empty($args['Bcc']) and count($args['Bcc']) > 0 ) $mailto  = get_strict_email_list(implode(',', $args['Bcc'])) ;
170                         
171                                $headers = preg_replace('/.*Bcc(.*).\n/i', '', $headers);
172                                $headers = preg_replace('/.*Cc(.*).\n/i', '', $headers);
173                               
174                  }
175        }else{
176   
177 
178     }
179
180//=============================================================================         
181if (!empty($args['Cc']))
182                        {
183                         $headers= corrige_header('Cc',$headers,$args);
184                        }
185                if (!empty($args['Bcc']))
186                        {   
187                                   $headers     = corrige_header('Bcc',$headers,$args); 
188                        }
189//=============================================================================                 
190if ($mails_options[2]=='on')   {
191
192        $texte = "This is a multi-part message in MIME format.\n"; 
193       
194        $texte .= "-----=".$conf_mail['boundary_key']."\n"; 
195        $texte .= "Ceci est un message est au format MIME.\n"; 
196        $texte .= 'Content-Type: text/plain; charset="UTF-8"'."\n"; 
197        $texte .= 'Content-Transfer-Encoding: 8bit'."\n\n"; 
198        $texte .= $mails_données['header_text']; // A voir texte brut...
199           
200                $texte .= "\n\n"; 
201       
202                $content =$texte .$content ;
203}
204 
205$ret=false;
206       
207            //======== Non mise en quarantaine =============
208                ob_start();
209                $ret = false;
210                 if ($nb_destinataires > 0) {
211                   $ret = mail($mailto, $subject, $content, $headers);
212
213                        }else{
214                           $message .= l10n('no_receiver_available');
215                        }
216                        $message .= ob_get_contents();
217            ob_end_clean();
218                        $mails_données['message']=$message;     
219                        Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto);
220               
221       
222       
223        } else {
224
225        //================== Est en quarantaine =======================
226                $erreur_message .=      l10n('To_day_is')."<br />";
227                        if ($alerte_spam ) { 
228                        $type="Spams";
229                               
230                                $d1=$mails_données['nb_spams'] . '  \\  ' . $mails_données['nb_spams_maxi'];
231                                $d2=$mails_données['nb_spams'];
232                                $d3= $mails_données['rest_spam'];
233                                $d4=time()+$d3 ;
234                        } 
235 
236                        if ($alerte_mail) {
237                        $type="Mails";
238                       
239                                $d1=$mails_données['nb_mails']. '  \\  ' . $mails_données['nb_mails_maxi'];;
240                                $d2=$mails_données['date_mail'];
241                                $d3= $mails_données['rest_mail'];
242                                $d4=time()+$d3 ;
243                               
244                               
245                        }
246                        if ($alerte) {
247                        $type="Spams(Quarantaine)";
248                                $d1=$mails_données['nb_spams'] . '  \\  ' . $mails_données['nb_spams_maxi'];
249                                $d2=$mails_données['date_quarantaine'];
250                                $d3= $mails_données['reste'];
251                                $d4=time()+$d3 ;
252                        } 
253
254
255$d2=date(l10n('formatdate'),$d2);
256$d3=int_to_heure($d3) ;
257$d4=date(l10n('formatdate'),$d4);
258
259        if ($alerte_mail) {
260              $erreur_message .='<br />'.sprintf(l10n('alerte_mail %s %s %s %s %s'),$type,$d1,$d2,$d3,$d4).'<br />';
261                }else{
262                           $erreur_message .='<br />'.sprintf(l10n('mise_en_quarantaine %s %s %s %s %s'),$type,$d1,$d2,$d3,$d4).'<br />';
263
264                }
265 $infos_message = l10n('mail_not_send');
266 
267 }                     
268 sauve_données();
269 return true ;
270}
271//===========================================================================================   
272function Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto)
273{                       
274         global $conf, $user,  $conf_mail,$mails_options,$mails_données , $template,$page ,$infos_message,$erreur_message,$args,$lang, $lang_info; 
275
276         
277         
278    $match=array("<",">");
279    $string=array("&lt;","&gt;") ;       
280
281//Warning: mail() has been disabled for security reasons
282       
283        $est_un_spam =!(strpos(strtolower($message), 'spam') === false);
284        $headers=str_replace( $match,$string, $headers); 
285       
286        if ( ($ret==false) || ($est_un_spam==true) || ($nb_destinataires == 0) ) {
287                 ;                              // Bad recipient address syntax
288                $pos1 = strpos(strtolower($message), 'bad recipient address syntax');
289                if (  !($pos1 === false) ){
290                        $erreur_message .= l10n('detection_de').' : Bad recipient address syntax .';
291                        $erreur_message .= l10n('verifiez_les_destinataires').' (Bcc,Cc,To)<br />';
292                        }       
293       
294        $erreur_message .= sprintf(l10n('debug_message %s %s %s %s %s'),($ret==true)? "true" : "false",$message,$mailto,$headers,$nb_destinataires);
295       //=============== La fonction mail à retournéeune erreur =========================               
296
297                               
298                                }       
299                               
300        if ($est_un_spam == false) 
301                        {
302                        if ($mails_données['nb_mails']==0) {    $mails_données['date_mail'] =  time();  }       
303                        $mails_données['nb_mails'] += $nb_destinataires;
304                        }
305                        else
306                        {
307                                $mails_données['nb_spams'] += $nb_destinataires;                       
308                            //--- Détection de spam ------------------
309                                if ($mails_données['nb_spams']==0) {$mails_données['date_spam'] = time();       }
310
311                                $trop_de_spam =!(strpos(strtolower($message), 'trop de spam') === false);
312                                if ( $trop_de_spam ) {
313                                                $mails_données['nb_spams_maxi']=$mails_données['nb_spams'];
314
315                                                $mails_données['date_quarantaine']= time()  ;   
316                                                $mails_données['quarantaine']= true    ;       
317                                                $infos_message .=memo_var($mails_données);
318                                        }
319
320                        }
321 
322 
323
324 
325 $ret=true;
326 return $ret ;
327 
328}
329
330//==========================================================================================
331add_event_handler('get_admin_plugin_menu_links', 'admin_menu');
332function admin_menu($menu)
333{
334    array_push($menu, array('NAME' => 'Mail_Superv',
335      'URL' => get_admin_plugin_menu_link(MAIL_SUPERV_PATH . 'admin/mail_superv_admin.php')));
336    return $menu;
337}
338//==========================================================================================
339add_event_handler('loc_after_page_header','affiche_spam' );//
340function affiche_spam ()
341
342{
343include_once (MAIL_SUPERV_PATH.'include/fonctions.php'); 
344
345  affiche_message();
346   
347}
348
349//==========================================================================================
350 add_event_handler('register_user_check', 'Test_spam_g');
351function Test_spam_g ()
352{
353include_once (MAIL_SUPERV_PATH.'include/fonctions.php'); 
354
355        if ($_POST['submit'])
356                { 
357                $ip = $_SERVER['REMOTE_ADDR'] ;
358                if (test_spam($ip,$_POST['login'] ,$_POST['mail_address']))
359                 {
360                   die(' ------------ BYE! ' . $_POST['login'] . '   ---------------------');
361                 }
362           }
363
364}
365?>
Note: See TracBrowser for help on using the repository browser.