1<?php
2/**
3 * Plugin FlashPlayer: Flash Media Player in the wiki
4 *
5 * http://arnowelzel.de/wiki/misc/flashplayer
6 *
7 * @license    BY-NC-SA (http://creativecommons.org/licenses/by-nc-sa/3.0/deed)
8 * @author     Arno Welzel (based on the work of Sam Hall)
9 */
10
11// must be run within Dokuwiki
12if(!defined('DOKU_INC')) die();
13
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once(DOKU_PLUGIN.'syntax.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_flashplayer extends DokuWiki_Syntax_Plugin {
22	var $uid='';
23	var $width=0;
24	var $height=0;
25	var $position=0;
26
27    /**
28     * return some info
29     */
30    function getInfo(){
31        return array(
32            'base'   => 'flashplayer',
33            'author' => 'Arno Welzel',
34            'email'  => 'himself@arnowelzel.de',
35            'date'   => '2012-05-11',
36            'name'   => 'FlashPlayer Plugin',
37            'desc'   => 'Embeds JW Flash Player 5.8.2011 in the page, based on the work of Sam Hall',
38            'url'    => 'http://arnowelzel.de/wiki/misc/flashplayer',
39        );
40    }
41
42    function getType(){ return 'formatting'; }
43    function getSort(){ return 158; }
44    function connectTo($mode) { $this->Lexer->addEntryPattern('<flashplayer.*?>(?=.*?</flashplayer>)',$mode,'plugin_flashplayer'); }
45    function postConnect() { $this->Lexer->addExitPattern('</flashplayer>','plugin_flashplayer'); }
46
47
48    /**
49     * Handle the match
50     */
51    function handle($match, $state, $pos, &$handler){
52        switch ($state) {
53            case DOKU_LEXER_ENTER :
54                $attributes = strtolower(substr($match, 10, -1));
55                $width = $this->_getNumericalAttribute($attributes, "width", "340");
56                $height = $this->_getNumericalAttribute($attributes, "height", "20");
57                $position = $this->_getNumericalAttribute($attributes, "position", "0");
58				$uid = md5(uniqid(rand(), true));
59                return array($state, array($width, $height, $position, $uid));
60            case DOKU_LEXER_UNMATCHED :  return array($state, $match);
61            case DOKU_LEXER_EXIT :       return array($state, '');
62        }
63        return array();
64    }
65
66    /**
67     * Create output
68     */
69    function render($mode, &$renderer, $data) {
70        if($mode == 'xhtml'){
71            list($state, $vars) = $data;
72            switch ($state) {
73                case DOKU_LEXER_ENTER :
74					list($this->width, $this->height, $this->position, $this->uid) = $vars;
75					switch($this->position)
76					{
77					case 1:
78						$renderer->doc .= '<p style="text-align:center">';
79						break;
80					case 2:
81						$renderer->doc .= '<p style="text-align:right">';
82						break;
83					}
84
85					$renderer->doc .= '<object id="flv'.$this->uid.'"';
86					$renderer->doc .= ' type="application/x-shockwave-flash"';
87					$renderer->doc .= ' data="'.DOKU_BASE.'lib/plugins/flashplayer/player/player.swf"';
88					$renderer->doc .= ' width="'.$this->width.'"';
89					$renderer->doc .= ' height="'.$this->height.'"';
90					$renderer->doc .= '>';
91					$renderer->doc .= '<param name="movie" value="'.DOKU_BASE.'lib/plugins/flashplayer/player/player.swf" />';
92					$renderer->doc .= '('.$this->getLang('getflash').')';
93					$renderer->doc .= '<param name="allowfullscreen" value="true" />';
94					// $renderer->doc .= '<param name="allowscriptaccess" value="always" />';
95					$renderer->doc .= '<param name="flashvars" value="';
96                    break;
97
98                case DOKU_LEXER_UNMATCHED :
99					$renderer->doc .= $renderer->_xmlEntities($vars);
100					break;
101
102                case DOKU_LEXER_EXIT :
103					$renderer->doc .= '" />';
104					$renderer->doc .= '</object>';
105
106					switch($this->position)
107					{
108					case 1:
109					case 2:
110						$renderer->doc .= '</p>';
111						break;
112					}
113					break;
114            }
115            return true;
116        }
117        return false;
118    }
119
120    function _getNumericalAttribute($attributeString, $attribute, $default){
121        $retVal = $default;
122        $pos = strpos($attributeString, $attribute."=");
123        if ($pos === false) {
124            //Maybe they have a space...
125            $pos = strpos($attributeString, $attribute." ");
126        }
127        if ($pos > 0) {
128            $pos = $pos + strlen($attribute);
129            $value = substr($attributeString,$pos);
130
131            //replace '=' and quote signs with null and trim leading spaces
132            $value = str_replace("=","",$value);
133            $value = str_replace("'","",$value);
134            $value = str_replace('"','',$value);
135            $value = ltrim($value);
136
137            //grab the text before the next space
138            $pos = strpos($value, " ");
139            if ($pos > 0) {
140                $value = substr($value,0,$pos);
141            }
142
143            //validate that it's numerical
144            if (preg_match("^[0-9]+$^", $value)) $retVal = $value;
145        }
146        return $retVal;
147    }
148}
149
150//Setup VIM: ex: et ts=4 enc=utf-8 :