<?php 
	include "db_config.php";
	class User{
		
		public $db;
		public function __construct(){
			$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
			mysqli_set_charset($this->db,"utf8");
			if(mysqli_connect_errno()) {
	 
				echo "Error: Could not connect to database.";
	 
			exit;
 
			}
		}

		/*** for registration process ***/
		public function reg_user($name,$username,$password,$email){

			
			$password = md5($password);
			$sql="SELECT * FROM users WHERE uname='$username' OR uemail='$email'";
			
			//checking if the username or email is available in db
			$check =  $this->db->query($sql) ;
			$count_row = $check->num_rows;

			//if the username is not in db then insert to the table
			if ($count_row == 0){
				$sql1="INSERT INTO users SET uname='$username', upass='$password', fullname='$name', uemail='$email'";
				$result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");
        		return $result;
			}
			else { return false;}
		}


		/*** for login process ***/
		public function check_login($username, $password){
        	$password = md5($password);
			$sql2="SELECT * FROM tbl_admin WHERE email='$username' and password='$password' and status='1'";
        	$result = mysqli_query($this->db,$sql2);
        	$user_data = mysqli_fetch_array($result);
        	$count_row = $result->num_rows;
	        if ($count_row == 1) { 	            
	            $_SESSION['login'] = true; 
	            $_SESSION['userid'] = $user_data['userid'];
				$user_id=$_SESSION['userid'];
	            return true;
	        }
	        else{
			    return false;
			}
    	}

    	/*** for showing the username or fullname ***/
		/*** for login process ***/
		public function check_member_login($username,$password,$type){			
        	$password = md5($password);
		echo $sql2="SELECT * FROM  tbl_staff WHERE email='$username' and password='$password' and status='1'";	
                
        	$result = mysqli_query($this->db,$sql2);
        	$user_data = mysqli_fetch_array($result);
        	 $count_row = $result->num_rows;
               
	        if ($count_row == 1) { 	            
	            $_SESSION['login'] = true; 
	            $_SESSION['staffid'] = $user_data['staffid'];
                    $_SESSION['userrole'] = $user_data['role'];
		    $staffid=$_SESSION['staffid'];
                    $userrole=$_SESSION['userrole']; 
                    
	            return true;
	        }
	        else{
			    return false;
			}
    	}

    	/*** for showing the username or fullname ***/
    	public function user_details($table,$userid){
    		$sql3="SELECT * FROM ".$table." WHERE userid = $userid";
	        $result = mysqli_query($this->db,$sql3);
	        $user_data = mysqli_fetch_array($result);
	        return $user_data;
    	}
		public function member_details($table,$memberid){
    		$sql3="SELECT * FROM ".$table." WHERE staffid = $memberid";
	        $result = mysqli_query($this->db,$sql3);
	        $user_data = mysqli_fetch_array($result);
	        return $user_data;
    	}
  
    	/*** starting the session ***/
	    public function get_session(){    
	        return $_SESSION['login'];
	    }
		 public function get_user_id(){    
	        return $_SESSION['userid'];
	    }
		 public function get_member_id(){    
	        return $_SESSION['staffid'];
	    }

	    public function user_logout() {
	        $_SESSION['login'] = FALSE;
	        session_destroy();
	    }
		public function check_email($email){       	
		$sql2="SELECT * FROM tbl_admin WHERE email='$email' and status='1'";
        	$result = mysqli_query($this->db,$sql2);
        	$user_data = mysqli_fetch_array($result);
        	$count_row = $result->num_rows;
			 
	        if ($count_row == 1) {
			   $pass=rand(10000,1000000);
			   $password=md5($pass);
			   mysqli_query($this->db,"update tbl_admin set password='$password' where email='$email'");
			   $message="<p>Hi your password got reset</p>";
			   $message.="<p>Your new password is <b>".$pass."</b></p>";
			   $message.="<p>Login to the Website and change the password</p>";
                            
				$to = $user_data['str_email'];
				 
                                $subject="Password changed on Vakson";
                                $emailto = $user_data['str_email'];
				$toname = 'Vakson';
				$emailfrom = 'info@vakson.com';
				$fromname = 'Vakson'; 
				$headers = 
					'Return-Path: ' . $emailfrom . "\r\n" . 
					'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
					'X-Priority: 3' . "\r\n" . 
					'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
					'Reply-To: ' . $fromname . ' <' . $emailto . '>' . "\r\n" .
					'MIME-Version: 1.0' . "\r\n" . 
					'Content-Transfer-Encoding: 8bit' . "\r\n" .  
					//'Bcc: alex@infobahnworld.com' . "\r\n" . 
					'Content-Type: text/html; charset=UTF-8' . "\r\n";
				$params = '-f ' . $emailfrom;
                                mail($emailto, $subject, $table, $headers, $params);
			   $success="Password Changed successfully<br/>Please check your email ";
			   return $success;
	        }
	        else{
			    $success="Email address is Invalid ";
			   return $success;
			}
    	}
public function check_member_email($email,$usertype){       	
		 $sql2="SELECT * FROM tbl_staff WHERE email='$email' and status='1'";			 
        	$result = mysqli_query($this->db,$sql2);
        	$user_data = mysqli_fetch_array($result);
        	$count_row = $result->num_rows;
			 
	        if ($count_row == 1) {
			   $pass=rand(10000,1000000);
			   $password=md5($pass);
			   mysqli_query($this->db,"update tbl_staff set password='$password' where email='$email'");
			   $message="<p>Hi your password got reset</p>";
			   $message.="<p>Your new password is <b>".$pass."</b></p>";
			   $message.="<p>Login to the Website and change the password</p>";
                           $table='<table width="600px">';
         $table.='<tr><td style=\'text-align:center;\'><img src=\'http://vakson.com/images/logo1.png\'/></td></tr>';
         $table.='<tr><td>'.$message.'</td></tr>';
         $table.='</table>';
				$to = $email;
				$subject = "Password changed on Mena Hotel Projects";
				$subject="Password changed on Mena Hotel Projects";
                                $emailto =$email;
				$toname = 'menahotelprojects';
				$emailfrom = 'sales@menahotelprojects.com';
				$fromname = 'menahotelprojects'; 
				$headers = 
					'Return-Path: ' . $emailfrom . "\r\n" . 
					'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
					'X-Priority: 3' . "\r\n" . 
					'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
					'Reply-To: ' . $fromname . ' <' . $emailto . '>' . "\r\n" .
					'MIME-Version: 1.0' . "\r\n" . 
					'Content-Transfer-Encoding: 8bit' . "\r\n" .  
					//'Bcc: alex@infobahnworld.com' . "\r\n" . 
					'Content-Type: text/html; charset=UTF-8' . "\r\n";
				$params = '-f ' . $emailfrom;
                                mail($emailto, $subject, $table, $headers, $params);
			   $success="Password Changed successfully<br/>Please check your email "; 
                           
			   return $success;
	        }
	        else{
			    $success="Email address is Invalid ";
			   return $success;
			}
    	}


	}
?>