1<?php
2/**
3 * Plugin SMB: Makes file system links globally accessible, even through Firefox.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Enki <enki1337@gmail.com>
7 */
8
9// must be run within DokuWiki
10if(!defined('DOKU_INC')) die('meh.');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_smblink extends DokuWiki_Syntax_Plugin {
19
20    /**
21     * return some info
22     */
23    function getInfo(){
24        return array(
25            'author' => 'Enki',
26            'email'  => 'enki1337@gmail.com',
27            'date'   => '2009-02-09',
28            'name'   => 'SMB Plugin',
29            'desc'   => 'Makes filesystem links globally accessible, even through firefox.',
30            'url'    => 'http://www.dokuwiki.org/',
31        );
32    }
33
34    function getType(){ return 'substition'; }
35
36    function getAllowedTypes() { return array(); }
37
38    function getSort(){ return 295; }
39
40    function connectTo($mode) {
41        //Add the smblink pattern to the lexer. Pattern will match [[\ASDF]]
42        //$this->Lexer->addSpecialPattern("\[\[(?i)smb://.+?\]\]",$mode,'plugin_smblink');
43        $this->Lexer->addSpecialPattern("\[\[\\\\.+?\]\]",$mode,'plugin_smblink');
44    }
45
46    /**
47     * Handle the match
48     */
49    function handle($match, $state, $pos, &$handler){
50        //Break the link out of [[ ]] and split it into link and description if there is a '|'
51        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
52        $link = preg_split('/\|/u',$link,2);
53        return array($state, $link);
54    }
55
56    /**
57     * Create output
58     */
59    function render($mode, &$renderer, $data) {
60        if ($mode != 'xhtml') return;
61        global $conf, $lang;
62        list($state, $matchdata) = $data;
63        list($url, $name) = $matchdata;
64        //If no name is given, use the last bit of the url
65        if (strlen($name) == 0) {
66            $urlbits = preg_split('/(\\\|\\/)/u',$url);
67            $name = $urlbits[count($urlbits)-1];
68        }
69        //simple setup
70        $link['target'] = $conf['target']['windows'];
71        $link['pre']    = '';
72        $link['suf']   = '';
73        $link['style']  = '';
74        $link['name'] = $renderer->_getLinkTitle($name, $url, $isImage);
75        $link['title'] = $renderer->_xmlEntities($url);
76        if ( !$isImage ) {
77            $link['class'] = 'windows';
78        } else {
79            $link['class'] = 'media';
80        }
81        //Format the link for smb (Linux or Win-Firefox)
82        //Just replace all \ with / to get something like smb://host/path/to/file
83        $smburl='smb:' . str_replace('\\', '/', $url);
84        //If we're using linux, then smb:// protocol works fine.
85        if (strstr($_SERVER['HTTP_USER_AGENT'], 'Linux')) {
86            $url = $smburl;
87        } else {
88            //If we're not on linux, we might be using IE, so...
89            //Replace the \\ with the file:/// protocol and put the \\ back in
90            $url = str_replace('\\\\', 'file:///\\\\', $url);
91            //Use javascript to change the link in Firefox to the smb url.
92            $link['more'] =
93                'onclick="if(document.all == null){' .
94                    "parent.location='".$smburl."';" .
95                '}" ' .
96                'onkeypress="if(document.all == null){' .
97                    "parent.location='".$smburl."';" .
98                '}"';
99        }
100
101        $link['url'] = $url;
102
103        //output formatted
104        $renderer->doc .= $renderer->_formatLink($link);
105        return;
106    }
107}
108?>
109