Commit | Line | Data |
---|---|---|
cbfaae15 VZ |
1 | #!/usr/bin/php5 -q |
2 | <?php | |
3 | /*************************************************************************** | |
9f5bd98e | 4 | * Copyright (C) 2003-2010 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 | ||
d5500c18 VZ |
26 | require_once('connect.db.inc.php'); |
27 | require_once('plmailer.php'); | |
28 | require_once('googleapps.inc.php'); | |
bb0727ea | 29 | if (!$globals->mailstorage->googleapps_domain) { |
cbfaae15 VZ |
30 | exit; |
31 | } | |
32 | ||
33 | /* Updates the l_userid parameter for newer user accounts. */ | |
bb0727ea VZ |
34 | $res = XDB::iterator( |
35 | "SELECT g.g_account_name, a.id | |
36 | FROM gapps_accounts AS g | |
37 | LEFT JOIN aliases as a ON (a.alias = g.g_account_name AND a.type = 'a_vie') | |
38 | WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND a.id 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['id'], $account['g_account_name']); | |
45 | } | |
46 | ||
47 | /* Emits a warning for GApps accounts without local user_id. */ | |
48 | $res = XDB::iterator( | |
49 | "SELECT g.g_account_name | |
50 | FROM gapps_accounts AS g | |
51 | LEFT JOIN aliases as a ON (a.alias = g.g_account_name AND a.type = 'a_vie') | |
52 | WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND a.id IS NULL"); | |
cbfaae15 | 53 | while ($account = $res->next()) { |
bb0727ea VZ |
54 | if (!preg_match("/^admin-/", $account['g_account_name'])) { |
55 | printf("Warning: GApps account '%s' has no local user_id.\n", $account['g_account_name']); | |
56 | } | |
cbfaae15 VZ |
57 | } |
58 | ||
65d7c623 VZ |
59 | /* Updates the l_userid parameter for newer nicknames. */ |
60 | $res = XDB::iterator( | |
61 | "SELECT g.g_account_name, a.id | |
62 | FROM gapps_nicknames AS g | |
63 | LEFT JOIN aliases AS a ON (a.alias = g.g_account_name AND a.type = 'a_vie') | |
64 | WHERE (g.l_userid IS NULL or g.l_userid <= 0) AND a.id 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['id'], $nickname['g_account_name']); | |
72 | } | |
73 | ||
74 | /* Emits a warning for nicknames without local user_id. */ | |
75 | $res = XDB::iterator( | |
76 | "SELECT g.g_account_name | |
77 | FROM gapps_nicknames AS g | |
78 | LEFT JOIN aliases as a ON (a.alias = g.g_account_name AND a.type = 'a_vie') | |
79 | WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND a.id IS NULL"); | |
80 | while ($nickname = $res->next()) { | |
81 | if (!preg_match("/^admin-/", $nickname['g_account_name'])) { | |
82 | printf("Warning: Nickname '%s' has no local user_id.\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, f.alias AS username, a.alias AS nickname | |
90 | FROM gapps_accounts AS g | |
91 | INNER JOIN aliases AS f ON (f.id = g.l_userid AND f.type = 'a_vie') | |
92 | INNER JOIN aliases AS a ON (a.id = g.l_userid AND a.type = 'alias') | |
93 | LEFT JOIN gapps_nicknames AS n ON (n.l_userid = g.l_userid AND n.g_nickname = a.alias) | |
c9fac5cf | 94 | WHERE g.g_status = 'active' AND n.g_nickname IS NULL AND g.l_userid IS NOT NULL"); |
65d7c623 VZ |
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['id'], | |
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 aliases AS a ON (a.id = g.l_userid AND a.type = 'alias' AND a.alias = g.g_nickname) | |
115 | WHERE g.l_userid IS NOT NULL AND a.alias 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['id'], | |
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"); | |
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 | ||
cbfaae15 VZ |
144 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
145 | ?> |