View file includes/functions_points.php

File size: 4.16Kb
<?php
/***************************************************************************
 *                           �  KaspeR
 *                            -------------------
 ***************************************************************************/

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
}

if (!function_exists('get_username_from_id'))
{
   function get_username_from_id($user_id)
   {
	global $db;

	$sql = "SELECT username
		FROM " . USERS_TABLE . "
		WHERE user_id = $user_id
		AND user_id <> " . ANONYMOUS;

	if (!$result = $db->sql_query($sql))
	{
		message_die(GENERAL_ERROR, "Could not get username from $user_id.", '', __LINE__, __FILE__, $sql);
	}
	$username = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	return $username['username'];
    }
}

if (!function_exists('get_userid_from_name'))
{
    function get_userid_from_name($username)
    {
	global $db;
	
	$username = str_replace("\'", "''", trim($username));

	$sql = "SELECT user_id
		FROM " . USERS_TABLE . "
		WHERE username = '$username'
		AND user_id <> " . ANONYMOUS;
	if (!$result = $db->sql_query($sql))
	{
		message_die(GENERAL_ERROR, "Could not get user_id from $username.", '', __LINE__, __FILE__, $sql);
	}
	$user_id = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	return $user_id['user_id'];
    }
}

if (!function_exists('get_user_points'))
{
    function get_user_points($user_id)
    {
	global $db, $userdata;
	if ($userdata['user_id'] == $user_id)
	{
		return $userdata['user_points'];
	}
	
	$sql = "SELECT user_points
		FROM " . USERS_TABLE . "
		WHERE user_id = $user_id";

	if (!$result = $db->sql_query($sql))
	{
		message_die(GENERAL_ERROR, "Could not get user_points from $user_id.", '', __LINE__, __FILE__, $sql);
	}
	$points = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	return $points['user_points'];
    }
}

if (!function_exists('change_points'))
{
    function change_points($udata, $amount, $points_reset = false)
    {
	global $db;
	if (is_array($udata))
	{
		$user_id = '';
		while( list($key, $value) = each($udata) )
		{
			if ( intval($value) > 0 )
			{
				$user_id .= ( ( $user_id != '' ) ? ', ' : '' ) . $value;
			}
		}
		$user_id = 'IN (' . $user_id . ')';
	}
	else
	{
		$user_id = '= ' . $udata;
	}
	$update_sql = ($points_reset) ? intval($amount) : "user_points + " . intval($amount);
	$sql = "UPDATE " . USERS_TABLE . "
		SET user_points = " . $update_sql . " 
		WHERE user_id " . $user_id;
	if (!$db->sql_query($sql))
	{
		message_die(GENERAL_ERROR, "Could not update user's points", '', __LINE__, __FILE__, $sql);
	}
	return;
    }
}

if (!function_exists('add_points'))
{
    function add_points($user_id, $amount)
    {
	global $db;
	if (intval($amount) > 0)
	{
		$sql = "UPDATE " . USERS_TABLE . "
			SET user_points = user_points + $amount
			WHERE user_id = $user_id";
		if (!$db->sql_query($sql))
		{
			message_die(GENERAL_ERROR, "Could not update user's points", '', __LINE__, __FILE__, $sql);
		}
	}
	return;
    }
}

if (!function_exists('subtract_points'))
{
    function subtract_points($user_id, $amount)
    {
	global $db;
	if (intval($amount) > 0)
	{
		$sql = "UPDATE " . USERS_TABLE . "
			SET user_points = user_points - $amount
			WHERE user_id = $user_id";
		if (!$db->sql_query($sql))
		{
			message_die(GENERAL_ERROR, "Could not update user's points", '', __LINE__, __FILE__, $sql);
		}
	}	
	return;
    }
}

if (!function_exists('user_is_authed'))
{
    function user_is_authed($user_id)
    {
	global $db, $board_config;

	static $is_authed;

	if (!isset($is_authed))
	{
		$is_authed = false;

		$points_user_group_auth_ids = explode("\n", $board_config['points_user_group_auth_ids']);

		$valid_ids_sql = '';
		foreach ($points_user_group_auth_ids as $id)
		{
			$id = intval(trim($id));

			if (!empty($id))
			{
				$valid_ids_sql .= (( $valid_ids_sql == '' ) ? '': ', ') . $id;
			}
		}

		if ($valid_ids_sql != '')
		{
			$sql = "SELECT group_id
				FROM " . USER_GROUP_TABLE . "
				WHERE group_id IN ($valid_ids_sql)
				AND user_id = $user_id
				AND user_pending = 0";
			$result = $db->sql_query($sql);
			if ($row = $db->sql_fetchrow($result))
			{
				$is_authed = true;
			}
			$db->sql_freeresult($result);
		}
	}
	return $is_authed;
    }
}
?>