1<?php 2/* 3 * Copyright (c) 2008-2012 Mark C. Prins <mprins@users.sf.net> 4* 5* Permission to use, copy, modify, and distribute this software for any 6* purpose with or without fee is hereby granted, provided that the above 7* copyright notice and this permission notice appear in all copies. 8* 9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*/ 17if (!defined('DOKU_INC')) die(); 18if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 19require_once DOKU_PLUGIN.'openlayersmap/StaticMap.php'; 20/** 21 * DokuWiki Plugin openlayersmap (staticmap Helper Component). 22 * This provides the interface to generate a static map based on predefined OSM layers. 23 * 24 * @author Mark Prins 25 */ 26class helper_plugin_openlayersmap_staticmap extends DokuWiki_Plugin { 27 /** maximum width of the resulting image. */ 28 private $maxWidth = 1024; 29 /** maximum heigth of the resulting image. */ 30 private $maxHeight = 1024; 31 32 /** 33 * Provide metadata of the public methods of this class. 34 * 35 * @return array Information to all provided methods. 36 */ 37 function getMethods(){ 38 $result = array(); 39 $result[] = array( 40 'name' => 'getMap', 41 'desc' => 'returns url to the image', 42 'params' => array( 43 'center' => 'string', 44 'zoom' => 'integer', 45 'size' => 'string', 46 'maptype' => 'string', 47 'markers' => 'string', 48 'gpx' => 'string', 49 'kml' => 'string'), 50 'return' => array('image' => 'string'), 51 ); 52 return $result; 53 } 54 55 /** 56 * Create the map. 57 * 58 * @param number lat the latitude of the map's center, eg. 40.714728 59 * @param number lon the longitude of the map's center, eg -73.998672 60 * @param number zoom the zoom level in the tile cache, eg. 14 61 * @param mixed size the size in WxH px, eg. 512x512 62 * @param string maptype the maptype, eg. cycle 63 * @param mixed markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), eg. array( 'lat'=>40.702147, 'lon'=>-74.015794, 'type'=>lightblue1); 64 * @param string gpx media link 65 * @param string kml media link 66 * 67 * @return the media id url 68 */ 69 public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml){ 70 global $conf; 71 // dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :'); 72 73 // normalize zoom 74 $zoom = $zoom?intval($zoom):0; 75 if($zoom > 18) $zoom = 18; 76 // normalize WxH 77 list($width, $height) = split('x',$size); 78 $width = intval($width); 79 if($width > $this->maxWidth) $width = $this->maxWidth; 80 $height = intval($height); 81 if($height > $this->maxHeight) $height = $this->maxHeight; 82 83 // cleanup/validate gpx/kml 84 $kml = $this->mediaIdToPath($kml); 85 // dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:'); 86 $gpx = $this->mediaIdToPath($gpx); 87 // dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:'); 88 89 // create map 90 $map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype, $markers, $gpx, $kml, 91 $conf['mediadir'], 92 $conf['cachedir'], 93 $this->getConf('autoZoomMap') 94 ); 95 96 // return the media id url 97 $mediaId = str_replace('/', ':', $map->getMap()); 98 // if($this->startsWith($mediaId,':')) { 99 // $mediaId = substr($mediaId, 1); 100 // } 101 return $mediaId; 102 } 103 104 private function startsWith($haystack, $needle) { 105 $length = strlen($needle); 106 return (substr($haystack, 0, $length) === $needle); 107 } 108 109 /** 110 * Constructs the path to a file. 111 * @param string $id the DW media id 112 * @return the path to the file 113 */ 114 private function mediaIdToPath($id){ 115 global $conf; 116 if(empty($id)) { 117 return ""; 118 } 119 $id=str_replace("[[","",$id); 120 $id=str_replace("]]","",$id); 121 if($this->startsWith($id,':')) { 122 $id = substr($id, 1); 123 } 124 $id=str_replace(":","/",$id); 125 return $conf['mediadir'].'/'.$id; 126 } 127}