Merge branch 'xorg/master' into xorg/f/geocoding
[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 global $error_mat, $error_key, $globals;
19
20 // Password verification.
21 if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)) {
22 return false;
23 }
24 // If address == -1, we do not retrieve any address.
25 if(isset($params[2]) && ($params[2] == -1)) {
26 unset($params[2]);
27 }
28
29 // We check we actually have an identification number.
30 if(!empty($params[1])) {
31 // We only retrieve addresses when required.
32 if(!isset($params[2])) {
33 $res = XDB::iterRow("SELECT pp.display_tel AS cell, p.birthdate AS age
34 FROM profiles AS p
35 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = 'user'
36 AND pp.tel_type = 'mobile')
37 WHERE p.xorg_id = {?} LIMIT 1",
38 $params[1]);
39 $array = $res->next();
40 } else {
41 $res = XDB::iterRow("SELECT p.birthdate, pa.text, pace3.short_name, pace2.short_name, pace1.short_name, p.pid, pa.id
42 FROM profiles AS p
43 LEFT JOIN profile_addresses AS pa ON (pa.pid = p.pid)
44 LEFT JOIN profile_addresses_components_enum AS pace1 ON (FIND_IN_SET('country', pace1.types))
45 LEFT JOIN profile_addresses_components_enum AS pace2 ON (FIND_IN_SET('locality', pace2.types))
46 LEFT JOIN profile_addresses_components_enum AS pace3 ON (FIND_IN_SET('postal_code', pace3.types))
47 LEFT JOIN profile_addresses_components AS pac1 ON (pa.pid = pac1.pid AND pa.jobid = pac1.jobid AND pa.groupid = pac1.groupid
48 AND pa.id = pac1.id AND pa.type = pac1.type AND pace1.id = pac1.component_id)
49 LEFT JOIN profile_addresses_components AS pac2 ON (pa.pid = pac2.pid AND pa.jobid = pac2.jobid AND pa.groupid = pac2.groupid
50 AND pa.id = pac2.id AND pa.type = pac2.type AND pace2.id = pac2.component_id)
51 LEFT JOIN profile_addresses_components AS pac3 ON (pa.pid = pac3.pid AND pa.jobid = pac3.jobid AND pa.groupid = pac3.groupid
52 AND pa.id = pac3.id AND pa.type = pac3.type AND pace3.id = pac3.component_id)
53 WHERE p.xorg_id = {?} AND NOT FIND_IN_SET('job', pa.flags)
54 ORDER BY NOT FIND_IN_SET('current', pa.flags),
55 FIND_IN_SET('secondary', pa.flags),
56 NOT FIND_IN_SET('mail', pa.flags)
57 GROUP BY pa.pid, pa.jobid, pa.groupid, pa.id, pa.type",
58 $params[1]);
59 // Process the addresses we got.
60 if(list($age, $text, $adr['cp'], $adr['ville'],
61 $adr['pays'], $pid, $adr['adrid']) = $res->next()) {
62 list($adr['adr1'], $adr['adr2'], $adr['adr3']) =
63 explode("\n", Geocoder::getFirstLines($text, $adr['cp'], 3));
64 $sql = XDB::query("SELECT display_tel
65 FROM profile_phones
66 WHERE pid = {?} AND link_type = 'user' AND tel_type = 'mobile'
67 LIMIT 1", $pid);
68 if ($sql->numRows() > 0) {
69 $array['cell'] = $sql->fetchOneCell();
70 } else {
71 $array['cell'] ='';
72 }
73 $array['age'] = $age;
74 $array['adresse'][] = $adr;
75
76
77 // We limit the address number by the number of available addresses.
78 $adresse = min((int) $params[2], $res->total());
79
80 if ($adresse != 1) { // We don't want the first address.
81 $i = 2;
82 while(list($age, $text, $adr['cp'], $adr['ville'],
83 $adr['pays'], , $adr['adrid']) = $res->next()) {
84 list($adr['adr1'], $adr['adr2'], $adr['adr3']) =
85 explode("\n", Geocoder::getFirstLines($text, $adr['cp'], 3));
86 if ($adresse == $i) {
87 // If we want this particular address.
88 $array['adresse'][0] = $adr;
89 //$res->free();
90 break;
91 } elseif ($adresse == 0) {
92 // If we want every address.
93 $array['adresse'][] = $adr;
94 }
95 $i++;
96 }
97 }
98
99 // We add the phone numbers.
100 $adrid_index = array();
101 foreach ($array['adresse'] as $i => $a) {
102 $adrid_index[$a['adrid']] = $i;
103 }
104 $restel = XDB::iterator("SELECT pp.display_tel AS tel, pp..tel_type, pp.link_id as adrid
105 FROM profile_phones AS pp
106 INNER JOIN profile_addresses AS pa ON (pp.link_id = pa.id AND pp.pid = pa.pid)
107 WHERE pp.pid = {?} AND pp.link_type = 'address'
108 AND NOT FIND_IN_SET('pro', pa.statut)",
109 $pid);
110 while ($tel = $restel->next()) {
111 $array['adresse'][$adrid_index[$tel['adrid']]]['tels'][] = $tel;
112 }
113 foreach ($array['adresse'] as $i => $adr) {
114 unset($lasttel);
115 foreach ($adr['tels'] as $j => $t) {
116 if (!isset($array['adresse'][$i]['tel']) && (strpos($t['tel_type'], 'Tél') === 0)) {
117 $array['adresse'][$i]['tel'] = $t['tel'];
118 } elseif (!isset($array['adresse'][$i]['fax'])
119 && (strpos($t['tel_type'], 'Fax') === 0)) {
120 $array['adresse'][$i]['fax'] = $t['tel'];
121 } else {
122 $lasttel = $t['tel'];
123 }
124 if (isset($array['adresse'][$i]['tel']) && isset($array['adresse'][$i]['fax'])) {
125 break;
126 }
127 }
128 if (!isset($array['adresse'][$i]['tel']) && isset($lasttel)) {
129 $array['adresse'][$i]['tel'] = $lasttel;
130 } elseif (!isset($array['adresse'][$i]['fax']) && isset($lasttel)) {
131 $array['adresse'][$i]['fax'] = $lasttel;
132 }
133 unset($array['adresse'][$i]['adrid']);
134 unset($array['adresse'][$i]['tels']);
135 }
136 } else {
137 $array = false;
138 }
139 }
140
141 if ($array) { // We did get a result: the identification number was rigth.
142
143 // We only send the age to manageurs.com; the format is YYYY-MM-DD 0123-56-89.
144 $year = (int) substr($array['age'], 0, 4);
145 $month = (int) substr($array['age'], 5, 2);
146 $day = (int) substr($array['age'], 8, 2);
147 $age = (int) date('Y') - $year - 1;
148 if(( $month < (int)date('m')) ||
149 (($month == (int)date('m')) && ($day >= (int)date('d')))) {
150 $age += 1;
151 }
152 $array['age'] = $age;
153
154 // We start the encryption of the data.
155 if (manageurs_encrypt_init($params[1]) == 1) {
156 // We did not find the key to encryptthe data.
157 $args = array("erreur" => 3, "erreurstring" => $error_key);
158 $reply = xmlrpc_encode_request(NULL,$args);
159 } else {
160 $reply = manageurs_encrypt_array($array);
161 manageurs_encrypt_close();
162 }
163 } else {
164 // The identification number was not valid.
165 $args = array("erreur" => 2, "erreurstring" => $erreur_mat);
166 $reply = xmlrpc_encode_request(NULL,$args);
167 }
168 } else {
169 // The identification number was not in argument.
170 $args = array("erreur" => 1, "erreurstring" => $error_mat);
171 $reply = xmlrpc_encode_request(NULL, $args);
172 }
173
174 return $reply;
175 }
176
177 function get_nouveau_infos($method, $params) {
178 global $error_mat, $error_key, $globals;
179 // Password verification.
180 if(!isset($params[0]) || ($params[0] != $globals->manageurs->manageurs_pass)) {
181 return false;
182 }
183 // We check we actually have an identification number.
184 if(!empty($params[1])) {
185 $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES);
186 $nameTypes = array_flip($nameTypes);
187
188 $res = XDB::query("SELECT pnl.name AS nom, pnu.name AS nom_usage, pnf.name AS prenom,
189 p.sex = 'female' AS femme, p.deathdate IS NOT NULL AS decede,
190 p.birthdate, pd.promo, CONCAT(e.email, '@', d.name) AS mail
191 FROM profiles AS p
192 INNER JOIN account_profiles AS ap ON (p.pid = ap.pid AND FIND_IN_SET('owner', perms)
193 INNER JOIN email_source_account AS s ON (s.uid = ap.uid AND FIND_IN_SET('bestalias', s.flags))
194 INNER JOIN email_virtual_domains AS d ON (s.domain = s.id)
195 INNER JOIN profile_display AS pd PN (p.pid = pd.pid)
196 INNER JOIN profile_name AS pnl ON (p.pid = pnl.pid AND pnl.typeid = {?})
197 INNER JOIN profile_name AS pnf ON (p.pid = pnf.pid AND pnf.typeid = {?})
198 INNER JOIN profile_name AS pnu ON (p.pid = pnu.pid AND pnu.typeid = {?})
199 WHERE a.flags = 'bestalias' AND p.xorg_id = {?}",
200 $nameTypes['name_ini'], $nameTypes['lastname_ordinary'],
201 $nameTypes['firstname_ini'], $params[1]);
202 // $data['mail'] .= '@polytechnique.org';
203
204
205 // We start the encryption of the data.
206 if (manageurs_encrypt_init($params[1]) == 1) {
207 // We did not find the key to encryptthe data.
208 $args = array("erreur" => 3, "erreurstring" => $error_key);
209 $reply = xmlrpc_encode_request(NULL, $args);
210 } else {
211 $reply = manageurs_encrypt_array($data);
212 manageurs_encrypt_close();
213 }
214
215 } else {
216 $reply = false;
217 }
218 return $reply;
219 }
220
221 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
222 ?>