From 835f87370508fc53b909086c0e9c63ed09fcaa24 Mon Sep 17 00:00:00 2001 From: bastianonm Date: Tue, 7 Feb 2012 13:06:51 +0000 Subject: [PATCH] --- readme.txt | 6 +- wp-gpx-maps.php | 130 ++++++++++++++++++++++------------- wp-gpx-maps_Utils.php | 45 ++++++++++++ wp-gpx-maps_admin.php | 17 ++++- wp-gpx-maps_admin_tracks.php | 23 +++++-- 5 files changed, 167 insertions(+), 54 deletions(-) diff --git a/readme.txt b/readme.txt index d11cf11..bf8661f 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=basti Tags: maps, gpx, gps, graph, google maps, google chart, track, garmin Requires at least: 2.0.0 Tested up to: 3.3 -Stable tag: 1.1.4 +Stable tag: 1.1.5 License: GPLv2 or later Draws a gpx track with altitude graph @@ -79,6 +79,9 @@ Yes! 2. Admin area - Settings == Changelog == += 1.1.5 = +* implemented cache (the plugin is much faster, especially on slow servers or external gpx) +* minor bug fixes = 1.1.4 = * improved admin area * added miles/feet unit of measure @@ -121,6 +124,7 @@ Yes! * Initial release. == Upgrade Notice == += 1.1.5 = = 1.1.4 = = 1.1.3 = = 1.1.2 = diff --git a/wp-gpx-maps.php b/wp-gpx-maps.php index 76847a9..bde5efd 100644 --- a/wp-gpx-maps.php +++ b/wp-gpx-maps.php @@ -3,7 +3,7 @@ Plugin Name: WP-GPX-Maps Plugin URI: http://www.darwinner.it/ Description: Draws a gpx track with altitude graph -Version: 1.1.4 +Version: 1.1.5 Author: Bastianon Massimo Author URI: http://www.pedemontanadelgrappa.it/ License: GPL @@ -82,60 +82,98 @@ function handle_WP_GPX_Maps_Shortcodes($attr, $content='') $uom = findValue($attr, "uom", "wpgpxmaps_unit_of_measure", "0"); $color_map = findValue($attr, "mlinecolor", "wpgpxmaps_map_line_color", "#3366cc"); $color_graph = findValue($attr, "glinecolor", "wpgpxmaps_graph_line_color", "#3366cc"); - + $r = rand(1,5000000); - - $sitePath = sitePath(); - - $gpx = trim($gpx); - - if (strpos($gpx, "http://") !== 0) - { - $gpx = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $gpx); - $gpx = $sitePath . $gpx; - } - else - { - $gpx = downloadRemoteFile($gpx); - } - - $points = getPoints( $gpx, $pointsoffset, $donotreducegpx); - $points_maps = ''; - $points_graph = ''; - $waypoints = ''; - foreach ($points as $p) { - $points_maps .= '['.(float)$p[0].','.(float)$p[1].'],'; + $cacheFileName = md5($gpx.$w.$mh.$mt.$gh.$showW.$donotreducegpx.$pointsoffset); + $gpxcache = gpxCacheFolderPath(); + + if(!(file_exists($gpxcache) && is_dir($gpxcache))) + { + @mkdir($gpxcache,0755,true); + } + + $gpxcache.= DIRECTORY_SEPARATOR.$cacheFileName.".tmp"; + + // Try to load cache + if (file_exists($gpxcache)) + { + 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= ''; + } + } + + if ($points_maps == '') + { + + $sitePath = sitePath(); - if ($uom == '1') + $gpx = trim($gpx); + + if (strpos($gpx, "http://") !== 0) { - // Miles and feet - $points_graph .= '['.((float)$p[3]*0.000621371192).','.((float)$p[2]*3.2808399).'],'; + $gpx = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $gpx); + $gpx = $sitePath . $gpx; } else { - $points_graph .= '['.(float)$p[3].','.(float)$p[2].'],'; + $gpx = downloadRemoteFile($gpx); } - } - - 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]).'\'],'; - } - } - - $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 = ""; - } + + $points = getPoints( $gpx, $pointsoffset, $donotreducegpx); + $points_maps = ''; + $points_graph = ''; + $waypoints = ''; + 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].'],'; + } + } + + 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]).'\'],'; + } + } + + $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); + } + $output = '
diff --git a/wp-gpx-maps_Utils.php b/wp-gpx-maps_Utils.php index 3e86a1f..477662a 100644 --- a/wp-gpx-maps_Utils.php +++ b/wp-gpx-maps_Utils.php @@ -15,6 +15,14 @@ return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $ret); } + function gpxCacheFolderPath() + { + $upload_dir = wp_upload_dir(); + $uploadsPath = $upload_dir['basedir']; + $ret = $uploadsPath.DIRECTORY_SEPARATOR."gpx".DIRECTORY_SEPARATOR."cache"; + return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $ret); + } + function relativeGpxFolderPath() { $sitePath = sitePath(); @@ -22,6 +30,43 @@ $ret = str_replace($sitePath,'',$realGpxPath).DIRECTORY_SEPARATOR; return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $ret); } + + function recursive_remove_directory($directory, $empty=FALSE) + { + if(substr($directory,-1) == '/') + { + $directory = substr($directory,0,-1); + } + if(!file_exists($directory) || !is_dir($directory)) + { + return FALSE; + }elseif(is_readable($directory)) + { + $handle = opendir($directory); + while (FALSE !== ($item = readdir($handle))) + { + if($item != '.' && $item != '..') + { + $path = $directory.'/'.$item; + if(is_dir($path)) + { + recursive_remove_directory($path); + }else{ + unlink($path); + } + } + } + closedir($handle); + if($empty == FALSE) + { + if(!rmdir($directory)) + { + return FALSE; + } + } + } + return TRUE; + } function getPoints($gpxPath,$gpxOffset = 10, $donotreducegpx) { diff --git a/wp-gpx-maps_admin.php b/wp-gpx-maps_admin.php index 6a4cc45..a3b787a 100644 --- a/wp-gpx-maps_admin.php +++ b/wp-gpx-maps_admin.php @@ -21,6 +21,7 @@ function ilc_admin_tabs( $current ) { function WP_GPX_Maps_html_page() { $realGpxPath = gpxFolderPath(); + $cacheGpxPath = gpxCacheFolderPath(); $relativeGpxPath = relativeGpxFolderPath(); $relativeGpxPath = str_replace("\\","/", $relativeGpxPath); $gpxRegEx = '/.gpx$/'; @@ -42,13 +43,27 @@ function WP_GPX_Maps_html_page() { } else { - if (!@mkdir($realGpxPath,755,true)) { + if (!@mkdir($realGpxPath,0755,true)) { echo '
Can\'t create '.$realGpxPath.' folder. Please create it and make it writable!
If not, you will must update the file manually!
'; } } + + if(file_exists($cacheGpxPath) && is_dir($cacheGpxPath)) + { + //dir exsist! + } + else + { + if (!@mkdir($cacheGpxPath,0755,true)) { + echo '
+ Can\'t create '.$cacheGpxPath.' folder. Please create it and make it writable!
+ If not, cache will not created and your site could be slower! +
'; + } + } ilc_admin_tabs($tab); diff --git a/wp-gpx-maps_admin_tracks.php b/wp-gpx-maps_admin_tracks.php index f508932..c535403 100644 --- a/wp-gpx-maps_admin_tracks.php +++ b/wp-gpx-maps_admin_tracks.php @@ -8,13 +8,19 @@ unlink($realGpxPath ."/". $del); } } + + if ( isset($_POST['clearcache']) ) + { + echo "pulisco la cahce"; + recursive_remove_directory($cacheGpxPath,true); + } if ( is_writable ( $realGpxPath ) ){ ?>
-
+ Choose a file to upload:
+ +
+ +
+
- Delete - | - Download - | - Shortcode: [sgpx gpx=""] + Delete + | + Download + | + Shortcode: [sgpx gpx=""]