<?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 user_admin extends serializeableObject
{

    /* private sessionManager */ var $sessionManager;
    /* private String */         var $user_name;

    /* constructor */ function user_admin() 
    {
        @session_start();
        global $YANA;
        if (isset($_SESSION['user_name'])) $this->user_name = $_SESSION['user_name'];
        $this->sessionManager = new sessionManager();
        $YANA->setVar("SESSION_ID", session_id());
        $YANA->setVar("SESSION_NAME", session_name());
    }

    /* public boolean */ function isEmpty() 
    {
        return $this->sessionManager->isEmpty();
    }

    /* public boolean */ function get_user_configuration()
    {
       global $YANA;
       $YANA->setVar("BESCHREIBUNG", $YANA->getVar("LANGUAGE.DESCR_USER_CONFIGURATION"));

       $USERLIST   = $this->sessionManager->getUserList();
       $USERGROUPS = $this->sessionManager->getUserGroups();
       $YANA->setVar("USERLIST",$USERLIST);
       $YANA->setVar("USERGROUPS",$USERGROUPS);
       return true;
    }

    /* public boolean */ function get_user(/* String */ $user_id, /* Boolean */ $original = false) 
    {
       settype($user_id,"string");
       settype($original,"boolean");
       global $YANA;

       $USER = $this->sessionManager->getUser($user_id);

       if (!$USER['ID'] || !$USER['ACTIVE']) {
           $YANA->message("error", "USER_UNKNOWN", "index");
           return false;
       } else {
           $YANA->setVar("USER",$USER);
           return true;
       }
    }

    /* public boolean */ function get_user_edit(/* String */ $user) 
    {
       settype($user,"string");
       global $YANA;
       $YANA->setVar("BESCHREIBUNG", $YANA->getVar("LANGUAGE.DESCR_USER_EDIT"));

       $USER = $this->sessionManager->getUser($user);
       
       if (!$USER['ID']) {
           $YANA->message("error", "USER_UNKNOWN", "index");
           return false;
       } else {
           if (!empty($USER['MAIL'])) {
               $USER['MAIL'] = DisplayUtility::scanForAt($USER['MAIL']);
           }
           $USERGROUPS = $this->sessionManager->getUserGroups();
           $YANA->setVar("USER",$USER);
           $YANA->setVar("USERGROUPS",$USERGROUPS);
           return true;
       }
    }

    /* public boolean */ function set_user_edit(/* String */ $user_name, /* Hashtable */ $INPUT) 
    {
       settype($user_name,"string");
       settype($INPUT,"array");
       global $YANA;

       $INPUT = $this->makeUser(mergeArrays($this->sessionManager->getUser($user_name), $INPUT), $user_name);

       if (!$this->sessionManager->insertUser($user_name, $INPUT)) {
           $YANA->message("error", "500", "index");
           return false;
       } else {
           $YANA->message("OK", "200", "index");
           return false;
       }
    }

    /* public boolean */ function set_profile_edit(/* Hashtable */ $INPUT) 
    {
       settype($INPUT,"array");
       global $YANA;

       // <-- Make user

               $INPUT = changeCaseArray($INPUT,CASE_UPPER);

               foreach ($INPUT as $KEY=>$element) {
                   settype($element, "string");
                   $element = strip_tags($element);
                   $element = preg_replace("/\n/","[br]",$element);
                   $element = preg_replace("/\s/"," ",$element);
                   $INPUT[$KEY] = $element;
               } /* end for */

               unset($INPUT["ACTIVE"]);
               unset($INPUT["PHOTO_ACTIVE"]);
               unset($INPUT["PERM_GROUP"]);
               unset($INPUT["PERM_ID"]);
               unset($INPUT["PASSWORT"]);
               unset($INPUT["PASSWORD"]);
               unset($INPUT["TYPE"]);
               unset($INPUT["PERMISSION"]);
               unset($INPUT["ACTION"]);
               unset($INPUT[strtoupper(session_name())]);
               unset($INPUT["ID"]);
       
               $INPUT["MODIFIED"] = time();

       // <-- Make user

       $USER = $this->sessionManager->getUser($this->user_name);
       
       if (!empty($INPUT['PHOTO']) && $INPUT['PHOTO'] != $USER['PHOTO']) {
           $INPUT['PHOTO_ACTIVE'] = false;
       }

       return $this->sessionManager->insertUser($this->user_name, $INPUT);
    }

    /* public boolean */ function set_user_delete(/* String */ $user_name) 
    {
       settype($user_name,"string");
       global $YANA;

       if (strtoupper($this->user_name) == strtoupper($user_name)) {
           $YANA->message("alert", "USER_DELETE_SELF", "index");
           return false;
       } else if (strtoupper($user_name) == "ADMINISTRATOR") {
           $YANA->message("alert", "UNGUELTIGE_EINGABE", "index");
           return false;
       } else if (!$this->sessionManager->removeUser($user_name)) {
           $YANA->message("error", "500", "index");
           return false;
       } else {
           $YANA->message("OK", "200", "index");
           return false;
       }
    }

    /* public boolean */ function get_user_delete(/* String */ $user_name) 
    {
       settype($user_name,"string");
       global $YANA;

       if (strtoupper($this->user_name) == strtoupper($user_name)) {
           $YANA->message("alert", "USER_DELETE_SELF", "index");
           return false;
       } else if (strtoupper($user_name) == "ADMINISTRATOR") {
           $YANA->message("alert", "UNGUELTIGE_EINGABE", "index");
           return false;
       } else {
           return true;
       }
    }
    
    /* public boolean */ function set_user_pwd(/* String */ $user_name) 
    {
       settype($user_name,"string");
       global $YANA;
       $YANA->language->read("user");

       $pwt = $this->sessionManager->setPwd($user_name);

       if (!$pwt) {
           return false;
       } else {
           $user = $this->sessionManager->getUser($user_name);
           $YANA->setVar('PASSWORT', $pwt);
           $YANA->setVar('NAME', $user['ID']);
           if ($user['MAIL']) user_admin::send_mail($user['MAIL'],$YANA->skin->getFile("USER_PASSWORD_MAIL"));
           return true;
       }
    }

    /* public boolean */ function get_user_new(/* String */ $user_name) 
    {
       settype($user_name,"string");
       $user_name = strtoupper($user_name);
       global $YANA;

       if (!$user_name) {
           $YANA->message("alert", "UNGUELTIGE_EINGABE", "index");
           return false;
       } else if ($this->userExists($user_name)) {
           $YANA->message("alert", "USER_ALREADY_EXISTS", "index");
           return false;
       } else {
           $USERGROUPS = $this->sessionManager->getUserGroups();
           $YANA->setVar("USERGROUPS",$USERGROUPS);
           $USER['ID']     = $user_name;
           $USER['ACTIVE'] = true;
           $YANA->setVar("USER",$USER);
           return true;
       }
    }

    /* public boolean */ function set_user_new(/* String */ $user_name, /* Hashtable */ $INPUT) 
    {
       settype($user_name,"string");
       settype($INPUT,"array");
       global $YANA;

       if (!$user_name) {
           $YANA->message("alert", "UNGUELTIGE_EINGABE", "index");
           return false;
       } else if ($this->userExists($user_name)) {
           $YANA->message("alert", "USER_ALREADY_EXISTS", "index");
           return false;
       } else {
           $INPUT = $this->makeUser($INPUT, $user_name);
           $INPUT['INSERTED'] = time();

           if (!$this->sessionManager->insertUser($user_name, $INPUT)) {
               $YANA->message("error", "500", "index");
               return false;
           } else {
               return $this->set_user_pwd($user_name);
           }
       }
    }

    /* public boolean */ function user_authentification(/* String */ $user_name, /* Hashtable */ $INPUT) 
    {
       settype($user_name,"string");
       settype($INPUT,"array");
       global $YANA;

       $INPUT['ACTIVE'] = true;

       if (!$user_name) {
           $YANA->message("alert", "UNGUELTIGE_EINGABE", "");
           return false;
       } else if ($this->userExists($user_name)) {
           $YANA->message("alert", "USER_ALREADY_EXISTS", "");
           return false;
       } else {
           $INPUT = $this->makeUser($INPUT, $user_name);
           $INPUT['PASSWORT'] = 'UNINITIALIZED';

           if (!$this->sessionManager->insertUser($user_name, $INPUT)) {
               $YANA->message("error", "500", "");
               return false;
           } else {
               return $this->set_user_pwd($user_name);
           }
       }
    }

    /* public boolean */ function userExists(/* String */ $username) 
    {
       settype($username, "string");
       return $this->sessionManager->userExists($username);
    }

    /* public Hashtable */ function getUserList() 
    {
        return $this->sessionManager->getUserList();
    }

    /* private Hashtable */ function makeUser(/* Hashtable */ $INPUT, /* String */ $user_name) 
    {
       settype($INPUT, "array");

       $REF = array();
       $INPUT = changeCaseArray($INPUT,CASE_UPPER);

       foreach ($INPUT as $KEY=>$element) {
           @settype($element, "string");
           $INPUT[$KEY] = strip_tags($element);
           if (preg_match("/^PERMISSION\/([\w]+)$/",$KEY, $MATCH)) {
               if ($element) {
                   $REF['PERMISSION'][$MATCH[1]] = $element;
               } /* end if */
           } /* end if */
       } /* end for */

       settype($INPUT['ACTIVE'],"boolean");
       settype($INPUT['PHOTO_ACTIVE'],"boolean");

       if (isset($INPUT['PERM_GROUP']) && $INPUT['PERM_ID']) $REF['PERMISSION'][$INPUT['PERM_ID']] = $INPUT['PERM_GROUP'];
       if (isset($INPUT['MAIL']))         $REF['MAIL']         = $INPUT['MAIL'];
       if (isset($INPUT['PHOTO']))        $REF['PHOTO']        = $INPUT['PHOTO'];
       if (!empty($INPUT['PHOTO'])) {
           if (isset($INPUT['PHOTO_ACTIVE'])) $REF['PHOTO_ACTIVE'] = $INPUT['PHOTO_ACTIVE'];
       } else {
           $REF['PHOTO_ACTIVE'] = false;
       }

       if (strtoupper($user_name) != "ADMINISTRATOR") {
           if (isset($INPUT['ACTIVE']))       $REF['ACTIVE']       = $INPUT['ACTIVE'];
           if (isset($INPUT['TYPE']))         $REF['TYPE']         = $INPUT['TYPE'];
       }
        
       return $REF;
    }

    /* public static void */ function send_mail($empfaenger,$template) 
    {
       settype($empfaenger,"string");
       settype($template,"string");
       settype($INPUT,"array");

       global $YANA;                                              	
       $heute                = getdate();   
       $mail                 = new mailer($template);
       $mail->subject        = $YANA->getVar("LANGUAGE.USER.MAIL_SUBJECT")."\n";
       $mail->insert('DATUM', $heute['mday'] . '.' . $heute['mon'] . '.' . $heute['year']);
       $mail->send($empfaenger);
    }

}

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