<?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_admin extends plugin
{

    /* private user_admin */ var $file;

    /* constructor */ function plugin_user_admin($name) {
        settype($name,"string");
        $this->name = $name;
        $this->file = new user_admin();
    }

    /* public void */ function _default(/* String */ $event, /* Hashtable */ $ARGS) {
         settype($event, "string");
         settype($ARGS, "array");
         global $YANA;

         if (!$this->file->isEmpty()) {

             switch ($event) {

                 case "get_user_list":
                     return $this->file->get_user_configuration();
                 break;

                 case "get_user_edit":
                     $YANA->setVar("TARGET",$ARGS['target']);
                     return $this->file->get_user_edit($ARGS['target']);
                 break;

                 case "set_user_edit":
                     $YANA->setVar("TARGET",$ARGS['target']);
                     $ARGS['active'] = !empty($ARGS['active']);
                     return $this->file->set_user_edit($ARGS['target'],$ARGS);
                 break;

                 case "get_user_delete":
                     $YANA->setVar("TARGET", $ARGS['target']);
                     return $this->file->get_user_delete($ARGS['target']);
                 break;

                 case "set_user_delete":
                     return $this->file->set_user_delete($ARGS['target']);
                 break;

                 case "get_user_new":
                     $YANA->setVar("TARGET",$ARGS['target']);
                     return $this->file->get_user_new($ARGS['target']);
                 break;

                 case "set_user_new":
                     if ($this->file->set_user_new($ARGS['target'],$ARGS)) {
                        return true;
                     } else {
                        return false;
                     }
                 break;

                 case "set_user_pwd":
                     $YANA->language->read("user");
                     if ($this->file->set_user_pwd($ARGS['target']))
                     {
                         $YANA->message("OK", "200", "index");
                         return false;
                     } else {
                         $YANA->message("error", "500", "index");
                         return false;
                     }
                 break;

                 /* <FORMULAR> Mail-Authentifizierung */
                 case "get_user_mail":
                     $YANA->setVar("TARGET",(!empty($ARGS['target'])) ? $ARGS['target'] : '');
                     if (!$YANA->getVar("PROFILE.USER.ALLOW_CREATE")) {
                         $YANA->message("error", "ungueltige_eingabe", "");
                         return false;
                     }

                     $YANA->setVar("ACTION_PARAMS", '<input type="hidden" name="action" value="SET_USER_MAIL">');
                 break;

                 /* <SESSION-ERZEUGEN> Mail-Authentifizierung */
                 case "set_user_mail":
                     if (!$YANA->getVar("PROFILE.USER.ALLOW_CREATE")) {
                        $YANA->report( new error("ungueltige_eingabe"));
                        return false;
                     }

                     $db = $YANA->connect("user");
                     $mail = untaintInput(strtolower($ARGS['mail']), 'mail', 255);
                     $name = strtolower($ARGS['username']);

                     if (empty($name) || empty($mail)) {
                         $YANA->report( new warning("ungueltige_eingabe"));
                         return false;
                     }

                     $key = md5($mail);

                     if ($db->exists("user.$name")) {
                         $YANA->report( new warning("user_already_exists"));
                         return false;
                     }

                     $duplicates = $db->get("newuser.*.newuser_id", "newuser_key=$key");
                     if (is_array($duplicates)) {
                        foreach ($duplicates as $newuser_id)
                        {
                            $db->remove("newuser.$newuser_id");
                        }
                     }
                     unset($duplicates);
                     $db->write();

                     /* limit = 24h = 86400sek */
                     $old_entries = $db->get("newuser");
                     $limit       = time() - 86400;
                     if (is_array($old_entries)) {
                         foreach ($old_entries as $row)
                         {
                             if ($row['NEWUSER_UTC'] < $limit) {
                                 $db->remove("newuser.".$row['NEWUSER_ID']);
                             }
                         }
                     }
                     unset($old_entries);
                     $db->write();

                     if (!$db->insert("newuser.*", array("newuser_name" => $name, "newuser_key" => $key, "newuser_mail" => $mail))) {
                         $YANA->report( new warning("ungueltige_eingabe"));
                         return false;
                     }

                     if (!$db->write()) {
                         return false;
                     }

                     $YANA->setVar('WEBSITE_URL', $YANA->getVar("REFERER"));
                     $YANA->setVar('KEY', $key);
                     $YANA->setVar('MAIL', $mail);
                     user_admin::send_mail($mail,$YANA->skin->getFile("USER_CONFIRM_MAIL"));

                     return true;
                 break;

                 case "user_authentification":
                     if (!$YANA->getVar("PROFILE.USER.ALLOW_CREATE")) {
                         $YANA->report( new error("ungueltige_eingabe"));
                         return false;
                     }

                     $db = $YANA->connect("user");

                     if (!$db->exists('newuser','newuser_key='.$ARGS['target'])) {
                         $YANA->report( new error("ungueltige_eingabe"));
                         return false;
                     } else {
                         $id = $db->get('newuser.*.newuser_id','newuser_key='.$ARGS['target']);
                         $entry = $db->get("newuser.${id}");
                         $REF = $ARGS;
                         $REF['MAIL'] = $entry['NEWUSER_MAIL'];
                         if ($this->file->user_authentification($entry['NEWUSER_NAME'],$REF)) {
                             $YANA->message("alert", "first_login", "");
                             return true;
                         } else {
                             return false;
                         }
                     }
                 break;

                 case "view_profile":
                     return $this->file->get_user($ARGS['target']);
                 break;

                 case "get_profile_edit":
                     return $this->file->get_user($_SESSION['user_name'], true);
                 break;

                 case "set_profile_edit":
                     return $this->file->set_profile_edit($ARGS);
                 break;

                 default:
                     $YANA->language->read("user");
                     $USERLIST   = $this->file->getUserList();
                     $YANA->setVar("USERLIST",$USERLIST);
                 break;

             } # end switch
         }
         return true;
    }

}

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