Hide empty pages
[platal.git] / modules / admin.php
index ccb5d57..54ef3b1 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -39,9 +39,11 @@ class AdminModule extends PLModule
             'admin/skins'                  => $this->make_hook('skins', AUTH_MDP, 'admin'),
             'admin/synchro_ax'             => $this->make_hook('synchro_ax', AUTH_MDP, 'admin'),
             'admin/user'                   => $this->make_hook('user', AUTH_MDP, 'admin'),
+            'admin/promo'                  => $this->make_hook('promo', AUTH_MDP, 'admin'),
             'admin/validate'               => $this->make_hook('validate', AUTH_MDP, 'admin'),
             'admin/validate/answers'       => $this->make_hook('validate_answers', AUTH_MDP, 'admin'),
             'admin/wiki'                   => $this->make_hook('wiki', AUTH_MDP, 'admin'),
+            'admin/ipwatch'                => $this->make_hook('ipwatch', AUTH_MDP, 'admin'),
         );
     }
 
@@ -258,7 +260,7 @@ class AdminModule extends PLModule
 
             $page->assign('session', $a = $res->fetchOneAssoc());
 
-            $res = XDB::iterator('SELECT  a.text, e.data, UNIX_TIMESTAMP(e.stamp) AS stamp
+            $res = XDB::iterator('SELECT  a.text, e.data, e.stamp
                                     FROM  logger.events  AS e
                                LEFT JOIN  logger.actions AS a ON e.action=a.id
                                    WHERE  e.session={?}', $arg);
@@ -308,7 +310,7 @@ class AdminModule extends PLModule
 
                 // get the requested sessions
                 $where  = $this->_makeWhere($year, $month, $day, $loguid);
-                $select = "SELECT  s.id, UNIX_TIMESTAMP(s.start) as start, s.uid,
+                $select = "SELECT  s.id, s.start, s.uid,
                                    a.alias as username
                              FROM  logger.sessions AS s
                         LEFT JOIN  aliases         AS a  ON (a.id = s.uid AND a.type='a_vie')
@@ -340,7 +342,7 @@ class AdminModule extends PLModule
             }
         }
 
-        $page->changeTpl('logger-view.tpl');
+        $page->changeTpl('admin/logger-view.tpl');
 
         $page->assign('xorg_title','Polytechnique.org - Administration - Logs des sessions');
     }
@@ -357,16 +359,16 @@ class AdminModule extends PLModule
         }
 
         if (Env::has('user_id')) {
-            $login = get_user_login(Env::i('user_id'));
+            $login = get_user_forlife(Env::i('user_id'));
             if (empty($login)) {
                 $login = Env::i('user_id');
             }
         } elseif (Env::has('login')) {
-            $login = get_user_login(Env::v('login'));
+            $login = get_user_forlife(Env::v('login'));
         }
 
         if(Env::has('logs_button') && $login) {
-            pl_redirect("admin/logger?login=$login&year=".date('Y')."&month=".date('m'));
+            pl_redirect("admin/logger?loguser=$login&year=".date('Y')."&month=".date('m'));
         }
 
         if (Env::has('ax_button') && $login) {
@@ -385,12 +387,14 @@ class AdminModule extends PLModule
 
         if ($login) {
             if (is_numeric($login)) {
-                $r = XDB::query("SELECT *, a.alias AS forlife, u.flags AS sexe
+                $r = XDB::query("SELECT *, a.alias AS forlife, u.flags AS sexe,
+                                        (year(naissance) > promo - 15 or year(naissance) < promo - 25) AS naiss_err
                                    FROM auth_user_md5 AS u
                               LEFT JOIN aliases       AS a ON (a.id = u.user_id AND type= 'a_vie')
                                   WHERE u.user_id = {?}", $login);
             } else {
-                $r  = XDB::query("SELECT  *, a.alias AS forlife, u.flags AS sexe
+                $r  = XDB::query("SELECT  *, a.alias AS forlife, u.flags AS sexe,
+                                          (year(naissance) > promo - 15 or year(naissance) < promo - 25) AS naiss_err
                                     FROM  auth_user_md5 AS u
                               INNER JOIN  aliases       AS a ON ( a.id = u.user_id AND a.alias={?} AND type!='homonyme' )", $login);
             }   
@@ -484,8 +488,7 @@ class AdminModule extends PLModule
                     if (XDB::execute($query)) {
                             user_reindex($mr['user_id']);
 
-                            require_once("diogenes/diogenes.hermes.inc.php");
-                            $mailer = new HermesMailer();
+                            $mailer = new PlMailer();
                             $mailer->setFrom("webmaster@polytechnique.org");
                             $mailer->addTo("web@polytechnique.org");
                             $mailer->setSubject("INTERVENTION de ".S::v('forlife'));
@@ -497,6 +500,9 @@ class AdminModule extends PLModule
                         if (Env::v('nomusageN') != $mr['nom_usage']) {
                             set_new_usage($mr['user_id'], Env::v('nomusageN'), make_username(Env::v('prenomN'), Env::v('nomusageN')));
                         }
+                        if (Env::v('decesN') != $mr['deces']) {
+                            user_clear_all_subs($mr['user_id'], false);
+                        }
                         $r = XDB::query("SELECT *, a.alias AS forlife, u.flags AS sexe
                                            FROM auth_user_md5 AS u
                                       LEFT JOIN aliases       AS a ON (a.id = u.user_id AND type= 'a_vie')
@@ -508,26 +514,33 @@ class AdminModule extends PLModule
                     case "u_kill":
                         user_clear_all_subs($mr['user_id']);
                         $page->trig("'{$mr['user_id']}' a été désinscrit !");
-                        require_once("diogenes/diogenes.hermes.inc.php");
-                        $mailer = new HermesMailer();
+                        $mailer = new PlMailer();
                         $mailer->setFrom("webmaster@polytechnique.org");
                         $mailer->addTo("web@polytechnique.org");
                         $mailer->setSubject("INTERVENTION de ".S::v('forlife'));
-                        $mailer->setTxtBody("\nUtilisateur $login effacé");
+                        $mailer->setTxtBody("\nUtilisateur $login désinscrit");
                         $mailer->send();
                         break;
                 }
             }
 
-            $res = XDB::query("SELECT  UNIX_TIMESTAMP(start), host
-                                           FROM  logger.sessions
-                                          WHERE  uid={?} AND suid=0
-                                       ORDER BY  start DESC
-                                          LIMIT  1", $mr['user_id']);
+            $res = XDB::query("SELECT  start, host
+                                 FROM  logger.sessions
+                                WHERE  uid={?} AND suid=0
+                             ORDER BY  start DESC
+                                LIMIT  1", $mr['user_id']);
             list($lastlogin,$host) = $res->fetchOneRow();
             $page->assign('lastlogin', $lastlogin);
             $page->assign('host', $host);
 
+            $res = XDB::query("SELECT  alias
+                                 FROM  virtual
+                           INNER JOIN  virtual_redirect USING(vid)
+                                WHERE  type = 'user' AND redirect LIKE '" . $login . "@%'");
+            if ($res->numRows()) {
+                $page->assign('virtual', $res->fetchOneCell());
+            }
+
             $page->assign('aliases', XDB::iterator(
                         "SELECT  alias, type='a_vie' AS for_life,FIND_IN_SET('bestalias',flags) AS best,expire
                            FROM  aliases
@@ -540,6 +553,55 @@ class AdminModule extends PLModule
             $page->assign('mr',$mr);
         }
     }
+
+    function getMatricule($line, $key)
+    {
+        $mat = $line['matricule'];
+        $year = intval(substr($mat, 0, 3));
+        $rang = intval(substr($mat, 3, 3));
+        if ($year > 200) { $year /= 10; };
+        if ($year < 96) {
+            return null;
+        } else {
+            return sprintf('%04u%04u', 1900+$year, $rang);
+        }
+    }
+
+    function handler_promo(&$page, $action = null, $promo = null)
+    {
+        if (Env::has('promo')) {
+            if(Env::i('promo') > 1900 && Env::i('promo') < 2050) {
+                $action = Env::v('valid_promo') == 'Ajouter des membres' ? 'add' : 'ax'; 
+                pl_redirect('admin/promo/' . $action . '/' . Env::i('promo'));
+            } else {
+                $page->trig('Promo non valide');
+            }
+        }
+
+        $page->changeTpl('admin/promo.tpl');
+        if ($promo > 1900 && $promo < 2050 && ($action == 'add' || $action == 'ax')) {
+            $page->assign('promo', $promo);
+        } else {
+            return;
+        }
+
+        $importer = new CSVImporter('auth_user_md5', 'matricule');
+        $importer->registerFunction('matricule', 'matricle Ecole vers X.org', array($this, 'getMatricule'));
+        switch ($action) {
+          case 'add':
+            $fields = array('nom', 'nom_ini', 'prenom',
+                            'prenom_ini', 'promo', 'promo_sortie', 'flags',
+                            'matricule', 'matricule_ax', 'perms');
+            $importer->forceValue('promo', $promo);
+            $importer->forceValue('promo_sortie', $promo + 3);
+            break;
+          case 'ax':
+            $fields = array('matricule', 'matricule_ax');
+            break;
+        }
+        $importer->apply($page, "admin/promo/$action/$promo", $fields);
+    }
+
     function handler_homonyms(&$page, $op = 'list', $target = null) {
         $page->changeTpl('admin/homonymes.tpl');
         $page->assign('xorg_title','Polytechnique.org - Administration - Homonymes');
@@ -565,15 +627,15 @@ class AdminModule extends PLModule
             // on examine l'op a effectuer
             switch ($op) {
                 case 'mail':
-                   send_warning_homonyme($prenom, $nom, $forlife, $loginbis);
-                   switch_bestalias($target, $loginbis);
+                send_warning_homonyme($prenom, $nom, $forlife, $loginbis);
+                switch_bestalias($target, $loginbis);
                     $op = 'list';
                     break;
                 case 'correct':
-                   switch_bestalias($target, $loginbis);
+                switch_bestalias($target, $loginbis);
                     XDB::execute("UPDATE aliases SET type='homonyme',expire=NOW() WHERE alias={?}", $loginbis);
                     XDB::execute("REPLACE INTO homonymes (homonyme_id,user_id) VALUES({?},{?})", $target, $target);
-                   send_robot_homonyme($prenom, $nom, $forlife, $loginbis);
+                send_robot_homonyme($prenom, $nom, $forlife, $loginbis);
                     $op = 'list';
                     break;
             }
@@ -645,15 +707,15 @@ class AdminModule extends PLModule
             $res = XDB::iterRow("SELECT user_id,matricule,nom,prenom,deces FROM auth_user_md5 WHERE promo = {?}", $promo);
             while (list($uid,$mat,$nom,$prenom,$deces) = $res->next()) {
                 $val = Env::v($mat);
-               if($val == $deces || empty($val)) continue;
-               XDB::execute('UPDATE auth_user_md5 SET deces={?} WHERE matricule = {?}', $val, $mat);
-               $new_deces[] = array('name' => "$prenom $nom", 'date' => "$val");
-               if($deces=='0000-00-00' or empty($deces)) {
-                   require_once('notifs.inc.php');
-                   register_watch_op($uid, WATCH_DEATH, $val);
-                   require_once('user.func.inc.php');
-                   user_clear_all_subs($uid, false);   // by default, dead ppl do not loose their email
-               }
+            if($val == $deces || empty($val)) continue;
+            XDB::execute('UPDATE auth_user_md5 SET deces={?} WHERE matricule = {?}', $val, $mat);
+            $new_deces[] = array('name' => "$prenom $nom", 'date' => "$val");
+            if($deces=='0000-00-00' or empty($deces)) {
+                require_once('notifs.inc.php');
+                register_watch_op($uid, WATCH_DEATH, $val);
+                require_once('user.func.inc.php');
+                user_clear_all_subs($uid, false);   // by default, dead ppl do not loose their email
+            }
             }
             $page->assign('new_deces',$new_deces);
         }
@@ -799,7 +861,9 @@ class AdminModule extends PLModule
         $table_editor->describe('description','description',false);
         $table_editor->apply($page, $action, $id);
     }
-    function handler_wiki(&$page, $action='list') {
+
+    function handler_wiki(&$page, $action='list')
+    {
         require_once 'wiki.inc.php';
 
         // update wiki perms
@@ -835,11 +899,116 @@ class AdminModule extends PLModule
             }
         }
         ksort($wiki_pages);
+        $wiki_tree = array();
+        foreach ($wiki_pages as $file => $desc) {
+            list($cat, $name) = explode('.', $file);
+            if (!isset($wiki_tree[$cat])) {
+                $wiki_tree[$cat] = array();
+            }
+            $wiki_tree[$cat][$name] = $desc;
+        }
+
+
 
         $page->changeTpl('admin/wiki.tpl');
-        $page->assign('wiki_pages', $wiki_pages);
+        $page->assign('wiki_pages', $wiki_tree);
         $page->assign('perms_opts', $perms);
     }
+
+    function handler_ipwatch(&$page, $action = 'list', $ip = null)
+    { 
+        $page->changeTpl('admin/ipwatcher.tpl');
+            
+        $states = array('safe'      => 'Ne pas surveiller',
+                        'unsafe'    => 'Surveiller les inscription',
+                        'dangerous' => 'Surveiller tous les accès',
+                        'ban'       => 'Bannir cette adresse');
+        $page->assign('states', $states);
+
+        switch (Post::v('action')) {
+        case 'create':
+            if (trim(Post::v('ipN')) != '') {
+                Xdb::execute('INSERT IGNORE INTO ip_watch (ip, state, detection, last, uid, description)
+                                          VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
+                             trim(Post::v('ipN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
+            };      
+            break;  
+                                   
+        case 'edit':               
+            Xdb::execute('UPDATE ip_watch 
+                             SET state = {?}, last = NOW(), uid = {?}, description = {?}
+                           WHERE ip = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('ipN'));
+            break;
+            
+        default:
+            if ($action == 'delete' && !is_null($ip)) {
+                Xdb::execute('DELETE FROM emails_watch WHERE ip = {?}', $ip);
+            }
+        }
+        if ($action != 'create' && $action != 'edit') {
+            $action = 'list';
+        }
+        $page->assign('action', $action);
+
+        if ($action == 'list') {
+            $sql = "SELECT  w.ip, s.host, w.detection, w.state, a.alias AS forlife
+                      FROM  ip_watch        AS w
+                 LEFT JOIN  logger.sessions AS s USING(ip)
+                 LEFT JOIN  aliases         AS a ON (a.id = s.uid AND a.type = 'a_vie')
+                  GROUP BY  w.ip, a.alias
+                  ORDER BY  w.state, w.ip, a.alias";
+            $it = Xdb::iterRow($sql);
+
+            $table = array();
+            $props = array();
+            while (list($ip, $host, $date, $state, $forlife) = $it->next()) {
+                if (count($props) == 0 || $props['ip'] != $ip) {
+                    if (count($props) > 0) {
+                        $table[] = $props;
+                    }
+                    $props = array('ip'        => $ip,
+                                   'host'      => $host,
+                                   'detection' => $date,
+                                   'state'     => $state,
+                                   'users'     => array($forlife));
+                } else {
+                    $props['users'][] = $forlife;
+                }
+            }
+            if (count($props) > 0) {
+                $table[] = $props;
+            }
+            $page->assign('table', $table);
+        } elseif ($action == 'edit') {
+            $sql = "SELECT  w.detection, w.state, w.last, w.description,
+                            a1.alias AS edit, a2.alias AS forlife, s.host
+                      FROM  ip_watch        AS w
+                 LEFT JOIN  aliases         AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')     
+                 LEFT JOIN  logger.sessions AS s  ON (w.ip = s.ip)
+                 LEFT JOIN  aliases         AS a2 ON (a2.id = s.uid AND a2.type = 'a_vie')
+                     WHERE  w.ip = {?}
+                  GROUP BY  a2.alias
+                  ORDER BY  a2.alias";
+            $it = Xdb::iterRow($sql, $ip);
+
+            $props = array();
+            while (list($detection, $state, $last, $description, $edit, $forlife, $host) = $it->next()) {
+                if (count($props) == 0) {
+                    $props = array('ip'          => $ip,
+                                   'host'        => $host,
+                                   'detection'   => $detection,
+                                   'state'       => $state,
+                                   'last'        => $last,
+                                   'description' => $description,
+                                   'edit'        => $edit,
+                                   'users'       => array($forlife));
+                } else {
+                    $props['users'][] = $forlife;
+                }
+            }
+            $page->assign('ip', $props);
+        }
+    }
 }
 
 ?>