【问题标题】:Encrypting smarty tpl module file in WHMCS在WHMCS中加密smarty tpl模块文件
【发布时间】: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


    【解决方案1】:

    当然你需要有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 类。我没有测试过这段代码,但它应该可以工作。根据您的设置,您可能需要删除所有已编译的模板文件才能重新生成它们。

    【讨论】:

    • 但是在 smarty.class 文件中你必须修改 _read_file(),我不想这样做,查看这个链接以获得更多信息:ioncube.com/sa_encoder.php?page=smarty_patch
    • Marcin 的解决方案很简洁。与处理透明的编码文件不同,您必须修改读取模板的例程,否则无法进行解密。
    【解决方案2】:

    您可以使用非 PHP 文件加密功能对模板文件进行加密,但需要修改 smarty 引擎才能处理解密。如果不这样做,您只会看到显示的加密内容。为此使用 ioncube_read_file() API 函数,它将无缝处理加密和非加密文件。请注意,调用该函数的文件必须进行编码,因为没有受保护的文件调用解密例程是没有意义的。

    【讨论】:

    • 但在这种情况下,他将修改 Smarty 类,他不想像问题那样这样做
    • 智能引擎需要进行修改,这就是答案所说的,并且由于问题表明它只是一个愿望而不是要求,答案是有效的。最好他现在意识到这一点,而不是因为头撞在砖墙上而住院。或者,他可以选择不同的模板引擎,或者将 smarty 的第二个副本合并到他的模块中,以便他修改的是 smarty 副本,而不是 WHMCS smarty。
    • 实际上我开始痛心,这是一个漫长的一周,但是如果我想创建我的 smarty 本地副本,我该如何扩展 smarty.class 并覆盖模块中的 _read_file 函数?跨度>
    • 我在答案中添加了 Smarty 2 和 Smarty 3 的示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多