Ioncube PHP Encoder 6.5 User Guide
Ioncube PHP Encoder 6.5 User Guide
User Guide
Contacting ionCube
Please see our contact details at www.ioncube.com/contact.php
FAQ
Find answers to common questions in our FAQ at www.ioncube.com/faq.php
Support
For online support please visit support.ioncube.com or our online forum at forum.ioncube.com
Purchasing Products
To purchase ionCube products please visit www.ioncube.com/purchase.php
CONTENTS
1 INTRODUCTION ........................................................................................................................ 7
1.1 Encoder Outline .............................................................................................................................. 8
1.2 User Guide Notation ....................................................................................................................... 9
1.2.1 Command Examples............................................................................................................ 9
1.2.2 PHP 4 and 5 Encoders ........................................................................................................ 9
1.2.3 Hints and Tips ...................................................................................................................... 9
2 GETTING STARTED................................................................................................................ 10
2.1 Licensing the Encoder ................................................................................................................. 10
2.1.1 Generating a License Request .......................................................................................... 10
2.1.2 Submitting your License Request ...................................................................................... 11
2.1.3 Specifying the License File ................................................................................................ 11
2.1.4 Transferring a License or Reinstalling a Machine.............................................................. 11
2.2 Running the Encoder.................................................................................................................... 12
2.2.1 Command Line Format ...................................................................................................... 12
2.2.2 Passing Command Line Options ....................................................................................... 12
2.2.3 Filename, Directory and Wildcard Pattern Matching ......................................................... 12
2.2.4 Using Wildcard Characters on UNIX ................................................................................. 14
2.3 Quick-Start Encoding Examples ................................................................................................. 15
2.3.1 Encoding Single Files ........................................................................................................ 15
2.3.2 Encoding Directories.......................................................................................................... 15
2.3.3 Encoding Files with non-default File Extensions ............................................................... 16
2.3.4 Encoding Shell Scripts ....................................................................................................... 16
2.3.5 Encrypting Templates and other Files ............................................................................... 16
2.3.6 Leaving Files Unencoded .................................................................................................. 17
2.3.7 Omitting Files from the Encoding Target ........................................................................... 17
2.3.8 Adding Copyright and License Details to Encoded Files................................................... 17
5 LOADER API............................................................................................................................ 46
5.1 File Information and Execution ................................................................................................... 46
5.1.1 Checking for an Encoded File [ioncube_file_is_encoded]...................................... 46
5.1.2 General Encoded File Information [ioncube_file_info] ............................................ 46
5.1.3 Retrieving Properties Stored in an Encoded File [ioncube_file_properties]......... 46
5.1.4 Retrieving the Loader String Version [ioncube_loader_version]............................. 46
5.1.5 Retrieving the Loader Integer Version [ioncube_loader_iversion]......................... 46
5.2 License and Server Information .................................................................................................. 47
5.2.1 Retrieving Properties Stored in a License [ioncube_license_properties]............. 47
5.2.2 Retrieving the List of Permissioned Servers [ioncube_licensed_servers] ............. 47
5.2.3 Creating a Server Data Block [ioncube_server_data]............................................... 47
7 TROUBLESHOOTING ............................................................................................................. 52
7.1 Unable to Start the Encoder ........................................................................................................ 52
7.1.1 On UNIX............................................................................................................................. 52
7.1.2 On Windows....................................................................................................................... 52
1 INTRODUCTION
The ionCube PHP Encoder is a powerful, high performance solution for encoding and licensing
PHP scripts and encrypting files of any type.
The Encoder protects PHP /HTML scripts with obfuscated bytecode protection and a custom
execution engine. In addition, any other project files can be automatically encrypted if required,
which is ideal for protecting files such as templates or XML documents. This is complemented
by Loader API functions for reading and writing encrypted files. For most existing template
engines, a small change is all that would be required to add the ability to read encrypted
templates, and for Smarty users, we have a ready made patch to enhance Smarty at
www.ioncube.com/resources/encrypted_smarty_patch.phps
The Encoder achieves script protection by first compiling and then optimising PHP scripts to
highly efficient binary data. This compilation process replaces source with virtual-machine
instructions and applies several layers of encoding and transformations to produce the final
platform independent files. Optional function name and local variable binary obfuscation adds
extra protection, with further internal obfuscations applied at compile and runtime. This
approach has the advantage that files are never restored to PHP source, compiled code is
changed and hidden from the Open Source PHP engine, and gives excellent run-time
performance due to parsing and compilation taking place at encoding time. Other Encoder
features allow for the easy addition of plain text to the start of files, which is ideal for including
custom copyright or license details, and is protected against tampering.
Create license files for your projects with the Pro and Cerberus Encoders to lock your projects to
particular machines with optional time expiry. Applications can also have custom behaviour
based on license file properties if required.
ionCube Loader
The ionCube Loader supports execution of encoded PHP files, encrypting or decrypting general
files, validating licenses, and so on. This component can be easily installed into the php.ini file,
or on many servers, automatically installed when required by the scripts themselves. This is
ideal for shared servers where users often do not have access to the php.ini file. The Loader
is also compatible with other popular extensions, and tools are provided to assist installation.
Windows GUI
For Windows users, a powerful GUI, described online and in the package, makes setting up
projects a breeze. As well as the expected encoding features, integration with Explorer adds
usability features such as dynamic icons to distinguish between encoded and unencoded files at
a glance, and quick right click encoding with a minimal cut-down GUI. Upgrade to the Special
Edition GUI for features such as automatic archive creation, FTP, and the unique dynamic fields
feature that dramatically simplifies data entry for custom encoding and license creation through
a dynamically created custom interface. Together with other features, the GUI helps to deliver
the best productivity and flexibility possible.
$ ioncube_encoder -V
Shell input is shown in bold, and program output is plain. $ is an example shell prompt for
command entry but this may be different on your own system.
You can use the bookmark feature in Adobe Acrobat to quickly navigate the user
guide, or click on items in the contents section.
2 GETTING STARTED
This chapter introduces some of the most common features of the ionCube command line
Encoder, with quick-start examples for typical scenarios. For Windows users, an additional
document describes the Windows GUI. Chapter 3 will then describe all command line options in
detail, and will be a useful reference guide to features even if using the Windows GUI. The
License generation program is described in chapter 4, and chapter 5 describes the Loader API,
including features for reading and writing encrypted files.
If reading this guide using Acrobat, you can also use the Acrobat bookmarks feature to quickly
jump to different sections, and in the document you can click on hyperlinks, entries in the
contents section, and on any section references.
This is not a problem. Although the license request files show an IP address, this is only help
you identify the adapter that you want to license to. IP address is not part of the license data.
Example:
$ ioncube_encoder --license-request
Windows
On Windows, the Encoder Installer creates a program group icon for generating license requests.
Click the “Start Menu”, and in the ionCube program entry, select the “License Request
Generator” item. On Vista you should be logged in with administrator privileges.
The Encoder expects to read license data from a file named licdata.txt in the same
directory as the Encoder itself.
To transfer a license, a revoke file should first be generated either by using the
--revoke-license Encoder command line option, or on Windows, with the Delete License
item on the Windows Start/ionCube program group menu.
NOTE: The revoke must be performed on the machine that is currently licensed, and not on the
new machine!
The revoke file will be created in the Encoder installation directory, and should be emailed to
[email protected]. Once the revoke file has been processed a new license request can be
generated and a new license downloaded.
or
ioncube_encoder [options] sources --into target
The -o option encodes source as target, where source and target are either both
single files or directories. Using –-into the Encoder sources can be one or more files or
directories that are encoded into the target with the same name. See sections 2.3 and 3.1 for
more details.
To syntax check use -S and specify one or more files or directories
--add-comment=”Encoded by ionCube”
Encoder options may be abbreviated to the shortest string that makes them unique.
The Encoder will report an error if there are multiple choices.
Matching Files
Examples:
Encode all files with default extensions and any files named x.inc
--encode x.inc
Encode all files with default extensions and any files named config/config.inc
--encode config/config.inc
Directories
Example:
Wildcard Matching
The Encoder also supports wildcard matching using the wildcard characters *, ? and [].
Examples:
Encode files with default extensions and any files ending in .inc
--encode “*.inc”
Encode any files named config having one character after the name
--encode “config?”
Use
--encode “*.ini”
--encode \*.ini
Instead of:
--encode *.ini
Files can also be encoded into a directory using –-into to name the target directory. The
encoded file is will be given the same name as the source file.
More than one source item can be specified when using –-into but giving a directory as the
source is usually more convenient.
Examples:
Recursively encode /project as /encoded-project
ioncube_encoder /project –o /encoded-project
or
ioncube_encoder /project –-into /encoded-projects
If the target directory already exists then you must specify how the Encoder should proceed. The
available choices are:
Example:
Encode files with default extensions and also any ending in .inc
ioncube_encoder –-encode “*.inc” /project –-into /encoded-projects
where the text inside the quotes is an example shell script line. Any encoded script can be used
as a shell script by manually adding a shell script line to the encoded file after the encoding
process is complete, and similarly an encoded PHP shell script will remain a valid encoded PHP
script if the shell script line is removed. On UNIX it is important to enclose the option argument
in single quotes as the ‘!’ character has a special meaning for the shell.
As an example of working with encrypted templates, a simple patch to the popular Smarty
template engine is available on the ionCube website, allowing the Smarty engine to process both
plain text and encrypted templates.
Example:
Encode PHP files with default extensions and encrypt files ending in .tpl
ioncube_encoder --encrypt “*.tpl” /project –-into /encoded-projects
Examples:
Example:
Use the –v option to check what files are being encoded, encrypted, copied, or
ignored when using –-encode, --encrypt, --copy, --ignore and --keep.
Example:
ioncube_encoder –-add-comment “Software written by FooSystems” -–add-comment
“Licensed to SomeCompany Inc.” /project –-into /encoded-projects
This chapter describes all available command line options grouped by their purpose.
To protect against accidental error and possible loss of source files, the Encoder checks for being
asked to encode directories that lie within the target tree or encoding into a directory that lies
within the source tree. If encoding into the source tree is required then the option
--allow-encoding-into-source may be used, see 3.15.2 below.
Examples:
This last example would be useful if you had used --copy tests to tell the Encoder to copy
the directory tests, but where you want the subdirectory encoded to be encoded. Note that this
does not request all files to be encoded, but ensures that any files matching the default
extensions or other file patterns will be.
Copy files in directory config but still encode files in any subdirectories
--copy “config/*”
The Encoder applies the options above in the order that they appear. Combining them
can achieve precise control over which files are to be encoded, encrypted, copied, or
that will be excluded. The –v option is useful to see the effect of these options and will
show how files were processed.
This option can also be used with any combination of the tokens functions, linenos,
locals, to allow the obfuscation of any combination of functions, line numbers and local
variables. Separate tokens with a comma and no whitespace.
Example:
Obfuscate line numbers and function names but not local variables
--obfuscate linenos,functions
--expire-in <period>
Examples:
Note that the expiry period is relative to the time that files are encoded.
--expire-on <yyyy-mm-dd>
Example:
Each server specification can contain as many domain names and IP addresses as desired. If
using Cerberus, a MAC address restriction may also be given. At least one type of restriction
must be specified, but items are optional. The option may also be used more than once to specify
multiple restrictions.
1
The server name is an attribute set by the web server for each domain or virtual host, and is the value accessed in
PHP as $_SERVER[‘SERVER_NAME’].
Specify domain names separated by commas and optionally ending the list with an @ character.
Examples:
Note the trailing @ after the name in this example. An @ after a list of domain names indicates
that all items before should be treated as domain names, even if they look like IP addresses.
Although rare, some domain names can look like the start of an IP address, and if writing a
script to automatically process domain names it is recommended to always add an @ to the end
of the server names to avoid any misinterpretation.
Domain name restrictions may contain wildcard characters. The wildcard characters have the
following interpretations:
Restricting by IP Address
Examples:
or
--allowed-server 192.168.1.20-25
MAC addresses are composed of 6 bytes and should be specified using hex notation as follows.
Example:
Combining Restrictions
Examples:
Note: License file restrictions override file based restrictions. To avoid accidentally setting file
based restrictions when files are encoded to use a license file, the Encoder will generate an error
if both file restrictions and license files are used.
to specify the path of the license file that should be used to restrict the execution of the encoded
script. At runtime, the Loader will search for the license file relative to the location of the
encoded script, so a relative path should be used when specifying the license. The path will often
be just the name of the license file, e.g. license.txt
Typically an application will have a single top level directory. In this case the license file could
be saved into this top level directory, and the filename of the license could be used on the
command line instead of a more complicated relative path. If the Loader cannot locate the
license file relative to the PHP script that needs it, the Loader will search parent directories until
either the License file is found or there are no further parent directories.
--passphrase phrase
to specify a passphrase. The passphrase used when encoding files must match the passphrase
used to generate the corresponding license file in order for the Loader to successfully decrypt
the license file when the script is executed. See section 4.2.2 below for more details. If the
Loader cannot decrypt the license file it will prevent execution of the script.
A script can also use the Loader API to validate properties, server restrictions, and any expiry
date contained in the license. In order to implement a manual license check, and so prevent the
Loader from automatically validating the license, encode files with the option
--license-check script
Several Loader API functions useful when implementing a manual license check are described
in chapter 5. Please be careful to ensure code security when implementing a license validator in
PHP. In particular if a validator is contained in a file which will be included in each top-level
script, then it is necessary to use Include File Protection to ensure that a hostile user cannot
replace the validator with a different file.
For example:
--register-autoglobal MYAUTO
would encode access to $MYAUTO as if it were a global even if not explicitly made global with
the global keyword. It is not necessary to use this option for standard autoglobals, but this
feature may be useful if encoding files containing references to variables that are declared as
autoglobals by a PHP module that will be used in the target system but that is unknown to the
Encoder.
--message-if-no-loader <text>
<text> must be a valid PHP expression and is passed to the PHP die() function.
Example:
Note the use of single quotes around the message because a string is being passed to the die()
function.
--loader-path <path>
The current default setting performs selection of the Loader based on operating system type and
PHP version, and is:
'/ioncube/ioncube_loader_'.$__oc.'_'.substr(phpversion(),0,3).(($__oc=='win')?'.dll':'.so')
strtolower(substr(php_uname(),0,3))
Changing the Loader path may be useful if you wish to distribute Loaders with your application
but in a different directory, or if you wish to use run-time Loading but do not need to use the
dynamic selection of the Loader.
<file> should be the path to a file containing PHP code to place at the start of the encoded
files.
Examples:
--add-comments custom/comments.txt
<event> should be the event type to customise and <message> the text to associate with the
event.
Event types are:
Example:
Custom messages may also contain display formats. Each format is replaced with specific text
as follows.
--callback-file <relative-path>
to specify a callback file. The path should be relative to the top-level directory of the PHP
application. In particular, if the callback file is contained in the top-level directory, then specify
the filename rather than a full path.
The error code will be passed as the first argument, and an associative array of context-
dependent values will be passed as the second argument. The error code is an integer, and for
convenience the Loader defines constants for all event error codes. See section 3.11.3 for a list
of all constants.
The name of the file that caused the error is always passed as a parameter with key
current_file, and for server restriction errors parameters are passed with keys
domain_name and ip_address. The path to the expected license file is passed with key
license_file, and for errors related to include file restrictions, the file that included the
encoded file, or was included by the encoded file, is passed with key include_file.
--property “name[=value]”
name is the name of the property to be defined and value is the property value. Use a comma
to separate multiple properties.
Examples:
Define an array
--property “features={options=>{‘save’,’load’},licensed_to=>’Foo Tech Inc.’}”
This option may be used more than once to define multiple sets of required properties.
Examples:
--disable-auto-prepend-append
If this option is used and a server has either the append or prepend php.ini setting enabled,
the encoded scripts will not run. Some servers may have a legitimate reason for enabling these
settings, so this Encoder option is not enabled by default.
The projects feature uses a project file to store and provide command line options. Project file
options are merged with any additional options passed to the Encoder, and the project file may
be updated or recreated when required. As the project file is a plain text file, it can also be edited
if necessary.
--project-file <file>
Once a project file has been created, to encode with the given project options only this option
need be used.
Examples:
Repeat encoding based on project file p1 but with verbose mode enabled
ioncube_encoder –-project-file p1 --verbose
Example:
Repeat encoding based on project file p1 but permanently add the –-replace option
ioncube_encoder –-project-file p1 –-replace –update-project
3.15 Miscellany
The options:
--optimise more
--optimise max
Examples:
In these examples we have a simple project in a directory called test. The project contains the
file helloworld.php
The Encoder safety check prevented encoding because the target is within the source tree.
encoded/ helloworld.php
helloworld.php
/**
My code comment
*/
These comments are exposed by the PHP 5 reflection API, and are preserved by the PHP 5
Encoder by default. In order to omit these documentation comments from encoded files specify
the --no-doc-comments option.
Examples:
--min-loader-version 3.1
--min-loader-version 3.1.5
-V or --version
A benefit of using license files as an alternative to setting restrictions to the encoded files
themselves, is that projects will not need to be encoded every time in order to create a custom
build for each end-user. This is particularly helpful when producing software updates to existing
customers, as with license files, one encoded update will work for all installations. If restrictions
are instead associated with each encoded file, a product update would need to be encoded for
each existing customer, complicating the process of issuing a product update.
Server Restrictions
License file server restrictions can be supplied to the license generator in one of two ways. If the
details of the server to be licensed are known, such as IP address, then these can be used
explicitly. Often, however, details are unknown, and the goal is simply to restrict to a server
without knowing the actual parameters. To support this, a Loader API function can be used to
generate server data containing information about the target server, and after being received via
a method such as email or a web form, the data can be passed to the license generator to create a
license file for that server. This can be ideal for automating license generation based on
information supplied from the installed PHP scripts during a licensing procedure.
Custom Properties
In addition to restrictions, custom key value properties can be added to license files and read via
the Loader API. This feature might be used to customise product behaviour based on
information read from the license file.
File Format
A license file consists of a header in plain text, followed by an encrypted data block. Any
properties or restrictions in the license data can be optionally exposed, e.g. the expiry time, and
additional text can be added to the header if desired. Finally, license files are protected by
signatures to prevent removal or changing of the plain text.
Each encoded file contains a relative path to its associated license file. Typically this will be just
the filename, e.g. license.txt. When accessing a license protected encoded file, the Loader first
looks for the license file path relative to the same directory as the encoded script. If not found,
parent directories will be searched until the license file is found or until the directory root is
reached. This allows for easy installation and management of license files for both shared and
dedicated servers.
When encoding files that require a license it is necessary to specify a product passphrase. The
passphrase specified when encoding a product should agree with the passphrase specified when
generating the corresponding license, and should generally be unique for a particular product.
The output path is the path to which the new license file will be saved.
Once server data has been received, e.g. in an email or through a web form, and then written to a
temporary file, the --use-server-file option can be used as follows
--use-server-file path
where path refers to the location of the server data file. As the machine may contain multiple
network adaptors, it is also necessary to use either the option --select-server-adapter
or --select-adapters to select which details to license to. These options are described
in the next section.
To license to the server information associated with the web request that called the server data
API function, use the option
--select-server-adapter
Provided that the server data was requested from a page under the same domain as the
application to be licensed, errors from mistaken domain names or IP addresses should not arise.
When using this option, if no IP address or domain name was reported then the
make_license program will exit with code 2 to allow handling of this case.
can be used to select the adapters. Here adapter list is either a comma separated list of numbers
that refer to the position of the adapter in the server data file, or the * character. The first adapter
is identified as 1, and using *, all adapters will be licensed.
These options take arguments in the same way as the options in sections 3.6.1 and 3.6.2 for
setting expiry time on files, and examples are included in those sections.
--property “name[=value]”
to specify a property. Multiple properties can be specified in this way. See section 3.13.1 for
more details on the supported syntax. Properties can also be exposed in plain text in the license
header block by using the option
--expose-property name
should be used. By default, encoded files secured by such a license must have a property with a
matching key and value. If the property is not found then the Loader will exit before execution
of the script begins. See section 3.7.3 for details on how to customise this behaviour.
--header-line text
--decode-server-file path
where path is the path to a file containing server data. The domain name and server IP address
that were reported by the web server for the request calling the API function are output first,
followed by the name, IP address, and MAC address of each adapter installed on the server. To
be both human readable and easily parsed, each line has a field name and value separated by a :
character. If there was no domain name or IP address stored for the request, the field value will
be the token none.
If it is necessary to determine the contents of a license file the Loader API can be used. Encode a
script with the options
then use the Loader API to output the server restrictions, expiry date, and any properties
contained in the license.
5 LOADER API
The ionCube Loader contains an API providing various functions and constants that may be
useful to PHP scripts. Most functions return results dependent on whether or not the calling
script was encoded.
Key Value
FILE_EXPIRY Either the file expiry time, or the license expiry time if a license file is
present. The time is an integer in UNIX timestamp format: the number of
seconds elapsed since midnight (00:00:00), January 1, 1970.
ENCODING_TIME UNIX timestamp representing the time the file was encoded.
DEMO TRUE if the file was encoded with an evaluation Encoder, otherwise
FALSE.
This API function can be used to read files encrypted by the Encoder with the –-encrypt
command-line option. If the file is read successfully the contents are returned as a binary-safe
string. An integer is returned in the case of an error condition (use the PHP function is_int()
to distinguish these cases). See 5.5 below for a table of error codes.
Both plain text and encrypted files can be read by this function, allowing the function to be used
in situations where it is not known ahead of time whether a file will be encrypted. For example,
a template engine could be designed that would accept both encrypted and unencrypted template
files. If it is necessary to know whether a file was encrypted after it has been read the second
optional argument (passed by reference) can be examined.
If an encrypted file has been written with a custom passphrase (i.e. a nonempty passphrase
argument was passed to the ioncube_write_file() API function), the same passphrase
should be specified as the third argument.
Files encrypted by one Encoder can only be read by PHP scripts encoded by the same Encoder,
and encrypted files cannot be read by unencoded scripts.
The optional third argument can be set to FALSE to write a plain text file.
The optional fourth argument can be used to specify a custom passphrase which will replace the
default installation-specific passphrase. If a custom passphrase is used then files encrypted with
one installation can be read by a different installation’s encoded files, if the correct custom
passphrase is passed to the ioncube_read_file() API function.
Code Meaning
0 The file was successfully written.
1 The file could not be opened.
2 The file is corrupt.
3 An updated Loader should be installed to read the file.
4 An error occurred while reading the source file.
5 An error occurred while writing an encrypted file.
6 An error occurred while encrypting the file contents.
7 An encrypted file cannot be read by an unencoded PHP script.
8 The wrong passphrase was specified, or the wrong Encoder
installation was used to encrypt the file.
6 ERROR REPORTING
filename:line number:message
This offers possible integration with emacs/xemacs and direct access to the point of error in
source files. For example, given a directory of PHP files called myproject, running the
xemacs compile command and specifying the compiler as
ioncube_encoder -S myproject
will syntax check all PHP files and report errors in a buffer. With the default xemacs key
bindings, simply hitting ctrl-X will visit each file reported as containing an error and place
the cursor at the line containing the error.
7 TROUBLESHOOTING
7.1.1 On UNIX
On UNIX if you receive an error similar to
$ ioncube_encoder
this is because the directory where the Encoder is installed is not listed in the shell PATH
variable. It is recommended to use either a relative or absolute path to the Encoder rather than
adding the Encoder directory to the PATH as this may cause product license related problems on
some systems.
Examples:
7.1.2 On Windows
On Windows, you can edit the PATH environment variable for the logged in user by going to the
control panel, selecting the System item and clicking on the Environment tab.