Merge branch 'platal-0.9.16'
[platal.git] / modules / auth / auth.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 function gpex_prepare_param($name, $val, &$to_hash, $charset)
23 {
24 $val = iconv('UTF-8', $charset, $val);
25 $to_hash .= $val;
26 return '&' . $name . '=' . urlencode($val);
27 }
28
29 function gpex_make($chlg, $privkey, $datafields, $charset)
30 {
31 $tohash = "1$chlg$privkey";
32 $params = "";
33 $fieldarr = explode(',', $datafields);
34
35 $res = XDB::query("SELECT matricule, matricule_ax, promo,
36 promo_sortie, flags, deces, nom,
37 prenom, nationalite, section,
38 naissance
39 FROM auth_user_md5 WHERE user_id = {?}",
40 S::v('uid'));
41 $personnal_data = $res->fetchOneAssoc();
42
43 foreach ($fieldarr as $val) {
44 /* on verifie qu'on n'a pas demandé une variable inexistante ! */
45 if ($val == 'perms') {
46 $params .= gpex_prepare_param($val, S::has_perms() ? 'admin' : 'user', $tohash, $charset);
47 } else if (S::has($val)) {
48 $params .= gpex_prepare_param($val, S::v($val), $tohash, $charset);
49 } else if (isset($personnal_data[$val])) {
50 $params .= gpex_prepare_param($val, $personnal_data[$val], $tohash, $charset);
51 } else if ($val == 'username') {
52 $res = XDB::query("SELECT alias FROM aliases
53 WHERE id = {?} AND FIND_IN_SET('bestalias', flags)",
54 S::v('uid'));
55 $min_username = $res->fetchOneCell();
56 $params .= gpex_prepare_param($val, $min_username, $tohash, $charset);
57 } else if ($val == 'grpauth') {
58 if (isset($_GET['group'])) {
59 $res = XDB::query("SELECT perms
60 FROM groupex.membres
61 INNER JOIN groupex.asso ON(id = asso_id)
62 WHERE uid = {?} AND diminutif = {?}",
63 S::v('uid'), $_GET['group']);
64 $perms = $res->fetchOneCell();
65 } else {
66 // if no group asked, return main rights
67 $perms = S::has_perms() ? 'admin' : 'membre';
68 }
69 $params .= gpex_prepare_param($val, $perms, $tohash, $charset);
70 }
71 }
72 $tohash .= "1";
73 $auth = md5($tohash);
74 return array($auth, "&auth=" . $auth . $params);
75 }
76
77 /* cree les parametres de l'URL de retour avec les champs demandes */
78 function gpex_make_params($chlg, $privkey, $datafields, $charset)
79 {
80 list ($auth, $param) = gpex_make($chlg, $privkey, $datafields, $charset);
81 return $param;
82 }
83
84 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
85 ?>