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

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

[mail_supervisor] update design for firefox

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