Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
0337d704 | 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 | ||
23 | require_once('applis.func.inc.php'); | |
24 | ||
25 | function replace_ifset(&$var,$req) { | |
26 | if (Env::has($req)){ | |
5e2307dc | 27 | $var = Env::v($req); |
0337d704 | 28 | } |
29 | } | |
30 | ||
31 | function replace_ifset_i(&$var,$req,$i) { | |
32 | if (isset($_REQUEST[$req][$i])){ | |
33 | $var[$i] = $_REQUEST[$req][$i]; | |
34 | } | |
35 | } | |
36 | ||
37 | function replace_ifset_i_j(&$var,$req,$i,$j) { | |
38 | if (isset($_REQUEST[$req][$j])){ | |
39 | $var[$i] = $_REQUEST[$req][$j]; | |
40 | } | |
41 | } | |
42 | ||
43 | //pour rentrer qqchose dans la base | |
44 | function put_in_db($string){ | |
45 | return trim($string); | |
46 | } | |
47 | ||
dd502514 | 48 | // example of use for diff_user_details : get $b from database, $a from other site |
eaf30d86 | 49 | // calculate diff $c and add $c in database (with set_user_details) |
dd502514 | 50 | function diff_user_details(&$a, &$b, $view = 'private') { // compute $c = $a - $b |
51 | // if (!isset($b) || !$b || !is_array($b) || count($b) == 0) | |
52 | // return $a; | |
53 | // if (!isset($a) || !$a || !is_array($a)) | |
54 | // $c = array(); | |
55 | // else | |
56 | $c = $a; | |
57 | foreach ($b as $val => $bvar) { | |
58 | if (isset($a[$val])) { | |
59 | if ($a[$val] == $bvar) | |
60 | unset($c[$val]); | |
61 | else { | |
62 | switch ($val) { | |
63 | case 'adr' : if (!($c['adr'] = diff_user_addresses($a[$val], $bvar, $view))) unset($c['adr']); break; | |
64 | case 'adr_pro' : if (!($c['adr_pro'] = diff_user_pros($a[$val], $bvar, $view))) unset($c['adr_pro']); break; | |
b235d980 | 65 | case 'tels' : if (!($c['tels'] = diff_user_tels($a[$val], $bvar, $view))) unset($c['tels']); break; |
dd502514 | 66 | } |
67 | } | |
68 | } | |
69 | } | |
b235d980 | 70 | // don't modify freetext if you don't have the right |
dd502514 | 71 | if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext'])) |
72 | unset($c['freetext']); | |
73 | if (!count($c)) | |
74 | return false; | |
75 | return $c; | |
76 | } | |
77 | ||
78 | function same_tel(&$a, &$b) { | |
bde2be3b GB |
79 | $numbera = format_phone_number((string) $a); |
80 | $numberb = format_phone_number((string) $b); | |
dd502514 | 81 | return $numbera === $numberb; |
82 | } | |
83 | function same_address(&$a, &$b) { | |
84 | return | |
85 | (same_field($a['adr1'],$b['adr1'])) && | |
bde2be3b GB |
86 | (same_field($a['adr2'],$b['adr2'])) && |
87 | (same_field($a['adr3'],$b['adr3'])) && | |
dd502514 | 88 | (same_field($a['postcode'],$b['postcode'])) && |
89 | (same_field($a['city'],$b['city'])) && | |
90 | (same_field($a['countrytxt'],$b['countrytxt'])) && | |
91 | true; | |
92 | } | |
93 | function same_pro(&$a, &$b) { | |
94 | return | |
95 | (same_field($a['entreprise'],$b['entreprise'])) && | |
96 | (same_field($a['fonction'],$b['fonction'])) && | |
97 | true; | |
98 | } | |
99 | function same_field(&$a, &$b) { | |
100 | if ($a == $b) return true; | |
101 | if (is_array($a)) { | |
102 | if (!is_array($b) || count($a) != count($b)) return false; | |
103 | foreach ($a as $val => $avar) | |
104 | if (!isset($b[$val]) || !same_field($avar, $b[$val])) return false; | |
105 | return true; | |
106 | } elseif (is_string($a)) | |
107 | return (strtoupper($a) == strtoupper($b)); | |
108 | } | |
109 | function diff_user_tel(&$a, &$b) { | |
110 | $c = $a; | |
111 | if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub'])) | |
112 | $c['tel_pub'] = $b['tel_pub']; | |
113 | foreach ($b as $val => $bvar) { | |
114 | if (isset($a[$val])) { | |
115 | if ($a[$val] == $bvar) | |
116 | unset($c[$val]); | |
117 | } | |
118 | } | |
119 | if (!count($c)) | |
120 | return false; | |
121 | $c['telid'] = $a['telid']; | |
122 | return $c; | |
123 | } | |
124 | ||
b235d980 GB |
125 | function diff_user_tels(&$a, &$b) |
126 | { | |
127 | $c = $a; | |
128 | $telids_b = array(); | |
129 | foreach ($b as $i => $telb) $telids_b[$telb['telid']] = $i; | |
dd502514 | 130 | |
b235d980 GB |
131 | foreach ($a as $j => $tela) { |
132 | if (isset($tela['telid'])) { | |
133 | // if b has a tel with the same telid, compute diff | |
134 | if (isset($telids_b[$tela['telid']])) { | |
135 | if (!($c[$j] = diff_user_tel($tela, $b[$telids_b[$tela['adrid']]]))) { | |
136 | unset($c[$j]); | |
dd502514 | 137 | } |
b235d980 GB |
138 | unset($telids_b[$tela['telid']]); |
139 | } | |
140 | } else { | |
141 | // try to find a match in b | |
142 | foreach ($b as $i => $telb) { | |
143 | if (same_tel($tela['tel'], $telb['tel'])) { | |
144 | $tela['telid'] = $telb['telid']; | |
145 | if (!($c[$j] = diff_user_tel($tela, $telb))) { | |
146 | unset($c[$j]); | |
dd502514 | 147 | } |
b235d980 GB |
148 | unset($telids_b[$tela['telid']]); |
149 | break; | |
dd502514 | 150 | } |
151 | } | |
152 | } | |
b235d980 | 153 | } |
eaf30d86 | 154 | |
b235d980 GB |
155 | foreach ($telids_b as $telidb => $i) |
156 | $c[] = array('telid' => $telidb, 'remove' => 1); | |
157 | return $c; | |
158 | } | |
159 | ||
160 | function diff_user_address($a, $b) { | |
161 | if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub'])) | |
162 | $a['pub'] = $b['pub']; | |
163 | if (isset($b['tels'])) { | |
164 | if (isset($a['tels'])) { | |
165 | $avar = $a['tels']; | |
166 | } else { | |
167 | $avar = array(); | |
168 | } | |
169 | $ctels = diff_user_tels($avar, $b['tels']); | |
dd502514 | 170 | |
171 | if (!count($ctels)) { | |
172 | $b['tels'] = $avar; | |
b235d980 | 173 | } else { |
dd502514 | 174 | $a['tels'] = $ctels; |
b235d980 | 175 | } |
dd502514 | 176 | } |
177 | ||
178 | foreach ($a as $val => $avar) { | |
179 | if (!isset($b[$val]) || !same_field($avar,$b[$val])) { | |
180 | return $a; | |
181 | } | |
182 | } | |
183 | return false; | |
184 | } | |
185 | ||
186 | // $b need to use adrids | |
187 | function diff_user_addresses(&$a, &$b) { | |
188 | $c = $a; | |
189 | $adrids_b = array(); | |
eaf30d86 PH |
190 | foreach ($b as $i => $adrb) $adrids_b[$adrb['adrid']] = $i; |
191 | ||
dd502514 | 192 | foreach ($a as $j => $adra) { |
193 | if (isset($adra['adrid'])) { | |
194 | // if b has an address with the same adrid, compute diff | |
195 | if (isset($adrids_b[$adra['adrid']])) { | |
196 | if (!($c[$j] = diff_user_address($adra, $b[$adrids_b[$adra['adrid']]]))) | |
197 | unset($c[$j]); | |
198 | unset($adrids_b[$adra['adrid']]); | |
199 | } | |
200 | } else { | |
201 | // try to find a match in b | |
202 | foreach ($b as $i => $adrb) { | |
203 | if (same_address($adra, $adrb)) { | |
204 | $adra['adrid'] = $adrb['adrid']; | |
205 | if (!($c[$j] = diff_user_address($adra, $adrb))) | |
206 | unset($c[$j]); | |
207 | if ($c[$j]) $c[$j]['adrid'] = $adra['adrid']; | |
208 | unset($adrids_b[$adra['adrid']]); | |
209 | break; | |
210 | } | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | foreach ($adrids_b as $adridb => $i) | |
216 | $c[] = array('adrid' => $adridb, 'remove' => 1); | |
217 | ||
218 | if (!count($c)) return false; | |
219 | return $c; | |
220 | } | |
221 | ||
222 | function diff_user_pro($a, &$b, $view = 'private') { | |
223 | if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub'])) | |
224 | $a['pub'] = $b['pub']; | |
225 | if (isset($b['adr_pub']) && !has_user_right($b['adr_pub'], $view)) { | |
226 | unset($a['adr1']); | |
227 | unset($a['adr2']); | |
228 | unset($a['adr3']); | |
229 | unset($a['postcode']); | |
230 | unset($a['city']); | |
231 | unset($a['countrytxt']); | |
232 | unset($a['region']); | |
233 | } | |
234 | if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub'])) | |
235 | $a['adr_pub'] = $b['adr_pub']; | |
b235d980 GB |
236 | if (isset($b['tels'])) { |
237 | if (isset($a['tels'])) | |
238 | $avar = $a['tels']; | |
239 | else | |
240 | $avar = array(); | |
241 | $ctels = diff_user_tels($avar, $b['tels']); | |
242 | ||
243 | if (!count($ctels)) { | |
244 | $b['tels'] = $avar; | |
245 | } else | |
246 | $a['tels'] = $ctels; | |
dd502514 | 247 | } |
dd502514 | 248 | if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view)) |
249 | unset($a['email']); | |
250 | if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub'])) | |
251 | $a['email_pub'] = $b['email_pub']; | |
252 | foreach ($a as $val => $avar) { | |
253 | if (($avar && !isset($b[$val])) || !same_field($avar,$b[$val])) { | |
254 | return $a; | |
255 | } | |
256 | } | |
257 | return false; | |
258 | } | |
259 | ||
260 | // $b need to use entrids | |
261 | function diff_user_pros(&$a, &$b, $view = 'private') { | |
262 | $c = $a; | |
263 | $entrids_b = array(); | |
264 | foreach ($b as $i => $prob) $entrids_b[$prob['entrid']] = $i; | |
265 | ||
266 | foreach ($a as $j => $proa) { | |
267 | if (isset($proa['entrid'])) { | |
268 | // if b has an address with the same adrid, compute diff | |
269 | if (isset($entrids_b[$proa['entrid']])) { | |
270 | if (!($c[$j] = diff_user_pro($proa, $b[$entrids_b[$proa['entrid']]], $view))) | |
271 | unset($c[$j]); | |
272 | unset($entrids_b[$proa['entrid']]); | |
273 | } | |
274 | } else { | |
275 | // try to find a match in b | |
276 | foreach ($b as $i => $prob) { | |
277 | if (same_pro($proa, $prob)) { | |
278 | $proa['entrid'] = $prob['entrid']; | |
279 | if (!($c[$j] = diff_user_pro($proa, $prob, $view))) | |
280 | unset($c[$j]); | |
281 | if ($c[$j]) $c[$j]['entrid'] = $proa['entrid']; | |
282 | unset($entrids_b[$proa['entrid']]); | |
283 | break; | |
284 | } | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | foreach ($entrids_b as $entridb => $i) | |
290 | $c[] = array('entrid' => $entridb, 'remove' => 1); | |
291 | ||
292 | if (!count($c)) return false; | |
293 | return $c; | |
294 | } | |
295 | ||
de15699b GB |
296 | function format_phone_number($tel) |
297 | { | |
298 | $tel = trim($tel); | |
299 | if (substr($tel, 0, 3) === '(0)') { | |
300 | $tel = '33' . $tel; | |
301 | } | |
302 | $tel = preg_replace('/\(0\)/', '', $tel); | |
303 | $tel = preg_replace('/[^0-9]/', '', $tel); | |
304 | if (substr($tel, 0, 2) === '00') { | |
305 | $tel = substr($tel, 2); | |
306 | } else if(substr($tel, 0, 1) === '0') { | |
307 | $tel = '33' . substr($tel, 1); | |
308 | } | |
309 | return $tel; | |
310 | } | |
311 | ||
68e6f28f | 312 | function format_display_number($tel, &$error, $format = array('format'=>'','phoneprf'=>'')) |
de15699b GB |
313 | { |
314 | $error = false; | |
315 | $ret = ''; | |
316 | $tel_length = strlen($tel); | |
68e6f28f | 317 | if((!isset($format['phoneprf'])) || ($format['phoneprf'] == '')) { |
b235d980 GB |
318 | $res = XDB::query("SELECT phoneprf, phoneformat AS format |
319 | FROM geoloc_pays | |
320 | WHERE phoneprf = {?} OR phoneprf = {?} OR phoneprf = {?} | |
321 | LIMIT 1", | |
68e6f28f GB |
322 | substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3)); |
323 | if ($res->numRows() == 0) { | |
324 | $error = true; | |
bde2be3b | 325 | return '+' . $tel; |
68e6f28f GB |
326 | } |
327 | $format = $res->fetchOneAssoc(); | |
de15699b | 328 | } |
de15699b GB |
329 | if ($format['format'] == '') { |
330 | $format['format'] = '+p'; | |
331 | } | |
332 | $j = 0; | |
333 | $i = strlen($format['phoneprf']); | |
334 | $length_format = strlen($format['format']); | |
335 | while (($i < $tel_length) && ($j < $length_format)){ | |
336 | if ($format['format'][$j] == '#'){ | |
337 | $ret .= $tel[$i]; | |
338 | $i++; | |
339 | } else if ($format['format'][$j] == 'p') { | |
340 | $ret .= $format['phoneprf']; | |
341 | } else { | |
342 | $ret .= $format['format'][$j]; | |
343 | } | |
344 | $j++; | |
345 | } | |
346 | for (; $i < $tel_length - 1; $i += 2) { | |
347 | $ret .= ' ' . substr($tel, $i, 2); | |
348 | } | |
349 | //appends last alone number to the last block | |
350 | if ($i < $tel_length) { | |
351 | $ret .= substr($tel, $i); | |
352 | } | |
353 | return $ret; | |
354 | } | |
355 | ||
a7de4ef7 | 356 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 357 | ?> |