Fixes the alias synchronization query in the gapps cronjob. It was
[platal.git] / bin / cron / google_apps.php
CommitLineData
cbfaae15
VZ
1#!/usr/bin/php5 -q
2<?php
3/***************************************************************************
5e1513f6 4 * Copyright (C) 2003-2011 Polytechnique.org *
cbfaae15
VZ
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 ***************************************************************************/
cbfaae15 22
bb0727ea
VZ
23// Updates the gapps_accounts table with Plat/al information.
24// Cleans-up the job queue, and execute post-queue hooks.
25
1bf36cd1
SJ
26require_once 'connect.db.inc.php';
27require_once 'plmailer.php';
28require_once 'googleapps.inc.php';
bb0727ea 29if (!$globals->mailstorage->googleapps_domain) {
cbfaae15
VZ
30 exit;
31}
32
33/* Updates the l_userid parameter for newer user accounts. */
bb0727ea 34$res = XDB::iterator(
337a6acf 35 "SELECT g.g_account_name, s.uid
bb0727ea 36 FROM gapps_accounts AS g
337a6acf
SJ
37 LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife')
38 WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND s.uid IS NOT NULL");
bb0727ea
VZ
39while ($account = $res->next()) {
40 XDB::execute(
41 "UPDATE gapps_accounts
42 SET l_userid = {?}
43 WHERE g_account_name = {?}",
152227e4 44 $account['uid'], $account['g_account_name']);
bb0727ea
VZ
45}
46
1bf36cd1 47/* Emits a warning for GApps accounts without local uid. */
bb0727ea
VZ
48$res = XDB::iterator(
49 "SELECT g.g_account_name
50 FROM gapps_accounts AS g
337a6acf
SJ
51 LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife')
52 WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND s.uid IS NULL");
cbfaae15 53while ($account = $res->next()) {
bb0727ea 54 if (!preg_match("/^admin-/", $account['g_account_name'])) {
1bf36cd1 55 printf("Warning: GApps account '%s' has no local uid.\n", $account['g_account_name']);
bb0727ea 56 }
cbfaae15
VZ
57}
58
65d7c623
VZ
59/* Updates the l_userid parameter for newer nicknames. */
60$res = XDB::iterator(
337a6acf 61 "SELECT g.g_account_name, s.uid
65d7c623 62 FROM gapps_nicknames AS g
337a6acf
SJ
63 LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife')
64 WHERE (g.l_userid IS NULL or g.l_userid <= 0) AND s.uid IS NOT NULL
65d7c623
VZ
65 GROUP BY g_account_name");
66while ($nickname = $res->next()) {
67 XDB::execute(
68 "UPDATE gapps_nicknames
69 SET l_userid = {?}
70 WHERE g_account_name = {?}",
152227e4 71 $nickname['uid'], $nickname['g_account_name']);
65d7c623
VZ
72}
73
1bf36cd1 74/* Emits a warning for nicknames without local uid. */
65d7c623
VZ
75$res = XDB::iterator(
76 "SELECT g.g_account_name
77 FROM gapps_nicknames AS g
337a6acf
SJ
78 LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife')
79 WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND s.uid IS NULL");
65d7c623
VZ
80while ($nickname = $res->next()) {
81 if (!preg_match("/^admin-/", $nickname['g_account_name'])) {
1bf36cd1 82 printf("Warning: Nickname '%s' has no local uid.\n", $nickname['g_account_name']);
65d7c623
VZ
83 }
84}
85
86/* Checks that all nicknames have been synchronized to GoogleApps. Creates the
87 missing ones. */
88$res = XDB::iterator(
337a6acf 89 "SELECT g.l_userid AS id, s1.email AS username, s2.email AS nickname
65d7c623 90 FROM gapps_accounts AS g
337a6acf
SJ
91 INNER JOIN email_source_account AS s1 ON (s1.uid = g.l_userid AND s1.type = 'forlife')
92 INNER JOIN email_source_account AS s2 ON (s2.uid = g.l_userid AND s2.type = 'alias')
93 LEFT JOIN gapps_nicknames AS n ON (n.l_userid = g.l_userid AND n.g_nickname = s2.email)
c9fac5cf 94 WHERE g.g_status = 'active' AND n.g_nickname IS NULL AND g.l_userid IS NOT NULL");
65d7c623
VZ
95while ($nickname = $res->next()) {
96 // Checks that the requested nickname doesn't look like a regular forlife;
97 // we might run in troubler later if we don't keep the two repos. If we need
98 // to add a forlife-looking nickname at some point, we'll do it manually.
99 if (!preg_match('/^[-a-z]+\.[-a-z]+\.\d{4}$/', $nickname['nickname'])) {
100 XDB::execute(
101 "INSERT INTO gapps_queue
102 SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
103 p_priority = 'offline', j_type = 'n_create', j_parameters = {?}",
152227e4 104 $nickname['uid'],
65d7c623
VZ
105 json_encode($nickname));
106 }
107}
108
109/* Checks that all nicknames in GoogleApps are also aliases on plat/al side.
110 Deletes the invalid ones. */
111$res = XDB::iterator(
112 "SELECT g.l_userid AS id, g.g_nickname AS nickname
113 FROM gapps_nicknames AS g
1f0d490c 114 LEFT JOIN email_source_account AS s ON (s.uid = g.l_userid AND s.type = 'alias' AND s.email = g.g_nickname)
337a6acf 115 WHERE g.l_userid IS NOT NULL AND s.email IS NULL");
65d7c623
VZ
116while ($nickname = $res->next()) {
117 XDB::execute(
118 "INSERT INTO gapps_queue
119 SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
120 p_priority = 'offline', j_type = 'n_delete', j_parameters = {?}",
152227e4 121 $nickname['uid'],
65d7c623
VZ
122 json_encode($nickname));
123}
124
bb0727ea
VZ
125/* Retrieves successful job queues for post-queue processing. */
126$res = XDB::iterator(
127 "SELECT q_id, q_recipient_id, j_type, j_parameters
128 FROM gapps_queue
129 WHERE p_status = 'success' AND q_recipient_id IS NOT NULL");
130while ($job = $res->next()) {
131 if ($job['j_type'] == 'u_create') {
132 post_queue_u_create($job);
133 } else if ($job['j_type'] == 'u_update') {
134 post_queue_u_update($job);
135 }
136}
137
138/* Removes successful jobs, and old failed jobs. */
139XDB::execute(
140 "DELETE FROM gapps_queue
141 WHERE p_status = 'success' OR
142 (p_status = 'hardfail' AND p_end_date < DATE_SUB(NOW(), INTERVAL 15 DAY))");
143
cbfaae15
VZ
144// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
145?>