2 /***************************************************************************
3 * Copyright (C) 2003-2004 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
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é
27 function geoloc_country($current) {
29 $res = $globals->xdb
->iterRow('SELECT a2,pays FROM geoloc_pays ORDER BY pays');
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);
38 function _geoloc_country_smarty($params){
39 if(!isset($params['country']))
41 return geoloc_country($params['country']);
43 $page->register_function('geoloc_country', '_geoloc_country_smarty');
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
49 function geoloc_region($country,$current) {
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);
60 function _geoloc_region_smarty($params){
61 if(!isset($params['country']))
63 if(!isset($params['region']))
65 return geoloc_region($params['country'], $params['region']);
67 $page->register_function('geoloc_region', '_geoloc_region_smarty');
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
75 function 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));
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) => "œ")));
84 $globals->xdb
->execute("REPLACE INTO geoloc_city VALUES ".$infos['sql']);
89 // {{{ get_address_text($adr)
90 /** make the text of an address that can be read by a mailman
91 * @param $adr an array with all the usual fields
93 function get_address_text($adr) {
95 if ($adr['adr1']) $t.= $adr['adr1'];
96 if ($adr['adr2']) $t.= "\n".$adr['adr2'];
97 if ($adr['adr3']) $t.= "\n".$adr['adr3'];
99 if ($adr['country'] == 'US' ||
$adr['country'] == 'CA') {
100 if ($adr['city']) $l .= $adr['city'].",\n";
101 if ($adr['region']) $l .= $adr['region']." ";
102 if ($adr['postcode']) $l .= $adr['postcode'];
104 if ($adr['postcode']) $l .= $adr['postcode']." ";
105 if ($adr['city']) $l .= $adr['city'];
107 if ($l) $t .= "\n".trim($l);
108 if ($adr['country'] != '00' && (!$adr['countrytxt'] ||
$adr['countrytxt'] == strtoupper($adr['countrytxt']))) {
110 $res = $globals->xdb
->query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']);
111 $adr['countrytxt'] = $res->fetchOneCell();
113 if ($adr['countrytxt']) $t .= "\n".$adr['countrytxt'];
118 // {{{ compare_addresses_text($a, $b)
119 /** compares if two address matches
120 * @param $a the raw text of an address
121 * @param $b the raw text of a complete valid address
123 function compare_addresses_text($a, $b) {
124 $ta = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a));
125 $tb = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b));
127 $la = explode("\n", $ta);
128 $lb = explode("\n", $tb);
130 if (count($lb) > count($la) +
1) return false
;
131 foreach ($la as $i=>$l) if (levenshtein($l, $lb[$i]) > 3) return false
;
137 function empty_address() {
150 // create a simple address from a text without geoloc
151 function cut_address($txt) {
152 $txt = str_replace("\r\n", "\n", $txt);
153 ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a);
154 return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5])));
157 // {{{ localize_addresses($uid)
158 /* localize all the address of a user and modify the database
159 * if the new address match with the old one
160 * @param $uid the id of the user
162 function localize_addresses($uid) {
164 $res = $globals->xdb
->iterator("SELECT * FROM adresses WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid);
167 while ($a = $res->next()) {
168 $new = get_address_infos($ta = get_address_text($a));
169 if (compare_addresses_text($ta, get_address_text($new))) {
170 $globals->xdb
->execute("UPDATE adresses SET
171 adr1 = {?}, adr2 = {?}, adr3 = {?},
172 cityid = {?}, city = {?}, postcode = {?},
173 region = {?}, country = {?}
174 WHERE uid = {?} AND adrid = {?}",
175 $new['adr1'], $new['adr2'], $new['adr3'],
176 $new['cityid'], $new['city'], $new['postcode'],
177 $new['region'], $new['country'],
179 $new['store'] = true
;
180 if (!$new['cityid']) $erreur[$a['adrid']] = $new;
182 $new['store'] = false
;
183 $erreur[$a['adrid']] = $new;
190 // {{{ synchro_city($id)
191 /** synchronise the local geoloc_city base to geoloc.org
192 * @param $id the id of the city to synchronize
194 function synchro_city($id) {
195 $url ="http://www.geoloc.org/adressparser/cityFinder.php?method=id&id=".$id."&out=sql";
196 if (!($f = @fopen
($url, 'r'))) return false
;
200 return $globals->xdb
->execute("REPLACE INTO geoloc_city VALUES ".$s) > 0;
204 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: