<?php

/**
 * Description of Encryption
 * based on chip_password_generator.php
 * Klasa do tworzenie haseł, saltów.
 *
 * @author Life.Object, Janusz Chodzicki
 */
class Eppe_Encryption {

	private static $instance = false;
	
	private $args = array(
	    'length' => 8,
	    'alpha_upper_include' => TRUE,
	    'alpha_lower_include' => TRUE,
	    'number_include' => TRUE,
	    'symbol_include' => TRUE,
	);
	private $alpha_upper = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
	private $alpha_lower = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
	private $number = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
	private $symbol = array("-", "_", "^", "~", "@", "|", "=", "+", ";", "!", ",", "(", ")", "{", "}", "[", "]", ".", "?", "%", "*", "#");
	private $input = 4;

	/**
	 * Zwraca instancję obiektu
	 *
	 * @return Core_Class_Password
	 * @access public
	 * @static 
	 */
	public static function getInstance() {
		if (self::$instance == false) {
			self::$instance = new Eppe_Encryption();
		}
		return self::$instance;
	}
	
	/**
	 * 
	 * @return Eppe_Encryption
	 */
	public static function _() {
		return self::getInstance();
	}
	
	public function __construct( $args = array() ) {
		$this->setArgs( $args );
	}
	
	private function chipParseArgs( $args = array(), $defaults = array() ) { 
		return array_merge( $defaults, $args );	 
	}
	
	
	private function setArgs( $args = array() ) { 
		
		$defaults = $this->getArgs();
		$args = $this->chipParseArgs( $args, $defaults );
		$this->args = $args;	 
	}
	

	public function getArgs() { 
		return $this->args;	 
	}
	
	private function getAlphaUpper() { 
		return $this->alpha_upper;	 
	}
	
	
	private function getAlphaLower() { 
		return $this->alpha_lower;	 
	}
	
	private function getNumber() { 
		return $this->number;	 
	}
	private function getSymbol() { 
		return $this->symbol;	 
	}
	
	/**
	 * Setuje hasło
	 * @return int 
	 */
	private function setPassword() { 
		
		/* Temporary Array(s) */
		$temp = array();
		$exec = array();
		
		/* Arguments */
		$args = $this->getArgs();	 
		extract($args);
		
		/* Minimum Validation */		
		if( $length <= 0 ) {
			return 0;
		}
		
		/* Execution Array Logic */
		
		/* Alpha Upper */
		if( $alpha_upper_include == TRUE ) {
			$alpha_upper = $this->getAlphaUpper();
			$exec[] = 1;
		}
		
		/* Alpha Lower */
		if( $alpha_lower_include == TRUE ) {
			$alpha_lower = $this->getAlphaLower();
			$exec[] = 2;
		}
		
		/* Number */
		if( $number_include == TRUE ) {
			$number = $this->getNumber();
			$exec[] = 3;
		}
		
		/* Symbol */
		if( $symbol_include == TRUE ) {
			$symbol = $this->getSymbol();
			$exec[] = 4;
		}
		
		/* Unique and Random Loop */
		$exec_count = count( $exec ) - 1;
		$input_index = 0;
		
		for ( $i = 1; $i <= $length; $i++ ) {
			switch( $exec[$input_index] ) {
				
				case 1:				
				shuffle( $alpha_upper );
				$temp[] = $alpha_upper[0];
				unset( $alpha_upper[0] );				
				break;
				
				case 2:				
				shuffle( $alpha_lower );
				$temp[] = $alpha_lower[0];
				unset( $alpha_lower[0] );				
				break;
				
				case 3:				
				shuffle( $number );
				$temp[] = $number[0];
				unset( $number[0] );				
				break;
				
				case 4:				
				shuffle( $symbol );
				$temp[] = $symbol[0];
				unset( $symbol[0] );				
				break;
			}
			
			if ( $input_index < $exec_count ) {
				$input_index++;
			} else {
				$input_index = 0;
			}
		
		}
		
		shuffle($temp);
		
		$password = implode( $temp );
		
		return $password;
		
	}	
	
	/**
	 * Tworzy losowe hasło.
	 * @return array 
	 */
	public function getPassword() { 		
		return $this->setPassword();		
	}	
	
	/**
	 * Tworzy salt
	 * @return array 
	 */
	public function getSalt($lenght = 6) {
		$this->setArgs(array('length' => $lenght));
		return $this->setPassword();
	}
	
	/**
	 * Hashuje hasło 
	 * @param string $password 
	 * @param string $salt	 
	 */
	public function hashPassword($password, $salt = null) {
		return md5($password . $salt);
	}
}

?>
