Quote:
Usage: phptwister.php <options...> <file>
If <file> will be obmitted stdin will be used as source.
All options have the syntax: -X value or -Xvalue.
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
-t <target>: selects the specified target for -d4
-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, 1=string, default=3
-z <1,2>: compress output, 1=enable gzip, 1=enable base64, default=0
-f <config.conf>: read options from config.conf
-o <out.php>: write final to out.php
-1 <stage1.php>, -2 <stage2.php>, -3 <stage3.php>, -4 <stage4.php>,
-S <scan.php>, -F <files.php>: debugging output
First option: -f <config.conf> to load the default configuration from the file config.conf instead of using the builtin. (The file default.conf of the distribution contains the builtin configuration.) All options can be overwritten by the command line.
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.
If -1 <stage1.php> is used, the actual state will be dumped to stage1.php.
The option -d <1,2,4> allows you to replace defines with their definitions.
Option 1 revoves all user defines with define('DEF', 'INE') where 'DEF' has to be a single constant string and 'INE' a single constant string, a single positive integer, a single positive float, a boolean constant (true, false) or null. And all occurencies of DEF will be replaced with 'INE'.
Option 2 replaces all systemwide defines with their definitions.
Option 4 you do enable the target replacement. First you have to submit a list of all targets in a config-file (see -f) or use the default's RELEASE and DEVELOP. By default the target to generate is the first in the config. This can be overwritten with the -t option. The selected target will be replaced by true all others with false.
You should place something like the following in your code:
define('DEVELOP', true);
define('RELEASE', false);
So, in a regular run DEVELOP is true but it is RELEASE in the generated code.
The option -d4 requires that -d1 is enabled.
The option -p <1,2,4>: with 1 the packit functionaluty is enabled. 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.
With 2 the gzip compression is disabled, if you don't want to use it. (maybe your php can't use it)
With 4 the base64 encoding is disabled.
A second feature wich is also enabled by 1 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.
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 <stage2.php>.
With -r <1> you can enable to remove unused functions, classes, methods, members from the code. Everything named in config: function, class, method, member will not be removed.
With the option 2 all if statements will be evaluated, if possible. Which means that if the statement are only constants and operators (or, and, +, ...) and no funcitions or variables the block will maybe killed or all following will killed. See the 2. demo.
The alternative syntax for control structures is also supported.
Note: With -d 1 you can replace defines by their value (e.g. false) and then let this function remove it.
After finishing this loop a third stage can be written on disk with -3 <stage3.php>.
Now -n <1,2> is on work. With the option 1 allmost all function, variable, class, method and member names are replaced by a shorter (and less readable) one. Now the config-file is important! First all $this inside a method will never be replaced. Please never add $this to any config value. Don't add $GLOBALS to the config, this variable is handeled especially.
All variables wich are defined in config: superblobals will not be changed.
All global variables which are defined in config: globals will not be replaced.
All other global variables will replaced.
All local variables which are not noted in config: local will be replaced.
All function names which are not noted in config: function will be replaced.
All class names which are not noted in config: class will be replaced.
All method names which are not noted in config: method will be replaced.
All member names which are not noted in config: member will be replaced.
A language constucts named in config: language will never be used for a function, class, method or member name.
Option 2 enables name in string replacement. If you use callback functions or variables/functions in a string, which will be evaluated you have to mark them so they can be replaced.
Example: eval('$x = dummy(0,1);'); Now you must mark the variable/function name in the text so that they can be replaced. To mark, write the comment /*VAR*/ before a string, which contain ONLY the variable name, with or without the $ sign. Use /*FUNCTION*/ for functions.
Usage: eval(/*VAR*/'$x'.' = './*FUNCTION*/'dummy'.'(0,1);');
Usage: $a = /*VAR*/'b'; $$a = 'this changes $b';
See -S for debug.
Now a fourth stage can be written on disk with -4 <stage4.php>.
Use -o <out.php> to write the resulting code without any comment and only a minimum of whitespaces to disk. The switch -z <1,2> contols the encoding of the output. The option 1 enables gzip compression, which makes the code much smaller. It requires gzip decompression in the php which should interpret your code. And it's not longer ASCII source, which may hve some pitfalls. To make it ASCII compatible again, you may want to use option 2 to enable base64 encoding.
With -F <files.php> will write a php code which contains an array of all files which are included or loaded while fileloader and a number which shows how often the file was loaded.
With -S <scan.php> will write a php code which contains an array of all name replacements which are made.
default.conf