<?php
class dBconn{
   public  $dbcon;
   public  $host="localhost";
   public  $dbname="aiderste_simuyangu";
   public  $user="aiderste_aiderstech";
   public  $pwd="Aid@2022_2023";
  // public  $user="root";
  // public  $pwd="";


                                      //FUNCTION FOR DATABASE CONNECTION GOES HERE
 
function __construct()

{
         $this->dbcon = new PDO("mysql:dbname=$this->dbname;host=$this->host;",$this->user,$this->pwd);
          
}
function getLostPhones(){
  try{
    $sts="Imepotea";
    $myp=$this->dbcon->prepare("select * from phone where Status=:sts");
    $myp->bindParam(":sts",$sts);
    $myp->execute(); 

     return $myp;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}


function getMyLostPhone($ownID){
  try{
    $sts="Imepotea";
    $myp=$this->dbcon->prepare("select * from phone where userid=:em and Status=:sts");
    $myp->bindParam(":em",$ownID);
    $myp->bindParam(":sts",$sts);
    $myp->execute(); 

     return $myp;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

function getMyLostDate($imei){
  try{
    $sts="Imepotea";
    $myp=$this->dbcon->prepare("select * from lPhone where imei=:em");
    $myp->bindParam(":em",$imei);
    $myp->execute(); 

     return $myp;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}


function getLostRipoti($imei){
  try{

    $myp=$this->dbcon->prepare("select * from lPhone where imei=:em");
    $myp->bindParam(":em",$imei);
    $myp->execute(); 

     return $myp;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

function getMyPhone($ownID){
  try{
    $myp=$this->dbcon->prepare("select * from phone where userid=:em");
    $myp->bindParam(":em",$ownID);
    $myp->execute(); 

     return $myp;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

function getPhones(){
  try{
    $myp=$this->dbcon->prepare("select * from phone");
    $myp->execute();

     return $myp;//->ErrorInfo();

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}
  function login($username,$password){
  try{
    $Rs=$this->dbcon->prepare("select * from users where email=:em and password=:pwd");
    $Rs->bindParam(":em",$username);
    $Rs->bindParam(":pwd",$password);
    $Rs->execute(); 
    if ($Rs->rowCount()>0)
     {
      /////////////
         //$rg=Date("h:i:sa");
    
         $regdate=Date("Y-m-d"); 

         $time=Date("h:i:s");

         $data=array($time,$regdate,$password,$username);
 
         $Qr=$this->dbcon->prepare("update users set lastlogTime=?, lastLogin =? where password=? and email=?"); 
         $Qr->execute($data);
      
      /////////////////
     }
        return $Rs;//$Qr->ErrorInfo();

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

function logins($username,$password){
  try{
    $Rs=$this->dbcon->prepare("select * from userss where email=:em and password=:pwd");
    $Rs->bindParam(":em",$username);
    $Rs->bindParam(":pwd",$password);
    $Rs->execute(); 
    if ($Rs->rowCount()>0)
     {
      /////////////
         //$rg=Date("h:i:sa");
    
         $regdate=Date("Y-m-d"); 

         $time=Date("h:i:s");

         $data=array($time,$regdate,$password,$username);
 
         $Qr=$this->dbcon->prepare("update userss set lastlogTime=?, lastLogin =? where password=? and email=?"); 
         $Qr->execute($data);
      
      /////////////////
     }
        return $Rs;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

function users(){
  try{
    $Rs=$this->dbcon->prepare("select * from users");
      $Rs->execute(); 
      return $Rs;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

/////////

function getPhoneOwner($ownID){
  try{
    $Rs=$this->dbcon->prepare("select * from users where userid=:Id");
    $Rs->bindParam(":Id",$ownID);
    $Rs->execute(); 
    return $Rs;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}

/////////
function addClients($fname,$lname,$phoneno,$email,$pwd,$date){
  
 $dLogD=Date("Y-m-d");

 $data=array($fname,$lname,$phoneno,$email,$date,$pwd, $dLogD);

   try{
    $Qr=$this->dbcon->prepare("insert into userss(fname,lname,phoneno,email,userid,password,lastLogin) values(?,?,?,?,?,?,?)");
    $Qr->execute($data);
    if($Qr->rowCount()>0)
    {
          return $Qr;
    }
    else
    {
      return  $Re=$Qr->ErrorInfo();//"Data not saved resubmit the form!..";
    }

    }
    catch(PDOException $ex){
    $ex->getMessage();
    }
   } 
                                         // CLASS TERMINAL
function addClient($fname,$lname,$phoneno,$email,$pwd,$date){
  
 $dLogD=Date("Y-m-d");

 $data=array($fname,$lname,$phoneno,$email,$date,$pwd, $dLogD);

   try{
    $Qr=$this->dbcon->prepare("insert into users(fname,lname,phoneno,email,userid,password,lastLogin) values(?,?,?,?,?,?,?)");
    $Qr->execute($data);
    if($Qr->rowCount()>0)
    {
          return $Qr;
    }
    else
    {
      return  $Re=$Qr->ErrorInfo();//"Data not saved resubmit the form!..";
    }

    }
    catch(PDOException $ex){
    $ex->getMessage();
    }
   } 




                                        // CLASS TERMINAL
function addPhone($imei,$ilini,$pType,$date,$ownID,$lineone){
  

  $data=array($imei,$ilini,$pType,$date,$ownID,$lineone);
 
   try{
    $Qr=$this->dbcon->prepare("insert into phone(imei,ilini,ptype,regDate,userid,lineone) values(?,?,?,?,?,?)");
    $Qr->execute($data);
    if($Qr->rowCount()>0)
    {
          return $Qr;
    }
    else
    {
      return  $Re=$Qr->ErrorInfo();//"Data not saved resubmit the form!..";
    }

    }
    catch(PDOException $ex){
    $ex->getMessage();
    }
   } 




   ////////////////////////////


////////////////////////////IMAGE TITLE//////////////////

function vTitle($imei){
   
  
    if(preg_match("/^[\sA-Za-z0-9]+$/",$imei)  && $imei!=""){
      return  $imei;
    } 
     else{
      return false;
     }
}  


function  eTitle($imei) {
   if($imei=="")
                 {

    return $imei="Imei required!..";
                 }
  
   else if(!preg_match("/^[\sA-Za-z0-9]+$/",$imei))
                {
     
        return $imei= "Error in imei!";
              }
   else         {
                return;
              }
             }                           



////////////////////////////////////////////
//////////////////////////////////////////////   FUNCTION  FOR IMAGE VALIDATIO  GOES HERE //////////////////////
function vImageType($ext,$allowedtype) {
   
  if(in_array($ext,$allowedtype))
  {
      
      return $ext;
      
    }
  else
   {
    return false; 
   }

     }  



function eImageType($ext,$allowedtype) {
   
  if(in_array($ext,$allowedtype))
  {
      
      return;
      
    }
  else
   {
    return $ex= "The extension ".$ext." is not type of an image!..";
   }

     }  
                                      //TYPE OF IMAGE ENDS




                             //IMAGE SIZE VALIDATION

function vImageSize($size) {
   
  if($size>=0 && $size<2000000)
  {
      
      return $size;
      
    }
  else
   {
    return false; 
   }

     }  



function eImageSize($size) 
 {
   
  if($size>=0 && $size<2000000)
  {
      
      return;
      
    }
   else
    {
   return $size= "The Height:  ".$height." and   Width: ".$width."   of image is not within range!..";
    }


} 
                                      //VALIDATION  OF IMAGE SIZE ENDS


                             //IMAGE ERROR VALIDATION

function vImageError($error) {
   
  if($error <= 0)
  {
      
      return true;
      
    }
  else
   {
    return false; 
   }

     }  



function eImageError($error) 
 {
   
  if($error > 0)
  {
      
      return $err="An error: ".$error." occurred while uploading file!..."; 

      
    }
   else
    {
   return ;
    }


} 

//IMAGE ERROR VALIDATION

                            //CHECK IF IMAGE EXIST

function vImageExist($fn) {
   
  if(file_exists("../images/".$fn))
  {
      
      return false;
      
    }
  else
   {
    return true; 
   }

     }  



function eImageExist($fn) 
 {
   
  
if(file_exists("../images/".$fn))
  {
      
      return  $exs= "The ".$fn." already uploaded!..";
  
    }
   else
    {
   return ;
    }


} 


                            //CHECK IF IMAGE SELECTED

function vImageSelectF($fn) {
   
  if($fn=="")
  {
      
      return false;
      
    }
  else
   {
    return true; 
   }

     }  



function eImageSelectF($fn) 
 {
   
  
if($fn=="")
  {
      
      return  $exs= "Select file to upload!..";
  
    }
   else
    {
   return ;
    }


} 

/////////////////////////////////UPLOAD ITEM IMAGES/////////////////////

function uploadImage($fn,$imei, $ownID,$tmp_name,$tDate){
  

 

    $site="images/".$fn;
    $ext = strtolower($site);


   
   $data=array($imei,$ownID,$site,$tDate);
   
   try{
  
   $Qr=$this->dbcon->prepare("insert into lPhone(
imei,userid,lripoti,regDate) values(?,?,?,?)");
  $Qr->execute($data);
   if($Qr->rowCount()>0)
   {

         move_uploaded_file($tmp_name,$site);
     
          $sts="Imepotea";
         
           $data=array($sts,$imei,$ownID);
 
           $Qr=$this->dbcon->prepare("update phone set Status=? where imei=? and userid=?"); 

           $Qr->execute($data);
           
           return $Qr="The image is successfully uploaded!..";

   }
   else
   {
      return $Qr= $Qr->ErrorInfo(); //$Re="Oops your network is low to upload data!..";
   } 

}
catch(PDOException $ex){
  $ex->getMessage();
}
}

function getCheckIfPostExist($imei,$ownID){
  try{
    $ownerH=$this->dbcon->prepare("select * from lPhone where userid=:Id and imei=:img");
    $ownerH->bindParam(":Id",$ownID);
    $ownerH->bindParam(":img",$imei);
      $ownerH->execute(); 
      
    return $ownerH;

  }
  catch(PDOException $xc){
    $xc->getMessage();
  }
}
   ////////////////////////////


}