aliases.id => aliases.uid
[platal.git] / include / webservices / manageurs.server.inc.php
1 <?php
2
3 require_once('webservices/manageurs.inc.php');
4
5 $error_mat = "You didn't provide me with a valid matricule number...";
6 $error_key = "You didn't provide me with a valid cipher key...";
7
8 // The first parameter has to be the identification number (matricule).
9 // The second parameter facultative has to be the number of the desired address:
10 // -1 => we do not want any address;
11 // 0 => we want all the addresses;
12 // n => we want the address number n.
13 //
14 // DO NOT CHANGE THE NAMES OF THE ADDRESSES' FIELDS.
15 // IF YOU HAVE TO MAKE SOME MODIFICATION, FIRST CONTACT:
16 // admin@manageurs.com
17 function get_annuaire_infos($method, $params) {
18 require "geocoding.inc.php";
19 global $error_mat, $error_key, $globals;
20
21 // Password verification.
22 if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)) {
23 return false;
24 }
25 // If address == -1, we do not retrieve any address.
26 if(isset($params[2]) && ($params[2] == -1)) {
27 unset($params[2]);
28 }
29
30 // We check we actually have an identification number.
31 if(!empty($params[1])) {
32 // We only retrieve addresses when required.
33 if(!isset($params[2])) {
34 $res = XDB::iterRow(
35 "SELECT ph.display_tel AS cell, a.naissance AS age
36 FROM auth_user_md5 AS a
37 INNER JOIN auth_user_quick AS q USING (user_id)
38 LEFT JOIN profile_phones AS ph ON (ph.pid = a.user_id AND link_type='user' AND tel_type = 'mobile')
39 WHERE a.matricule = {?} LIMIT 1", $params[1]);
40 $array = $res->next();
41 } else {
42 $res = XDB::iterRow(
43 "SELECT a.naissance, addr.text, addr.postalCode,
44 l.name, addr.countryId, addr.pid, addr.id
45 FROM auth_user_md5 AS a
46 INNER JOIN auth_user_quick AS q USING (user_id)
47 LEFT JOIN profile_addresses AS addr ON(adr.pid = a.user_id)
48 LEFT JOIN geoloc_localities AS l ON (l.id = addr.localityId)
49 WHERE a.matricule = {?} AND
50 NOT FIND_IN_SET('pro', adr.statut)
51 ORDER BY NOT FIND_IN_SET('active', adr.statut),
52 FIND_IN_SET('res-secondaire', adr.statut),
53 NOT FIND_IN_SET('courrier', adr.statut)", $params[1]);
54 // Process the addresses we got.
55 if(list($age, $text, $adr['cp'], $adr['ville'],
56 $adr['pays'], $uid, $adr['adrid']) = $res->next()) {
57 list($adr['adr1'], $adr['adr2'], $adr['adr3']) =
58 explode("\n", Geocoder::getFirstLines($text, $adr['cp'], 3));
59 $sql = XDB::query("SELECT display_tel
60 FROM profile_phones
61 WHERE pid = {?} AND link_type = 'user' AND tel_type = 'mobile'
62 LIMIT 1", $uid);
63 if ($sql->numRows() > 0) {
64 $array['cell'] = $sql->fetchOneCell();
65 } else {
66 $array['cell'] ='';
67 }
68 $array['age'] = $age;
69 $array['adresse'][] = $adr;
70
71
72 // We limit the address number by the number of available addresses.
73 $adresse = min((int) $params[2], $res->total());
74
75 if ($adresse != 1) { // We don't want the first address.
76 $i = 2;
77 while(list($age, $text, $adr['cp'], $adr['ville'],
78 $adr['pays'], , $adr['adrid']) = $res->next()) {
79 list($adr['adr1'], $adr['adr2'], $adr['adr3']) =
80 explode("\n", Geocoder::getFirstLines($text, $adr['cp'], 3));
81 if ($adresse == $i) {
82 // If we want this particular address.
83 $array['adresse'][0] = $adr;
84 //$res->free();
85 break;
86 } elseif ($adresse == 0) {
87 // If we want every address.
88 $array['adresse'][] = $adr;
89 }
90 $i++;
91 }
92 }
93
94 // We add the phone numbers.
95 $adrid_index = array();
96 foreach ($array['adresse'] as $i => $a) {
97 $adrid_index[$a['adrid']] = $i;
98 }
99 $restel = XDB::iterator(
100 "SELECT t.display_tel AS tel, t.tel_type, t.link_id as adrid
101 FROM profile_phones AS t
102 INNER JOIN profile_addresses AS a ON (t.link_id = a.id AND t.uid = a.pid)
103 WHERE t.pid = {?} AND t.link_type = 'address'
104 AND NOT FIND_IN_SET('pro', a.statut)", $uid);
105 while ($tel = $restel->next()) {
106 $array['adresse'][$adrid_index[$tel['adrid']]]['tels'][] = $tel;
107 }
108 foreach ($array['adresse'] as $i => $adr) {
109 unset($lasttel);
110 foreach ($adr['tels'] as $j => $t) {
111 if (!isset($array['adresse'][$i]['tel']) && (strpos($t['tel_type'], 'Tél') === 0)) {
112 $array['adresse'][$i]['tel'] = $t['tel'];
113 } elseif (!isset($array['adresse'][$i]['fax'])
114 && (strpos($t['tel_type'], 'Fax') === 0)) {
115 $array['adresse'][$i]['fax'] = $t['tel'];
116 } else {
117 $lasttel = $t['tel'];
118 }
119 if (isset($array['adresse'][$i]['tel']) && isset($array['adresse'][$i]['fax'])) {
120 break;
121 }
122 }
123 if (!isset($array['adresse'][$i]['tel']) && isset($lasttel)) {
124 $array['adresse'][$i]['tel'] = $lasttel;
125 } elseif (!isset($array['adresse'][$i]['fax']) && isset($lasttel)) {
126 $array['adresse'][$i]['fax'] = $lasttel;
127 }
128 unset($array['adresse'][$i]['adrid']);
129 unset($array['adresse'][$i]['tels']);
130 }
131 } else {
132 $array = false;
133 }
134 }
135
136 if ($array) { // We did get a result: the identification number was rigth.
137
138 // We only send the age to manageurs.com; the format is YYYY-MM-DD 0123-56-89.
139 $year = (int) substr($array['age'], 0, 4);
140 $month = (int) substr($array['age'], 5, 2);
141 $day = (int) substr($array['age'], 8, 2);
142 $age = (int) date('Y') - $year - 1;
143 if(( $month < (int)date('m')) ||
144 (($month == (int)date('m')) && ($day >= (int)date('d')))) {
145 $age += 1;
146 }
147 $array['age'] = $age;
148
149 // We start the encryption of the data.
150 if (manageurs_encrypt_init($params[1]) == 1) {
151 // We did not find the key to encryptthe data.
152 $args = array("erreur" => 3, "erreurstring" => $error_key);
153 $reply = xmlrpc_encode_request(NULL,$args);
154 } else {
155 $reply = manageurs_encrypt_array($array);
156 manageurs_encrypt_close();
157 }
158 } else {
159 // The identification number was not valid.
160 $args = array("erreur" => 2, "erreurstring" => $erreur_mat);
161 $reply = xmlrpc_encode_request(NULL,$args);
162 }
163 } else {
164 // The identification number was not in argument.
165 $args = array("erreur" => 1, "erreurstring" => $error_mat);
166 $reply = xmlrpc_encode_request(NULL, $args);
167 }
168
169 return $reply;
170 }
171
172 function get_nouveau_infos($method, $params) {
173 global $error_mat, $error_key, $globals;
174 // Password verification.
175 if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)) {
176 return false;
177 }
178 // We check we actually have an identification number.
179 if(!empty($params[1])) {
180 $res = XDB::query(
181 "SELECT a.nom, a.nom_usage,a.prenom, FIND_IN_SET('femme', a.flags) as femme,
182 a.deces!= 0 as decede, a.naissance, a.promo, concat(al.alias, '@m4x.org') as mail
183 FROM auth_user_md5 AS a
184 INNER JOIN aliases AS al ON (a.user_id = al.uid)
185 WHERE al.flags = 'bestalias' AND a.matricule = {?}",$params[1]);
186 $data=$res->fetchOneAssoc();
187 //$data['mail'].='@polytechnique.org';
188
189
190 // We start the encryption of the data.
191 if (manageurs_encrypt_init($params[1]) == 1) {
192 // We did not find the key to encryptthe data.
193 $args = array("erreur" => 3, "erreurstring" => $error_key);
194 $reply = xmlrpc_encode_request(NULL, $args);
195 } else {
196 $reply = manageurs_encrypt_array($data);
197 manageurs_encrypt_close();
198 }
199
200 } else {
201 $reply = false;
202 }
203 return $reply;
204 }
205
206 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
207 ?>