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


Posted in PHP onAugust 29, 2010

使用说明:

//载入函式
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{ 
//欲?行?嚎s加密的?碓促Y料?A 
var $sourceDir = '.'; 
//?行?嚎s加密的存放的?料?A 
var $targetDir = 'tmp'; 
//是否?行加密 
var $bcompiler = true; 
//是否去除空白?解?嘈 
var $strip = true; 
//?碓促Y料?A?n案路?疥?列 
var $sourcefilePaths = array(); 
//目的?料?A?n案路?疥?列 
var $targetPaths = array(); 
//?行?嚎s加密前的?料?A大小 
var $sizeBeforeZip = null; 
//?行?嚎s加密後的?料?A大小 
var $sizeAfterZip = null; 
//?嘈械妮?出 
var $newline = ''; 
/** 
* 建??子 
* 
* @param string $sourceDir ?碓促Y料?A 
* @param string $targetDir 目的?料?A 
* @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; 
//?z查?碓促Y料是否存在 if(!is_dir($this->sourceDir)){ 
die('指定的?碓促Y料?A'.$this->sourceDir.'不存在,?重新?定'); 
} else { 
//如果指定的目的?料?A存在,砍掉重? 
if(is_dir($this->targetDir)){ 
echo '【初始化目的地?料?A】'.$this->newline.$this->newline; 
$this->cleanDir($this->targetDir,true); 
} 
//建立??碓促Y料?A???一?拥哪康馁Y料?A 
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); 
} 
//取得?碓促Y料?A的?n案路?角? 
$this->sourcefilePaths = $this->getPaths($this->sourceDir,'*'); 
//配???康牡氐?n案路?角? 
foreach($this->sourcefilePaths as $key => $path){ 
//?定目的?料?A?n案路? 
$path = explode('/',$path); 
$path[0] = $this->targetDir; 
$this->targetPaths[$key] = join('/',$path); 
} 
//???绦星暗馁Y料?A大小 
$this->sizeBeforeZip = $this->getSizeUnit($this->getDirSize($this->sourceDir),2); 
echo $this->newline.$this->newline; 
} 
} 
/** 
* ?行?嚎s加密 
* @return boolean 
*/ 
public function zip(){ 
$this->newline = ''; 
echo '【?始?行加密程序】(?料?A大小:'.$this->sizeBeforeZip.')'.$this->newline.$this->newline; 
//??碓?n案?行?嚎s 
foreach($this->sourcefilePaths as $key => $path){ 
if(is_file($path)){ 
//取得?n案?? 
$pathInfo = pathInfo($path); 
echo '?取?碓?n:'.$path.$this->newline; 
//取得?嚎s後的?热 
echo '=>去除空白?解..........'; 
if($this->strip && $pathInfo['extension'] == 'php'){ 
$fileAterZip = php_strip_whitespace($path); 
} else { 
$fileAterZip = file_get_contents($path); 
} 
echo '完?'.$this->newline; 
//取?嚎s後的?热??到目的位置 
$fp = fopen($this->targetPaths[$key],'w+'); 
echo '=>??入目的?n..........'; 
fwrite($fp,$fileAterZip); 
fclose($fp); 
echo '完?'.$this->newline; 
//是否若??襁M行加密 
if($this->bcompiler && $pathInfo['extension'] == 'php'){ 
echo '=>加密原始?n..........'; 
//??原始?n 
$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); 
//?h除未加密的原始?n 
unlink($this->targetPaths[$key]); 
//重新命名加密?後的?n案 
rename($this->targetPaths[$key].'encrypt.php',$this->targetPaths[$key]); 
echo '完?'.$this->newline; 
} 
echo $this->newline.$this->newline; 
} 
} 
//重新?算?嚎s加密後的?料?A大小 
$this->sizeAfterZip = $this->getSizeUnit($this->getDirSize($this->targetDir),2); 
echo '【?束加密程序】'.$this->newline.$this->newline; 
echo '《?蟾尜Y?》'.$this->newline; 
echo '?碓促Y料?A:'.$this->sourceDir.'('.$this->sizeBeforeZip.')'.$this->newline; 
echo '目的?料?A:'.$this->targetDir.'('.$this->sizeAfterZip.')'.$this->newline; 
echo '?n案大小增幅:+'.$this->getSizeUnit(($this->getDirSize($this->targetDir) - $this->getDirSize($this->sourceDir))).$this->newline; 
echo '?n案??担?.count($this->sourcefilePaths).'??'.$this->newline; 
} 
/** 
* ?h除目??A所有?n案 
* 
* @param string $dir 欲?h除的?料?A 
* @param boolean $deleteSelf 同?r?h除?料?A 
* @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); 
} 
} 
/** 
* 取得?料?A的??n案大小 
* 
* @param string $dir 欲剖析的?料?A 
* @return int 位元? 
*/ 
private function getDirSize($dir='.'){ 
//取得?n案路?角? 
$filePaths = $this->getPaths($dir,'*'); 
//初始化??灯 
$sizeCounter = 0; 
foreach($filePaths as $key => $path){ 
$sizeCounter = $sizeCounter + filesize($path); 
} 
return ($sizeCounter); 
} 
/** 
* 取得?料?A所有配?Φ穆? 
* 
* @param string $start_dir 欲剖析的?料?A 
* @return array ?n案路?疥?列 
*/ 
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; 
} 
/** 
* ?n案大小?挝晦D?Q函式 
* 
* @param int ?n案大小 
* @param int 小?迭c位? 
* @param boolean 是否要?①Y料切成?列 
* @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++; 
} 
//是否要??抵蹬c?挝环珠_ 
if($split){ 
$sizeUnit = array( 
'size' => number_format($size,$decimal), 
'unit' => $unit[$flag] 
); 
} else { 
$sizeUnit = (number_format($size,$decimal)).$unit[$flag]; 
} 
//回?鞔笮∨c?挝 
return ($sizeUnit); 
} 
}
PHP 相关文章推荐
php算开始时间到过期时间的相隔的天数
Jan 12 PHP
php统计文件大小,以GB、MB、KB、B输出
May 29 PHP
深入理解php的MySQL连接类
Jun 07 PHP
浅析PHP中的字符串编码转换(自动识别原编码)
Jul 02 PHP
php文件夹与文件目录操作函数介绍
Sep 09 PHP
PHP根据IP判断地区名信息的示例代码
Mar 03 PHP
Thinkphp中的volist标签用法简介
Jun 18 PHP
php使用memcoder将视频转成mp4格式的方法
Mar 12 PHP
ThinkPHP框架分布式数据库连接方法详解
Mar 14 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
Oct 23 PHP
Laravel利用gulp如何构建前端资源详解
Jun 03 PHP
php实例化一个类的具体方法
Sep 19 PHP
php采集时被封ip的解决方法
Aug 29 #PHP
PHP isset()与empty()的使用区别详解
Aug 29 #PHP
php Notice: Undefined index 错误提示解决方法
Aug 29 #PHP
php下统计用户在线时间的一种尝试
Aug 26 #PHP
PHP生成excel时单元格内换行问题的解决方法
Aug 26 #PHP
PHP下对字符串的递增运算代码
Aug 21 #PHP
深入理解PHP原理之异常机制
Aug 21 #PHP
You might like
php中处理模拟rewrite 效果
2006/12/09 PHP
Bootstarp风格的toggle效果分享
2016/02/23 Javascript
适用于javascript开发者的Processing.js入门教程
2016/02/24 Javascript
JQuery学习总结【二】
2016/12/01 Javascript
微信小程序 基础知识css样式media标签
2017/02/15 Javascript
Angular2整合其他插件的方法
2018/01/20 Javascript
JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法
2018/06/07 Javascript
微信小程序中进行地图导航功能的实现方法
2018/06/29 Javascript
vue系列之requireJs中引入vue-router的方法
2018/07/18 Javascript
小程序视频或音频自定义可拖拽进度条的示例代码
2018/09/30 Javascript
vue.js实现的全选与全不选功能示例【基于elementui】
2018/12/03 Javascript
vue element中axios下载文件(后端Python)
2019/05/10 Javascript
[02:17]TI4西雅图DOTA2前线报道 啸天mik夫妻档解说
2014/07/08 DOTA
[00:36]DOTA2上海特级锦标赛 Alliance战队宣传片
2016/03/04 DOTA
python dict 字典 以及 赋值 引用的一些实例(详解)
2017/01/20 Python
Python面向对象基础入门之编码细节与注意事项
2018/12/11 Python
python使用zip将list转为json的方法
2018/12/31 Python
python脚本当作Linux中的服务启动实现方法
2019/06/28 Python
使用python爬取抖音视频列表信息
2019/07/15 Python
Python 获取项目根路径的代码
2019/09/27 Python
使用python和pygame制作挡板弹球游戏
2019/12/03 Python
Python的Django框架实现数据库查询(不返回QuerySet的方法)
2020/05/19 Python
QML用PathView实现轮播图
2020/06/03 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
2020/07/15 Python
Python实现GIF图倒放
2020/07/16 Python
Python HTMLTestRunner如何下载生成报告
2020/09/04 Python
浅析Python中字符串的intern机制
2020/10/03 Python
python如何利用Mitmproxy抓包
2020/10/10 Python
python requests库的使用
2021/01/06 Python
CSS3线性渐变简单实现以及该属性在浏览器中的不同
2012/12/12 HTML / CSS
优秀的自荐信要注意哪些
2014/01/03 职场文书
幼儿园教学管理制度
2014/02/04 职场文书
质量保证书范本
2014/04/29 职场文书
企业党员一句话承诺
2014/05/30 职场文书
2014年重阳节活动策划方案书
2014/09/16 职场文书
2014年党建工作汇报材料
2014/11/02 职场文书