使用bcompiler对PHP文件进行加密的代码

使用说明:

//载入函式
include_once('phpCodeZip.php');
//建立加密文件(sourceDir要加密的php文件目录,targetDir加密后的文件目录)
$encryption = new PhoCodeZip('sourceDir','targetDir');
//执行行加密
$encryption->zip();

phpCodeZip.php源码下载
phpCodeZip.rar
phpCodeZip.php源码内容
复制代码 代码如下:
/*
* @license:MIT & GPL
*/
class phpCodeZip{
//欲行加密的源料
var $sourceDir = '.';
//行加密的存放的料
var $targetDir = 'tmp';
//是否行加密
var $bcompiler = true;
//是否去除空白解行
var $strip = true;
//源料案路列
var $sourcefilePaths = array();
//目的料案路列
var $targetPaths = array();
//行加密前的料大小
var $sizeBeforeZip = null;
//行加密後的料大小
var $sizeAfterZip = null;
//行的出
var $newline = '';
/**
* 建子
*
* @param string $sourceDir 源料
* @param string $targetDir 目的料
* @param boolean $bcompiler 是否行加密
* @param boolean $strip 是否去除空白解行
* @return boolean
*/
public function phpCodeZip($sourceDir='.',$targetDir='tmp',$bcompiler=true,$strip=true){
//配置初始
$this->sourceDir = $sourceDir;
$this->targetDir = $targetDir;
$this->bcompiler = $bcompiler;
//查源料是否存在

if(!is_dir($this->sourceDir)){
die('指定的源料'.$this->sourceDir.'不存在,重新定');
} else {
//如果指定的目的料存在,砍掉重
if(is_dir($this->targetDir)){
echo '【初始化目的地料】'.$this->newline.$this->newline;
$this->cleanDir($this->targetDir,true);
}
//建立源料一的目的料
mkdir($this->targetDir,0777);
$dir_paths = $this->getPaths($this->sourceDir,'*',GLOB_ONLYDIR);
foreach($dir_paths as $key => $path){
$path = explode('/',$path);
$path[0] = $this->targetDir;
echo '=> '.join('/',$path).$this->newline;
mkdir(join('/',$path),0777);
}
//取得源料的案路清
$this->sourcefilePaths = $this->getPaths($this->sourceDir,'*');
//配目的地的案路清
foreach($this->sourcefilePaths as $key => $path){
//定目的料案路
$path = explode('/',$path);
$path[0] = $this->targetDir;
$this->targetPaths[$key] = join('/',$path);
}
//行前的料大小
$this->sizeBeforeZip = $this->getSizeUnit($this->getDirSize($this->sourceDir),2);
echo $this->newline.$this->newline;
}
}
/**
* 行加密
* @return boolean
*/
public function zip(){
$this->newline = '';
echo '【始行加密程序】(料大小:'.$this->sizeBeforeZip.')'.$this->newline.$this->newline;
//源案行
foreach($this->sourcefilePaths as $key => $path){
if(is_file($path)){
//取得案
$pathInfo = pathInfo($path);
echo '取源:'.$path.$this->newline;
//取得後的容
echo '=>去除空白解..........';
if($this->strip && $pathInfo['extension'] == 'php'){
$fileAterZip = php_strip_whitespace($path);
} else {
$fileAterZip = file_get_contents($path);
}
echo '完'.$this->newline;

//取後的容到目的位置
$fp = fopen($this->targetPaths[$key],'w+');
echo '=>入目的..........';
fwrite($fp,$fileAterZip);
fclose($fp);
echo '完'.$this->newline;
//是否若行加密
if($this->bcompiler && $pathInfo['extension'] == 'php'){
echo '=>加密原始..........';
//原始
$fh = fopen($this->targetPaths[$key].'encrypt.php', "w");
bcompiler_write_header($fh);
bcompiler_write_file($fh, $this->targetPaths[$key]);
bcompiler_write_footer($fh);
fclose($fh);
//除未加密的原始

unlink($this->targetPaths[$key]);
//重新命名加密後的案

rename($this->targetPaths[$key].'encrypt.php',$this->targetPaths[$key]);
echo '完'.$this->newline;
}
echo $this->newline.$this->newline;
}
}
//重新算加密後的料大小
$this->sizeAfterZip = $this->getSizeUnit($this->getDirSize($this->targetDir),2);
echo '【束加密程序】'.$this->newline.$this->newline;

echo '《告》'.$this->newline;
echo '源料:'.$this->sourceDir.'('.$this->sizeBeforeZip.')'.$this->newline;
echo '目的料:'.$this->targetDir.'('.$this->sizeAfterZip.')'.$this->newline;
echo '案大小增幅:+'.$this->getSizeUnit(($this->getDirSize($this->targetDir) - $this->getDirSize($this->sourceDir))).$this->newline;
echo '案:'.count($this->sourcefilePaths).''.$this->newline;

}
/**
* 除目所有案
*
* @param string $dir 欲除的料
* @param boolean $deleteSelf 同除料
* @return void
*/
private function cleanDir($dir='.',$deleteSelf=true){
if(!$dh = @opendir($dir)) return;
while (($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) $this->cleanDir($dir.'/'.$obj, true);
}
if ($deleteSelf){
closedir($dh);
@rmdir($dir);
}
}
/**
* 取得料的案大小
*
* @param string $dir 欲剖析的料
* @return int 位元
*/
private function getDirSize($dir='.'){
//取得案路清
$filePaths = $this->getPaths($dir,'*');
//初始化器
$sizeCounter = 0;
foreach($filePaths as $key => $path){
$sizeCounter = $sizeCounter + filesize($path);
}
return ($sizeCounter);
}
/**
* 取得料所有配的路
*
* @param string $start_dir 欲剖析的料
* @return array 案路列
*/
private function getPaths($sDir, $sPattern, $nFlags = NULL){
$sDir = escapeshellcmd($sDir);
$aFiles = glob("$sDir/$sPattern", $nFlags);
foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir) {
$aSubFiles = $this->getPaths($sSubDir, $sPattern, $nFlags);
$aFiles = array_merge($aFiles, $aSubFiles);
}
return $aFiles;
}
/**
* 案大小位函式
*
* @param int 案大小
* @param int 小位
* @param boolean 是否要料切成列
* @return mix 字串或列
*/
public function getSizeUnit($size,$decimal=2,$split=false){
//定位序列
$unit = array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB');
//初始化索引
$flag = 0;
//行化除算
while($size >= 1024){
$size = $size / 1024;
$flag++;
}
//是否要值位分
if($split){
$sizeUnit = array(
'size' => number_format($size,$decimal),
'unit' => $unit[$flag]
);
} else {
$sizeUnit = (number_format($size,$decimal)).$unit[$flag];
}
//回大小位
return ($sizeUnit);
}
}

php技术使用bcompiler对PHP文件进行加密的代码,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。