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