3 /***************************************************************************
4 * Copyright (C) 2003-2011 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
23 // Updates the gapps_accounts table with Plat/al information.
24 // Cleans-up the job queue, and execute post-queue hooks.
26 require_once 'connect.db.inc.php';
27 require_once 'plmailer.php';
28 require_once 'googleapps.inc.php';
29 if (!$globals->mailstorage
->googleapps_domain
) {
33 /* Updates the l_userid parameter for newer user accounts. */
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()) {
41 "UPDATE gapps_accounts
43 WHERE g_account_name = {?}",
44 $account['uid'], $account['g_account_name']);
47 /* Emits a warning for GApps accounts without local uid. */
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']);
59 /* Updates the l_userid parameter for newer nicknames. */
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()) {
68 "UPDATE gapps_nicknames
70 WHERE g_account_name = {?}",
71 $nickname['uid'], $nickname['g_account_name']);
74 /* Emits a warning for nicknames without local uid. */
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']);
86 /* Checks that all nicknames have been synchronized to GoogleApps. Creates the
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 $pending_tasks = XDB
::fetchOneCell(
103 WHERE q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_create' AND j_parameters = {?}",
104 $nickname['id'], json_encode($nickname));
105 if ($pending_tasks == 0) {
107 "INSERT INTO gapps_queue
108 SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
109 p_priority = 'offline', j_type = 'n_create', j_parameters = {?}",
110 $nickname['id'], json_encode($nickname));
115 /* Checks that all nicknames in GoogleApps are also aliases on plat/al side.
116 Deletes the invalid ones. */
117 $res = XDB
::iterator(
118 "SELECT g.l_userid AS id, g.g_nickname AS nickname
119 FROM gapps_nicknames AS g
120 LEFT JOIN email_source_account AS s ON (s.uid = g.l_userid AND s.type = 'alias' AND s.email = g.g_nickname)
121 WHERE g.l_userid IS NOT NULL AND s.email IS NULL");
122 while ($nickname = $res->next()) {
123 $pending_tasks = XDB
::fetchOneCell(
126 WHERE q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_delete' AND j_parameters = {?}",
127 $nickname['id'], json_encode($nickname));
128 if ($pending_tasks == 0) {
130 "INSERT INTO gapps_queue
131 SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
132 p_priority = 'offline', j_type = 'n_delete', j_parameters = {?}",
133 $nickname['id'], json_encode($nickname));
137 /* Retrieves successful job queues for post-queue processing. */
138 $res = XDB
::iterator(
139 "SELECT q_id, q_recipient_id, j_type, j_parameters
141 WHERE p_status = 'success' AND q_recipient_id IS NOT NULL");
142 while ($job = $res->next()) {
143 if ($job['j_type'] == 'u_create') {
144 post_queue_u_create($job);
145 } else if ($job['j_type'] == 'u_update') {
146 post_queue_u_update($job);
150 /* Removes successful jobs, and old failed jobs. */
152 "DELETE FROM gapps_queue
153 WHERE p_status = 'success' OR
154 (p_status = 'hardfail' AND p_end_date < DATE_SUB(NOW(), INTERVAL 15 DAY))");
156 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: