<?php
/**
 * phpaga
 *
 * User
 *
 * This is the user management interface.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: add_user.php 726 2006-08-05 22:34:30Z florian $
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

include_once("./config.php");

phpaga_user_pageperm(PHPAGA_PERM_MANAGE_SYSSETTINGS);


$user = array ();
$user["usr_login"] = "";
$user["usr_skin"] = PHPAGA_DEFAULT_SKIN;
$user["lng_id"] = PHPAGA_DEFAULTLANGUAGE;
$user["pe_id"] = "";
$user["pe_name"] = "";
$user["usr_pwd_new"] = "";
$user["usr_pwd_confirm"] = "";

$skins = array();
$languages = array();
$persons = array();
$persons_count = 0;

$error = array();

if (isset ($_REQUEST["action"]) && ($_REQUEST["action"] == ACTION_ADD))
{

    if (isset($_REQUEST["usr_login"]) && strlen($_REQUEST["usr_login"]))
        $user["usr_login"] = $_REQUEST["usr_login"];
    else
        $error[] = PHPAGA_ERR_AUTH_LOGINLENGTH;

    if (isset($_REQUEST["pe_id"]) && is_numeric($_REQUEST["pe_id"]))
        $user["pe_id"] = $_REQUEST["pe_id"];
    else
        $error[] = PHPAGA_ERR_USR_NOPEASSOC;

    /* set the id of the person who wants to create this record (owner) */
    $user["usr_pe_id"] = $_SESSION["auth_user"]["pe_id"];

    if (isset($_REQUEST["lng_id"]) && is_numeric($_REQUEST["lng_id"]))
        $user["lng_id"] = $_REQUEST["lng_id"];

    if (isset($_REQUEST["usr_skin"]) && strlen($_REQUEST["usr_skin"]))
        $user["usr_skin"] = $_REQUEST["usr_skin"];

    /* check if password is set and confirmation matches */
    if (isset($_REQUEST["usr_pwd_new"]) && strlen($_REQUEST["usr_pwd_new"]))
    {

        if (!isset($_REQUEST["usr_pwd_confirm"])
            || !strlen($_REQUEST["usr_pwd_confirm"])
            || ($_REQUEST["usr_pwd_new"] != $_REQUEST["usr_pwd_confirm"]))

            $error[] = PHPAGA_ERR_LOGIN_PASSWDNOMATCH;
        else
            $user["usr_passwd"] = $_REQUEST["usr_pwd_new"];
    }
    else
        $error[] = PHPAGA_ERR_AUTH_PASSWDLENGTH;

    /* if no error has occured, then add the new user */
    if (count($error) == 0)
    {
        $result = phpaga_user_add($user);

        if ($result != PHPAGA_ERR_NONE)
            $error[] = $result;
        else
            header("Location: users.php");
    }
}

$skins = phpaga_getSkinNames();
phpaga_getLanguages($languages);
phpaga_persons_list ($persons, $persons_count, "", "");

foreach ($persons as $person)
    $h_pe[$person["pe_id"]] = $person["pe_person"];

$persons = $h_pe;

phpaga_header(array("menuitem" => "administration"));

if (count($error) > 0)
    phpaga_error($error);

$users_tpl = new psmarty;

$users_tpl->assign("SELF", basename($_SERVER["PHP_SELF"]));
$users_tpl->assign("persons", $persons);
$users_tpl->assign("user", $user);
$users_tpl->assign("skins", $skins);
$users_tpl->assign("languages", $languages);

$users_tpl->display("add_user.tpl.html");

phpaga_footer();

?>