/[drupal]/contributions/modules/google_earth/google_earth.module
ViewVC logotype

Contents of /contributions/modules/google_earth/google_earth.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Tue May 15 22:37:42 2007 UTC (2 years, 6 months ago) by bonvga
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +17 -39 lines
File MIME type: text/x-php
Apply patch #143872
Whois support integration (ripe.net)
Explode map image to optimize the kml size and fix the preview in google earth
1 <?php
2 // $Id: google_earth.module,v 1.4 2007/05/10 22:10:20 bonvga Exp $
3
4 /*
5 * Drupal Module: Google Earth
6 *
7 * @author: Arnaud Bonnevigne <www.bonvga.net/contact>
8 */
9
10 function google_earth_perm() {
11 return array('google earth download map', 'google earth settings', 'google earth generate map');
12 }
13
14 function google_earth_menu($may_cache) {
15 $items = array();
16 if ($may_cache) {
17 if (variable_get("google_earth_kml_path","")!="") {
18 $items[] = array(
19 'path' => 'google_earth',
20 'title' => t("Google Earth map"),
21 'callback' => 'google_earth_download',
22 'access' => user_access('google earth download map'),
23 'type' => MENU_NORMAL_ITEM);
24 }
25 $items[] = array(
26 'path' => 'admin/settings/google_earth',
27 'title' => t("Google Earth map"),
28 'description' => t('Configure the settings used to generate your Google Earth map.'),
29 'callback' => 'drupal_get_form',
30 'callback arguments' => array('google_earth_admin_settings'),
31 'access' => user_access('google earth settings'),
32 'type' => MENU_NORMAL_ITEM);
33 $items[] = array(
34 'path' => 'admin/settings/google_earth/generate',
35 'title' => t("Generate Google Earth map"),
36 'callback' => 'google_earth_page',
37 'access' => user_access('google earth generate map'),
38 'type' => MENU_NORMAL_ITEM);
39 return $items;
40 }
41 }
42
43 function google_earth_download() {
44 if (!user_access('google earth download map')) {
45 return;
46 }
47 $buffer = "";
48 $google_earth_kml_path = variable_get("google_earth_kml_path","");
49 $fp = fopen($google_earth_kml_path, "rb");
50 if ($fp) {
51 while (!feof($fp)) {
52 $buffer .= fread($fp, 8192);
53 }
54 fclose($fp);
55 $google_earth_visitor_placemark = variable_get("google_earth_visitor_placemark", TRUE);
56 if ($google_earth_visitor_placemark) {
57 $google_earth_geoapi_database_path = variable_get("google_earth_geoapi_database_path", '');
58 $google_earth_geoapi_api_path = variable_get("google_earth_geoapi_api_path", '');
59 if (file_exists($google_earth_geoapi_api_path)) {
60 if (file_exists($google_earth_geoapi_database_path)) {
61 include_once($google_earth_geoapi_api_path);
62 $geoip = geoip_open($google_earth_geoapi_database_path,GEOIP_STANDARD);
63 $ip = $_SERVER["REMOTE_ADDR"];
64 $geoip_data = geoip_record_by_addr($geoip,$ip);
65 if (is_numeric($geoip_data->longitude)&&is_numeric($geoip_data->latitude)) {
66 $buffer = str_replace("</Folder>
67 </kml>"," <Placemark>
68 <name><![CDATA[".t('You')."]]></name>
69 <description><![CDATA[<b>".$geoip_data->country_name." ".$geoip_data->city."</b>]]></description>
70 <visibility>1</visibility>
71 <Style>
72 <IconStyle>
73 <Icon>
74 <href>root://icons/palette-4.png</href>
75 <x>160</x>
76 <y>128</y>
77 <w>32</w>
78 <h>32</h>
79 </Icon>
80 </IconStyle>
81 </Style>
82 <Point>
83 <coordinates>".$geoip_data->longitude.",".$geoip_data->latitude.",0</coordinates>
84 </Point>
85 </Placemark>
86 </Folder>
87 </kml>",$buffer);
88 }
89 }
90 }
91 }
92 }
93
94 if ($buffer!="") {
95 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
96 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
97 header("Pragma: public");
98 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
99 header("Content-type: application/vnd.google-earth.kml+xml kml");
100 header("Content-disposition: attachment; filename=google-earth.kml");
101 echo $buffer;
102 flush();
103 }
104 }
105
106 function google_earth_page() {
107 $output = google_earth_generate();
108 drupal_set_message($output);
109 print theme("page","");
110 }
111
112 function google_earth_generate() {
113 $output = "";
114 $google_earth_limit = variable_get("google_earth_limit", '');
115 $google_earth_kml_path = variable_get("google_earth_kml_path", '');
116 $google_earth_title = variable_get("google_earth_title", '');
117 $google_earth_limit_time = variable_get("google_earth_limit_time", '');
118 $google_earth_icon_scale = variable_get("google_earth_icon_scale", '');
119 $google_earth_label_scale = variable_get("google_earth_label_scale", '');
120 $google_earth_hide_unresolved = variable_get("google_earth_hide_unresolved", '');
121
122 if (is_numeric($google_earth_limit)) {
123 if (is_numeric($google_earth_limit_time)&&($google_earth_limit_time!="0")) {
124 $result = db_query('SELECT hostname, count(*) AS count FROM {accesslog} WHERE timestamp >= (%d - %d) GROUP BY hostname ORDER BY count DESC LIMIT %d', time(), $google_earth_limit_time, $google_earth_limit);
125 } else {
126 $result = db_query('SELECT hostname, count(*) AS count FROM {accesslog} GROUP BY hostname ORDER BY count DESC LIMIT %d', $google_earth_limit);
127 }
128 } else {
129 if (is_numeric($google_earth_limit_time)&&($google_earth_limit_time!="0")) {
130 $result = db_query('SELECT hostname, count(*) AS count FROM {accesslog} WHERE timestamp >= (%d - %d) GROUP BY hostname ORDER BY count DESC', time(), $google_earth_limit_time);
131 } else {
132 $result = db_query('SELECT hostname, count(*) AS count FROM {accesslog} GROUP BY hostname ORDER BY count DESC');
133 }
134 }
135
136 $valid_ip = 0;
137 $data = array();
138 $data_geo_index = array();
139 $google_earth_geoapi_database_path = variable_get("google_earth_geoapi_database_path", '');
140 $google_earth_geoapi_api_path = variable_get("google_earth_geoapi_api_path", '');
141 if (file_exists($google_earth_geoapi_api_path)) {
142 if (file_exists($google_earth_geoapi_database_path)) {
143 include_once($google_earth_geoapi_api_path);
144 $geoip = geoip_open($google_earth_geoapi_database_path,GEOIP_STANDARD);
145
146 while($line = db_fetch_object($result)) {
147 $geoip_data = geoip_record_by_addr($geoip,$line->hostname);
148 if ($google_earth_hide_unresolved=="1"&&(!$geoip_data->latitude||!$geoip_data->longitude||$geoip_data->latitude==0||$geoip_data->longitude==0)) {
149 continue;
150 }
151
152 $valid_ip++;
153 if (!isset($data_geo_index[$geoip_data->latitude."+".$geoip_data->longitude])) {
154 $data_geo_index[$geoip_data->latitude."+".$geoip_data->longitude] = count($data);
155 $data[] = array(
156 "country_name" => $geoip_data->country_name,
157 "city" => $geoip_data->city,
158 "latitude" => $geoip_data->latitude,
159 "longitude" => $geoip_data->longitude,
160 "ip" => array(),
161 "count_by_host" => array(),
162 "count" => 0
163 );
164 }
165 $data[$data_geo_index[$geoip_data->latitude."+".$geoip_data->longitude]]["count"] += $line->count;
166 $data[$data_geo_index[$geoip_data->latitude."+".$geoip_data->longitude]]["ip"][] = $line->hostname;
167 $data[$data_geo_index[$geoip_data->latitude."+".$geoip_data->longitude]]["count_by_host"][] = $line->count;
168 }
169 } else {
170 $output .= t('Please, check the GeoIP database path ('.$google_earth_geoapi_database_path.')');
171 }
172 } else {
173 $output .= t('Please, check the GeoIP PHP API path ('.$google_earth_geoapi_api_path.')');
174 }
175
176 if ($output!="") {
177 return $output;
178 }
179
180 if (count($data)>0) {
181 $buffer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
182 <kml xmlns=\"http://earth.google.com/kml/2.0\">
183 <Folder>
184 <name><![CDATA[".$google_earth_title."]]></name>
185 <description><![CDATA[".$google_earth_title."\n".format_date(time(), 'medium')."\n\nGenerated by the google_earth Drupal module available at http://www.bonvga.net/google_earth/]]></description>
186 <open>0</open>
187 ";
188 for ($i=0;$i<count($data);$i++) {
189 $scale = ((2.8 / $data[0]["count"]) * $data[$i]["count"]) + 1.0;
190 $img_number = round((31.0 / count($data)) * $i)+1;
191
192 $desc_buffer = "";
193 for ($j=0;$j<count($data[$i]["ip"]);$j++) {
194 $desc_buffer .= "<a href=\"http://www.ripe.net/whois?searchtext=".$data[$i]["ip"][$j]."\">".$data[$i]["ip"][$j]."</a> (".format_plural($data[$i]["count_by_host"][$j], t("1 visit"), t("@count visits")).")<br />";
195 }
196 $buffer .= " <Placemark>
197 <name><![CDATA[".format_plural($data[$i]["count"], t("1 visit"), t("@count visits"))."]]></name>
198 <description><![CDATA[<b>".$data[$i]["country_name"]." ".$data[$i]["city"]."</b><br /><br />".$desc_buffer."]]></description>
199 <visibility>1</visibility>
200 <Style>
201 <IconStyle>
202 <scale>".($scale * ($google_earth_icon_scale / 10))."</scale>
203 <Icon>
204 <href>".url('', NULL, NULL, TRUE).drupal_get_path('module', 'google_earth')."/p".$img_number.".png</href>
205 </Icon>
206 </IconStyle>
207 <LabelStyle>
208 <scale>".round(($scale * ($google_earth_label_scale / 10)),2)."</scale>
209 </LabelStyle>
210 </Style>
211 <Point>
212 <coordinates>".$data[$i]["longitude"].",".$data[$i]["latitude"].",0</coordinates>
213 </Point>
214 </Placemark>
215 ";
216 }
217 $buffer .= "</Folder>
218 </kml>";
219
220 $fp = fopen($google_earth_kml_path,"w");
221 if ($fp) {
222 fwrite($fp,$buffer);
223 fclose($fp);
224 $output .= t('File save with '.count($data).' dots and '.$valid_ip.' valid ip');
225 } else {
226 $output .= t("Write error : check write permission on '".$google_earth_kml_path);
227 }
228 } else {
229 $output .= t('No data ! Please check accesslog table content');
230 }
231 return $output;
232 }
233
234 function google_earth_cron() {
235 if (variable_get("google_earth_cron", 0 ) == 1) {
236 $output = google_earth_generate();
237 watchdog('google_earth',$output,WATCHDOG_NOTICE,'');
238 }
239 }
240
241 function google_earth_admin_settings() {
242 if (!file_check_directory(file_create_path(dirname(variable_get('google_earth_kml_path', 'files/google_earth/google_earth.kml'))), FILE_CREATE_DIRECTORY)) {
243 drupal_set_message(t('The script directory does not exist, or is not writable.'), 'error');
244 }
245 if (!file_exists(variable_get("google_earth_geoapi_api_path", "geoip/geoipcity.inc"))) {
246 drupal_set_message(t('The \'GeoIP PHP API path\' is wrong. Change the \'GeoIP PHP API path\' value or check your GeoIP installation.'), 'error');
247 }
248 if (!file_exists(variable_get("google_earth_geoapi_database_path", "geoip/GeoIPCity.dat"))) {
249 drupal_set_message(t('The \'GeoIP database path\' is wrong. Change the \'GeoIP database path\' value or check your GeoIP installation.'), 'error');
250 }
251
252 $group0name = 'google_earth_settings_about';
253 $form0[$group0name] = array(
254 '#type' => 'fieldset',
255 '#title' => t('About this module'),
256 '#description' => t("This module generates a KML file representation of this site's visitors.<br />To display the map, it is necessary preliminary to install the Google Earth client available at this address : <a href=\"http://earth.google.com/\">Google Earth</a><br /><br />".l("Click on this link for generating the map","admin/settings/google_earth/generate")."<br />"),
257 '#collapsible' => FALSE,
258 '#collapsed' => FALSE,
259 );
260
261 $group1name = 'google_earth_settings_base';
262 $form1[$group1name] = array(
263 '#type' => 'fieldset',
264 '#title' => t('Main configuration'),
265 '#description' => '',
266 '#collapsible' => TRUE,
267 '#collapsed' => FALSE,
268 );
269 $get1 = google_earth_settings_base($group1name);
270
271 $group2name = 'google_earth_settings_geoip';
272 $form2[$group2name] = array(
273 '#type' => 'fieldset',
274 '#title' => t('GeoIP configuration'),
275 '#description' => t('GeoIP enable you to determine the Internet visitor\'s country based on the IP address.<br /><br />GeoIP country/city (full) is a paying product.<ul><li><a href="http://www.maxmind.com/app/country">GeoIP Country</a></li><li><a href="http://www.maxmind.com/app/city">GeoIP City</a></li></ul>You can also use the free/open source version.<ul><li><a href="http://www.maxmind.com/app/geolitecountry">GeoLite Country</a></li><li><a href="http://www.maxmind.com/app/geolitecity">GeoLite City</a></li></ul>'),
276 '#collapsible' => TRUE,
277 '#collapsed' => FALSE,
278 );
279 $get2 = google_earth_settings_geoip($group2name);
280
281 $form = array_merge_recursive($form0, $form1, $get1, $form2, $get2);
282 return system_settings_form($form);
283 }
284
285 function google_earth_settings_base($group) {
286 $form[$group]['google_earth_title'] = array(
287 '#type' => 'textfield',
288 '#title' => t('Map title'),
289 '#size' => 40,
290 '#maxlength' => 80,
291 '#default_value' => variable_get("google_earth_title", variable_get("site_name",'mysitename')." visits",''),
292 '#description' => t("Title of the KML file"),
293 '#required' => FALSE,
294 );
295 $form[$group]['google_earth_kml_path'] = array(
296 '#type' => 'textfield',
297 '#title' => t('Path to store the KML file'),
298 '#size' => 80,
299 '#maxlength' => 255,
300 '#default_value' => variable_get("google_earth_kml_path", "files/google_earth/google_earth.kml"),
301 '#description' => t("Set the path to store the KML file"),
302 '#required' => TRUE,
303 );
304 $form[$group]['google_earth_limit'] = array(
305 '#type' => 'textfield',
306 '#title' => t('Limit query result'),
307 '#size' => 10,
308 '#maxlength' => 10,
309 '#default_value' => variable_get("google_earth_limit", "100"),
310 '#description' => t("Set the limit for the query into accesslog table. To disable query limit, enter a void string"),
311 '#required' => FALSE,
312 );
313 $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
314 $period["0"] = t("No time limit");
315 $form[$group]['google_earth_limit_time'] = array(
316 '#type' => 'select',
317 '#title' => t('Time limit'),
318 '#default_value' => variable_get('google_earth_limit_time', variable_get('statistics_flush_accesslog_timer', 0)),
319 '#options' => $period,
320 '#description' => t('Allows to define a time limit on the accesslog table'),
321 '#multiple' => FALSE,
322 '#required' => TRUE,
323 );
324 $scale = array();
325 for ($i=1;$i<=40;$i++) {
326 $scale[$i] = sprintf("%0.1f",($i / 10.0));
327 }
328 $form[$group]['google_earth_icon_scale'] = array(
329 '#type' => 'select',
330 '#title' => t('Icon scale'),
331 '#default_value' => variable_get('google_earth_icon_scale', 10),
332 '#options' => $scale,
333 '#description' => t('Set the icon scale'),
334 '#multiple' => FALSE,
335 '#required' => TRUE,
336 );
337 $form[$group]['google_earth_label_scale'] = array(
338 '#type' => 'select',
339 '#title' => t('Label scale'),
340 '#default_value' => variable_get('google_earth_label_scale', 10),
341 '#options' => $scale,
342 '#description' => t('Set the label scale'),
343 '#multiple' => FALSE,
344 '#required' => TRUE,
345 );
346 $form[$group]['google_earth_hide_unresolved'] = array(
347 '#type' => 'checkbox',
348 '#title' => t('Hide unresolved geographic targets'),
349 '#default_value' => variable_get("google_earth_hide_unresolved", TRUE),
350 '#description' => t("Some addresses cannot be solved, mainly anonymous proxies and local networks addresses. This option makes it possible to hide them"),
351 '#return_value' => 1,
352 '#required' => FALSE,
353 );
354 $form[$group]['google_earth_cron'] = array(
355 '#type' => 'checkbox',
356 '#title' => t('Auto generate file in cron run'),
357 '#default_value' => variable_get("google_earth_cron", FALSE),
358 '#description' => t("Check this box to have Drupal's cron run produce the Google Earth map"),
359 '#return_value' => 1,
360 '#required' => FALSE,
361 );
362 $form[$group]['google_earth_visitor_placemark'] = array(
363 '#type' => 'checkbox',
364 '#title' => t('Add the current visitor placemark'),
365 '#default_value' => variable_get("google_earth_visitor_placemark", TRUE),
366 '#description' => t("Add the current visitor placemark on the downloaded FML file"),
367 '#return_value' => 1,
368 '#required' => FALSE,
369 );
370 return $form;
371 }
372
373 function google_earth_settings_geoip($group) {
374 $form[$group]['google_earth_geoapi_database_path'] = array(
375 '#type' => 'textfield',
376 '#title' => t('GeoIP database path'),
377 '#size' => 80,
378 '#maxlength' => 255,
379 '#default_value' => variable_get("google_earth_geoapi_database_path", "geoip/GeoIPCity.dat"),
380 '#description' => t("Set the full path to the 'GeoIPCity.dat' or 'GeoLiteCity.dat' file"),
381 '#required' => TRUE,
382 );
383 $form[$group]['google_earth_geoapi_api_path'] = array(
384 '#type' => 'textfield',
385 '#title' => t('GeoIP PHP API path'),
386 '#size' => 80,
387 '#maxlength' => 255,
388 '#default_value' => variable_get("google_earth_geoapi_api_path", "geoip/geoipcity.inc"),
389 '#description' => t("Set the full path to the 'geoipcity.inc' file"),
390 '#required' => TRUE,
391 );
392 return $form;
393 }
394 ?>

  ViewVC Help
Powered by ViewVC 1.1.2