<?php
ob_start();
$table = 'php4all_members_system';//Sets the table name
switch($_GET['get'])
{
// Default page --------------
default:
//Pagination
$limit = 25;// Set the limit of results per page
$page = $_GET['page'];// Sets the page, the default is ?page=
$totalrows = mysql_num_rows(mysql_query("SELECT id FROM $table"));// Counts the total rows
if(empty($page))// If the page is empty
{
	$page = '1';// Page is 1
};
$start = ($page-1)*$limit;
$start = round($start,0);// Sets the start
echo 'Registered Users:<br />';
$result = mysql_query("SELECT * FROM $table LIMIT $start, $limit");// Normal query
while ($r = mysql_fetch_array($result))
{
	echo "$r[user]<br />";// Echoes the content
};
$totalusers = mysql_num_rows(mysql_query("SELECT id FROM $table"));
echo "<br />Total Users: $totalusers<br />";// Echoes the total users
$totalpages = $totalrows / $limit;// Get the total pages
$totalpages = ceil($totalpages);// Rounds the total pages to the highest possible numer
if($page == 1)// If the page is 1
{
	$actualpage = '[1]';// Sets the actual page
}
else// Else
{
	$actualpage = "[$page]";// The page is the one it's specified in ?page=
}
if($page < $totalpages)// If the actual page is smaller than the totalpages
{
	$nv = $page+1;// Sets the next variable
	$pv = $page-1;// Sets the preview variable
	$nextpage = "<a href=?page=$nv>></a>";// Sets the next page
	$prevpage = "<a href=?page=$pv><</a>";// Sets the prev page
	$firstpage = "<a href=\"?page=1\">«</a>";// Sets the first page
	$finalpage = "<a href=\"?page=$totalpages\">»</a>";// Sets the final page
}
if($page == '1')
{
	$nv = $page+1;
	$nextpage = "<a href=?page=$nv>></a>";
	$prevpage = "<";
	$firstpage = "«";
	$finalpage = "<a href=\"?page=$totalpages\">»</a>";
}elseif($page == $totalpages){
	$pv = $page-1;
	$nextpage = ">";
	$prevpage = "<a href=?page=$pv><</a>";
	$firstpage = "<a href=\"?page=1\">«</a>";
	$finalpage = "»";
}
if($totalpages == '1' || $totalpages == '0'){
		$nextpage = ">";
		$prevpage = "<";
		$firstpage = "«";
		$finalpage = "»";
}
echo "$firstpage $prevpage Actual Page: $actualpage $nextpage $finalpage<br />Pages: $totalpages";
// End of Pagination ---------
// End of Default Page -------
break;
// Register Page -------------
case 'register':
if(!(isset($_COOKIE['php4all_user'])))// If the cookie is not set it will show the form
{
	if($_POST['Submit'])// If the form is submitted
	{
		$user = $_POST['user'];// Gets the user
		$pass = $_POST['pass'];// Gets the pass
		$pass = md5($pass);// Encrypt the pass using md5
		$email = $_POST['email'];// Gets the email	
		if(!ereg("[a-z||0-9]", $user))// Check if the username is alphanumeric
		{
			die('Only alphanumerical nicks are allowed.');// If it isn't alphanumeric it will kill the script
		}
		if(!ereg("[a-z||0-9]@[a-z||0-9].[a-z]", $email))// Checks if it's a real email
		{
			die('Please, insert a valid email.');//If it isn't a valid email, it kill the script
		}
		$ckeck = mysql_fetch_array(mysql_query("SELECT id FROM $table WHERE user='$user'"));//Checks if there is another user with the same nick
		if($ckeck)//If there is one
		{
		die('That username is already in use, please, choose another one.');// Kills the script
		}
		$ok = mysql_query("INSERT INTO $table (`id`, `user`, `pass`, `email`) VALUES ('NULL', '$user', '$pass', '$email')");// If everything is ok it will insert the info to the DB
		if($ok)// If there were no errors
		{
			echo "Congratulations $user, you have registered succesfully";// Echoes this message
		}
		else// Else
		{
			echo 'Oops, Error.';// Echoes this one
		}
	}
	else
	{//Else, if it's not submitted, it will show the form
	echo "<form action=\"?go=members&get=register\" method=\"post\">
	<table border=\"0\">
	  <tr>
	    <td>Username</td>
	    <td><input name=\"user\" type=\"text\" id=\"user\" /></td>
	  </tr>
	  <tr>
	    <td>Password</td>
	    <td><input name=\"pass\" type=\"text\" id=\"pass\" /></td>
	  </tr>
	  <tr>
	    <td>Email</td>
	    <td><input name=\"email\" type=\"text\" id=\"email\" /></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><input type=\"submit\" name=\"Submit\" value=\"Register\" /></td>
	  </tr>
	</table>
	</form>";
	}
}
else
{
	echo 'You are already regsitered and logged in!.';// If there is a cookie it will display this message
}
// End of Register Page ------
break;
// Login Page ----------------
case 'login':
if(!isset($_COOKIE['php4all_user']))// Checks if the user is logged
{
	if($_POST['Submit'])
	{
		$user = $_POST['user'];
		$pass = $_POST['pass'];
		$pass = md5($pass);
		$ok = mysql_fetch_array(mysql_query("SELECT id FROM $table WHERE user='$user' and pass='$pass'"));//Checks in the DB for the User and Pass
		if($ok)//If it found it
		{
			setcookie('php4all_user',$user,time()+3600*24*30);// Cookie for 1 month
			echo "Welcome $user, Enjoy your stay.";
		}
		else
		{
			echo 'Wrong username and password conbination, please try again.';
		}
	}
	else
	{
	echo "<form action=\"?go=members&get=login\" method=\"post\">
	<table border=\"0\">
	  <tr>
	    <td>Username</td>
	    <td><input name=\"user\" type=\"text\" id=\"user\" /></td>
	  </tr>
	  <tr>
	    <td>Password</td>
	    <td><input name=\"pass\" type=\"password\" id=\"pass\" /></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><input type=\"submit\" name=\"Submit\" value=\"Login\" /></td>
	  </tr>
	</table>
	</form>";
	}
}
else
{
	echo 'You are already logged in!';
}
// End of Login Page ---------
break;
// Logout --------------------
case 'logout':
if(isset($_COOKIE['php4all_user']))//Checks if the user is logged
{
	setcookie('php4all_user',$user,time()-3600*24*30);
	echo 'You are now logged out.';
}
else
{
	echo 'You must be logged in, inorder to logout.';
}
// End of Logout -------------
break;
// User CP -------------------
case 'cp':
if(isset($_COOKIE['php4all_user']))//Checks if the user is logged
{
	$user = $_COOKIE['php4all_user'];
	$r = mysql_fetch_array(mysql_query("SELECT * FROM $table where user='$user'"));//Select the user stuff
	if(!(file_exists("avatars/$r[user].gif")))//Checks if there is an avatar
	{
		$avatar = 'You do not have an avatar';
	}
	else
	{
		$avatar = "<br /><img src=\"avatars/$r[user].gif\" />";
	}// Echoes the user info
	echo "Your info:<br />
	Username: $r[user]<br />
	Password: Encrypted<br />
	Email: $r[email]<br />
	Avatar: $avatar<br />
	<a href=\"?go=members&get=avatar\">Change Avatar?</a><br />
	<a href=\"?go=members&get=edit\">Edit Profile?</a>";
}
else
{
	echo 'Sorry, you must be registered and logged in to see the Control Panel.';
}
// End of User CP ------------
break;
// Avatar ---------------------
case 'avatar':
if(isset($_COOKIE['php4all_user'])){
// Avatar script modified, i got this one from http://zulumonkey.org/?id=tutorials&page=comment&oid=115
	$username = $_COOKIE['php4all_user'];
	$maxwidth = "80";
	$maxheight = "80";
	if (isset($_GET['delete']))
	{
		$filename = 'avatars/'.$username.'.gif';
		if (file_exists($filename))
		{
		unlink($filename);
		echo "Your avatar has been deleted<br /><a href=?go=members&get=avatar>Go back</a>";
		die();
		}
	}
	
	if (isset($_POST['Submit']))
	{
		$filetype = $_FILES['avatar']['type'];
		$filetypex = substr($filetype,0,5);		
		if ($filetypex == image)
		{
			$newid = "avatars/";
			$newid .= "$username";
			$newid .= ".gif";	
			$mysock = getimagesize($_FILES['avatar']['tmp_name']);
			$imagewidth = $mysock[0];
			$imageheight = $mysock[1];
			if ($imagewidth <= "$maxwidth" && $imageheight <= "$maxheight")
			{
				if(!(copy($_FILES['avatar']['tmp_name'], $newid))) die("Cannot upload files.");
				echo "Your New Avatar Has Been Created";	
			}
			else 
			{
				echo "This avatar is to big, please change it";
			}		
		}
		else
		{
			echo "This File Is Not a Image Fool";
		}
	}
	else
	{
	echo "<form action=\"?go=members&get=avatar\" method=\"post\" enctype=\"multipart/form-data\">
	<table border=\"0\">
	  <tr>
	    <td>Avatar</td>
	    <td><input name=\"avatar\" type=\"file\" id=\"user\" /></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><input type=\"submit\" value=\"Upload\" name=\"Submit\"></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td>Max Size 80x80<br />
	      All files will be converted to gif </td>
	  </tr>
	</table>
	</form>";
	}
}
else
{
	echo 'You need to be registered and logged to see this page.';
}
// End of Avatar --------------
break;
// Edit -----------------------
case 'edit':
if($_POST['Submit'])
{
	$user = $_POST['user'];
	$email = $_POST['email'];
	$id = $_POST['id'];
	$pass = $_POST['pass'];
	$md5pass = md5($pass);
	/*
	Here we make 2 different queries because if the password is empty
	It will encode it and introduce a new password, so, only introduce the password
	If you want to change it! 
	*/
	if(empty($pass))
	{
	$ok = mysql_query("
	UPDATE `$table` SET `id` = '$id',
	`user` = '$user',
	`email` = '$email' WHERE `id` =$id LIMIT 1 ;");
	}
	else
	{
	$ok = mysql_query("
	UPDATE `$table` SET `id` = '$id',
	`user` = '$user',
	`pass` = '$md5pass',
	`email` = '$email' WHERE `id` =$id LIMIT 1 ;");
	}
	if($ok)
	{
		echo 'Your profile has been updated succesfully.';
	}
	else
	{
		echo 'Oops, Error';
	}
}
else
{
	$user = $_COOKIE['php4all_user'];
	$r = mysql_fetch_array(mysql_query("SELECT * FROM $table WHERE user='$user'"));
	echo "<form action=\"?go=members&get=edit\" method=\"post\">
		<table border=\"0\">
		  <tr>
		    <td>Username</td>
		    <td><input name=\"user\" type=\"text\" id=\"user\" value=\"$r[user]\" /></td>
		  </tr>
		   <tr>
		    <td>Password</td>
		    <td><input name=\"pass\" type=\"text\" id=\"pass\" /></td>
		  </tr>
		  <tr>
		    <td>Email</td>
		    <td><input name=\"email\" type=\"text\" id=\"email\" value=\"$r[email]\" /><input type=\"hidden\" value=\"$r[id]\" id=\"id\" name=\"id\" /></td>
		  </tr>
		  <tr>
		    <td>&nbsp;</td>
		    <td><input type=\"submit\" name=\"Submit\" value=\"Edit\" /></td>
	      </tr>
		</table><br />ONLY ADD THE PASS IF YOU WANT TO CHANGE IT.
</form>";
}
// End of Edit ----------------
break;
}
?>