| 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 | |
| 23 | /** Check if there is some pending validations, |
| 24 | * and if so sends notification email. |
| 25 | */ |
| 26 | |
| 27 | $M_PERIOD = "INTERVAL 3 HOUR"; // 3 hour lap (old validations) |
| 28 | $R_PERIOD = "INTERVAL 6 HOUR"; // 6 hour lap (very old validations) |
| 29 | |
| 30 | require 'connect.db.inc.php'; |
| 31 | require 'plmailer.php'; |
| 32 | |
| 33 | $res = XDB::query("SELECT count(r.stamp), UNIX_TIMESTAMP(MIN(r.stamp)), |
| 34 | sum(r.stamp < NOW() - $M_PERIOD), sum(r.stamp < NOW() - $R_PERIOD) |
| 35 | FROM requests AS r"); |
| 36 | list($nb, $age, $nbold, $nbveryold) = $res->fetchOneRow(); |
| 37 | |
| 38 | $age = (time() - intval($age)) / 86400; |
| 39 | $head = ''; |
| 40 | if ($age > 15) { |
| 41 | $head = "[Autodestruction du serveur] "; |
| 42 | } elseif ($age > 7) { |
| 43 | $head = "[Armageddon imminent] "; |
| 44 | } elseif ($age > 5) { |
| 45 | $head = "[Guerre nucléaire] "; |
| 46 | } elseif ($age > 3) { |
| 47 | $head = "[ET Téléphone maison] "; |
| 48 | } elseif ($age > 1) { |
| 49 | $head = "[Réveil !] "; |
| 50 | } elseif (!empty($nbveryold)) { |
| 51 | $head = "[Urgent] "; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | if (empty($nb)) { |
| 56 | exit; |
| 57 | } |
| 58 | |
| 59 | $plural = $nb == 1 ? '' : 's'; |
| 60 | |
| 61 | $mymail = new PlMailer(); |
| 62 | $mymail->setFrom('validation@' . $globals->mail->domain); |
| 63 | $mymail->addTo('validation@' . $globals->mail->domain); |
| 64 | $mymail->setSubject($head . "Il y a $nb validation$plural non effectuée$plural"); |
| 65 | |
| 66 | $message = |
| 67 | "Il y a $nb validation$plural à effectuer\n" |
| 68 | . (empty($nbold) ? '' : "dont $nbold depuis le dernier mail !!!\n") |
| 69 | . (empty($nbveryold) ? '' : "et dont *$nbveryold* " . ($nbveryold == 1 ? 'est' : 'sont') . ' en retard de plus de 6h !!!') |
| 70 | . "\n" |
| 71 | . "https://www.polytechnique.org/admin/validate\n\n" |
| 72 | . "Par catégorie :\n"; |
| 73 | $res = XDB::iterRow('SELECT type, count(*) |
| 74 | FROM requests |
| 75 | GROUP BY type |
| 76 | ORDER BY type'); |
| 77 | while (list($type, $nb) = $res->next()) { |
| 78 | $message .= "- $type : $nb\n"; |
| 79 | } |
| 80 | |
| 81 | $message = wordwrap($message, 78); |
| 82 | $mymail->setTxtBody($message); |
| 83 | $mymail->send(); |
| 84 | |
| 85 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 86 | ?> |