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