<?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");

$userInfo = array ();
$skins = array();
$languages = array();
$permchecked = array();

$options_truefalse = array();
$options_truefalse[true] = _('Yes');
$options_truefalse[false] = _('No');

$error = null;
$privmanage = false;


/* an admin can change other user's settings */
if (phpaga_user_hasperm(PHPAGA_PERM_MANAGE_SYSSETTINGS)
    && isset($REQUEST_DATA["id"]) && is_numeric ($REQUEST_DATA["id"]))
{
    $userInfo["usr_id"] = $REQUEST_DATA["id"];

    if (isset($REQUEST_DATA["perm"]) && is_array($REQUEST_DATA["perm"]))
        $userInfo["perm"] = $REQUEST_DATA["perm"];
    else
        $userInfo["perm"] = array();
}
else
{
    /* common users can only change their own settings */
    $userInfo["usr_id"] = $_SESSION["auth_user"]["usr_id"];
}

if (phpaga_user_hasperm(PHPAGA_PERM_MANAGE_SYSSETTINGS))
{
    $privmanage = true;
}

if (isset ($REQUEST_DATA["action"]) && ($REQUEST_DATA["action"] == ACTION_UPDATE))
{
    if (isset($REQUEST_DATA["lng_id"]) && is_numeric($REQUEST_DATA["lng_id"]))
        $userInfo["lng_id"] = $REQUEST_DATA["lng_id"];

    if (isset($REQUEST_DATA["usr_skin"]) && strlen($REQUEST_DATA["usr_skin"]))
        $userInfo["usr_skin"] = $REQUEST_DATA["usr_skin"];

    /* check if password change is requested and if new password and
     * confirmation match */

    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 = _("The new password and its confirmation do not match.");
        }
        else
        {

            /* if current user has no admin rights then check the old passwd */
            if (!phpaga_user_hasperm(PHPAGA_PERM_MANAGE_SYSSETTINGS))
            {
                if (isset($REQUEST_DATA["usr_pwd_old"]) && strlen($REQUEST_DATA["usr_pwd_old"]))
                    $h_oldpwd = $REQUEST_DATA["usr_pwd_old"];
                else
                    $h_oldpwd = "";

                $h_userID = phpaga_auth_login($_SESSION["auth_user"]["usr_login"], $h_oldpwd);

                if (PhPagaError::isError($h_userID))
                    $error = _("The old password is incorrect.");
                else
                    /* old password is correct, new password and confirmation
                     * do match */

                    $userInfo["usr_passwd"] = $REQUEST_DATA["usr_pwd_new"];
            }
            else
            {
                /* we have an admin calling this, so he does not need to know
                 * the old password  */

                $userInfo["usr_passwd"] = $REQUEST_DATA["usr_pwd_new"];
            }
        }
    }

    /* If no error has occured, then update user settings */
    if (!strlen($error))
    {
        $update_result = null;

        $result = phpaga_user_update($userInfo);

        if (PhPagaError::isError($result))
            $error = $result->getMessage();
        else {

            if (isset($REQUEST_DATA['userconfig']) && count($REQUEST_DATA['userconfig'])) {
                foreach($REQUEST_DATA['userconfig'] as $key => $value) {

                    $result = phpaga_config_usersetitem($key, $value, $userInfo["usr_id"]);

                    if (PhPagaError::isError($result)) {
                        $result->printMesssage();
                        $update_result = $result;
                    }
                }
            }

            if (!PhPagaError::isError($update_result)) {
                header("Location: ".$_SERVER["PHP_SELF"]."?id=".$userInfo["usr_id"]."&action=".ACTION_UPDATE_OK);
                exit;
            }
        }
    }
}

$skins = phpaga_getSkinNames();
$languages = phpaga_getLanguages();

phpaga_header();

if (strlen($error))
    phpaga_error($error);


if (isset ($REQUEST_DATA["action"]) && ($REQUEST_DATA["action"] == ACTION_UPDATE_OK))
    phpaga_message(_("User data updated"),
                   _("The user settings have been updated successfully."));

$user = phpaga_user_getUserInfo($userInfo["usr_id"]);

if (PhPagaError::isError($user))
    $user->printMessage();
else {

    foreach ($phpaga_perm as $h_perm => $permtxt)
        if (phpaga_user_hasperm($h_perm, $userInfo["usr_id"]))
            $permchecked[$h_perm] = 'checked="checked\"';
        else
            $permchecked[$h_perm] = '';

    $tpl = new psmarty;

    $tpl->assign("SELF", basename($_SERVER["PHP_SELF"]));
    $tpl->assign("user", $user);
    $tpl->assign("skins", $skins);
    $tpl->assign("languages", $languages);
    $tpl->assign("privmanage", $privmanage);
    $tpl->assign("perm", $phpaga_perm);
    $tpl->assign("permchecked", $permchecked);
    $tpl->assign('options_truefalse', $options_truefalse);

    $tpl->display("user.tpl.html");
}

phpaga_footer();

?>
