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