Of course you need to have ionCube Php Encoder, you need to create project, add files and then in GUI in `Project settings -> Source` you should right click on your TPL file and select "Encrypt non-PHP file". There is no way you can do it without applying Smarty patch as in ionCube documentation.
You can also extend Smarty class.
For **Smarty 2** the code will be simple:
<?php
class MyTemplate extends Smarty {
// 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)) {
if (function_exists('ioncube_read_file')) {
$res = ioncube_read_file($filename);
if (is_int($res)) $res = false;
}
else if ( ($fd = @fopen($filename, 'rb')) ) {
$res = ($size = filesize($filename)) ? fread($fd, $size) : '';
fclose($fd);
}
}
return $res;
}
}
and you should create object of this class to use modified code.
For **Smarty 3** it's a bit more complex.
You need to create `MyFileResource` class as below:
<?php
//MyFileResource.php
class MyFileResource extends Smarty_Internal_Resource_File {
/**
* Load template's source from file into current template object
*
* @param Smarty_Template_Source $source source object
* @return string template source
* @throws SmartyException if source cannot be loaded
*/
public function getContent(Smarty_Template_Source $source)
{
if ($source->timestamp) {
if (file_exists($source->filepath) && function_exists('ioncube_read_file')) {
$res = ioncube_read_file($source->filepath);
if (is_int($res)) {
$res = false;
}
return $res;
}
else {
return file_get_contents($source->filepath);
}
}
if ($source instanceof Smarty_Config_Source) {
throw new SmartyException("Unable to read config {$source->type} '{$source->name}'");
}
throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
}
}
And in place where you created Smarty object add some code.
Assume you created Smarty object this way:
require '../libs/Smarty.class.php';
$smarty = new Smarty;
You should change it into:
require '../libs/Smarty.class.php';
require('MyFileResource.php');
$smarty = new Smarty;
$smarty->registerResource('file', new MyFileResource());
This way each time you read templates from files you use your MyFileResource class. I haven't tested this code but it should work. Depending on your settings it's possible you will need to remove all your compiled template files to regenerate them again.