Updates ChangeLog, core, and adds the new upgrade directory.
[platal.git] / include / geoloc.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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
0337d704 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 */
b536b5ae 49function geoloc_region($country, $current, $avail_only = false)
50{
51 if ($avail_only) {
52 $res = XDB::iterRow('SELECT r.region, r.name
53 FROM geoloc_region AS r
54 INNER JOIN adresses AS a ON (a.country = r.a2 AND a.region = r.region)
55 WHERE r.a2 = {?}
56 GROUP BY r.region
57 ORDER BY r.name', $country);
58 } else {
59 $res = XDB::iterRow('SELECT region,name
60 FROM geoloc_region
61 WHERE a2 = {?}
62 ORDER BY name', $country);
63 }
0337d704 64 $html = "<option value=\"\"></option>";
65 while (list($regid, $regname) = $res->next()) {
b536b5ae 66 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
67 $regid, ($current==$regid?"selected='selected'":""), $regname);
0337d704 68 }
69 return $html;
0337d704 70}
0337d704 71// }}}
72
73// {{{ get_address_infos($txt)
74/** retrieve the infos on a text address
75 * store on the fly the info of the city concerned
76 * @param $txt the raw text of an address
77 */
787bb3d7 78function get_address_infos($txt)
8c4a0c30 79{
56670b6a 80 global $globals;
3d1a2c7d 81
8c4a0c30 82 $url = $globals->geoloc->webservice_url."address.php?precise=1&txt=" . urlencode($txt);
3d1a2c7d
FB
83 if ($globals->debug & DEBUG_BT) {
84 if (!isset(PlBacktrace::$bt['Geoloc'])) {
85 new PlBacktrace('Geoloc');
86 }
87 PlBacktrace::$bt['Geoloc']->start($url);
88 }
89 $f = @fopen($url, 'r');
90 if ($f === false) {
91 if ($globals->debug & DEBUG_BT) {
92 PlBacktrace::$bt['Geoloc']->stop(0, 'Can\'t fetch result');
93 }
94 return false;
95 }
0337d704 96 $keys = explode('|',fgets($f));
97 $vals = explode('|',fgets($f));
3d1a2c7d
FB
98 if ($globals->debug & DEBUG_BT) {
99 $data = array();
100 for ($i = 0 ; $i < count($keys) ; ++$i) {
101 $data[] = array($keys[$i], $vals[$i]);
102 }
103 PlBacktrace::$bt['Geoloc']->stop(count($keys), null, $data);
104 }
8eab04f4 105 $infos = empty_address();
2b98ac11 106 foreach ($keys as $i=>$key) {
107 if($vals[$i]) {
108 if ($key == 'sql') {
109 $infos[$key] = $vals[$i];
110 } else {
111 $val = strtr($vals[$i], array(chr(197).chr(147) => "&oelig;"));
8c4a0c30 112 $infos[$key] = $val;
2b98ac11 113 }
114 }
115 }
8eab04f4
FB
116 if (empty($infos['country'])) {
117 $infos['country'] = '00';
118 }
a25cdc91
FB
119 if (isset($infos['sql']) && $infos['sql']) {
120 $sql = explode(', ', trim($infos['sql'], '()'));
121 if (count($sql) == 16) {
122 for ($i = 0 ; $i < 16 ; ++$i) {
123 $sql[$i] = stripslashes(trim($sql[$i], ' \''));
124 }
125 XDB::execute("REPLACE INTO geoloc_city
126 VALUES ({?}, {?}, {?}, {?}, {?}, {?},
127 {?}, {?}, {?}, {?}, {?}, {?},
128 {?}, {?}, {?}, {?})",
129 $sql[0], $sql[1], $sql[2], $sql[3], $sql[4], $sql[5],
130 $sql[6], $sql[7], $sql[8], $sql[9], $sql[10], $sql[11],
131 $sql[12], $sql[13], $sql[14], $sql[15]);
132 }
133 }
5ee95138 134 if (isset($infos['display']) && $infos['display'])
de0485fd
FB
135 XDB::execute("UPDATE geoloc_pays
136 SET display = {?}
137 WHERE a2 = {?}", $infos['display'], $infos['country']);
138 if (isset($infos['cityid'])) {
b536b5ae 139 fix_cities_not_on_map(1, $infos['cityid']);
de0485fd
FB
140 if (floatval($infos['precise_lat']) && floatval($infos['precise_lon'])) {
141 $res = XDB::query("SELECT c.lat / 100000, c.lon / 100000
142 FROM geoloc_city AS c
143 WHERE c.id = {?}", $infos['cityid']);
144 if ($res->numRows()) {
145 list($glat, $glng) = $res->fetchOneRow();
146 $infos['precise_lat'] = $glat;
147 $infos['precise_lon'] = $glng;
148 }
149 }
150 }
0337d704 151 return $infos;
152}
153// }}}
154
56670b6a 155// {{{ get_cities_maps($array)
156/* get all the maps id of the cities contained in an array */
157function get_cities_maps($array)
158{
159 global $globals;
160 implode("\n",$array);
8c4a0c30 161 $url = $globals->geoloc->webservice_url."findMaps.php?datatext=".urlencode(implode("\n", $array));
56670b6a 162 if (!($f = @fopen($url, 'r'))) return false;
163 $maps = array();
164 while (!feof($f))
165 {
166 $l = trim(fgets($f));
167 $tab = explode(';', $l);
168 $i = $tab[0];
169 unset($tab[0]);
170 $maps[$i] = $tab;
171 }
172 return $maps;
173}
174// }}}
175
176// {{{ get_new_maps($url)
177/** set new maps from url **/
178function get_new_maps($url)
179{
a3a049fc 180 if (!($f = @fopen($url, 'r'))) {
181 return false;
182 }
183 XDB::query('TRUNCATE TABLE geoloc_maps');
184 $s = '';
185 while (!feof($f)) {
186 $l = fgetcsv($f, 1024, ';', '"');
187 foreach ($l as $i => $val) {
188 if ($val != 'NULL') {
189 $l[$i] = '\''.addslashes($val).'\'';
190 }
191 }
192 $s .= ',('.implode(',',$l).')';
193 }
194 XDB::execute('INSERT INTO geoloc_maps VALUES '.substr($s, 1));
195 return true;
56670b6a 196}
80244bbe 197// }}}
56670b6a 198
0337d704 199// {{{ get_address_text($adr)
200/** make the text of an address that can be read by a mailman
201 * @param $adr an array with all the usual fields
202 */
787bb3d7 203function get_address_text($adr)
8c4a0c30 204{
0337d704 205 $t = "";
5ee95138 206 if (isset($adr['adr1']) && $adr['adr1']) $t.= $adr['adr1'];
207 if (isset($adr['adr2']) && $adr['adr2']) $t.= "\n".$adr['adr2'];
208 if (isset($adr['adr3']) && $adr['adr3']) $t.= "\n".$adr['adr3'];
0337d704 209 $l = "";
5ee95138 210 if (isset($adr['display']) && $adr['display']) {
80ca1b4e 211 $keys = explode(' ', $adr['display']);
212 foreach ($keys as $key) {
8c4a0c30 213 if (isset($adr[$key])) {
80ca1b4e 214 $l .= " ".$adr[$key];
8c4a0c30 215 } else {
80ca1b4e 216 $l .= " ".$key;
8c4a0c30 217 }
80ca1b4e 218 }
8c4a0c30 219 if ($l) substr($l, 1);
220 } elseif ($adr['country'] == 'US' || $adr['country'] == 'CA' || $adr['country'] == 'GB') {
221 if ($adr['city']) $l .= $adr['city'].",\n";
222 if ($adr['region']) $l .= $adr['region']." ";
223 if ($adr['postcode']) $l .= $adr['postcode'];
224 } else {
225 if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode']." ";
226 if (isset($adr['city']) && $adr['city']) $l .= $adr['city'];
0337d704 227 }
228 if ($l) $t .= "\n".trim($l);
a953f7e7 229 if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == mb_strtoupper($adr['countrytxt']))) {
08cce2ff 230 $res = XDB::query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']);
0337d704 231 $adr['countrytxt'] = $res->fetchOneCell();
232 }
8c4a0c30 233 if (isset($adr['countrytxt']) && $adr['countrytxt']) {
234 $t .= "\n".$adr['countrytxt'];
235 }
0337d704 236 return trim($t);
237}
238// }}}
239
240// {{{ compare_addresses_text($a, $b)
241/** compares if two address matches
242 * @param $a the raw text of an address
243 * @param $b the raw text of a complete valid address
244 */
8c4a0c30 245function compare_addresses_text($a, $b)
246{
a953f7e7
SJ
247 $ta = mb_strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a));
248 $tb = mb_strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b));
787bb3d7 249
0337d704 250 $la = explode("\n", $ta);
251 $lb = explode("\n", $tb);
252
8c4a0c30 253 if (count($lb) > count($la) + 1) {
254 return false;
255 }
256 foreach ($la as $i=>$l) {
37d44b3b 257 if (levenshtein(trim($l), trim($lb[$i])) > 3) {
8c4a0c30 258 return false;
259 }
260 }
0337d704 261 return true;
262}
263
264// }}}
265
266function empty_address() {
267 return Array(
268 "adr1" => "",
269 "adr2" => "",
270 "adr3" => "",
271 "cityid" => NULL,
272 "city" => "",
273 "postcode" => "",
274 "region" => "",
80ca1b4e 275 "regiontxt" => "",
0337d704 276 "country" => "00",
de0485fd
FB
277 "countrytxt" => "",
278 "precise_lat" => "",
279 "precise_lon" => "");
0337d704 280}
281
282// create a simple address from a text without geoloc
8c4a0c30 283function cut_address($txt)
284{
0337d704 285 $txt = str_replace("\r\n", "\n", $txt);
286 ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a);
287 return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5])));
288}
289
290// {{{ localize_addresses($uid)
291/* localize all the address of a user and modify the database
292 * if the new address match with the old one
293 * @param $uid the id of the user
294 */
8c4a0c30 295function localize_addresses($uid)
296{
297 $res = XDB::iterator("SELECT *
298 FROM adresses
299 WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid);
0337d704 300 $erreur = Array();
301
302 while ($a = $res->next()) {
303 $new = get_address_infos($ta = get_address_text($a));
304 if (compare_addresses_text($ta, get_address_text($new))) {
8c4a0c30 305 XDB::execute("UPDATE adresses
306 SET adr1 = {?}, adr2 = {?}, adr3 = {?},
307 cityid = {?}, city = {?}, postcode = {?},
308 region = {?}, regiontxt = {?}, country = {?},
309 glat = {?}, glng = {?}
310 WHERE uid = {?} AND adrid = {?}",
311 $new['adr1'], $new['adr2'], $new['adr3'],
312 $new['cityid'], $new['city'], $new['postcode'],
313 $new['region'], $new['regiontxt'], $new['country'],
314 $new['precise_lat'], $new['precise_lon'],
315 $uid, $a['adrid']);
316 $new['store'] = true;
317 if (!$new['cityid']) {
318 $erreur[$a['adrid']] = $new;
319 }
0337d704 320 } else {
321 $new['store'] = false;
322 $erreur[$a['adrid']] = $new;
323 }
324 }
325 return $erreur;
326}
327// }}}
328
329// {{{ synchro_city($id)
330/** synchronise the local geoloc_city base to geoloc.org
331 * @param $id the id of the city to synchronize
332 */
787bb3d7 333function synchro_city($id)
8c4a0c30 334{
56670b6a 335 global $globals;
336 $url = $globals->geoloc->webservice_url."cityFinder.php?method=id&id=".$id."&out=sql";
8c4a0c30 337 if (!($f = @fopen($url, 'r'))) {
338 return false;
339 }
0337d704 340 $s = fgets($f);
8c4a0c30 341 if ($s) {
08cce2ff 342 return XDB::execute("REPLACE INTO geoloc_city VALUES ".$s) > 0;
8c4a0c30 343 }
344}
0337d704 345 // }}}
346
56670b6a 347// {{{ function fix_cities_not_on_map($limit)
7f3ac007 348function fix_cities_not_on_map($limit=false, $cityid=false)
56670b6a 349{
8c4a0c30 350 $missing = XDB::query("SELECT c.id
351 FROM geoloc_city AS c
787bb3d7 352 LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id)
8c4a0c30 353 WHERE m.city_id IS NULL"
354 . ($cityid ? " AND c.id = '" . $cityid . "'" : "" )
355 . ($limit ? " LIMIT $limit" : "" ));
56670b6a 356 $maps = get_cities_maps($missing->fetchColumn());
8c4a0c30 357 if ($maps) {
56670b6a 358 $values = "";
8c4a0c30 359 foreach ($maps as $cityid => $maps_c) {
360 foreach ($maps_c as $map_id) {
56670b6a 361 $values .= ",($cityid, $map_id, '')";
8c4a0c30 362 }
363 }
22cc3370
FB
364 if (strlen($values) > 1) {
365 XDB::execute("REPLACE INTO geoloc_city_in_maps
366 VALUES ".substr($values, 1));
367 }
8c4a0c30 368 } else {
56670b6a 369 return false;
8c4a0c30 370 }
014c8464 371 return true;
372}
56670b6a 373
8c4a0c30 374function set_smallest_levels()
375{
376 $maxlengths = XDB::iterRow("SELECT MAX(LENGTH(gm.path)), gcim.city_id
377 FROM geoloc_city_in_maps AS gcim
378 INNER JOIN geoloc_maps AS gm USING ( map_id )
379 GROUP BY gcim.city_id");
014c8464 380 while (list($length, $id) = $maxlengths->next()) {
8c4a0c30 381 XDB::execute("UPDATE geoloc_city_in_maps AS gcim
382 INNER JOIN geoloc_maps AS gm USING(map_id)
383 SET gcim.infos = IF(LENGTH(gm.path) = {?}, 'smallest', '')
384 WHERE gcim.city_id = {?}", $length, $id);
014c8464 385 }
56670b6a 386 return true;
387}
388// }}}
389
390
8c4a0c30 391function geoloc_to_x($lon, $lat)
392{
393 return deg2rad(1) * $lon *100;
394}
395
396function geoloc_to_y($lon, $lat)
397{
398 if ($lat < -75) {
399 return latToY(-75);
400 }
401 if ($lat > 75) {
402 return latToY(75);
403 }
787bb3d7 404 return -100 * log(tan(pi()/4 + deg2rad(1)/2*$lat));
8c4a0c30 405}
56670b6a 406
8c4a0c30 407function size_of_city($nb)
408{
409 $s = round(log($nb + 1)*2,2);
410 if ($s < 1) {
411 return 1;
412 }
413 return $s;
56670b6a 414}
415
8c4a0c30 416function size_of_territory($nb)
417{
418 return size_of_city($nb);
419}
56670b6a 420
8c4a0c30 421function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true)
422{
423 if ($SFields instanceof UserSet) {
424 $set = $SFields;
425 $SFields = array();
426 } else {
427 $set = new UserSet();
428 }
429 for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) {
430 if ($SFields[$i_mapfield]->fieldFormName == 'mapid') {
431 break;
432 }
433 }
434 $SFields[$i_mapfield] = new MapSField('mapid',
435 array('gcim.map_id'),
436 array('adresses','geoloc_city_in_maps'),
437 array('am','gcim'),
438 array(getadr_join('am'), 'am.cityid = gcim.city_id'),
439 $mapid);
2b105fb6 440 $fields = new SFieldGroup(true, $SFields);
441 $where = $fields->get_where_statement();
86b5c8f0 442 $joins = $fields->get_select_statement();
8c4a0c30 443 if ($where) {
444 $where .= ' AND ';
445 }
446 $cityres = $set->get('gc.id,
447 gc.lon / 100000 AS x, gc.lat/100000 AS y,
448 gc.name,
449 COUNT(u.user_id) AS pop,
450 SUM(u.promo % 2) AS yellow',
86b5c8f0 451 "$joins
8c4a0c30 452 LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id)",
453 $where . ($direct ? "gcim.infos = 'smallest'" : '1'),
454 'gc.id, gc.alias',
455 'pop DESC');
06df222b 456 foreach($cityres as $c) {
8c4a0c30 457 if ($c['pop'] > 0) {
2b105fb6 458 $city = $c;
459 $city['x'] = geoloc_to_x($c['x'], $c['y']);
460 $city['y'] = geoloc_to_y($c['x'], $c['y']);
461 $city['size'] = size_of_city($c['pop']);
462 $cities[$c['id']] = $city;
463 }
8c4a0c30 464 }
2b105fb6 465}
466
787bb3d7 467function geoloc_getData_subcountries($mapid, $sin, $minentities)
8c4a0c30 468{
2b105fb6 469 $countries = array();
470 $cities = array();
787bb3d7 471
8c4a0c30 472 if ($mapid === false) {
b536b5ae 473 $wheremapid = "WHERE gm.parent IS NULL";
8c4a0c30 474 } else {
b536b5ae 475 $wheremapid = "WHERE gm.parent = {?}";
8c4a0c30 476 }
08cce2ff 477 $submapres = XDB::iterator(
787bb3d7 478 "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip,
8c4a0c30 479 gm.width, gm.height, gm.scale, 1 AS rat
480 FROM geoloc_maps AS gm
481 ". $wheremapid, Env::v('mapid',''));
2b105fb6 482
edfc872d 483 global $globals;
484
8c4a0c30 485 while ($c = $submapres->next()) {
2b105fb6 486 $country = $c;
2b105fb6 487 $country['color'] = 0xFFFFFF;
488 $country['swf'] = $globals->geoloc->webservice_url."maps/mercator/map_".$c['id'].".swf";
489 $countries[$c['id']] = $country;
490 }
787bb3d7 491
8c4a0c30 492 if ($mapid === false) {
493 return array($countries, $cities);
494 }
2b105fb6 495
8c4a0c30 496 geoloc_getData_subcities(Env::i('mapid'), $sin, $cities);
b536b5ae 497 $nbcities = count($cities);
498 $nocity = $nbcities == 0;
8c4a0c30 499 if ($sin instanceof UserSet) {
500 $set = $sin;
501 $SFields = array();
502 } else {
503 $set = new UserSet();
504 $SFields = $sin;
505 }
2b105fb6 506
8c4a0c30 507 for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) {
508 if ($SFields[$i_mapfield]->fieldFormName == 'mapid') {
509 break;
510 }
511 }
512 $SFields[$i_mapfield] = new MapSField('mapid',
513 array('map.parent'),
514 array('adresses','geoloc_city_in_maps','geoloc_maps'),
515 array('am','gcim','map'),
787bb3d7
FB
516 array(getadr_join('am'),
517 'am.cityid = gcim.city_id',
8c4a0c30 518 'map.map_id = gcim.map_id'));
b536b5ae 519 $fields = new SFieldGroup(true, $SFields);
86b5c8f0 520 $where = $fields->get_where_statement();
521 $joins = $fields->get_select_statement();
8c4a0c30 522 $countryres = $set->get('map.map_id AS id,
523 COUNT(u.user_id) AS nbPop,
524 SUM(u.promo % 2) AS yellow,
525 COUNT(DISTINCT gcim.city_id) AS nbCities,
526 SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop,
527 SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop',
86b5c8f0 528 $joins,
529 $where,
8c4a0c30 530 'map.map_id',
531 'NULL');
787bb3d7 532
b536b5ae 533 $maxpop = 0;
06df222b
FB
534 $nbentities = $nbcities + count($countryres);
535 foreach ($countryres as $c) {
b536b5ae 536 $c['latPop'] /= $c['nbPop'];
537 $c['lonPop'] /= $c['nbPop'];
538 $c['rad'] = size_of_territory($c['nbPop']);
539 if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop'];
540 $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']);
541 $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']);
86b5c8f0 542 @$countries[$c['id']] = array_merge($countries[$c['id']], $c);
787bb3d7 543
b536b5ae 544 $nbcities += $c['nbCities'];
787bb3d7
FB
545 }
546
8c4a0c30 547 if ($nocity && $nbcities < $minentities){
548 foreach($countries as $i => $c) {
b536b5ae 549 $countries[$i]['nbPop'] = 0;
86b5c8f0 550 if (@$c['nbCities'] > 0) {
8c4a0c30 551 geoloc_getData_subcities($c['id'], $sin, $cities, false);
787bb3d7 552 }
8c4a0c30 553 }
b536b5ae 554 }
787bb3d7 555
8c4a0c30 556 foreach ($countries as $i => $c) {
c1895524 557 if (@$c['nbPop'] > 0) {
8c4a0c30 558 $lambda = pow($c['nbPop'] / $maxpop,0.3);
559 $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100;
560 }
b536b5ae 561 }
787bb3d7
FB
562
563 return array($countries, $cities);
564}
138b3c8e 565// }}}
566
a7de4ef7 567// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 568?>