使用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结束标签的使用细节探讨及联想
Mar 04 PHP
win7+apache+php+mysql环境配置操作详解
Jun 10 PHP
PHP设计模式之调解者模式的深入解析
Jun 13 PHP
深入array multisort排序原理的详解
Jun 18 PHP
destoon实现公司新闻详细页添加评论功能的方法
Jul 15 PHP
Php连接及读取和写入mysql数据库的常用代码
Aug 11 PHP
php curl 上传文件代码实例
Apr 27 PHP
php根据用户语言跳转相应网页
Nov 04 PHP
Redis使用Eval多个键值自增的操作实例
Nov 04 PHP
PHP排序算法之希尔排序(Shell Sort)实例分析
Apr 20 PHP
PHP SESSION机制的理解与实例
Mar 22 PHP
PHP实现简易用户登录系统
Jul 10 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实现Mysql读写分离
2013/06/28 PHP
PHP无限分类(树形类)
2013/09/28 PHP
destoon数据库表说明汇总
2014/07/15 PHP
php ci框架中加载css和js文件失败的原因及解决方法
2014/07/29 PHP
php使用pdo连接mssql server数据库实例
2014/12/25 PHP
PHP JSON格式的中文显示问题解决方法
2015/04/09 PHP
PHP is_array() 检测变量是否是数组的实现方法
2016/06/13 PHP
php微信公众号开发之校园图书馆
2018/10/20 PHP
详解PHP多个进程配合redis的有序集合实现大文件去重
2019/03/06 PHP
浅谈laravel框架sql中groupBy之后排序的问题
2019/10/17 PHP
一段多浏览器的"复制到剪贴板"javascript代码
2007/03/27 Javascript
各种常用浏览器getBoundingClientRect的解析
2009/05/21 Javascript
Struts2的s:radio标签使用及用jquery添加change事件
2013/04/08 Javascript
jquery中使用循环下拉菜单示例代码
2014/09/24 Javascript
jQuery实现ajax调用WCF服务的方法(附带demo下载)
2015/12/04 Javascript
基于jQuery实现美观且实用的倒计时实例代码
2015/12/30 Javascript
JavaScript中英文字符长度统计方法示例【按照中文占2个字符】
2017/01/17 Javascript
详解Jquery EasyUI tree 的异步加载(遍历指定文件夹,根据文件夹内的文件生成tree)
2017/02/11 Javascript
vue组件中点击按钮后修改输入框的状态实例代码
2017/04/14 Javascript
详解vue.js2.0父组件点击触发子组件方法
2017/05/10 Javascript
react高阶组件经典应用之权限控制详解
2017/09/07 Javascript
详解vue-cli+element-ui树形表格(多级表格折腾小计)
2019/04/17 Javascript
在NodeJs中使用node-schedule增加定时器任务的方法
2020/06/08 NodeJs
Vue Router中应用中间件的方法
2020/08/06 Javascript
Python深入06——python的内存管理详解
2016/12/07 Python
Python 开发工具PyCharm安装教程图文详解(新手必看)
2020/02/28 Python
Python try except异常捕获机制原理解析
2020/04/18 Python
python实现简单遗传算法
2020/09/18 Python
HTML5打开本地app应用的方法
2016/03/31 HTML / CSS
类、抽象类、接口的差异
2016/06/13 面试题
中专生自我鉴定
2013/12/17 职场文书
初中生操行评语大全
2014/04/24 职场文书
民主生活会对照检查材料思想汇报
2014/09/27 职场文书
群众路线教育实践活动心得体会(教师)
2014/10/31 职场文书
Vue3中的Refs和Ref详情
2021/11/11 Vue.js
一文教你快速生成MySQL数据库关系图
2022/06/28 Redis