<?php
//User
class User {

  public function User_Login($username, $password)
  {
	  $username=mysql_real_escape_string($username);
	  $password=mysql_real_escape_string($password);
      $md5_password=md5($password);
	  // The query checking login detail
      $query=mysql_query("SELECT uid,notification_created,status FROM users WHERE (username='$username' or email='$username') and password='$md5_password'");
	 
      if(mysql_num_rows($query)==1) {
		   $row=mysql_fetch_array($query);
		   if($row['status'] == 1){
		   $notification_created=$row['notification_created'];
	       $time=time();
	       $uid=$row['uid'];
		   
		   $token = bin2hex(openssl_random_pseudo_bytes(32)) . uniqid();
		   $expires = time() + 86400 * 7;
		   setcookie('remember', $token, $expires, '/');
		   $query = mysql_query("INSERT INTO remember_tokens (uid, token, expires) VALUES ('$uid', '$token', '$expires')") or die(mysql_error());
	       // The query to select user uploads from the login user id
           $photos_query=mysql_query("SELECT id FROM user_uploads WHERE uid_fk='$uid' and group_id_fk='0'");
           $photos_count=mysql_num_rows($photos_query);
           // The query to select user messages from the messages
           $updates_query=mysql_query("SELECT msg_id FROM messages WHERE uid_fk='$uid' and group_id_fk='0'");
           $updates_count=mysql_num_rows($updates_query);
           // The query to update user login time photos count and updates count 
           mysql_query("UPDATE users SET last_login='$time',photos_count='$photos_count',updates_count='$updates_count' WHERE uid='$uid'") or die(mysql_error());
	       if(empty($notification_created)) {
			   // The query to update notification created time
	          mysql_query("UPDATE users SET notification_created='$time' WHERE uid='$uid'") or die(mysql_error());
	        }
             return $uid;
			} elseif($row['status'] == 2){
				 header("Location: banned.php");
			}
            } else {
              return false;
        }
   }
   // Logout cookie
	 public function LogoutCookie($uid)
	 {
	     $uid=mysql_real_escape_string($uid);
		 if(strlen($uid)) {
		     $query= mysql_query("DELETE FROM remember_tokens WHERE uid='$uid'")or die(mysql_error());
		     setcookie('remember', '', time() - 3600, '/');
	     }
		 return $query;
	 }
    // Check remember Token
    public function check_remember($token) 
	{
        $token = mysql_real_escape_string($token);
        $result = mysql_query("SELECT uid FROM remember_tokens WHERE token = '$token' AND expires > NOW()")  or die(mysql_error());
        $row = mysql_fetch_assoc($result);
        // If a matching token was found and not expired, return the uid
        if ($row) {
           return $row['uid'];
         } else return false;
    }
    public function User_Registration($username,$password,$email,$name) 
	{
		$username = mysql_real_escape_string($username);
		$name = mysql_real_escape_string($name);
        $email = mysql_real_escape_string($email);
        $password = mysql_real_escape_string($password);
        $md5_password = md5($password);
		// The querty to checking register detail like password,email,username and fullname
        $q = mysql_query("SELECT uid FROM users WHERE username='$username' or email='$email' or name='$name' ");
        if(mysql_num_rows($q)==0) {
            $time=time();
			// If all registered detail is correct then instert the user details from the users table
            $query = mysql_query("INSERT INTO users(username,password,email,name,last_login,hide_following,hide_followers,join_date)VALUES('$username','$md5_password','$email','$name','$time','her','her',NOW())");
            $sql   = mysql_query("SELECT uid FROM users WHERE username='$username'");
            $row   = mysql_fetch_array($sql);
            $uid   = $row['uid'];
			// The query insterting the friends status
			// New registered user status is me from the friends table
            $friend_query = mysql_query("INSERT INTO friends(friend_one,friend_two,role)VALUES('$uid','$uid','me')");
            // Update the last login time and notificatino time
			mysql_query("UPDATE users SET last_login='$time',notification_created='$time' WHERE uid='$uid'") or die(mysql_error());
            return $uid ;
    

         } else {
               return false;
              }
     }
    
public function Forgot($username,$forgot)
{
	$username=mysql_real_escape_string($username);
	// The query to checking correct username or email
	$query=mysql_query("SELECT uid FROM users WHERE (username='$username' or email='$username') AND status='1'");
	if(mysql_num_rows($query)==1) {
		// If the username correct
		$sql=mysql_query("SELECT email FROM users WHERE username='$username'");
		$row=mysql_fetch_array($sql);
        $email=$row['email'];
		// Send activation link from the email
	    $active_code=md5($forgot.$email.time());
		// The query updating to the forgot code from the users table
	    mysql_query("UPDATE users SET forgot_code='$active_code' WHERE username='$username' or email='$username'");
        return $active_code;
	  } else {
           return false;
           }
 }
public function Activecode($activecode)
     {
     $activecode=mysql_real_escape_string($activecode);
	 // The query is selected activate code
	 $query=mysql_query("SELECT uid FROM users WHERE  forgot_code='$activecode' AND status='1'");
     if(mysql_num_rows($query)==1)
     {
	 return true;
	 }
	 }
	 
/* User New Password Change*/
public function ChangePassword($newPassword,$activecode)
{
	// If user forgot password
	$newPassword=md5(mysql_real_escape_string($newPassword)); // Then check the new passwords
	$activecode=mysql_real_escape_string($activecode); // Activatecode for changing
	// Update a password old to new
    $query=mysql_query("UPDATE users SET password='$newPassword' WHERE forgot_code='$activecode'");
	// Update the forgot code
    mysql_query("UPDATE users SET forgot_code='' WHERE forgot_code='$activecode'");
    return true;
}

public function Get_Configurations()
{
	$query=mysql_query("SELECT forgot FROM configuration WHERE con_id='1' ")or die(mysql_error());
    $data=mysql_fetch_array($query);
	return $data;
}
 // Banned User
     public function Check_Banned($uid)
     {
         $uid = mysql_real_escape_string($uid);
         $query = mysql_query("SELECT uid FROM users WHERE uid='$uid' AND status = 2");
         if (mysql_num_rows($query) == 1) {
         // User is banned! redirect or show error
          header("Location: banned.php");
         exit();
      }
         return false;
    }	
}
?>
