source: extensions/Mail_supervisor/include/fonctions.php @ 4150

Last change on this file since 4150 was 4150, checked in by cljosse, 15 years ago

[mail_supervisor] modification of Black list

File size: 16.7 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
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die('Hacking attempt!');
27}
28
29if (!defined('MAIL_SUPERV_PATH')) define('MAIL_SUPERV_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
30
31
32 if (!isset($_COOKIE[session_name()]))
33{
34
35
36
37}
38include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
39include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
40include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
41global $lang ;
42
43
44include_once (MAIL_SUPERV_PATH.'include/function_test_send.php'); 
45
46
47    load_language('plugin.lang', MAIL_SUPERV_PATH);
48
49
50global  $superv_champs , $superv_type ,$superv_bl_champs , $superv_bl_type ; 
51          $superv_champs = array(
52                       "id",
53                                        "nb_mails",
54                                        "date_mail",
55                                        "nb_mails_maxi",               
56                                        "nb_mails_periode",     
57                                        "nb_spams",
58                                        "date_spam",   
59                                        "nb_spams_maxi",       
60                                        "nb_spams_periode",     
61                                        "quarantaine",
62                                        "date_quarantaine",
63                                        "quarantaine_periode", 
64                                        "message", 
65                                        "message_test", 
66                                    "header_text",
67                               
68                                        );
69                                       
70        $superv_type = array(
71                        " SMALLINT( 5 ) NOT NULL DEFAULT '0'"      ,
72                                        "INT NOT NULL DEFAULT '0'"  ,
73                                        "INT NOT NULL DEFAULT '0'"  ,
74                                        "INT NOT NULL DEFAULT '2000'",         
75                                        "TEXT"  ,       
76                                        "INT NOT NULL DEFAULT '0'",
77                                        "INT NOT NULL DEFAULT '0'",     
78                                        "INT NOT NULL DEFAULT '10'",   
79                                        "TEXT"  ,       
80                                        "TEXT" ,
81                                        "INT NOT NULL DEFAULT '0'",
82                                        "TEXT"  ,       
83                                        "TEXT"  , 
84                                        "TEXT"  , 
85                                        "TEXT"  ,
86                                       
87                                        );             
88                                       
89 
90          $superv_bl_champs = array(
91                                        "id",
92                                        "ip",
93                                        "pays",
94                                        "region",               
95                                        "ville",       
96                                        "adresse",
97                                        "fai", 
98                                        "nb"
99                                        );
100                                       
101        $superv_bl_type = array(
102                        " smallint(5) UNSIGNED NOT NULL auto_increment ",
103                                        "TEXT"  ,
104                                        "TEXT"  ,
105                                        "TEXT"  ,
106                                        "TEXT"  ,
107                                        "TEXT"  ,
108                                        "TEXT"  ,
109                                        " smallint(5) UNSIGNED "
110                                        );                                                                                                                                     
111
112vérif_base() ;
113       
114//==========================================================
115function sauve_options()
116{
117global $mails_options ;
118        $query = '
119    UPDATE '.CONFIG_TABLE.'
120    SET value="'.$mails_options[0]. ',' .$mails_options[1]. ',' .$mails_options[2].',' .$mails_options[3]. ',' .$mails_options[4]. '"
121    WHERE param="mail_superv"
122    LIMIT 1';
123 pwg_query($query);
124
125}
126//=================================================================================
127function sauve_données()
128{
129global $mails_données,$superv_champs,$superv_type,$erreur_message ;
130//=====================================================================
131        ajust_table(MAIL_SUPERV_TABLE,$superv_champs,$superv_type) ;
132        $clefs = $superv_champs ;
133        $valeurs = array();
134
135foreach ( $clefs as $champ)
136   {
137     $champ = trim($champ);
138     
139         if ( isset($mails_données[  $champ ]) ) {
140           $sep= (is_string($mails_données[ $champ ])) ? '"' : '' ;
141       array_push($valeurs , "`".$champ."`" . ' = ' . $sep . $mails_données[  $champ ] . $sep)  ;
142         }else{
143         
144            array_push($valeurs , "`".$champ."`" . ' = ' .  "0"  )  ;
145         }
146    }
147$valeurs = implode(", ",$valeurs) ;
148//=====================================================================
149$query = '
150        UPDATE '.MAIL_SUPERV_TABLE.'
151        SET '. $valeurs .'
152                WHERE `id` = 1 LIMIT 1 ';
153                ;
154        ob_start();
155                $ret=  pwg_query($query)    ;
156                $m= ob_get_contents();
157        ob_end_clean() ;
158        $erreur_message .= $m ;
159
160}
161         
162//=================================================================================
163function affiche_message()
164{
165 global $template,$infos_message,$erreur_message, $user ;;
166
167       
168  if (isset($erreur_message))
169                {       
170 
171                if ($erreur_message <> "")
172                        {
173                            $erreur_message=str_replace("\n",'<br />',$erreur_message) ;
174                                $template->assign('errors',$erreur_message);
175                                $erreur_message="";
176                        }
177                }
178               
179       
180  if (isset($infos_message))
181                {       
182               
183                if ($infos_message <> "")
184                        {
185                                $infos_message=str_replace("\n",'<br />',$infos_message) ;
186                                $template->assign('infos',$infos_message);
187                                $infos_message="";
188                        }
189                }
190                               
191        if (kill_list() && $user['status'] == 'guest')
192       
193        if (!isset($_GET['admin'])) {
194          // die('blacklist Hacking attempt!');
195          }else{
196          if   ($_GET['admin'] != 'piwigo')
197                        {
198                //      die('blacklist Hacking attempt!');
199                        }
200          }
201         
202               
203}
204
205
206
207//=================================================================================
208function Get_Datas()
209{
210        ob_start();
211                $data=Lire_datas();
212                        $m= ob_get_contents();
213        ob_end_clean();
214       
215        if (strlen($m)>0) {
216          vérif_base() ;
217          $data=Lire_datas();
218        }
219    return $data;
220}
221
222function Lire_datas()
223{
224
225
226 $query = "
227    SELECT *
228    FROM ".MAIL_SUPERV_TABLE."
229        ;";
230    $data = mysql_fetch_array(pwg_query($query),MYSQL_ASSOC);
231        //====================================================================================
232        $next_day = Str_To_Time( $data['nb_mails_periode'], $data['date_mail'] );;
233        $data['rest_mail']=    $next_day - time() ;
234
235        $next_day =  + Str_To_Time( $data['nb_spams_periode'],$data['date_spam']);
236        $data['rest_spam']=  $next_day - time() ;
237
238
239        $next_day = Str_To_Time($data['quarantaine_periode'] ,$data['date_quarantaine']);       
240       
241        if ($data['quarantaine'] == 'true' ) {
242                        $data['reste'] =   ( $next_day )-time() ;
243        }else{
244                        $data['reste'] = 0;
245        }
246       
247         return $data;
248}
249//===============================================
250
251function corrige_header($Carbonne,$headers,$args)
252        {
253        global $mails_options,$conf_mail,$mailto,$infos_message;
254
255 if (!empty($args[$Carbonne]))
256         {
257 
258                if ( count($args[$Carbonne]) > 0 ) 
259                {
260                 if ($mails_options[1] == 'on') {
261                        $list_mail  = str_replace(","," ,",get_strict_email_list(implode(',', $args[$Carbonne])))." \n" ;
262                        $headers = preg_replace('/.*'.$Carbonne.'(.*).\n/i', $Carbonne.': '.$list_mail, $headers);
263                 }
264                }
265        } else {
266        // Raz Bcc, Cc dans headers
267                        $headers = preg_replace('/.*'.$Carbonne.'(.*).\n/i', "" , $headers);
268        }
269               
270               
271return $headers;
272
273               
274        }
275       
276function int_to_heure($int)
277{
278
279        $delta=(7*24*60*60) ;
280        $week = (int) ($int/$delta); 
281        $int= $int-($week*$delta) ; 
282
283        $delta=(24*60*60) ;
284        $day= (int) ($int/$delta); 
285        $int=$int-($day*$delta);
286
287        $delta=(60*60) ;
288        $heures=(int) ($int/$delta);
289        $int=$int-($heures*$delta);
290
291        $delta=(60) ;
292        $minutes=(int) ($int/$delta);
293        $int= $int - ($minutes*$delta);
294
295        $secondes=(int) ($int);
296        $day      = substr ('00'.    $day,-2,2);   
297        $heures   = substr ('00'. $heures,-2,2);   
298        $minutes  = substr ('00'.$minutes,-2,2);
299        $secondes = substr ('00'.$secondes,-2,2);
300
301   
302if ($week > 0 )
303{
304        return $week. " " .l10n('Week'). " " .  $day . " " .l10n('Day'). "s ". $heures . " ". l10n('Hour'). "s " .    $minutes. " " .l10n('minute'). "s ".  $secondes." " .l10n('seconde')."s";
305}
306return $day . " " .l10n('Day'). "s ". $heures . " ". l10n('Hour'). "s " . $minutes. " " .l10n('Minute'). "s ".  $secondes." " .l10n('Seconde')."s";
307
308}       
309
310function memo_var($variables)
311{
312                ob_start();
313                echo '<pre>';
314                print_r($variables);
315                echo '</pre>';
316                $m= ob_get_contents();
317
318        ob_end_clean();
319                return $m;
320               
321}
322   
323
324       
325function vérif_base()
326{
327
328global $lang,$superv_champs,$superv_type,$superv_bl_champs,$superv_bl_type;
329    load_language('plugin.lang', MAIL_SUPERV_PATH);
330        create_table(MAIL_SUPERV_TABLE,$superv_champs,$superv_type) ;
331        ajust_table(MAIL_SUPERV_TABLE,$superv_champs,$superv_type)  ;
332//==============================================================================               
333    $valeurs= array (   1,
334                                                0,
335                           time(),
336                         2000,
337         '"1 '.l10n('Week').'"' ,
338                            0,
339                  time(),
340                           10,
341          '"2 '.l10n('Day').'"',
342                       '"false"',
343                time(),
344          '"2 '.l10n('Day').'"',
345       
346                      '"Init"',
347   '"'.l10n('supervisor').'"',
348        '"'.l10n('hello').'"',
349         )
350        ;
351
352        ajout_ligne(MAIL_SUPERV_TABLE,$superv_champs, $valeurs,false ) ;
353       
354   create_table(MAIL_BLACK_LISTE_TABLE,$superv_bl_champs,$superv_bl_type) ;
355        ajust_table(MAIL_BLACK_LISTE_TABLE,$superv_bl_champs,$superv_bl_type)  ;
356}
357
358
359function Get_colonnes_de($table)
360{
361  $columns_of = array();
362    $query = 'DESC '.$table.';';
363    $result = mysql_query($query);
364    $columns_of[$table] = array();
365    while ($row = mysql_fetch_row($result))
366    {
367      array_push($columns_of[$table], $row[0]);
368    }
369 
370  return $columns_of;
371}
372
373function get_liste($group_id)
374{
375global $conf ;
376
377  $query = '
378SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
379                u.'.$conf['user_fields']['username'].' AS username,
380                u.'.$conf['user_fields']['email'].' AS email,
381                ui.status,
382                ui.adviser,
383                ui.enabled_high,
384                ui.level
385  FROM '.USERS_TABLE.' AS u
386    INNER JOIN '.USER_INFOS_TABLE.' AS ui
387      ON u.'.$conf['user_fields']['id'].' = ui.user_id
388    LEFT JOIN '.USER_GROUP_TABLE.' AS ug
389      ON u.'.$conf['user_fields']['id'].' = ug.user_id
390  WHERE ug.group_id='.$group_id.' ';
391
392
393 
394 $groups=array();
395  $datas = pwg_query($query);
396 
397 
398  if (!empty($datas))
399  {
400    while ($group = mysql_fetch_array($datas,MYSQL_ASSOC))
401    {
402
403       
404      if (!empty($group['email']))
405      {
406        array_push($groups, format_email($group['username'], $group['email'] ));
407      }
408    }
409  } 
410  return $groups ;
411 
412}
413//=================================================================================
414
415function create_table($nom_table,$champs,$types)  //MAIL_SUPERV_TABLE
416{
417/*    $nb = Get_colonnes_de($nom_table);
418        $table=$nb[$nom_table];         */
419        $data =         $types ;
420        $i=0;
421        $valeurs=array();
422
423
424       
425foreach ( $champs as $champ)
426   {
427        $champ = trim($champ);
428                array_push($valeurs , " `".$champ."`" . '  ' . $data[  $i] )  ;
429                         
430                $i +=1;
431    }
432        if ( count($valeurs) == 0) return ;
433    $valeurs=implode(", ",$valeurs) ;
434        //===============================================================================
435                $query = "CREATE TABLE IF NOT EXISTS  `" . $nom_table . "` (". $valeurs . " , PRIMARY KEY  (`id` ) ) ;"; 
436                         if ( pwg_query($query) ) return ; 
437                                die($query);         
438         
439}
440function ajust_table($nom_table,$champs,$types)  //MAIL_SUPERV_TABLE
441{
442
443    $nb = Get_colonnes_de($nom_table);
444        $table=$nb[$nom_table];         
445$data =         $types ;
446$i=0;
447 $valeurs=array();
448 
449
450foreach ( $champs as $champ)
451   {
452     $champ = trim($champ);
453          if (!in_array( $champ,$table)) {
454         array_push($valeurs , " ADD `".$champ."`" . '  ' . $data[  $i] )  ;
455         }
456         $i +=1;
457         
458    }
459        if ( count($valeurs) == 0) return ;
460       
461$valeurs=implode(", ",$valeurs) ;
462        //===============================================================================
463       
464                $query = "ALTER TABLE `" . $nom_table . "` ". $valeurs ;
465                        if (   pwg_query($query)) return ;           
466       
467                die($query);
468
469
470}
471
472function ajout_ligne($nom_table,$champs,$valeurs,$force)
473{
474
475       
476
477  if (!$force) {
478     $query = "
479                SELECT COUNT(`id`) as total
480                FROM `".$nom_table."`
481                ;";     
482//==============================================================================               
483                list($count) = mysql_fetch_row(pwg_query($query));
484
485}else{
486
487        $count = 0 ;
488}
489 
490  if ($count == 0)
491  {
492  $n_champs=implode(",",$champs);
493  $n_valeurs=implode(",",$valeurs);
494 
495
496       
497           $query = "
498        INSERT INTO `".$nom_table."` (". $n_champs.")
499        VALUES ( ".     $n_valeurs .")";
500               
501                if (pwg_query($query)) return ;
502                die($query);
503               
504  }
505}
506//=====================================================================
507
508function Str_To_Time( $ajout,$init_date)
509{
510//========================================================
511  global $erreur_message;
512$a_ajouter=$ajout ;
513
514        $period_search=array("W","D","H","M","S");
515                       array_push($period_search,l10n('Week').'s',l10n('Day').'s',l10n('Hour').'s',l10n('Minute').'s',l10n('Seconde').'s');
516                               array_push($period_search,l10n('Week'),l10n('Day'),l10n('Hour'),l10n('Minute'),l10n('Seconde'));
517
518
519       
520        $period_match = array(' week ',' day ',' hour ',' minute ',' seconde ');       
521                        array_push($period_match,' week ',' day ',' hour ',' minute ',' seconde ');
522                        array_push($period_match,' week ',' day ',' hour ',' minute ',' seconde ');
523       
524
525       
526$a_ajouter = "+ " .  str_replace($period_search, $period_match, $a_ajouter) ;
527$a_ajouter=sup_double_espace($a_ajouter);
528
529$v1 = array('- ','+ ','week','day','hour','minute','seconde',' -',' +',' ');
530$v2 = array('-','+', (7*24*60*60) ,  (24*60*60),  (60*60),  60,1,'|-','|+',"*" );
531$new_valeur =  str_replace($v1,$v2,$a_ajouter) ;
532$new_valeur =  str_replace('++','+',$new_valeur ) ;
533
534$new_valeur = explode( "|",$new_valeur );
535$p = $init_date ;
536
537        //$new_valeur = $a_ajouter + $init_date ;
538        foreach($new_valeur as $nv)
539        {
540        $return = matheval($nv) ;
541                 if (strpos("error",$return)===false) {
542                    $p += $return ;
543                 }else {
544                 $erreur_message .= $return . " nv : " .  $nv ;
545                 }
546                       
547         
548        }
549return  $p ;
550 
551}
552//=====================================================================================
553function matheval($equation) 
554  { 
555
556  $return="error";
557
558 
559    $equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation); 
560    $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation); 
561    $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation); 
562    $equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation); 
563       
564    if ( $equation == "" ) 
565    { 
566      $return = 0; 
567    }     else  { 
568       @eval("\$return=" . $equation . ";" ); 
569    }
570 return $return; 
571   
572  } 
573
574//======================================================================================
575function sup_double_espace($chaine){
576global $erreur_message;
577        $str_temp = $chaine." ";
578        $sep=" ";
579        $chaine = trim($chaine);
580        $existe = (strpos($sep.$sep,$chaine )===false) ;
581         while  ($existe ){
582         $str_temp = str_replace($sep.$sep, $sep, $chaine ); 
583                if($str_temp == $chaine ) return $chaine ;
584//                      $erreur_message .= memo_var($str_temp) ;
585                $chaine  = $str_temp;
586                        $existe =  (strpos($sep.$sep,$chaine )===false) ;
587                }
588        return $chaine;
589}
590
591
592
593function kill_list()
594{
595
596
597 //==================== TEST black_liste ============================================           
598     $query = "
599                SELECT *,COUNT(`id`) as total
600                FROM `".MAIL_BLACK_LISTE_TABLE."`
601                    WHERE ip='".$_SERVER['REMOTE_ADDR']."'
602                ;";     
603//==============================================================================               
604        $row = mysql_fetch_row(pwg_query($query));
605        if (count($row) > 0)
606        {
607            $nb =  ($row[7])+1;
608                $pays = isset($_POST['pays']) ? $_POST['pays'] : $pays="" ;
609                $ville = (isset($_POST['ville']))   ? $_POST['ville']: $ville="" ; 
610                $region =  isset($_POST['region']) ? $_POST['region'] : $region="" ;
611               
612         $query ="
613                        UPDATE " . MAIL_BLACK_LISTE_TABLE . "
614                        SET ";
615                if (isset($_POST['pays']))     
616                 $query .= "   
617                        `pays` = '". $pays ."',
618                        `region` = '" . $region ."',
619                        `ville` = '" . $ville . "',
620                        ";
621                 $query .= "           
622                        `nb` = " . $nb . "
623                          WHERE ip = '".$_SERVER['REMOTE_ADDR']."' ;
624                ";
625                                pwg_query($query );
626        //================================================================================     
627               
628        }
629return (count($row) > 0) ;
630}
631
632
633       
634?>
Note: See TracBrowser for help on using the repository browser.