Ioncube and Smarty

The latest version of ionCube PHP Encoder has the ability to encrypt any non-PHP files as well as encoding PHP files. The ionCube PHP Loader extends PHP with functions to decrypt these files. This means that it is now possible to encrypt and distribute secure templates with your projects.

On this article we provide a patch to the popular Smarty template engine enabling Smarty to read encrypted template files. These encrypted template files can be created automatically by the ionCube Encoder at the same time as the PHP scripts are encoded.

The patch to Smarty is extremely simple - we have replaced a function that reads the contents of the template file with a call to the Loader function that reads encrypted files. Since this Loader function is able to read both encrypted and unencrypted files, the modified version of Smarty will work seamlessly with both encrypted and regular unencrypted templates. The same approach can undoubtedly be used for other template engines, reading/writing XML documents and configuration files, etc. Please note that the script calling ioncube_read_file(), e.g. the template engine, must be encoded.

To apply the patch, simply comment out the method _read_file in the file Smarty.class.php in the Smarty source, and insert the replacement method. The method can be viewed by clicking the link below.

// Replacement function for _read_file() in Smarty.class.php to add support
// for reading both ionCube encrypted templates and plain text templates.
// Smarty.class.php must be encoded by the creator of the templates for
// ioncube_read_file() to decode encrypted template files

function _read_file($filename)
$res = false;

if (file_exists($filename) && is_readable($filename)) {
if (function_exists('ioncube_read_file')) {
$res = ioncube_read_file($filename);
if (is_int($res)) $res = false;
else if ( ($fd = @fopen($filename, 'rb')) ) {
$contents = '';
while (!feof($fd)) {
$contents .= fread($fd, 8192);
$res = $contents;
$res = false;
return $res;

Posted on: 12/12/2009

If you want to leave a comment please Login or Register