1<?php
2/**
3 * DokuWiki Plugin ryubin (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Robert Kormann <ryubin@kormann.info>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'syntax.php';
17
18class syntax_plugin_ryubin_ryubin extends DokuWiki_Syntax_Plugin {
19    public function getType() {
20        return 'substition';
21    }
22
23    public function getPType() {
24        return 'block';
25    }
26
27    public function getSort() {
28        return 316;
29    }
30
31    public function connectTo($mode) {
32        $this->Lexer->addSpecialPattern('\{\{ryubin>[^}]*\}\}',$mode,'plugin_ryubin_ryubin');
33    }
34
35    public function handle($match, $state, $pos, &$handler){
36        global $conf;
37        $data = array();
38
39        $match = substr($match,9,-2);
40
41        // extract image or xml path and parameters
42        list($img,$params) = explode('&',$match,2);
43        $img = trim($img);
44        $data['id'] = substr(md5($img),0,8);
45        $data['mime'] = strtolower(explode('.',$img));
46        $data['img_path'] = $conf['basedir']."_media/".$img;
47
48        // default values for the parameters
49        $primary_xml               = 'ryubin.xml'; if (is_file(DOKU_PLUGIN."ryubin/".$this->getconf('primary_xml'))) $primary_xml = $this->getConf('primary_xml');
50        $data['playmode']          = $this->getConf('playmode');
51        $data['fov']               = $this->getConf('fov');
52        $data['yaw']               = $this->getConf('yaw');
53        $data['pitch']             = $this->getConf('pitch');
54        $data['view_mode']         = intval($this->getConf('view_mode'));
55        $data['hv_ratio']          = $this->getConf('hv_ratio');
56        $data['lock_nadirs']       = 'no'; if ($this->getConf('lock_nadirs')) $data['lock_nadirs'] = 'yes';
57        $data['lock_vertical']     = 'no'; if ($this->getConf('lock_vertical')) $data['lock_vertical'] = 'yes';
58        $data['lock_fov']          = 'no'; if ($this->getConf('lock_fov')) $data['lock_fov'] = 'yes';
59        $data['limit_vertical']    = 'no'; if ($this->getConf('limit_vertical')) $data['limit_vertical'] = 'yes';
60        $data['top_limit']         = $this->getConf('top_limit');
61        $data['bottom_limit']      = $this->getConf('bottom_limit');
62        $data['limit_horizontal']  = 'no'; if ($this->getConf('limit_horizontal')) $data['limit_horizontal'] = 'yes';
63        $data['right_limit']       = $this->getConf('right_limit');
64        $data['left_limit']        = $this->getConf('left_limit');
65        $data['proj_implicit']     = 'no'; if ($this->getConf('proj_implicit')) $data['proj_implicit'] = 'yes';
66        $data['elevation']         = $this->getConf('elevation');
67        $data['depression']        = $this->getConf('depression');
68        $data['proj_type']         = intval($this->getConf('proj_type'));
69        $data['adminMode']         = 'no'; if ($this->getConf('adminMode')) $data['adminMode'] = 'yes';
70        $data['width']             = $this->getConf('width');
71        $data['height']            = $this->getConf('height');
72        $data['bgcolor']           = $this->getConf('bgcolor');
73        $data['xml_path']          = $conf['basedir']."lib/plugins/ryubin/".$primary_xml;
74        $data['smooth']            = 'no'; if ($this->getConf('smooth')) $data['smooth'] = 'yes';
75        $data['disable_wheel']     = 'no'; if ($this->getConf('disable_wheel')) $data['disable_wheel'] = 'yes';
76        $data['allowFullScreen']   = 'false'; if ($this->getConf('allowFullScreen')) $data['allowFullScreen'] = 'true';
77        $data['allowScriptAccess'] = $this->getConf('allowScriptAccess');
78
79        // implement parameters from markup
80        $params = explode('&',$params);
81        foreach ($params as $param) {
82            list($xml,$value)=explode('=',$param,2);
83            $xml = trim($xml);
84            switch ($xml) {
85                case 'playmode':
86                    $modes = array('cube3x2','cube2x3','cube6','cube24','cylinder','doughnut','dualfisheye','fish2rect','fisheye','flat','simpleflat','sphere','sphere8','verticalfish2rect','verticalfisheye');
87                    foreach ($modes as $mode) {
88                        if ($value == $mode) $data['playmode'] = $value;
89                    }
90                    break;
91                case 'fov':               $value = intval($value); if ($value > 19 && $value < 131) $data['fov'] = $value; break;
92                case 'yaw':               $value = intval($value); if ($value > -181 && $value < 181) $data['yaw'] = $value; break;
93                case 'pitch':             $value = intval($value); if ($value > -181 && $value < 181) $data['pitch'] = $value; break;
94                case 'view_mode':         $value = intval($value); if ($value > 0 && $value < 4) $data['view_mode'] = $value; break;
95                case 'hv_ratio':          if (is_numeric($value) && $value >= 0.1 && $value <= 10.0) $data['hv_ratio'] = $value; break;
96                case 'lock_nadirs':       if ($value == 'yes') $data['lock_nadirs'] = $value; break;
97                case 'lock_vertical':     if ($value == 'yes') $data['lock_vertical'] = $value; break;
98                case 'lock_fov':          if ($value == 'yes') $data['lock_fov'] = $value; break;
99                case 'limit_vertical':    if ($value == 'yes') $data['limit_vertical'] = $value; break;
100                case 'top_limit':         $value = intval($value); if ($value > 9 && $value < 91) $data['top_limit'] = $value; break;
101                case 'bottom_limit':      $value = intval($value); if ($value > 9 && $value < 91) $data['bottom_limit'] = $value; break;
102                case 'limit_horizontal':  if ($value == 'yes') $data['limit_horizontal'] = $value; break;
103                case 'right_limit':       $value = intval($value); if ($value > 9 && $value < 91) $data['right_limit'] = $value; break;
104                case 'left_limit':        $value = intval($value); if ($value > 9 && $value < 91) $data['left_limit'] = $value; break;
105                case 'proj_implicit':     if ($value == 'no') $data['proj_implicit'] = $value; break;
106                case 'elevation':         $value = intval($value); if ($value > 0 && $value < 91) $data['elevation'] = $value; break;
107                case 'depression':        $value = intval($value); if ($value > 0 && $value < 91) $data['depression'] = $value; break;
108                case 'proj_type':         $value = intval($value); if ($value == 2) $data['proj_type'] = $value; break;
109            }
110            if ($data['adminMode']) {
111                if ($data['mime'] == 'xml') $data['xml_path'] = $conf['basedir']."_media/".$img;
112                switch ($xml) {
113                    case 'width':             $value = intval($value); if ($value > 319 && $value < 1921) $data['width'] = $value; break;
114                    case 'height':            $value = intval($value); if ($value > 239 && $value < 1081) $data['height'] = $value; break;
115                    case 'bgcolor':           if (preg_match("^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$",$value)) $data['bgcolor'] = $value; break; //TODO reason for preg_match warning?
116                    case 'smooth':            if ($value == 'yes') $data['smooth'] = $value; break;
117                    case 'disable_wheel':     if ($value == 'yes') $data['disable_wheel'] = $value; break;
118                    case 'allowFullScreen':   if ($value == 'true') $data['allowFullScreen'] = $value; break;
119                    case 'allowScriptAccess': if ($value == 'sameDomain') $data['allowFullScreen'] = $value;
120                                              if ($value == 'always') $data['allowFullScreen'] = $value; break;
121                }
122            }
123        }
124
125        // check markup parameters for consistency
126        // TODO
127
128        return $data;
129    }
130
131    public function render($mode, &$renderer, $data) {
132        global $conf;
133        if($mode != 'xhtml') return false;
134
135        $doc = "\n<div class=\"ryubin\" id=\"".$data['id']."\">\n  <script type=\"text/javascript\">\n";
136        $doc .= "    AC_FL_RunContent(\n    'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',\n    'pluginspage', 'http://www.macromedia.com/go/getflashplayer',\n";
137        $doc .= "    'width', '".$data['width']."',\n";
138        $doc .= "    'height', '".$data['height']."',\n";
139        $doc .= "    'bgcolor', '".$data['bgcolor']."',\n";
140        $doc .= "    'id','".$data['id']."',\n";
141        $doc .= "    'movie', '".$conf['basedir']."lib/plugins/ryubin/ryubin/swf/RyubinPanoPlayer5',\n";
142
143        // FlashVars
144        $doc .= "    'FlashVars','";
145        $doc .= "playmode=".$renderer->_xmlEntities($data['playmode']);
146        $doc .= "&internal_ctrl=no";
147        $doc .= "&xml_path=".$renderer->_xmlEntities($data['xml_path']);
148        if ($data['mime'] != 'xml')  $doc .= "&img_path=".$renderer->_xmlEntities($data['img_path']);
149        if ($data['fov'] != 60) $doc .= "&fov=".$data['fov'];
150        if ($data['yaw'] != 0) $doc .= "&yaw=".$data['yaw'];
151        if ($data['pitch'] != 0) $doc .= "&pitch=".$data['pitch'];
152        if ($data['view_mode'] != 1) $doc .= "&view_mode=".$data['view_mode'];
153        if ($data['hv_ratio'] != 2.0) $doc .= "&hv_ratio=".$data['hv_ratio'];
154        if ($data['smooth'] == 'yes') $doc .= "&smooth=".$data['smooth'];
155        if ($data['lock_nadirs'] == 'yes') $doc .= "&lock_nadirs=".$data['lock_nadirs'];
156        if ($data['lock_vertical'] == 'yes') $doc .= "&lock_vertical=".$data['lock_vertical'];
157        if ($data['lock_fov'] == 'yes') $doc .= "&lock_fov=".$data['lock_fov'];
158        if ($data['limit_vertical'] == 'yes') $doc .= "&limit_vertical=".$data['limit_vertical']."&top_limit=".$data['top_limit']."&bottom_limit=".$data['bottom_limit'];
159        if ($data['limit_horizontal'] == 'yes') $doc .= "&limit_horizontal=".$data['limit_horizontal']."&right_limit=".$data['right_limit']."&left_limit=".$data['left_limit'];
160        if ($data['proj_implicit'] == 'no') $doc .= "&proj_implicit=".$data['proj_implicit']."&elevation=".$data['elevation']."&depression=".$data['depression'];
161        if ($data['proj_type'] == 2) $doc .= "&proj_type=".$data['proj_type'];
162        if ($data['disable_wheel'] == 'yes') $doc .= "&disable_wheel=".$data['disable_wheel'];
163        $doc .= "',\n";
164
165        if ($data['allowFullScreen'] == 'true') $doc .= "    'allowFullScreen','true',\n";
166        $doc .= "    'allowScriptAccess','".$data['allowScriptAccess']."'\n    );\n";
167        $doc .= "  </script>\n</div>\n\n";
168
169        $renderer->doc .= $doc;
170
171        return true;
172    }
173}
174