Bug de presentation des validations d'evenements (pas de retours à la ligne)
[platal.git] / include / geoloc.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2004 Polytechnique.org *
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// {{{ liste les pays ou les régions d'un pays
24/** donne la liste déroulante des pays
25 * @param $current pays actuellement selectionné
26 */
27function geoloc_country($current) {
28 global $globals;
29 $res = $globals->xdb->iterRow('SELECT a2,pays FROM geoloc_pays ORDER BY pays');
30 $html = "";
31 while (list($my_id, $my_pays) = $res->next()) {
32 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
33 $my_id, ($current==$my_id?"selected='selected'":""), $my_pays);
34 }
35 return $html;
36}
37
38function _geoloc_country_smarty($params){
39 if(!isset($params['country']))
40 return;
41 return geoloc_country($params['country']);
42}
80ca1b4e 43$GLOBALS['page']->register_function('geoloc_country', '_geoloc_country_smarty');
0337d704 44
45/** donne la liste deroulante des regions pour un pays
46 * @param $pays le pays dont on veut afficher les regions
47 * @param $current la region actuellement selectionnee
48 */
49function geoloc_region($country,$current) {
50 global $globals;
51 $res = $globals->xdb->iterRow('SELECT region,name FROM geoloc_region where a2={?} ORDER BY name', $country);
52 $html = "<option value=\"\"></option>";
53 while (list($regid, $regname) = $res->next()) {
54 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
55 $regid, ($current==$regid?"selected='selected'":""), $regname);
56 }
57 return $html;
58
59}
60function _geoloc_region_smarty($params){
61 if(!isset($params['country']))
62 return;
63 if(!isset($params['region']))
64 return;
65 return geoloc_region($params['country'], $params['region']);
66}
80ca1b4e 67$GLOBALS['page']->register_function('geoloc_region', '_geoloc_region_smarty');
0337d704 68// }}}
69
70// {{{ get_address_infos($txt)
71/** retrieve the infos on a text address
72 * store on the fly the info of the city concerned
73 * @param $txt the raw text of an address
74 */
75function get_address_infos($txt) {
76 $url ="http://www.geoloc.org/adressparser/address.php?txt=".urlencode(utf8_encode($txt));
77 if (!($f = @fopen($url, 'r'))) return false;
78 $keys = explode('|',fgets($f));
79 $vals = explode('|',fgets($f));
80 $infos = array();
81 foreach ($keys as $i=>$key) if($vals[$i]) $infos[$key] = ($key == 'sql')?$vals[$i]:utf8_decode(strtr($vals[$i], array(chr(197).chr(147) => "&oelig;")));
82 global $globals;
83 if ($infos['sql'])
84 $globals->xdb->execute("REPLACE INTO geoloc_city VALUES ".$infos['sql']);
80ca1b4e 85 if ($infos['display'])
86 $globals->xdb->execute("UPDATE geoloc_pays SET display = {?} WHERE a2 = {?}", $infos['display'], $infos['country']);
0337d704 87 return $infos;
88}
89// }}}
90
91// {{{ get_address_text($adr)
92/** make the text of an address that can be read by a mailman
93 * @param $adr an array with all the usual fields
94 */
95function get_address_text($adr) {
96 $t = "";
97 if ($adr['adr1']) $t.= $adr['adr1'];
98 if ($adr['adr2']) $t.= "\n".$adr['adr2'];
99 if ($adr['adr3']) $t.= "\n".$adr['adr3'];
100 $l = "";
80ca1b4e 101 if ($adr['display']) {
102 $keys = explode(' ', $adr['display']);
103 foreach ($keys as $key) {
104 if (isset($adr[$key]))
105 $l .= " ".$adr[$key];
106 else
107 $l .= " ".$key;
108 }
109 if ($l) $l = substr($l, 1);
110 }
111 else
112 {
113 if ($adr['country'] == 'US' || $adr['country'] == 'CA' || $adr['country'] == 'GB') {
114 if ($adr['city']) $l .= $adr['city'].",\n";
115 if ($adr['region']) $l .= $adr['region']." ";
116 if ($adr['postcode']) $l .= $adr['postcode'];
117 } else {
118 if ($adr['postcode']) $l .= $adr['postcode']." ";
119 if ($adr['city']) $l .= $adr['city'];
120 }
0337d704 121 }
122 if ($l) $t .= "\n".trim($l);
123 if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == strtoupper($adr['countrytxt']))) {
124 global $globals;
125 $res = $globals->xdb->query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']);
126 $adr['countrytxt'] = $res->fetchOneCell();
127 }
128 if ($adr['countrytxt']) $t .= "\n".$adr['countrytxt'];
129 return trim($t);
130}
131// }}}
132
133// {{{ compare_addresses_text($a, $b)
134/** compares if two address matches
135 * @param $a the raw text of an address
136 * @param $b the raw text of a complete valid address
137 */
138function compare_addresses_text($a, $b) {
139 $ta = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a));
140 $tb = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b));
141
142 $la = explode("\n", $ta);
143 $lb = explode("\n", $tb);
144
145 if (count($lb) > count($la) + 1) return false;
146 foreach ($la as $i=>$l) if (levenshtein($l, $lb[$i]) > 3) return false;
147 return true;
148}
149
150// }}}
151
152function empty_address() {
153 return Array(
154 "adr1" => "",
155 "adr2" => "",
156 "adr3" => "",
157 "cityid" => NULL,
158 "city" => "",
159 "postcode" => "",
160 "region" => "",
80ca1b4e 161 "regiontxt" => "",
0337d704 162 "country" => "00",
163 "countrytxt" => "");
164}
165
166// create a simple address from a text without geoloc
167function cut_address($txt) {
168 $txt = str_replace("\r\n", "\n", $txt);
169 ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a);
170 return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5])));
171}
172
173// {{{ localize_addresses($uid)
174/* localize all the address of a user and modify the database
175 * if the new address match with the old one
176 * @param $uid the id of the user
177 */
178function localize_addresses($uid) {
179 global $globals;
180 $res = $globals->xdb->iterator("SELECT * FROM adresses WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid);
181 $erreur = Array();
182
183 while ($a = $res->next()) {
184 $new = get_address_infos($ta = get_address_text($a));
185 if (compare_addresses_text($ta, get_address_text($new))) {
186 $globals->xdb->execute("UPDATE adresses SET
187 adr1 = {?}, adr2 = {?}, adr3 = {?},
188 cityid = {?}, city = {?}, postcode = {?},
80ca1b4e 189 region = {?}, regiontxt = {?}, country = {?}
0337d704 190 WHERE uid = {?} AND adrid = {?}",
191 $new['adr1'], $new['adr2'], $new['adr3'],
192 $new['cityid'], $new['city'], $new['postcode'],
80ca1b4e 193 $new['region'], $new['regiontxt'], $new['country'],
0337d704 194 $uid, $a['adrid']);
195 $new['store'] = true;
196 if (!$new['cityid']) $erreur[$a['adrid']] = $new;
197 } else {
198 $new['store'] = false;
199 $erreur[$a['adrid']] = $new;
200 }
201 }
202 return $erreur;
203}
204// }}}
205
206// {{{ synchro_city($id)
207/** synchronise the local geoloc_city base to geoloc.org
208 * @param $id the id of the city to synchronize
209 */
210 function synchro_city($id) {
211 $url ="http://www.geoloc.org/adressparser/cityFinder.php?method=id&id=".$id."&out=sql";
212 if (!($f = @fopen($url, 'r'))) return false;
213 $s = fgets($f);
214 global $globals;
215 if ($s)
216 return $globals->xdb->execute("REPLACE INTO geoloc_city VALUES ".$s) > 0;
217 }
218 // }}}
219
220// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
221?>