This text is from my arcives. It may be out of date, but I thought it should be preserved.This tool loads all files required by a project (including binaries) into one file.
All unused functions will be removed, all function and variable names will be replaced
by shorter (and unreadable) ones, all not needed whitespaces will be removed, debugging code will be removed, ...
You'll get informed when a global or local variable is not used.
The resulting file is still a regular php-file. (No extra softeate is required!) Requirements: PHP 4.3.0 and up, CLI or CGI (developed under 5.0 beta)
You have to use it from a shell and it requires much memory. (The coding of twister [60kB code = 2k5 lines] needs about 8MB).
Here is an basic example output. (currently n/a)
Here is an complex -r2 example output. (currently n/a)
Get version 0.9 tar.gz (about 20 KB)
(Note: in the demo -n, -r2, -d4, -t, -S and -F are disabled) Please mail me, and tell me what do you think about it.
Usage: php twister.php
Some options are bit masks. Example usage: -X <1,2,4> you have to add the number of all features you want to use. For 1 and 4 use -X 5. To diable all -X 0. If a option is omitted the default will be used. (Either builtin or from a config-file, see -f.) Some functions are controlled by comments, you have to use exact that comment. (But you can add whitespaces outside of the comment like you want.)
Here is a quick overview about the options.
-i <1>: include other code, 1=on, default=1
-d <1,2,4>: replace defines, 1=user, 2=system, 4=target, default=3
-r <1,2>: remove unused code, 1=on, 2=optimize if, default=3
-p <1,2,4>: packit/fileloader, 1=on, 2=diable gzip, 4=disable base64, default=3
-n <1,2>: name substituation, 1=on, 2=string, default=3
-z <1,2>: compress output, 1=enable gzip, 2=enable base64, default=0
-c <1,2>: replace variable constants, 1=originals, 2=dummy, default=1
The config-file contains the defaults for all options in the config: opts section.
All other sections are described later. In the config: opts section is now a way to specify the input file.
What do the colors mean?
Command line options, References to the config-file, Example code, Things that don't work yet In the left column is the described option noted, in the right column the config-file.
|Here is a detailed description, what the tool will do in which order, and what options can be used:|
First option: -f
|The main script will be load and parsed.|
Option: -i <1>. Now all files included by the main file will be also parsed and included. You have to use the command include, include_once, require, require_once with exactly one string as parameter, a trailing semicolon and NO whitespaces or comments.
Example: include('file.php'); will work, include($file.'.php'); will not be replaced, cause the tool don't know what's in $file. To disable it for one include you may use include('file.php' );. See -F for debug.
Multiple paths can be entered, seperated by a colon.
The default path is ./, where ./ is the path of the file to twist, which must not be the actual directory. In case of your are using stdin the current directory will used.
The option -d <1,2,4> allows you to replace defines with their definitions.
With the option -c <1,2> you can switch the replacement of variable constants on.
Variable constants are: __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__.
The option -p <1,2,4> selects the packit functionality.
A part of the code will be evaluated now and replaced by a packed version. Only strings and pure constant arrays can be packed. (For arrays (un)serialize will be used.) A gzip compression will be used if it decreases the length. At least base64 encoding is used, so it's a plain ASCII text.
Code: $x = /*PACKIT*/ array(0=>'big array', 1=>'really big') will be replaced by $x = P(1, 'XXXX...') The name of the function is PHP_CODE_TWISTER_PACKIT_FUNCTION, not P, which will be created automatically. So the code is less readable.
A second feature wich is also enabled by -p1 is the fileloader function.
Code like $x = /*FILELOADER*/ file_get_contents('file.gif') will be replaced by the contents of the file, like packit: $x = P(1, 'XXXX...'). So now the file is IN your php program. Note: only a single constant string as parameter for file_get_contents is supported. Alternative fileloader usage: $x = /*PACKIT*/ array( filemtime('file.gif'), file_get_contents('file.gif') ) will be replaced with something like this: $x = array(105523325, P(1, 'XXXX...')). The switches 2 and 4 are also used by the fileloader.
|-p||With /*PACKIT:X*/ and /*FILELOADER:X*/ you can set the -p options to X for this call. The option 1 is ignored for now, but should be set. Example: $x = /*PACKIT:7*/ 'Hello'." ".'World'."\n" will be packed, but neither gzip nor base64 encoded, the result is a single string.|
Now a second stage can be written on disk with -2
|The following loop will remove unused code.|
After finishing this loop a third stage can be written on disk with -3
Now a fourth stage can be written on disk with -4
|Now we're trough. At last you can get two debugging files:|