first reimport from platal
[platal.git] / include / geoloc.inc.php
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 */
27 function 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
38 function _geoloc_country_smarty($params){
39 if(!isset($params['country']))
40 return;
41 return geoloc_country($params['country']);
42 }
43 $page->register_function('geoloc_country', '_geoloc_country_smarty');
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 */
49 function 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 }
60 function _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 }
67 $page->register_function('geoloc_region', '_geoloc_region_smarty');
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 */
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));
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']);
85 return $infos;
86 }
87 // }}}
88
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
92 */
93 function get_address_text($adr) {
94 $t = "";
95 if ($adr['adr1']) $t.= $adr['adr1'];
96 if ($adr['adr2']) $t.= "\n".$adr['adr2'];
97 if ($adr['adr3']) $t.= "\n".$adr['adr3'];
98 $l = "";
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'];
103 } else {
104 if ($adr['postcode']) $l .= $adr['postcode']." ";
105 if ($adr['city']) $l .= $adr['city'];
106 }
107 if ($l) $t .= "\n".trim($l);
108 if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == strtoupper($adr['countrytxt']))) {
109 global $globals;
110 $res = $globals->xdb->query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']);
111 $adr['countrytxt'] = $res->fetchOneCell();
112 }
113 if ($adr['countrytxt']) $t .= "\n".$adr['countrytxt'];
114 return trim($t);
115 }
116 // }}}
117
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
122 */
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));
126
127 $la = explode("\n", $ta);
128 $lb = explode("\n", $tb);
129
130 if (count($lb) > count($la) + 1) return false;
131 foreach ($la as $i=>$l) if (levenshtein($l, $lb[$i]) > 3) return false;
132 return true;
133 }
134
135 // }}}
136
137 function empty_address() {
138 return Array(
139 "adr1" => "",
140 "adr2" => "",
141 "adr3" => "",
142 "cityid" => NULL,
143 "city" => "",
144 "postcode" => "",
145 "region" => "",
146 "country" => "00",
147 "countrytxt" => "");
148 }
149
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])));
155 }
156
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
161 */
162 function localize_addresses($uid) {
163 global $globals;
164 $res = $globals->xdb->iterator("SELECT * FROM adresses WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid);
165 $erreur = Array();
166
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'],
178 $uid, $a['adrid']);
179 $new['store'] = true;
180 if (!$new['cityid']) $erreur[$a['adrid']] = $new;
181 } else {
182 $new['store'] = false;
183 $erreur[$a['adrid']] = $new;
184 }
185 }
186 return $erreur;
187 }
188 // }}}
189
190 // {{{ synchro_city($id)
191 /** synchronise the local geoloc_city base to geoloc.org
192 * @param $id the id of the city to synchronize
193 */
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;
197 $s = fgets($f);
198 global $globals;
199 if ($s)
200 return $globals->xdb->execute("REPLACE INTO geoloc_city VALUES ".$s) > 0;
201 }
202 // }}}
203
204 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
205 ?>