1<?php
2/**
3 * DokuWiki Plugin htvid (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Joel Carnes <jjoelc@gmail.com>
7 *
8 * Based on the html5video plugin Jason van Gumster, which had
9 * Parts borrowed from the videogg plugin written by Ludovic Kiefer,
10 * which is based on Christophe Benz' Dailymotion plugin, which, in turn,
11 * is based on Ikuo Obataya's Youtube plugin. Whew...
12 *
13 * Supports mp4 and ogv videos (with flash fallback)
14 */
15
16// must be run within Dokuwiki
17if (!defined('DOKU_INC')) die();
18
19if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
20if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
21if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
22
23require_once DOKU_PLUGIN.'syntax.php';
24
25class syntax_plugin_htvid_video extends DokuWiki_Syntax_Plugin {
26
27    public function getType() {
28        return 'substition';
29    }
30
31    public function getPType() {
32        return 'block';
33    }
34
35    public function getSort() {
36        return 159;
37    }
38
39    public function connectTo($mode) {
40        // recognizes the {{htvid> tag. Does not do any checking for parameters
41        $this->Lexer->addSpecialPattern('{{htvid>.*?}}',$mode,'plugin_htvid_video');
42    }
43
44    public function handle($match, $state, $pos, &$handler){
45        $params = substr($match, strlen('{{htvid>'), - strlen('}}')); //Strip markup
46        if(strpos($params, ' ') === 0) { // Space as first character after 'htvid>'
47            if(substr_compare($params, ' ', -1, 1) === 0) { // Space at front and back = centered
48                $params = trim($params);
49                $params = 'center|' . $params;
50            }
51            else { // Only space at front = right-aligned
52                $params = trim($params);
53                $params = 'right|' . $params;
54            }
55        }
56        elseif(substr_compare($params, ' ', -1, 1) === 0) { // Space only as last character = left-aligned
57            $params = trim($params);
58            $params = 'left|' . $params;
59        }
60        else { // No space padding = inline
61            $params = 'inline|' . $params;
62        }
63        return array(state, explode('|', $params));
64    }
65
66    public function render($mode, &$renderer, $data) {
67        if($mode != 'xhtml') return false;
68
69        list($state, $params) = $data;
70        list($video_align, $video_url1, $video_url2, $video_size, $video_attr) = $params;
71
72        if($video_align == "center") {
73            $align = "margin: 0 auto;";
74        }
75        elseif($video_align == "left") {
76            $align = "float: left;";
77        }
78        elseif($video_align == "right") {
79            $align = "float: right;";
80        }
81        else { // Inline
82            $align = "";
83        }
84
85		if(substr($video_url1, -3) != 'ogv' && substr($video_url1, -3) != 'mp4') {
86          $renderer->doc .= "Error: The video must be in ogv, or mp4 format. Bad file is:<br />" . $video_url1;
87            return false;
88        }
89
90		if(substr($video_url2, -3) != 'ogv' && substr($video_url2, -3) != 'mp4') {
91          $renderer->doc .= "Error: The video must be in ogv, or mp4 format. Bad file is:<br />" . $video_url2;
92            return false;
93        }
94
95//	jw player doesn't seem to like the 'fetch.php=' links that the ml() function returns
96//   so can't use them, but plugin breaks without these calls...
97       if(!substr_count($video_url1, '/')) {
98           $video_url1 = ml($video_url1,true,true);
99       }
100
101		if(!substr_count($video_url2, '/')) {
102            $video_url2 = ml($video_url2,$abs=true);
103        }
104
105        //set default video size if none given
106        if(is_null($video_size) or !substr_count($video_size, 'x')) {
107            $width  = 640;
108            $height = 360;
109        }
110        else {
111            $obj_dimensions = explode('x', $video_size);
112            $width  = $obj_dimensions[0];
113            $height = $obj_dimensions[1];
114        }
115
116       //see if any attributes were given, set them if they exist...
117       if(is_null($video_attr)) {
118            $attr = "";
119        }
120        else {
121            $arr_attr = explode(',', $video_attr);
122            if(count($arr_attr) == 1) {
123                if($arr_attr[0] == "loop") {
124                    $attr = 'loop="loop"';
125                }
126                elseif($arr_attr[0] == "autoplay") {
127                    $attr = 'autoplay="autoplay"';
128                }
129            }
130            elseif(count($arr_attr) == 2) {
131                if($arr_attr[0] != $arr_attr[1]) {
132                    $attr = 'loop="loop" autoplay="autoplay"';
133                }
134                else {
135                    $renderer->doc .= "Error: Duplicate parameters.<br />";
136                    return false;
137                }
138            }
139            else {
140                $renderer->doc .= "Error: Wrong number of parameters.<br />";
141                return false;
142            }
143        }
144
145//now finally the code to render...
146		$obj.= '<video width="' . $width . '" height="' . $height . '" controls="controls"' . $attr . '>';
147		$obj.= '<source src="' . $video_url1 . '">' ;
148		$obj.= '<source src="' . $video_url2 . '">' ;
149		$obj.= '<object type="application/x-shockwave-flash" data="/lib/plugins/htvid/player/player.swf" width="' . $width . '" height="' . $height . '">';
150		$obj.= '<!--[if IE]><param name="movie" value="/lib/plugins/flashplayer/player/player.swf" /><![endif] -->';
151		$obj.= '"It appears you do not have Flash installed or your browser does not support it. You may also <a href="' . $video_url1 . '">download this video</a> to watch it offline"';
152		$obj.= '<param name="allowfullscreen" value="true">';
153		$obj.= '<param name="flashvars" value="file=' . $video_url1 . '">';
154		$obj.= '</object>';
155		$obj.= '</video>';
156        if($align != "") {
157            $obj = '<div style="width: ' . $width . 'px; ' . $align . '">' . $obj . '</div>';
158        }
159
160        $renderer->doc .= $obj;
161        return true;
162    }
163    private function _getAlts($filename) {
164        return false;
165    }
166}