Commit | Line | Data |
---|---|---|
bb0727ea VZ |
1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
bb0727ea VZ |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | class GoogleAppsModule extends PLModule | |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | global $globals; | |
27 | if (!$globals->mailstorage->googleapps_domain) { | |
28 | return array(); | |
29 | } | |
30 | ||
31 | return array( | |
6f2f6c83 | 32 | 'googleapps' => $this->make_hook('index', AUTH_MDP, 'mail'), |
eb5a266d SJ |
33 | 'admin/googleapps' => $this->make_hook('admin', AUTH_MDP, 'admin'), |
34 | 'admin/googleapps/job' => $this->make_hook('admin_job', AUTH_MDP, 'admin'), | |
0089e594 | 35 | 'admin/googleapps/user' => $this->make_hook('admin_user', AUTH_MDP, 'admin'), |
bb0727ea VZ |
36 | ); |
37 | } | |
38 | ||
26ba053e | 39 | function handler_index($page, $action = null) |
bb0727ea VZ |
40 | { |
41 | require_once("emails.inc.php"); | |
42 | require_once("googleapps.inc.php"); | |
43 | $page->changeTpl('googleapps/index.tpl'); | |
46f272fe | 44 | $page->setTitle('Compte Google Apps'); |
bb0727ea | 45 | |
88cddbaa VZ |
46 | $user = S::user(); |
47 | $account = new GoogleAppsAccount($user); | |
bb0727ea VZ |
48 | |
49 | // Fills up the 'is Google Apps redirection active' variable. | |
50 | $page->assign('redirect_active', false); | |
51 | $page->assign('redirect_unique', true); | |
52 | ||
f5c4bf30 | 53 | if ($account->active()) { |
88cddbaa | 54 | $redirect = new Redirect($user); |
afde0a3a | 55 | $page->assign('redirect_unique', !$redirect->other_active('googleapps')); |
bb0727ea | 56 | |
88cddbaa | 57 | $storage = new EmailStorage($user, 'googleapps'); |
afde0a3a | 58 | $page->assign('redirect_active', $storage->active); |
bb0727ea VZ |
59 | } |
60 | ||
61 | // Updates the Google Apps account as required. | |
62 | if ($action) { | |
fc6f30c0 VZ |
63 | if ($action == 'password' && Post::has('pwsync')) { |
64 | S::assert_xsrf_token(); | |
65 | if (Post::v('pwsync') == 'sync') { | |
bb0727ea | 66 | $account->set_password_sync(true); |
84270653 | 67 | $account->set_password(S::v('password')); |
fc6f30c0 | 68 | } else { |
bb0727ea | 69 | $account->set_password_sync(false); |
bb0727ea | 70 | } |
81b5a6c9 | 71 | } elseif ($action == 'password' && Post::has('pwhash') && Post::t('pwhash') && !$account->sync_password) { |
fc6f30c0 | 72 | S::assert_xsrf_token(); |
81b5a6c9 | 73 | $account->set_password(Post::t('pwhash')); |
bb0727ea VZ |
74 | } |
75 | ||
f5c4bf30 | 76 | if ($action == 'suspend' && Post::has('suspend') && $account->active()) { |
40d428d8 VZ |
77 | S::assert_xsrf_token(); |
78 | ||
79 | if ($account->pending_update_suspension) { | |
a7d35093 | 80 | $page->trigWarning("Ton compte est déjà en cours de désactivation."); |
bb0727ea | 81 | } else { |
afde0a3a | 82 | if ($redirect->modify_one_email('googleapps', false) == SUCCESS) { |
bb0727ea | 83 | $account->suspend(); |
a7d35093 | 84 | $page->trigSuccess("Ton compte Google Apps est dorénavant désactivé."); |
bb0727ea | 85 | } else { |
a7d35093 | 86 | $page->trigError("Ton compte Google Apps est ta seule adresse de redirection. Ton compte ne peux pas être désactivé."); |
bb0727ea VZ |
87 | } |
88 | } | |
f5c4bf30 | 89 | } elseif ($action == 'unsuspend' && Post::has('unsuspend') && $account->suspended()) { |
bb0727ea | 90 | $account->unsuspend(Post::b('redirect_mails', true)); |
a7d35093 | 91 | $page->trigSuccess("Ta demande de réactivation a bien été prise en compte."); |
bb0727ea VZ |
92 | } |
93 | ||
94 | if ($action == 'create') { | |
95 | $page->assign('has_password_sync', Get::has('password_sync')); | |
96 | $page->assign('password_sync', Get::b('password_sync', true)); | |
97 | } | |
98 | if ($action == 'create' && Post::has('password_sync') && Post::has('redirect_mails')) { | |
40d428d8 VZ |
99 | S::assert_xsrf_token(); |
100 | ||
bb0727ea VZ |
101 | $password_sync = Post::b('password_sync'); |
102 | $redirect_mails = Post::b('redirect_mails'); | |
103 | if ($password_sync) { | |
47d7e105 | 104 | $password = $user->password(); |
bb0727ea | 105 | } else { |
81b5a6c9 | 106 | $password = Post::t('pwhash'); |
bb0727ea VZ |
107 | } |
108 | ||
40d428d8 | 109 | $account->create($password_sync, $password, $redirect_mails); |
a7d35093 | 110 | $page->trigSuccess("La demande de création de ton compte Google Apps a bien été enregistrée."); |
bb0727ea VZ |
111 | } |
112 | } | |
113 | ||
114 | $page->assign('account', $account); | |
115 | } | |
0089e594 | 116 | |
26ba053e | 117 | function handler_admin($page, $action = null) { |
0089e594 VZ |
118 | require_once("googleapps.inc.php"); |
119 | $page->changeTpl('googleapps/admin.tpl'); | |
46f272fe | 120 | $page->setTitle('Administration Google Apps'); |
0089e594 VZ |
121 | $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid'))); |
122 | ||
123 | if ($action == 'ack') { | |
124 | $qid = @func_get_arg(2); | |
125 | if ($qid) { | |
126 | XDB::execute( | |
127 | "DELETE FROM gapps_queue | |
128 | WHERE q_id = {?} AND p_status = 'hardfail'", $qid); | |
a7d35093 | 129 | $page->trigSuccess("La requête échouée a bien été retirée."); |
0089e594 VZ |
130 | } |
131 | } | |
132 | ||
133 | // Retrieves latest pending administrative requests from the gappsd queue. | |
134 | $res = XDB::iterator( | |
135 | "SELECT q_id, q_recipient_id, a.alias, j_type, j_parameters, | |
136 | UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date | |
137 | FROM gapps_queue AS q | |
fe13bc1d | 138 | LEFT JOIN aliases AS a ON (a.uid = q_recipient_id AND a.type = 'a_vie') |
0089e594 VZ |
139 | WHERE p_status IN ('idle', 'active', 'softfail') AND |
140 | p_admin_request IS TRUE | |
141 | ORDER BY p_entry_date"); | |
142 | while ($request = $res->next()) { | |
143 | $j_parameters = json_decode($request['j_parameters'], true); | |
144 | unset($j_parameters['username']); | |
145 | $parameters = array_keys($j_parameters); | |
146 | $request['parameters'] = implode(', ', $parameters); | |
147 | ||
148 | $page->append('admin_requests', $request); | |
149 | } | |
150 | ||
151 | // Retrieves latest failed requests from the gappsd queue. | |
152 | $res = XDB::iterator( | |
153 | "SELECT q.q_id, q.q_recipient_id, a.alias, q.j_type, q.r_result, | |
154 | UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date | |
155 | FROM gapps_queue AS q | |
fe13bc1d | 156 | LEFT JOIN aliases AS a ON (a.uid = q.q_recipient_id AND a.type = 'a_vie') |
0089e594 VZ |
157 | WHERE q.p_status = 'hardfail' |
158 | ORDER BY p_entry_date DESC | |
159 | LIMIT 20"); | |
160 | $page->assign('failed_requests', $res); | |
161 | } | |
162 | ||
26ba053e | 163 | function handler_admin_job($page, $job = null) { |
0089e594 VZ |
164 | require_once("googleapps.inc.php"); |
165 | $page->changeTpl('googleapps/admin.job.tpl'); | |
46f272fe | 166 | $page->setTitle('Administration Google Apps'); |
0089e594 VZ |
167 | $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid'))); |
168 | ||
169 | if ($job) { | |
170 | $res = XDB::query( | |
171 | "SELECT q.*, ao.alias AS q_owner, ar.alias AS q_recipient | |
172 | FROM gapps_queue AS q | |
fe13bc1d FB |
173 | LEFT JOIN aliases AS ao ON (ao.uid = q.q_owner_id AND ao.type = 'a_vie') |
174 | LEFT JOIN aliases AS ar ON (ar.uid = q.q_recipient_id AND ar.type = 'a_vie') | |
0089e594 VZ |
175 | WHERE q_id = {?}", $job); |
176 | $sql_job = $res->fetchOneAssoc(); | |
177 | $sql_job['decoded_parameters'] = var_export(json_decode($sql_job['j_parameters'], true), true); | |
178 | $page->assign('job', $sql_job); | |
179 | } | |
180 | } | |
181 | ||
26ba053e | 182 | function handler_admin_user($page, $user = null) { |
0089e594 VZ |
183 | require_once("emails.inc.php"); |
184 | require_once("googleapps.inc.php"); | |
185 | $page->changeTpl('googleapps/admin.user.tpl'); | |
46f272fe | 186 | $page->setTitle('Administration Google Apps'); |
0089e594 | 187 | $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid'))); |
fcef7ce2 VZ |
188 | |
189 | if (!$user && Post::has('login')) { | |
190 | $user = Post::v('login'); | |
191 | } | |
d56cb887 | 192 | $user = User::get($user); |
0089e594 VZ |
193 | |
194 | if ($user) { | |
195 | $account = new GoogleAppsAccount($user); | |
88cddbaa | 196 | $storage = new EmailStorage($user, 'googleapps'); |
0089e594 | 197 | |
bf88be24 VZ |
198 | // Apply requested actions. |
199 | if (Post::has('suspend') && $account->active() && !$account->pending_update_suspension) { | |
200 | S::assert_xsrf_token(); | |
201 | $account->suspend(); | |
202 | $page->trigSuccess('Le compte est en cours de suspension.'); | |
203 | } else if (Post::has('unsuspend') && $account->suspended() && !$account->pending_update_suspension) { | |
204 | S::assert_xsrf_token(); | |
205 | $account->do_unsuspend(); | |
206 | $page->trigSuccess('Le compte est en cours de réactivation.'); | |
207 | } else if (Post::has('forcesync') && $account->active() && $account->sync_password) { | |
2f8bacc2 | 208 | $account->set_password($user->password()); |
bf88be24 VZ |
209 | $page->trigSuccess('Le mot de passe est en cours de synchronisation.'); |
210 | } else if (Post::has('sync') && $account->active()) { | |
0001ba7a | 211 | $account->set_password($user->password()); |
bf88be24 VZ |
212 | $account->set_password_sync(true); |
213 | } else if (Post::has('nosync') && $account->active()) { | |
214 | $account->set_password_sync(false); | |
0089e594 VZ |
215 | } |
216 | ||
217 | // Displays basic account information. | |
218 | $page->assign('account', $account); | |
d56cb887 | 219 | $page->assign('admin_account', GoogleAppsAccount::is_administrator($user->id())); |
afde0a3a | 220 | $page->assign('googleapps_storage', $storage->active); |
d56cb887 | 221 | $page->assign('user', $user->id()); |
0089e594 VZ |
222 | |
223 | // Retrieves user's pending requests. | |
224 | $res = XDB::iterator( | |
225 | "SELECT q_id, q_recipient_id, p_status, j_type, UNIX_TIMESTAMP(p_entry_date) AS p_entry_date | |
226 | FROM gapps_queue | |
227 | WHERE q_recipient_id = {?} | |
d56cb887 | 228 | ORDER BY p_entry_date DESC", $user->id()); |
0089e594 VZ |
229 | $page->assign('requests', $res); |
230 | } | |
231 | } | |
bb0727ea VZ |
232 | } |
233 | ||
234 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
235 | ?> |