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