Adapts bin scripts to new mail chain.
[platal.git] / bin / cron / google_apps.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2011 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 // Updates the gapps_accounts table with Plat/al information.
24 // Cleans-up the job queue, and execute post-queue hooks.
25
26 require_once 'connect.db.inc.php';
27 require_once 'plmailer.php';
28 require_once 'googleapps.inc.php';
29 if (!$globals->mailstorage->googleapps_domain) {
30 exit;
31 }
32
33 /* Updates the l_userid parameter for newer user accounts. */
34 $res = XDB::iterator(
35 "SELECT g.g_account_name, s.uid
36 FROM gapps_accounts AS g
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");
39 while ($account = $res->next()) {
40 XDB::execute(
41 "UPDATE gapps_accounts
42 SET l_userid = {?}
43 WHERE g_account_name = {?}",
44 $account['uid'], $account['g_account_name']);
45 }
46
47 /* Emits a warning for GApps accounts without local uid. */
48 $res = XDB::iterator(
49 "SELECT g.g_account_name
50 FROM gapps_accounts AS g
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");
53 while ($account = $res->next()) {
54 if (!preg_match("/^admin-/", $account['g_account_name'])) {
55 printf("Warning: GApps account '%s' has no local uid.\n", $account['g_account_name']);
56 }
57 }
58
59 /* Updates the l_userid parameter for newer nicknames. */
60 $res = XDB::iterator(
61 "SELECT g.g_account_name, s.uid
62 FROM gapps_nicknames AS g
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
65 GROUP BY g_account_name");
66 while ($nickname = $res->next()) {
67 XDB::execute(
68 "UPDATE gapps_nicknames
69 SET l_userid = {?}
70 WHERE g_account_name = {?}",
71 $nickname['uid'], $nickname['g_account_name']);
72 }
73
74 /* Emits a warning for nicknames without local uid. */
75 $res = XDB::iterator(
76 "SELECT g.g_account_name
77 FROM gapps_nicknames AS g
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");
80 while ($nickname = $res->next()) {
81 if (!preg_match("/^admin-/", $nickname['g_account_name'])) {
82 printf("Warning: Nickname '%s' has no local uid.\n", $nickname['g_account_name']);
83 }
84 }
85
86 /* Checks that all nicknames have been synchronized to GoogleApps. Creates the
87 missing ones. */
88 $res = XDB::iterator(
89 "SELECT g.l_userid AS id, s1.email AS username, s2.email AS nickname
90 FROM gapps_accounts AS g
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)
94 WHERE g.g_status = 'active' AND n.g_nickname IS NULL AND g.l_userid IS NOT NULL");
95 while ($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 = {?}",
104 $nickname['uid'],
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
114 LEFT JOIN email_source_account AS s ON (s.uid = g.l_userid AND s.type = 'forlife' AND s.email = g.g_nickname)
115 WHERE g.l_userid IS NOT NULL AND s.email IS NULL");
116 while ($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 = {?}",
121 $nickname['uid'],
122 json_encode($nickname));
123 }
124
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");
130 while ($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. */
139 XDB::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
144 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
145 ?>