wp-gpx-maps/wp-gpx-maps.php

303 lines
8.7 KiB
PHP
Raw Normal View History

2011-12-14 10:35:25 +00:00
<?php
/*
Plugin Name: WP-GPX-Maps
Plugin URI: http://www.darwinner.it/
2011-12-19 13:27:22 +00:00
Description: Draws a gpx track with altitude graph
2012-02-15 16:04:18 +00:00
Version: 1.1.6
2011-12-14 10:35:25 +00:00
Author: Bastianon Massimo
Author URI: http://www.pedemontanadelgrappa.it/
License: GPL
*/
2012-01-08 16:47:40 +00:00
//error_reporting (E_ALL);
2011-12-14 10:35:25 +00:00
include 'wp-gpx-maps_Utils.php';
2011-12-19 13:27:22 +00:00
include 'wp-gpx-maps_admin.php';
2011-12-14 10:35:25 +00:00
add_action( 'wp_print_scripts', 'enqueue_WP_GPX_Maps_scripts' );
2011-12-19 13:27:22 +00:00
add_shortcode('sgpx','handle_WP_GPX_Maps_Shortcodes');
register_activation_hook(__FILE__,'WP_GPX_Maps_install');
register_deactivation_hook( __FILE__, 'WP_GPX_Maps_remove');
add_filter('plugin_action_links', 'WP_GPX_Maps_action_links', 10, 2);
function WP_GPX_Maps_action_links($links, $file) {
static $this_plugin;
if (!$this_plugin) {
$this_plugin = plugin_basename(__FILE__);
}
// check to make sure we are on the correct plugin
if ($file == $this_plugin) {
// the anchor tag and href to the URL we want. For a "Settings" link, this needs to be the url of your settings page
$settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=WP-GPX-Maps">Settings</a>';
// add the link to the list
array_unshift($links, $settings_link);
}
return $links;
}
2011-12-14 10:35:25 +00:00
function enqueue_WP_GPX_Maps_scripts()
{
?>
<script type='text/javascript' src='https://www.google.com/jsapi?ver=3.2.1'></script>
2011-12-14 13:05:57 +00:00
<script type='text/javascript'>
2011-12-14 10:35:25 +00:00
google.load('visualization', '1', {'packages':['corechart']});
2011-12-20 17:17:24 +00:00
google.load("maps", "3", {other_params: 'sensor=false'});
2011-12-14 10:35:25 +00:00
</script>
2011-12-20 17:17:24 +00:00
<script type='text/javascript' src='<?php echo plugins_url('/WP-GPX-Maps.js', __FILE__) ?>'></script>
2011-12-14 10:35:25 +00:00
<?php
}
2011-12-20 17:17:24 +00:00
2012-01-08 16:47:40 +00:00
function findValue($attr, $attributeName, $optionName, $defaultValue)
2011-12-15 17:10:41 +00:00
{
2012-01-08 16:47:40 +00:00
$val = '';
if ( isset($attr[$attributeName]) )
2011-12-19 13:27:22 +00:00
{
2012-01-08 16:47:40 +00:00
$val = $attr[$attributeName];
2011-12-19 13:27:22 +00:00
}
2012-01-08 16:47:40 +00:00
if ($val == '')
2011-12-19 13:27:22 +00:00
{
2012-01-08 16:47:40 +00:00
$val = get_option($optionName);
2011-12-19 13:27:22 +00:00
}
2012-01-08 16:47:40 +00:00
if ($val == '')
2011-12-19 13:27:22 +00:00
{
2012-01-08 16:47:40 +00:00
$val = $defaultValue;
2011-12-19 13:27:22 +00:00
}
2012-01-08 16:47:40 +00:00
return $val;
}
2012-01-03 09:53:36 +00:00
2012-01-08 16:47:40 +00:00
function handle_WP_GPX_Maps_Shortcodes($attr, $content='')
{
$gpx = findValue($attr, "gpx", "", "");
$w = findValue($attr, "width", "wpgpxmaps_width", "100%");
$mh = findValue($attr, "mheight", "wpgpxmaps_height", "450px");
$mt = findValue($attr, "mtype", "wpgpxmaps_map_type", "HYBRID");
$gh = findValue($attr, "gheight", "wpgpxmaps_graph_height", "200px");
$showW = findValue($attr, "waypoints", "wpgpxmaps_show_waypoint", false);
2012-02-15 16:04:18 +00:00
$showSpeed = findValue($attr, "showspeed", "wpgpxmaps_show_speed", false);
2012-01-08 16:47:40 +00:00
$donotreducegpx = findValue($attr, "donotreducegpx", "wpgpxmaps_donotreducegpx", false);
$pointsoffset = findValue($attr, "pointsoffset", "wpgpxmaps_pointsoffset", 10);
2012-01-28 11:56:52 +00:00
$uom = findValue($attr, "uom", "wpgpxmaps_unit_of_measure", "0");
2012-02-15 16:04:18 +00:00
$uomspeed = findValue($attr, "uomspeed", "wpgpxmaps_unit_of_measure_speed", "0");
2012-01-28 11:56:52 +00:00
$color_map = findValue($attr, "mlinecolor", "wpgpxmaps_map_line_color", "#3366cc");
$color_graph = findValue($attr, "glinecolor", "wpgpxmaps_graph_line_color", "#3366cc");
2012-02-15 16:04:18 +00:00
$color_graph_speed = findValue($attr, "glinecolorspeed", "wpgpxmaps_graph_line_color_speed", "#ff0000");
2012-02-07 13:06:51 +00:00
$r = rand(1,5000000);
2012-02-15 16:04:18 +00:00
$cacheFileName = md5($gpx.$w.$mh.$mt.$gh.$showW.$donotreducegpx.$pointsoffset,$showSpeed);
2012-02-07 13:06:51 +00:00
$gpxcache = gpxCacheFolderPath();
2011-12-29 09:54:00 +00:00
2012-02-07 13:06:51 +00:00
if(!(file_exists($gpxcache) && is_dir($gpxcache)))
2012-01-24 17:05:59 +00:00
{
2012-02-07 13:06:51 +00:00
@mkdir($gpxcache,0755,true);
2012-01-24 17:05:59 +00:00
}
2012-02-07 13:06:51 +00:00
$gpxcache.= DIRECTORY_SEPARATOR.$cacheFileName.".tmp";
// Try to load cache
if (file_exists($gpxcache))
2012-01-24 17:05:59 +00:00
{
2012-02-07 13:06:51 +00:00
try {
$cache_str = file_get_contents($gpxcache);
$cache_obj = unserialize($cache_str);
$points_maps = $cache_obj["points_maps"];
$points_graph = $cache_obj["points_graph"];
$waypoints = $cache_obj["waypoints"];
} catch (Exception $e) {
$points_maps= '';
$points_graph= '';
$waypoints= '';
}
2012-01-24 17:05:59 +00:00
}
2011-12-14 10:35:25 +00:00
2012-02-15 16:04:18 +00:00
if ($points_maps == '' || true)
{
2012-02-07 13:06:51 +00:00
$sitePath = sitePath();
2012-01-28 11:56:52 +00:00
2012-02-07 13:06:51 +00:00
$gpx = trim($gpx);
if (strpos($gpx, "http://") !== 0)
2012-01-28 11:56:52 +00:00
{
2012-02-07 13:06:51 +00:00
$gpx = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $gpx);
$gpx = $sitePath . $gpx;
2012-01-28 11:56:52 +00:00
}
else
{
2012-02-07 13:06:51 +00:00
$gpx = downloadRemoteFile($gpx);
2012-01-28 11:56:52 +00:00
}
2012-02-15 16:04:18 +00:00
if ($gpx == '')
{
return "No gpx found";
}
2012-02-07 13:06:51 +00:00
$points = getPoints( $gpx, $pointsoffset, $donotreducegpx);
$points_maps = '';
$points_graph = '';
$waypoints = '';
2012-02-15 16:04:18 +00:00
if ($showSpeed == true)
{
foreach ($points as $p) {
$points_maps .= '['.(float)$p[0].','.(float)$p[1].'],';
$_speed = $p[4]; // dafault m/s
if ($uomspeed == '2') // miles/h
{
$_speed *= 2.2369362920544025;
}
else if ($uomspeed == '1') // km/h
{
$_speed *= 3.6;
}
if ($uom == '1')
{
// Miles and feet
$points_graph .= '['.((float)$p[3]*0.000621371192).','.((float)$p[2]*3.2808399).','.$_speed.'],';
}
else
{
$points_graph .= '['.(float)$p[3].','.(float)$p[2].','.$_speed.'],';
}
}
}
else
{
foreach ($points as $p) {
$points_maps .= '['.(float)$p[0].','.(float)$p[1].'],';
if ($uom == '1')
{
// Miles and feet
$points_graph .= '['.((float)$p[3]*0.000621371192).','.((float)$p[2]*3.2808399).'],';
}
else
{
$points_graph .= '['.(float)$p[3].','.(float)$p[2].'],';
}
}
2012-02-07 13:06:51 +00:00
}
if ($showW == true)
{
$wpoints = getWayPoints($gpx);
foreach ($wpoints as $p) {
$waypoints .= '['.(float)$p[0].','.(float)$p[1].',\''.unescape($p[4]).'\',\''.unescape($p[5]).'\',\''.unescape($p[7]).'\'],';
}
2011-12-24 14:37:05 +00:00
}
2012-02-07 13:06:51 +00:00
$p="/,$/";
$points_maps = preg_replace($p, "", $points_maps);
$points_graph = preg_replace($p, "", $points_graph);
$waypoints = preg_replace($p, "", $waypoints);
if (preg_match("/^(\[0,0\],?)+$/", $points_graph))
{
$points_graph = "";
}
@file_put_contents($gpxcache,
serialize(array("points_maps" => $points_maps,
"points_graph" => $points_graph,
"waypoints" => $waypoints)
),
LOCK_EX);
@chmod($gpxcache,0755);
2011-12-24 14:37:05 +00:00
}
$output = '
2011-12-14 10:35:25 +00:00
<div id="wpgpxmaps_'.$r.'" style="clear:both;">
2011-12-19 13:27:22 +00:00
<div id="map_'.$r.'" style="width:'.$w.'; height:'.$mh.'"></div>
<div id="chart_'.$r.'" class="plot" style="width:'.$w.'; height:'.$gh.'"></div>
2011-12-14 10:35:25 +00:00
</div>
<script type="text/javascript">
2012-02-15 16:04:18 +00:00
wpgpxmaps({ targetId : "'.$r.'",
mapType : "'.$mt.'",
mapData : ['.$points_maps.'],
graphData : ['.$points_graph.'],
waypoints : ['.$waypoints.'],
unit : "'.$uom.'",
unitspeed : "'.$uomspeed.'",
color1 : "'.$color_map.'",
color2 : "'.$color_graph.'",
color3 : "'.$color_graph_speed.'",
});
2011-12-24 14:37:05 +00:00
</script>';
return $output;
}
2011-12-20 17:17:24 +00:00
2012-01-24 17:05:59 +00:00
function downloadRemoteFile($remoteFile)
{
2012-02-15 16:04:18 +00:00
try
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remoteFile);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
$resp = curl_exec($ch);
curl_close($ch);
$tmpfname = tempnam ( '/tmp', 'gpx' );
$fp = fopen($tmpfname, "w");
fwrite($fp, $resp);
fclose($fp);
return $tmpfname;
} catch (Exception $e) {
return '';
}
2012-01-24 17:05:59 +00:00
}
2011-12-24 14:37:05 +00:00
function unescape($value)
{
$value = str_replace("'", "\'", $value);
$value = str_replace(array("\n","\r"), "", $value);
return $value;
2011-12-15 16:03:29 +00:00
}
2011-12-14 10:35:25 +00:00
function WP_GPX_Maps_install() {
add_option("wpgpxmaps_width", '100%', '', 'yes');
2011-12-19 13:27:22 +00:00
add_option("wpgpxmaps_graph_height", '200px', '', 'yes');
2011-12-14 10:35:25 +00:00
add_option("wpgpxmaps_height", '450px', '', 'yes');
add_option('wpgpxmaps_map_type','HYBRID','','yes');
2011-12-24 14:37:05 +00:00
add_option('wpgpxmaps_show_waypoint','','','yes');
2012-02-15 16:04:18 +00:00
add_option('wpgpxmaps_show_speed','','','yes');
2012-01-03 09:53:36 +00:00
add_option('wpgpxmaps_pointsoffset','10','','yes');
add_option('wpgpxmaps_donotreducegpx','true','','yes');
2012-02-15 16:04:18 +00:00
add_option("wpgpxmaps_unit_of_measure", '0', '', 'yes');
add_option("wpgpxmaps_unit_of_measure_speed", '0', '', 'yes');
2012-01-28 11:56:52 +00:00
add_option("wpgpxmaps_graph_line_color", '#3366cc', '', 'yes');
2012-02-15 16:04:18 +00:00
add_option("wpgpxmaps_graph_line_color_speed", '#ff0000', '', 'yes');
2012-01-28 11:56:52 +00:00
add_option("wpgpxmaps_map_line_color", '#3366cc', '', 'yes');
2011-12-14 10:35:25 +00:00
}
function WP_GPX_Maps_remove() {
delete_option('wpgpxmaps_width');
2011-12-19 13:27:22 +00:00
delete_option('wpgpxmaps_graph_height');
2011-12-14 10:35:25 +00:00
delete_option('wpgpxmaps_height');
delete_option('wpgpxmaps_map_type');
2011-12-24 14:37:05 +00:00
delete_option('wpgpxmaps_show_waypoint');
2012-02-15 16:04:18 +00:00
delete_option('wpgpxmaps_show_speed');
2012-01-03 09:53:36 +00:00
delete_option('wpgpxmaps_pointsoffset');
delete_option('wpgpxmaps_donotreducegpx');
2012-01-28 11:56:52 +00:00
delete_option('wpgpxmaps_unit_of_measure');
2012-02-15 16:04:18 +00:00
delete_option('wpgpxmaps_unit_of_measure_speed');
2012-01-28 11:56:52 +00:00
delete_option('wpgpxmaps_graph_line_color');
delete_option('wpgpxmaps_map_line_color');
2012-02-15 16:04:18 +00:00
delete_option('wpgpxmaps_graph_line_color_speed');
2011-12-14 10:35:25 +00:00
}
2011-12-15 16:03:29 +00:00
?>