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

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

[mail_supervisor] update init database.

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