Adapts paypal and manageur scripts to new address tables.
[platal.git] / include / webservices / manageurs.server.inc.php
CommitLineData
0337d704 1<?php
2
3require_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...";
8f60459a
SJ
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
0337d704 17function get_annuaire_infos($method, $params) {
18 global $error_mat, $error_key, $globals;
19
8f60459a
SJ
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 }
0337d704 28
8f60459a
SJ
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])) {
9e319def
SJ
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]);
79a5acea 39 $array = $res->next();
8f60459a 40 } else {
c9a1b3ff
SJ
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)
9e319def
SJ
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),
c9a1b3ff
SJ
56 NOT FIND_IN_SET('mail', pa.flags)
57 GROUP BY pa.pid, pa.jobid, pa.groupid, pa.id, pa.type",
9e319def 58 $params[1]);
8f60459a
SJ
59 // Process the addresses we got.
60 if(list($age, $text, $adr['cp'], $adr['ville'],
9e319def 61 $adr['pays'], $pid, $adr['adrid']) = $res->next()) {
8f60459a
SJ
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
ce0b2c6f 66 WHERE pid = {?} AND link_type = 'user' AND tel_type = 'mobile'
9e319def 67 LIMIT 1", $pid);
8f60459a 68 if ($sql->numRows() > 0) {
b235d980
GB
69 $array['cell'] = $sql->fetchOneCell();
70 } else {
71 $array['cell'] ='';
72 }
0337d704 73 $array['age'] = $age;
74 $array['adresse'][] = $adr;
75
79a5acea 76
8f60459a 77 // We limit the address number by the number of available addresses.
0337d704 78 $adresse = min((int) $params[2], $res->total());
79
8f60459a 80 if ($adresse != 1) { // We don't want the first address.
0337d704 81 $i = 2;
8f60459a
SJ
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.
0337d704 88 $array['adresse'][0] = $adr;
89 //$res->free();
90 break;
8f60459a
SJ
91 } elseif ($adresse == 0) {
92 // If we want every address.
0337d704 93 $array['adresse'][] = $adr;
94 }
95 $i++;
96 }
97 }
eaf30d86 98
8f60459a 99 // We add the phone numbers.
79a5acea 100 $adrid_index = array();
8f60459a
SJ
101 foreach ($array['adresse'] as $i => $a) {
102 $adrid_index[$a['adrid']] = $i;
103 }
9e319def
SJ
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);
8f60459a
SJ
110 while ($tel = $restel->next()) {
111 $array['adresse'][$adrid_index[$tel['adrid']]]['tels'][] = $tel;
112 }
79a5acea 113 foreach ($array['adresse'] as $i => $adr) {
114 unset($lasttel);
8f60459a
SJ
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 }
79a5acea 127 }
8f60459a 128 if (!isset($array['adresse'][$i]['tel']) && isset($lasttel)) {
79a5acea 129 $array['adresse'][$i]['tel'] = $lasttel;
8f60459a 130 } elseif (!isset($array['adresse'][$i]['fax']) && isset($lasttel)) {
79a5acea 131 $array['adresse'][$i]['fax'] = $lasttel;
8f60459a 132 }
79a5acea 133 unset($array['adresse'][$i]['adrid']);
134 unset($array['adresse'][$i]['tels']);
135 }
8f60459a 136 } else {
0337d704 137 $array = false;
138 }
139 }
0337d704 140
8f60459a 141 if ($array) { // We did get a result: the identification number was rigth.
0337d704 142
8f60459a
SJ
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);
0337d704 147 $age = (int) date('Y') - $year - 1;
148 if(( $month < (int)date('m')) ||
8f60459a 149 (($month == (int)date('m')) && ($day >= (int)date('d')))) {
0337d704 150 $age += 1;
151 }
152 $array['age'] = $age;
153
8f60459a
SJ
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.
0337d704 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 }
8f60459a
SJ
163 } else {
164 // The identification number was not valid.
0337d704 165 $args = array("erreur" => 2, "erreurstring" => $erreur_mat);
166 $reply = xmlrpc_encode_request(NULL,$args);
167 }
8f60459a
SJ
168 } else {
169 // The identification number was not in argument.
0337d704 170 $args = array("erreur" => 1, "erreurstring" => $error_mat);
8f60459a 171 $reply = xmlrpc_encode_request(NULL, $args);
436e5ff3 172 }
eaf30d86
PH
173
174 return $reply;
175}
0337d704 176
436e5ff3 177function get_nouveau_infos($method, $params) {
178 global $error_mat, $error_key, $globals;
8f60459a
SJ
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])) {
9e319def
SJ
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,
c0436d0b 190 p.birthdate, pd.promo, CONCAT(e.email, '@', d.name) AS mail
9e319def
SJ
191 FROM profiles AS p
192 INNER JOIN account_profiles AS ap ON (p.pid = ap.pid AND FIND_IN_SET('owner', perms)
c0436d0b
SJ
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)
9e319def
SJ
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';
436e5ff3 203
eaf30d86 204
8f60459a
SJ
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.
436e5ff3 208 $args = array("erreur" => 3, "erreurstring" => $error_key);
8f60459a 209 $reply = xmlrpc_encode_request(NULL, $args);
436e5ff3 210 } else {
211 $reply = manageurs_encrypt_array($data);
212 manageurs_encrypt_close();
213 }
214
8f60459a
SJ
215 } else {
216 $reply = false;
436e5ff3 217 }
8f60459a 218 return $reply;
436e5ff3 219}
220
a7de4ef7 221// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 222?>