使用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动态生成JavaScript代码
Mar 09 PHP
PHPnow安装服务[apache_pn]失败的问题的解决方法
Sep 10 PHP
自定义php类(查找/修改)xml文档
Mar 26 PHP
解析strtr函数的效率问题
Jun 26 PHP
浅析php中三个等号(===)和两个等号(==)的区别
Aug 06 PHP
使用php伪造referer的方法 利用referer防止图片盗链
Jan 20 PHP
php采用ajax数据提交post与post常见方法总结
Nov 10 PHP
php 调用百度sms来发送短信的实现示例
Nov 02 PHP
PHP PDOStatement::getAttribute讲解
Feb 01 PHP
PHP Swoole异步MySQL客户端实现方法示例
Oct 24 PHP
PHP中通过getopt解析GNU C风格命令行选项
Nov 18 PHP
如何解决php-fpm启动不了问题
Nov 17 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
德劲1104的电路分析与改良
2021/03/01 无线电
php urlencode()与urldecode()函数字符编码原理详解
2011/12/06 PHP
PHP测试程序运行时间的类
2012/02/05 PHP
使用PHP和JavaScript判断请求是否来自微信内浏览器
2015/08/18 PHP
Zend Framework入门教程之Zend_View组件用法示例
2016/12/09 PHP
使用jquery hover事件实现表格的隔行换色功能示例
2013/09/03 Javascript
去除字符串左右两边的空格(实现代码)
2016/05/12 Javascript
JavaScript中函数声明与函数表达式的区别详解
2016/08/18 Javascript
jQuery图片轮播(二)利用构造函数和原型创建对象以实现继承
2016/12/06 Javascript
JavaScript实现前端实时搜索功能
2020/03/26 Javascript
深入浅析vue组件间事件传递
2017/12/29 Javascript
对angularJs中2种自定义服务的实例讲解
2018/09/30 Javascript
layui实现图片虚拟路径上传,预览和删除的例子
2019/09/25 Javascript
VueX模块的具体使用(小白教程)
2020/06/05 Javascript
[04:03]DOTA2肉山黑名单梦之声 风暴之灵中文配音鉴赏
2013/07/03 DOTA
[16:14]教你分分钟做大人:米拉娜(HEROS)
2014/11/24 DOTA
[47:45]DOTA2-DPC中国联赛 正赛 Phoenix vs Dragon BO3 第一场 2月26日
2021/03/11 DOTA
深入理解Python 代码优化详解
2014/10/27 Python
Python flask框架post接口调用示例
2019/07/03 Python
Python-Tkinter Text输入内容在界面显示的实例
2019/07/12 Python
python读写Excel表格的实例代码(简单实用)
2019/12/19 Python
python中线程和进程有何区别
2020/06/17 Python
利用CSS3的border-radius绘制太极及爱心图案示例
2016/05/17 HTML / CSS
乌克兰网上珠宝商店:GoldSoveren
2020/03/31 全球购物
简述数据库的设计过程
2015/06/22 面试题
软件测试企业面试试卷
2016/07/13 面试题
JSP和Servlet有哪些相同点和不同点,他们之间的联系是什么?
2015/10/22 面试题
总经理办公室主任岗位职责
2013/11/12 职场文书
竞职演讲稿范文
2014/01/11 职场文书
《燕子专列》教学反思
2014/02/21 职场文书
师范生自荐信模板
2014/05/28 职场文书
庆国庆国旗下讲话稿2014
2014/09/21 职场文书
亚布力滑雪场导游词
2015/02/09 职场文书
整脏治乱工作简报
2015/07/21 职场文书
2019年公司卫生管理制度样本
2019/08/21 职场文书
详解Python+OpenCV绘制灰度直方图
2022/03/22 Python