<?php
class admin extends engine
{
	public function serializehmusers($page,$user)
	{
		$onpage = 10;
		$page = $page * $onpage;
		$this->message = '';
		$count = mysql_query("SELECT COUNT(*) FROM users");
		$total = mysql_fetch_assoc($count);
		$tot = $total['COUNT(*)'];
		$pages = intval(($tot- 1) / $onpage) + 1;
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("VIEW_USERS") != false)
			{
				if($user!="") 	$sql = "SELECT * FROM users WHERE username = '$user' OR real_name LIKE '$user' OR real_surname LIKE '$user' ORDER BY id DESC LIMIT $page, $onpage";
					else		$sql = "SELECT * FROM users WHERE status = '1' ORDER BY id DESC LIMIT $page, $onpage";
				$query = mysql_query($sql);
				if(@mysql_num_rows($query) != 0)
				{
					$this->message .= '<table width="100%" border="0">';
					$this->message .= '<tr class="tableheading">';
					$this->message .= '<td width="8%" align="center">'.engine::getlangvar(51).'</td>';
					$this->message .= '<td width="30%" align="center">'.engine::getlangvar(52).' / '.engine::getlangvar(399).'</td>';
					$this->message .= '<td width="22%" align="center">'.engine::getlangvar(53).'</td>';
					$this->message .= '<td width="15%" align="center">'.engine::getlangvar(55).'</td>';
					$this->message .= '</tr>';
					while($ii = mysql_fetch_assoc($query))
					{		
						if(engine::getuserrights("SUPREME_COMMANDS"))
						{
							if($ii['verified'] == "" || $ii['verified'] == 0)
							{
								$list = '<input type="checkbox" onclick="verified(\''.$ii['username'].'\',1);" style="margin-left:5px"/>';
							}
							else
							{
								$list = '<input type="checkbox" onclick="verified(\''.$ii['username'].'\',0);" checked style="margin-left:5px"/>';
							}
						}
						else
						{
							$list = '';
						}
						$this->message .= '<tr>';
						$this->message .= '<td width="5%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["id"].'</td>';
						$this->message .= '<td width="40%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;"><a href="'.ucfirst($ii["username"]).'" target="_blank">'.ucfirst($ii["username"]).'</a>'.$list.'</td>';
						$this->message .= '<td width="25%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["email"].'</td>';
						$this->message .= '<td width="15%" align="center" style="border-top: 1px dashed grey;">';
						$this->message .= '<table>';
						$this->message .= '<tr>';
						$this->message .= '<a href="administration/edit_user_account.php?id='.$ii["id"].'">'.engine::getlangvar(56).'</a> | ';
						$this->message .= '<a href="administration/edit_user_profile.php?id='.$ii["id"].'">'.engine::getlangvar(57).'</a><br>';
						$this->message .= '<a href="administration/delete_user.php?id='.$ii["id"].'">'.engine::getlangvar(58).'</a> | ';
						$this->message .= '<a href="administration/edit_user_gifts.php?id='.$ii["id"].'">'.ucfirst(engine::getlangvar(813)).'</a>';
						$this->message .= '</tr>';
						$this->message .= '</table>';
						$this->message .= '</td>';
						$this->message .= '</tr>';					
					}
					$this->message .= '</table>';
					if($user=="") 
					{
						$this->message .= '<div id="pagination">';
						for($i=0;$i<$pages;$i++)
						{
							$this->message .= '<a href="http://'.SITE_DOMAIN.'/administration/manage_users.php?page='.$i.'">'.($i+1).'</a>';
						}
						$this->message .= '</div>';
					}
				}
				else
				{
					$this->message = '<font color="red"><p><center>'.engine::getlangvar(59).'</center><p></font>';
				}
			}
		}
		return $this->message;
	}

	public function serializehmpayments($page, $squery)
	{
		$onpage = 10;
		$page = $page * $onpage;
		$this->message = '';
		$count = mysql_query("SELECT COUNT(*) FROM payments");
		$total = mysql_fetch_assoc($count);
		$tot = $total['COUNT(*)'];
		$pages = intval(($tot- 1) / $onpage) + 1;
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("VIEW_USERS") != false)
			{
				if($squery!="") 	$sql = "SELECT * FROM payments WHERE transaction_id LIKE '$squery' OR payer_email LIKE '$squery' OR user LIKE '$squery' ORDER BY id DESC LIMIT $page, $onpage";
					else			$sql = "SELECT * FROM payments ORDER BY id DESC LIMIT $page, $onpage";
				$query = mysql_query($sql);
				if(@mysql_num_rows($query) != 0)
				{
					$this->message .= '<table width="100%" border="0">';
					$this->message .= '<tr class="tableheading">';
					$this->message .= '<td width="8%" align="center"><b>'.engine::getlangvar(51).'</b></td>';
					$this->message .= '<td width="12%" align="center"><b>'.engine::getlangvar(895).'</b></td>';
					$this->message .= '<td width="20%" align="center"><b>'.engine::getlangvar(896).'</b></td>';
					$this->message .= '<td width="20%" align="center"><b>'.engine::getlangvar(897).'</b></td>';
					$this->message .= '<td width="15%" align="center"><b>'.engine::getlangvar(898).'</b></td>';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(899).'</b></td>';
					$this->message .= '<td width="15%" align="center"><b>'.engine::getlangvar(900).'</b></td>';
					$this->message .= '</tr>';
					while($ii = mysql_fetch_assoc($query))
					{		
						$this->message .= '<tr>';
						$this->message .= '<td width="8%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;">'.$ii["id"].'</td>';
						$this->message .= '<td width="12%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;">'.ucfirst($ii["type"]).'</td>';
						$this->message .= '<td width="20%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;">'.$ii["transaction_id"].'</td>';
						$this->message .= '<td width="20%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;">'.$ii["payer_email"].'</td>';
						$this->message .= '<td width="15%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;"><a href="'.$ii["user"].'" target="_blank">'.ucfirst($ii["user"]).'</a>'.$list.'</td>';
						$this->message .= '<td width="10%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;">'.$ii["quantity"].'</td>';
						$this->message .= '<td width="15%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;text-align:center;">'.date('Y-m-d H:i:s', $ii["date"]).'</td>';
						$this->message .= '</tr>';					
					}
					$this->message .= '</table>';
					if($squery=="") 
					{
						$this->message .= '<div id="pagination">';
						for($i=0;$i<$pages;$i++)
						{
							$this->message .= '<a href="http://'.SITE_DOMAIN.'/administration/payments.php?page='.$i.'">'.($i+1).'</a>';
						}
						$this->message .= '</div>';
					}
				}
				else
				{
					$this->message = '<font color="red"><p><center>'.engine::getlangvar(295).'</center><p></font>';
				}
			}
		}
		return $this->message;
	}

	public function addgift($name, $photo_file)
	{
		$this->photo = $photo_file = $_FILES["photo"];
		$file = isset($this->photo) ? $this->photo : FALSE;
		if($file)
		{
			preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $file["name"], $ext);
			$photo_crypt_name = engine::crypts(''.USER_USERNAME.''.time().'', 1, 0) . "." . $ext[1];
			$root = realpath($_SERVER["DOCUMENT_ROOT"]);
			$directory = GIFTS_IMG_PATH . '' . $photo_crypt_name;
			$query = "INSERT INTO giftstype (name, image) VALUES ('$name','$photo_crypt_name')";
			move_uploaded_file($file["tmp_name"], $directory);
			if(SESSION_STATUS != false)
			{
				if(mysql_query($query) != 0)
				{
					$_SESSION['msg'] = 6;
					header('Location: http://'.SITE_DOMAIN.'/administration/gifts.php');
					exit;
				} 
				else 
				{ 
					die('<center>'.engine::getlangvar(39).'</center>'); 
				}
			}
		}
	}

	public function serializehmtabs($in_tab)
	{
		$this->message = '<div id="topTabs" class="admintabs">';
		if($in_tab == "tab-[1]") $this->message .= '<a class="toptabs toptabsselected" '; 
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/index.php">'.engine::getlangvar(83).'</a>';
		$this->message .= '</li>';
		if($in_tab == "tab-[2]") $this->message .= '<a class="toptabs toptabsselected" '; 
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/site_settings.php">'.engine::getlangvar(84).'</a>';
		$this->message .= '</li>';
		if($in_tab == "tab-[3]") $this->message .= '<a class="toptabs toptabsselected" ';
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/ranks.php">'.engine::getlangvar(85).'</a>';
		$this->message .= '</li>';
		if($in_tab == "tab-[4]") $this->message .= '<a class="toptabs toptabsselected" '; 
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/manage_users.php">'.engine::getlangvar(86).'</a>';
		$this->message .= '</li>';
		if($in_tab == "tab-[5]") $this->message .= '<a class="toptabs toptabsselected" '; 
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/manage_posts.php">'.engine::getlangvar(87).'</a>';
		$this->message .= '</li>';
		if($in_tab == "tab-[6]") $this->message .= '<a class="toptabs toptabsselected" '; 
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/gifts.php">'.ucfirst(engine::getlangvar(813)).'</a>';
		$this->message .= '</li>';
		if($in_tab == "tab-[7]") $this->message .= '<a class="toptabs toptabsselected" '; 
		else $this->message .= '<a class="toptabs" '; 
		$this->message .= 'href="http://'.SITE_DOMAIN.'/administration/payments.php">'.engine::getlangvar(892).'</a>';
		$this->message .= '</li>';
		$this->message .= '<div class="clear"></div></div>';
		return $this->message;
	}

	public function updatepaypalsettings($enabled, $mode, $apiusername, $apipassword, $apisignature, $currencycode, $price)
	{
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("EDIT_SITE_SETTINGS") != false)
			{
				if($mode=='none' || $mode=='live' || $mode=='sandbox')
				{
					$mysqlmessage = "UPDATE paypal_settings SET ";
					$mysqlmessage .= "enabled = '$enabled', pay_mode = '$mode', apiusername = '$apiusername', apipassword = '$apipassword', apisignature = '$apisignature', currencycode = '$currencycode', price = '$price'";
					if(mysql_query($mysqlmessage) != 0)
					{
						$_SESSION['msg'] = 1;
						$this->headerin("administration/payments.php");
						exit;
					}
					else 
					{ 
						die(''.engine::getlangvar(39).''); 
					}
				}
			}
		}
	}

	public function updatesitesettings($site_name, $keywords, $description, $site_domain, $freepoints, $giftprice)
	{
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("EDIT_SITE_SETTINGS") != false)
			{
				$mysqlmessage = "UPDATE site_config SET ";
				$mysqlmessage .= "website_name = '$site_name', website_domain = '$site_domain', website_keywords = '$keywords', website_description = '$description', website_free_points = '$freepoints', website_gift_price = '$giftprice'";
				if(mysql_query($mysqlmessage) != 0)
				{
					$_SESSION['msg'] = 1;
					$this->headerin("administration/site_settings.php");
					exit;
				}
				else 
				{ 
					die(''.engine::getlangvar(39).''); 
				}
			}
		}
	}
	
	public function serializehmposts($page)
	{
		$onpage = 10;
		$page = $page * $onpage;
		$this->message = '';
		$count = mysql_query("SELECT COUNT(*) FROM posts");
		$total = mysql_fetch_assoc($count);
		$tot = $total['COUNT(*)'];
		$pages = intval(($tot- 1) / $onpage) + 1;
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("VIEW_USERS") != false)
			{
				$sql = "SELECT * FROM posts ORDER BY time DESC LIMIT $page, $onpage";
				$query = mysql_query($sql);
				if(@mysql_num_rows($query) != 0)
				{
					$this->message .= '<table border="0">';
					$this->message .= '<tr class="tableheading">';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(51).'</b></td>';
					$this->message .= '<td width="50%" align="center"><b>'.engine::getlangvar(87).'</b></td>';
					$this->message .= '<td width="35%" align="center"><b>'.engine::getlangvar(88).'</b></td>';
					$this->message .= '<td width="5%"></td>';
					$this->message .= '</tr>';
					while($ii = mysql_fetch_assoc($query))
					{
						switch($ii["anonymous"])
						{
							case 0:		$from_user = $ii["from_user"];
										break;
							case 1:		$from_user = ''.ANONYMOUS.'';
										break;
						}
						$this->message .= '<tr>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["id"].'</td>';
						if($ii["image"]!='') $img = '<img src="media/images/answers/'.$ii["image"].'" style="width:100%">';
						else $img = '';
						$this->message .= '<td width="50%" valign="top" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;"><p style="max-width:400px;word-wrap:break-word">'.$ii["post"].''.$img.'</p></td>';
						$this->message .= '<td width="35%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">';
						$this->message .= ''.engine::getlangvar(89).' <a href="http://'.SITE_DOMAIN.'/administration/manage_users.php?user='.$from_user.'">'.$from_user.'</a> '.engine::getlangvar(90).' <a href="http://'.SITE_DOMAIN.'/administration/manage_users.php?user='.$ii["to_user"].'">'.$ii["to_user"].'</a><br>';
						$this->message .= '<br>'.date("d/m/Y H:i:s",$ii["time"]).'</td>';
						$this->message .= '<td width="5%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">';
						$this->message .= '<table>';
						$this->message .= '<tr>';
						$this->message .= '<td><a href="administration/delete_post.php?id='.$ii["id"].'">'.engine::getlangvar(58).'</a></td>';
						$this->message .= '</tr>';
						$this->message .= '</table>';
						$this->message .= '</td>';
						$this->message .= '</tr>';
					}
					$this->message .= '</table>';
					$this->message .= '<div id="pagination">';
					for($i=0;$i<$pages;$i++)
					{
						$this->message .= '<a href="http://'.SITE_DOMAIN.'/administration/manage_posts.php?page='.$i.'">'.($i+1).'</a>';
					}
					$this->message .= '</div>';
				}
				else
				{
					$this->message .= '<font color="red"><p><center>'.engine::getlangvar(92).'</center><p></font>';
				}
			}
		}
		return $this->message;
	}
	
	public function deletehmpost($p_id, $A = "SELECT", $B = "DELETE FROM")
	{
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("VIEW_USERS") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if(isset($p_id) && is_numeric($p_id))
				{
					$sql = "$A * FROM posts WHERE id = '$p_id' LIMIT 1";
					$query = mysql_query($sql);
					if(@mysql_num_rows($query) != 0)
					{
						if(mysql_query("DELETE FROM posts WHERE id = '$p_id'") != 0)
						{
							if(mysql_query("DELETE FROM comments WHERE post_id = '$this->p_id'") != 0)
							{
								if(mysql_query("DELETE FROM likes WHERE post_id = '$this->p_id'") != 0)
								{
									$_SESSION['msg'] = 1;
									$this->headerin("administration/manage_posts.php");
									exit;
								}
								else 
								{ 
									$_SESSION['msg'] = 2;
									$this->headerin("administration/manage_posts.php");
									exit;
								}
							}
							else 
							{ 
								$_SESSION['msg'] = 2;
								$this->headerin("administration/manage_posts.php");
								exit;
							}
						}
						else 
						{ 
							$_SESSION['msg'] = 2;
							$this->headerin("administration/manage_posts.php");
							exit;
						}
					}
					else
					{
						$_SESSION['msg'] = 2;
						$this->headerin("administration/manage_posts.php");
						exit;
					}
				}
			}
		}
	}

	public function deletehmuser($u_id, $A = "SELECT", $B = "DELETE FROM")
	{
		$this->username = engine::getusernamebyid($u_id);
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("DELETE_USER") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if(isset($u_id) && is_numeric($u_id))
				{
					$sql = "$A * FROM users WHERE id = '$u_id' LIMIT 1";
					$query = mysql_query($sql);
					if(@mysql_num_rows($query) != 0)
					{
						if(mysql_query("$B users WHERE id = $u_id") != 0)
						{							
							if(mysql_query("$B likes WHERE to_user = '$this->username' OR user = '$this->username'") != 0)
							{
								if(mysql_query("$B comments WHERE to_user = '$this->username' OR from_user = '$this->username'") != 0)
								{
									if(mysql_query("$B users_friends WHERE username = '$this->username' OR friend = '$this->username'") != 0)
									{
										if(mysql_query("$B posts WHERE to_user = '$this->username' OR from_user = '$this->username'") != 0)
										{
											if(mysql_query("$B gifts WHERE to_user = '$this->username' OR from_user = '$this->username'") != 0)
											{
												$_SESSION['msg'] = 1;
												$this->headerin("administration/manage_users.php");
												exit;
											}
											else 
											{ 
												$_SESSION['msg'] = 2;
												$this->headerin("administration/manage_users.php");
												exit;
											}
										}
										else 
										{ 
											$_SESSION['msg'] = 2;
											$this->headerin("administration/manage_users.php");
											exit;
										}
									}
									else 
									{ 
										$_SESSION['msg'] = 2;
										$this->headerin("administration/manage_users.php");
										exit;
									}
								}
								else 
								{ 
									$_SESSION['msg'] = 2;
									$this->headerin("administration/manage_users.php");
									exit;
								}
							}
							else 
							{ 
								$_SESSION['msg'] = 2;
								$this->headerin("administration/manage_users.php");
								exit;
							}
						}
						else 
						{ 
							$_SESSION['msg'] = 2;
							$this->headerin("administration/manage_users.php");
							exit;
						}
					}
					else
					{
						$_SESSION['msg'] = 2;
						$this->headerin("administration/manage_users.php");
						exit;
					}
				}
			}
		}
	}

	public function updatehmua($id, $name, $surname, $email)
	{
		$this->id = $id;
		$this->name = $name;
		$this->surname = $surname;
		$this->email = $email;
		if(isset($this->id) && isset($this->name) && isset($this->surname) && isset($this->email))
		{
			if(SESSION_STATUS != false)
			{
				if(engine::getuserrights("EDIT_USER_ACCOUNT") != false || engine::getuserrights("SUPREME_COMMANDS"))
				{
					$mysqlmessage = "UPDATE users SET ";
					$mysqlmessage .= "real_name = '$this->name', ";
					$mysqlmessage .= "real_surname = '$this->surname', ";
					$mysqlmessage .= "email = '$this->email' ";
					$mysqlmessage .= "WHERE id = '$this->id'";
					echo $mysqlmessage;
					if(mysql_query($mysqlmessage) != 0)
					{
						$_SESSION['msg'] = 1;
						$this->headerin("administration/edit_user_account.php?id=$this->id");
						exit;
					}
					else 
					{ 
						die("mysql Error !"); 
					}
				}
			}
		}
	}

	public function updatehmup($id, $points, $website, $location, $bio, $ask)
	{
		$this->id = $id;
		$this->points = $points;
		$this->website = $website;
		$this->location = $location;
		$this->about_me = $bio;
		$this->what_ask = $ask;
		if(isset($this->id) && isset($this->website) && isset($this->location) && isset($this->about_me) && isset($this->what_ask))
		{
			if(SESSION_STATUS != false)
			{
				if(engine::getuserrights("EDIT_USER_PROFILE") != false || engine::getuserrights("SUPREME_COMMANDS"))
				{
					$mysqlmessage = "UPDATE users SET ";
					$mysqlmessage .= "website = '$this->website', ";
					$mysqlmessage .= "points = '$this->points', ";
					$mysqlmessage .= "localization = '$this->location', ";
					$mysqlmessage .= "bio = '$this->about_me', ";
					$mysqlmessage .= "user_status = '$this->what_ask' ";
					$mysqlmessage .= "WHERE id = '$this->id'";
					if(mysql_query($mysqlmessage) != 0)
					{
						$_SESSION['msg'] = 1;
						$this->headerin("administration/edit_user_profile.php?id=$this->id");
						exit;
					}
					else 
					{ 
						die(''.engine::getlangvar(39).''); 
					}
				}
			}
		}
	}

	public function serializehmmasters($modeView)
	{
		$this->message = '';
		if(SESSION_STATUS)
		{
			if(engine::getuserrights("VIEW_RANKS") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if($modeView == 2)
				{
					$rank = 2;
				}
				else if($modeView == 1)
				{
					$rank = 1;
				}
				$sql = "SELECT * FROM users WHERE rank = '$rank' ORDER BY id DESC";
				$query = mysql_query($sql);
				if(@mysql_num_rows($query) != 0)
				{
					if($modeView == 2)
					{
						$this->message .= '<table width="100%" border="0">';
						$this->message .= '<tr class="tableheading">';
						$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(51).'</b></td>';
						$this->message .= '<td width="50%" align="center"><b>'.engine::getlangvar(93).'</b></td>';
						$this->message .= '<td width="35%" align="center"><b>'.engine::getlangvar(94).'</b></td>';
						$this->message .= '<td width="35%" align="center"><b>'.engine::getlangvar(95).'</b></td>';
						$this->message .= '<td width="5%"></td>';
						$this->message .= '</tr>';
					}
					while($ii = mysql_fetch_assoc($query))
					{
						switch($ii["rank"])
						{
							case 1:		$userRank = ''.engine::getlangvar(96).'';
										break;
							case 2:		$userRank = ''.engine::getlangvar(97).'';
										break;
						}
						$this->message .= '<tr>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["id"].'</td>';	$this->message .= '</td>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["username"].'</td>';	$this->message .= '</td>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$userRank.'</td>';	$this->message .= '</td>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;"><a href="administration/ranks.php?act=remove&id='.$ii["id"].'"><font color="red"><b>('.engine::getlangvar(95).')</b></font></a></td>';	$this->message .= '</td>';
						$this->message .= '</tr>';
					}
					if($modeView == 1 && @mysql_num_rows($query) > 0)
					{
						$this->message .= '</table>';
					}
				}
				else
				{
					if($modeView == 1 && @mysql_num_rows($query) == 0)
					{
						$this->message .= '</table>';
					}
					if($modeView != 1)
					{
						$this->message .= '<font color="red"><p><center>'.engine::getlangvar(98).'</center><p></font>';
					}
				}
				
			}
		}
		return $this->message;
	}
	
	public function serializehmgifts()
	{
		$this->message = '';
		if(SESSION_STATUS)
		{
			if(engine::getuserrights("VIEW_RANKS") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				$sql = "SELECT * FROM giftstype ORDER BY name";
				$query = mysql_query($sql);
				if(@mysql_num_rows($query) != 0)
				{
					$this->message .= '<h1>'.engine::getlangvar(825).'</h1>';
					$this->message .= '<table width="100%" border="0">';
					$this->message .= '<tr>';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(51).'</b></td>';
					$this->message .= '<td width="25%" align="center"><b>'.engine::getlangvar(820).'</b></td>';
					$this->message .= '<td width="40%" align="center"><b>'.engine::getlangvar(822).'</b></td>';
					$this->message .= '<td width="25%" align="center"><b>'.engine::getlangvar(824).'</b></td>';
					$this->message .= '</tr>';
					while($ii = mysql_fetch_assoc($query))
					{
						$this->message .= '<tr>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["id"].'</td>';
						$this->message .= '<td width="25%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["name"].'</td>';
						$this->message .= '<td width="40%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;"><img src="'.GIFTS_IMG_PATH.''.$ii['image'].'" width="50"></td>';
						$this->message .= '<td width="25%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;"><a href="administration/gifts.php?act=remove&id='.$ii["id"].'&type=1"><font color="red"><b>('.engine::getlangvar(824).')</b></font></a></td>';
						$this->message .= '</tr>';
					}
					$this->message .= '</table>';
				}
			}
		}
		return $this->message;
	}

	public function getusergifts($user, $userid)
	{
		$this->message = '';
		if(SESSION_STATUS)
		{
			if(engine::getuserrights("VIEW_RANKS") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				$sql = "SELECT * FROM gifts WHERE to_user='$user' ORDER BY time DESC";
				$query = mysql_query($sql);
				if(@mysql_num_rows($query) != 0)
				{
					$this->message .= '<table width="100%" border="0">';
					$this->message .= '<tr>';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(51).'</b></td>';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(821).'</b></td>';
					$this->message .= '<td width="20%" align="center"><b>'.engine::getlangvar(834).'</b></td>';
					$this->message .= '<td width="20%" align="center"><b>'.engine::getlangvar(835).'</b></td>';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(821).'</b></td>';
					$this->message .= '<td width="10%" align="center"><b>'.engine::getlangvar(824).'</b></td>';
					$this->message .= '</tr>';
					while($ii = mysql_fetch_assoc($query))
					{
						$sqlimage = @mysql_query("SELECT image FROM giftstype WHERE id = '".$ii['gift_id']."'") or die(mysql_error());
						$readimage = @mysql_fetch_assoc($sqlimage);
						$image = $readimage["image"];
						$this->message .= '<tr>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["id"].'</td>';						
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;"><img src="'.GIFTS_IMG_PATH.''.$image.'" width="50"></td>';
						$this->message .= '<td width="20%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$ii["from_user"].'</td>';
						$this->message .= '<td width="20%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.date("d/m/Y H:i",$ii["time"]).'</td>';
						if($ii['type']==0) $type = ''.engine::getlangvar(830).'';
						if($ii['type']==1) $type = ''.engine::getlangvar(831).'';
						if($ii['type']==2) $type = ''.engine::getlangvar(832).'';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;">'.$type.'</td>';
						$this->message .= '<td width="10%" align="center" style="border-top: 1px dashed grey; padding: 10px 10px 10px 10px;"><a href="administration/edit_user_gifts.php?act=remove&giftid='.$ii["id"].'&userid='.$userid.'"><font color="red"><b>('.engine::getlangvar(824).')</b></font></a></td>';
						$this->message .= '</tr>';
					}
					$this->message .= '</table>';
				}
			}
		}
		return $this->message;
	}

	public function removehmrank($id)
	{
		$this->id = $id;
		$this->username = engine::getusernamebyid($this->id);
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("TAKE_RANK") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if(is_numeric($this->id))
				{
					if(engine::profileuserexists($this->username) != 0 && engine::getuserdata($this->username, "RANK") == 2 || engine::getuserdata($this->username, "RANK") == 1)
					{
						if(mysql_query("UPDATE users SET rank = '0' WHERE id = '$this->id'") != 0)
						{
							$_SESSION['msg'] = 2;
							$this->headerin("administration/ranks.php");
							exit;
						}
					}
					else
					{
						$_SESSION['msg'] = 4;
						$this->headerin("administration/ranks.php");
						exit;
					}
				}
			}
		}
	}
	
	public function removegift($id)
	{
		$this->id = text_filter($id, 1);
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("TAKE_RANK") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if(is_numeric($this->id))
				{
					$query = "DELETE FROM giftstype WHERE id = '$id'";
					if(mysql_query($query) != 0)
					{
						$_SESSION['msg'] = 2;
						$this->headerin("administration/gifts.php");
						exit;
					}
					else
					{
						$_SESSION['msg'] = 4;
						$this->headerin("administration/gifts.php");
						exit;
					}
				}
			}
		}
	}

	public function addusergift($id, $type, $fromuser, $touser, $text, $userid)
	{
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("TAKE_RANK") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if(is_numeric($id))
				{
					if($type==0 || $type==1 || $type==2) 
					{
						$query = "INSERT INTO gifts (to_user, from_user, gift_id, type, text, time, position_x, position_y) VALUES ('$touser', '$fromuser', $id, '$type', '$text', '".time()."', 50, 80)";
						if(mysql_query($query) != 0)
						{
							$_SESSION['msg'] = 2;
							$this->headerin("administration/edit_user_gifts.php?id=$userid");
							exit;
						}
						else
						{
							$_SESSION['msg'] = 4;
							$this->headerin("administration/edit_user_gifts.php?id=$userid");
							exit;
						}
					}
				}
			}
		}
	}

	public function addhmrank($id, $give_rank)
	{
		$this->id = $id;
		$this->rank = $give_rank;
		$this->username = engine::getusernamebyid($this->id);
		if(empty($this->id))
		{
			$_SESSION['msg'] = 5;
			$this->headerin("administration/ranks.php");
			exit;
		}
		if(SESSION_STATUS != false)
		{
			if(engine::getuserrights("GIVE_RANK") != false || engine::getuserrights("SUPREME_COMMANDS"))
			{
				if(is_numeric($this->id))
				{
					if(engine::profileuserexists($this->username) != 0 && engine::getuserdata($this->username, "RANK") == 0)
					{
						if(mysql_query("UPDATE users SET rank = '$this->rank' WHERE id = '$this->id'") != 0)
						{
							$_SESSION['msg'] = 1;
							$this->headerin("administration/ranks.php");
							exit;
						}
					}
					else
					{
						$_SESSION['msg'] = 3;
						$this->headerin("administration/ranks.php");
						exit;
					}
				}
			}
		}
	}

	public function numofrows($table)
	{
		if(isset($table))
		{
			$query = mysql_query("SELECT * FROM $table");
			$query = mysql_num_rows($query);
			if($query > 0) { return $query;	}
			else { return 0; }
		}
	}
}
?>