Fixes an "undefined variable" error in modules/email.php.
[platal.git] / modules / googleapps.php
CommitLineData
bb0727ea
VZ
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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(
0089e594
VZ
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'),
35 'admin/googleapps/user' => $this->make_hook('admin_user', AUTH_MDP, 'admin'),
bb0727ea
VZ
36 );
37 }
38
39 function handler_index(&$page, $action = null, $subaction = null)
40 {
41 require_once("emails.inc.php");
42 require_once("googleapps.inc.php");
43 $page->changeTpl('googleapps/index.tpl');
44 $page->addJsLink('motdepasse.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) {
64 if ($action == 'password') {
65 if ($subaction == 'sync') {
66 $account->set_password_sync(true);
84270653 67 $account->set_password(S::v('password'));
89fcda94 68 pl_redirect('googleapps#password');
bb0727ea
VZ
69 } else if ($subaction == 'nosync') {
70 $account->set_password_sync(false);
71 } else if (Post::has('response2') && !$account->sync_password) {
40d428d8
VZ
72 S::assert_xsrf_token();
73 $account->set_password(Post::v('response2'));
bb0727ea
VZ
74 }
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) {
84270653 105 $password = S::v('password');
bb0727ea
VZ
106 } else {
107 $password = Post::v('response2');
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
139 LEFT JOIN aliases AS a ON (a.id = q_recipient_id AND a.type = 'a_vie')
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
157 LEFT JOIN aliases AS a ON (a.id = q.q_recipient_id AND a.type = 'a_vie')
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
174 LEFT JOIN aliases AS ao ON (ao.id = q.q_owner_id AND ao.type = 'a_vie')
175 LEFT JOIN aliases AS ar ON (ar.id = q.q_recipient_id AND ar.type = 'a_vie')
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
183 function handler_admin_user(&$page, $user = null, $action = null) {
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
VZ
198
199 // Force synchronization of plat/al and Google Apps passwords.
200 if ($action == 'forcesync' && $account->sync_password) {
d56cb887 201 $res = XDB::query("SELECT password FROM auth_user_md5 WHERE user_id = {?}", $user->id());
0089e594 202 $account->set_password($res->fetchOneCell());
a7d35093 203 $page->trigSuccess('Le mot de passe a été synchronisé.');
0089e594
VZ
204 }
205
206 // Displays basic account information.
207 $page->assign('account', $account);
d56cb887 208 $page->assign('admin_account', GoogleAppsAccount::is_administrator($user->id()));
afde0a3a 209 $page->assign('googleapps_storage', $storage->active);
d56cb887 210 $page->assign('user', $user->id());
0089e594
VZ
211
212 // Retrieves user's pending requests.
213 $res = XDB::iterator(
214 "SELECT q_id, q_recipient_id, p_status, j_type, UNIX_TIMESTAMP(p_entry_date) AS p_entry_date
215 FROM gapps_queue
216 WHERE q_recipient_id = {?}
d56cb887 217 ORDER BY p_entry_date DESC", $user->id());
0089e594
VZ
218 $page->assign('requests', $res);
219 }
220 }
bb0727ea
VZ
221}
222
223// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
224?>