1<?php
2/**
3 * DokuWiki Syntax Plugin Mp3Play
4 *
5 * Shows an arrow image which links to the top of the page.
6 * The image can be defined via the configuration manager.
7 *
8 * Syntax: {{mp3play>soundfile.mp3}}
9 *
10 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author  Michael Klier <chi@chimeric.de>
12 */
13
14// must be run within DokuWiki
15if(!defined('DOKU_INC')) die();
16
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18require_once(DOKU_PLUGIN.'syntax.php');
19
20if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
21
22/**
23 * All DokuWiki plugins to extend the parser/rendering mechanism
24 * need to inherit from this class
25 */
26class syntax_plugin_mp3play extends DokuWiki_Syntax_Plugin {
27
28    /**
29     * Syntax Type
30     *
31     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
32     */
33    function getType()  { return 'substition'; }
34    function getPType() { return 'block'; }
35    function getSort()  { return 309; }
36
37    /**
38     * Connect pattern to lexer
39     */
40    function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{mp3play>.*?\}\}',$mode,'plugin_mp3play'); }
41
42    /**
43     * Handle the match
44     */
45    function handle($match, $state, $pos, Doku_Handler $handler){
46        $match = substr($match, 10, -2);
47        $data = array();
48        if(file_exists(mediaFN($mp3))) {
49            $data = array();
50            $data['loop'] = 0;
51            $data['autostart'] = 0;
52
53            list($mp3, $params) = explode('?', $match);
54            $data['mp3'] = $mp3;
55
56            $params = explode('&', $params);
57            if($params) {
58                foreach($params as $param) {
59                    switch($param) {
60                        case 'loop':
61                            $data['loop'] = 1;
62                            break;
63                        case 'autostart':
64                            $data['autostart'] = 1;
65                            break;
66                    }
67                }
68            }
69
70            return $data;
71
72        } else {
73            return array();
74        }
75    }
76
77    /**
78     * Create output
79     */
80    function render($mode, Doku_Renderer $renderer, $data) {
81        global $ID;
82
83        if(empty($data['mp3'])) return;
84
85        if($mode == 'xhtml') {
86            $renderer->info['cache'] = false;
87
88            $params = '';
89            $color_cfg = DOKU_PLUGIN . 'mp3play/colors.conf';
90
91            if(@file_exists($color_cfg)) {
92
93                $colors = array();
94                $lines = @file($color_cfg);
95
96                foreach($lines as $line) {
97                    $line = preg_replace("/\ *#.*$/", '', $line);
98                    $line = trim($line);
99                    if(empty($line)) continue;
100                    list($key, $color) = explode('=', $line);
101                    $colors[trim($key)] = trim($color);
102                }
103
104                if(!empty($colors)) {
105                    foreach($colors as $key => $color) {
106                        $params .= $key . '=0x' . $color . '&amp;';
107                    }
108                }
109            }
110
111            $params .= ($data['loop']) ? 'loop=yes&amp;' : 'loop=no&amp;';
112            $params .= ($data['autostart']) ? 'autostart=yes&amp;' : 'autostart=no&amp;';
113
114            $renderer->doc .= '<div class="plugin_mp3play">' . DOKU_LF;
115            $renderer->doc .= '  <object type="application/x-shockwave-flash" data="' . DOKU_URL . 'lib/plugins/mp3play/player.swf" class="plugin_mp3play" height="24" width="290">' . DOKU_LF;
116            $renderer->doc .= '    <param name="movie" value="' . DOKU_URL . 'lib/plugins/mp3play/player.swf" />' . DOKU_LF;
117            $renderer->doc .= '    <param name="FlashVars" value="' . $params . 'soundFile=' . DOKU_URL . '/lib/exe/fetch.php?media=' . $data['mp3'] . '" />' . DOKU_LF;
118            $renderer->doc .= '    <param name="quality" value="high" />' . DOKU_LF;
119            $renderer->doc .= '    <param name="menu" value="false" />' . DOKU_LF;
120            $renderer->doc .= '    <param name="wmode" value="transparent" />' . DOKU_LF;
121            $renderer->doc .= '  </object>' . DOKU_LF;
122            $renderer->doc .= '</div>' . DOKU_LF;
123
124        }
125    }
126}
127// vim:ts=4:sw=4:et:enc=utf-8:
128