#570: Fix geoloc skin on xnet && fix access right
[platal.git] / modules / geoloc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 class GeolocModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'geoloc' => $this->make_hook('default', AUTH_COOKIE),
28 'geoloc/icon.swf' => $this->make_hook('icon', AUTH_COOKIE),
29 'geoloc/dynamap.swf' => $this->make_hook('dynamap', AUTH_COOKIE),
30 'geoloc/init' => $this->make_hook('init', AUTH_COOKIE),
31 'geoloc/city' => $this->make_hook('city', AUTH_COOKIE),
32 'geoloc/country' => $this->make_hook('country', AUTH_COOKIE),
33 '%grp/geoloc' => $this->make_hook('default',AUTH_COOKIE),
34 '%grp/geoloc/icon.swf' => $this->make_hook('icon', AUTH_COOKIE),
35 '%grp/geoloc/dynamap.swf'=> $this->make_hook('dynamap',AUTH_COOKIE),
36 '%grp/geoloc/init' => $this->make_hook('init', AUTH_COOKIE),
37 '%grp/geoloc/city' => $this->make_hook('city', AUTH_COOKIE),
38 '%grp/geoloc/country' => $this->make_hook('country',AUTH_COOKIE),
39 'admin/geoloc' => $this->make_hook('admin', AUTH_MDP, 'admin'),
40 'admin/geoloc/dynamap' => $this->make_hook('admin_dynamap', AUTH_MDP, 'admin'),
41 );
42 }
43
44 function _make_qs()
45 {
46 $querystring = "";
47
48 foreach ($_GET as $v => $a) {
49 if ($v != 'initfile' && $v != 'n' && $v != 'mapid') {
50 $querystring .= urlencode($v).'='.urlencode($a).'&amp;';
51 }
52 }
53
54 return $querystring;
55 }
56
57 function use_map()
58 {
59 return is_file(dirname(__FILE__).'/geoloc/dynamap.swf') &&
60 is_file(dirname(__FILE__).'/geoloc/icon.swf');
61 }
62
63 function handler_default(&$page)
64 {
65 global $globals;
66
67 if (!$this->use_map())
68 $page->assign('request_geodesix', 1);
69
70 if (!empty($GLOBALS['IS_XNET_SITE'])) {
71 $page->assign('no_annu', 1);
72 if ($globals->asso('cat') == 'Promotions') {
73 new_group_open_page('geoloc/index.tpl');
74 } elseif ($globals->asso('pub') == 'public') {
75 new_group_page('geoloc/index.tpl');
76 } else {
77 new_groupadmin_page('geoloc/index.tpl');
78 }
79 } else {
80 $page->changeTpl('geoloc/index.tpl');
81 }
82
83 require_once dirname(__FILE__).'/search/search.inc.php';
84
85 $fields = new SFieldGroup(true, advancedSearchFromInput());
86 $search = str_replace('&amp;','&',$fields->get_url());
87 if ((!Env::has('only_current') && !Env::has('rechercher')) || Env::v('only_current') == 'on')
88 $search .= '&only_current=on';
89 elseif (Env::i('only_current') != 'on')
90 $search .= '&only_current=';
91
92 $search = preg_replace('/(^|&)mapid=([0-9]+)(&)/','\1\3', $search);
93 if ($search)
94 $search = '?'.$search;
95 $page->assign('search',urlencode($search));
96
97 $page->assign('protocole', substr($globals->baseurl,0,strpos($globals->baseurl,':')));
98
99 if (!$search) {
100 $res = XDB::query('SELECT COUNT(DISTINCT uid)
101 FROM adresses WHERE cityid IS NOT NULL');
102 $page->assign('localises', $res->fetchOneCell());
103 }
104 }
105
106 function handler_icon(&$page)
107 {
108 global $globals;
109
110 header("Content-type: application/x-shockwave-flash");
111 header("Pragma:");
112
113 readfile(dirname(__FILE__).'/geoloc/icon.swf');
114 exit;
115
116 return PL_NOT_FOUND;
117 }
118
119 function handler_dynamap(&$page)
120 {
121 global $globals;
122
123 header("Content-type: application/x-shockwave-flash");
124
125 header("Pragma:");
126 readfile(dirname(__FILE__).'/geoloc/dynamap.swf');
127 exit;
128
129 return PL_NOT_FOUND;
130 }
131
132 function handler_init(&$page)
133 {
134 global $globals;
135
136 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
137
138 header('Content-type: text/xml');
139 header('Pragma:');
140 if(!empty($GLOBALS['IS_XNET_SITE']))
141 $page->assign('background', 0xF2E9D0);
142 $page->assign('querystring', $this->_make_qs());
143 }
144
145 function handler_city(&$page)
146 {
147 global $globals;
148
149 header("Content-type: text/xml");
150 header("Pragma:");
151
152 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
153
154 require_once dirname(__FILE__).'/search/search.inc.php';
155 require_once('geoloc.inc.php');
156
157 if (empty($GLOBALS['IS_XNET_SITE'])) {
158 $usual_fields = advancedSearchFromInput();
159 $fields = new SFieldGroup(true, $usual_fields);
160 } else {
161 $_REQUEST['asso_id'] = $globals->asso('id');
162 $_REQUEST['only_current'] = 'on';
163 $fields = new SFieldGroup(true, array(
164 new RefSField('asso_id',array('gxm.asso_id'),'groupex.membres','gxm','u.user_id=gxm.uid'),
165 new RefSField('cityid',array('av.cityid'),'adresses','av',getadr_join('av'))));
166 }
167 $where = $fields->get_where_statement();
168 if ($where) $where = "WHERE ".$where;
169
170 $users = XDB::iterator("
171 SELECT u.user_id AS id, u.prenom, u.nom, u.promo, alias
172 FROM adresses AS a
173 INNER JOIN auth_user_md5 AS u ON(u.user_id = a.uid)
174 INNER JOIN auth_user_quick AS q ON(q.user_id = a.uid)
175 LEFT JOIN aliases ON(u.user_id = aliases.id AND FIND_IN_SET(aliases.flags,'bestalias'))
176 ".$fields->get_select_statement()."
177 ".$where."
178 GROUP BY u.user_id LIMIT 11", $id);
179
180 $page->assign('users', $users);
181 }
182
183 function handler_country(&$page)
184 {
185 global $globals;
186
187 // to debug sql use the next line
188 if (Env::has('debug')) {
189 $page->changeTpl('geoloc/country.tpl', SIMPLE);
190 } else {
191 header("Content-type: text/xml");
192 header("Pragma:");
193 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
194 }
195
196 require_once dirname(__FILE__).'/search/search.inc.php';
197 require_once 'geoloc.inc.php';
198
199 $querystring = $this->_make_qs();
200 $page->assign('searchvars', $querystring);
201
202 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
203 if (empty($GLOBALS['IS_XNET_SITE'])) {
204 $fields = advancedSearchFromInput();
205 } else {
206 $_REQUEST['asso_id'] = $globals->asso('id');
207 $_REQUEST['only_current'] = 'on';
208 $fields = array(new RefSField('asso_id',array('gxm.asso_id'),'groupex.membres','gxm','u.user_id=gxm.uid'));
209 }
210
211 list($countries, $cities) = geoloc_getData_subcountries($mapid, $fields, 10);
212
213 $page->assign('countries', $countries);
214 $page->assign('cities', $cities);
215 }
216
217 function handler_admin(&$page, $action = false) {
218 $page->changeTpl('geoloc/admin.tpl');
219 require_once("geoloc.inc.php");
220 $page->assign('xorg_title','Polytechnique.org - Administration - Geolocalisation');
221
222 $nb_synchro = 0;
223
224 if (Env::has('id') && is_numeric(Env::v('id'))) {
225 if (synchro_city(Env::v('id'))) $nb_synchro ++;
226 }
227
228 if ($action == 'missinglat') {
229 $res = XDB::iterRow("SELECT id FROM geoloc_city WHERE lat = 0 AND lon = 0");
230 while ($a = $res->next()) if (synchro_city($a[0])) $nb_synchro++;
231 }
232
233 if ($nb_synchro)
234 $page->trig(($nb_synchro > 1)?($nb_synchro." villes ont été synchronisées"):"Une ville a été synchronisée");
235
236 $res = XDB::query("SELECT COUNT(*) FROM geoloc_city WHERE lat = 0 AND lon = 0");
237 $page->assign("nb_missinglat", $res->fetchOneCell());
238 }
239
240 function handler_admin_dynamap(&$page, $action = false) {
241 $page->changeTpl('geoloc/admin_dynamap.tpl');
242
243 if ($action == 'cities_not_on_map') {
244 require_once('geoloc.inc.php');
245 if (!fix_cities_not_on_map(20))
246 $page->trig("Impossible d'accéder au webservice");
247 else
248 $refresh = true;
249 }
250
251 if ($action == 'smallest_maps') {
252 require_once('geoloc.inc.php');
253 set_smallest_levels();
254 }
255
256 if ($action == 'precise_coordinates') {
257 XDB::execute("UPDATE adresses AS a INNER JOIN geoloc_city AS c ON(a.cityid = c.id) SET a.glat = c.lat / 100000, a.glng = c.lon / 100000");
258 }
259
260 if ($action == 'newmaps') {
261 require_once('geoloc.inc.php');
262 if (!get_new_maps(Env::v('url')))
263 $page->trig("Impossible d'accéder aux nouvelles cartes");
264 }
265
266 $countMissing = XDB::query("SELECT COUNT(*) 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");
267 $missing = $countMissing->fetchOneCell();
268
269 $countNoSmallest = XDB::query("SELECT SUM(IF(infos = 'smallest',1,0)) AS n FROM geoloc_city_in_maps GROUP BY city_id ORDER BY n");
270 $noSmallest = $countNoSmallest->fetchOneCell() == 0;
271
272 $countNoCoordinates = XDB::query("SELECT COUNT(*) FROM adresses WHERE cityid IS NOT NULL AND glat = 0 AND glng = 0");
273 $noCoordinates = $countNoCoordinates->fetchOneCell();
274
275 if (isset($refresh) && $missing) {
276 $page->assign("xorg_extra_header", "<meta http-equiv='Refresh' content='3'/>");
277 }
278 $page->assign("nb_cities_not_on_map", $missing);
279 $page->assign("no_smallest", $noSmallest);
280 $page->assign("no_coordinates", $noCoordinates);
281 }
282
283 }
284
285 ?>