<?php 
/****************************************************************/
/*          e-mo.pl © 2017                                      */
/*          http://www.e-mo.com.pl                              */
/*          e-mail: info@e-mo.pl                                */
/*          produkt: Action2osCommerce                          */
/*          wersja: 1.9.7                                       */
/*          webserwis: oscGold                                  */
/*          sciezka: https://www.e-mo.com.pl/plikit,1918,533545b244d7d.php*/
/*          data: 2017-09-25                                    */
/*          godz: 10:45:20                                      */
/****************************************************************/

class administrators { 
	public $Conn = 0;
	public $errMsg = '';
	public $errNo= 0;
	public $id=0;
	public $user_name=0;
	public $user_password=0;

	public function Dodaj(){
		$zwrot=true;
		$query="Insert administrators (user_name, user_password) VALUES (:user_name, :user_password) ";
		$sth = $this->Conn->prepare($query);

		$sth->bindParam(':user_name', $this->user_name, PDO::PARAM_STR);
		$sth->bindParam(':user_password', $this->user_password, PDO::PARAM_STR);

		$sth->execute();

		if($sth->errorCode() != 0){
			$zwrot=false;
			$this->errMsg = $sth->errorInfo();
			$this->errNo = $sth->errorCode();
		}

		 return $zwrot;
	 } 
	public function Zmien(){
		$zwrot=true;
		$query="Update administrators SET user_name=:user_name, user_password=:user_password Where id=:id";
		$sth = $this->Conn->prepare($query);

		$sth->bindParam(':user_name', $this->user_name, PDO::PARAM_STR);
		$sth->bindParam(':user_password', $this->user_password, PDO::PARAM_STR);
		$sth->bindParam(':id', $this->id, PDO::PARAM_INT);

		$sth->execute();

		if($sth->errorCode() != 0){
			$zwrot=false;
			$this->errMsg = $sth->errorInfo();
			$this->errNo = $sth->errorCode();
		}

		 return $zwrot;
	 } 
	public function Usun(){
		$zwrot=true;
		$query="Delete from administrators Where id=:id";

		$sth = $this->Conn->prepare($query);
		$sth->bindParam(':id', $this->id, PDO::PARAM_INT);

		$sth->execute();

		if($sth->errorCode() != 0){
			$zwrot=false;
			$this->errMsg = $sth->errorInfo();
			$this->errNo = $sth->errorCode();
		}

		 return $zwrot;
	 } 
	 
	  private function tep_validate_password($plain, $encrypted) {

		  $stack = explode(':', $encrypted);
         		  
		  if (sizeof($stack) != 2) return false;
		
		  if (md5($stack[1] . $plain) == $stack[0]) 
		  {	
			  return true;	
		  }
		  
		 
		  
		return false;
	  }
	 public function Login(){
		$zwrot = 0;
		
		$query='show tables like \'%admin%\'';
		$sth = $this->Conn->prepare($query);
		$sth->execute();
		$query2 ='';

		while ($row = $sth->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_NEXT))
		{
			if($row[0]=='administrators')
				$query2='Select * from administrators Where user_name=:user_name Limit 1';
			elseif ($row[0]=='admin')				
			   $query2='Select *, admin_password as user_password, admin_id as id  from admin Where admin_email_address=:user_name Limit 1';
		
		}
		$sth =null;

		if(!empty($query2))
		{
			
			$sth2 = $this->Conn->prepare($query2);				
			$sth2->bindParam(':user_name', $this->user_name, PDO::PARAM_STR);
			$sth2->execute();
			
			$x=0;
						
			while ($row2 = $sth2->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))
			{	
				if($this->tep_validate_password($this->user_password,$row2['user_password']))
					$zwrot = $row2['id'];				
			}
		}

		return $zwrot;
	}
	public function Lista(){
		$query='Select * from administrators';
		
		$sth = $this->Conn->prepare($query);
		$sth->execute();
		$tablica = array();
		$x=0;

		while ($row = $sth->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))
		{
			foreach ($row as $k => $v)
			{
				$tablica[$x][$k]=trim($v);
			}
			$x++;
		}

		return $tablica;
	}

}
?>
