<?php
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'admin.php');

/**
 * All DokuWiki plugins to extend the admin function
 * need to inherit from this class
 */
class admin_plugin_unameban extends DokuWiki_Admin_Plugin {

    /**
     * access for managers
     */
    function forAdminOnly(){
        return false;
    }

    /**
     * return sort order for position in admin menu
     */
    function getMenuSort() {
        return 41;
    }

    /**
     * handle user request
     */
    function handle() {
        global $conf;
        if($_REQUEST['banusername']){
            $newban = trim($_REQUEST['banusername'])."\t".time()."\t".$_SERVER['REMOTE_USER'];
            $cause  = trim(preg_replace('/[\n\r\t]+/','',$_REQUEST['cause']));
            $newban .= "\t".$cause."\n";
            io_savefile($conf['cachedir'].'/unamebanplugin.txt',$newban,true);
        }

        if(is_array($_REQUEST['delusername'])){
            $del = trim(array_shift(array_keys($_REQUEST['delusername'])));
            $del = preg_quote($del,'/');
            $new = array();
            $bans = @file($conf['cachedir'].'/unamebanplugin.txt');
            if(is_array($bans)) foreach($bans as $ban){
                if(!preg_match('/^'.$del.'\t/',$ban)) $new[] = $ban;
            }
            io_savefile($conf['cachedir'].'/unamebanplugin.txt',join('',$new));
        }
    }

    /**
     * output appropriate html
     */
    function html() {
        global $conf;

        echo $this->locale_xhtml('intro');

        echo '<form method="post" action="">';
        echo '<table class="inline" width="100%">';
        echo '<tr>';
        echo '<th>'.$this->getLang('username').'</th>';
        echo '<th>'.$this->getLang('date').'</th>';
        echo '<th>'.$this->getLang('by').'</th>';
        echo '<th>'.$this->getLang('cause').'</th>';
        echo '<th>'.$this->getLang('del').'</th>';
        echo '</tr>';
        $bans = @file($conf['cachedir'].'/unamebanplugin.txt');
        if(is_array($bans)) foreach($bans as $ban){
            $fields = explode("\t",$ban);
            echo '<tr>';
            echo '<td>'.hsc($fields[0]).'</td>';
            echo '<td>'.strftime($conf['dformat'],$fields[1]).'</td>';
            echo '<td>'.hsc($fields[2]).'</td>';
            echo '<td>'.hsc($fields[3]).'</td>';
            echo '<td><input type="submit" name="delusername['.$fields[0].']" value="'.$this->getLang('del').'" class="button" /></td>';
            echo '</tr>';
        }
        echo '<tr>';
        echo '<th colspan="6">';
        echo '<div>'.$this->getLang('newban').':</div>';
        echo '<label for="plg__unameban_username">'.$this->getLang('username').':</label>';
        echo '<input type="text" name="banusername" id="plg__unameban_username" class="edit" /> ';
        echo '<label for="plg__unameban_cause">'.$this->getLang('cause').':</label>';
        echo '<input type="text" name="cause" id="plg__unameban_cause" class="edit" /> ';
        echo '<input type="submit" class="button" value="'.$this->getLang('ban').'" />';
        echo '</th>';
        echo '</tr>';
        echo '</table>';
        echo '</form>';

    }


}
