14baa8e6df47628fa0b9ba70de1aa2459e37d2f9
[platal.git] / modules / googleapps.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
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(
32 'googleapps' => $this->make_hook('index', AUTH_MDP),
33 'admin/googleapps' => $this->make_hook('admin', AUTH_MDP. 'admin'),
34 'admin/googleapps/job' => $this->make_hook('admin_job', AUTH_MDP, 'admin'),
35 'admin/googleapps/user' => $this->make_hook('admin_user', AUTH_MDP, 'admin'),
36 );
37 }
38
39 function handler_index(&$page, $action = null, $subaction = null)
40 {
41 require_once("emails.inc.php");
42 require_once("googleapps.inc.php");
43 $page->changeTpl('googleapps/index.tpl');
44 $page->addJsLink('motdepasse.js');
45 $page->assign('xorg_title', 'Polytechnique.org - Compte Google Apps');
46
47 $account = new GoogleAppsAccount(S::v('uid'), S::v('forlife'));
48
49 // Fills up the 'is Google Apps redirection active' variable.
50 $page->assign('redirect_active', false);
51 $page->assign('redirect_unique', true);
52
53 if ($account->active()) {
54 $redirect = new Redirect(S::v('uid'));
55 $page->assign('redirect_unique', !$redirect->other_active('googleapps'));
56
57 $storage = new EmailStorage(S::v('uid'), 'googleapps');
58 $page->assign('redirect_active', $storage->active);
59 }
60
61 // Updates the Google Apps account as required.
62 if ($action) {
63 if ($action == 'password') {
64 if ($subaction == 'sync') {
65 $account->set_password_sync(true);
66 $account->set_password(S::v('password'));
67 pl_redirect('googleapps#password');
68 } else if ($subaction == 'nosync') {
69 $account->set_password_sync(false);
70 } else if (Post::has('response2') && !$account->sync_password) {
71 if (S::has_xsrf_token()) {
72 $account->set_password(Post::v('response2'));
73 } else {
74 $page->trig("Le changement de ton mot de passe Google Apps a échoué, merci de réessayer.");
75 }
76 }
77 }
78
79 if ($action == 'suspend' && Post::has('suspend') && $account->active()) {
80 if (!S::has_xsrf_token()) {
81 $page->trig("La demande de suspension de ton compte a échouée, merci de réessayer.");
82 } else if ($account->pending_update_suspension) {
83 $page->trig("Ton compte est déjà en cours de désactivation.");
84 } else {
85 if ($redirect->modify_one_email('googleapps', false) == SUCCESS) {
86 $account->suspend();
87 $page->trig("Ton compte Google Apps est dorénavant désactivé.");
88 } else {
89 $page->trig("Ton compte Google Apps est ta seule adresse de redirection. Ton compte ne peux pas être désactivé.");
90 }
91 }
92 } elseif ($action == 'unsuspend' && Post::has('unsuspend') && $account->suspended()) {
93 $account->unsuspend(Post::b('redirect_mails', true));
94 $page->trig("Ta demande de réactivation a bien été prise en compte.");
95 }
96
97 if ($action == 'create') {
98 $page->assign('has_password_sync', Get::has('password_sync'));
99 $page->assign('password_sync', Get::b('password_sync', true));
100 }
101 if ($action == 'create' && Post::has('password_sync') && Post::has('redirect_mails')) {
102 $password_sync = Post::b('password_sync');
103 $redirect_mails = Post::b('redirect_mails');
104 if ($password_sync) {
105 $password = S::v('password');
106 } else {
107 $password = Post::v('response2');
108 }
109
110 if (S::has_xsrf_token()) {
111 $account->create($password_sync, $password, $redirect_mails);
112 $page->trig("La demande de création de ton compte Google Apps a bien été enregistrée.");
113 } else {
114 $page->trig("La demande de création de ton compte Google Apps a échouée, merci de réessayer.");
115 }
116 }
117 }
118
119 $page->assign('account', $account);
120 }
121
122 function handler_admin(&$page, $action = null) {
123 require_once("googleapps.inc.php");
124 $page->changeTpl('googleapps/admin.tpl');
125 $page->assign('xorg_title', 'Polytechnique.org - Administration Google Apps');
126 $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid')));
127
128 if ($action == 'ack') {
129 $qid = @func_get_arg(2);
130 if ($qid) {
131 XDB::execute(
132 "DELETE FROM gapps_queue
133 WHERE q_id = {?} AND p_status = 'hardfail'", $qid);
134 $page->trig("La requête échouée a bien été retirée.");
135 }
136 }
137
138 // Retrieves latest pending administrative requests from the gappsd queue.
139 $res = XDB::iterator(
140 "SELECT q_id, q_recipient_id, a.alias, j_type, j_parameters,
141 UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date
142 FROM gapps_queue AS q
143 LEFT JOIN aliases AS a ON (a.id = q_recipient_id AND a.type = 'a_vie')
144 WHERE p_status IN ('idle', 'active', 'softfail') AND
145 p_admin_request IS TRUE
146 ORDER BY p_entry_date");
147 while ($request = $res->next()) {
148 $j_parameters = json_decode($request['j_parameters'], true);
149 unset($j_parameters['username']);
150 $parameters = array_keys($j_parameters);
151 $request['parameters'] = implode(', ', $parameters);
152
153 $page->append('admin_requests', $request);
154 }
155
156 // Retrieves latest failed requests from the gappsd queue.
157 $res = XDB::iterator(
158 "SELECT q.q_id, q.q_recipient_id, a.alias, q.j_type, q.r_result,
159 UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date
160 FROM gapps_queue AS q
161 LEFT JOIN aliases AS a ON (a.id = q.q_recipient_id AND a.type = 'a_vie')
162 WHERE q.p_status = 'hardfail'
163 ORDER BY p_entry_date DESC
164 LIMIT 20");
165 $page->assign('failed_requests', $res);
166 }
167
168 function handler_admin_job(&$page, $job = null) {
169 require_once("googleapps.inc.php");
170 $page->changeTpl('googleapps/admin.job.tpl');
171 $page->assign('xorg_title', 'Polytechnique.org - Administration Google Apps');
172 $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid')));
173
174 if ($job) {
175 $res = XDB::query(
176 "SELECT q.*, ao.alias AS q_owner, ar.alias AS q_recipient
177 FROM gapps_queue AS q
178 LEFT JOIN aliases AS ao ON (ao.id = q.q_owner_id AND ao.type = 'a_vie')
179 LEFT JOIN aliases AS ar ON (ar.id = q.q_recipient_id AND ar.type = 'a_vie')
180 WHERE q_id = {?}", $job);
181 $sql_job = $res->fetchOneAssoc();
182 $sql_job['decoded_parameters'] = var_export(json_decode($sql_job['j_parameters'], true), true);
183 $page->assign('job', $sql_job);
184 }
185 }
186
187 function handler_admin_user(&$page, $user = null, $action = null) {
188 require_once("emails.inc.php");
189 require_once("googleapps.inc.php");
190 $page->changeTpl('googleapps/admin.user.tpl');
191 $page->assign('xorg_title', 'Polytechnique.org - Administration Google Apps');
192 $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid')));
193
194 if (!$user && Post::has('login')) {
195 $user = Post::v('login');
196 }
197 if ($user && !is_numeric($user)) {
198 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?} AND type != 'homonyme'", $user);
199 $user = $res->fetchOneCell();
200 }
201
202 if ($user) {
203 $account = new GoogleAppsAccount($user);
204 $storage = new EmailStorage($user, 'googleapps');
205
206 // Force synchronization of plat/al and Google Apps passwords.
207 if ($action == 'forcesync' && $account->sync_password) {
208 $res = XDB::query("SELECT password FROM auth_user_md5 WHERE user_id = {?}", $user);
209 $account->set_password($res->fetchOneCell());
210 $page->trig('Le mot de passe a été synchronisé.');
211 }
212
213 // Displays basic account information.
214 $page->assign('account', $account);
215 $page->assign('admin_account', GoogleAppsAccount::is_administrator($user));
216 $page->assign('googleapps_storage', $storage->active);
217 $page->assign('user', $user);
218
219 // Retrieves user's pending requests.
220 $res = XDB::iterator(
221 "SELECT q_id, q_recipient_id, p_status, j_type, UNIX_TIMESTAMP(p_entry_date) AS p_entry_date
222 FROM gapps_queue
223 WHERE q_recipient_id = {?}
224 ORDER BY p_entry_date DESC", $user);
225 $page->assign('requests', $res);
226 }
227 }
228 }
229
230 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
231 ?>