Merge branch 'xorg/maint'
[platal.git] / bin / cron / cron_validations.php
CommitLineData
ee68ddc1 1#!/usr/bin/php5 -q
0337d704 2<?php
3/***************************************************************************
c441aabe 4 * Copyright (C) 2003-2014 Polytechnique.org *
0337d704 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 ***************************************************************************/
0337d704 22
5ecaa68e
SJ
23/** Check if there is some pending validations,
24 * and if so sends notification email.
25 */
0337d704 26
5ecaa68e
SJ
27$M_PERIOD = "INTERVAL 3 HOUR"; // 3 hour lap (old validations)
28$R_PERIOD = "INTERVAL 6 HOUR"; // 6 hour lap (very old validations)
29
30require 'connect.db.inc.php';
31require 'plmailer.php';
0337d704 32
816d3bd0
FB
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)
00112b2e 35 FROM requests AS r");
816d3bd0
FB
36list($nb, $age, $nbold, $nbveryold) = $res->fetchOneRow();
37
38$age = (time() - intval($age)) / 86400;
5ecaa68e 39$head = '';
816d3bd0 40if ($age > 15) {
354adb18 41 $head = "[Autodestruction du serveur] ";
816d3bd0 42} elseif ($age > 7) {
354adb18 43 $head = "[Armageddon imminent] ";
816d3bd0 44} elseif ($age > 5) {
354adb18 45 $head = "[Guerre nucléaire] ";
816d3bd0
FB
46} elseif ($age > 3) {
47 $head = "[ET Téléphone maison] ";
48} elseif ($age > 1) {
354adb18 49 $head = "[Réveil !] ";
816d3bd0 50} elseif (!empty($nbveryold)) {
354adb18 51 $head = "[Urgent] ";
816d3bd0
FB
52}
53
0337d704 54
55if (empty($nb)) {
56 exit;
57}
58
5ecaa68e 59$plural = $nb == 1 ? '' : 's';
19f558ad 60
1e33266a 61$mymail = new PlMailer();
1d55fe45 62$mymail->setFrom('validation@' . $globals->mail->domain);
5ecaa68e 63$mymail->addTo('validation@' . $globals->mail->domain);
354adb18 64$mymail->setSubject($head . "Il y a $nb validation$plural non effectuée$plural");
0337d704 65
66$message =
354adb18 67 "Il y a $nb validation$plural à effectuer\n"
5ecaa68e
SJ
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(*)
00112b2e 74 FROM requests
9243a284 75 GROUP BY type
5ecaa68e 76 ORDER BY type');
9243a284 77while (list($type, $nb) = $res->next()) {
354adb18 78 $message .= "- $type : $nb\n";
9243a284 79}
0337d704 80
5ecaa68e 81$message = wordwrap($message, 78);
0337d704 82$mymail->setTxtBody($message);
83$mymail->send();
5ecaa68e 84
448c8cdc 85// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
0337d704 86?>