#173: Select countries where there is somebody
[platal.git] / include / geoloc.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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
a7de4ef7 22// {{{ liste les pays ou les régions d'un pays
23/** donne la liste déroulante des pays
24 * @param $current pays actuellement selectionné
0337d704 25 */
b536b5ae 26function geoloc_country($current, $avail_only = false)
27{
28 if ($avail_only) {
29 $res = XDB::iterRow('SELECT g.a2, g.pays
30 FROM geoloc_pays AS g
31 INNER JOIN adresses AS a ON(a.country = g.a2)
32 GROUP BY g.a2
33 ORDER BY g.pays');
34 } else {
35 $res = XDB::iterRow('SELECT a2,pays FROM geoloc_pays ORDER BY pays');
36 }
0337d704 37 $html = "";
38 while (list($my_id, $my_pays) = $res->next()) {
b536b5ae 39 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
40 $my_id, ($current==$my_id?"selected='selected'":""), $my_pays);
0337d704 41 }
42 return $html;
43}
44
b536b5ae 45function _geoloc_country_smarty($params)
46{
47 if(!isset($params['country']))
48 return;
49 return geoloc_country($params['country'], @$params['available']);
0337d704 50}
b536b5ae 51
80ca1b4e 52$GLOBALS['page']->register_function('geoloc_country', '_geoloc_country_smarty');
0337d704 53
54/** donne la liste deroulante des regions pour un pays
55 * @param $pays le pays dont on veut afficher les regions
56 * @param $current la region actuellement selectionnee
57 */
b536b5ae 58function geoloc_region($country, $current, $avail_only = false)
59{
60 if ($avail_only) {
61 $res = XDB::iterRow('SELECT r.region, r.name
62 FROM geoloc_region AS r
63 INNER JOIN adresses AS a ON (a.country = r.a2 AND a.region = r.region)
64 WHERE r.a2 = {?}
65 GROUP BY r.region
66 ORDER BY r.name', $country);
67 } else {
68 $res = XDB::iterRow('SELECT region,name
69 FROM geoloc_region
70 WHERE a2 = {?}
71 ORDER BY name', $country);
72 }
0337d704 73 $html = "<option value=\"\"></option>";
74 while (list($regid, $regname) = $res->next()) {
b536b5ae 75 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
76 $regid, ($current==$regid?"selected='selected'":""), $regname);
0337d704 77 }
78 return $html;
79
80}
b536b5ae 81
82function _geoloc_region_smarty($params)
83{
84 if(!isset($params['country']))
85 return;
86 if(!isset($params['region']))
87 return;
88 return geoloc_region($params['country'], $params['region'], @$params['available']);
0337d704 89}
80ca1b4e 90$GLOBALS['page']->register_function('geoloc_region', '_geoloc_region_smarty');
0337d704 91// }}}
92
0f0dfb0b 93function geoloc_is_utf8($text)
94{
95 return (iconv('utf-8', 'utf-8', $text) == $text);
96}
97
98function geoloc_utf8_decode($text)
99{
100 return geoloc_is_utf8($text) ? utf8_decode($text) : $text;
101}
102
0337d704 103// {{{ get_address_infos($txt)
104/** retrieve the infos on a text address
105 * store on the fly the info of the city concerned
106 * @param $txt the raw text of an address
107 */
108function get_address_infos($txt) {
56670b6a 109 global $globals;
c41e72df 110 $url = $globals->geoloc->webservice_url."address.php?precise=1&txt=".urlencode(utf8_encode($txt));
0337d704 111 if (!($f = @fopen($url, 'r'))) return false;
112 $keys = explode('|',fgets($f));
113 $vals = explode('|',fgets($f));
114 $infos = array();
2b98ac11 115 foreach ($keys as $i=>$key) {
116 if($vals[$i]) {
117 if ($key == 'sql') {
118 $infos[$key] = $vals[$i];
119 } else {
120 $val = strtr($vals[$i], array(chr(197).chr(147) => "&oelig;"));
0f0dfb0b 121 $infos[$key] = geoloc_utf8_decode($val);
2b98ac11 122 }
123 }
124 }
5ee95138 125 if (isset($infos['sql']) && $infos['sql'])
08cce2ff 126 XDB::execute("REPLACE INTO geoloc_city VALUES ".$infos['sql']);
5ee95138 127 if (isset($infos['display']) && $infos['display'])
7f3ac007 128 XDB::execute("UPDATE geoloc_pays SET display = {?} WHERE a2 = {?}", $infos['display'], $infos['country']);
5ee95138 129 if (isset($infos['cityid']))
b536b5ae 130 fix_cities_not_on_map(1, $infos['cityid']);
0337d704 131 return $infos;
132}
133// }}}
134
56670b6a 135// {{{ get_cities_maps($array)
136/* get all the maps id of the cities contained in an array */
137function get_cities_maps($array)
138{
139 global $globals;
140 implode("\n",$array);
141 $url = $globals->geoloc->webservice_url."findMaps.php?datatext=".urlencode(utf8_encode(implode("\n", $array)));
142 if (!($f = @fopen($url, 'r'))) return false;
143 $maps = array();
144 while (!feof($f))
145 {
146 $l = trim(fgets($f));
147 $tab = explode(';', $l);
148 $i = $tab[0];
149 unset($tab[0]);
150 $maps[$i] = $tab;
151 }
152 return $maps;
153}
154// }}}
155
156// {{{ get_new_maps($url)
157/** set new maps from url **/
158function get_new_maps($url)
159{
a3a049fc 160 if (!($f = @fopen($url, 'r'))) {
161 return false;
162 }
163 XDB::query('TRUNCATE TABLE geoloc_maps');
164 $s = '';
165 while (!feof($f)) {
166 $l = fgetcsv($f, 1024, ';', '"');
167 foreach ($l as $i => $val) {
168 if ($val != 'NULL') {
169 $l[$i] = '\''.addslashes($val).'\'';
170 }
171 }
172 $s .= ',('.implode(',',$l).')';
173 }
174 XDB::execute('INSERT INTO geoloc_maps VALUES '.substr($s, 1));
175 return true;
56670b6a 176}
80244bbe 177// }}}
56670b6a 178
0337d704 179// {{{ get_address_text($adr)
180/** make the text of an address that can be read by a mailman
181 * @param $adr an array with all the usual fields
182 */
183function get_address_text($adr) {
184 $t = "";
5ee95138 185 if (isset($adr['adr1']) && $adr['adr1']) $t.= $adr['adr1'];
186 if (isset($adr['adr2']) && $adr['adr2']) $t.= "\n".$adr['adr2'];
187 if (isset($adr['adr3']) && $adr['adr3']) $t.= "\n".$adr['adr3'];
0337d704 188 $l = "";
5ee95138 189 if (isset($adr['display']) && $adr['display']) {
80ca1b4e 190 $keys = explode(' ', $adr['display']);
191 foreach ($keys as $key) {
192 if (isset($adr[$key]))
193 $l .= " ".$adr[$key];
194 else
195 $l .= " ".$key;
196 }
197 if ($l) $l = substr($l, 1);
198 }
199 else
200 {
201 if ($adr['country'] == 'US' || $adr['country'] == 'CA' || $adr['country'] == 'GB') {
202 if ($adr['city']) $l .= $adr['city'].",\n";
203 if ($adr['region']) $l .= $adr['region']." ";
204 if ($adr['postcode']) $l .= $adr['postcode'];
205 } else {
5ee95138 206 if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode']." ";
207 if (isset($adr['city']) && $adr['city']) $l .= $adr['city'];
80ca1b4e 208 }
0337d704 209 }
210 if ($l) $t .= "\n".trim($l);
211 if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == strtoupper($adr['countrytxt']))) {
08cce2ff 212 $res = XDB::query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']);
0337d704 213 $adr['countrytxt'] = $res->fetchOneCell();
214 }
5ee95138 215 if (isset($adr['countrytxt']) && $adr['countrytxt']) $t .= "\n".$adr['countrytxt'];
0337d704 216 return trim($t);
217}
218// }}}
219
220// {{{ compare_addresses_text($a, $b)
221/** compares if two address matches
222 * @param $a the raw text of an address
223 * @param $b the raw text of a complete valid address
224 */
225function compare_addresses_text($a, $b) {
226 $ta = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a));
227 $tb = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b));
228
229 $la = explode("\n", $ta);
230 $lb = explode("\n", $tb);
231
232 if (count($lb) > count($la) + 1) return false;
233 foreach ($la as $i=>$l) if (levenshtein($l, $lb[$i]) > 3) return false;
234 return true;
235}
236
237// }}}
238
239function empty_address() {
240 return Array(
241 "adr1" => "",
242 "adr2" => "",
243 "adr3" => "",
244 "cityid" => NULL,
245 "city" => "",
246 "postcode" => "",
247 "region" => "",
80ca1b4e 248 "regiontxt" => "",
0337d704 249 "country" => "00",
250 "countrytxt" => "");
251}
252
253// create a simple address from a text without geoloc
254function cut_address($txt) {
255 $txt = str_replace("\r\n", "\n", $txt);
256 ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a);
257 return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5])));
258}
259
260// {{{ localize_addresses($uid)
261/* localize all the address of a user and modify the database
262 * if the new address match with the old one
263 * @param $uid the id of the user
264 */
265function localize_addresses($uid) {
08cce2ff 266 $res = XDB::iterator("SELECT * FROM adresses WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid);
0337d704 267 $erreur = Array();
268
269 while ($a = $res->next()) {
270 $new = get_address_infos($ta = get_address_text($a));
271 if (compare_addresses_text($ta, get_address_text($new))) {
08cce2ff 272 XDB::execute("UPDATE adresses SET
0337d704 273 adr1 = {?}, adr2 = {?}, adr3 = {?},
274 cityid = {?}, city = {?}, postcode = {?},
56670b6a 275 region = {?}, regiontxt = {?}, country = {?},
276 glat = {?}, glng = {?}
0337d704 277 WHERE uid = {?} AND adrid = {?}",
278 $new['adr1'], $new['adr2'], $new['adr3'],
279 $new['cityid'], $new['city'], $new['postcode'],
80ca1b4e 280 $new['region'], $new['regiontxt'], $new['country'],
56670b6a 281 $new['precise_lat'], $new['precise_lon'],
0337d704 282 $uid, $a['adrid']);
283 $new['store'] = true;
284 if (!$new['cityid']) $erreur[$a['adrid']] = $new;
285 } else {
286 $new['store'] = false;
287 $erreur[$a['adrid']] = $new;
288 }
289 }
290 return $erreur;
291}
292// }}}
293
294// {{{ synchro_city($id)
295/** synchronise the local geoloc_city base to geoloc.org
296 * @param $id the id of the city to synchronize
297 */
298 function synchro_city($id) {
56670b6a 299 global $globals;
300 $url = $globals->geoloc->webservice_url."cityFinder.php?method=id&id=".$id."&out=sql";
0337d704 301 if (!($f = @fopen($url, 'r'))) return false;
302 $s = fgets($f);
0337d704 303 if ($s)
08cce2ff 304 return XDB::execute("REPLACE INTO geoloc_city VALUES ".$s) > 0;
0337d704 305 }
306 // }}}
307
56670b6a 308// {{{ function fix_cities_not_on_map($limit)
7f3ac007 309function fix_cities_not_on_map($limit=false, $cityid=false)
56670b6a 310{
7f3ac007 311 $missing = XDB::query("SELECT c.id FROM geoloc_city AS c LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id) WHERE m.city_id IS NULL".($cityid?(" AND c.id = '".$cityid."'"):"").($limit?" LIMIT $limit":""));
56670b6a 312 $maps = get_cities_maps($missing->fetchColumn());
313 if ($maps)
314 {
315 $values = "";
316 foreach ($maps as $cityid => $maps_c)
317 foreach ($maps_c as $map_id)
318 $values .= ",($cityid, $map_id, '')";
08cce2ff 319 XDB::execute("REPLACE INTO geoloc_city_in_maps VALUES ".substr($values, 1));
56670b6a 320 }
321 else
322 return false;
014c8464 323 return true;
324}
56670b6a 325
014c8464 326function set_smallest_levels() {
08cce2ff 327 $maxlengths = XDB::iterRow("SELECT MAX(LENGTH(gm.path)), gcim.city_id
014c8464 328 FROM geoloc_city_in_maps AS gcim
329 INNER JOIN geoloc_maps AS gm
330 USING ( map_id )
331 GROUP BY gcim.city_id
332 ");
333 while (list($length, $id) = $maxlengths->next()) {
08cce2ff 334 XDB::execute("UPDATE geoloc_city_in_maps AS gcim
014c8464 335 INNER JOIN geoloc_maps AS gm USING(map_id)
336 SET gcim.infos = IF(LENGTH(gm.path) = {?}, 'smallest', '')
337 WHERE gcim.city_id = {?}", $length, $id);
338 }
56670b6a 339 return true;
340}
341// }}}
342
343
344function geoloc_to_x($lon, $lat) { return deg2rad(1) * $lon *100; }
345
346function geoloc_to_y($lon, $lat) {
b536b5ae 347 if ($lat < -75) return latToY(-75);
348 if ($lat > 75) return latToY(75);
349 return -100 * log(tan(pi()/4 + deg2rad(1)/2*$lat));
56670b6a 350}
351
352function size_of_city($nb) { $s = round(log($nb + 1)*2,2); if ($s < 1) return 1; return $s; }
353function size_of_territory($nb) { return size_of_city($nb); }
354
2b105fb6 355function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true) {
2b105fb6 356 for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) if ($SFields[$i_mapfield]->fieldFormName == 'mapid') break;
357 $SFields[$i_mapfield] = new MapSField('mapid', array('gcim.map_id'), array('adresses','geoloc_city_in_maps'), array('am','gcim'), array(getadr_join('am'), 'am.cityid = gcim.city_id'), $mapid);
358
359 $fields = new SFieldGroup(true, $SFields);
360 $where = $fields->get_where_statement();
361 if ($where) $where = " AND ".$where;
362
08cce2ff 363 $cityres = XDB::iterator("
2b105fb6 364 SELECT gc.id,
365 gc.lon / 100000 AS x, gc.lat/100000 AS y,
366 gc.name,
367 COUNT(u.user_id) AS pop,
368 SUM(u.promo % 2) AS yellow
369 FROM auth_user_md5 AS u
370 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
371 ".$fields->get_select_statement()."
b536b5ae 372 LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id)
2b105fb6 373 WHERE ".($direct?"gcim.infos = 'smallest'":"1")."
374 $where
375 GROUP BY gc.id,gc.alias ORDER BY pop DESC");
376 while ($c = $cityres->next())
377 if ($c['pop'] > 0)
378 {
379 $city = $c;
0f0dfb0b 380 // $city['name'] = geoloc_utf8_decode($city['name']);
381 if (!geoloc_is_utf8($city['name'])) {
382 $city['name'] = utf8_encode($city['name']);
383 }
2b105fb6 384 $city['x'] = geoloc_to_x($c['x'], $c['y']);
385 $city['y'] = geoloc_to_y($c['x'], $c['y']);
386 $city['size'] = size_of_city($c['pop']);
387 $cities[$c['id']] = $city;
388 }
389}
390
391function geoloc_getData_subcountries($mapid, $SFields, $minentities) {
2b105fb6 392 $countries = array();
393 $cities = array();
394
395 if ($mapid === false)
b536b5ae 396 $wheremapid = "WHERE gm.parent IS NULL";
2b105fb6 397 else
b536b5ae 398 $wheremapid = "WHERE gm.parent = {?}";
08cce2ff 399 $submapres = XDB::iterator(
2b105fb6 400 "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip,
401 gm.width, gm.height, gm.scale, 1 AS rat
402 FROM geoloc_maps AS gm
5e2307dc 403 ".$wheremapid, Env::v('mapid',''));
2b105fb6 404
edfc872d 405 global $globals;
406
2b105fb6 407 while ($c = $submapres->next())
408 {
409 $country = $c;
410 $country['name'] = utf8_decode($country['name']);
411 $country['color'] = 0xFFFFFF;
412 $country['swf'] = $globals->geoloc->webservice_url."maps/mercator/map_".$c['id'].".swf";
413 $countries[$c['id']] = $country;
414 }
415
416 if ($mapid === false) return array($countries, $cities);
417
b536b5ae 418 geoloc_getData_subcities(Env::i('mapid'), $SFields, $cities);
419 $nbcities = count($cities);
420 $nocity = $nbcities == 0;
2b105fb6 421
422 for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) if ($SFields[$i_mapfield]->fieldFormName == 'mapid') break;
b536b5ae 423 $SFields[$i_mapfield] = new MapSField('mapid', array('map.parent'), array('adresses','geoloc_city_in_maps','geoloc_maps'), array('am','gcim','map'), array(getadr_join('am'), 'am.cityid = gcim.city_id', 'map.map_id = gcim.map_id'));
424
425 $fields = new SFieldGroup(true, $SFields);
426 $where = $fields->get_where_statement();
427 if ($where) $where = " WHERE ".$where;
428
429 $countryres = XDB::iterator("
430 SELECT map.map_id AS id,
431 COUNT(u.user_id) AS nbPop,
432 SUM(u.promo % 2) AS yellow,
433 COUNT(DISTINCT gcim.city_id) AS nbCities,
434 SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop,
435 SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop
436 FROM auth_user_md5 AS u
437 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
438 ".$fields->get_select_statement()."
439 $where
440 GROUP BY map.map_id ORDER BY NULL", $hierarchy);
441
442 $maxpop = 0;
443 $nbentities = $nbcities + $countryres->total();
444 while ($c = $countryres->next())
445 {
446 $c['latPop'] /= $c['nbPop'];
447 $c['lonPop'] /= $c['nbPop'];
448 $c['rad'] = size_of_territory($c['nbPop']);
449 if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop'];
450 $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']);
451 $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']);
452 $countries[$c['id']] = array_merge($countries[$c['id']], $c);
453
454 $nbcities += $c['nbCities'];
455 }
456
457 if ($nocity && $nbcities < $minentities)
458 {
459 foreach($countries as $i => $c)
460 {
461 $countries[$i]['nbPop'] = 0;
462 if ($c['nbCities'] > 0)
463 geoloc_getData_subcities($c['id'], $SFields, $cities, false);
464 }
465 }
466
467 foreach ($countries as $i => $c) if ($c['nbPop'] > 0)
468 {
469 $lambda = pow($c['nbPop'] / $maxpop,0.3);
470 $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100;
471 }
472
2b105fb6 473 return array($countries, $cities);
474}
138b3c8e 475// }}}
476
a7de4ef7 477// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 478?>