Moving to GitHub.
[platal.git] / modules / googleapps.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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_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'),
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->setTitle('Compte Google Apps');
45
46 $user = S::user();
47 $account = new GoogleAppsAccount($user);
48
49 // Fills up the 'is Google Apps redirection active' variable.
50 $redirect_active = false;
51 $redirect_unique = true;
52 $gapps_email = '';
53
54 if ($account->active()) {
55 $redirect = new Redirect($user);
56 foreach ($redirect->emails as $email) {
57 if ($email->type == 'googleapps') {
58 $gapps_email = $email->email;
59 $redirect_active = $email->active;
60 $redirect_unique = !$redirect->other_active($email->email);
61 }
62 }
63 }
64 $page->assign('redirect_active', $redirect_active);
65 $page->assign('redirect_unique', $redirect_unique);
66
67 // Updates the Google Apps account as required.
68 if ($action) {
69 if ($action == 'password' && Post::has('pwsync')) {
70 S::assert_xsrf_token();
71 if (Post::v('pwsync') == 'sync') {
72 $account->set_password_sync(true);
73 $account->set_password($user->password());
74 } else {
75 $account->set_password_sync(false);
76 }
77 } elseif ($action == 'password' && Post::has('pwhash') && Post::t('pwhash') && !$account->sync_password) {
78 S::assert_xsrf_token();
79 $account->set_password(Post::t('pwhash'));
80 }
81
82 if ($action == 'suspend' && Post::has('suspend') && $account->active()) {
83 S::assert_xsrf_token();
84
85 if ($account->pending_update_suspension) {
86 $page->trigWarning("Ton compte est déjà en cours de désactivation.");
87 } else {
88 if (!$redirect_active || $redirect->modify_one_email($gapps_email, false) == SUCCESS) {
89 $account->suspend();
90 $page->trigSuccess("Ton compte Google Apps est dorénavant désactivé.");
91 } else {
92 $page->trigError("Ton compte Google Apps est ta seule adresse de redirection. Ton compte ne peux pas être désactivé.");
93 }
94 }
95 } elseif ($action == 'unsuspend' && Post::has('unsuspend') && $account->suspended()) {
96 $account->unsuspend(Post::b('redirect_mails', true));
97 $page->trigSuccess("Ta demande de réactivation a bien été prise en compte.");
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')) {
105 S::assert_xsrf_token();
106
107 $password_sync = Post::b('password_sync');
108 $redirect_mails = Post::b('redirect_mails');
109 if ($password_sync) {
110 $password = $user->password();
111 } else {
112 $password = Post::t('pwhash');
113 }
114
115 $account->create($password_sync, $password, $redirect_mails);
116 $page->trigSuccess("La demande de création de ton compte Google Apps a bien été enregistrée.");
117 }
118 }
119
120 $page->assign('account', $account);
121 }
122
123 function handler_admin($page, $action = null) {
124 require_once 'googleapps.inc.php';
125 $page->changeTpl('googleapps/admin.tpl');
126 $page->setTitle('Administration Google Apps');
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);
135 $page->trigSuccess("La requête échouée a bien été retirée.");
136 }
137 }
138
139 // Retrieves latest pending administrative requests from the gappsd queue.
140 $res = XDB::iterator(
141 "SELECT q_id, q_recipient_id, s.email AS alias, j_type, j_parameters,
142 UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date
143 FROM gapps_queue AS q
144 LEFT JOIN email_source_account AS s ON (s.uid = q.q_recipient_id AND s.type = 'forlife')
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(
159 "SELECT q.q_id, q.q_recipient_id, s.email AS alias, q.j_type, q.r_result,
160 UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date
161 FROM gapps_queue AS q
162 LEFT JOIN email_source_account AS s ON (s.uid = q.q_recipient_id AND s.type = 'forlife')
163 WHERE q.p_status = 'hardfail'
164 ORDER BY p_entry_date DESC
165 LIMIT 20");
166 $page->assign('failed_requests', $res);
167 }
168
169 function handler_admin_job($page, $job = null) {
170 require_once 'googleapps.inc.php';
171 $page->changeTpl('googleapps/admin.job.tpl');
172 $page->setTitle('Administration Google Apps');
173 $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid')));
174
175 if ($job) {
176 $res = XDB::query(
177 "SELECT q.*, so.email AS q_owner, sr.email AS q_recipient
178 FROM gapps_queue AS q
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')
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
188 function handler_admin_user($page, $user = null) {
189 require_once 'emails.inc.php';
190 require_once 'googleapps.inc.php';
191 $page->changeTpl('googleapps/admin.user.tpl');
192 $page->setTitle('Administration Google Apps');
193 $page->assign('googleapps_admin', GoogleAppsAccount::is_administrator(S::v('uid')));
194
195 if (!$user && Post::has('login')) {
196 $user = Post::v('login');
197 }
198 $user = User::get($user);
199
200 if ($user) {
201 $account = new GoogleAppsAccount($user);
202
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) {
213 $account->set_password($user->password());
214 $page->trigSuccess('Le mot de passe est en cours de synchronisation.');
215 } else if (Post::has('sync') && $account->active()) {
216 $account->set_password($user->password());
217 $account->set_password_sync(true);
218 } else if (Post::has('nosync') && $account->active()) {
219 $account->set_password_sync(false);
220 }
221
222 // Displays basic account information.
223 $page->assign('account', $account);
224 $page->assign('admin_account', GoogleAppsAccount::is_administrator($user->id()));
225 $page->assign('googleapps_storage', Email::is_active_storage($user, 'googleapps'));
226 $page->assign('user', $user->id());
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 = {?}
233 ORDER BY p_entry_date DESC", $user->id());
234 $page->assign('requests', $res);
235 }
236 }
237 }
238
239 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
240 ?>