<?
	### Database functions version 1.01 17th july 2006
	$version_functions_database=1.01;
?>
<?
	$link = "";
	function ConnectDB($db)	{
		global $link;
		switch ($db) {
			case "ludo" : $host=""; $user="root"; $pass=""; $dbase="ludo";
			break;
			case "planet" : $host=""; $user="root"; $pass=""; $dbase="planet";
			break;
			case "fchain" : $host=""; $user="root"; $pass=""; $dbase="foodchain";
			break;
			case "muad.net" : $host=""; $user="arrakeen";  $pass="SN7QHnv7"; $dbase="ragnarok_org";
			break;
			case "muad.com" : $host=""; $user="arrakeen";  $pass="muad"; $dbase="arrakeen_arena";
			break;
			case "poll" : $host=""; $user="arrakeen";  $pass="muad"; $dbase="arrakeen_poll";
			break;
		}
		$link = mysql_connect ($host, $user, $pass)
			or die ("Could not connect");
#		echo "Connected successfully<br>";
		mysql_select_db($dbase, $link);
	}
	
	function InsertDB($table, $col, $value)	{
		$result = mysql_query("INSERT INTO $table ($col) VALUES ($value)")
			or die ("Invalid query Insert");
		return $result;
	}
	
	function UpdateDB($table, $col, $value, $where)	{
		$result = mysql_query("UPDATE $table SET $col=$value WHERE $where")
			or die ("Invalid query Update");
		return $result;
	}
	
	function SelectDB($table, $col, $search) {
		$result = mysql_query("SELECT * FROM $table WHERE $col='$search'")
			or die ("Invalid query Select");
		return $result;
	}
	
	function CloseDB() {
		global $link;
		mysql_close ($link);
	}

	function Checkloggedin3() {
		global $link;
		global $login, $password, $logged, $firstname, $lastname;
	
		$logged=false;
		if ($login <> '') {
			ConnectDB("muad.com");
			$result = SelectDB('Users', 'Login', $login);
		
			if (mysql_num_rows($result) <> 0)	{
				$returnpassword = mysql_result($result,0,"password");
				$login = mysql_result($result,0,"login");
			
				if ($returnpassword == $password) {
					$logged=true;
				}
			}
			CloseDB();
		}
	}
	
	# 16/04/01 large tests with cookies
	# seem to work at first, but cant load them at any time
	# doesnt seem to be perfect in php...
	
	function Checkloggedin2() {
		global $link;
		global $logged;
		global $login, $password, $master, $lord;
		global $HTTP_COOKIE_VARS;
		
		echo "1".$login."*".$password."*".$logged."*<br>";
		
		ConnectDB("muad.net");
		$login = $HTTP_COOKIE_VARS["login"];
		echo "2".$login."*".$password."*".$logged."*<br>";
		$password = $HTTP_COOKIE_VARS["password"];
		echo "3".$login."*".$password."*".$logged."*<br>";
		$result = SelectDB('Users', 'Login', $login);
		$logged = "false";
		if (mysql_num_rows($result) <> '0')	{
			$returnpassword = mysql_result($result,0,"Password");
			if ($returnpassword == $password) {
				$login = mysql_result($result,0,"Login");
				$master = mysql_result($result,0,"Master");
				$lord = "no";
				if ($master == "lord") { $master = "yes"; $lord = "yes"; }
				$logged = "true";
			}
		}
		echo "4".$login."*".$password."*".$logged."*<br>";
	}

	function Checkloggedin() {
		global $link;
		global $logged;
		global $login, $master, $lord;
		global $REMOTE_ADDR;

#		ConnectDB("muad.net");
		$result = mysql_query("SELECT * FROM Users WHERE IP='$REMOTE_ADDR'");
		
		if (mysql_num_rows($result) == '0') {
#			Message ('User not registered','Please register first');
		} else {
			$login = mysql_result($result,0,"Login");
			$master = mysql_result($result,0,"Master");
			$lord = "no";
			if ($master == "lord") { $master = "yes"; $lord = "yes"; }
			$returndate = mysql_result($result,0,"Date");
			
#			echo $returndate." / ".$currentdate."<br>";
			
			if ($currentdate <= $returndate) {
#				echo "<br><br>&nbsp;&nbsp;Logged in<br>";
				# Update user activity
				$delayminutes = 30;
				$delayseconds = 0;
				$checkdate = date ("Y-m-d H:i:s", mktime (date("H"),date("i")+$delayminutes,date("s")+$delayseconds,date("m"),date("d"),date("Y")));
				$result = mysql_query("UPDATE Users SET Date='$checkdate' WHERE IP='$REMOTE_ADDR'");
				$logged = "true";
			} else {
				Message ('Logged out for inactivity','Please log back in<br>');
			}
		}
	}
	
	function GetNextId($table) {
		$result = mysql_query("SELECT * FROM $table");
		if (mysql_num_rows($result) == 0)	{
			$id = 1;
		} else {
			$id = 0;
			do {
				$id++;
				$result = mysql_query("SELECT * FROM $table WHERE Id=$id");
			} while (mysql_num_rows($result) <> 0);
		}
		return $id;
	}

	function StripText($text) {
		$ntext = "";
		$i = -1;
		do {
			$i++;
			$c = substr($text, $i, 1);
			if ($c == "'") { $ntext .= "`"; }
			if (($c <> "'") and ($c <> "~")) { $ntext .= $c; }
		} while ($i < strlen($text)-1);
		return $ntext;
	}
?>
