Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
Bugdar - Blob - ViewGit - Blue Static
<?php
/*=====================================================================*\
|| ###################################################################
|| # Bugdar
|| # Copyright ©2002-2007 Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version 2 of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/

$fetchtemplates = array(
	'userctrl',
	'userctrl_column',
	'userctrl_search'
);

define('SVN', '$Id$');

$focus['user'] = 'focus';

require_once('./global.php');
require_once('./includes/class_api_error.php');
require_once('./includes/api_user.php');

APIError(array(new API_Error_Handler($message), 'user_cumulative'));

if (!$bugsys->userinfo['userid'])
{
	$message->errorPermission();
}

$userapi = new UserAPI($bugsys);
$userapi->set('userid',		$bugsys->userinfo['userid']);
$userapi->set_condition();

$userinfo = $bugsys->userinfo;

// ###################################################################

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

// ###################################################################

if ($_POST['do'] == 'killsearch')
{
	$search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
	if (!$search)
	{
		$message->errorPermission();
	}

	$db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->in['searchid']);

	$message->redirect(T('This saved search has been removed from your list.'), 'userctrl.php');
}

// ###################################################################

if ($_REQUEST['do'] == 'deletesearch')
{
	$search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
	if (!$search)
	{
		$message->errorPermission();
	}

	$message->confirm(T('Are you sure you want to delete this saved search?'), 'userctrl.php', 'killsearch', T('Delete'), T('Cancel'), array('searchid' => $bugsys->in['searchid']));
}

// ###################################################################

if ($_POST['do'] == 'update')
{
	// -------------------------------------------------------------------
	// authentication
	if (!empty($bugsys->in['password_change']) OR !empty($bugsys->in['email_change']))
	{
		if (empty($bugsys->in['validate']))
		{
			$message->addError(T('You need to enter your current password to change your email or password'));
		}
		else
		{
			if (md5(md5($bugsys->in['validate']) . md5($bugsys->userinfo['salt'])) != $bugsys->userinfo['password'])
			{
				$message->addError(T('Your authentication password does not match the one in our records'));
			}
		}
	}

	// -------------------------------------------------------------------
	// email validation
	if (!empty($bugsys->in['email_change']))
	{
		if (!empty($bugsys->in['email_change']) AND empty($bugsys->in['email_confirm']))
		{
			$message->addError(T('You need to enter both the email and confirm email fields to change your address'));
		}

		if ($bugsys->in['email_change'] != $bugsys->in['email_confirm'])
		{
			$message->addError(T('Your email and confirm email addresses do not match'));
		}

		$userapi->set('email',	$bugsys->in['email_change']);
	}

	// -------------------------------------------------------------------
	// password validation
	if (!empty($bugsys->in['password_change']))
	{
		if (!empty($bugsys->in['password_change']) AND empty($bugsys->in['password_confirm']))
		{
			$message->addError(T('You need to enter both the password and confirm password fields to change your password'));
		}
		else
		{
			if ($bugsys->in['password_change'] != $bugsys->in['password_confirm'])
			{
				$message->addError(T('Your password and confirm password do not match'));
			}
		}

		$userapi->set('password',	$bugsys->in['password_change']);
	}

	$userapi->set('displayname',	$bugsys->in['displayname']);
	$userapi->set('showemail',		$bugsys->in['showemail']);
	$userapi->set('showcolors',		$bugsys->in['showcolors']);
	$userapi->set('languageid',		$bugsys->in['languageid']);
	$userapi->set('timezone',		$bugsys->in['timezone']);
	$userapi->set('usedst',			$bugsys->in['usedst']);
	$userapi->set('hidestatuses',	$bugsys->in['hidestatuses']);
	$userapi->set('defaultsortkey',	$bugsys->in['defaultsortkey']);
	$userapi->set('defaultsortas',	$bugsys->in['defaultsortas']);
	$userapi->set('columnoptions',	$bugsys->input_clean('columnoptions', TYPE_UINT));

	// -------------------------------------------------------------------
	// copy fields
	$userinfo['displayname'] = $bugsys->in['displayname'];
	$userinfo['showemail'] = $bugsys->in['showemail'];
	$userinfo['showcolors'] = $bugsys->in['showcolors'];
	$userinfo['languageid'] = $bugsys->in['languageid'];
	$userinfo['timezone'] = $bugsys->in['timezone'];
	$userinfo['usedst'] = $bugsys->in['usedst'];
	$userinfo['hidestatuses'] = $bugsys->in['hidestatuses'];
	$userinfo['defaultsorkey'] = $bugsys->in['defaultsorkey'];
	$userinfo['defaultsortas'] = $bugsys->in['defaultsortas'];
	$userinfo['columnoptions'] = $bugsys->in['columnoptions'];

	$email = $bugsys->in['email_change'];
	$email_confirm = $bugsys->in['email_confirm'];

	// -------------------------------------------------------------------
	// error handling
	if ($message->hasErrors())
	{
		$show['errors'] = true;
		$_REQUEST['do'] = 'modify';
	}
	else
	{
		$userapi->update();

		$db->query("DELETE FROM " . TABLE_PREFIX . "useremail WHERE userid = " . $bugsys->userinfo['userid']);
		$bugsys->input_clean('emailopts', TYPE_INT);
		if (is_array($bugsys->in['emailopts']))
		{
			foreach ($bugsys->in['emailopts'] AS $relation => $bitarr)
			{
				$bitmask = 0;
				if (is_array($bitarr))
				{
					foreach ($bitarr AS $option => $yes)
					{
						$bitmask += $option * $yes;
					}
				}
				$db->query("INSERT INTO " . TABLE_PREFIX . "useremail (userid, relation, mask) VALUES (" . $bugsys->userinfo['userid'] . ", $relation, $bitmask)");
			}
		}

		$message->redirect(T('The changes to your account have been made.'), 'userctrl.php');
	}
}

// ###################################################################

if ($_REQUEST['do'] == 'modify')
{
	$langselect = construct_datastore_select('language', 'title', 'languageid', $userinfo['languageid']);

	$hidestatuses = construct_datastore_select('status', 'status', 'statusid', (!is_array($userinfo['hidestatuses']) ? explode(',', $userinfo['hidestatuses']) : $userinfo['hidestatuses']), 0);
	$hidestatusesnum = (sizeof($bugsys->datastore['status']) < 8 ? sizeof($bugsys->datastore['status']) + 1 : 8);

	$defaultsortkey = construct_option_select('defaultsortkey', ListSorter::fetch_by_text(false), $userinfo['defaultsortkey']);
	$defaultsortas = construct_option_select('defaultsortas', ListSorter::fetch_as_text(false), $userinfo['defaultsortas']);

	foreach ($datef->fetch_timezone_list() AS $value => $label)
	{
		$selected = ($value == $userinfo['timezone']);
		eval('$tzselect .= "' . $template->fetch('selectoption') . '";');
	}

	$checked = array();
	if (!is_array($bugsys->in['emailopts']))
	{
		$options = $db->query("SELECT * FROM " . TABLE_PREFIX . "useremail WHERE userid = " . $bugsys->userinfo['userid']);
		while ($opt = $db->fetch_array($options))
		{
			foreach ($bugsys->emailoptions['notifications'] AS $name => $notif)
			{
				foreach ($bugsys->emailoptions['relations'] AS $name => $relation)
				{
					if ($opt['mask'] & $notif AND $opt['relation'] == $relation)
					{
						$checked["$relation"]["$notif"] = HTML_CHECKED;
					}
				}
			}
		}
	}
	else
	{
		$checked = $bugsys->in['emailopts'];
		foreach ($checked AS $key1 => $value1)
		{
			foreach ($value1 AS $key2 => $value2)
			{
				if ($value2)
				{
					$checked["$key1"]["$key2"] = HTML_CHECKED;
				}
			}
		}
	}

	$columns = array();
	$columnOptions = '';
	if (!is_array($bugsys->in['columnoptions']))
	{
		foreach ($bugsys->columns AS $column => $mask)
		{
			if (is_array($bugsys->userinfo['columnoptions']))
			{
				$columns["$column"] = $bugsys->userinfo['columnoptions']["$column"];
			}
			else
			{
				$columns["$column"] = $bugsys->options['columnoptions']["$column"];
			}
			eval('$columnOptions .= "' . $template->fetch('userctrl_column') . '";');
		}
	}
	else
	{
		$columns = $bugsys->in['columnoptions'];
		foreach ($bugsys->columns AS $column => $mask)
		{
			eval('$columnOptions .= "' . $template->fetch('userctrl_column') . '";');
		}
	}

	// searches
	$searches = '';
	$searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo['userid']);
	while ($search = $db->fetch_array($searchesFetch))
	{
		eval('$searches .= "' . $template->fetch('userctrl_search') . '";');
	}

	eval('$template->flush("' . $template->fetch('userctrl') . '");');
}

/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>