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); } }
使用bcompiler对PHP文件进行加密的代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@