<?php
// $Id: users.php,v 1.1 2004/07/11 10:29:02 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Francisco Burzi
// Author Website : http://phpnuke.org
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

/*********************************************************/
/* Users Functions                                       */
/*********************************************************/
include_once(XOOPS_ROOT_PATH."/class/xoopslists.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function displayUsers() {
global $db, $xoopsConfig, $xoopsModule, $HTTP_GET_VARS;

$userstart = isset($HTTP_GET_VARS['userstart']) ? intval($HTTP_GET_VARS['userstart']) : 0;

xoops_cp_header();
OpenTable();

$usercount   = XoopsUser::countAllUsers();
$nav         = new XoopsPageNav($usercount, 500, $userstart, "userstart", "fct=users");
$editform    = new XoopsThemeForm(_AM_EDEUSER, "edituser", "admin.php");
$user_select = new XoopsFormSelect('', "uid");
$user_select->addOptionArray(XoopsUser::getAllUsersList(array(), 'uname ASC', 500, $userstart));
$user_select_tray = new XoopsFormElementTray(_AM_NICKNAME, "<br />");
$user_select_tray->addElement($user_select);
$user_select_nav = new XoopsFormLabel('', $nav->renderNav(4));
$user_select_tray->addElement($user_select_nav);
$op_select = new XoopsFormSelect("", "op");
$op_select->addOptionArray(array("modifyUser"=>_AM_MODIFYUSER, "delUser"=>_AM_DELUSER));
$submit_button = new XoopsFormButton("", "submit", _GO, "submit");
$fct_hidden    = new XoopsFormHidden("fct", "users");
$editform->addElement($user_select_tray);
$editform->addElement($op_select);
$editform->addElement($submit_button);
$editform->addElement($fct_hidden);
$editform->display();

echo "<br />";

$uid_value        = "";
$uname_value      = "";
$name_value       = "";
$email_value      = "";
$email_cbox_value = 0;
$url_value        = "";
$avatar_value     = "blank.gif";
$theme_value      = $xoopsConfig['default_theme'];
$timezone_value   = $xoopsConfig['default_TZ'];
$icq_value        = "";
$aim_value        = "";
$yim_value        = "";
$msnm_value       = "";
$location_value   = "";
$occ_value        = "";
$interest_value   = "";
$sig_value        = "";
$sig_cbox_value   = 0;
$umode_value      = $xoopsConfig['com_mode'];
$uorder_value     = $xoopsConfig['com_order'];
$bio_value        = "";
$rank_value       = 0;
$op_value         = "addUser";
$form_title       = _AM_ADDUSER;

include_once(XOOPS_ROOT_PATH."/modules/system/admin/users/userform.php");
CloseTable();
xoops_cp_footer();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function modifyUser($user) {
global $db, $xoopsConfig, $xoopsModule;

xoops_cp_header();
OpenTable();

$user = new XoopsUser($user);

if ( !$user->isActive() ) {
	echo "<h4 style='text-align:left;'>"._AM_NOTACTIVE."</h4>";
	echo "<table><tr><td>";
	echo myTextForm("admin.php?fct=users&amp;op=reactivate&amp;uid=".$user->getVar("uid"), _YES);
	echo "</td><td>";
	echo myTextForm("admin.php?fct=users", _NO);
	echo "</td></tr></table>";
	CloseTable();
	xoops_cp_footer();
	exit();
}

if ($user) {
	$uid_value        = $user->getVar("uid");
	$uname_value      = $user->getVar("uname", "E");
	$name_value       = $user->getVar("name", "E");
	$email_value      = $user->getVar("email", "E");
	$email_cbox_value = $user->getVar("user_viewemail") ? 1 : 0;
	$url_value        = $user->getVar("url", "E");
	$avatar_value     = $user->getVar("user_avatar");
	$temp             = $user->getVar("theme");
	$theme_value      = empty($temp) ? $xoopsConfig['default_theme'] : $temp;
	$timezone_value   = $user->getVar("timezone_offset");
	$icq_value        = $user->getVar("user_icq", "E");
	$aim_value        = $user->getVar("user_aim", "E");
	$yim_value        = $user->getVar("user_yim", "E");
	$msnm_value       = $user->getVar("user_msnm", "E");
	$location_value   = $user->getVar("user_from", "E");
	$occ_value        = $user->getVar("user_occ", "E");
	$interest_value   = $user->getVar("user_intrest", "E");
	$sig_value        = $user->getVar("user_sig", "E");
	$sig_cbox_value   = ($user->getVar("attachsig") == 1) ? 1 : 0;
	$umode_value      = $user->getVar("umode");
	$uorder_value     = $user->getVar("uorder");
	$bio_value        = $user->getVar("bio", "E");
	$rank_value       = $user->rank(false);
	$op_value         = "updateUser";
	$form_title       = _AM_UPDATEUSER.": ".$user->getVar("uname");

	include_once(XOOPS_ROOT_PATH."/modules/system/admin/users/userform.php");
	echo "
	<br /><b>"._AM_USERPOST."</b><br /><br />
	<table border='0'>
	<tr><td>"._AM_COMMENTS."</td><td>".$user->getVar("posts")."</td></tr>
	</table>
	<br />"._AM_PTBBTSDIYT."<br />
	<form action='admin.php' method='post'>
	<input type='hidden' name='id' value='".$user->getVar("uid")."'>
	<input type='hidden' name='type' value='user'>
	<input type='hidden' name='fct' value='users'>
	<input type='hidden' name='op' value='synchronize'>
	<input type='submit' class='button' value='"._AM_SYNCHRONIZE."'>
	</form>";
	CloseTable();
	} else {
		echo "<h4 style='text-align:left;'>";
		echo _AM_USERDONEXIT;
		echo "</h4>";
		CloseTable();
	}

xoops_cp_footer();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function updateUser($uid, $uname, $name, $url, $email, $user_icq, $user_aim, $user_yim, $user_msnm, $user_from, $user_occ, $user_intrest, $user_viewemail, $user_avatar, $user_sig, $attachsig, $theme, $pass, $pass2, $rank, $bio, $uorder, $umode, $timezone_offset) {
global $xoopsConfig, $db, $xoopsModule, $xoopsBadEmails;

if ( !empty($xoopsBadEmails) && hasMatch($xoopsBadEmails, $email) ) {
	xoops_cp_header();
	OpenTable();
	echo _US_EMAILRESERVED;
	CloseTable();
	xoops_cp_footer();
	exit();
}

$edituser = new XoopsUser($uid);
$edituser->setVar("name", $name);
$edituser->setVar("uname", $uname);
$edituser->setVar("email", $email);
$edituser->setVar("url", formatURL($url));
$edituser->setVar("user_avatar", $user_avatar);
$edituser->setVar("user_icq", $user_icq);
$edituser->setVar("user_from", $user_from);
$edituser->setVar("user_sig", $user_sig);
$edituser->setVar("user_viewemail", $user_viewemail);
$edituser->setVar("user_aim", $user_aim);
$edituser->setVar("user_yim", $user_yim);
$edituser->setVar("user_msnm", $user_msnm);
$edituser->setVar("attachsig", $attachsig);
$edituser->setVar("timezone_offset", $timezone_offset);
$edituser->setVar("theme", $theme);
$edituser->setVar("uorder", $uorder);
$edituser->setVar("umode", $umode);
$edituser->setVar("bio", $bio);
$edituser->setVar("rank", $rank);
$edituser->setVar("user_occ", $user_occ);
$edituser->setVar("user_intrest", $user_intrest);

if ($pass2 != "") {
	if ($pass != $pass2) {
		xoops_cp_header();
		OpenTable();
		echo "<center>"._AM_STNPDNM."</center>";
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	$edituser->setVar("pass", md5($pass));
}

if ( !$edituser->store() ) {
	xoops_cp_header();
	OpenTable();
	echo $editusers->getErrors();
	CloseTable();
	xoops_cp_footer();
	} else {
		redirect_header("admin.php?fct=users",1,_UPDATED);
	}

exit();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function synchronize($id, $type) {
global $db;

switch($type) {
	case 'user':
		$tables = array("comments","xoopspollcomments");
		$total_posts = 0;
		$sql = "SELECT COUNT(*) AS total FROM ".$db->prefix("bb_posts")." WHERE uid=$id";
		if ($result = $db->query($sql)) {
			if($row = $db->fetch_array($result)) {
					$total_posts = $total_posts + $row['total'];
				}
			}
		foreach ($tables as $table) {
			$sql = "SELECT COUNT(*) AS total FROM ".$db->prefix($table)." WHERE user_id=$id";
			if ( $result = $db->query($sql) ) {
				if ($row = $db->fetch_array($result)) {
					$total_posts = $total_posts + $row['total'];
				}
			}
		}
		$sql = "UPDATE ".$db->prefix("users")." SET posts=$total_posts WHERE uid=$id";
		if (!$result = $db->query($sql)) {
			die(sprintf(_AM_CNUUSER %s ,$id));
		}
		break;

	case 'all users':
		$sql = "SELECT uid FROM ".$db->prefix("users")."";
		if (!$result = $db->query($sql)) {
			die(_AM_CNGUSERID);
		}
		while ($row = $db->fetch_array($result)) {
			$id = $row['uid'];
			synchronize($id, "user");
		}
		break;
}

redirect_header("admin.php?fct=users&op=modifyUser&uid=".$id."", 1, _UPDATED);
exit();
}
//---------------------------------------------------------------------------------------//
	} else {
		echo "Access Denied";
	}
?>
