migrate annuaire-admin
[platal.git] / modules / xnetgrp.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 XnetGrpModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'grp' => $this->make_hook('index', AUTH_PUBLIC),
28 'grp/asso.php' => $this->make_hook('index', AUTH_PUBLIC),
29 'grp/logo' => $this->make_hook('logo', AUTH_PUBLIC),
30 'grp/edit' => $this->make_hook('edit', AUTH_MDP),
31 'grp/mail' => $this->make_hook('mail', AUTH_MDP),
32 'grp/annuaire' => $this->make_hook('annuaire', AUTH_MDP),
33
34 'grp/admin/annuaire'
35 => $this->make_hook('admin_annuaire', AUTH_MDP),
36 );
37 }
38
39 function handler_index(&$page, $arg = null)
40 {
41 global $globals;
42
43 if (!is_null($arg)) {
44 return PL_NOT_FOUND;
45 }
46
47 $page->changeTpl('xnet/groupe/asso.tpl');
48 $page->useMenu();
49 $page->setType($globals->asso('cat'));
50 $page->assign('is_member', is_member());
51 $page->assign('logged', logged());
52
53 $page->assign('asso', $globals->asso());
54 }
55
56 function handler_logo(&$page)
57 {
58 global $globals;
59
60 $res = $globals->xdb->query("SELECT logo, logo_mime
61 FROM groupex.asso WHERE id = {?}",
62 $globals->asso('id'));
63 list($logo, $logo_mime) = $res->fetchOneRow();
64
65 if (!empty($logo)) {
66 header("Content-type: $mime");
67 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
68 header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT');
69 header('Cache-Control: no-cache, must-revalidate');
70 header('Pragma: no-cache');
71 echo $logo;
72 } else {
73 header('Content-type: image/jpeg');
74 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
75 header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT');
76 header('Cache-Control: no-cache, must-revalidate');
77 header('Pragma: no-cache');
78 readfile(dirname(__FILE__).'/../htdocs.net/images/dflt_carre.jpg');
79 }
80
81 exit;
82 }
83
84 function handler_edit(&$page)
85 {
86 global $globals;
87
88 new_groupadmin_page('xnet/groupe/edit.tpl');
89
90 if (Post::has('submit')) {
91 if (has_perms()) {
92 if (Post::get('mail_domain') && (strstr(Post::get('mail_domain'), '.') === false)) {
93 $page->trig_run("le domaine doit être un FQDN (aucune modif effectuée) !!!");
94 }
95 $globals->xdb->execute(
96 "UPDATE groupex.asso
97 SET nom={?}, diminutif={?}, cat={?}, dom={?},
98 descr={?}, site={?}, mail={?}, resp={?},
99 forum={?}, mail_domain={?}, ax={?}, pub={?},
100 sub_url={?}, inscriptible={?}
101 WHERE id={?}",
102 Post::get('nom'), Post::get('diminutif'),
103 Post::get('cat'), Post::getInt('dom'),
104 Post::get('descr'), Post::get('site'),
105 Post::get('mail'), Post::get('resp'),
106 Post::get('forum'), Post::get('mail_domain'),
107 Post::has('ax'), Post::has('pub')?'private':'public',
108 Post::get('sub_url'), Post::get('inscriptible'),
109 $globals->asso('id'));
110 if (Post::get('mail_domain')) {
111 $globals->xdb->execute('INSERT INTO virtual_domains (domain) VALUES({?})',
112 Post::get('mail_domain'));
113 }
114 } else {
115 $globals->xdb->execute(
116 "UPDATE groupex.asso
117 SET descr={?}, site={?}, mail={?}, resp={?},
118 forum={?}, ax={?}, pub= {?}, sub_url={?}
119 WHERE id={?}",
120 Post::get('descr'), Post::get('site'),
121 Post::get('mail'), Post::get('resp'),
122 Post::get('forum'), Post::has('ax'),
123 Post::has('pub')?'private':'public',
124 Post::get('sub_url'), $globals->asso('id'));
125 }
126
127 if ($_FILES['logo']['name']) {
128 $logo = file_get_contents($_FILES['logo']['tmp_name']);
129 $mime = $_FILES['logo']['type'];
130 $globals->xdb->execute('UPDATE groupex.asso
131 SET logo={?}, logo_mime={?}
132 WHERE id={?}', $logo, $mime,
133 $globals->asso('id'));
134 }
135
136 redirect('../'.Post::get('diminutif', $globals->asso('diminutif')).'/edit');
137 }
138
139 if (has_perms()) {
140 $dom = $globals->xdb->iterator('SELECT * FROM groupex.dom ORDER BY nom');
141 $page->assign('dom', $dom);
142 $page->assign('super', true);
143 }
144 }
145
146 function handler_mail(&$page)
147 {
148 global $globals;
149
150 require_once 'lists.inc.php';
151
152 new_groupadmin_page('xnet/groupe/mail.tpl');
153 $client =& lists_xmlrpc(Session::getInt('uid'),
154 Session::get('password'),
155 $globals->asso('mail_domain'));
156 $page->assign('listes', $client->get_lists());
157
158 if (Post::has('send')) {
159 $from = Post::get('from');
160 $sujet = Post::get('sujet');
161 $body = Post::get('body');
162
163 $mls = array_keys(Env::getMixed('ml', array()));
164
165 require_once 'xnet/mail.inc.php';
166 $tos = get_all_redirects(Post::has('membres'), $mls, $client);
167 send_xnet_mails($from, $sujet, $body, $tos, Post::get('replyto'));
168 $page->kill("Mail envoyé !");
169 $page->assign('sent', true);
170 }
171 }
172
173 function handler_annuaire(&$page)
174 {
175 global $globals;
176
177 define('NB_PER_PAGE', 25);
178
179 if ($globals->asso('pub') == 'public') {
180 new_group_page('xnet/groupe/annuaire.tpl');
181 } else {
182 new_groupadmin_page('xnet/groupe/annuaire.tpl');
183 }
184
185 $page->assign('admin', may_update());
186
187 switch (Env::get('order')) {
188 case 'promo' : $group = 'promo'; $tri = 'promo_o DESC, nom, prenom'; break;
189 case 'promo_inv': $group = 'promo'; $tri = 'promo_o, nom, prenom'; break;
190 case 'alpha_inv': $group = 'initiale'; $tri = 'nom DESC, prenom DESC, promo'; break;
191 default : $group = 'initiale'; $tri = 'nom, prenom, promo';
192 }
193
194 if ($group == 'initiale')
195 $res = $globals->xdb->iterRow(
196 'SELECT UPPER(SUBSTRING(
197 IF(m.origine="X", IF(u.nom_usage<>"", u.nom_usage, u.nom),m.nom),
198 1, 1)) as letter, COUNT(*)
199 FROM groupex.membres AS m
200 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid )
201 WHERE asso_id = {?}
202 GROUP BY letter
203 ORDER BY letter', $globals->asso('id'));
204 else
205 $res = $globals->xdb->iterRow(
206 'SELECT IF(m.origine="X",u.promo,"extérieur") AS promo,
207 COUNT(*), IF(m.origine="X",u.promo,"") AS promo_o
208 FROM groupex.membres AS m
209 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid )
210 WHERE asso_id = {?}
211 GROUP BY promo
212 ORDER BY promo_o DESC', $globals->asso('id'));
213
214 $alphabet = array();
215 $nb_tot = 0;
216 while (list($char, $nb) = $res->next()) {
217 $alphabet[] = $char;
218 $nb_tot += $nb;
219 if (Env::has($group) && $char == strtoupper(Env::get($group))) {
220 $tot = $nb;
221 }
222 }
223 $page->assign('group', $group);
224 $page->assign('request_group', Env::get($group));
225 $page->assign('alphabet', $alphabet);
226 $page->assign('nb_tot', $nb_tot);
227
228 $ofs = Env::getInt('offset');
229 $tot = Env::get($group) ? $tot : $nb_tot;
230 $nbp = intval(($tot-1)/NB_PER_PAGE);
231 $links = array();
232 if ($ofs) {
233 $links['précédent'] = $ofs-1;
234 }
235 for ($i = 0; $i <= $nbp; $i++) {
236 $links[(string)($i+1)] = $i;
237 }
238 if ($ofs < $nbp) {
239 $links['suivant'] = $ofs+1;
240 }
241 if (count($links)>1) {
242 $page->assign('links', $links);
243 }
244
245 $ini = '';
246 if (Env::has('initiale')) {
247 $ini = 'AND IF(m.origine="X",
248 IF(u.nom_usage<>"", u.nom_usage, u.nom),
249 m.nom) LIKE "'.addslashes(Env::get('initiale')).'%"';
250 } elseif (Env::has('promo')) {
251 $ini = 'AND IF(m.origine="X", u.promo, "extérieur") = "'
252 .addslashes(Env::get('promo')).'"';
253 }
254
255 $ann = $globals->xdb->iterator(
256 "SELECT IF(m.origine='X',IF(u.nom_usage<>'', u.nom_usage, u.nom) ,m.nom) AS nom,
257 IF(m.origine='X',u.prenom,m.prenom) AS prenom,
258 IF(m.origine='X',u.promo,'extérieur') AS promo,
259 IF(m.origine='X',u.promo,'') AS promo_o,
260 IF(m.origine='X',a.alias,m.email) AS email,
261 IF(m.origine='X',FIND_IN_SET('femme', u.flags),0) AS femme,
262 m.perms='admin' AS admin,
263 m.origine='X' AS x,
264 m.uid
265 FROM groupex.membres AS m
266 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid )
267 LEFT JOIN aliases AS a ON ( a.id = m.uid AND a.type='a_vie' )
268 WHERE m.asso_id = {?} $ini
269 ORDER BY $tri
270 LIMIT {?},{?}", $globals->asso('id'), $ofs*NB_PER_PAGE, NB_PER_PAGE);
271
272 $page->assign('ann', $ann);
273 }
274
275 function handler_admin_annuaire(&$page)
276 {
277 global $globals;
278
279 require_once 'lists.inc.php';
280 require_once 'xnet/mail.inc.php';
281
282 new_groupadmin_page('xnet/groupe/annuaire-admin.tpl');
283 $client =& lists_xmlrpc(Session::getInt('uid'),
284 Session::get('password'),
285 $globals->asso('mail_domain'));
286 $lists = $client->get_lists();
287 if (!$lists) $lists = array();
288 $listes = array_map(create_function('$arr', 'return $arr["list"];'), $lists);
289
290 $subscribers = array();
291
292 foreach ($listes as $list) {
293 list(,$members) = $client->get_members($list);
294 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
295 $subscribers = array_unique(array_merge($subscribers, $mails));
296 }
297
298 $not_in_group_x = array();
299 $not_in_group_ext = array();
300
301 foreach ($subscribers as $mail) {
302 $res = $globals->xdb->query(
303 'SELECT COUNT(*)
304 FROM groupex.membres AS m
305 LEFT JOIN auth_user_md5 AS u ON (m.uid=u.user_id AND m.uid<50000)
306 LEFT JOIN aliases AS a ON (a.id=u.user_id and a.type="a_vie")
307 WHERE asso_id = {?} AND
308 (m.email = {?} OR CONCAT(a.alias, "@polytechnique.org") = {?})',
309 $globals->asso('id'), $mail, $mail);
310 if ($res->fetchOneCell() == 0) {
311 if (strstr($mail, '@polytechnique.org') === false) {
312 $not_in_group_ext[] = $mail;
313 } else {
314 $not_in_group_x[] = $mail;
315 }
316 }
317 }
318
319 $page->assign('not_in_group_ext', $not_in_group_ext);
320 $page->assign('not_in_group_x', $not_in_group_x);
321 $page->assign('lists', $lists);
322 }
323 }
324
325 ?>