使用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读取IMAP邮件
Oct 09 PHP
php输入流php://input使用示例(php发送图片流到服务器)
Dec 25 PHP
总结PHP中DateTime的常用方法
Aug 11 PHP
PHP 接入支付宝即时到账功能
Sep 18 PHP
深入讲解PHP的对象注入(Object Injection)
Mar 01 PHP
php显示页码分页类的封装
Jun 08 PHP
PHP如何根据文件头检测文件类型实例代码
Oct 14 PHP
PHP压缩图片功能的介绍
Mar 21 PHP
PHP实现统计代码行数小工具
Sep 19 PHP
PHP设计模式之外观模式(Facade)入门与应用详解
Dec 13 PHP
再谈Yii Framework框架中的事件event原理与应用
Apr 07 PHP
php 防护xss,PHP的防御XSS注入的终极解决方案
Apr 01 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转换颜色为其反色的方法
2015/04/27 PHP
Thinkphp3.2简单解决多文件上传只上传一张的问题
2017/09/26 PHP
PHP中OpenSSL加密问题整理
2017/12/14 PHP
IE 下的只读 innerHTML
2009/08/21 Javascript
js中将URL中的参数提取出来作为对象的实现代码
2011/08/16 Javascript
jQuery动画效果-slideUp slideDown上下滑动示例代码
2013/08/28 Javascript
jQuery控制TR显示隐藏的几种方法
2014/06/18 Javascript
简介JavaScript中的getUTCFullYear()方法的使用
2015/06/10 Javascript
jQuery实现的简单手风琴效果示例
2018/08/29 jQuery
详解Vue中使用Axios拦截器
2019/04/22 Javascript
JS根据Unix时间戳显示发布时间是多久前【项目实测】
2019/07/10 Javascript
使用Vue调取接口,并渲染数据的示例代码
2019/10/28 Javascript
微信小程序页面渲染实现方法
2019/11/06 Javascript
Vue 3.0 全家桶抢先体验
2020/04/28 Javascript
Vue如何将页面导出成PDF文件
2020/08/17 Javascript
ant-design-vue 时间选择器赋值默认时间的操作
2020/10/27 Javascript
Vue中避免滥用this去读取data中数据
2021/03/02 Vue.js
Python实现word2Vec model过程解析
2019/12/16 Python
详解Python 重学requests发起请求的基本方式
2020/02/07 Python
Django 多对多字段的更新和插入数据实例
2020/03/31 Python
PyCharm常用配置和常用插件(小结)
2021/02/06 Python
手把手教你用纯css3实现轮播图效果实例
2017/05/04 HTML / CSS
HTML5探秘:用requestAnimationFrame优化Web动画
2018/06/03 HTML / CSS
基于html5实现的图片墙效果
2014/10/16 HTML / CSS
野兽派官方旗舰店:THE BEAST 野兽派
2016/08/05 全球购物
全球性的在线鞋类品牌:Public Desire
2019/04/03 全球购物
创业计划书——互联网商机
2014/01/12 职场文书
护士试用期自我鉴定
2014/02/08 职场文书
优秀毕业生的求职信
2014/07/21 职场文书
奉献爱心演讲稿
2014/09/04 职场文书
党员学习群众路线心得体会
2014/11/04 职场文书
2016年政治理论学习心得体会
2016/01/25 职场文书
2019垃圾分类宣传口号汇总
2019/08/16 职场文书
一次SQL如何查重及去重的实战记录
2022/03/13 MySQL
使用Bandicam录制鼠标指针并附带点击声音,还可以添加点击动画效果
2022/04/11 数码科技
SQL bool盲注和时间盲注详解
2022/07/23 SQL Server