Monday, 29 March 2010

"Bambalam!" or "compile PHP for me!"

This simple article will talk about Bambalam. This is not music. Here is the site of the project:
http://www.bambalam.se/bamcompile/


We can see, in big letters, on the page:
Bambalam PHP EXE Compiler/Embedder


Though PHP is supposed to be a scripting and interpreted language, we can compile it in order to:
- hide code;
- make windows applications and exempt someone to have PHP on his computer;
- (in theory) increase the speed execution.

Life is beautiful, isn't it?!

Concerning the "linuxians", don't worry, you can download the Bambalam source code.

Let's fetch the project's archive, extract it into a folder, and see the content:
- readme.txt : don't care;
- project_template.bcp : don't care;
- bamcompile.exe : the executable which will receive arguments in order to generate executables by php;
- examples: a folder containing full of interestings... examples! :)

To put us at ease, let's crate a "project" directory. In this directory, let's create a "hello" folder. Now, in this last folder, let's create a file called "hello.php". Let's write our code:
<?php
echo 'Hello!';
?>


We will now compile it. First of all, you have the choice between the following options:
- Using the command line in order to place you to the bambalam's folder then entering your options manually;
- Making a batch file which will execute sequentially your commands and keeping from them a trace.

For my part, I choose the second option.

On the project's web-site, we are told this:
Usage:
bamcompile [-options] infile.php [outfile.exe]
bamcompile [-options] project_directory mainfile.php [outfile.exe]
bamcompile projectfile.bcp

Options:
-w Hide console window for windowed applications
-c Compress output exe (using UPX)
-d Do not encode PHP files
-e:extension.dll Embed and use PHP extension
-i:icon.ico Add icon to exe


Let's try the first line in order to compile our unique php file! We will create a batch file called "make.bat" in the working directory and write the following lines:

@echo off
"../../bamcompile.exe" hello.php
rem The next instruction prevent console from closing itself
pause


Yeah, we have to put "../../" because we have to get back in the tree. I don't consider you as idiots, but we have to think about it by reflex.

Then we launch make.bat and we don't have the time to say "Leek". (I love leeks).

Bambalam PHP EXE Compiler/Embedder 1.21

Mainfile: hello.php
Outfile: hello.exe

Encoding and embedding hello.php

hello.exe created successfully!
Appuyez sur une touche pour continuer...


Concerning the last line, it is in French because it's my windows's language. It means "Press a key to continue". :)

Finally, we have a wonderful executable which has been created in the same directory. If we launch it by a double click, it immediately shuts; normal.

Let's see what does it give in command line:
C:\bamcompile1.21\projects\hello>hello.exe

Hello!


Great!

Well, ok, there is an empty line over our "Hello!", but we don't really care.

Now, let's come back in the "projects" folder and create two files into it :
- main.php
<?php
require_once('age.php');
if($_SERVER['argc'] < 2) {
echo "Usage: ".$_SERVER['argv'][0]." [number]\n";
exit(-1);
}
echo ShowFollowingAge(abs($_SERVER['argv'][1]));
?>


I will sum up the code. Firstly we begin in including the "age.php" file (that we'll write after this), we check we've entered an argument in the command line - which corresponds to an age of any person - and next we call the ShowFollowingAge() function. This function is defined in the "age.php" file. Finally, the "intval()" function let us to ensure that the argument is an integer value, and abs() a positive value.

Let's handle the "age.php" file:
<?php
if(!defined("AGE_PHP")) {
define("AGE_PHP",TRUE);
function ShowFollowingAge($var) {
return $var >= 18 ? "You're major" : "You're minor";
}
}
?>


Briefly, I prevent the file from being multi-included (even if it doesn't really care here) and then I define my function(s). The define is here because if we define a function several times - with the same name - the scripts doesn't run. Normal.

The function returns a string: "You're major" if the age is above or equal to 18, "You're minor" in the opposite.

Concerning guys who does not understand the ternary expression, it consists to affect a variable a value following a condition. Example:
$variable = condition ? "value if true" : "value if false";


This code is equal to the following one:
if(condition)
$variable = "value if true";
else
$variable = "value if false";


The last code takes four lines whereas the ternary expression just takes one line. That's why I use this last.

But we shouldn't go away from the main topic!!

Now, we will go back to the bamcompile executable's folder. The we create a "make_age.bat" file and write into it:
@echo off
bamcompile.exe projects/age main.php projects/age/age.exe
pause


Indeed, in the Bambalam's web-site, We are told that we can use:
bamcompile [-options] project_directory mainfile.php [outfile.exe]


By identifying:
project_directory = projects/age ;
mainfile.php = main.php ;
outfile.exe (optional argument) = projects/age/age.exe

Then we launch our batch file and it's the ecstasy (normally)!

Bambalam PHP EXE Compiler/Embedder 1.21

Mainfile: main.php
Outfile: projects/age/age.exe
Project dir: projects/age

Embedding age.exe
Encoding and embedding age.php
Encoding and embedding main.php

projects/age/age.exe created successfully!
Appuyez sur une touche pour continuer...


We execute age.exe with the command line:
C:\bamcompile1.21\projects\age>age.exe 18
You're major
C:\bamcompile1.21\projects\age>age.exe 16
You're minor
C:\bamcompile1.21\projects\age>age.exe aaa
You're minor
C:\bamcompile1.21\projects\age>age.exe -4
You're minor
C:\bamcompile1.21\projects\age>age.exe -18
You're major
C:\bamcompile1.21\projects\age>age.exe 32
You're major


Since the program has'nt a real interest, this is a simple example.


I regret to announce that the fairground ride ends up here. We could say this article busy me in order to present you a tool that I love much; I sent nice executables to friend thanks to it. Funny.

Take a look in the "examples" folder, it's worth it! There are even a calculator programmed with the winbinder extension (http://www.winbinder.org) which let us to make GUI programs. Anyway, It worth it for those who make php scripts and who would like to share with other persons that, themselves, haven't PHP set up in their computer.

That's all. See you soon.

Geo

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey thanks for this tutorial... I'm confused how to make exe of my whole project. I have a directory "myPro" inside that directory I have "index.php" It is a login page of my application. complete login it will redirect to corresponding pages. How to .bat file in this case? I should run it as GUI. any idea?

    ReplyDelete