It's been a while since my past post, but yes, I'm still using bcompiler. I just found another problem that I think you should be aware of. It took me hours to find.
It pretty much comes to this: When compiling on a 64-bit Linux operating system, your compiled code will NOT run on a 32-bit version, and visa versa. It will merely echo the file's content.
Regards,
Albert Kok
VIII. PHP バイトコードコンパイラ (bcompiler)
導入
| 警告 |
この拡張モジュールは、 実験的 なものです。この拡張モジュールの動作・ 関数名・その他ドキュメントに書かれている事項は、予告なく、将来的な PHP のリリースにおいて変更される可能性があります。 このモジュールは自己責任で使用してください。 |
Bcompiler は、以下のような目的で作成されました。
| プロプライエタリな PHP アプリケーションのスクリプト全体を エンコードするため |
| プロプライエタリな PHP アプリケーションの一部のクラスや関数を エンコードするため |
| クライアントのデスクトップで動作する php-gtk アプリケーションを、 php.exe を用いずに実行させるため |
| PHP から C へのコンバータの実現可能性を調べるため |
2 番目の目的を実現するには bcompiler_write_header()、 bcompiler_write_class()、 bcompiler_write_footer()、 bcompiler_read() および bcompiler_load() 関数を使用します。 バイトコードのファイルが、圧縮されていないか あるいはプレーンな形式で書き出されます。 bcompiler_load() は、bzip で圧縮された バイトコードファイルを読み込みます。これは元のファイルに比べて 1/3 程度の大きさになります。
EXE 形式のファイルを作成するには、修正された sapi ファイルか 共有ライブラリとしてコンパイルされた PHP とともに bcompiler を使用する必要があります。この場合、bcompiler は 圧縮されたバイトコードを exe ファイルの後ろから読み込みます。
圧縮しないバイトコードのみで使用した場合、bcompiler によって 処理速度を約 30% 向上させることが可能です。しかし、圧縮しない バイトコードは元のソースコードに比べて 5 倍程度の大きさに なることに気をつけてください。バイトコードを圧縮することで 容量を節約することが可能ですが、圧縮ファイルを展開するのには ソースコードをパースするよりはるかに長い時間がかかります。 bcompiler はバイトコードに対する最適化を一切行いません。 これは将来のバージョンで対応する予定です…。
コードの保護という点に関しては、もとのソースコードやコメントを 復元することは不可能であると考えて間違いありません。bcompiler のバイトコードをもとにしてコードを復元し、クラスに手を加える といったことは無意味です。しかし、bcompiler で作成した バイトコードファイルからデータを取り出すことは可能です。 個人的なパスワードなどの情報をバイトコードの中に含めないでください。
インストール手順
簡単なインストール手順
圧縮機能を動作させるには、少なくとも PHP 4.3 が必要です。
PHP 4.3 以降のバージョンに unix コマンドプロンプト上で インストールするには、pear install bcompiler とタイプします。
Windows 上でインストールする場合、バイナリパッケージを配布する 仕組みが整うまでは、pear-general メーリングリストのアーカイブから ビルド済みパッケージを探してください (あるいは、もし見つからなければ メーリングリストにメールを送ってください)。
古いバージョンにインストールするには、ビルドするために 多少手を加える必要があります。
bcompiler.tgz アーカイブを php4/ext に展開します (これは、PECL http://pecl.php.net/get/bcompiler から取得可能です)。
新しく作成されたディレクトリが bcompiler-0.x のような名前に なっていれば、それを bcompiler という名前に変更します (単に PHP モジュールのみをビルドしたいのであれば、これは不要です)。
PHP 4.3 より前のバージョンを使用している場合は、 Makefile.in.old を Makefile.in に、そして config.m4.old を config.m4 にそれぞれコピーする必要があります。
ext/bcompiler で phpize を実行します。
php4 で ./buildconf を実行します。
--enable-bcompiler (およびその他のオプション) を指定して configure を実行します。
make; make install
これで終わりです。
連絡先
コメント・バグ修正・機能拡張の提案や、 開発を手伝ってくださるという方は、alan_k@php.net までメールをください。お待ちしています。
- 目次
- bcompiler_load_exe -- bcompiler の exe ファイルを読み込み、クラスを生成する
- bcompiler_load -- bz 圧縮されたファイルを読み込み、クラスを生成する
- bcompiler_parse_class -- クラスのバイトコードを読み込み、ユーザ関数をコールする
- bcompiler_read -- ファイルハンドルを読み込み、クラスを生成する
- bcompiler_write_class -- 定義したクラスをバイトコードとして書き込む
- bcompiler_write_constant -- 定義した定数をバイトコードとして書き込む
- bcompiler_write_exe_footer -- 開始位置および exe 形式ファイルのフッタを書き込む
- bcompiler_write_file -- php ソースファイルをバイトコードとして書き込む
- bcompiler_write_footer -- コンパイルされたデータの終了を示す文字 \x00 を書き込む
- bcompiler_write_function -- 定義した関数をバイトコードとして書き込む
- bcompiler_write_functions_from_file -- ファイル内で定義されているすべての関数を バイトコードとして書き込む
- bcompiler_write_header -- bcompiler のヘッダを書き込む
try this, I believe that it will solve the problem with __FILE__
<?php
eval("\$Foo = dirname(__FILE__);");
echo "\n<br>FILE = '$Foo'\n";
?>
A few notes to succesfully configure PHP5 to load bcompiler.
In case of using Debian/Ubuntu Linux: When installing from the PECL (PEAR) package, make sure you have the php5-dev library installed in your system (apt-get install php5-dev).
Newer versions of Ubuntu may error out while installing bcompiler thru PECL, asking you to reinstall bzip2. This will not fix the installation. Instead, install the libbz2-dev library (apt-get install libbz2-dev) and try installing bcompiler from PECL again.
After installing bcompiler from PECL, don't forget to make sure you load the bcompiler.so library in your PHP configuration for the new bcompiler functions to become available. This is done by adding the following line to the bottom of your php.ini file(s):
extension=bcompiler.so
If you were to use the bcompiler functions thru your webserver, you need to restart it to reload the php configuration.
Regards,
Albert Kok
hello guys, since bencoder don't work for __FILE__ constants i have a bcompiler script working, anyone that want it can send me an email
the source is 100% compatible with original file, the only problem is php5 new function methods that
<?
if (!function_exists('function_name')){
function function_name()
}
?>
don't work but it's a php5 new method for understand bytecode
Here's a script to act as a front-end for bcompiler, which may be very similar to the eA ones:
http://bbs.giga.net.tw/bencoder.php
BENCODER v1.1 - Encode your PHP script using bcompiler
Usage: bencoder [-f] -o FILE file1.php
bencoder [-f] -o OUTDIR file1.php file2.php ...
bencoder [-f] -o OUTDIR -a SRCDIR [-s SUFFIX] [-c] [-r]
-f : force overwriting even if the target exists
-o FILE : the file name to write the encoded script
(default to '-encoded.php' suffix)
-o OUTDIR : the directory to write all encoded files
-a SRCDIR : encode all files in this source directory
-s SUFFIX : encode the files with the SUFFIX extension only (default: php)
-c : copy files those shouldn't be encoded (no by default)
PHP seems to choke on trying to compile PHP5 classes with the private, protected or public modifiers. Additionally, when including files with properly compiled classes on PHP5, it works fine but when php tries to exit it throws a segmentation fault.
It's just not a great idea to use bcompiler for anything other than functions on PHP5, at least for now.
Creating a function after checking if a function exists using function_exists will result in a "Zero Sized Reply" error.
<?php
this does NOT work any longer:
if (!function_exists("file_get_contents")) {
function file_get_contents($filename, $use_include_path = 0)
{ /*...define your own function here...*/ }
}
?>
Apache returns: "Zero Sized Reply" after compilation.
