PHP 使用bcompiler对文件加密的功能实例

前端之家收集整理的这篇文章主要介绍了PHP 使用bcompiler对文件加密的功能实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. /**
  2. * 使用bcompiler对文件加密
  3. *
  4. * @param
  5. * @author 编程之家 jb51.cc jb51.cc
  6. **/
  7. class PHPCodeZip{
  8. //欲進行壓縮加密的來源資料夾
  9. var $sourceDir = '.';
  10. //進行壓縮加密的存放的資料夾
  11. var $targetDir = 'tmp';
  12. //是否進行加密
  13. var $bcompiler = true;
  14. //是否去除空白註解斷行
  15. var $strip = true;
  16. //來源資料夾檔案路徑陣列
  17. var $sourcefilePaths = array();
  18. //目的資料夾檔案路徑陣列
  19. var $targetPaths = array();
  20. //進行壓縮加密前的資料夾大小
  21. var $sizeBeforeZip = null;
  22. //進行壓縮加密後的資料夾大小
  23. var $sizeAfterZip = null;
  24. //斷行的輸出
  25. var $newline = '';
  26. /**
  27. * 建構子
  28. *
  29. * @param string $sourceDir 來源資料夾
  30. * @param string $targetDir 目的資料夾
  31. * @param boolean $bcompiler 是否進行加密
  32. * @param boolean $strip 是否去除空白註解斷行
  33. * @return boolean
  34. */
  35. public function PHPCodeZip($sourceDir='.',$targetDir='tmp',$bcompiler=true,$strip=true){
  36. //配置初始變數
  37. $this->sourceDir = $sourceDir;
  38. $this->targetDir = $targetDir;
  39. $this->bcompiler = $bcompiler;
  40. //檢查來源資料是否存在
  41. if(!is_dir($this->sourceDir)){
  42. die('指定的來源資料夾'.$this->sourceDir.'不存在,請重新設定');
  43. } else {
  44. //如果指定的目的資料夾存在,砍掉重練
  45. if(is_dir($this->targetDir)){
  46. echo '【初始化目的地資料夾】'.$this->newline.$this->newline;
  47. $this->cleanDir($this->targetDir,true);
  48. }
  49. //建立與來源資料夾結構一樣的目的資料夾
  50. mkdir($this->targetDir,0777);
  51. $dir_paths = $this->getPaths($this->sourceDir,'*',GLOB_ONLYDIR);
  52. foreach($dir_paths as $key => $path){
  53. $path = explode('/',$path);
  54. $path[0] = $this->targetDir;
  55. echo '=> '.join('/',$path).$this->newline;
  56. mkdir(join('/',$path),0777);
  57. }
  58. //取得來源資料夾的檔案路徑清單
  59. $this->sourcefilePaths = $this->getPaths($this->sourceDir,'*');
  60. //配對應目的地的檔案路徑清單
  61. foreach($this->sourcefilePaths as $key => $path){
  62. //設定目的資料夾檔案路徑
  63. $path = explode('/',$path);
  64. $path[0] = $this->targetDir;
  65. $this->targetPaths[$key] = join('/',$path);
  66. }
  67. //記錄執行前的資料夾大小
  68. $this->sizeBeforeZip = $this->getSizeUnit($this->getDirSize($this->sourceDir),2);
  69. echo $this->newline.$this->newline;
  70. }
  71. }
  72. /**
  73. * 進行壓縮加密
  74. * @return boolean
  75. */
  76. public function zip(){
  77. $this->newline = '';
  78. echo '【開始進行加密程序】(資料夾大小:'.$this->sizeBeforeZip.')'.$this->newline.$this->newline;
  79. //將來源檔案進行壓縮
  80. foreach($this->sourcefilePaths as $key => $path){
  81. if(is_file($path)){
  82. //取得檔案資訊
  83. $pathInfo = pathInfo($path);
  84. echo '讀取來源檔:'.$path.$this->newline;
  85. //取得壓縮後的內容
  86. echo '=>去除空白註解..........';
  87. if($this->strip && $pathInfo['extension'] == 'PHP'){
  88. $fileAterZip = PHP_strip_whitespace($path);
  89. } else {
  90. $fileAterZip = file_get_contents($path);
  91. }
  92. echo '完畢'.$this->newline;
  93. //取壓縮後的內容寫到目的位置
  94. $fp = fopen($this->targetPaths[$key],'w+');
  95. echo '=>寫入目的檔..........';
  96. fwrite($fp,$fileAterZip);
  97. fclose($fp);
  98. echo '完畢'.$this->newline;
  99. //是否若選擇進行加密
  100. if($this->bcompiler && $pathInfo['extension'] == 'PHP'){
  101. echo '=>加密原始檔..........';
  102. //複製原始檔
  103. $fh = fopen($this->targetPaths[$key].'encrypt.PHP',"w");
  104. bcompiler_write_header($fh);
  105. bcompiler_write_file($fh,$this->targetPaths[$key]);
  106. bcompiler_write_footer($fh);
  107. fclose($fh);
  108. //刪除未加密的原始檔
  109. unlink($this->targetPaths[$key]);
  110. //重新命名加密過後的檔案
  111. rename($this->targetPaths[$key].'encrypt.PHP',$this->targetPaths[$key]);
  112. echo '完畢'.$this->newline;
  113. }
  114. echo $this->newline.$this->newline;
  115. }
  116. }
  117. //重新計算壓縮加密後的資料夾大小
  118. $this->sizeAfterZip = $this->getSizeUnit($this->getDirSize($this->targetDir),2);
  119. echo '【結束加密程序】'.$this->newline.$this->newline;
  120. echo '《報告資訊》'.$this->newline;
  121. echo '來源資料夾:'.$this->sourceDir.'('.$this->sizeBeforeZip.')'.$this->newline;
  122. echo '目的資料夾:'.$this->targetDir.'('.$this->sizeAfterZip.')'.$this->newline;
  123. echo '檔案大小增幅:+'.$this->getSizeUnit(($this->getDirSize($this->targetDir) - $this->getDirSize($this->sourceDir))).$this->newline;
  124. echo '檔案總數:'.count($this->sourcefilePaths).'個'.$this->newline;
  125. }
  126. /**
  127. * 刪除目錄夾所有檔案
  128. *
  129. * @param string $dir 欲刪除的資料夾
  130. * @param boolean $deleteSelf 同時刪除資料夾
  131. * @return void
  132. */
  133. private function cleanDir($dir='.',$deleteSelf=true){
  134. if(!$dh = @opendir($dir)) return;
  135. while (($obj = readdir($dh))) {
  136. if($obj=='.' || $obj=='..') continue;
  137. if (!@unlink($dir.'/'.$obj)) $this->cleanDir($dir.'/'.$obj,true);
  138. }
  139. if ($deleteSelf){
  140. closedir($dh);
  141. @rmdir($dir);
  142. }
  143. }
  144. /**
  145. * 取得資料夾的總檔案大小
  146. *
  147. * @param string $dir 欲剖析的資料夾
  148. * @return int 位元組
  149. */
  150. private function getDirSize($dir='.'){
  151. //取得檔案路徑清單
  152. $filePaths = $this->getPaths($dir,'*');
  153. //初始化計數器
  154. $sizeCounter = 0;
  155. foreach($filePaths as $key => $path){
  156. $sizeCounter = $sizeCounter + filesize($path);
  157. }
  158. return ($sizeCounter);
  159. }
  160. /**
  161. * 取得資料夾所有配對的路徑
  162. *
  163. * @param string $start_dir 欲剖析的資料夾
  164. * @return array 檔案路徑陣列
  165. */
  166. private function getPaths($sDir,$sPattern,$nFlags = NULL){
  167. $sDir = escapeshellcmd($sDir);
  168. $aFiles = glob("$sDir/$sPattern",$nFlags);
  169. foreach (glob("$sDir/*",GLOB_ONLYDIR) as $sSubDir) {
  170. $aSubFiles = $this->getPaths($sSubDir,$nFlags);
  171. $aFiles = array_merge($aFiles,$aSubFiles);
  172. }
  173. return $aFiles;
  174. }
  175. /**
  176. * 檔案大小單位轉換函式
  177. *
  178. * @param int 檔案大小
  179. * @param int 小數點位數
  180. * @param boolean 是否要將資料切成陣列
  181. * @return mix 字串或陣列
  182. */
  183. public function getSizeUnit($size,$decimal=2,$split=false){
  184. //設定單位序列
  185. $unit = array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB');
  186. //初始化索引
  187. $flag = 0;
  188. //進行簡化除算
  189. while($size >= 1024){
  190. $size = $size / 1024;
  191. $flag++;
  192. }
  193. //是否要將數值與單位分開
  194. if($split){
  195. $sizeUnit = array(
  196. 'size' => number_format($size,$decimal),'unit' => $unit[$flag]
  197. );
  198. } else {
  199. $sizeUnit = (number_format($size,$decimal)).$unit[$flag];
  200. }
  201. //回傳大小與單位
  202. return ($sizeUnit);
  203. }
  204. }

猜你在找的PHP相关文章