<?php
/**
 * Plugin SMB: Makes file system links globally accessible, even through Firefox.
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Enki <enki1337@gmail.com>
 */

// must be run within DokuWiki
if(!defined('DOKU_INC')) die('meh.');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_smblink extends DokuWiki_Syntax_Plugin {

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Enki',
            'email'  => 'enki1337@gmail.com',
            'date'   => '2009-02-09',
            'name'   => 'SMB Plugin',
            'desc'   => 'Makes filesystem links globally accessible, even through firefox.',
            'url'    => 'http://www.dokuwiki.org/',
        );
    }

    function getType(){ return 'substition'; }

    function getAllowedTypes() { return array(); }

    function getSort(){ return 295; }

    function connectTo($mode) {
        //Add the smblink pattern to the lexer. Pattern will match [[\ASDF]]
        //$this->Lexer->addSpecialPattern("\[\[(?i)smb://.+?\]\]",$mode,'plugin_smblink');
        $this->Lexer->addSpecialPattern("\[\[\\\\.+?\]\]",$mode,'plugin_smblink');
    }

    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        //Break the link out of [[ ]] and split it into link and description if there is a '|'
        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
        $link = preg_split('/\|/u',$link,2);
        return array($state, $link);
    }

    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if ($mode != 'xhtml') return;
        global $conf, $lang;
        list($state, $matchdata) = $data;
        list($url, $name) = $matchdata;
        //If no name is given, use the last bit of the url
        if (strlen($name) == 0) {
            $urlbits = preg_split('/(\\\|\\/)/u',$url);
            $name = $urlbits[count($urlbits)-1];
        }
        //simple setup
        $link['target'] = $conf['target']['windows'];
        $link['pre']    = '';
        $link['suf']   = '';
        $link['style']  = '';
        $link['name'] = $renderer->_getLinkTitle($name, $url, $isImage);
        $link['title'] = $renderer->_xmlEntities($url);
        if ( !$isImage ) {
            $link['class'] = 'windows';
        } else {
            $link['class'] = 'media';
        }
        //Format the link for smb (Linux or Win-Firefox)
        //Just replace all \ with / to get something like smb://host/path/to/file
        $smburl='smb:' . str_replace('\\', '/', $url);
        //If we're using linux, then smb:// protocol works fine.
        if (strstr($_SERVER['HTTP_USER_AGENT'], 'Linux')) {
            $url = $smburl;
        } else {
            //If we're not on linux, we might be using IE, so...
            //Replace the \\ with the file:/// protocol and put the \\ back in
            $url = str_replace('\\\\', 'file:///\\\\', $url);
            //Use javascript to change the link in Firefox to the smb url.
            $link['more'] =
                'onclick="if(document.all == null){' .
                    "parent.location='".$smburl."';" .
                '}" ' .
                'onkeypress="if(document.all == null){' .
                    "parent.location='".$smburl."';" .
                '}"';
        }

        $link['url'] = $url;

        //output formatted
        $renderer->doc .= $renderer->_formatLink($link);
        return;
    }
}
?>
