Moves database verification to ut.
[platal.git] / bin / cron / checkdb.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2010 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22 /* Checks inconsistances in tables ans joins. */
23
24 require './connect.db.inc.php';
25 require 'Console/Getopt.php';
26
27 function check($sql, $comment = '')
28 {
29 $it = XDB::iterRow($sql);
30 if ($err = XDB::error()) {
31 echo $err;
32 }
33 if ($it->total() > 0) {
34 echo "Erreur pour la verification : $comment\n$sql\n\n";
35 echo "|";
36 while($col = $it->nextField()) {
37 echo "\t" . $col->name . "\t|";
38 }
39 echo "\n";
40
41 while ($arr = $it->next()) {
42 echo "|";
43 foreach ($arr as $val) {
44 echo "\t$val\t|";
45 }
46 echo "\n";
47 }
48 echo "\n";
49 }
50 }
51
52 function info($sql, $comment = '') {
53 global $opt_verbose;
54 if ($opt_verbose) {
55 check($sql, $comment);
56 }
57 }
58
59 /* Parses options. */
60 $opts = Console_GetOpt::getopt($argv, 'v');
61 $opt_verbose = false;
62
63 if (PEAR::isError($opts)) {
64 echo $opts->getMessage();
65 } else {
66 $opts = $opts[0];
67 foreach ($opts as $opt) {
68 switch ($opt[0]) {
69 case 'v':
70 $opt_verbose = true;
71 break;
72 }
73 }
74 }
75
76 /* Checks rewriting on deleted aliases. */
77 check("SELECT a.alias, e.email, e.rewrite AS broken
78 FROM aliases AS a
79 INNER JOIN emails AS e ON (a.uid = e.uid AND rewrite != '')
80 LEFT JOIN aliases AS b ON (b.uid = a.uid AND rewrite LIKE CONCAT(b.alias, '@%') AND b.type != 'homonyme')
81 WHERE a.type = 'a_vie' AND b.type IS NULL",
82 "Personnes qui ont des rewrite sur un alias perdu.");
83
84 /* Lists unsound emails that remain unprocessed by the administrators. */
85 check("SELECT a1.alias, a2.alias, e1.email, e2.flags
86 FROM emails AS e1
87 INNER JOIN emails AS e2 ON (e1.email = e2.email AND e1.uid != e2.uid AND
88 (e1.uid < e2.uid OR NOT FIND_IN_SET('active', e2.flags)))
89 INNER JOIN email_watch AS w ON (w.email = e1.email AND w.state = 'pending')
90 INNER JOIN aliases AS a1 ON (a1.uid = e1.uid AND a1.type = 'a_vie')
91 INNER JOIN aliases AS a2 ON (a2.uid = e2.uid AND a2.type = 'a_vie')
92 WHERE FIND_IN_SET('active', e1.flags)
93 ORDER BY a1.alias",
94 "Donne la liste des emails douteux actuellement non traites par les administrateurs.");
95
96 /* Lists dangerous and unsound emails. */
97 info("SELECT a1.alias, a2.alias, e1.email, e2.flags, w.state
98 FROM emails AS e1
99 INNER JOIN emails AS e2 ON (e1.email = e2.email AND e1.uid != e2.uid AND
100 (e1.uid < e2.uid OR NOT FIND_IN_SET('active', e2.flags)))
101 INNER JOIN email_watch AS w ON (w.email = e1.email AND w.state != 'safe')
102 INNER JOIN aliases AS a1 ON (a1.uid = e1.uid AND a1.type = 'a_vie')
103 INNER JOIN aliases AS a2 ON (a2.uid = e2.uid AND a2.type = 'a_vie')
104 WHERE FIND_IN_SET('active', e1.flags)
105 ORDER BY a1.alias",
106 "Donne la liste des emails dangereux ou douteux.");
107
108 /* Lists homonyms who have an alias equals to their loginbis for more than a month. */
109 check("SELECT a.alias AS username, b.alias AS loginbis, b.expire
110 FROM aliases AS a
111 INNER JOIN aliases AS b ON (a.uid=b.uid AND b.type != 'homonyme' and b.expire < NOW())
112 WHERE a.type = 'a_vie'",
113 "Donne la liste des homonymes qui ont un alias égal à leur loginbis depuis plus d'un mois, il est temps de supprimer leur alias.");
114
115 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
116 ?>