Intro
We all know that PHP is designed to quickly create dynamic Web pages. In any case, this is what is written on the official site of PHP developers. However, why not try to find PHP "a little" another use, for example, try to create a full-fledged application for Windows? Interested? Then rushed!
Black square
Applications are divided into console and window. Let's start with the creation of the first, as it is easier. So, first we need to decide on what we will write ... I suggest writing a simple MD5 & Base64 Encoder - a necessary and useful thing in the household, especially since everything is quite simple. To run the PHP script from the command line and transfer the necessary parameters to it, use the following construction:
php.exe [options] [-f] <our script> [parameters]
As for the contents of the script itself, the number of parameters passed is stored in the $ argc variable, and the parameters themselves are stored in the $ argv array. This concludes the briefing on the console applications and it's time to start taking action.
')
In order not to bother you with writing a script, I decided to write it for you. That's what i have
happened:
<? php
if ($ argc! = 3) {echo "
+ -------------------------------------- +
| Create MD5 hash: enc md5 string |
| Create Base64 hash: enc b64 string |
+ -------------------------------------- + \ n ";
} else {
echo "\ n";
if ($ argv [1] == 'md5') {// MD5 encode
echo md5 ($ argv [2]). "\ n \ n"; die ();
}
if ($ argv [1] == 'b64') {// Base64 encode
echo base64_encode ($ argv [2]). "\ n \ n"; die ();
} else {echo "Unknown command. Type \ "enc \" for help. \ N \ n "; }
}
?>
<img src = "
habrastorage.org/getpro/habr/olpictures/cf6/1d1/61b/cf61d161b4a795fc4c658d661d96fdae.gif " width = 450 height = 186 border = 0 alt = Demonstration of the script "hspace = 10 vspace => 186
The script is written and now the most interesting is a compilation into an EXE application. To compile console applications, I use Roadsend Compiler Studio. It can be downloaded at
www.roadsend.com after free registration. Downloaded? Now open the Roadsend IDE, create a new project (Project -> New), set the necessary parameters (I recommend throwing our script to the project directory), click “OK” and finally give the cherished Project -> Build. Now we are heading to the project folder, and if you did everything right, you will find a ready-made EXE in it. It's all? Yes. It's simple, if you know some of the nuances. The only thing I would recommend is that sometimes it is useful to mess with plug-in libraries, and you can also pack our EXE UPX’s, although I’ll touch on this topic just below. Now let's go to the window applications ...
Windows
As you probably already guessed, when creating window applications using standard PHP tools, you can’t do it any more, so first you need to choose a tool for developing our window. Among all the tools, two stand out the most - it is a well-promoted PHP-GTK (www.gtk.php.net) and a less well-promoted, but no less functional WinBlider (www.winbinder.org). Here is the case when the more promoted and well-known is not so good (this is my IMHO), so we will use WinBlinder to develop our first vent for PHP. First you need to download it. Is it done? OK, move on ...
Winblinder has its own data format - phpw, which you can immediately bind to your favorite code editor. By default, when you run a phpw file, the PHP script contained in it opens and is run through the interpreter with the already connected blinder libraries. As you probably understood, you will have to learn how to program in this balalaika yourself, but this is not difficult, especially to a person who is at least a little familiar with OOP in PHP. However, even a stranger will also be able to code on it, since in essence the blinder (unlike PHP-GTK) also supports procedural programming. In addition, WinBlinder is very well documented, and the installation archive already contains some very interesting examples (please pay special attention to the form editor). The binder has a lot of chips, functions and interesting features, but the framework of this article does not allow me to dwell on them. Now perhaps the best time to remember about our coder. In order not to overload all the code, I decided to write only the MD5 Encoder:
<? php
// Connect the main library
include "include / winbinder.php";
// Give labels to edit windows
define ('IDC_EDITBOX1002', 1002);
define ('IDC_EDITBOX1003', 1003);
// Draw a window
$ winmain = wb_create_window (null,
AppWindow,
'MD5 Hash Generator by S1B [SBT]',
WBC_CENTER, WBC_CENTER, 315, 134,
0x00000000, 0);
// Skipping windows with buttons
# Draw a frame
wb_create_control ($ winmain, Frame, '', 10, 10, 290, 85, 0, 0x00000000, 0, 0);
# Draw inscriptions (labels)
wb_create_control ($ winmain, Label, 'Text:', 23, 35, 45, 15, 0, 0x00000000, 0, 0);
wb_create_control ($ winmain, Label, 'Hash:', 23, 65, 45, 15, 0, 0x00000000, 0, 0);
# We draw editing windows (the bottom will be inactive), and we drive their data into variables
$ text = wb_create_control ($ winmain, EditBox, '', 78, 30, 210, 20, IDC_EDITBOX1002, 0x00000000, 0, 0);
$ hash = wb_create_control ($ winmain, EditBox, '', 78, 60, 210, 20, IDC_EDITBOX1003, 0x00000040, 0, 0);
// Two required labels =)
wb_set_handler ($ winmain, "process_main");
wb_main_loop ();
function process_main ($ window, $ id) {
# Declare data editing windows global
global $ text, $ hash;
switch ($ id) {
case IDC_EDITBOX1002:
wb_set_text ($ hash, md5 (wb_get_text ($ text)));
break;
case IDCLOSE:
wb_destroy_window ($ window);
break;
}
}
?>
The code is quite simple and I think after sitting with the documentation for 1-2 hours you will start to understand it just like me. Well, now the climax - we collect a window leaf ... For this I propose to create a separate directory and transfer our script there. You also need to transfer the include folder from the phpcode directory of the winblinder. Is it done? Now compile! Uh ... stop, you don't have to grab Roadsend IDE. How? That's how! It does not support Blinder, but only supports GTK, and judging by the hazy answers of the developers of Roadsend on their forum - the support of the blinder can be a long time coming. However, wipe snot, and we will look for other ways, and the name of these ways is Bamcompile (www.bambalam.se/bamcompile/). However, for some reason, version 1.21 does not always work correctly and sometimes compiles everything into a non-working application, however version 1.1 does not suffer from such glitches, therefore I recommend using it. Now generously select for our compiler a separate folder, go to the command line and proudly command:
bambalam -w -c C: \ PHP_Projects \ MD5_Hash_Generator \ md5.phpw md5.exe
If everything is done correctly, then in the directory with the compiler there will be a freshly split window application. Everything. I will only add that I would recommend all applications to be packaged with UPX (which comes directly from Bamcompile).
Magic casket
All of the above is certainly very interesting and informative, but there are times when the right tools are not at hand ... What to do then? Especially for such a case, there is one very curious method of obtaining an EXE through shamanism in the SFX options of the WinRAR archive. As an example, I will use our first script for the console. To begin with, we place our script in a separate folder, copy php.exe and php * ts.dll there. Next, create in the same folder run.bat as follows:
@echo off
php.exe -q console.php% 1% 2
Now we need to pack this stuff, to do this, select all the files, right-click and select "Add to archive ...".
In the window that appears, select the format of the RAR archive, set the maximum compression and tick the “Create SFX archive” checkbox.
After that, go to the “Advanced -> SFX Options” fold, set the switch to the “Create in curent folder” there and set “Run.bat” in the “Run after extraction”, as well as in the “Modes” tab set the “Unpack to temporary folder ”, as well as expose the switches“ Hide all ”and“ Overwrite all files ”.
To taste, you can stick your icon in the appropriate tab. Now we confirm the creation of the archive and at the output we get the EXE without any compilers.
Outro
So, today I showed you how you can easily and simply push the boundaries of using PHP. Of course, someone would call all this nonsense, they say the size of applications is large, and their memory is flowing, and all this is a chore. Let them say, but no one calls you to write all window applications in PHP, and this article is only a good help for php coders and all interested. On this I will finish. Good luck.