<?php
/**
 * @author     Thomas Meyer
 * @licence    http://creativecommons.org/licenses/by-nc-sa/2.5/
 * @package    yana
 * @subpackage plugins
 */

/**
 * @access     public
 * @package    yana
 * @subpackage plugins
 */
class plugin_user extends plugin
{

    /**
     * @access  private
     * @var     sessionManager
     */
    var $sessionManager = null;
    /**
     * @access  private
     * @var     string
     */
    var $user_name = "";

    /**
     * constructor
     *
     * @param  string  $name
     */
    function plugin_user($name)
    {
        global $YANA;
        settype($name,"string");
        $this->name = $name;
        $this->sessionManager = new sessionManager();

        if (!empty($_SESSION['user_name'])) {
            $this->user_name = $_SESSION['user_name'];
            $YANA->setVar("SESSION_USER_ID", $this->user_name);
        } else {
            $YANA->unsetVar("SESSION_USER_ID", $this->user_name);
        }
        $YANA->setVar("SESSION_ID", session_id());
        $YANA->setVar("SESSION_NAME", session_name());
    }

    /**
     * Default event handler
     *
     * returns bool(true) on success and bool(false) on error
     *
     * @access  public
     * @return  bool
     * @name    plugin_spamprotect::_default()
     * @param   string  $event  name of the called event in lower-case
     * @param   array   $ARGS   array of arguments passed to the function
     */
    function _default($event, $ARGS) 
    {
        settype($event, "string");
        settype($ARGS, "array");

        global $YANA;
        $YANA->language->read("user");
        if (!empty($ARGS['id'])) {
            $id = $ARGS['id'];
        } else {
            $id = "";
        }

        if ($this->_checkPermission($this->user_name,$id,$event)) {
            # access granted
            $YANA->setVar("PERMISSION",$this->sessionManager->getUserPermission($this->user_name,$id));
            return true;
        } else {
            return false;
        }
    }

    /**
     * Set new password
     *
     * @access  public
     * @param   array    $ARGS
     * @return  bool
     */
    function set_pwd($ARGS)
    {
        settype($ARGS, "array");
        global $YANA;

        if (isset($ARGS['pwt_neu']) && isset($ARGS['pwt_wdh']) && $ARGS['pwt_neu']===$ARGS['pwt_wdh'] && is_string($ARGS['pwt_neu']) && is_string($ARGS['pwt_alt'])) {

            if (!$this->sessionManager->checkLogin($this->user_name,$ARGS['pwt_alt'])) {
                $YANA->message("error", "LOGIN_UNGUELTIG");
                return false;
            } else if (!$this->sessionManager->changePwd($this->user_name,$ARGS['pwt_neu'])) {
                $YANA->message("error", "500", "index");
                return false;
            } else {
                $YANA->message("OK", "200", "index");
                return true;
            }
        } else {
            return false;
        }

    }

    /**
     * Logout
     *
     * @access  public
     * @param   array    $ARGS
     * @return  bool
     */
    function logout($ARGS)
    {
        $this->sessionManager->logout();
        return true;
    }

    /**
     * Check if login data is correct
     *
     * @access  public
     * @param   array    $ARGS
     * @return  bool
     */
    function check_login($ARGS)
    {
        if (!is_string($ARGS['nutzername']) || !is_string($ARGS['passwort'])) {
            return false;
        } else {
            $nutzername = $ARGS['nutzername'];
            $passwort   = $ARGS['passwort'];
        }

        global $YANA;
        if ($this->sessionManager->login($nutzername,$passwort)) {
            /* update user information */
            $USER = $this->sessionManager->getUser($nutzername);
            settype($USER['COUNT_LOGINS'], "integer");
            $this->sessionManager->insertUser($nutzername, array("LAST_LOGIN" => time(), "COUNT_LOGINS" => $USER['COUNT_LOGINS'] +1));
            /* route next action */
            if (isset($_SESSION['on_login_goto'])) {
                $goto = $_SESSION['on_login_goto'];
                unset($_SESSION['on_login_goto']);
                if (!empty($goto)) {
                    global $YANA;     
                    $YANA->message('OK', 'LOGIN_OKAY', $goto);
                }
            }
            return true;
        } else {
            if (isset($_SESSION['on_login_goto'])) {
                unset($_SESSION['on_login_goto']);
            }
            return false;
        }
    }
    

    /**
     * _checkPermission
     *
     * @access  private
     * @param   string    $user_name
     * @param   string    $id
     * @param   string    $event
     * @return  boolean
     * @ignore
     */ 
    function _checkPermission($user_name, $id, $event)
    {

        settype($user_name, "string");
        settype($id, "string");
        settype($event, "string");
        global $YANA;
        $user_name = strtoupper($user_name);

        $required_perm = $YANA->plugins->getVar("*", "INTERFACE.$event.PERMISSION");
        $user_perm     = $this->sessionManager->getUserPermission($user_name,$id);
        if (is_null($required_perm)) {
            $required_perm = $YANA->getDefault('EVENT.PERMISSION');
        }
        settype($required_perm, "integer");
        settype($user_perm, "integer");

        if ($required_perm < 0) {
            $YANA->report(new error("404"));
            return false;
        }

        if ($required_perm == 0 && $user_perm > -1) { // if no permission is required, then don't care for it
            return true;
        } else {
            if (!$this->sessionManager->isLoggedIn()) {
                  $meldung = "403";
                  $next_action = "login";
            } else {
                if ($required_perm <= $user_perm) {
                    return true;
                } else {
                    $meldung = "UNGENUEGENDE_RECHTE";
                    $next_action = "";
                }
            }
            $_SESSION['on_login_goto'] = $event;
            $YANA->message("alert", $meldung, $next_action);
            return false;
        }
    }

}

// © 2006, Thomas Meyer www.All-Community.de
// Copyright Hinweis nicht entfernen!
?>