1<?php 2/* 3 * Copyright (c) 2008-2022 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 * 17 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps 18 */ 19 20use dokuwiki\plugin\openlayersmap\StaticMap; 21 22/** 23 * DokuWiki Plugin openlayersmap (staticmap Helper Component). 24 * This provides the interface to generate a static map based on predefined OSM layers. 25 * 26 * @author Mark Prins 27 */ 28class helper_plugin_openlayersmap_staticmap extends DokuWiki_Plugin { 29 /** maximum width of the resulting image. */ 30 private $maxWidth = 1024; 31 /** maximum heigth of the resulting image. */ 32 private $maxHeight = 1024; 33 34 /** 35 * Provide metadata of the public methods of this class. 36 * 37 * @return array Information to all provided methods. 38 */ 39 public function getMethods(): array { 40 $result = array(); 41 $result[] = array( 42 'name' => 'getMap', 43 'desc' => 'returns url to the image', 44 'params' => array( 45 'lat' => 'float', 46 'lon' => 'float', 47 'zoom' => 'integer', 48 'size' => 'string', 49 'maptype' => 'string', 50 'markers' => 'string', 51 'gpx' => 'string', 52 'kml' => 'string', 53 'geojson' => 'string', 54 'apikey' => 'string' 55 ), 56 'return' => array('image' => 'string'), 57 ); 58 return $result; 59 } 60 61 /** 62 * Create the map. 63 * 64 * @param float $lat the latitude of the map's center, eg. 40.714728 65 * @param float $lon the longitude of the map's center, eg -73.998672 66 * @param int $zoom the zoom level in the tile cache, eg. 14 67 * @param string $size the size in WxH px, eg. 512x512 68 * @param string $maptype the maptype, eg. cycle 69 * @param array $markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), 70 * eg. array('lat'=>40.702147,'lon'=>-74.015794,'type'=>lightblue1); 71 * @param string $gpx media link 72 * @param string $kml media link 73 * @param string $geojson media link 74 * @param string $apikey optional API key eg. for Thunderforest maps 75 * 76 * @return string 77 */ 78 public function getMap( 79 float $lat, 80 float $lon, 81 int $zoom, 82 string $size, 83 string $maptype, 84 array $markers, 85 string $gpx, 86 string $kml, 87 string $geojson, 88 string $apikey = '' 89 ): string { 90 global $conf; 91 // dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :'); 92 93 // normalize zoom 94 $zoom = $zoom ?: 0; 95 if($zoom > 18) { 96 $zoom = 18; 97 } 98 // normalize WxH 99 list($width, $height) = explode('x', $size); 100 $width = (int) $width; 101 if($width > $this->maxWidth) { 102 $width = $this->maxWidth; 103 } 104 $height = (int) $height; 105 if($height > $this->maxHeight) { 106 $height = $this->maxHeight; 107 } 108 109 // cleanup/validate gpx/kml 110 $kml = $this->mediaIdToPath($kml); 111 // dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:'); 112 $gpx = $this->mediaIdToPath($gpx); 113 // dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:'); 114 $geojson = $this->mediaIdToPath($geojson); 115 116 // create map 117 $map = new StaticMap( 118 $lat, $lon, $zoom, $width, $height, $maptype, 119 $markers, $gpx, $kml, $geojson, $conf['mediadir'], $conf['cachedir'], 120 $this->getConf('autoZoomMap'), $apikey 121 ); 122 123 // return the media id url 124 // $mediaId = str_replace('/', ':', $map->getMap()); 125 // if($this->startsWith($mediaId,':')) { 126 // $mediaId = substr($mediaId, 1); 127 // } 128 // return $mediaId; 129 return str_replace('/', ':', $map->getMap()); 130 } 131 132 /** 133 * Constructs the path to a file. 134 * @param string $id the DW media id 135 * @return string the path to the file 136 */ 137 private function mediaIdToPath(string $id): string { 138 global $conf; 139 if(empty($id)) { 140 return ""; 141 } 142 $id = str_replace(array("[[", "]]"), "", $id); 143 if((strpos($id, ':') === 0)) { 144 $id = substr($id, 1); 145 } 146 $id = str_replace(":", "/", $id); 147 return $conf['mediadir'] . '/' . $id; 148 } 149} 150