Location: PHPKode > scripts > SecurityAuthParam > securityauthparam/securityAuthParam.php
<?php
/**
 * Classe que retorna que encripa e decripa parametro
 * @author Marcelo Soares da Costa
 * @email phpmafia at yahoo dot com dot br
 * @copyright Marcelo Soares da Costa © 2007/2009.
 * @license FreeBSD http://www.freebsd.org/copyright/freebsd-license.html
 * @version 1.0
 * @access public
 * @package OOE
 * @chagelog
 * @data 2009-16-12
 * @page
 */
################################################################################
class securityAuthParam {

private $string;
private $mixed = array();
private $hash;
private $merge = array();
private $result;
private $arrayRef = array();

private $auth;

/**
 * Verifica parametros obrigatorios e opcionais
 * Check parameters
 */
function __construct() {
if (!defined('PRIVATEKEY')) {
throw new Exception("PRIVATEKEY not defined");
}
if (!defined('HASHMODE')) {
define("HASHMODE", "ripemd160");
}
if (!defined('AUTHMODE')) {
define("AUTHMODE", "AUTHARRAY");
}

}

/**
 * Convert string encode base 64 in safe for URL string
 * RFC 4648 :http://www.faqs.org/rfcs/rfc4648
 * RFC 2045 : http://www.faqs.org/rfcs/rfc2045
 * @acess private
 * @input string
 * @return string
 */

private function encode64Str($string) {
return strtr(chunk_split(base64_encode($string)), '+/=', '-_.');
}

/**
 * decode safe base 64 encode
 * RFC 4648 :http://www.faqs.org/rfcs/rfc4648
 * RFC 2045 : http://www.faqs.org/rfcs/rfc2045
 * @acess private
 * @input string
 * @return string
 */

private function decode64Str($string) {
return base64_decode(strtr($string, '-_.', '+/='));
}


/**
 * This function compress the given serialize string using the ZLIB data format.
 * @acess private
 * @input mixed
 * @return string
 */

private function compresSerialize($mixed) {
return gzcompress(serialize($mixed), 9);
}

/**
 * This function uncompress and unserialize the given string using the ZLIB data format.
 * @acess private
 * @input string
 * @return mixed
 */

private function decompresSerialize($string) {
return unserialize(gzuncompress($string));
}

/**
 * This function return hash HMAC the given string.
 * @acess private
 * @input string
 * @return string
 */

private function srtHashHmac($string) {
return hash_hmac(HASHMODE, $string, PRIVATEKEY);
}

/**
 * This function return hash HMAC the given mixed.
 * @acess private
 * @input mixed
 * @return string
 */

private function authParam($mixed) {
return self::srtHashHmac(self::compresSerialize($mixed));
}


/**
 * This function merge a auth param.
 * @acess private
 * @input mixed
 * @return string
 */

private function encodeSafeParam($mixed) {
$hash = array("AUTH"=>self::authParam($mixed));
$merge = array_merge((array)$mixed, (array)$hash);
$result = self::encode64Str(self::compresSerialize($merge));
return $result;
}

/**
 * This function decode safe param.
 * @acess private
 * @input string
 * @return mixed
 */

private function dencodeSafeParam($string) {
return self::decompresSerialize(self::decode64Str($string));
}

/**
 * This function check auth param with given mixed.
 * @acess private
 * @input mixed,string
 * @return mixed
 */

private function checkAuthParam($mixed,$auth){
if(self::authParam($mixed)===$auth){
return $mixed;
}else{
throw new Exception("Authentic Param Fail");
}
}

/**
 * This function check auth param with given string with auth param.
 * @acess private
 * @input string
 * @return mixed
 */

private function checkAuthArray($string) {
$arrayRef = self::dencodeSafeParam($string);
$auth = array_pop($arrayRef);
return self::checkAuthParam($arrayRef,$auth);
}

/**
 * This function encode a given mixed and return auth param encode.
 * @acess final public
 * @input string
 * @return mixed
 */

final public function encodeAuthParam($mixed){
switch(AUTHMODE)
{
case "AUTHSTR":
return self::authParam($mixed);
break;
case "AUTHARRAY":
return self::encodeSafeParam($mixed);
break;
default:
throw new Exception("Unknown Encode Authentic Method");
}
}

/**
 * This function decode and check given mixed or param
 * @acess final public
 * @input string
 * @return mixed
 */


final public function decodeAuthParam($param,$mixed=null){
switch(AUTHMODE)
{
case "AUTHSTR":
return self::checkAuthParam($mixed,$param);
break;
case "AUTHARRAY":
return self::checkAuthArray($param);
break;
default:
throw new Exception("Unknown Encode Authentic Method");
}
}
// fim da classe
}
?>
Return current item: SecurityAuthParam