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

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

[mail_supervisor] update design black list

File size: 17.5 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
167global $mails_options,$conf,$lang ;
168$mails_options = explode("," , $conf['mail_superv']);
169 if ( !isset($_POST['submit']) ) {
170 if ($mails_options[4] == 'on'){
171        if (kill_list())
172                 {
173                    $erreur_message .= l10n('is_a_spam') ;
174                        if ($user['status'] == 'guest')
175                                if (!isset($_GET['admin'])) {
176                                                 die('Blacklist : <br />' .$erreur_message. '<br /> Hacking attempt!');
177                                }elseif   ($_GET['admin'] != 'piwigo')
178                                        {
179                                                 die('blacklist Hacking attempt!');
180                                        }
181           }
182        }
183 }       
184
185 //==============================================================
186  if (isset($erreur_message))
187                {       
188 
189                if ($erreur_message <> "")
190                        {
191                            $erreur_message=str_replace("\n",'<br />',$erreur_message) ;
192                                $template->assign('errors',$erreur_message);
193                                $erreur_message="";
194                        }
195                }
196               
197       
198  if (isset($infos_message))
199                {       
200               
201                if ($infos_message <> "")
202                        {
203                                $infos_message=str_replace("\n",'<br />',$infos_message) ;
204                                $template->assign('infos',$infos_message);
205                                $infos_message="";
206                        }
207                }
208                //=============================================================
209               
210         
211               
212}
213
214
215
216//=================================================================================
217function Get_Datas()
218{
219        ob_start();
220                $data=Lire_datas();
221                        $m= ob_get_contents();
222        ob_end_clean();
223       
224        if (strlen($m)>0) {
225          vérif_base() ;
226          $data=Lire_datas();
227        }
228    return $data;
229}
230
231function Lire_datas()
232{
233
234
235 $query = "
236    SELECT *
237    FROM ".MAIL_SUPERV_TABLE."
238        ;";
239    $data = mysql_fetch_array(pwg_query($query),MYSQL_ASSOC);
240        //====================================================================================
241        $next_day = Str_To_Time( $data['nb_mails_periode'], $data['date_mail'] );;
242        $data['rest_mail']=    $next_day - time() ;
243
244        $next_day =  + Str_To_Time( $data['nb_spams_periode'],$data['date_spam']);
245        $data['rest_spam']=  $next_day - time() ;
246
247
248        $next_day = Str_To_Time($data['quarantaine_periode'] ,$data['date_quarantaine']);       
249       
250        if ($data['quarantaine'] == 'true' ) {
251                        $data['reste'] =   ( $next_day )-time() ;
252        }else{
253                        $data['reste'] = 0;
254        }
255       
256         return $data;
257}
258//===============================================
259
260function corrige_header($Carbonne,$headers,$args)
261        {
262        global $mails_options,$conf_mail,$mailto,$infos_message;
263
264 if (!empty($args[$Carbonne]))
265         {
266 
267                if ( count($args[$Carbonne]) > 0 ) 
268                {
269                 if ($mails_options[1] == 'on') {
270                        $list_mail  = str_replace(","," ,",get_strict_email_list(implode(',', $args[$Carbonne])))." \n" ;
271                        $headers = preg_replace('/.*'.$Carbonne.'(.*).\n/i', $Carbonne.': '.$list_mail, $headers);
272                 }
273                }
274        } else {
275        // Raz Bcc, Cc dans headers
276                        $headers = preg_replace('/.*'.$Carbonne.'(.*).\n/i', "" , $headers);
277        }
278               
279               
280return $headers;
281
282               
283        }
284       
285function int_to_heure($int)
286{
287
288        $delta=(7*24*60*60) ;
289        $week = (int) ($int/$delta); 
290        $int= $int-($week*$delta) ; 
291
292        $delta=(24*60*60) ;
293        $day= (int) ($int/$delta); 
294        $int=$int-($day*$delta);
295
296        $delta=(60*60) ;
297        $heures=(int) ($int/$delta);
298        $int=$int-($heures*$delta);
299
300        $delta=(60) ;
301        $minutes=(int) ($int/$delta);
302        $int= $int - ($minutes*$delta);
303
304        $secondes=(int) ($int);
305        $day      = substr ('00'.    $day,-2,2);   
306        $heures   = substr ('00'. $heures,-2,2);   
307        $minutes  = substr ('00'.$minutes,-2,2);
308        $secondes = substr ('00'.$secondes,-2,2);
309
310   
311if ($week > 0 )
312{
313        return $week. " " .l10n('Week'). " " .  $day . " " .l10n('Day'). "s ". $heures . " ". l10n('Hour'). "s " .    $minutes. " " .l10n('minute'). "s ".  $secondes." " .l10n('seconde')."s";
314}
315return $day . " " .l10n('Day'). "s ". $heures . " ". l10n('Hour'). "s " . $minutes. " " .l10n('Minute'). "s ".  $secondes." " .l10n('Seconde')."s";
316
317}       
318
319function memo_var($variables)
320{
321                ob_start();
322                echo '<pre>';
323                print_r($variables);
324                echo '</pre>';
325                $m= ob_get_contents();
326
327        ob_end_clean();
328                return $m;
329               
330}
331   
332
333       
334function vérif_base()
335{
336
337global $lang,$superv_champs,$superv_type,$superv_bl_champs,$superv_bl_type;
338    load_language('plugin.lang', MAIL_SUPERV_PATH);
339        create_table(MAIL_SUPERV_TABLE,$superv_champs,$superv_type) ;
340        ajust_table(MAIL_SUPERV_TABLE,$superv_champs,$superv_type)  ;
341//==============================================================================               
342    $valeurs= array (   1,
343                                                0,
344                           time(),
345                         2000,
346         '"1 '.l10n('Week').'"' ,
347                            0,
348                  time(),
349                           10,
350          '"2 '.l10n('Day').'"',
351                       '"false"',
352                time(),
353          '"2 '.l10n('Day').'"',
354       
355                      '"Init"',
356   '"'.l10n('supervisor').'"',
357        '"'.l10n('hello').'"',
358         )
359        ;
360
361        ajout_ligne(MAIL_SUPERV_TABLE,$superv_champs, $valeurs,false ) ;
362       
363   create_table(MAIL_BLACK_LISTE_TABLE,$superv_bl_champs,$superv_bl_type) ;
364        ajust_table(MAIL_BLACK_LISTE_TABLE,$superv_bl_champs,$superv_bl_type)  ;
365}
366
367
368function Get_colonnes_de($table)
369{
370  $columns_of = array();
371    $query = 'DESC '.$table.';';
372    $result = mysql_query($query);
373    $columns_of[$table] = array();
374    while ($row = mysql_fetch_row($result))
375    {
376      array_push($columns_of[$table], $row[0]);
377    }
378 
379  return $columns_of;
380}
381
382function get_liste($group_id)
383{
384global $conf ;
385
386  $query = '
387SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
388                u.'.$conf['user_fields']['username'].' AS username,
389                u.'.$conf['user_fields']['email'].' AS email,
390                ui.status,
391                ui.adviser,
392                ui.enabled_high,
393                ui.level
394  FROM '.USERS_TABLE.' AS u
395    INNER JOIN '.USER_INFOS_TABLE.' AS ui
396      ON u.'.$conf['user_fields']['id'].' = ui.user_id
397    LEFT JOIN '.USER_GROUP_TABLE.' AS ug
398      ON u.'.$conf['user_fields']['id'].' = ug.user_id
399  WHERE ug.group_id='.$group_id.' ';
400
401
402 
403 $groups=array();
404  $datas = pwg_query($query);
405 
406 
407  if (!empty($datas))
408  {
409    while ($group = mysql_fetch_array($datas,MYSQL_ASSOC))
410    {
411
412       
413      if (!empty($group['email']))
414      {
415        array_push($groups, format_email($group['username'], $group['email'] ));
416      }
417    }
418  } 
419  return $groups ;
420 
421}
422//=================================================================================
423
424function create_table($nom_table,$champs,$types)  //MAIL_SUPERV_TABLE
425{
426/*    $nb = Get_colonnes_de($nom_table);
427        $table=$nb[$nom_table];         */
428        $data =         $types ;
429        $i=0;
430        $valeurs=array();
431
432
433       
434foreach ( $champs as $champ)
435   {
436        $champ = trim($champ);
437                array_push($valeurs , " `".$champ."`" . '  ' . $data[  $i] )  ;
438                         
439                $i +=1;
440    }
441        if ( count($valeurs) == 0) return ;
442    $valeurs=implode(", ",$valeurs) ;
443        //===============================================================================
444                $query = "CREATE TABLE IF NOT EXISTS  `" . $nom_table . "` (". $valeurs . " , PRIMARY KEY  (`id` ) ) ;"; 
445                         if ( pwg_query($query) ) return ; 
446                                die($query);         
447         
448}
449function ajust_table($nom_table,$champs,$types)  //MAIL_SUPERV_TABLE
450{
451
452    $nb = Get_colonnes_de($nom_table);
453        $table=$nb[$nom_table];         
454$data =         $types ;
455$i=0;
456 $valeurs=array();
457 
458
459foreach ( $champs as $champ)
460   {
461     $champ = trim($champ);
462          if (!in_array( $champ,$table)) {
463         array_push($valeurs , " ADD `".$champ."`" . '  ' . $data[  $i] )  ;
464         }
465         $i +=1;
466         
467    }
468        if ( count($valeurs) == 0) return ;
469       
470$valeurs=implode(", ",$valeurs) ;
471        //===============================================================================
472       
473                $query = "ALTER TABLE `" . $nom_table . "` ". $valeurs ;
474                        if (   pwg_query($query)) return ;           
475       
476                die($query);
477
478
479}
480
481function ajout_ligne($nom_table,$champs,$valeurs,$force)
482{
483  if (!$force) {
484     $query = "
485                SELECT COUNT(`id`) as total
486                FROM `".$nom_table."`
487                ;";     
488//==============================================================================               
489                list($count) = mysql_fetch_row(pwg_query($query));
490}else{
491
492        $count = 0 ;
493}
494 
495  if ($count == 0)
496  {
497      $n_champs=implode(",",$champs);
498      $n_valeurs=implode(",",$valeurs);
499           $query = "
500        INSERT INTO `".$nom_table."` (". $n_champs.")
501        VALUES ( ".     $n_valeurs .")";
502               
503                if (pwg_query($query)) return ;
504                die($query);
505               
506  }
507}
508//=====================================================================
509
510function Str_To_Time( $ajout,$init_date)
511{
512//========================================================
513  global $erreur_message;
514$a_ajouter=$ajout ;
515
516        $period_search=array("W","D","H","M","S");
517                       array_push($period_search,l10n('Week').'s',l10n('Day').'s',l10n('Hour').'s',l10n('Minute').'s',l10n('Seconde').'s');
518                               array_push($period_search,l10n('Week'),l10n('Day'),l10n('Hour'),l10n('Minute'),l10n('Seconde'));
519
520
521       
522        $period_match = array(' week ',' day ',' hour ',' minute ',' seconde ');       
523                        array_push($period_match,' week ',' day ',' hour ',' minute ',' seconde ');
524                        array_push($period_match,' week ',' day ',' hour ',' minute ',' seconde ');
525       
526
527       
528$a_ajouter = "+ " .  str_replace($period_search, $period_match, $a_ajouter) ;
529$a_ajouter=sup_double_espace($a_ajouter);
530
531$v1 = array('- ','+ ','week','day','hour','minute','seconde',' -',' +',' ');
532$v2 = array('-','+', (7*24*60*60) ,  (24*60*60),  (60*60),  60,1,'|-','|+',"*" );
533$new_valeur =  str_replace($v1,$v2,$a_ajouter) ;
534$new_valeur =  str_replace('++','+',$new_valeur ) ;
535
536$new_valeur = explode( "|",$new_valeur );
537$p = $init_date ;
538
539        //$new_valeur = $a_ajouter + $init_date ;
540        foreach($new_valeur as $nv)
541        {
542        $return = matheval($nv) ;
543                 if (strpos("error",$return)===false) {
544                    $p += $return ;
545                 }else {
546                 $erreur_message .= $return . " nv : " .  $nv ;
547                 }
548                       
549         
550        }
551return  $p ;
552 
553}
554//=====================================================================================
555function matheval($equation) 
556  { 
557
558  $return="error";
559
560 
561    $equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation); 
562    $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation); 
563    $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation); 
564    $equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation); 
565       
566    if ( $equation == "" ) 
567    { 
568      $return = 0; 
569    }     else  { 
570       @eval("\$return=" . $equation . ";" ); 
571    }
572 return $return; 
573   
574  } 
575
576//======================================================================================
577function sup_double_espace($chaine){
578global $erreur_message;
579        $str_temp = $chaine." ";
580        $sep=" ";
581        $chaine = trim($chaine);
582        $existe = (strpos($sep.$sep,$chaine )===false) ;
583         while  ($existe ){
584         $str_temp = str_replace($sep.$sep, $sep, $chaine ); 
585                if($str_temp == $chaine ) return $chaine ;
586//                      $erreur_message .= memo_var($str_temp) ;
587                $chaine  = $str_temp;
588                        $existe =  (strpos($sep.$sep,$chaine )===false) ;
589                }
590        return $chaine;
591}
592
593
594
595function kill_list()
596{
597 //==================== TEST black_liste ============================================           
598     $query = "
599                SELECT *,COUNT(`id`) as total
600                FROM `".MAIL_BLACK_LISTE_TABLE."`
601                    WHERE `ip` LIKE '".$_SERVER['REMOTE_ADDR']."'
602                ;";     
603                 $datas = mysql_fetch_array(pwg_query($query),MYSQL_ASSOC);
604                 $nb = $datas['total']; 
605//==============================================================================       
606        if (  $nb > 0 )
607        {
608            $row  = mysql_fetch_row(pwg_query($query));
609            $nb =  ($row[7])+1;
610                $pays = isset($_POST['pays']) ? $_POST['pays'] : $pays="" ;
611                $ville = (isset($_POST['ville']))   ? $_POST['ville']: $ville="" ; 
612                $region =  isset($_POST['region']) ? $_POST['region'] : $region="" ;
613               
614         $query ="
615                        UPDATE " . MAIL_BLACK_LISTE_TABLE . "
616                        SET ";
617                if (isset($_POST['pays']))     
618                 $query .= "   
619                        `pays` = '". $pays ."',
620                        `region` = '" . $region ."',
621                        `ville` = '" . $ville . "',
622                        ";
623                 $query .= "           
624                        `nb` = " . $nb . "
625                          WHERE ip = '".$_SERVER['REMOTE_ADDR']."' ;
626                ";
627                                pwg_query($query );
628        //================================================================================     
629               
630        }
631return ($nb > 0) ;
632}
633
634function test_spam($ip)
635{
636$buffer="";
637
638
639if (isset($ip))
640        {
641        if (strlen($ip) > 3)
642        {
643
644        $handle =  @fopen("http://www.stopforumspam.com/api?ip=$ip","r"); 
645        if ($handle) {
646                while (!feof($handle)) { 
647                $buffer .= fgets($handle, 4096);
648                          }   
649                          fclose($handle);
650                          }
651                        return  !(strpos(  $buffer,'yes' ) === false) ;
652
653        }
654       
655         }
656          return  false;
657}
658       
659?>
Note: See TracBrowser for help on using the repository browser.