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

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

[mail_supervisor] Correction egrep bug

File size: 9.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/*
25Plugin Name: Mail supervisor
26Version: 1.2.1.a
27Description:  Mail supervisor surveille l'envoie des mails.
28Plugin URI: http://piwigo.org/ext/extension_view.php?eid=315
29Author: cljosse
30Author URI:http://cl.josse.free.fr
31*/
32
33if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
34
35if (!defined('MAIL_SUPERV_DIR')) define('MAIL_SUPERV_DIR' , basename(dirname(__FILE__)));
36if (!defined('MAIL_SUPERV_PATH')) define('MAIL_SUPERV_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
37
38include_once (MAIL_SUPERV_PATH.'include/constants.php'); 
39include_once (MAIL_SUPERV_PATH.'include/fonctions.php'); 
40
41/*
42EVENT_HANDLER_PRIORITY_NEUTRAL  */
43
44add_event_handler('send_mail', 'clj_mail',EVENT_HANDLER_PRIORITY_NEUTRAL - 1 , 6);
45
46function clj_mail($result, $mailto, $subject, $content, $headers, $args)
47{
48    global $conf, $user, $lang_info,  $conf_mail,$mails_options,$mails_données , $template,$page ,$erreur_message,$infos_message, $lang; 
49        // lecture config table -------------
50 load_language('plugin.lang', MAIL_SUPERV_PATH);
51   
52        $mails_options = explode("," , $conf['mail_superv']);
53        $message ="";
54        if (!isset($infos_message))
55        {
56        $infos_message = "";
57        }
58if (!isset($erreur_message))
59        {
60        $erreur_message = '';
61        }
62        // lecture données       -------------
63    $mails_données = Get_Datas();
64
65       
66        $nb_mails         =     $mails_données['nb_mails'] ;
67        $next_day  =  $mails_données['date_mail'] ;
68       
69        $reste =    $mails_données['reste'] ;
70       
71       
72        $rest_mail =    $mails_données['rest_mail'] ;
73        if ($rest_mail <=0 || $mails_données['nb_mails'] ==0 ){
74           $mails_données['nb_mails'] =0 ;
75           $mails_données['date_mail'] = time();
76        }
77       
78        $rest_spam =    $mails_données['rest_spam'] ;
79        if ($rest_spam <=0 || $mails_données['nb_spams'] ==0 ){
80           $mails_données['nb_spams'] =0 ;
81           $mails_données['date_spam'] = time();
82        }
83       
84        $alerte =   ( $mails_données['reste'] >0 ) ? true : false ; 
85       
86        $alerte_mail =    ( $mails_données['nb_mails'] >   $mails_données['nb_mails_maxi']-1 ) ? true : false ;
87        $alerte_spam =    (  $mails_données['nb_spams'] >   $mails_données['nb_spams_maxi']-1 ) ? true : false ;
88       
89 
90 if ( ( !$alerte  && !$alerte_spam  && !$alerte_mail ) ) 
91        {
92                 
93  $nb_destinataires=0;
94                if ($mails_options[0] == 'on') {
95                        if ($mailto == "" )     $mailto = $conf_mail['email_webmaster'];
96                }   
97 if ($mailto<>"" )            $nb_destinataires +=1; 
98
99                if (!empty($args['Bcc']))  { 
100                        $nb_destinataires += count($args['Bcc']); 
101                        } 
102                if (!empty($args['Cc']))  {   
103                        $nb_destinataires += count($args['Cc']); 
104                        }
105   //================= réécriture de l'entête ===============================
106if ( $nb_destinataires  == 1 )
107        {
108                if ($mailto == "" ) 
109                  {
110                          if (!empty($args['Cc']) and count($args['Cc']) > 0 )  $mailto  = get_strict_email_list(implode(',', $args['Cc'])) ;
111                          if (!empty($args['Bcc']) and count($args['Bcc']) > 0 ) $mailto  = get_strict_email_list(implode(',', $args['Bcc'])) ;
112                         
113                                $headers = preg_replace('/.*Bcc(.*).\n/i', '', $headers);
114                                $headers = preg_replace('/.*Cc(.*).\n/i', '', $headers);
115                               
116                  }
117        }else{
118   
119 
120     }
121
122//=============================================================================         
123if (!empty($args['Cc']))
124                        {
125                         $headers= corrige_header('Cc',$headers,$args);
126                        }
127                if (!empty($args['Bcc']))
128                        {   
129                                   $headers     = corrige_header('Bcc',$headers,$args); 
130                        }
131//=============================================================================                 
132if ($mails_options[2]=='on')   {
133
134        $texte = "This is a multi-part message in MIME format.\n"; 
135       
136        $texte .= "-----=".$conf_mail['boundary_key']."\n"; 
137        $texte .= "Ceci est un message est au format MIME.\n"; 
138        $texte .= 'Content-Type: text/plain; charset="UTF-8"'."\n"; 
139        $texte .= 'Content-Transfer-Encoding: 8bit'."\n\n"; 
140        $texte .= $mails_données['header_text']; // A voir texte brut...
141           
142                $texte .= "\n\n"; 
143       
144                $content =$texte .$content ;
145}
146 
147$ret=false;
148       
149            //======== Non mise en quarantaine =============
150                ob_start();
151                $ret = false;
152                 if ($nb_destinataires > 0) {
153                   $ret = mail($mailto, $subject, $content, $headers);
154
155                        }else{
156                           $message .='Nombre de destinataires = 0';
157                        }
158                        $message .= ob_get_contents();
159            ob_end_clean();
160                        $mails_données['message']=$message;     
161                        Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto);
162               
163       
164       
165        } else {
166
167        //================== Est en quarantaine =======================
168                $erreur_message .=      l10n('To_day_is')."<br />";
169                        if ($alerte_spam ) { 
170                        $type="Spams";
171                               
172                                $d1=$mails_données['nb_spams'] . '  \\  ' . $mails_données['nb_spams_maxi'];
173                                $d2=$mails_données['nb_spams'];
174                                $d3= $mails_données['rest_spam'];
175                                $d4=time()+$d3 ;
176                        } 
177 
178                        if ($alerte_mail) {
179                        $type="Mails";
180                       
181                                $d1=$mails_données['nb_mails']. '  \\  ' . $mails_données['nb_mails_maxi'];;
182                                $d2=$mails_données['date_mail'];
183                                $d3= $mails_données['rest_mail'];
184                                $d4=time()+$d3 ;
185                               
186                               
187                        }
188                        if ($alerte) {
189                        $type="Spams(Quarantaine)";
190                                $d1=$mails_données['nb_spams'] . '  \\  ' . $mails_données['nb_spams_maxi'];
191                                $d2=$mails_données['date_quarantaine'];
192                                $d3= $mails_données['reste'];
193                                $d4=time()+$d3 ;
194                        } 
195
196
197$d2=date(l10n('formatdate'),$d2);
198$d3=int_to_heure($d3) ;
199$d4=date(l10n('formatdate'),$d4);
200
201        if ($alerte_mail) {
202              $erreur_message .='<br />'.sprintf(l10n('alerte_mail %s %s %s %s %s'),$type,$d1,$d2,$d3,$d4).'<br />';
203                }else{
204                           $erreur_message .='<br />'.sprintf(l10n('mise_en_quarantaine %s %s %s %s %s'),$type,$d1,$d2,$d3,$d4).'<br />';
205
206                }
207 $infos_message = l10n('mail_not_send');
208 
209 }                     
210 sauve_données();
211 return true ;
212}
213//===========================================================================================   
214function Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto)
215{                       
216         global $conf, $user, $lang_info,  $conf_mail,$mails_options,$mails_données , $template,$page ,$infos_message,$erreur_message,$args; 
217    $match=array("<",">");
218    $string=array("&lt;","&gt;") ;       
219
220//Warning: mail() has been disabled for security reasons
221       
222        $est_un_spam =!(strpos(strtolower($message), 'spam') === false);
223        $headers=str_replace( $match,$string, $headers); 
224       
225        if ( ($ret==false) || ($est_un_spam==true) || ($nb_destinataires == 0) ) {
226                 ;                              // Bad recipient address syntax
227                $pos1 = strpos(strtolower($message), 'bad recipient address syntax');
228                if (  !($pos1 === false) ){
229                        $erreur_message .=l10n('Détection_de').' : Bad recipient address syntax .';
230                        $erreur_message .=l10n('Vérifiez_les_destinataires').' (Bcc,Cc,To)<br />';
231                        }       
232       
233        $erreur_message .= sprintf(l10n('debug_message %s %s %s %s %s'),($ret==true)? "true" : "false",$message,$mailto,$headers,$nb_destinataires);
234       //=============== La fonction mail à retournéeune erreur =========================               
235
236                               
237                                }       
238                               
239        if ($est_un_spam == false) 
240                        {
241                        if ($mails_données['nb_mails']==0) {    $mails_données['date_mail'] =  time();  }       
242                        $mails_données['nb_mails'] += $nb_destinataires;
243                        }
244                        else
245                        {
246                                $mails_données['nb_spams'] += $nb_destinataires;                       
247                            //--- Détection de spam ------------------
248                                if ($mails_données['nb_spams']==0) {$mails_données['date_spam'] = time();       }
249
250                                $trop_de_spam =!(strpos(strtolower($message), 'trop de spam') === false);
251                                if ( $trop_de_spam ) {
252                                                $mails_données['nb_spams_maxi']=$mails_données['nb_spams'];
253
254                                                $mails_données['date_quarantaine']= time()  ;   
255                                                $mails_données['quarantaine']= true    ;       
256                                                $infos_message .=memo_var($mails_données);
257                                        }
258
259                        }
260 
261 
262
263 
264 $ret=true;
265 return $ret ;
266 
267}
268
269//==========================================================================================
270function admin_menu($menu)
271{
272    array_push($menu, array('NAME' => 'Mail_Superv',
273      'URL' => get_admin_plugin_menu_link(MAIL_SUPERV_PATH . 'admin/mail_superv_admin.php')));
274    return $menu;
275}
276
277
278add_event_handler('get_admin_plugin_menu_links', 'admin_menu');
279//add_event_handler('loc_end_page_tail', 'affiche_message');
280add_event_handler('loc_after_page_header', 'affiche_message');
281?>
Note: See TracBrowser for help on using the repository browser.