使用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分页函数
Jul 08 PHP
初学者入门:细述PHP4的核心Zend
Sep 05 PHP
PHP 和 COM
Oct 09 PHP
php设计模式 Proxy (代理模式)
Jun 26 PHP
php数组函数序列之krsort()- 对数组的元素键名进行降序排序,保持索引关系
Nov 02 PHP
解析如何通过PHP函数获取当前运行的环境 来进行判断执行逻辑(小技巧)
Jun 25 PHP
php fsockopen解决办法 php实现多线程
Jan 20 PHP
让ThinkPHP支持大小写url地址访问的方法
Oct 31 PHP
PHP实现获取FLV文件的时间
Feb 10 PHP
PHP实现QQ登录实例代码
Jan 14 PHP
在PHP中输出JS语句以及乱码问题的解决方案
Feb 13 PHP
php 使用html5 XHR2实现上传文件与进度显示功能示例
Mar 03 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+AJAX聊天程序[聊天室]提供下载
2007/07/21 PHP
javascript 操作select下拉列表框的一点小经验
2010/03/20 Javascript
js onload事件不起作用示例分析
2013/10/09 Javascript
Javascript基础教程之数据类型 (数值 Number)
2015/01/18 Javascript
JS中字符串trim()使用示例
2015/05/26 Javascript
jQuery validate插件submitHandler提交导致死循环解决方法
2016/01/21 Javascript
Bootstrap入门书籍之(三)栅格系统
2016/02/17 Javascript
JS判断是否长按某一键的方法
2016/03/02 Javascript
原生javascript+css3编写的3D魔方动画旋扭特效
2016/03/14 Javascript
Jquery Easyui验证组件ValidateBox使用详解(20)
2016/12/18 Javascript
薪资那么高的Web前端必看书单
2017/10/13 Javascript
vue通过滚动行为实现从列表到详情,返回列表原位置的方法
2018/08/31 Javascript
JS获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天的示例代码
2018/12/05 Javascript
bootstrap+spring boot实现面包屑导航功能(前端代码)
2019/10/09 Javascript
vuex实现数据状态持久化
2019/11/11 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
解决vue组件销毁之后计时器继续执行的问题
2020/07/21 Javascript
python 文件和路径操作函数小结
2009/11/23 Python
Windows系统下使用flup搭建Nginx和Python环境的方法
2015/12/25 Python
python如何通过实例方法名字调用方法
2018/03/21 Python
Python实现手写一个类似django的web框架示例
2018/07/20 Python
Python Series从0开始索引的方法
2018/11/06 Python
Python实现连接MySql数据库及增删改查操作详解
2019/04/16 Python
G-Form护具官方网站:美国运动保护装备
2019/09/04 全球购物
会计与审计专业大专生求职信
2013/10/03 职场文书
初中生三年学习生活的自我评价
2013/11/03 职场文书
机械设计职业生涯规划书
2013/12/27 职场文书
幼儿园教学管理制度
2014/02/04 职场文书
母亲节感恩活动记录
2014/03/16 职场文书
水污染治理工程专业求职信
2014/06/14 职场文书
出纳岗位职责
2015/01/31 职场文书
蜗居观后感
2015/06/11 职场文书
quickjs 封装 JavaScript 沙箱详情
2021/11/02 Javascript
面试分析分布式架构Redis热点key大Value解决方案
2022/03/13 Redis
Python采集壁纸并实现炫轮播
2022/04/30 Python