1<?php
2/**
3 *  Panorama-Picture Plugin
4 *
5 *  @license    GPL 2 ( http://www.gnu.org/licenses/gpl.html )
6 *  @author     jaloma.ac [at] googlemail [dot] com
7 *
8 *  Description:
9 *  Plugin to display Panorama-Pictures with Java-Applet http://www.tshsoft.de/panostudioapplet/index.html
10 *
11 *  Acknowledgements:
12 *  The java applet author(s)
13 */
14
15if (!defined('DOKU_INC'))define('DOKU_INC', realpath(dirname( __FILE__ ).'/../../').'/');
16if (!defined('DOKU_PLUGIN'))define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
17require_once (DOKU_PLUGIN.'syntax.php');
18require_once (DOKU_PLUGIN.'panorama/render_helper.php');
19
20class syntax_plugin_panorama_panorama2 extends DokuWiki_Syntax_Plugin
21{
22    var $dflt = array (
23    'width'=>800,
24    'height'=>400,
25    'name'=>'',
26    'file'=>'',
27    'hsfile'=>'off',
28    'mode'=>'java'
29    );
30    /**
31     *  Plugin Info
32     */
33    function getInfo()
34    {
35        return array (
36        'author'=>'Jürgen A. Lamers',
37        'email'=>'jaloma.ac [at] googlemail [dot] com',
38        'date'=>@file_get_contents(DOKU_PLUGIN . 'panorama/VERSION'),
39        'name'=>'panorama2',
40        'desc'=>'Panorama-Picture Plugin <panorama2 name="" width="" height="" mode="" file="" hsfile="">xmlData</panorama2>',
41        'url'=>'http://www.dokuwiki.org/plugin:panorama',
42        );
43    }
44
45    /**
46     *  Typology?
47     */
48    function getType()
49    {
50        return 'substition';
51    }
52
53    /**
54     *  Sort Code?
55     */
56    function getSort()
57    {
58        return 316;
59    }
60
61    /**
62     *  Pattern Matching?
63     */
64    function connectTo($mode)
65    {
66        $this->Lexer->addSpecialPattern('<panorama2 ?[^>\n]*>.*?</panorama2>', $mode, 'plugin_panorama_panorama2');
67    }
68
69    function matchLength()
70    {
71        return strlen("<panorama2");
72    }
73    /**
74     *  Parsing
75     *  1. Very rough parsing involved
76     *  2. Applet parameters not included
77     */
78    function handle($match, $state, $pos, & $handler)
79    {
80        list ($str_params, $str_images) = explode('>', substr($match, $this->matchLength(), -1*$this->matchLength()-2), 2);
81        $thumb = $this->_extract_params($str_params);
82        $imgs = $this->_extract_images($str_images);
83        return array ($thumb, $imgs);
84
85    }
86    /**
87     * extract parameters for the googlemap from the parameter string
88     *
89     * @param   string    $str_params   string of key="value" pairs
90     * @return  array                   associative array of parameters key=>value
91     */
92    function _extract_params($str_params)
93    {
94        $param = array ();
95        preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);
96        if (sizeof($param) == 0)
97        {
98            preg_match_all("/(\w*)='(.*?)'/us", $str_params, $param, PREG_SET_ORDER);
99        }
100        // parse match for instructions, break into key value pairs
101        $gmap = $this->dflt;
102        foreach ($param as $kvpair)
103        {
104            list ($match, $key, $val) = $kvpair;
105            if ( isset ($gmap[$key]))$gmap[$key] = $val;
106        }
107
108        return $gmap;
109    }
110    function _extract_images($str_images)
111    {
112        return $str_images;
113    }
114
115    /**
116     *  Rendering
117     */
118    function render($mode, & $renderer, $all_data)
119    {
120        if ($mode == 'xhtml')
121        {
122            global $conf;
123            $data = $all_data[0];
124            $name = $data['name'];
125            $file = $data['file'];
126            $file = $this->createFileName4Picture($file);
127            if (strpos($file, "http://") === null)
128            {
129                $fqPicFile = getcwd().$file;
130                if (!file_exists($fqPicFile))
131                {
132                    $renderer->doc .= " File not found ".$fqPicFile;
133                    return true;
134                }
135            }
136            $hsfile = $data['hsfile'];
137            if ($hfile != 'off')
138            {
139                $hsfile = $this->createFileName4Picture($hsfile);
140                if (strpos($hsfile, "http://") === null)
141                {
142                    $fqHSPicFile = getcwd().$hsfile;
143                    if (!file_exists($fqHSPicFile))
144                    {
145                        $renderer->doc .= " Hotspot-File not found ".$fqHSPicFile;
146                        return true;
147                    }
148                }
149            }
150
151            $xmlFileName = str_replace(':', '/', $name);
152            $xmlData = $all_data[1];
153            $this->writeXMLFile($xmlFileName, $file, $xmlData, $hsfile);
154            $mode = $data['mode'];
155            $txt = "";
156            if ($mode == null || $mode == 'java')
157            {
158                $txt = render_helper::_render_java($data);
159            } elseif ($mode == 'swf' || $mode == 'flash')
160            {
161                $txt = render_helper::_render_swf($data);
162            }
163            if ($this->getConf('showPanoramaUrl'))
164            {
165                $txt .= '
166<br/>
167Powered by <a href="http://www.tshsoft.com/panostudioapplet/index.html">PanoramaStudio Viewer</a>';
168            }
169            $renderer->doc .= $txt;
170            return true;
171        } else
172        {
173            return false;
174        }
175    }
176
177    function createFileName4Picture($file)
178    {
179        global $conf;
180        if (strpos($file, "http://") === false)
181        {
182            if (strpos($file, ':') !== false)
183            {
184                $dataDir = $conf['savedir'].'/media';
185                $file = str_replace(':', '/', $file);
186                $file = $dataDir.'/'.$file;
187            }
188        }
189        return $file;
190    }
191    function writeXMLFile($xmlFileName, $picFileName, $panoInfo, $hotspotFile)
192    {
193        global $conf;
194        $dataDir = $conf['savedir'].'/media';
195        if ($dataDir[0] == '.')
196        {
197            $dataDir = substr($dataDir, 1);
198        }
199        if ($picFileName[0] == '.')
200        {
201            $picFileName = substr($picFileName, 1);
202        }
203        $currentPath = getcwd();
204        $fqFileName = $currentPath.$dataDir.'/'.$xmlFileName.'.xml';
205        if (strpos($currentPath, '\\') !== false)
206        {
207            $fqFileName = str_replace('/', '\\', $fqFileName);
208        }
209        $panoInfo = str_replace('[', '<', $panoInfo);
210        $panoInfo = str_replace(']', '>', $panoInfo);
211        $picFileName = '<file>'.$picFileName.'</file>';
212        if ($hotspotFile != 'off' && $hotspotFile != '')
213        {
214            $picFileName .= "\n".'<hsfile>'.$hotspotFile.'</hsfile>';
215        }
216        $fstream = fopen($fqFileName, 'w');
217        $head = '<?xml version="1.0" encoding="ISO-8859-1"
218?>
219<panoramaStudioViewer>
220    <panorama>
221        ';
222        $foot = '
223    </panorama>
224</panoramaStudioViewer>';
225fwrite($fstream, $head);
226fwrite($fstream, $picFileName);
227fwrite($fstream,$panoInfo);
228fwrite($fstream, $foot);
229fclose($fstream);
230}
231} // clas
232