0337d704 |
1 | <?php |
2 | /*************************************************************************** |
50a40a33 |
3 | * Copyright (C) 2003-2006 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 | function generate_new_adrid(){ |
24 | global $adresses; |
25 | if(!isset($adresses)) //aucune adresses => retourne 1 |
26 | return 1; |
27 | reset($adresses); |
28 | $i = 0; |
29 | $adrid_array = Array(); |
30 | foreach($adresses as $numero => $adr){ |
31 | $adrid_array[$i] = $numero; |
32 | $i++; |
33 | } |
34 | sort($adrid_array,SORT_NUMERIC); // classe les adrid dans l'ordre croissant |
35 | $new_adrid = 1; |
36 | while(list($key,$current_adrid) = each($adrid_array)){ |
37 | if($current_adrid == $new_adrid) |
38 | $new_adrid++; |
39 | else |
40 | return $new_adrid; //s'ils sont differents, il y a un trou dans la liste des adrid donc new_adrid convient |
41 | } |
42 | //si aucun convient, on retourne le plus grand des adrid actuel + 1 |
43 | return $new_adrid; |
44 | } |
45 | |
79a5acea |
46 | function generate_new_telid($adr){ |
47 | $i = 0; |
48 | $telid_array = Array(); |
49 | foreach($adr['tels'] as $tel){ |
50 | $telid_array[$i] = $tel['telid']; |
51 | $i++; |
52 | } |
53 | sort($telid_array,SORT_NUMERIC); // classe les adrid dans l'ordre croissant |
54 | $new_telid = 0; |
55 | foreach($telid_array as $current_telid) |
56 | if ($current_telid == $new_telid) |
57 | $new_telid ++; |
58 | else |
59 | return $new_telid;//s'ils sont differents, il y a un trou dans la liste des telid donc new_telid convient |
60 | //si aucun convient, on retourne le plus grand des telid actuel + 1 |
61 | return $new_telid; |
62 | } |
63 | |
0337d704 |
64 | function replace_ifset_adr($varname, $i){ |
65 | $tab = Env::getMixed($varname, Array()); |
66 | if (isset($tab[$i])) |
67 | $GLOBALS['adresses'][$i][$varname] = $tab[$i]; |
68 | } |
69 | |
79a5acea |
70 | function replace_ifset_tel($varname, $i, $t){ |
71 | $tab = Env::getMixed($varname.$t, Array()); |
72 | if (isset($tab[$i])) |
73 | $GLOBALS['adresses'][$i]['tels'][$t][$varname] = $tab[$i]; |
74 | } |
75 | |
0337d704 |
76 | function get_adr_arg($varname, $i) { |
77 | $tab = Env::getMixed($varname, Array()); |
78 | return $tab[$i]; |
79 | } |
80 | |
81 | function set_flag_adr($varname,$i){ |
82 | $tab = Env::getMixed($varname, Array()); |
83 | if (isset($tab[$i])){ |
84 | $GLOBALS['adresses'][$i][$varname] = 1; |
85 | } |
86 | else |
87 | $GLOBALS['adresses'][$i][$varname] = '0'; |
88 | } |
89 | |
79a5acea |
90 | function replace_tel($i, $t){ |
91 | replace_ifset_tel('telid', $i, $t); |
92 | replace_ifset_tel('tel', $i, $t); |
93 | replace_ifset_tel('tel_pub', $i, $t); |
94 | replace_ifset_tel('tel_type', $i, $t); |
95 | replace_ifset_tel('new_tel', $i, $t); |
96 | } |
0337d704 |
97 | |
98 | function replace_address($i){ |
79a5acea |
99 | global $adresses, $nb_tel_max; |
0337d704 |
100 | if(!isset($adresses[$i])){ |
101 | $adresses[$i]['nouvelle'] = 'ajout'; |
102 | $adresses[$i]['adrid'] = $i; |
103 | } |
104 | |
105 | replace_ifset_adr('secondaire', $i); |
106 | set_flag_adr('courrier', $i); |
107 | replace_ifset_adr('temporaire', $i); |
108 | if(Env::getInt('adrid_active', $i+1) == $i) |
109 | $adresses[$i]['active'] = 1; |
110 | else |
111 | $adresses[$i]['active'] = 0; |
112 | replace_ifset_adr('adr1', $i); |
113 | replace_ifset_adr('adr2', $i); |
114 | replace_ifset_adr('adr3', $i); |
115 | replace_ifset_adr('postcode', $i); |
116 | replace_ifset_adr('city', $i); |
117 | replace_ifset_adr('cityid', $i); |
118 | replace_ifset_adr('country', $i); |
119 | replace_ifset_adr('region', $i); |
0337d704 |
120 | replace_ifset_adr('pub', $i); |
79a5acea |
121 | |
122 | for ($telid = 0; $telid <= $nb_tel_max; $telid++) { |
123 | $tab = Env::getMixed('telid'.$telid, Array()); |
124 | if(isset($tab[$i])){ //ce telid etait donc present dans le formulaire |
125 | replace_tel($i, $telid); |
126 | } |
127 | } |
128 | |
0337d704 |
129 | if (!get_adr_arg('parsevalid', $i)) replace_ifset_adr('txt', $i); |
130 | $tab = Env::getMixed('numero_formulaire', Array()); |
131 | if($tab[$i]) |
132 | $adresses[$i]['numero_formulaire'] = $tab[$i]; |
133 | else |
134 | $adresses[$i]['numero_formulaire'] = -1; |
135 | } |
136 | |
137 | function geoloc_adresse($i) { |
138 | global $adresses; |
139 | $change = Env::get('change'.$i); |
140 | if (get_adr_arg('parsevalid', $i) || ($adresses[$i]['txt'] && $change) || (!$adresses[$i]['cityid'])) { |
141 | require_once('geoloc.inc.php'); |
142 | // erases the previous address (but not the phone or pub) |
143 | $adresses[$i] = array_merge($adresses[$i], empty_address()); |
144 | // localize new address |
145 | $new = get_address_infos($adresses[$i]['txt']); |
146 | if (compare_addresses_text($adresses[$i]['txt'], $geotxt = get_address_text($new)) || get_adr_arg('parsevalid', $i)) |
147 | $adresses[$i] = array_merge($adresses[$i], $new); |
148 | else { |
149 | $adresses[$i] = array_merge($adresses[$i], cut_address($adresses[$i]['txt'])); |
150 | $adresses[$i]['geoloc'] = $geotxt; |
151 | $adresses[$i]['geoloc_cityid'] = $new['cityid']; |
152 | } |
153 | } |
154 | $adresses[$i]['txt'] = get_address_text($adresses[$i]); |
155 | } |
156 | |
157 | //remplace par les eventuelles nouvelles valeurs : |
158 | for ($adrid = 1; $adrid <= $nb_adr_max; $adrid++) { |
159 | $tab = Env::getMixed('adrid', Array()); |
160 | if(isset($tab[$adrid])){ //cet adrid etait donc present dans le formulaire |
161 | replace_address($adrid); |
162 | } |
163 | if (isset($adresses[$adrid]['txt'])) |
164 | geoloc_adresse($adrid); |
165 | } |
166 | |
167 | if(Env::get('old_tab', '') == 'adresses' && Env::has('modifier')){ // on ne valide que qd on vient du formulaire |
168 | $adresses_principales = 0; |
169 | reset($adresses); |
170 | foreach($adresses as $adrid => $adr) { |
171 | //validité de chaque adresse |
172 | $description = (($adr['numero_formulaire'] > 0)?"Adresse n°{$adr['numero_formulaire']}":"Nouvelle adresse"); |
173 | if (strlen(strtok($adr['adr1'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr1'])) |
174 | { |
79a5acea |
175 | $page->trig("Le champ '$description - Ligne 1' contient un caractère interdit."); |
0337d704 |
176 | } |
177 | if (strlen(strtok($adr['adr2'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr2'])) |
178 | { |
79a5acea |
179 | $page->trig("Le champ '$description - Ligne 2' contient un caractère interdit."); |
0337d704 |
180 | } |
181 | if (strlen(strtok($adr['adr3'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr3'])) |
182 | { |
79a5acea |
183 | $page->trig("Le champ '$description - Ligne 3' contient un caractère interdit."); |
0337d704 |
184 | } |
185 | if (strlen(strtok($adr['postcode'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode'])) |
186 | { |
79a5acea |
187 | $page->trig("Le champ '$description - Code Postal' contient un caractère interdit."); |
0337d704 |
188 | } |
189 | if (strlen(strtok($adr['city'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode'])) |
190 | { |
79a5acea |
191 | $page->trig("Le champ '$description - Ville' contient un caractère interdit."); |
0337d704 |
192 | } |
79a5acea |
193 | foreach ($adr['tels'] as $tel) { |
194 | if (strlen(strtok($tel['tel'],"<>{}@&#~\/:;?,!§*_`[]|%$^=\"")) < strlen($tel['tel'])) |
195 | { |
196 | $page->trig("Le champ '$description - ".$tel['tel_type']."' contient un caractère interdit."); |
197 | } |
0337d704 |
198 | } |
199 | if(!$adr['secondaire']){ |
200 | if($adresses_principales == 1){ //deja une adresse principale |
79a5acea |
201 | $page->trig("Tu ne peux avoir qu'une résidence principale."); |
0337d704 |
202 | $adresses_principales++;//pour eviter de repeter le message plusieurs fois |
203 | } |
204 | else $adresses_principales = 1; |
205 | } |
206 | } |
207 | |
208 | } |
209 | |
210 | //on vire les adresses vides : |
211 | if(isset($adresses)){ // s'il y en a |
212 | reset($adresses); |
213 | foreach($adresses as $adrid => $adr){ |
79a5acea |
214 | // on vire les tels vides |
215 | foreach ($adr['tels'] as $telid => $tel) { |
216 | if ($tel['tel'] == '') unset($adresses[$adrid]['tels'][$telid]); |
217 | } |
0337d704 |
218 | if(is_adr_empty($adrid)){ |
219 | delete_address($adrid); |
220 | } |
221 | } |
222 | } |
0337d704 |
223 | |
224 | ?> |