1<?php
2/**
3 * Plugin ITrackViewer: Allow Display a Google Map, BikeMap-Track, GPSies-Track or a GPX-Track in a wiki page.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Juergen A. Lamers <jaloma.ac@googlemail.com>
7 * <googlemap kml="http://www.ich-bin-am-wandern-gewesen.de/tracks/20080127_NettersheimTour.kmz" width="600px" height="400px" lat="50.471986" lon="6.643214">
8</googlemap>
9
10<googlemap kml="http://www.ich-bin-am-wandern-gewesen.de/tracks/20080224_ErftTour.kmz" lat="50.70939" lon="6.809865" width="600px" height="425px">
11</googlemap>
12 */
13
14if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_PLUGIN.'syntax.php');
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_itrackviewer extends DokuWiki_Syntax_Plugin {
22
23  var $dflt = array(
24		    'zoom' => 8,
25		    'alt' => 'Ein Track...',
26		    'gpx' => 'off',
27		    'kml' => 'off',
28			// Toursprung-Portale
29		    'bikemap' => 'off',
30			'wandermap' => 'off',
31			'runmap' => 'off',
32			'inlinemap' => 'off',
33			'mopedmap' => 'off',
34			'qrcode' => 'off',
35
36		    'quality' => 'high',
37		    'allowScriptAccess' => 'sameDomain',
38		    'width' => 425,
39		    'height' => 400,
40		    'kmlsubtitle' => 'off',
41		    'mapkey' => 'AARTsJqRkryeAr7c_b0hTdzckxU0KnKhBg', /* Get from GoogleMap... */
42		    'type' => 'p', /* p = Gelaende, h = Hybrid, k = Satellit, m = Karte, o = OSM */
43			// GSPies
44		    'gpsies' => 'off',
45			'picasa' => 'off'
46		    );
47
48    /**
49     * return some info
50     */
51  function getInfo(){
52    return array(
53		 'author' => 'Jürgen A. Lamers',
54		 'email'  => 'jaloma.ac@googlemail.com',
55		 'date'   => '2008-11-22',
56		 'name'   => 'ITrackViewer Plugin',
57		 'desc'   => 'Add maps to your wiki
58                              Syntax: <itrackviewer params></itrackviewer>',
59		 'url'    => 'http://jaloma.ac.googlepages.com/plugin:itrackviewer'
60		 );
61  }
62
63    /**
64     * What kind of syntax are we?
65     */
66  function getType() { return 'substition'; }
67  function getPType() { return 'block'; }
68    /**
69     * Where to sort in to parse?
70     */
71  function getSort() { return 900; }
72
73    /**
74    * Connect pattern to lexer
75    */
76  function connectTo($mode) {
77    $this->Lexer->addSpecialPattern('<itrackviewer ?[^>\n]*>.*?</itrackviewer>',$mode,'plugin_itrackviewer');
78  }
79
80
81  function postConnect() {
82    $this->Lexer->addExitPattern('</itrackviewer>','plugin_itrackviewer');
83  }
84
85    /**
86     * Handle the match
87     */
88  function handle($match, $state, $pos, &$handler){
89    // break matched cdata into its components
90    list($str_params,$str_points) = explode('>',substr($match,13,-15),2);
91    $gmap = $this->_extract_params($str_params);
92    return array($gmap);
93  }
94
95    /**
96     * Create output
97     */
98  function render($mode, &$renderer, $data) {
99    static $initialised = false;//true;//    // set to true after script initialisation
100    if ($mode == 'xhtml') {
101      list($param) = $data;
102
103      $script = '';
104      if (!$initialised) {
105		$initialised = true;
106		$script = $this->getConf('script').$this->getConf('key');
107		$script = '<script type="text/javascript" src="'.$script.'"></script>';
108      }
109      if (isset($param['kml']) && $param['kml'] != 'off') {
110        $renderer->doc .= $this->googlemaps_iframe($script,$param);
111      } else if (isset($param['bikemap']) && $param['bikemap'] != 'off') {
112		$renderer->doc .= $this->toursprung_iframe("bikemap", "Bikemap",'bikemap', $param);
113	  } else if (isset($param['wandermap']) && $param['wandermap'] != 'off') {
114		$renderer->doc .= $this->toursprung_iframe("wandermap", "Wandermap",'wandermap', $param);
115	  } else if (isset($param['runmap']) && $param['runmap'] != 'off') {
116		$renderer->doc .= $this->toursprung_iframe("runmap", "Runmap", 'runmap',$param);
117	  } else if (isset($param['mopedmap']) && $param['mopedmap'] != 'off') {
118		$renderer->doc .= $this->toursprung_iframe("mopedmap", "Mopedmap", 'mopedmap', $param);
119	  } else if (isset($param['inlinemap']) && $param['inlinemap'] != 'off') {
120		$renderer->doc .= $this->toursprung_iframe("inlinemap", "Inlinemap", 'inlinemap', $param);
121      } else if (isset($param['gpsies']) && $param['gpsies'] != 'off') {
122		$renderer->doc .= $this->gpsies_iframe($param);
123      } else if (isset($param['gpx']) && $param['gpx'] != 'off') {
124        $renderer->doc .= $this->gpxviewer_iframe($script, $param);
125      }
126    }
127    return false;
128  }
129
130	function gpsies_iframe($param) {
131		/*
132		 * # Straßenkarte, Wert: "n", Parameter: "&mapType=n"
133# Satellitenkarte, Wert: "k", Parameter: "&mapType=k"
134# Hybridkarte, Wert: "h", Parameter: "&mapType=h"
135# Geländekarte, Wert: "p", Parameter: "&mapType=p"
136		 * # Google Earth Plugin, Wert: "ge", Parameter: "&mapType=ge"
137# OpenStreetMap Standard, Wert: "mapnik", Parameter: "&mapType=mapnik"
138# OpenStreetMap Fahrrad/Cycle, Wert: "cycle", Parameter: "&mapType=cycle"
139		 */
140		$mapType = '';
141		if ($param['type'] != 'off') {
142		$mapType = '&mapType='.$param['type'];
143		}
144		//&picasa=false"
145		$picasa = '&picasa=false';
146		if ($param['gpsies_picasa'] != 'off') {
147			$picasa = '&picasa=true';
148		}
149		$txt = '<iframe align="left" valign="top" title="'.$param['alt'].
150		'" src="http://www.gpsies.de/mapOnly.do?fileId='.$param['gpsies'].
151		$mapType.
152		$picasa.
153		'"'.
154		' width="'.$param['width'].'"'.
155		' height="'.$param['height'].'"'.
156		' style="width:'.$param['width'].'; height: '.$param['height'].';">'.$param['alt'].'</iframe>';
157		/* Der Download bei GPSies gestaltet sich etwas komplizierter (Formular) */
158		//if ($param['qrcode'] != 'off') {
159		//	$txt .= $this->makeQRCode('www.'.$webSpace.'.net/route/'.$param[$tourSprungKey].'/export.gpx',null,null);
160		//}
161		return $txt;
162	}
163
164	function toursprung_iframe($webSpace, $webSpaceDesc, $tourSprungKey, $param)  {
165		// maptype = 0 : Gelaende
166		// maptype = 1 : Satellit
167		// maptype = 2 : Hybrid
168		// maptype = 3 : Karte
169		// maptype = 4 : OSM
170		/* p = Gelaende, h = Hybrid, k = Satellit, m = Karte, o = OSM */
171		$maptype = 0;
172		switch ($param['type']) {
173			case 'p' : $maptype = 0; break;
174			case 'k' : $maptype = 1; break;
175			case 'h' : $maptype = 2; break;
176			case 'm' : $maptype = 3; break;
177			case 'o' : $maptype = 4; break;
178			default :
179			$maptype = 0;
180		}
181		$w_width = str_replace("px","",$param['width']);
182		$w_height = str_replace("px","",$param['height']);
183		$txt = '<div style="margin-top:2px;margin-bottom:2px;width:'.$w_width.'px;font-family:Arial,Helvetica,sans-serif;font-size:9px;color:#535353;background-color:#ffffff;border:2px solid #6db466;font-style:normal;text-align:right;padding:0px;padding-bottom:3px !important;">';
184		$txt .= '<iframe src="http://www.'.$webSpace.'.net/route/'.$param[$tourSprungKey].'/widget?width='.$w_width.'&amp;height='.$w_height.'&amp;maptype='.$maptype.'&amp;extended=true" width="'.$w_width.'" height="'.$w_height.'" border="0" frameborder="0" marginheight="0" marginwidth="0"  scrolling="no">'.$param['alt'].'</iframe>';
185		$txt .= '<br />Route <a style="color:#6db466; text-decoration:underline;" href="http://www.'.$webSpace.'.net/route/'.$param[$tourSprungKey].'">'.$param[$tourSprungKey].'</a> - powered by <a style="color:#6db466; text-decoration:underline;" href="http://www.'.$webSpace.'.net/">'.$webSpaceDesc.'</a>&nbsp;';
186		$txt .= '</div>';
187		if ($param['qrcode'] != 'off') {
188			$txt .= $this->makeQRCode('www.'.$webSpace.'.net/route/'.$param[$tourSprungKey].'/export.gpx',null,null);
189		}
190		return $txt;
191	}
192
193	function makeQRCode($destLink, $caption, $alt) {
194		global $conf;
195		$txt = '';
196		$txt .= '<table>';
197		$txt .= '<tr>';
198		$txt .= '<td>';
199		$txt .= '<a target="external" href="http://de.wikipedia.org/wiki/QR_Code">';
200		$inigma = '<img border="0" style="vertical-align:top;" src="http://encode.i-nigma.com/QRCode/img.php?d=http%3A%2F%2F'.$destLink.'&s=3"';
201		$qrcode = '<img border="0" style="vertical-align:top;" src="http://qrcode.kaywa.com/img.php?d=http%3A%2F%2F'.$destLink.'&s=3"';
202		if ($this->getConf('qrcode') == 'inigma') {
203			$txt .= $inigma;
204		} else if ($this->getConf('qrcode') == 'all') {
205			$txt .= "<span class='2dcode'>".$qrcode."</span>".
206			"<span class='2dcode'>".$inigma."</span>";
207		} else {
208			$txt .= $qrcode;
209		}
210		if ($caption != null && $caption != "") {
211			$txt .= '&c='.$caption;
212		}
213		if ($alt == null || $alt == "") {
214			$alt = "Track";
215		}
216		$txt .= ' alt="'.$alt.'" />';
217		$txt .= '</a>';
218		$txt .= '</td>'; //style="vertical-align:top;"
219		$txt .= '<td valign="top">';
220		$txt .= 'Get this track directly to your mobile!.<br/>Dont have barcode reader ? <a href="http://www.freewarepocketpc.net/ppc-tag-barcode.html" target="external"> click here</a>';
221		$txt .= '<br/>Get some information about <a target="external" href="http://de.wikipedia.org/wiki/QR_Code">QR_Code</a> (WikiPedia.de)';
222		$txt .= '</td>';
223		$txt .= '</tr>';
224		$txt .= '</table>';
225		return $txt;
226	}
227
228	function gpxviewer_iframe($script, $param) {
229        return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="400" height="300" id="gpxviewer" align="middle">'.
230    	'<param name="swLiveConnect" value="true">'.
231		'<param name="allowScriptAccess" value='.$param['allowScriptAccess'].' />'.
232		'<param name="movie" value="swf/gpxviewer.swf?gpx='.$param['gpx'].'&amp;zoom='.$param['zoom'].'" />'.
233		'<param name="quality" value="'.$param['quality'].'" />'.
234		'<param name="scale" value="noscale" />'.
235		'<param name="salign" value="lt" />'.
236		'<param name="bgcolor" value="#ffffff" />'.
237		'<embed src="swf/gpxviewer.swf?gpx='.$param['gpx'].'&amp;zoom='.$param['zoom'].'" '.
238		       'quality="high" '.
239		       'scale="noscale" '.
240		       'salign="lt" '.
241		       'bgcolor="#ffffff" '.
242		       'width="400" '.
243		       'height="300" '.
244		       'name="gpxviewer" '.
245		       'align="middle" '.
246		       'allowScriptAccess='.$param['allowScriptAccess'].''.
247		       'type="application/x-shockwave-flash" '.
248		       'swliveconnect="true" '.
249		       'pluginspage="http://www.macromedia.com/go/getflashplayer" />'.
250		'</object>'.
251		'	';
252	}
253
254	function googlemaps_iframe($script, $param) {
255		$maptype = $param['type'];
256		if ('o' == $maptype) {
257			$maptype = 'p'; // Zurückschalten, da GoogleMaps bestimmt kein OSM anbietet ;-)
258		}
259		$googleurl = 'http://maps.google.de/maps?f=q&amp;hl=de&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q='.$param['kml'].'&amp;ie=UTF8&amp;om=1&amp;s='.$param['mapkey'].'&amp;z='.$param['zoom'].'&amp;t='.$maptype.'&amp;hl=de&amp;';
260		$txt ='<iframe width="'.$param['width'].'" height="'.$param['height'].'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$googleurl.'output=embed"></iframe>';//ll=50.5443,6.221945&amp;spn=0.038181,0.072956
261		if ($param['kmlsubtitle'] != 'off') {
262	  		$txt .= '<br /><small><a href="'.$googleurl.'&amp;source=embed"  style="color:#0000FF;text-align:left">Gr&ouml;&szlig;ere Kartenansicht</a></small>';
263	    //	  '<br /><small><a href="'.$url.'&amp;ie=UTF8&amp;om=1&amp;z='.$param['zoom'].'&amp;t='.$param['type'].'&amp;source=embed" style="color:#0000FF;text-align:left">Gr��ere Kartenansicht</a></small>';
264	  //&amp;ll=50.5443,6.221945&amp;spn=0.038181,0.072956
265		}
266		return $txt;
267	}
268
269  /**
270   * extract parameters for the googlemap from the parameter string
271   *
272   * @param   string    $str_params   string of key="value" pairs
273   * @return  array                   associative array of parameters key=>value
274   */
275  function _extract_params($str_params) {
276    $param = array();
277    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
278    if (sizeof($param) == 0) {
279      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
280    }
281    // parse match for instructions, break into key value pairs
282    $gmap = $this->dflt;
283    foreach($param as $kvpair) {
284      list($match,$key,$val) = $kvpair;
285//    $key = strtolower($key);
286      if (isset($gmap[$key])) $gmap[$key] = $val;
287    }
288
289    return $gmap;
290  }
291} /* CLASS */
292