| 
                          
                
                                    
                    
                      
	| 發表者 | 
	討論內容 | 
 
	 冷日 (冷日) | 
	發表時間:2010/11/18 3:58 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- [轉貼]安裝 PHP bcompile 做 編譯/加密 原始碼
 
		安裝 PHP bcompile 做 編譯/加密 原始碼PHP 的程式在公司在交付程式中, 比較麻煩的就是要如何把原始碼編譯或加密, 免費的方案, 可以考慮用 bcompiler(PHP bytecode Compiler). 不過這套於 PHP5 的物件寫法, 繼承等等, compiler 後執行會有問題, 純 function / include 等等, 是正常的. PHP bcompiler 安裝步驟 下述環境於 Debian / Ubuntu Linux 安裝  - apt-get install libbz2-dev php-pear php5-dev # 因為 pecl compile bcompiler 需要 libbz2(bz2 library)
  - pecl install channel://pecl.php.net/bcompiler-0.8 # 因為 bcompiler 是 beta, 不是 stable, 所以需要完整路徑.
  - echo "extension=bcompiler.so" >> /etc/php5/conf.d/bcompile.ini
  - /etc/init.d/apache2 restart
  確認 Module 是否載入  - php -m | grep bcompiler
  - phpinfo(); # 看是否有載入 bcompiler module
  PHP bcompiler 使用 先寫一個編譯程式, 準備把 example.php 編譯成 example.phb (或 xxx.php 都可以), 詳細可見: bcompiler_write_file. 編譯其他程式用的檔案 bcompiler.php   <?php   $fh = fopen("example.phb", "w");   bcompiler_write_header($fh);   bcompiler_write_file($fh, "example.php");   bcompiler_write_footer($fh);   fclose($fh);   ?> hello.php   <?php   function hello()   {      echo "hello\n";   }   ?> example.php   <?php   include('hello.php');   hello();   ?>  - 編譯: php bcompiler.php # 會發現有產生出 example.phb (或於 bcompiler.php 有另外取名字的 xxx.php)
  - 執行: php example.phb # 會秀 hello, 然後此檔案已經是編譯過的 php code.
  另外查到 使用 bcompiler 來編譯(加密)您的 PHP 原始碼 這篇的作者, 寫了一個 bencoder 的好用程式, 可以 compile 整個目錄, 詳細可見: BENCODER v1.3 - Encode your PHP script using bcompiler Bencoder 安裝使用  - wget http://bencoder.urdada.net/bencoder
  - chmod +x ./bencoder
  - vim bencoder # 修改 php 執行路徑, Debian PHP 存放路徑在 /usr/bin/php
  #!/usr/local/bin/php 改成 #!/usr/bin/php   - 執行: ./bencoder -f -o php/Breadcrumb_ben/ -s php/Breadcrumb/ -r
  encoded: php/Breadcrumb_ben//first.php    encoded: php/Breadcrumb_ben//index.php    encoded: php/Breadcrumb_ben//bread.php    encoded: php/Breadcrumb_ben//second.php   
  原文出處:安裝 PHP bcompile 做 編譯/加密 原始碼 | Tsung's Blog 
	 
	 | 
 
	| 
	
	
	 | 
 
	 冷日 (冷日) | 
	發表時間:2010/11/18 4:00 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- [轉貼]使用 bcompiler 來編譯(加密)您的 PHP 原始碼
 
		Using bcomipler to  compile (encode) your PHP script (in FreeBSD)  eAccelerator 的 encoder 在 PHP  5.1 以後就無法運作了,使是最新的版本 0.9.5 也不行,因此如果您還想用 eAccelerator 的 encoder,乖乖待在  PHP 4.x or PHP 5.0 吧,相關資訊可參考之前的兩篇文章: 根據 mailing  list 上面作者最新的 說法,短期內應該都還不會修復,甚至有可能還會放棄支援 encoder 部份,作者還推薦大家改用 bcompiler ... orz 所以如果你跟我一樣必須要用 PHP 5.1 的話,就跟我一起來玩看看 bcompiler 囉 附帶一提,原本是要買 ionCube 公司開發的 PHP Encoder  的,這看來是目前編譯(加密)PHP 原始碼最好的選擇,價格也不算貴,而且花的是公司的錢,不過還是先試用看看 bcompiler  好了,真的不好用的話,再來買囉 以下的安裝與使用說明均以 FreeBSD  為範例,其他作業系統應該也差不多,請自己試看看吧 在正式安裝之前提醒你, bcompiler 會用到 bzip2 這個  extension,所以先安裝一下 php5-bz2: # cd /usr/ports/archivers/php5-bz2 # make install clean  bcompiler 是一個收容於 PECL 的 PHP extension,您可以直接使用 pecl 這個命令來安裝,而 pecl 的使用方法與 pear 相同,因此你必須先安裝 pear : # cd /usr/ports/devel/pear # make install clean pear 安裝好後,同時會有一個 pecl 命令可用 (/usr/locel/bin/pecl),可用它來安裝 bcompiler pecl install channel://pecl.php.net/bcompiler-0.8 不過呢,我發現直接執行 pecl 會發生錯誤(至少在 FreeBSD 下是如此): # pecl Fatal error: Call to undefined function preg_match() in ... 問題出在 /usr/local/bin/pecl 這個 script 的最後一行: exec $PHP -C -n -q $INCARG -d output_buffering=1 -d safe_mode=0 ... 這一行的 -n 這個選項代表不載入 php.ini 設定檔,因此 pcre.so  就沒有被載入, 然後你就看到上面的錯誤了,所以自己去改一下程式把 -n 拿掉吧.... 拿掉之後,安裝程式應該就可以正常運作了了: pecl install channel://pecl.php.net/bcompiler-0.8 安裝好後,記得要改設定把 bcompiler 這個 extension 載入 > /usr/local/etc/php/extensions.ini bcompiler 全名是 PHP bytecode Compiler,詳細的使用方法可以參考 PHP 網站上的詳細說明: PHP  bytecode Compiler: http://www.php.net/manual/en/ref.bcompiler.php 看不懂的話, bcompiler_write_file() 這個 function 的說明裡面有範例: bcompiler_write_header($fh); bcompiler_write_file($fh, "example.php"); bcompiler_write_footer($fh); fclose($fh); 為了易於使用,我寫了一個簡單的 script,使用方法如下: BENCODER v1.3 - Encode your PHP script using bcompiler Usage: bencoder [-f] [-q] -o FILE    file1.php        bencoder [-f] [-q] -o OUTDIR  file1.php file2.php ...        bencoder [-f] [-q] -o OUTDIR  -s SRCDIR  [-e SUFFIX] [-r] [-c] [-l]   -o FILE   : the file name to write the encoded script               (default to '-encoded.XXX' suffix)   -o OUTDIR : the directory to write all encoded files   -a SRCDIR   -s SRCDIR : encode all files in this source directory   -r        : encode directories recursively (no by default)   -f        : force overwriting even if the target exists   -e SUFFIX : encode the files with the SUFFIX extension only (default: php)               (regular expression allowed, ex: "php|inc")   -c        : copy files those shouldn't be encoded (no by default)   -l        : follow symbolic link (no by default)   -q        : do not print the file name while encoding or copying   -b   -bz2      : compress the encoded files with bz2 (needs bzip2-extension) 想用的人到 這邊  自己抓回去玩玩看吧 # wget http://bencoder.urdada.net/bencoder # chmod 755 bencoder # mv bencoder /usr/local/bin/ 在把程式使用 bcompiler 編碼之後,執行的時候發覺 $_SERVER 以及 $_ENV  的變數都不見了,後來查了資料,才發現要把 auto_globals_jit 關掉 (Off) 才行,請直接修改您的 php.ini : ; When enabled, the SERVER and ENV variables are created when they're first ; used (Just In Time) instead of when the script starts. If these variables ; are not used within a script, having this directive on will result in a ; performance gain. The PHP directives register_globals, register_long_arrays, ; and register_argc_argv must be disabled for this directive to have any affect. auto_globals_jit = Off 
 原文出處:  使用 bcompiler 來編譯(加密)您的 PHP 原始碼 | Dada's Blog
	
	  | 
 
	| 
	
	
	 | 
 
	 冷日 (冷日) | 
	發表時間:2010/11/18 4:04 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- [轉貼]使用bcompiler將PHP加密
 
		-   發文者 小王子 於 星期三, 五月 19, 2010
公司最近說要將PHP原始碼加密@_@,我之前壓根都沒想到這事,所以今天花了一整天拜請google大仙,找到有一種使用bcompiler的方法,趴了官方文件後,索性就寫了一個加密的class,或許以會有機會用到。
 
  //載入函式 include_once('phpCodeZip.php'); //建立加密物件 $encryption  = new PhoCodeZip('sourceDir','targetDir'); //執行加密 $encryption->zip(); //如此一來,你就可以在targetDir找到經過加密的PHP原始碼   /*   * @license:MIT & GPL   */ class PhpCodeZip{     //欲進行壓縮加密的來源資料夾     var $sourceDir = '.';     //進行壓縮加密的存放的資料夾     var $targetDir = 'tmp';     //是否進行加密     var $bcompiler = true;     //是否去除空白註解斷行     var $strip = true;     //來源資料夾檔案路徑陣列     var $sourcefilePaths = array();     //目的資料夾檔案路徑陣列     var $targetPaths = array();     //進行壓縮加密前的資料夾大小     var $sizeBeforeZip = null;     //進行壓縮加密後的資料夾大小     var $sizeAfterZip = null;     //斷行的輸出     var $newline = ''; /**  *  建構子  *  * @param string $sourceDir 來源資料夾  * @param string $targetDir 目的資料夾  * @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;         //檢查來源資料是否存在           if(!is_dir($this->sourceDir)){             die('指定的來源資料夾'.$this->sourceDir.'不存在,請重新設定');         } else {             //如果指定的目的資料夾存在,砍掉重練             if(is_dir($this->targetDir)){                 echo '【初始化目的地資料夾】'.$this->newline.$this->newline;                 $this->cleanDir($this->targetDir,true);             }             //建立與來源資料夾結構一樣的目的資料夾             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);             }             //取得來源資料夾的檔案路徑清單             $this->sourcefilePaths = $this->getPaths($this->sourceDir,'*');             //配對應目的地的檔案路徑清單             foreach($this->sourcefilePaths as $key => $path){                 //設定目的資料夾檔案路徑                 $path = explode('/',$path);                 $path[0] = $this->targetDir;                 $this->targetPaths[$key] = join('/',$path);             }             //記錄執行前的資料夾大小             $this->sizeBeforeZip = $this->getSizeUnit($this->getDirSize($this->sourceDir),2);             echo $this->newline.$this->newline;         }     } /**  *  進行壓縮加密  * @return boolean  */     public function zip(){         $this->newline = '';         echo '【開始進行加密程序】(資料夾大小:'.$this->sizeBeforeZip.')'.$this->newline.$this->newline;         //將來源檔案進行壓縮         foreach($this->sourcefilePaths as $key => $path){             if(is_file($path)){                 //取得檔案資訊                 $pathInfo = pathInfo($path);                 echo '讀取來源檔:'.$path.$this->newline;                 //取得壓縮後的內容                 echo '=>去除空白註解..........';                 if($this->strip && $pathInfo['extension'] == 'php'){                     $fileAterZip = php_strip_whitespace($path);                 } else {                     $fileAterZip = file_get_contents($path);                 }                 echo '完畢'.$this->newline;                   //取壓縮後的內容寫到目的位置                 $fp = fopen($this->targetPaths[$key],'w+');                 echo '=>寫入目的檔..........';                 fwrite($fp,$fileAterZip);                 fclose($fp);                 echo '完畢'.$this->newline;                 //是否若選擇進行加密                 if($this->bcompiler && $pathInfo['extension'] == 'php'){                     echo '=>加密原始檔..........';                     //複製原始檔                     $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);                     //刪除未加密的原始檔                       unlink($this->targetPaths[$key]);                     //重新命名加密過後的檔案                       rename($this->targetPaths[$key].'encrypt.php',$this->targetPaths[$key]);                     echo '完畢'.$this->newline;                 }                 echo $this->newline.$this->newline;             }         }         //重新計算壓縮加密後的資料夾大小         $this->sizeAfterZip = $this->getSizeUnit($this->getDirSize($this->targetDir),2);         echo '【結束加密程序】'.$this->newline.$this->newline;           echo '《報告資訊》'.$this->newline;         echo '來源資料夾:'.$this->sourceDir.'('.$this->sizeBeforeZip.')'.$this->newline;         echo '目的資料夾:'.$this->targetDir.'('.$this->sizeAfterZip.')'.$this->newline;         echo '檔案大小增幅:+'.$this->getSizeUnit(($this->getDirSize($this->targetDir) - $this->getDirSize($this->sourceDir))).$this->newline;         echo '檔案總數:'.count($this->sourcefilePaths).'個'.$this->newline;       } /**  *  刪除目錄夾所有檔案  *  * @param string $dir 欲刪除的資料夾  * @param boolean $deleteSelf 同時刪除資料夾  * @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);         }     } /**  *  取得資料夾的總檔案大小  *  * @param string $dir 欲剖析的資料夾  * @return int 位元組  */     private function getDirSize($dir='.'){         //取得檔案路徑清單         $filePaths = $this->getPaths($dir,'*');         //初始化計數器         $sizeCounter = 0;         foreach($filePaths as $key => $path){             $sizeCounter = $sizeCounter + filesize($path);         }         return ($sizeCounter);     } /**  *  取得資料夾所有配對的路徑  *  * @param string $start_dir 欲剖析的資料夾  * @return array 檔案路徑陣列  */     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;     } /**  *  檔案大小單位轉換函式  *  * @param int 檔案大小  * @param int 小數點位數  * @param boolean 是否要將資料切成陣列  * @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++;         }         //是否要將數值與單位分開         if($split){             $sizeUnit = array(                 'size' => number_format($size,$decimal),                 'unit' => $unit[$flag]             );         } else {             $sizeUnit = (number_format($size,$decimal)).$unit[$flag];         }         //回傳大小與單位         return ($sizeUnit);     }  原文出處:我的網頁設計: 使用bcompiler將PHP加密 - 網頁設計教學,網站規劃,平面設計分享,PHP程式,SEO,CSS,FLASH設計,排前技術 
	 
	 | 
 
	| 
	
	
	 | 
 
| 
 
 
 | 
 
	 冷日 (冷日) | 
	發表時間:2010/11/18 4:18 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- [分享]CentOS5上安裝bcompiler的方法
 
		- 來分享一下冷日在CentOS5上安裝bcompiler的方法:
 1.安裝bzip
  2.安裝php-pear
  3.安裝php-dev
  4.安裝bcompiler
 pecl install channel://pecl.php.net/bcompiler-0.8  5.編輯php.ini
  在最後面加入一行如下
  6.重新啟動Apache
 /etc/rc.d/init.d/httpd restart  7.檢查bcompiler是否有順利掛載成module
  應該要看到以下這一行
  8.也可以寫一頁phpinfo來檢查 應該要看到:
 
 
 | bcompiler support | enabled | 
|---|
 
 | bcompiler version  | 0.8s  |  
 | current bytecode version  | 0.14  |  
 | can parse bytecode version  | 0.7, 0.9, 0.11, 0.12, 0.14  |  
 
 
  剩下的,就可以參考上面前輩所寫得Code來替你的php做壓縮加密啦! 
	 
	 | 
 
	| 
	
	
	 | 
 
 
 
 
 
                       |