<?php
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_distribute_syntax extends DokuWiki_Syntax_Plugin {
    var $options = array();
    function getInfo(){
        return array(
            'author' => 'Ahmet Sacan',
            'email'  => 'ahmet@ceng.metu.edu.tr',
            'date'   => '2007-02-07',
            'name'   => 'Distribute plugin',
            'desc'   => 'helps distribute your plugins',
            'url'    => 'http://www.ceng.metu.edu.tr/~ahmet',
        );
    }

    function getType(){ return 'substition'; }
    function getSort(){ return 158; }

    function connectTo($mode) {
	$this->Lexer->addSpecialPattern('\x5cdistribute\x7b[^\x7d]*\x7d',$mode,'plugin_distribute_syntax');
    }
    function handle($match, $state, $pos, &$handler){
	$ret = (object) array('match'=>$match);
	switch ($state) {
         case DOKU_LEXER_SPECIAL:
	 if(preg_match('/^\x5cdistribute\x7b([^\x7d]+)\x7d$/', $match, $ms)){
	    $ret->command = 'distribute';
            $ret->arg = $ms[1];
	 }
	}
        return array($state,$ret);
    }
    ///////////////////////////////////////////////////////////////
    function render($mode, &$renderer, $data) {
        global $ID;
	require_once dirname(__FILE__).'/../helper.funcs.util.php';
        if($mode == 'xhtml'){
            list($state, $match) = $data;
	    $this->options['match'] .= $match->match;
            switch ($state) {
	      case DOKU_LEXER_SPECIAL:
	          if(!in_array($match->arg, explode(',',$this->getConf('plugins'))))
		    $renderer->doc .= "[[distribution of $match->arg plugin not allowed in configuration]]";
		else{
		    $lastupdate = my_filemtime(DOKU_PLUGIN.$match->arg, true);
		    $renderer->doc .= "<a href='$_SERVER[PHP_SELF]?do=distribute&plugin=$match->arg&id=$ID' class='media mediafile mf_zip'>$match->arg".'_'.date('Ymd',$lastupdate).".zip</a>";
		}
            }
            return true;
        }//xhtml
        return false;
    }//render()
}
