Happy New Year!
[platal.git] / modules / googleapps.php
CommitLineData
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
22class 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(
eb5a266d
SJ
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'),
0089e594 35 'admin/googleapps/user' => $this->make_hook('admin_user', AUTH_MDP, 'admin'),
bb0727ea
VZ
36 );
37 }
38
fc6f30c0 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');
4baa7323 44 $page->addJsLink('password.js');
46f272fe 45 $page->setTitle('Compte Google Apps');
bb0727ea 46
88cddbaa
VZ
47 $user = S::user();
48 $account = new GoogleAppsAccount($user);
bb0727ea
VZ
49
50 // Fills up the 'is Google Apps redirection active' variable.
51 $page->assign('redirect_active', false);
52 $page->assign('redirect_unique', true);
53
f5c4bf30 54 if ($account->active()) {
88cddbaa 55 $redirect = new Redirect($user);
afde0a3a 56 $page->assign('redirect_unique', !$redirect->other_active('googleapps'));
bb0727ea 57
88cddbaa 58 $storage = new EmailStorage($user, 'googleapps');
afde0a3a 59 $page->assign('redirect_active', $storage->active);
bb0727ea
VZ
60 }
61
62 // Updates the Google Apps account as required.
63 if ($action) {
fc6f30c0
VZ
64 if ($action == 'password' && Post::has('pwsync')) {
65 S::assert_xsrf_token();
66 if (Post::v('pwsync') == 'sync') {
bb0727ea 67 $account->set_password_sync(true);
84270653 68 $account->set_password(S::v('password'));
fc6f30c0 69 } else {
bb0727ea 70 $account->set_password_sync(false);
bb0727ea 71 }
81b5a6c9 72 } elseif ($action == 'password' && Post::has('pwhash') && Post::t('pwhash') && !$account->sync_password) {
fc6f30c0 73 S::assert_xsrf_token();
81b5a6c9 74 $account->set_password(Post::t('pwhash'));
bb0727ea
VZ
75 }
76
f5c4bf30 77 if ($action == 'suspend' && Post::has('suspend') && $account->active()) {
40d428d8
VZ
78 S::assert_xsrf_token();
79
80 if ($account->pending_update_suspension) {
a7d35093 81 $page->trigWarning("Ton compte est déjà en cours de désactivation.");
bb0727ea 82 } else {
afde0a3a 83 if ($redirect->modify_one_email('googleapps', false) == SUCCESS) {
bb0727ea 84 $account->suspend();
a7d35093 85 $page->trigSuccess("Ton compte Google Apps est dorénavant désactivé.");
bb0727ea 86 } else {
a7d35093 87 $page->trigError("Ton compte Google Apps est ta seule adresse de redirection. Ton compte ne peux pas être désactivé.");
bb0727ea
VZ
88 }
89 }
f5c4bf30 90 } elseif ($action == 'unsuspend' && Post::has('unsuspend') && $account->suspended()) {
bb0727ea 91 $account->unsuspend(Post::b('redirect_mails', true));
a7d35093 92 $page->trigSuccess("Ta demande de réactivation a bien été prise en compte.");
bb0727ea
VZ
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')) {
40d428d8
VZ
100 S::assert_xsrf_token();
101
bb0727ea
VZ
102 $password_sync = Post::b('password_sync');
103 $redirect_mails = Post::b('redirect_mails');
104 if ($password_sync) {
47d7e105 105 $password = $user->password();
bb0727ea 106 } else {
81b5a6c9 107 $password = Post::t('pwhash');
bb0727ea
VZ
108 }
109
40d428d8 110 $account->create($password_sync, $password, $redirect_mails);
a7d35093 111 $page->trigSuccess("La demande de création de ton compte Google Apps a bien été enregistrée.");
bb0727ea
VZ
112 }
113 }
114
115 $page->assign('account', $account);
116 }
0089e594
VZ
117
118 function handler_admin(&$page, $action = null) {
119 require_once("googleapps.inc.php");
120 $page->changeTpl('googleapps/admin.tpl');
46f272fe 121 $page->setTitle('Administration Google Apps');
0089e594
VZ
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);
a7d35093 130 $page->trigSuccess("La requête échouée a bien été retirée.");
0089e594
VZ
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
fe13bc1d 139 LEFT JOIN aliases AS a ON (a.uid = q_recipient_id AND a.type = 'a_vie')
0089e594
VZ
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
fe13bc1d 157 LEFT JOIN aliases AS a ON (a.uid = q.q_recipient_id AND a.type = 'a_vie')
0089e594
VZ
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');
46f272fe 167 $page->setTitle('Administration Google Apps');
0089e594
VZ
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
fe13bc1d
FB
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')
0089e594
VZ
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
bf88be24 183 function handler_admin_user(&$page, $user = null) {
0089e594
VZ
184 require_once("emails.inc.php");
185 require_once("googleapps.inc.php");
186 $page->changeTpl('googleapps/admin.user.tpl');
46f272fe 187 $page->setTitle('Administration Google Apps');
0089e594 188 $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid')));
fcef7ce2
VZ
189
190 if (!$user && Post::has('login')) {
191 $user = Post::v('login');
192 }
d56cb887 193 $user = User::get($user);
0089e594
VZ
194
195 if ($user) {
196 $account = new GoogleAppsAccount($user);
88cddbaa 197 $storage = new EmailStorage($user, 'googleapps');
0089e594 198
bf88be24
VZ
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) {
2f8bacc2 209 $account->set_password($user->password());
bf88be24
VZ
210 $page->trigSuccess('Le mot de passe est en cours de synchronisation.');
211 } else if (Post::has('sync') && $account->active()) {
0001ba7a 212 $account->set_password($user->password());
bf88be24
VZ
213 $account->set_password_sync(true);
214 } else if (Post::has('nosync') && $account->active()) {
215 $account->set_password_sync(false);
0089e594
VZ
216 }
217
218 // Displays basic account information.
219 $page->assign('account', $account);
d56cb887 220 $page->assign('admin_account', GoogleAppsAccount::is_administrator($user->id()));
afde0a3a 221 $page->assign('googleapps_storage', $storage->active);
d56cb887 222 $page->assign('user', $user->id());
0089e594
VZ
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 = {?}
d56cb887 229 ORDER BY p_entry_date DESC", $user->id());
0089e594
VZ
230 $page->assign('requests', $res);
231 }
232 }
bb0727ea
VZ
233}
234
235// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
236?>