<?php
/**
 * phpaga
 *
 * User
 *
 * This is the user management interface.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: user.php 848 2007-08-31 15:41:51Z 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");

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

$user_id = "";
$search_args = "";
$rows = "";
$count = "";
$browse = "";

$error = "";

$privmanage = false;



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

    if (isset($_REQUEST["perm"]) && is_array($_REQUEST["perm"]))
        $userInfo["perm"] = $_REQUEST["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["action"]) && ($_REQUEST["action"] == ACTION_UPDATE))
{
    if (isset($_REQUEST["lng_id"]) && is_numeric($_REQUEST["lng_id"]))
        $userInfo["lng_id"] = $_REQUEST["lng_id"];

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

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

    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 = _("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["usr_pwd_old"]) && strlen($_REQUEST["usr_pwd_old"]))
                    $h_oldpwd = $_REQUEST["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["usr_pwd_new"];
            }
            else
            {
                /* we have an admin calling this, so he does not need to know
                 * the old password  */

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

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

        if (PhPagaError::isError($result))
            $error = $result->getMessage();
        else {
            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["action"]) && ($_REQUEST["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";
        else
            $permchecked[$h_perm] = "";

    $users_tpl = new psmarty;

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

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

phpaga_footer();

?>