Location: PHPKode > scripts > PHP-Obfuscator > php-obfuscator-1072/encoder.class.inc
<?php
/**Developer: Anish Karim C*[hide@address.com]*****                       *     *     *
***FileFor: PHPCodeObfuscator[CLASS FILE]***************                        *   *   *
***CreatedOn: 17th December 2009*****************************                     * * *
***Modified On: xxth XXXXXX 2009 *******************************             * * *  C  * * *
***************************************************************                   * * *
***Details: CLASS is the core that make things work***********                  *   *   *
***Suggestions and Comments are welcome******************                     *     *     *
*/


//////////////////////////////////////////////////////VERSION DETAILS///////////////////////////////////////////////////////////////////////////
// 			Version				Date Modified			Changes							Remarks												////
//------------------------------------------------------------------------------------------------------------------------------------------////
//			0.11b				17th December 09		Created							Initial Development.								////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//Class Starts
class PHPEncoder
{
/*For removing comments from your PHP script give mode as `uncomment`.If you are looking for Obfuscate, there are two modes avaialble, 1. obfnorm, which uses simple base64 method and 2. obfhard, uses the combination of base64 and deflate compression method.*/
	var $mode	= "uncomment"; 
	var $filename	='';
	var $error	="";
	
//Function for removing comments in a php file.
	function _removeComments($content)
	{
		$tokens	= token_get_all($content);
		//$this->_test($tokens);
		$out	= "";
		$commentTokens	= array(T_COMMENT);
			if (defined('T_DOC_COMMENT'))
        			$commentTokens[] = T_DOC_COMMENT;
    			if (defined('T_ML_COMMENT'))
        			$commentTokens[] = T_ML_COMMENT;
		foreach($tokens as $token)
		{
			if(is_array($token))
			{
				if(in_array($token[0],$commentTokens))
				{
					continue;
				}
				$token = $token[1];
			}
			$out.=$token;
		}
		return $out;
	}
//Function for base64 encoding for Obfuscation.
function _baseEncode($content)
	{
		return base64_encode($content);
	}
//Function for deflate compression algorithm for hard Obfuscation.
function _deflateEncode($content)
	{
		return gzdeflate($content,9);
	}

//Function for force download the file after obfuscation.

function _downLoad()
	{
		 @ob_end_clean(); //turn off output buffering to decrease cpu usage
 
		// required for IE, otherwise Content-Disposition may be ignored
		if(ini_get('zlib.output_compression'))
		ini_set('zlib.output_compression', 'Off');

		header('Content-Type: text/plain');
 		header('Content-Disposition: attachment; filename="'.basename($this->filename).'"');
 		header("Content-Transfer-Encoding: binary");
 		header('Accept-Ranges: bytes');

		/* The three lines below basically make the download non-cacheable */
		header("Cache-control: private");
		header('Pragma: private');
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

		header("Content-Length: ".filesize($this->filename));
		readfile($this->filename);
	}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMain Function.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
function encode()
	{
		if(empty($this->filename))
		{
			$this->error = "Unable to get the Filename";
			return false;
		}
		elseif(!($data=(file_get_contents($this->filename))))
		{
			$this->error = "Unable to read the File or NO Data";
			return false;
		}
		else
		{
			switch(strtolower($this->mode))
			{
				case 'uncomment':
				{
					$output = $this->_removeComments($data);
					break;
				}
				case 'obfnorm':
				{
					$output = $this->_removeComments($data);
					$data = str_replace("<?php", "", $output);
					$data = str_replace("<?", "", $data);
					$data = str_replace("?>", "", $data);
					$output = $this->_baseEncode($data);
					$output = "<?php eval(base64_decode('$output')); ?>";
					break;
				}
				case 'obfhard':
				{
					$output = $this->_removeComments($data);
					$data = str_replace("<?php", "", $output);
					$data = str_replace("<?", "", $data);
					$data = str_replace("?>", "", $data);
					$output = $this->_deflateEncode($data);
					$output = $this->_baseEncode($output);
					$output = "<?php eval(gzinflate(base64_decode('$output'))); ?>";
					break;
				}
				default:
				{
					$this->error = "Cannot understand the Mode you are selected";
					return false;
				}
			}
			$fp=fopen($this->filename,"w+");
			fwrite($fp,$output);
			fclose($fp);
			$this->_download();//Force Downloading the file.
		}
		return true;
	}

//Pure test function no role in this class.
	function _test($arr)
	{
		echo "<pre>";
		print_r($arr);
		echo "</pre>";
	}

}//Class Ends
?>
Return current item: PHP-Obfuscator