<?php
/**
 * phpaga
 *
 * User
 *
 * This is the user management interface.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * 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_DATA['action']) && ($REQUEST_DATA['action'] == ACTION_ADD))
{

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

    if (isset($REQUEST_DATA['pe_id']) && is_numeric($REQUEST_DATA['pe_id']))
        $user['pe_id'] = $REQUEST_DATA['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_DATA['lng_id']) && is_numeric($REQUEST_DATA['lng_id']))
        $user['lng_id'] = $REQUEST_DATA['lng_id'];

    if (isset($REQUEST_DATA['usr_skin']) && strlen($REQUEST_DATA['usr_skin']))
        $user['usr_skin'] = $REQUEST_DATA['usr_skin'];

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

        if (!isset($REQUEST_DATA['usr_pwd_confirm']) || !strlen($REQUEST_DATA['usr_pwd_confirm'])
            || ($REQUEST_DATA['usr_pwd_new'] != $REQUEST_DATA['usr_pwd_confirm']))

            $error[] = PHPAGA_ERR_LOGIN_PASSWDNOMATCH;
        else
            $user['usr_passwd'] = $REQUEST_DATA['usr_pwd_new'];
    }
    else
        $error[] = PHPAGA_ERR_AUTH_PASSWDLENGTH;

    if (count($error) == 0) {

        $result = phpaga_user_add($user);

        if (PhPagaError::isError($result)) {

            if ($result->getCode() == PHPAGA_ERR_DB_DUPLICATE)
                $error[] = sprintf(_('Unable to insert user - the login %s is already in use.'), $user['usr_login']);
            else
                $error[] = $result->getCode();
        } else {
            header('Location: users.php');
            exit;
        }
    }
}

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

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();

?>