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