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

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

[mail_supervisor] addition of a bad book

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