2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class GoogleAppsModule
extends PLModule
27 if (!$globals->mailstorage
->googleapps_domain
) {
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'),
39 function handler_index(&$page, $action = null
)
41 require_once("emails.inc.php");
42 require_once("googleapps.inc.php");
43 $page->changeTpl('googleapps/index.tpl');
44 $page->setTitle('Compte Google Apps');
47 $account = new GoogleAppsAccount($user);
49 // Fills up the 'is Google Apps redirection active' variable.
50 $page->assign('redirect_active', false
);
51 $page->assign('redirect_unique', true
);
53 if ($account->active()) {
54 $redirect = new Redirect($user);
55 $page->assign('redirect_unique', !$redirect->other_active('googleapps'));
57 $storage = new EmailStorage($user, 'googleapps');
58 $page->assign('redirect_active', $storage->active
);
61 // Updates the Google Apps account as required.
63 if ($action == 'password' && Post
::has('pwsync')) {
64 S
::assert_xsrf_token();
65 if (Post
::v('pwsync') == 'sync') {
66 $account->set_password_sync(true
);
67 $account->set_password(S
::v('password'));
69 $account->set_password_sync(false
);
71 } elseif ($action == 'password' && Post
::has('pwhash') && Post
::t('pwhash') && !$account->sync_password
) {
72 S
::assert_xsrf_token();
73 $account->set_password(Post
::t('pwhash'));
76 if ($action == 'suspend' && Post
::has('suspend') && $account->active()) {
77 S
::assert_xsrf_token();
79 if ($account->pending_update_suspension
) {
80 $page->trigWarning("Ton compte est déjà en cours de désactivation.");
82 if ($redirect->modify_one_email('googleapps', false
) == SUCCESS
) {
84 $page->trigSuccess("Ton compte Google Apps est dorénavant désactivé.");
86 $page->trigError("Ton compte Google Apps est ta seule adresse de redirection. Ton compte ne peux pas être désactivé.");
89 } elseif ($action == 'unsuspend' && Post
::has('unsuspend') && $account->suspended()) {
90 $account->unsuspend(Post
::b('redirect_mails', true
));
91 $page->trigSuccess("Ta demande de réactivation a bien été prise en compte.");
94 if ($action == 'create') {
95 $page->assign('has_password_sync', Get
::has('password_sync'));
96 $page->assign('password_sync', Get
::b('password_sync', true
));
98 if ($action == 'create' && Post
::has('password_sync') && Post
::has('redirect_mails')) {
99 S
::assert_xsrf_token();
101 $password_sync = Post
::b('password_sync');
102 $redirect_mails = Post
::b('redirect_mails');
103 if ($password_sync) {
104 $password = $user->password();
106 $password = Post
::t('pwhash');
109 $account->create($password_sync, $password, $redirect_mails);
110 $page->trigSuccess("La demande de création de ton compte Google Apps a bien été enregistrée.");
114 $page->assign('account', $account);
117 function handler_admin(&$page, $action = null
) {
118 require_once("googleapps.inc.php");
119 $page->changeTpl('googleapps/admin.tpl');
120 $page->setTitle('Administration Google Apps');
121 $page->assign('googleapps_admin', GoogleAppsAccount
::is_administrator(S
::v('uid')));
123 if ($action == 'ack') {
124 $qid = @func_get_arg
(2);
127 "DELETE FROM gapps_queue
128 WHERE q_id = {?} AND p_status = 'hardfail'", $qid);
129 $page->trigSuccess("La requête échouée a bien été retirée.");
133 // Retrieves latest pending administrative requests from the gappsd queue.
134 $res = XDB
::iterator(
135 "SELECT q_id, q_recipient_id, a.alias, j_type, j_parameters,
136 UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date
137 FROM gapps_queue AS q
138 LEFT JOIN aliases AS a ON (a.uid = q_recipient_id AND a.type = 'a_vie')
139 WHERE p_status IN ('idle', 'active', 'softfail') AND
140 p_admin_request IS TRUE
141 ORDER BY p_entry_date");
142 while ($request = $res->next()) {
143 $j_parameters = json_decode($request['j_parameters'], true
);
144 unset($j_parameters['username']);
145 $parameters = array_keys($j_parameters);
146 $request['parameters'] = implode(', ', $parameters);
148 $page->append('admin_requests', $request);
151 // Retrieves latest failed requests from the gappsd queue.
152 $res = XDB
::iterator(
153 "SELECT q.q_id, q.q_recipient_id, a.alias, q.j_type, q.r_result,
154 UNIX_TIMESTAMP(q.p_entry_date) AS p_entry_date
155 FROM gapps_queue AS q
156 LEFT JOIN aliases AS a ON (a.uid = q.q_recipient_id AND a.type = 'a_vie')
157 WHERE q.p_status = 'hardfail'
158 ORDER BY p_entry_date DESC
160 $page->assign('failed_requests', $res);
163 function handler_admin_job(&$page, $job = null
) {
164 require_once("googleapps.inc.php");
165 $page->changeTpl('googleapps/admin.job.tpl');
166 $page->setTitle('Administration Google Apps');
167 $page->assign('googleapps_admin', GoogleAppsAccount
::is_administrator(S
::v('uid')));
171 "SELECT q.*, ao.alias AS q_owner, ar.alias AS q_recipient
172 FROM gapps_queue AS q
173 LEFT JOIN aliases AS ao ON (ao.uid = q.q_owner_id AND ao.type = 'a_vie')
174 LEFT JOIN aliases AS ar ON (ar.uid = q.q_recipient_id AND ar.type = 'a_vie')
175 WHERE q_id = {?}", $job);
176 $sql_job = $res->fetchOneAssoc();
177 $sql_job['decoded_parameters'] = var_export(json_decode($sql_job['j_parameters'], true
), true
);
178 $page->assign('job', $sql_job);
182 function handler_admin_user(&$page, $user = null
) {
183 require_once("emails.inc.php");
184 require_once("googleapps.inc.php");
185 $page->changeTpl('googleapps/admin.user.tpl');
186 $page->setTitle('Administration Google Apps');
187 $page->assign('googleapps_admin', GoogleAppsAccount
::is_administrator(S
::v('uid')));
189 if (!$user && Post
::has('login')) {
190 $user = Post
::v('login');
192 $user = User
::get($user);
195 $account = new GoogleAppsAccount($user);
196 $storage = new EmailStorage($user, 'googleapps');
198 // Apply requested actions.
199 if (Post
::has('suspend') && $account->active() && !$account->pending_update_suspension
) {
200 S
::assert_xsrf_token();
202 $page->trigSuccess('Le compte est en cours de suspension.');
203 } else if (Post
::has('unsuspend') && $account->suspended() && !$account->pending_update_suspension
) {
204 S
::assert_xsrf_token();
205 $account->do_unsuspend();
206 $page->trigSuccess('Le compte est en cours de réactivation.');
207 } else if (Post
::has('forcesync') && $account->active() && $account->sync_password
) {
208 $account->set_password($user->password());
209 $page->trigSuccess('Le mot de passe est en cours de synchronisation.');
210 } else if (Post
::has('sync') && $account->active()) {
211 $account->set_password($user->password());
212 $account->set_password_sync(true
);
213 } else if (Post
::has('nosync') && $account->active()) {
214 $account->set_password_sync(false
);
217 // Displays basic account information.
218 $page->assign('account', $account);
219 $page->assign('admin_account', GoogleAppsAccount
::is_administrator($user->id()));
220 $page->assign('googleapps_storage', $storage->active
);
221 $page->assign('user', $user->id());
223 // Retrieves user's pending requests.
224 $res = XDB
::iterator(
225 "SELECT q_id, q_recipient_id, p_status, j_type, UNIX_TIMESTAMP(p_entry_date) AS p_entry_date
227 WHERE q_recipient_id = {?}
228 ORDER BY p_entry_date DESC", $user->id());
229 $page->assign('requests', $res);
234 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: