<?php
class Mat_Updates {
	public $perpage = 15; /*Uploads perpage*/
	
    //Configurations
    public function Get_Configurations()
	{
		//The query to select configuration notification perpage, upload image size profile image with an ext.
		$query=mysql_query("SELECT notificationPerPage,uploadImage,profileWidth,friendsWidgetPerPage,adminPerPage,annoncement,scriptName,scriptTitle,scriptLogo FROM configuration WHERE con_id='1' ")or die(mysql_error());
        $data=mysql_fetch_array($query);
        return $data;
     }
     //Username Check for Oauth users
    public function usernameCheck($uid)
	{
		$uid=mysql_real_escape_string($uid);
		//This query to select a username from the users table
        $query=mysql_query("SELECT username FROM users WHERE uid='$uid'");
		 
		if(mysql_num_rows($query)==1) {
			 $row=mysql_fetch_array($query);
             return $row['username'];
			 } else {
				 return false;
			 }
     }
	 
      //Username Update
     public function usernameUpdate($uid, $username)
	 {
		 $uid=mysql_real_escape_string($uid);
         $username=mysql_real_escape_string($username);
		 $query=mysql_query("SELECT uid FROM users WHERE username='$username'");
		 
		 if(mysql_num_rows($query)==0) {
			 
			 //  Prepare the statement
			 mysql_query("UPDATE users SET username='$username' WHERE uid = '$uid'");
			 mysql_query("INSERT INTO friends(friend_one,friend_two,role)VALUES('$uid','$uid','me')");//Friend Page Entry
			 return $uid;
			 } else {
				 return false;
			 }
      }
	  
       /* Intro Tour Check */
      public function Announce($uid)
      {
          $query=mysql_query("UPDATE users SET announcement_readed='0' WHERE uid='$uid' ")or die(mysql_error());
          $data=mysql_fetch_array($query);
	      return $data;
       }
     /*User Login Check*/
     public function Login_Check($value, $type)
	 {
		 $username_email=mysql_real_escape_string($value);
		 if($type) {
			 //This is if user login with username
			 $query=mysql_query("SELECT uid FROM users WHERE username='$username_email' ");
			 } else {
			     //This is if user login with email
			     $query=mysql_query("SELECT uid FROM users WHERE email='$username_email' ");
			 }
			   return mysql_num_rows($query);
	 }
      /* User ID Valid Check*/
     public function User_ID($username)
	 {
		 $username=mysql_real_escape_string($username);
		 //This is user id Cheking for valid
		 $query=mysql_query("SELECT uid FROM users WHERE username='$username' AND status='1'");
		 if(mysql_num_rows($query)==1) {
	     //This is if user id valid is ok then return user id
		 $row=mysql_fetch_array($query);
		 return $row['uid'];
		 } else {
			 return false;
		 }
     }
	 // Banned User
     public function User_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;
    }
    /*User Folower Count*/
    public function Follower_Count($uid)
	{
		$uid = mysql_real_escape_string($uid);
		//This is checking user follower count
		//We use COUNT(*)-1 because of you are not your friend. 
		$q = mysql_query("SELECT COUNT(*)-1 AS ufollowers FROM friends WHERE friend_two = '$uid'");
		$row = mysql_fetch_array($q);
		if ($row) {
			//This is if user follower is not empty show how many follower
			return $row['ufollowers'];
			} 
			//If user not have follower then show 0
			else return 0;
     }
    /*User Folower Count*/
    public function Following_Count($uid)
	{
		$uid = mysql_real_escape_string($uid);
		//This is checking user follower count
		//We use COUNT(*)-1 because of you are not your friend. 
		$q = mysql_query("SELECT COUNT(*)-1 AS ufollowers FROM friends WHERE friend_one = '$uid'");
		$row = mysql_fetch_array($q);
		if ($row) {
			//This is if user follower is not empty show how many follower
			return $row['ufollowers'];
			} 
			//If user not have follower then show 0
			else return 0;
     }
 
    /*User Uploaded Image Count*/
    public function User_Image_Count($uid)
	{
		$uid = mysql_real_escape_string($uid);
        //Calculate how many Image user uploads
        $q = mysql_query("SELECT COUNT(*) AS upload_count FROM user_uploads WHERE uid_fk = '$uid'");
		$row = mysql_fetch_array($q);
		if ($row) {
			//If user have uploaded image then show how many image indicate this user
			return $row['upload_count'];
			} 
			// If user not have upladed image then show 0
			else return 0;
	 }
	 /*Count Secret 0*/
	public function Post_Secret_Zero($uid)
	{
	   $uid= mysql_real_escape_string($uid); 
       $uploads= ''; 
       $query = mysql_query("SELECT COUNT(uploads) num_rows FROM messages WHERE TRIM(IFNULL(uploads, '')) != '' AND secret_option = '1' AND uid_fk = '$uid'") or die(mysql_error()); 
       $row = mysql_fetch_array($query); 
       if($row) { 
           return $row['num_rows']; 
         } 
          else return 0; 
	}
     /* User Details*/
    public function User_Details($uid)
	{
		$username=mysql_real_escape_string($uid);
		// The query to select for user details
		$query=mysql_query("SELECT 
		uid,
		email,
		username,
		notification_created,
		last_login,
		join_date,
		u_lang,
		photos_count,
		hide_photos,
		verified,
		profile_pic,
		mini_profile_pic,
		mini_profile_pic_status,
		profile_bg_position,
		profile_pic_status,
		conversation_count,
		updates_count,
		announcement_readed,
		friend_count,
		hide_following,
		hide_followers,
		s_fb,
		s_tw,
		s_tumb,
		s_yout,
		s_inst,
		s_gp,
		gender,
		b_day,
		u_month,
		u_year,
		hide_maynow_people,
		user_case,
		user_city,
		status,
		name AS full_name  
		FROM 
		users WHERE uid='$uid' AND status='1'") or die(mysql_error());
        $data=mysql_fetch_array($query);
        return $data;    
    }

	/*User Profile Picture */
	public function User_Profilepic($uid, $base_url)
	{
		$uid=mysql_real_escape_string($uid);
	   //This is checking user uploded profile image or not
	    $query = mysql_query("SELECT email,profile_pic,profile_pic_status FROM `users` WHERE uid='$uid'") or die(mysql_error());
	    $row=mysql_fetch_array($query);
		if($row['profile_pic_status']) { // Checking profile image status 1 or 0 
		if(!empty($row['profile_pic'])) {  // If profile image is not empty then show the profile image from user profile image folder
			 $profile_pic_path=$base_url.'user_profile_uploads/';
			 $data= $profile_pic_path.$row['profile_pic'];
			 return $data;
		   } else { 
		      // If user not uploaded an profile image then show default image from mat_icons folder
		      // If you want to change default image for profile image then you can fined it from mat_cions folder in default.jpg
			   $data=$base_url."mat_icons/default.jpg";
			   return $data;
		   }
		   } else { 
		       // If the profile imag status is 0 then show the user gravatar image from the data link
		      /*Gravator Image*/
			   $email=$row['email']; // Gravatar email
			   $lowercase = strtolower($email); // Checking email for gravatar image 
			   $imagecode = md5($lowercase);
			   $data="http://www.gravatar.com/avatar/".$imagecode."?d=mm&s=230"; // Gravatar image link for user profile image
			   return $data;
		   }
	}
	/*User Profile Picture */
	public function User_MiniProfilepic($uid, $base_url)
	{
		$uid=mysql_real_escape_string($uid);
	   //This is checking user uploded profile image or not
	    $query = mysql_query("SELECT email,mini_profile_pic,mini_profile_pic_status FROM `users` WHERE uid='$uid'") or die(mysql_error());
	    $row=mysql_fetch_array($query);
		if($row['mini_profile_pic_status']) { // Checking profile image status 1 or 0 
		if(!empty($row['mini_profile_pic'])) {  // If profile image is not empty then show the profile image from user profile image folder
			 $profile_pic_path=$base_url.'user_profile_uploads/';
			 $data= $profile_pic_path.$row['mini_profile_pic'];
			 return $data;
		   } else { 
		      // If user not uploaded an profile image then show default image from mat_icons folder
		      // If you want to change default image for profile image then you can fined it from mat_cions folder in default.jpg
			   $data=$base_url."mat_icons/default.jpg";
			   return $data;
		   }
		   } else { 
		       // If the profile imag status is 0 then show the user gravatar image from the data link
		      /*Gravator Image*/
			   $email=$row['email']; // Gravatar email
			   $lowercase = strtolower($email); // Checking email for gravatar image 
			   $imagecode = md5($lowercase);
			   $data="http://www.gravatar.com/avatar/".$imagecode."?d=mm&s=230"; // Gravatar image link for user profile image
			   return $data;
		   }
	}
	/*User profile image setting*/
	public function Update_Settings()
	{
		$uid=mysql_real_escape_string($uid);
		if($actual_image_name) {
			// Prepare the statement
			mysql_query("UPDATE users SET profile_pic='$actual_image_name' WHERE uid='$uid'");
			//Insert the porfile image from user_profile_images table
			mysql_query("insert into user_profile_images (profile_image_path,uid_fk)values('$actual_image_name' ,'$uid')") or die(mysql_error());
			}
			return true;
		}
	
	/*User Full Name Settings*/
	public function Update_FullName_Settings($full_name, $uid)
	{
		if(strlen($full_name)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET name='$full_name' WHERE uid='$uid'");
			return true;
			}
	 }
	
	/*User Join_Date */
	public function User_Join_Date($uid,$base_url) {
	   $uid=mysql_real_escape_string($uid);
	   $query = mysql_query("SELECT email,join_date FROM `users` WHERE uid='$uid'") or die(mysql_error());
	   $row=mysql_fetch_array($query);
	   if($row['join_date']){
		  $data=$row['join_date'];
		  return $data;
		  }
	}
	
	/*Profile Image Upload*/
	public function Profile_Image_Upload($uid, $actual_image_name)
	{
		//Prepare the statement
	   mysql_query("UPDATE users SET profile_pic='$actual_image_name',profile_pic_status='1' WHERE uid='$uid'") or die(mysql_error());
	   // Insert an image if user selected correct formating image
	   mysql_query("INSERT INTO user_profile_images (profile_image_path,uid_fk) VALUES ('$actual_image_name','$uid')") or die(mysql_error());
	   //Check the user id for profile_pic from the user table then update it
	   $query = mysql_query("SELECT uid,profile_pic FROM users WHERE uid='$uid'") or die(mysql_error());
	   $result = mysql_fetch_array($query);
	   return $result;
	}
	/*Mini Profile Image Upload*/
	public function Mini_Profile_Image_Upload($uid, $actual_image_name)
	{
		//Prepare the statement
	   mysql_query("UPDATE users SET mini_profile_pic='$actual_image_name',mini_profile_pic_status='1' WHERE uid='$uid'") or die(mysql_error());
	   // Insert an image if user selected correct formating image
	   mysql_query("INSERT INTO user_profile_images (profile_image_path,uid_fk) VALUES ('$actual_image_name','$uid')") or die(mysql_error());
	   //Check the user id for profile_pic from the user table then update it
	   $query = mysql_query("SELECT uid,mini_profile_pic FROM users WHERE uid='$uid'") or die(mysql_error());
	   $result = mysql_fetch_array($query);
	   return $result;
	}
	
	/*User Full Name*/
	public function UserFullName($username)
	{
		$username=mysql_real_escape_string($username);
	    $query = mysql_query("SELECT name FROM `users` WHERE username='$username'") or die(mysql_error());
	    $data=mysql_fetch_array($query);
	    if($data['name']){  // If name is updated
		   $name=$data['name'];
		   $str_length = strlen($name);    
		   if($str_length > 25){  // If the name is greater than 25 characters 
			  $name= substr($name, 0, 25) . "..." ; //Show points after 25 characters
			}
		    return ucfirst($name); //Show capitalize the first letter of the name
		    } else {
			   return $username; // If name is not updated then show username
		    }
	}
	
	/*User Search  */ 	
	public function User_Search($searchword)
	{
		$q=mysql_real_escape_string($_POST['searchword']);
	    //Check for the username word.
	    $query=mysql_query("select username,uid from users where username like '%$q%' or name like '%$q%' order by uid LIMIT 5");
	    while($row=mysql_fetch_array($query)) {
			  $data[]=$row;
			 }
			  if(!empty($data)) {
					 // Store the result into array
					 return $data;
				  } 
	}
	/*Profile image for following and follower page*/
	public function User_Lister($uid, $base_url)
	{
		$uid=mysql_real_escape_string($uid);
	    // Select the user id for profile image
	    $query = mysql_query("SELECT email,profile_pic FROM `users` WHERE uid='$uid'") or die(mysql_error());
	    $row=mysql_fetch_array($query);
	    if(!empty($row['profile_pic'])){// If user uploaded a profile image then
		   $profile_pic_path=$base_url.'user_profile_uploads/'; // Show the image from user porfile upload folder
		   $data= $profile_pic_path.$row['profile_pic'];
		   return $data;
		  } else {
		      $data=$base_url."mat_icons/default.jpg"; // If user not have profile image then show the default profile image.
		      return $data;
		  }
	}
	
	
	/*Status User Check*/
	public function Status_User($msgid) {
	   $msgid=mysql_real_escape_string($msgid);
	   $query=mysql_query("SELECT username FROM messages M, users U WHERE  M.uid_fk=U.uid and M.msg_id='$msgid' AND U.status='1'") or die(mysql_error());
	   if(mysql_num_rows($query)>0){
		  $row=mysql_fetch_array($query);
		  return $row['username'];
		}else{
		 return false;
		}
	}
	
	/* Status Msg Id Check*/
	public function Status_ID($msgid) {
	   $msgid=mysql_real_escape_string($msgid);
	   $query=mysql_query("SELECT msg_id FROM messages M, users U WHERE  M.uid_fk=U.uid and M.msg_id='$msgid' AND U.status='1'") or die(mysql_error());
	   if(mysql_num_rows($query)>0){
		  $row=mysql_fetch_array($query);
		  return $row['msg_id'];
		}else{
		return false;
		}
	}
	
	
	/*Like Message*/
	public function Like($msg_id, $uid)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    $uid=mysql_real_escape_string($uid);
	    // Select the message like_id from message_like table
	    $q=mysql_query("SELECT like_id FROM message_like WHERE  uid_fk='$uid' and msg_id_fk='$msg_id' ");
	    if(mysql_num_rows($q)==0) {// If user not liked the message before
		   $q=mysql_query("SELECT uid_fk FROM messages WHERE msg_id='$msg_id'");
		   $r=mysql_fetch_array($q);
		   $ouid=$r['uid_fk'];
		   $time=time();
		   // then insert the like from message like table
		   $query=mysql_query("INSERT INTO message_like (msg_id_fk,uid_fk,ouid_fk,created) VALUES('$msg_id','$uid','$ouid','$time')");
		   // Prepare the statement
		   $q=mysql_query("UPDATE messages SET like_count=like_count+1 WHERE msg_id='$msg_id'") or die(mysql_error());
		   $g=mysql_query("SELECT like_count FROM messages WHERE msg_id='$msg_id'");
		   $d=mysql_fetch_array($g);
		   return $d['like_count'];
		} else {
			return false;
		}
	}
	
	/*Comment Like */
	public function Comment_Like($com_id, $uid)
	{
		$com_id=mysql_real_escape_string($com_id);
	    $uid=mysql_real_escape_string($uid);
	    //Select the message comment like id from comment_like table
	    $q=mysql_query("SELECT clike_id FROM comment_like WHERE  uid_fk='$uid' and com_id_fk='$com_id' ");
	    if(mysql_num_rows($q)==0) {// If user not liked this comment before
		   $q=mysql_query("SELECT M.uid_fk FROM messages M, comments C WHERE M.msg_id=C.msg_id_fk AND C.com_id='$com_id'");
		   $r=mysql_fetch_array($q);
		   $ouid=$r['uid_fk'];
		   $time=time();
		   // Then insert the like from the comment_like table
		   $query=mysql_query("INSERT INTO comment_like (com_id_fk,uid_fk,ouid_fk,created) VALUES('$com_id','$uid','$ouid','$time')");
		   // Prepare the statement
		   $q=mysql_query("UPDATE comments SET like_count=like_count+1 WHERE com_id='$com_id'") or die(mysql_error());
		   $g=mysql_query("SELECT like_count FROM comments WHERE com_id='$com_id'");
		   $d=mysql_fetch_array($g);
		   return $d['like_count'];
		} else {
			return false;
		}
	}
	/*Comment Like Check*/
	public function Comment_Like_Check($com_id, $uid)
	{
		$com_id=mysql_real_escape_string($com_id);
	    $uid=mysql_real_escape_string($uid);
	    $q=mysql_query("SELECT clike_id FROM comment_like WHERE  uid_fk='$uid' and com_id_fk='$com_id' ");
	    if(mysql_num_rows($q)==0){ // If the user liked the comment id before then show the liked button
		   return true;
		 } else {//If the user not liked the comment id before then show normal button
		   return false;
		 }
	}
	
	/*Comment Unlike Check*/
	public function Comment_Unlike($com_id, $uid)
	{
		$com_id=mysql_real_escape_string($com_id);
	    $uid=mysql_real_escape_string($uid);
	    //Select the comment like id from comment like table
	    $q=mysql_query("SELECT clike_id FROM comment_like WHERE uid_fk='$uid' and com_id_fk='$com_id' ");
	    if(mysql_num_rows($q)>0) {//If user liked the comment before
		    // Then user see the liked button
		    // If user click the unlike button
		    $query=mysql_query("DELETE FROM comment_like WHERE com_id_fk='$com_id' and uid_fk='$uid'");
		    //Prepare the statement
		    $q=mysql_query("UPDATE comments SET like_count=like_count-1 WHERE com_id='$com_id'") or die(mysql_error());
		    // Also the comments like count 1 deprecent
		    $g=mysql_query("SELECT like_count FROM comments WHERE com_id='$com_id'");
		    $d=mysql_fetch_array($g);
		    return $d['like_count'];
		  } else {
			  return false;
		 }
	}
	/*Like Users*/
	public function Like_Users($msg_id)
	{
		// This is showing which user liked the updated message
	    $msg_id=mysql_real_escape_string($msg_id); 
	    $q=mysql_query("SELECT like_id FROM message_like WHERE msg_id_fk='$msg_id' ");
	    if(mysql_num_rows($q)>0) { // If any user liked the updated message
		    $query=mysql_query("SELECT U.username FROM message_like M, users U WHERE U.uid=M.uid_fk AND M.msg_id_fk='$msg_id' LIMIT 3"); 
		    // Then showing the username in liked user. Like: abc,def liked this
		    while($row=mysql_fetch_array($query)) {
				 $data[]=$row;
			   }
		   return $data;
		}
	}
	/*Share Message*/
	public function Share_Msg($msg_id)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    // Check the shared message id.
	    $query=mysql_query("SELECT uid_fk FROM message_share WHERE msg_id_fk='$msg_id' ORDER BY share_id DESC  LIMIT 1 ") or die(mysql_error());
	    $data=mysql_fetch_array($query);
	    return $data['uid_fk']; // return the shared user id from data
	}
	/*Share Validate Check*/
	public function Share_Check($msg_id, $uid)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    $uid=mysql_real_escape_string($uid);
	    //Check that users share the message.
	     $q=mysql_query("SELECT share_id FROM message_share WHERE  uid_fk='$uid' and msg_id_fk='$msg_id' ") or die(mysql_error());
	    if(mysql_num_rows($q)==0) {
		   return true;
		 } else {
			 return false;
		 }
	}
	/* Share Message*/
	public function Share($msg_id, $uid)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    $uid=mysql_real_escape_string($uid);
		
	    $q=mysql_query("SELECT share_id FROM message_share WHERE  uid_fk='$uid' and msg_id_fk='$msg_id' ") or die(mysql_error());
	    if(mysql_num_rows($q)==0) {//Check the message is not shared.
		   // If Message id is not shared before 
		   $q=mysql_query("SELECT uid_fk FROM messages WHERE msg_id='$msg_id'") or die(mysql_error());
		   $r=mysql_fetch_array($q);
		   $ouid=$r['uid_fk'];
		   $time=time();
		   // Insert the Message from message share table 
		   $query=mysql_query("INSERT INTO message_share (msg_id_fk,uid_fk,ouid_fk,created) VALUES('$msg_id','$uid','$ouid','$time')") or die(mysql_error());
		   //Prepare the statement
		   $q=mysql_query("UPDATE messages SET share_count=share_count+1 WHERE msg_id='$msg_id'") or die(mysql_error());
		   return true;
		 } else {
		  return false;
		 }
	}
	/* Unshare*/
	public function Unshare($msg_id, $uid)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    $uid=mysql_real_escape_string($uid);
	
	    $q=mysql_query("SELECT share_id FROM message_share WHERE  uid_fk='$uid' and msg_id_fk='$msg_id' ") or die(mysql_error());
	    if(mysql_num_rows($q)>0){// Check the message is shared
		   // If message id is shared before 
		   // then click the unshare button delete the message id from the message share table
		    $query=mysql_query("DELETE FROM message_share WHERE msg_id_fk='$msg_id' and uid_fk='$uid'") or die(mysql_error());
		  //Prepare the statement
		    $q=mysql_query("UPDATE messages SET share_count=share_count-1 WHERE msg_id='$msg_id'") or die(mysql_error());
		    return true;
		    } else {
		     return false;
		 }
	}
	/*Like Validate Check*/
	public function Like_Check($msg_id, $uid)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    $uid=mysql_real_escape_string($uid);
	    //Check that users Liked the message.
	     $q=mysql_query("SELECT like_id FROM message_like WHERE  uid_fk='$uid' and msg_id_fk='$msg_id'");
	    // Output the result
	    if(mysql_num_rows($q)==0) {
		    return true;
		 } else {
			 return false;
		 }
	}
	
	/*Unlike*/
	public function Unlike($msg_id, $uid)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    $uid=mysql_real_escape_string($uid);
	     $q=mysql_query("SELECT like_id FROM message_like WHERE uid_fk='$uid' and msg_id_fk='$msg_id'");
	    if(mysql_num_rows($q)>0) {
		    //If user liked the message id then showing unlike button
		    //If user clicked the unlike button then delete the message id and uid from the message like table
		    $query=mysql_query("DELETE FROM message_like WHERE msg_id_fk='$msg_id' and uid_fk='$uid'");
		    //Prepare the statement
		    $q=mysql_query("UPDATE messages SET like_count=like_count-1 WHERE msg_id='$msg_id'") or die(mysql_error());
		    $g=mysql_query("SELECT like_count FROM messages WHERE msg_id='$msg_id'");
		    $d=mysql_fetch_array($g);
		    return $d['like_count'];
		   } else {
			   return false;
		  }
	}
	/* Status Updates */
	public function Status_Update($msgid)
	{
		$msgid = mysql_real_escape_string($msgid);
	    // Show the status update details. Like tex, like count , comment count ext.
	     $query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message,M.video,M.secret_option, M.created,M.like_count,M.comment_count,M.share_count,U.username,M.uploads 
	     FROM messages M, users U  
	     WHERE U.status='1' 
	     AND M.uid_fk=U.uid and M.msg_id='$msgid'") or die(mysql_error());
	     while($row=mysql_fetch_array($query)) {
			   $data[]=$row;
		     }
		     return $data;
	}
	/*This is for Profile status update*/ 	
	public function Updates($uid, $lastid)
	{
		/* More Button*/
	    $morequery="";
	    if($lastid) {//If posted any message. So the lastid is 0 from the posts.php file
		   //If the last message id greater then the first message id then continue with $morequery=" and M.msg_id<'".$lastid."' ";
		   $morequery=" and M.msg_id<'".$lastid."' ";
		 }
		 // Create variable $data with a null value before the query
		  $data = null;
		 /* More Button End*/
		 $v=mysql_query("SELECT share_id FROM message_share") or die(mysql_error());
		 if(mysql_num_rows($v)) {
			 //The profile updates relation here
			 //Message and message_share table relationship
			 //Checking the message shared
			 //Checking the message for the profile
			 //Showing the message shared details
			 $query=mysql_query("(SELECT DISTINCT 
			 M.msg_id, 
			 M.uid_fk, 
			 M.message,
			 M.video, 
			 S.created, 
			 M.like_count,
			 M.secret_option,
			 M.comment_count,
			 M.share_count,
			 U.last_login,
			 U.username,
			 M.uploads, 
			 S.uid_fk as share_uid,
			 S.ouid_fk as share_ouid 
			 FROM 
			 messages M, users U, message_share S 
			 WHERE 
			 M.uid_fk=U.uid AND
			 U.status='1' AND 
			 S.msg_id_fk=M.msg_id AND
			 S.uid_fk='$uid'
			 $morequery group by msg_id)
			 UNION
			 (SELECT M.msg_id, 
			 M.uid_fk, 
			 M.message,
			 M.video, 
			 M.created,
			 M.like_count,
			 M.secret_option,
			 M.comment_count,
			 M.share_count,
			 U.last_login,
			 U.username,
			 M.uploads, '0' AS share_uid, '0' as share_ouid  FROM messages M, users U 
			 WHERE
			 U.status='1' AND 
			 M.uid_fk=U.uid and 
			 M.uid_fk='$uid'  $morequery group by msg_id ) 
			 order by created desc limit " .$this->perpage) or die(mysql_error());
		   } else {
			   //If the update has not shared then show only user messages and messages details from the message table
			   $query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message,M.video, M.created,M.like_count,M.secret_option,M.comment_count,U.last_login,U.username,M.uploads, '0' AS share_uid, '0' as share_ouid FROM messages M, users U  WHERE U.status='1' AND M.uid_fk=U.uid and M.uid_fk='$uid' $morequery order by M.msg_id desc limit " .$this->perpage) or die(mysql_error());
		   }
		   //Store the result
		   while($row=mysql_fetch_array($query)) {
				 $data[]=$row;
		       }
		       return $data;
	}
	/* Total Updates   */	
	public function Total_Updates($uid)
	{
		$v=mysql_query("SELECT share_id FROM message_share") or die(mysql_error());
	    if(mysql_num_rows($v)) {
		   //The query to select friend messages
		   $query=mysql_query("(SELECT DISTINCT 
		   M.msg_id, M.uid_fk, M.message,M.video,
		   M.created, 
		   M.like_count,
		   M.secret_option,
		   M.comment_count,
		   M.share_count,
		   U.last_login,
		   U.username,
		   M.uploads, 
		   S.uid_fk 
		   AS share_uid,S.ouid_fk 
		   AS share_ouid 
		   FROM 
		   messages M, 
		   users U, 
		   message_share S 
		   WHERE 
		   M.uid_fk=U.uid 
		   AND U.status='1' 
		   AND S.msg_id_fk=M.msg_id 
		   AND S.uid_fk='$uid' group by msg_id)
		   UNION
		   (SELECT 
		   M.msg_id, 
		   M.uid_fk, 
		   M.message,
		   M.video, 
		   M.created,
		   M.like_count,
		   M.secret_option,
		   M.comment_count,
		   M.share_count,
		   U.last_login,
		   U.username,
		   M.uploads, 
		   '0' AS share_uid, '0' AS share_ouid  FROM messages M, users U 
		   WHERE
		   U.status='1' AND 
		   M.uid_fk=U.uid and 
		   M.uid_fk='$uid' group by msg_id ) 
		   order by created desc ");
		  } else {
			  $query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message,M.video, M.created,M.like_count,M.secret_option,M.comment_count,U.username,U.last_login,M.uploads, '0' AS share_uid, '0' AS share_ouid FROM messages M, users U  WHERE U.status='1' AND M.uid_fk=U.uid and M.uid_fk='$uid'") or die(mysql_error());
		}
		 $data=mysql_num_rows($query);
		 return $data;
	}
	/* Friends_Updates   */	
	public function Friends_Updates($uid, $lastid)
	{
		/* More Button*/
	    $morequery="";
	    if($lastid) {
		    //build up the query
		    $morequery=" and M.msg_id<'".$lastid."' ";
		 }
                     $data = null;
		  /*More Button End*/
		  $v=mysql_query("SELECT share_id FROM message_share") or die(mysql_error());
		  if(mysql_num_rows($v)) {
			 //The query to select friend messages
			 $query=mysql_query("(SELECT DISTINCT 
       M.msg_id,
       M.uid_fk,
       M.message,
       M.video,
       M.share_count,
       S.created,
       M.like_count,
       M.secret_option,
       M.comment_count,
       U.username,
       U.last_login,
       M.uploads,
       S.uid_fk AS share_uid,
       S.ouid_fk AS share_ouid
       FROM 
       messages M
       JOIN message_share S ON M.msg_id = S.msg_id_fk
	   JOIN friends F ON F.friend_two = S.uid_fk OR S.uid_fk = '$uid'
       JOIN users U ON S.ouid_fk = U.uid
       WHERE 
       F.friend_one='$uid'
       AND U.uid != F.friend_one 
       AND U.status='1'
       AND F.role='fri')
       UNION
       (SELECT DISTINCT 
       M.msg_id,
       M.uid_fk,
       M.message,
       M.video,
       M.share_count,
       M.created,
       M.like_count,
       M.secret_option,
       M.comment_count,
       U.username,
       U.last_login,
       M.uploads,
       '0' AS share_uid,
       '0' AS share_ouid
       FROM 
        messages M 
        JOIN users U ON M.uid_fk = U.uid
        JOIN friends F ON M.uid_fk = F.friend_two
       WHERE 
        F.friend_one='$uid'
        AND U.status='1'
    $morequery ) order by created desc limit " .$this->perpage) or die(mysql_error()); 
			 } else {	   
			    $query = mysql_query("SELECT DISTINCT M.msg_id, M.uid_fk, M.message,M.video,M.like_count, M.created,M.secret_option,U.username,U.last_login,M.uploads FROM messages M, users U, friends F  WHERE U.status='1' AND M.uid_fk=U.uid AND  M.uid_fk = F.friend_two AND F.friend_one='$uid' $morequery order by M.msg_id desc limit " .$this->perpage) or die(mysql_error());
			 }
			  //Store the result
			  while($row=mysql_fetch_array($query)) {
				  // Store the result into array
				  $data[]=$row;
				  }
				   if(!empty($data)) {
					 // Store the result into array
					 return $data;
				  }  
	}
	
	/*Total Friends Updates */  	
	public function Total_Friends_Updates($uid)
	{
		$v=mysql_query("SELECT share_id FROM message_share") or die(mysql_error()); 
	    if(mysql_num_rows($v)) {
		    //The query to select total friend messages
		    $query=mysql_query("(SELECT DISTINCT 
       M.msg_id,
       M.uid_fk,
       M.message,
       M.video,
       M.share_count,
       S.created,
       M.like_count,
       M.secret_option,
       M.comment_count,
       U.username,
       U.last_login,
       M.uploads,
       S.uid_fk AS share_uid,
       S.ouid_fk AS share_ouid
       FROM 
       messages M
       JOIN message_share S ON M.msg_id = S.msg_id_fk
	   JOIN friends F ON F.friend_two = S.uid_fk OR S.uid_fk = '$uid'
       JOIN users U ON S.ouid_fk = U.uid
       WHERE 
       F.friend_one='$uid'
       AND U.uid != F.friend_one 
       AND U.status='1'
       AND F.role='fri')
       UNION
       (SELECT DISTINCT 
       M.msg_id,
       M.uid_fk,
       M.message,
       M.video,
       M.share_count,
       M.created,
       M.like_count,
       M.secret_option,
       M.comment_count,
       U.username,
       U.last_login,
       M.uploads,
       '0' AS share_uid,
       '0' AS share_ouid
       FROM 
        messages M 
        JOIN users U ON M.uid_fk = U.uid
        JOIN friends F ON M.uid_fk = F.friend_two
       WHERE 
        F.friend_one='$uid'
        AND U.status='1'
    ) order by created desc ") or die(mysql_error()); 
		    } else {	   
			    $query = mysql_query("SELECT DISTINCT M.msg_id, M.uid_fk, M.message,M.video,M.like_count, M.created,M.secret_option, U.username,U.last_login,M.uploads FROM messages M, users U, friends F  WHERE U.status='1' AND M.uid_fk=U.uid AND  M.uid_fk = F.friend_two AND F.friend_one='$uid'  ") or die(mysql_error());
		    }
		    // Store the result into array
		    $data=mysql_num_rows($query);
		    return $data;		
	 }
	
	/*Comments*/
	public function Comments($msg_id, $second_count)
	{
		$query='';
	    if($second_count) {
		    // The scond count is showing comment count limited
			 $query="limit $second_count,2";
		 }
		 //The query to select comments
		 $query = mysql_query("SELECT C.com_id, C.uid_fk, C.comment,C.p_mention, C.created,C.like_count,U.last_login, U.username FROM comments C, users U WHERE U.status='1' AND C.uid_fk=U.uid and C.msg_id_fk='$msg_id' order by C.com_id asc $query") or die(mysql_error());
		 //Store the result
		 while($row=mysql_fetch_array($query))
		 // Store the result into array
		       $data[]=$row;
			   if(!empty($data)) {
				  // Store the result into array
				  return $data;
				}
	}
	/*Notification Created Update*/
	public function Notification_Created_Update($uid)
	{
		$time=time();
	    // Update the notification time
	    $query=mysql_query("UPDATE `users` SET notification_created='$time' WHERE uid='$uid'") or die(mysql_error());
	    return true;
	}
	/*Notifications*/
	public function Notifications($uid, $lastid, $notifications_perpage)
	{
		$notifications_perpage = 5; // How many notifications showing
	    $morequery=""; // query is empty
	    $morequery_friend = ""; // query friend is empty
	    $morequery_group = "";  // query helper is empty
	    $morequery_group_status = ""; // query helper status is empty
	    if($lastid) {
		   $morequery=" and S.created<'".$lastid."' "; // query was created
		   $morequery_friend=" and F.created<'".$lastid."' "; // query friend was created
		   $morequery_group=" and X.created<'".$lastid."' "; // query helper was created
		   $morequery_group_status=" and M.created<'".$lastid."' "; // query helper status was created
		  }
		  $uid=mysql_real_escape_string($uid);
		  //The query to select for notifications
		  $query=mysql_query("(SELECT DISTINCT M.msg_id as msg_id, S.uid_fk, S.ouid_fk as ouid_fk , M.group_id_fk,M.message, S.created, '0' as type
		  FROM
		  messages M, users U, friends F,message_share S
		  WHERE
		  F.friend_one='$uid' AND
		  U.uid = F.friend_one AND
		  U.status='1' AND
		  F.friend_two != S.ouid_fk AND
		  S.uid_fk  = F.friend_two AND
		  M.uid_fk = S.ouid_fk AND F.role='fri' AND
		  S.msg_id_fk = M.msg_id AND S.uid_fk<>'$uid' $morequery GROUP BY M.msg_id)
		  UNION
		  
		  (SELECT DISTINCT '0' as msg_id, F.friend_one as uid_fk, '0' as ouid_fk , '0' as group_id_fk, '0' as message, F.created, '3' as type
		  FROM users U, friends F
		  WHERE F.friend_two='$uid' AND U.uid = F.friend_one AND U.status='1' AND F.role='fri' $morequery_friend)
		  UNION
		  
		  (SELECT
		  DISTINCT '0' as msg_id, X.uid as uid_fk, '0' as ouid_fk , X.group_id_fk as group_id_fk, '0' as message, X.created, '4' as type
		  FROM users U, notification_helper X
		  WHERE X.uid=U.uid AND U.uid='$uid' AND X.uid<>'$uid' AND X.help_status='1' AND U.status='1' $morequery_group)
		  UNION
		  (SELECT
		  DISTINCT M.msg_id as msg_id, M.uid_fk as uid_fk, '0' as ouid_fk , M.group_id_fk as group_id_fk, M.message as message, M.created, '5' as type
		  FROM users U, messages M,notification_helper X
		  WHERE X.uid=U.uid AND U.uid='$uid' AND M.uid_fk<>X.uid AND X.help_status='1' AND U.status='1' $morequery_group_status)
		  
		  UNION
		  (SELECT DISTINCT M.msg_id  as msg_id, S.uid_fk, S.ouid_fk as ouid_fk, M.group_id_fk,M.message, S.created, '1' as type
		  FROM
		  messages M, users U, friends F,message_like S
		  WHERE
		  F.friend_one='$uid' AND
		  U.uid = F.friend_one AND
		  U.status='1' AND
		  F.friend_two != S.ouid_fk AND
		  S.uid_fk  = F.friend_two AND
		  M.uid_fk = S.ouid_fk AND F.role='fri' AND
		  S.msg_id_fk = M.msg_id AND S.uid_fk<>S.ouid_fk AND S.uid_fk<>'$uid' $morequery  GROUP BY M.msg_id)
		  
		  UNION
		  (SELECT DISTINCT M.msg_id  as msg_id, S.uid_fk, M.uid_fk as ouid_fk, M.group_id_fk,M.message, S.created, '2' as type
		  FROM
		  messages M, users U, friends F,comments S
		  WHERE
		  F.friend_one='$uid' AND
		  U.uid = F.friend_one AND
		  U.status='1' AND
		  F.friend_two != S.uid_fk AND
		  M.uid_fk = '$uid' AND F.role='fri' AND
		  S.msg_id_fk = M.msg_id  AND S.uid_fk<>'$uid' $morequery  GROUP BY M.msg_id)
		  ORDER BY created DESC LIMIT $notifications_perpage") or die(mysql_error());
		  //Store the result
		  while($row=mysql_fetch_array($query)) {
			  // Store the result into array
				$data[]=$row;
			  }
			  if(!empty($data)) {
			 // Store the result into array
			   return $data;
			}
	}
	
	/*Notifications*/
	public function Notifications_Count($uid, $created)
	{
		$uid=mysql_real_escape_string($uid);
	    $created=mysql_real_escape_string($created);
	   //The query to select for notifications count
	    $query=mysql_query("(SELECT DISTINCT M.msg_id, S.uid_fk, S.ouid_fk, M.group_id_fk,M.message, S.created, '0' as type
		FROM
		messages M, users U, friends F,message_share S
		WHERE
		F.friend_one='$uid' AND
		U.uid = F.friend_one AND
		U.status='1' AND
		F.friend_two != S.ouid_fk AND
		S.uid_fk  = F.friend_two AND
		M.uid_fk = S.ouid_fk AND F.role='fri' AND
		S.msg_id_fk = M.msg_id AND S.uid_fk<>'$uid' AND S.created>'$created' GROUP BY M.msg_id)
		UNION
		(SELECT DISTINCT '1' as msg_id, F.friend_one as uid_fk, '1' as ouid_fk , '1' as group_id_fk, '1' as message, F.created, '3' as type
		FROM users U, friends F
		WHERE F.friend_two='$uid' AND U.uid = F.friend_one AND U.status='1' AND F.role='fri' AND F.created>'$created' )
		UNION
		(SELECT DISTINCT M.msg_id, S.uid_fk, S.ouid_fk, M.group_id_fk,M.message, S.created, '1' as type
		FROM
		messages M, users U, friends F,message_like S
		WHERE
		F.friend_one='$uid' AND
		U.uid = F.friend_one AND
		U.status='1' AND
		F.friend_two != S.ouid_fk AND
		S.uid_fk  = F.friend_two AND
		M.uid_fk = S.ouid_fk AND F.role='fri' AND
		S.msg_id_fk = M.msg_id AND S.uid_fk<>S.ouid_fk AND S.uid_fk<>'$uid' AND S.created>'$created' GROUP BY M.msg_id)
		UNION
		(SELECT
		DISTINCT '0' as msg_id, X.uid as uid_fk, '0' as ouid_fk , X.group_id_fk as group_id_fk, '0' as message, X.created, '4' as type
		FROM users U, notification_helper X
		WHERE X.uid=U.uid AND U.uid='$uid' AND X.uid<>'$uid' AND X.help_status='1' AND U.status='1' AND X.created>'$created')
		UNION
		(SELECT
		DISTINCT M.msg_id as msg_id, M.uid_fk as uid_fk, '0' as ouid_fk , M.group_id_fk as group_id_fk, M.message as message, M.created, '5' as type
		FROM users U, messages M,notification_helper X
		WHERE X.uid=U.uid AND U.uid='$uid' AND M.uid_fk<>X.uid AND X.help_status='1' AND U.status='1' AND M.created>'$created')
		
		UNION
		(SELECT DISTINCT M.msg_id, S.uid_fk, M.uid_fk as ouid_fk, M.group_id_fk,M.message, S.created, '2' as type
		FROM
		messages M, users U, friends F,comments S
		WHERE
		F.friend_one='$uid' AND
		U.uid = F.friend_one AND
		U.status='1' AND
		F.friend_two != S.uid_fk AND
		M.uid_fk = '$uid' AND F.role='fri' AND
		S.msg_id_fk = M.msg_id  AND S.uid_fk<>'$uid' AND S.created>'$created' GROUP BY M.msg_id)
		ORDER BY created DESC") or die(mysql_error());
		// Output the result
		return mysql_num_rows($query);
		}
	
	/*Insert Image Update*/
	// If the user upload an image with text
	public function Insert_Image_Update($uid, $update, $uploads, $secret_option,$hashtags,$p_mention)
	{   
		$update=mysql_real_escape_string($update); // Update is for with text update
	    $secret_option=mysql_real_escape_string($secret_option); // Who can see this post like friend,everyone or only following user.
	    $time=time(); // Current post time
	    $ip=$_SERVER['REMOTE_ADDR']; // user ip
	    //The query to select for message
	    $query = mysql_query("SELECT msg_id,message,hashtag,p_mention FROM `messages` WHERE uid_fk='$uid' order by msg_id desc limit 1") or die(mysql_error());
	    $result = mysql_fetch_array($query);	
	    $uploads_array=explode(',',$uploads); // Explode the images string value
	    $uploads=implode(',',array_unique($uploads_array)); 
	    // Add the insert message
	    $query = mysql_query("INSERT INTO `messages` (message,hashtag,p_mention, uid_fk, ip,created,uploads,secret_option) VALUES (N'$update','$hashtags','$p_mention', '$uid', '$ip','$time','$uploads','$secret_option')") or die(mysql_error());
	    // Prepare the statement
	    $v = mysql_query("UPDATE `users` SET updates_count=updates_count+1 WHERE uid='$uid'") or die(mysql_error());
	    //The newquery to select for message
	    $newquery = mysql_query("SELECT M.msg_id, M.uid_fk, M.message,M.hashtag,M.p_mention,M.secret_option, M.created,M.uploads,M.like_count,M.comment_count,M.share_count,U.last_login,U.username FROM messages M, users U where M.uid_fk=U.uid and M.uid_fk='$uid' order by M.msg_id desc limit 1 ") or die(mysql_error());
	    $result = mysql_fetch_array($newquery);
	    return $result;
	}
	/*Update Text*/
	public function Insert_Text_Update($uid, $update, $secret_option, $hashtags,$p_mention)
	{
		
		$update=mysql_real_escape_string($update);// Update is text update
	    $secret_option=mysql_real_escape_string($secret_option);// Who can see this post like friend,everyone or only following user.
	    $time=time();// Current post time
	    $ip=$_SERVER['REMOTE_ADDR'];// user ip
	    //The query to select for message
	    $query = mysql_query("SELECT msg_id,message,hashtag,p_mention FROM `messages` WHERE uid_fk='$uid' order by msg_id desc limit 1") or die(mysql_error());
	    $result = mysql_fetch_array($query);	
	    if($update!=$result['message']) {
		   // Add the insert message
		   $query = mysql_query("INSERT INTO `messages` (message,hashtag,p_mention, uid_fk,ip,created,secret_option) VALUES (N'$update','$hashtags','$p_mention','$uid', '$ip','$time','$secret_option')") or die(mysql_error());
		   // Prepare the statement
		   $v = mysql_query("UPDATE `users` SET updates_count=updates_count+1 WHERE uid='$uid'") or die(mysql_error());
		   //The newquery to select for message
		   $newquery = mysql_query("SELECT M.msg_id, M.uid_fk,M.message,M.hashtag,M.p_mention,M.secret_option,M.created,M.like_count,M.comment_count,M.share_count,U.last_login, U.username FROM messages M, users U where M.uid_fk=U.uid and M.uid_fk='$uid' order by M.msg_id desc limit 1 ") or die(mysql_error());
		   $result = mysql_fetch_array($newquery);
		   return $result;
		} else {
			return false;
		}   
	}
	
	/*Delete update*/
	public function Delete_Update($uid, $msg_id)
	{
		$msg_id=mysql_real_escape_string($msg_id);
	    //The query to select for delete message
	    $query=mysql_query("SELECT msg_id FROM `messages` WHERE msg_id = '$msg_id' and uid_fk='$uid'") or die(mysql_error());
	    $msg_num=mysql_num_rows($query);
	    if($msg_num) {
		   // Delete the message like
		   mysql_query("DELETE FROM `message_like` WHERE msg_id_fk = '$msg_id'") or die(mysql_error());
		   //Delete the comments
		   mysql_query("DELETE FROM `comments` WHERE msg_id_fk = '$msg_id'") or die(mysql_error());
		   //The query to select for delete upload message
		   $q=mysql_query("SELECT uploads FROM `messages` WHERE msg_id = '$msg_id'") or die(mysql_error());
		   $row=mysql_fetch_array($q);
		   if(strlen($row['uploads'])>1) {//If more than one image to be deleted
			   $s = explode(",", $row['uploads']); // Define the array which holds the value names
			   $i=1;
			   $f=count($s);
			   foreach($s as $a) {
				  // Delete each image
				  // for each user uploads
				  //The query to select for delete upload image
				$query = mysql_query("SELECT image_path FROM user_uploads WHERE id='$a'") or die(mysql_error());
				$newdata = mysql_fetch_array($query);
				if($newdata) {
					// Delete each image
					$final_image="uploads/".$newdata['image_path'];
					mysql_query("DELETE FROM user_uploads WHERE id='$a'") or die(mysqli_error());
					unlink($final_image);
				 }
				 $i=$i+1;
			}
		 }
	/* End */
	//Delete the msg_id from messages
	mysql_query("DELETE FROM `messages` WHERE msg_id = '$msg_id'") or die(mysql_error());
	//reduce the updates count -1
	mysql_query("UPDATE `users` SET updates_count=updates_count-1 WHERE uid='$uid'") or die(mysql_error());
	return true;
	}
	
	}  
	/*Image Upload*/
	public function Image_Upload($uid, $image)
	{
		$path="uploads/"; // User uploads folder
		$img_src = $path.$image; // User uploads image path
		$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));  //Let's read the contents of a file into a string,
		// To get the title of a image src
		$img_base = base64_encode($imgbinary);//The encoded image is returned as a string.
		$ids = 0;
		// Add the insert image
		$query = mysql_query("insert into user_uploads (image_path,uid_fk)values('$image' ,'$uid')") or die(mysql_error());
		//Increasing the number of pictures uploaded 1
		$query=mysql_query("UPDATE users SET photos_count=photos_count+1 WHERE uid='$uid'") or die(mysql_error());
		$ids = mysql_insert_id();
		return $ids;
	}
	
	/*get Image Upload*/  
	public function Get_Upload_Image($uid, $image)
	{
		if($image) {
		   //If image is not empty 
		   //The query to select for image path
		   $query = mysql_query("select id,image_path from user_uploads where image_path='$image'") or die(mysql_error());
		 } else {
			 //The query to select for image id
			 $query = mysql_query("select id,image_path from user_uploads where uid_fk='$uid' order by id desc ") or die(mysql_error());
		 }
		 $result = mysql_fetch_array($query);
		 return $result;
	}
	/*Id Image Upload*/
	public function Get_Upload_Image_Id($id)
	{
		//The query to select for image ids, how many define values uploaded id
	    $query = mysql_query("select image_path from user_uploads where id='$id'") or die(mysql_error());
	    $result = mysql_fetch_array($query);
	    return $result;
	}
	
	/*Delete Comments*/
	public function Insert_Comment($uid, $msg_id, $comment,$ip,$hashtags,$p_mention)
	{
		$comment=mysql_real_escape_string($comment); // Comment
		$time=time(); // Current time
		$ip=$_SERVER['REMOTE_ADDR'];  // User ip
		// The query to select for comment
		$query = mysql_query("SELECT com_id,comment,hashtag,p_mention FROM `comments` WHERE uid_fk='$uid' and msg_id_fk='$msg_id' order by com_id desc limit 1 ") or die(mysql_error());
		$result = mysql_fetch_array($query);
		if ($comment!=$result['comment']) {
			//Add the insert comment
			$query = mysql_query("INSERT INTO `comments` (comment,hashtag,p_mention, uid_fk,msg_id_fk,ip,created) VALUES (N'$comment','$hashtags','$p_mention', '$uid','$msg_id', '$ip','$time')") or die(mysql_error());
			// Prepare the statement
			mysql_query("UPDATE messages SET comment_count=comment_count+1 WHERE msg_id='$msg_id'") or die(mysql_error());
			// The query to select commented user ids
			$newquery = mysql_query("SELECT C.com_id, C.uid_fk, C.comment,C.hashtag,C.p_mention, C.msg_id_fk, C.created, U.username FROM comments C, users U where C.uid_fk=U.uid and C.uid_fk='$uid' and C.msg_id_fk='$msg_id' order by C.com_id desc limit 1 ");
			$result = mysql_fetch_array($newquery);
			return $result;
		   } else {
			   return false;
		   }
	}
	/*Delete Comments*/
	public function Delete_Comment($uid, $com_id)
	{
		$uid=mysql_real_escape_string($uid);//User id
		$com_id=mysql_real_escape_string($com_id);// comment id
		// The query to select for delete comment
		$q=mysql_query("SELECT M.uid_fk FROM comments C, messages M WHERE C.msg_id_fk = M.msg_id AND C.com_id='$com_id'");
		$d=mysql_fetch_array($q);
		$oid=$d['uid_fk']; // oid is commented user id
		if($uid==$oid) { // if user id is equal to oid
		   // Delete the comment
		   $query = mysql_query("DELETE FROM `comments` WHERE com_id='$com_id'") or die(mysql_error());
		   //reduce the like cound count -1
		   mysql_query("UPDATE messages SET like_count=like_count-1 WHERE msg_id='$msgid'") or die(mysql_error());
		   return true;
		 } else {
			 // The query to select for delete comment
			 $query = mysql_query("DELETE FROM `comments` WHERE uid_fk='$uid' and com_id='$com_id'") or die(mysql_error());
			 return true;
		 }
	}
	/*Friend List*/
	public function Friends_List($uid, $page, $offset, $rowsPerPage)
	{
		$uid=mysql_real_escape_string($uid);
		$page=mysql_real_escape_string($page); // page is for url number like page=1,page=2
		$offset=mysql_real_escape_string($offset);//Balancing the number of user shown
		$rowsPerPage=mysql_real_escape_string($rowsPerPage);//How many friends showing in one page
		if($page) {
		   $con=$offset.",".$rowsPerPage;
		 } else {
			 $con=$rowsPerPage;
		 }
		 // The query to select for showing user details
		 $query=mysql_query("SELECT U.username,U.name,U.user_case,U.verified,U.photos_count,U.updates_count,U.friend_count, U.uid FROM users U, friends F WHERE U.status='1' AND U.uid=F.friend_two AND F.friend_one='$uid' AND F.role='fri' ORDER BY U.name ASC LIMIT $con")or die(mysql_error());
		 while($row=mysql_fetch_array($query)) {
			$data[]=$row;
		 }
		 if(!empty($data)) {
			 // Store the result into array
			   return $data;
			}     
	}
	/*Chat_follow_widget*/
	// This is the following user list for the chat page
	public function Chat_Friends_List($uid)
	{
		$uid=mysql_real_escape_string($uid);
	    //The query to select for showing user details from the chat page
	    $query=mysql_query("SELECT U.username,U.name,U.user_case,U.verified,U.photos_count,U.updates_count,U.friend_count,U.last_login, U.uid FROM users U, friends F WHERE U.status='1' AND U.uid=F.friend_two AND F.friend_one='$uid' AND F.role='fri' ORDER BY U.name ASC ")or die(mysql_error());
	    while($row=mysql_fetch_array($query)) {
		   // Store the result into array
		     $data[]=$row;
	       }
	     if(!empty($data)) {
			// Store the result into array
			   return $data;
			}  
	}
	
	/*Friend List*/
	//This is user follower list 
	public function Follower_List($uid, $tpage, $toffset, $trowsPerPage)
	{
		$uid=mysql_real_escape_string($uid);
	    $tpage=mysql_real_escape_string($tpage); // tpage is for url number like tpage=1,tpage=2
	    $toffset=mysql_real_escape_string($toffset); //Balancing the number of user shown
	    $trowsPerPage=mysql_real_escape_string($trowsPerPage); //How many friends showing in one page
	    if($tpage) {
		   $con=$toffset.",".$trowsPerPage;
		 } else {
			 $con=$trowsPerPage;
		 }
		 // The query to select for showing user details
		 $query=mysql_query("SELECT U.username,U.user_case,U.photos_count,U.verified,U.updates_count,U.friend_count, U.uid FROM users U, friends F WHERE U.status='1' AND U.uid=F.friend_one AND F.friend_two='$uid' AND F.role='fri' ORDER BY F.friend_id DESC LIMIT $con")or die(mysql_error());
		 while($row=mysql_fetch_array($query)) {
			   // Store the result into array
			   $data[]=$row;
		     }
		 if(!empty($data)) {
			// Store the result into array
			   return $data;
			}  
	}
	/*Friend Valid Check*/
	public function Friends_Check($uid, $fid)
	{
		//The query to select for friend role status 'me' or 'fri'
	    $query=mysql_query("SELECT role FROM friends WHERE friend_one='$uid' AND friend_two='$fid'") or die(mysql_error());	
	    $num=mysql_fetch_array($query);
	    return $num['role'];
	}
	/*Friend check for message for privacy section*/
	public function Friends_Check_for_Message($uid, $fid)
	{
		//This query to select for who can see my message button and who can send a message to me
	    $query=mysql_query("SELECT role FROM friends WHERE friend_one='$fid' AND friend_two='$uid'") or die(mysql_error());	
	    $num=mysql_fetch_array($query);
	    return $num['role'];
	}
	/*Friends count*/
	public function Friends_Check_Count($uid, $fid)
	{
		//This query to select check whether user friends
	    $query=mysql_query("SELECT friend_id FROM friends WHERE friend_one='$uid' AND friend_two='$fid'") or die(mysql_error());	
	    $num=mysql_num_rows($query);
	    return $num;
	}
	/*Add Friend*/
	public function Add_Friend($uid, $fid)
	{
		$fid=mysql_real_escape_string($fid); // Visited user id
	    //This query to select friend id and friend role
	    $q=mysql_query("SELECT friend_id FROM friends WHERE friend_one='$uid' AND friend_two='$fid' AND role='fri'");
	    if(mysql_num_rows($q)==0) {
		   // if user role is not fri then show the follow button
		   // and if user role is fri then show unfollow button
		   $time=time(); // current time
		   // Add to friend list
		   $query=mysql_query("INSERT INTO friends(friend_one,friend_two,role,created) VALUES ('$uid','$fid','fri','$time')") or die(mysql_error());
		   //Increasing the number of friend list 1
		   $query=mysql_query("UPDATE users SET friend_count=friend_count+1 WHERE uid='$uid'") or die(mysql_error());
		   return true;
		 }
	}
	/*Remove Friend*/
	public function Remove_Friend($uid, $fid)
	{
		$fid=mysql_real_escape_string($fid); // Visited user id
	    //This query to select friend id and friend role
	    $q=mysql_query("SELECT friend_id FROM friends WHERE friend_one='$uid' AND friend_two='$fid' AND role='fri'");
	    if(mysql_num_rows($q)==1) {
		   // if user role is fri then show the unfollow button
		   // and if user role is not fri then show follow button
		   // Remove to friend list
		   $query=mysql_query("DELETE FROM friends WHERE friend_one='$uid' AND friend_two='$fid'") or die(mysql_error());
		   //reduce the following count -1
		   $query=mysql_query("UPDATE users SET friend_count=friend_count-1 WHERE uid='$uid'") or die(mysql_error());	
		   return true;
		}
	}
	/*Conversations*/
	public function Conversation_Single($user_one, $conversation_uid)
	{
		$user_one=mysql_real_escape_string($user_one);
	    //This query to select conversation
	    $query=mysql_query("SELECT u.uid,c.c_id,u.username,u.last_login,u.email,c.time
	    FROM conversation c, users u
	    WHERE CASE 
	    WHEN c.user_one = '$user_one'
	    THEN c.user_two = u.uid
	    WHEN c.user_two = '$user_one'
	    THEN c.user_one= u.uid
	    END 
	    AND (
	    c.user_one ='$user_one'
	    OR c.user_two ='$user_one'
	    ) AND u.uid='$conversation_uid' ") or die(mysql_error());
	    while($row=mysql_fetch_array($query)) {
		     // Store the result into array
		     $data[]=$row;
	        }
	    if(!empty($data)) {
		    // Store the result into array
		    return $data;
		}
	}
	/* Converstaions */
	public function Conversation($user_one, $last_time, $conversation_uid)
	{
		/* More Records*/
	    $morequery="";
	    if($last_time) {
		    //build up the query
		   $morequery=" and c.time<'".$last_time."' ";
		 }
		 /* More Button End*/
		 $user_one=mysql_real_escape_string($user_one);
		 //This query to select conversation
		 $query=mysql_query("SELECT u.uid,c.c_id,u.username,u.last_login,u.email,c.time
		 FROM conversation c, users u
		 WHERE CASE 
		 WHEN c.user_one = '$user_one'
		 THEN c.user_two = u.uid
		 WHEN c.user_two = '$user_one'
		 THEN c.user_one= u.uid
		 END 
		 AND (
		 c.user_one ='$user_one'
		 OR c.user_two ='$user_one'
		 ) AND u.uid<>'$conversation_uid'
		 $morequery ORDER BY c.time DESC Limit 10") or die(mysql_error());
		 while($row=mysql_fetch_array($query)){
		    	// Store the result into array
			   $data[]=$row;
		     }
		     if(!empty($data)) {
		        return $data;
		     }
	}
	
	/*Insert Conversation Reply*/
	public function Conversation_List($c_id, $uid)
	{
		$user_one=mysql_real_escape_string($uid);
	    $c_id=mysql_real_escape_string($c_id);
	    //This query to select conversation user list for time, reply, upload
	    $query= mysql_query("SELECT R.cr_id,R.time,R.reply,R.upload,R.user_id_fk,R.read_status FROM conversation_reply R WHERE R.c_id_fk='$c_id' ORDER BY R.cr_id DESC LIMIT 1") or die(mysql_error());
	    while($row=mysql_fetch_array($query)) {
		    // Store the result into array
			  $data[]=$row;
	    }
	    if(!empty($data)) {
		    return $data;
		}
	}
	/*Insert Conversation Reply*/
	public function Conversation_Updates($c_id,$uid,$last_time,$conversation_uid,$last_message)
	{
		$c_id=mysql_real_escape_string($c_id);
	    //This query to select conversation insert reply
	    $query= mysql_query("SELECT R.cr_id,R.time, U.conversation_count FROM users U, conversation_reply R WHERE R.user_id_fk=U.uid and R.c_id_fk='$c_id'") or die(mysql_error());
	    $g=mysql_num_rows($query);
	    $second_count=$g-20; // Count the conversation
	    $squery='';
		$morequery="";
	    $x_count= "";
	    if($second_count && $g>20) { // if conversation count greater then 20
		    //build up the query
		    $x_count=$second_count.',';
		 }
		 //This query to select conversation reply detail
		 $q= mysql_query("SELECT R.cr_id,R.time,R.reply,R.upload,R.user_id_fk FROM conversation_reply R WHERE R.c_id_fk='$c_id' ORDER BY R.cr_id DESC LIMIT 1") or die(mysql_error());
		 $k=mysql_fetch_array($q);//Fetch a result row as an associative array
		 $o_uid=$k['user_id_fk']; // Current loged in user id
		 $r=mysql_fetch_array($query);
		 if($conversation_uid) {
			  if($o_uid!=$uid) {  // if cunversation user is not equal loged in user id
				 //Prepare the statement
				 mysql_query("UPDATE conversation_reply SET read_status='0' WHERE c_id_fk='$c_id' ORDER BY cr_id DESC LIMIT 1") or die(mysql_error());
				 $sql=mysql_query("SELECT conversation_count from users WHERE uid='$uid'");
				 $vv=mysql_fetch_array($sql);
				 $conversation_count=$vv['conversation_count'];
				    if($conversation_count>0) { // if conversation count greater then 0
					    //Prepare the statement
					   mysql_query("UPDATE users SET conversation_count=conversation_count-1 WHERE uid='$uid' ") or die(mysql_error());
					  }
				}
		  }
		  
	/* More Records*/
		  
		  if($last_time) {
			  //build up the query
			 $last_time=mysql_real_escape_string($last_time);
			 $morequery=" and R.cr_id > '".$last_time."' ";
			 $x_count='';
			}
		  if($last_message){
			  $last_message=mysql_real_escape_string($last_message);
			  $morequery=" and R.cr_id < '".$last_message."' ";
			  $x_count='';
			  } 
			 //This query to select conversation insert reply
			 $query= mysql_query("SELECT R.cr_id,R.time,R.reply,R.upload,R.user_id_fk,R.c_id_fk,U.uid,U.username,U.email,U.conversation_count FROM users U, conversation_reply R WHERE R.user_id_fk=U.uid and R.c_id_fk='$c_id' $morequery ORDER BY R.cr_id ASC LIMIT $x_count 20") or die(mysql_error());
			 while($row=mysql_fetch_array($query)) {
				 // Store the result into array
				   $data[]=$row;
				 }
				 if(!empty($data)) {
					return $data;
				  }
	}
	
	/*Insert Conversation Reply*/
	public function Conversation_Insert($user_one, $user_two)
	{
		
		$user_one=mysql_real_escape_string($user_one);
	    $user_two=mysql_real_escape_string($user_two);
	    if($user_one!=$user_two) {
		    if($user_one>0 && $user_two>0 ) {
				 //This query to select conversation insert
			    $q= mysql_query("SELECT c_id FROM conversation WHERE (user_one='$user_one' and user_two='$user_two') or (user_one='$user_two' and user_two='$user_one') ") or die(mysql_error());
			    $time=time();
			    $ip=$_SERVER['REMOTE_ADDR'];
			    if(mysql_num_rows($q)==0) { 
				   // Add to conversation
				   $query = mysql_query("INSERT INTO conversation (user_one,user_two,ip,time) VALUES ('$user_one','$user_two','$ip','$time')") or die(mysql_error());
				   //This query to select conversation user id
				   $q=mysql_query("SELECT c_id FROM conversation WHERE user_one='$user_one' ORDER BY c_id DESC limit 1");
				   $v=mysql_fetch_array($q);
				   return $v['c_id'];
				   } else {
					   $v=mysql_fetch_array($q);
					   return  $v['c_id'];
				   }
			  }
		  }
	}
	
	/*Insert Conversation Reply*/
	public function ConversationReply_Insert($reply, $cid, $upload, $uid)
	{
		$upload=mysql_real_escape_string($upload); // Conversation upload image
	    $reply=mysql_real_escape_string($reply); // conversation reply text
	    $cid=mysql_real_escape_string($cid);  // conversation uid
	    $uid=mysql_real_escape_string($uid); // Current user id
	    $time=time(); // Current time
	    $ip=$_SERVER['REMOTE_ADDR'];  // User ip
	    if($uid>0 && $cid>0) {
			 // If current user id and conversation uid inserted
			 // Then add the new conversation from the conversation reply table
			 mysql_query("INSERT INTO conversation_reply (user_id_fk,reply,upload,ip,time,c_id_fk) VALUES ('$uid','$reply','$upload','$ip','$time','$cid')") or die(mysql_error());
			 $time=time();
			 // Update the conversation time
			 mysql_query("UPDATE conversation SET time='$time' WHERE c_id='$cid'") or die(mysql_error());
			 // The query to selected conversation user id
			 $q=mysql_query("SELECT if(user_one = '$uid',user_two,user_one) AS uid FROM conversation where c_id = '$cid' ") or die(mysql_error());
			 $v=mysql_fetch_array($q);
			 $o_uid=$v['uid'];
			 if($o_uid!=$uid) {	
				 // This quert to selected read status for conversation reply
				 $g=mysql_query("SELECT read_status FROM conversation_reply WHERE c_id_fk='$cid' and user_id_fk='$uid' ORDER BY cr_id DESC LIMIT 1,1") or die(mysql_error());
				 $h=mysql_fetch_array($g);
				 if($h['read_status']==0 || $h['read_status']='') { 
					 //Increasing the number of conversation count 1
					 mysql_query("UPDATE users SET conversation_count=conversation_count+1 WHERE uid='$o_uid'") or die(mysql_error());
				  }
			  }
			  // This query to selected conversation detail
			  $q=mysql_query("SELECT R.cr_id,R.time,R.reply,R.upload,U.uid,U.username,U.email FROM users U, conversation_reply R WHERE R.user_id_fk=U.uid and R.c_id_fk='$cid' ORDER BY R.cr_id DESC");
			  $v=mysql_fetch_array($q);
			  return $v;
		}
	}
	/*Image Upload*/
	public function Conversation_Image_Upload($uid, $image)
	{
		$conversation_uploads="conversation_images/"; // Conversation image file
	    $img_src = $conversation_uploads.$image; // Conversation image path
	    $imgbinary = fread(fopen($img_src, "r"), filesize($img_src));//Let's read the contents of a file into a string,
		 // To get the title of a image src
	    $img_base = base64_encode($imgbinary);//The encoded image is returned as a string.
	    $ids = 0;
	    // Add the insert conversation image
	    $query = mysql_query("insert into conversation_images (image_path,uid_fk)values('$image' ,'$uid')") or die(mysql_error());
	    $ids = mysql_insert_id();
	    return $ids;
	}
	 /*get Image Upload*/  
	public function Get_Conversation_Upload_Image($uid, $image)
	{
		if($image) {
		   //If image is not empty 
		   //The query to select for conversation image path
		   $query = mysql_query("select id,image_path from conversation_images where image_path='$image'") or die(mysql_error());
		} else {
			//The query to select for conversation image id
			$query = mysql_query("select id,image_path from conversation_images where uid_fk='$uid' order by id desc ") or die(mysql_error());
			}
			$result = mysql_fetch_array($query);
			return $result;
	}
	/*Id Image Upload*/
	public function Get_Conversation_Upload_Image_Id($id)
	{	
		//The query to select for conversation image ids, how many define values uploaded id
	    $query = mysql_query("select image_path from conversation_images where id='$id'") or die(mysql_error());
	    $result = mysql_fetch_array($query);
	    return $result;
	}
	/*Delete Conversation Reply*/
	public function Delete_Conversation($uid, $c_id)
	{
		$uid=mysql_real_escape_string($uid);
	    $c_id=mysql_real_escape_string($c_id);
	    // The query to selected for conversation id
	    $q = mysql_query("SELECT c_id FROM conversation WHERE c_id = '$c_id' and (user_one='$uid' or user_two='$uid')") or die(mysql_error());
	    if(mysql_num_rows($q)>0) {
		    // The query to selected for conversation relpy read status and user id fk
		    $g=mysql_query("SELECT read_status,user_id_fk FROM conversation_reply WHERE c_id_fk='$c_id'  ORDER BY cr_id DESC LIMIT 1") or die(mysql_error());
		    $h=mysql_fetch_array($g);
		    $vid=$h['user_id_fk'];
		    if($h['read_status']==1) {
			   // The quert to selected for if read status is 1
			   $q=mysql_query("SELECT if(user_one = '$vid',user_two,user_one) AS uid FROM conversation where c_id = '$c_id' ") or die(mysql_error());
			   $v=mysql_fetch_array($q);
			   $o_uid=$v['uid'];
			   //Update the conversation count -1
			   mysql_query("UPDATE users SET conversation_count=conversation_count-1 WHERE uid='$o_uid'") or die(mysql_error());
			 }
			 // Delete the conversation reply
			  mysql_query("DELETE FROM `conversation_reply` WHERE c_id_fk = '$c_id'") or die(mysql_error());	
		    // Delete the conversation
			  mysql_query("DELETE FROM `conversation` WHERE c_id = '$c_id'") or die(mysql_error());	
		   } 
		 return true;
	}
	/*User Settings*/
	public function Change_User_Case($uid, $user_case)
	{
		$user_case=mysql_real_escape_string($user_case);
	    $uid=mysql_real_escape_string($uid);
	    if(strlen($user_case)>0) {
		    // Add the user case if value greater then 0
		    mysql_query("UPDATE users SET user_case='$user_case' WHERE uid='$uid'");
		    return true;
		 }
	}
	//Change User City
	public function Live_City($uid, $user_city)
	{
		$user_city=mysql_real_escape_string($user_city);
	    $query = mysql_query("UPDATE users SET user_city='$user_city' WHERE uid='$uid'") or die(mysql_error()); 
	}
	//Change Password
	public function ChangeSecretPassword($oldpassword, $newpassword, $cpassword, $uid)
	{
		$oldpassword=mysql_real_escape_string($oldpassword); // Currently used password
	    $md5_oldpassword=md5($oldpassword); //Currently used password
	    $newpassword=mysql_real_escape_string($newpassword); // New password
	    $md5_newpassword=md5($newpassword);// new password
	    $cpassword=mysql_real_escape_string($cpassword);
	    $md5_cpassword=md5($cpassword);
	    $uid=mysql_real_escape_string($uid);
	    if($newpassword==$cpassword) {
		    // If new passwords are equel
		    $query=mysql_query("SELECT uid FROM users WHERE uid='$uid' AND password='$md5_oldpassword'");
		    if(mysql_num_rows($query)>0){
			    // Add a new password
			    $query=mysql_query("UPDATE users SET password='$md5_newpassword' WHERE uid='$uid'") or die(mysql_error());
			    return true;
			 } else {
				 return false;
		  }
		  } else {
			  return false;
		  }
	}
	
	//Total User Profile Photos
	public function TotalUserUploadsPhotos($uid)
	{
		// This query to select the total upload image from the user profile
	    $query=mysql_query("SELECT users.uid,
	    users.username,
		users.hide_photos,
	    user_uploads.uid_fk,
	    user_uploads.id,
	    user_uploads.image_path,
		user_uploads.video_name,
	    messages.msg_id,
	    messages.uid_fk,
		messages.video,
	    messages.uploads,
		messages.secret_option,
	    messages.share_count,
	    messages.comment_count,
	    messages.like_count 
	    FROM users 
	    INNER JOIN messages
	    ON users.uid  = messages.uid_fk
	    INNER JOIN user_uploads 
	    ON messages.uploads = user_uploads.id
		OR messages.video = user_uploads.id
	    WHERE uid='$uid' order by msg_id DESC ") or die(mysql_error());
	    while($row=mysql_fetch_array($query)) {
		    // Store the result into array
			 $data[]=$row;
		    }
		    if(!empty($data)) {
			    return $data;
			}
	}
	//User Porfile Photos
	public function UserUploadsPhotos($uid, $lastrmid)
	{
		$morequery="";
	    if($lastrmid) {
		    //build up the query
		   $morequery=" and messages.msg_id<'".$lastrmid."' ";
		 }
		 // The query to selected more uploaded image from the user profile
		 $query=mysql_query("SELECT users.uid,
		 users.username,
		 users.hide_photos,
		 user_uploads.uid_fk,
		 user_uploads.id,
		 user_uploads.image_path,
		 user_uploads.video_name,
		 messages.msg_id,
		 messages.uid_fk,
		 messages.video,
		 messages.uploads,
		 messages.secret_option,
		 messages.share_count,
		 messages.comment_count,
		 messages.like_count 
		 FROM users 
		 INNER JOIN messages
		 ON users.uid  = messages.uid_fk
		 INNER JOIN user_uploads 
		 ON messages.uploads = user_uploads.id
		 OR messages.video = user_uploads.id
		 WHERE uid='$uid' $morequery order by msg_id DESC limit " .$this->perpage) or die(mysql_error());
		 while($row=mysql_fetch_array($query)) {
			 // Store the result into array
			   $data[]=$row;
			 }
		   	 if(!empty($data)) {
				 return $data;
			 }
	}
	// Report Post
	public function Report_Post($uid,$msg_id,$report_type) 
	{
	   $msg_id=mysql_real_escape_string($msg_id);
	   $report_type=mysql_real_escape_string($report_type);
       $time=time();
	   $ip=$_SERVER['REMOTE_ADDR'];
       $query = mysql_query("INSERT INTO `reported_post` (report_type, uid_fk,report_id,ip,created) VALUES('$report_type', '$uid','$msg_id', '$ip','$time')") or die(mysql_error()); 
    }
	
   /*Change post privacy*/
   public function Post_privacy($msg_id,$secret_option) 
	{
	$secret_option=mysql_real_escape_string($secret_option);
    $query = mysql_query("UPDATE messages SET secret_option='$secret_option' WHERE msg_id='$msg_id'") or die(mysql_error()); 
    }
  //TakipÇileri gizle göster
  public function Follower_privacy($uid,$hide_followers) 
	{
	$hide_followers=mysql_real_escape_string($hide_followers);
    $query = mysql_query("UPDATE users SET hide_followers='$hide_followers' WHERE uid='$uid'") or die(mysql_error()); 
    }
 // Following User Privacy
  public function Following_privacy($uid,$hide_following) 
	{
	$hide_following=mysql_real_escape_string($hide_following);
    $query = mysql_query("UPDATE users SET hide_following='$hide_following' WHERE uid='$uid'") or die(mysql_error()); 
    }
  // Photos Privacy
  public function Photos_privacy($uid,$hide_photos) 
	{
	$hide_photos=mysql_real_escape_string($hide_photos);
    $query = mysql_query("UPDATE users SET hide_photos='$hide_photos' WHERE uid='$uid'") or die(mysql_error()); 
    }
 // Language Settings
  public function LanguageSetting($uid,$u_lang) 
	{
	$u_lang=mysql_real_escape_string($u_lang);
    $query = mysql_query("UPDATE users SET u_lang='$u_lang' WHERE uid='$uid'") or die(mysql_error()); 
    }
 // Hide Show Maynow People
  public function MayNowPeople_Privacy($uid,$hide_maynow_people) 
	{
	$hide_maynow_people=mysql_real_escape_string($hide_maynow_people);
    $query = mysql_query("UPDATE users SET hide_maynow_people='$hide_maynow_people' WHERE uid='$uid'") or die(mysql_error()); 
    }
  /* MyKnow People*/
	public function Explore()
	{

	$query=mysql_query("SELECT uid,username,name,friend_count,profile_pic,hide_maynow_people FROM users WHERE hide_maynow_people = '0' ORDER BY RAND() LIMIT 4")or die(mysql_error());
	while($row=mysql_fetch_array($query))
	{
	$data[]=$row;
	}
	if(!empty($data)) {
	   return $data;
	 }
	}
	/*Keşif listesi toplam*/
	public function ExploreUserEx($uid,$base_url) {
		 $uid=mysql_real_escape_string($uid);
		 $query=mysql_query("SELECT * FROM users ") or die(mysql_error());
		 $data = array();
		 $data=mysql_num_rows($query);
        return $data;	
		 }
	/*Keşif*/
	 public function ExploreUserTotal($lastuid) {
		 
		 $morequery="";
		if($lastuid)
		{
		$morequery=" WHERE uid<'".$lastuid."' ";
	    }
		 $query=mysql_query("SELECT * FROM users $morequery ORDER BY uid desc LIMIT " .$this->perpage) or die(mysql_error());
		 $data = array();
		 while($row=mysql_fetch_array($query)) {
			 $data[]=$row;
			 }
			 return $data;
		 }
	/*Insert Update*/
	public function Video_Insert_Update($uid, $update, $video, $secret_option,$hashtags,$p_mention) 
	{
	    $update=mysql_real_escape_string($update);
	    $secret_option=mysql_real_escape_string($secret_option);
        $time=time();
	    $ip=$_SERVER['REMOTE_ADDR'];
        $query = mysql_query("SELECT msg_id,message,hashtag,p_mention FROM `messages` WHERE uid_fk='$uid' order by msg_id desc limit 1") or die(mysql_error());
        $result = mysql_fetch_array($query);	
        if ($update!=$result['message']) {
	        $uploads_array=explode(',',$video);
	        $video=implode(',',array_unique($uploads_array)); 
            $query = mysql_query("INSERT INTO `messages` (message,hashtag,p_mention, uid_fk, ip,created,secret_option,video) VALUES (N'$update','$hashtags','$p_mention', '$uid', '$ip','$time','$secret_option','$video')") or die(mysql_error());
            $v = mysql_query("UPDATE `users` SET updates_count=updates_count+1 WHERE uid='$uid'") or die(mysql_error());
            $newquery = mysql_query("SELECT M.msg_id, M.uid_fk, M.message,M.hashtag,M.p_mention,M.video, M.created,M.secret_option,M.like_count,M.comment_count,M.share_count,U.last_login, U.username FROM messages M, users U where M.uid_fk=U.uid and M.uid_fk='$uid' order by M.msg_id desc limit 1 ");
            $result = mysql_fetch_array($newquery);
            return $result;
        } else {
	       return false;
	    }   
    }
	     /*Image Upload*/
     public function Video_Upload($uid, $video_name, $video_ext) 
     {
        $path="uploads/"; // Vide upload from uploads folder
        $img_src = $path.$video_name;
	    $video_ext= mysql_real_escape_string($video_ext);
        $ids = 0;
        $query = mysql_query("insert into user_uploads (video_name,video_ext,uid_fk)values('$video_name','$video_ext' ,'$uid')") or die(mysql_error());
        $ids = mysql_insert_id();
        return $ids;
    }
	
    /*get Image Upload*/  
	public function Get_Upload_Video($uid,$video_name)  
	{	
	 if($video_name)
	 {
	 $query = mysql_query("select id,video_name from user_uploads where video_name='$video_name'") or die(mysql_error());
	 }
	 else
	 {
	 $query = mysql_query("select id,video_name from user_uploads where uid_fk='$uid' order by id desc ") or die(mysql_error());
	 }
     $result = mysql_fetch_array($query);
   	return $result;
    }
	
	/*Id Image Upload*/
	public function Get_Upload_Video_Id($id) 
	{	
        $query = mysql_query("select video_name,video_ext from user_uploads where id='$id'") or die(mysql_error());
        $result = mysql_fetch_array($query);
    	return $result;
    }
	// Hashtag
	public function Total_Get_Hashtags($lasthid, $hashtag="")
	{
		$hashtag = mysql_real_escape_string(strip_tags(trim($hashtag)));
		$query = mysql_query("SELECT COUNT(*) AS total
                             FROM users
                             INNER JOIN messages ON users.uid =  messages.uid_fk
                             WHERE FIND_IN_SET('$hashtag', hashtag)") or die(mysql_error());
		$row = mysql_fetch_array($query);
		 if(!empty($row)) {
		    return $row['total'];
		}
	}
	// Hashtags
	
	public function Get_Hashtags($lasthid, $uid, $hashtag="")
	{
		$hashtag = mysql_real_escape_string(strip_tags(trim($hashtag)));
		// Now if it has commas, you have to explode() it to an array
		$hashtags_list = explode(',', $hashtag);
		// A variable to hold all the hashtag LIKE conditions
		$hashtag_query = array();
		foreach ($hashtags_list as $ht) {
			// Each tag has to be checked with LIKE alone
			$hashtag_query[] = "FIND_IN_SET(LOWER('$ht'), LOWER(hashtag))";
		}
		// Make them into AND conditions
		$hashtag_query = implode(' AND ', $hashtag_query);
   
		$morequery="";
	    if($lasthid) {
		    //build up the query
		   $morequery=" and messages.msg_id<'".$lasthid."' ";
		 }
	   $query = mysql_query("SELECT users.uid,
	       users.username,
		   users.name,
		   users.last_login,
		   users.profile_pic,
		   messages.message,
		   messages.msg_id,
		   messages.uid_fk,
		   messages.like_count,
		   messages.created,
		   messages.secret_option,
		   messages.comment_count,
		   messages.video,
		   messages.uploads,
		   messages.hashtag
		   FROM users
		   INNER JOIN messages
		   ON users.uid =  messages.uid_fk
		   WHERE ($hashtag_query) $morequery ORDER BY msg_id DESC limit " .$this->perpage) or die(mysql_error());
		   while($row=mysql_fetch_array($query)) {
			 // Store the result into array
			   $data[]=$row;
			 }
		   	 if(!empty($data)) {
				 return $data;
			 }
	}
	/*Facebook Profile ID*/
	public function Facebook_Profile_ID($s_fb, $uid)
	{
		if(strlen($s_fb)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET s_fb='$s_fb' WHERE uid='$uid'");
			return true;
			}
	 }
	 /*Twitter Profile ID*/
	public function Twitter_Profile_ID($s_tw, $uid)
	{
		if(strlen($s_tw)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET s_tw='$s_tw' WHERE uid='$uid'");
			return true;
			}
	 }
	  /*Tumblr Profile ID*/
	public function Tumblr_Profile_ID($s_tumb, $uid)
	{
		if(strlen($s_tumb)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET s_tumb='$s_tumb' WHERE uid='$uid'");
			return true;
			}
	 }
	   /*Google Plus Profile ID*/
	public function GooglePlus_Profile_ID($s_gp, $uid)
	{
		if(strlen($s_gp)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET s_gp='$s_gp' WHERE uid='$uid'");
			return true;
			}
	 }
     /*Youtube Profile ID*/
	public function Youtube_Profile_ID($s_yout, $uid)
	{
		if(strlen($s_yout)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET s_yout='$s_yout' WHERE uid='$uid'");
			return true;
			}
	 }
	  /*Instagram Profile ID*/
	public function Instagram_Profile_ID($s_inst, $uid)
	{
		if(strlen($s_inst)>0) {
			//Checking if user trying to update empty value
			//If value is not 0 then update the user full name
			mysql_query("UPDATE users SET s_inst='$s_inst' WHERE uid='$uid'");
			return true;
			}
	 }
   /*User Gender*/
   public function Change_User_Gender($gender,$uid) 
	{
	$gender=mysql_real_escape_string($gender);
    $query = mysql_query("UPDATE users SET gender='$gender' WHERE uid='$uid'") or die(mysql_error()); 
    }
	//Birthday Setting
	public function User_BirthDaySet($uid,$b_day,$u_month, $u_year)
	{
		$b_day=mysql_real_escape_string($b_day);
		$u_month=mysql_real_escape_string($u_month);
		$u_year=mysql_real_escape_string($u_year);
	    $query = mysql_query("UPDATE users SET b_day='$b_day',u_month='$u_month',u_year='$u_year' WHERE uid='$uid'") or die(mysql_error()); 
	}
	//User Block Check
	public function Friends_block_Check($uid,$fid)
    {
	$query=mysql_query("SELECT block_status FROM blocked_users WHERE blocker_id='$fid' AND blocked_id='$uid'") or die(mysql_error());
	   if (mysql_num_rows($query) > 0) {
  	       $num=mysql_fetch_array($query);
	       return $num['block_status'];
	    } else {
	      return 0;
	    }  
    }
		// Block User
	public function Block_Friend($uid,$fid)
    {
	    $fid=mysql_real_escape_string($fid);
	    $q=mysql_query("SELECT b_id FROM blocked_users WHERE blocked_id='$fid' AND blocker_id='$uid' AND block_status='1'");
	    if(mysql_num_rows($q)==0) {
	       $time=time();
	       $query=mysql_query("INSERT INTO blocked_users(blocker_id,blocked_id,block_status,created) VALUES ('$uid','$fid','1','$time')") or die(mysql_error());
	       $query=mysql_query("DELETE FROM friends WHERE friend_one='$uid' AND friend_two='$fid'") or die(mysql_error());
		   $query=mysql_query("DELETE FROM friends WHERE friend_two='$uid' AND friend_one='$fid'") or die(mysql_error());
	       $query=mysql_query("UPDATE users SET friend_count=friend_count-1 WHERE uid='$uid'") or die(mysql_error());
	       return true;
	     }
    }
	
	/*Unblock User*/
	public function Unblock_Friend($uid,$fid)
	{
	   $fid=mysql_real_escape_string($fid);
	   $q=mysql_query("SELECT b_id FROM blocked_users WHERE blocker_id='$uid' AND blocked_id='$fid' AND block_status='1'");
	   if(mysql_num_rows($q)==1) {
	      $query=mysql_query("DELETE FROM blocked_users WHERE blocker_id='$uid' AND blocked_id='$fid'") or die(mysql_error());	
	      return true;
	    }
	
	}
	/* Advertisments*/
	public function Advertisments()
	{
      $query=mysql_query("SELECT ads_id,ads_title,ads_description,ads_url,ads_img,ads_durl FROM s_advertisements ORDER BY RAND() LIMIT 1")or die(mysql_error());
	    while($row=mysql_fetch_array($query)) {
	         $data[]=$row;
	        }
	   if(!empty($data)) {
	      return $data;
	     }
	   }
}

?>