【发布时间】:2014-06-11 20:12:47
【问题描述】:
我正在尝试在 WHMCS 的模块中使用 ionCube 加密 tpl 文件,而不修改 WHMCS smarty.class 文件。有人知道我该怎么做吗?
欲了解更多信息,请参阅http://www.ioncube.com/sa_encoder.php?page=smarty_patch
【问题讨论】:
标签: php encryption smarty whmcs ioncube
我正在尝试在 WHMCS 的模块中使用 ionCube 加密 tpl 文件,而不修改 WHMCS smarty.class 文件。有人知道我该怎么做吗?
欲了解更多信息,请参阅http://www.ioncube.com/sa_encoder.php?page=smarty_patch
【问题讨论】:
标签: php encryption smarty whmcs ioncube
当然你需要有ionCube Php Encoder,你需要创建项目,添加文件,然后在Project settings -> Source的GUI中你应该右键点击你的TPL文件并选择“加密非PHP文件”。如果不应用 ionCube 文档中的 Smarty 补丁,您将无法做到这一点。
您还可以扩展 Smarty 类。
对于 Smarty 2,代码很简单:
<?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;
}
}
并且您应该创建此类的对象以使用修改后的代码。
对于 Smarty 3,它有点复杂。
您需要创建MyFileResource 类,如下所示:
<?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}'");
}
}
在您创建 Smarty 对象的地方添加一些代码。
假设您以这种方式创建 Smarty 对象:
require '../libs/Smarty.class.php';
$smarty = new Smarty;
你应该把它改成:
require '../libs/Smarty.class.php';
require('MyFileResource.php');
$smarty = new Smarty;
$smarty->registerResource('file', new MyFileResource());
这样每次你从文件中读取模板时,你都使用你的 MyFileResource 类。我没有测试过这段代码,但它应该可以工作。根据您的设置,您可能需要删除所有已编译的模板文件才能重新生成它们。
【讨论】:
您可以使用非 PHP 文件加密功能对模板文件进行加密,但需要修改 smarty 引擎才能处理解密。如果不这样做,您只会看到显示的加密内容。为此使用 ioncube_read_file() API 函数,它将无缝处理加密和非加密文件。请注意,调用该函数的文件必须进行编码,因为没有受保护的文件调用解密例程是没有意义的。
【讨论】: