简体   繁体   English

在WHMCS中加密smarty tpl模块文件

[英]Encrypting smarty tpl module file in WHMCS

I am trying to encrypt a tpl file with ionCube in my module in WHMCS without modifying WHMCS smarty.class file. 我试图在WHMCS的模块中使用ionCube加密tpl文件,而不修改WHMCS smarty.class文件。 Anyone have any idea how can I do that? 有人知道我该怎么做吗?

For further information see http://www.ioncube.com/sa_encoder.php?page=smarty_patch 有关更多信息,请参见http://www.ioncube.com/sa_encoder.php?page=smarty_patch

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". 当然,您需要有ionCube Php编码器,需要创建项目,添加文件,然后在GUI的“ Project settings -> Source ,右键单击TPL文件并选择“加密非PHP文件”。 There is no way you can do it without applying Smarty patch as in ionCube documentation. 如ionCube文档中所述,没有应用Smarty修补程序就无法做到。

You can also extend Smarty class. 您还可以扩展Smarty类。

For Smarty 2 the code will be simple: 对于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;
    }    
}

and you should create object of this class to use modified code. 并且您应该创建此类的对象以使用修改后的代码。

For Smarty 3 it's a bit more complex. 对于Smarty 3,它要复杂一些。

You need to create MyFileResource class as below: 您需要创建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}'");
    }

}

And in place where you created Smarty object add some code. 并在创建Smarty对象的位置添加一些代码。

Assume you created Smarty object this way: 假设您通过以下方式创建了Smarty对象:

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. 这样,每次您从文件中读取模板时,都使用MyFileResource类。 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. 根据您的设置,可能需要删除所有已编译的模板文件以重新生成它们。

You can encrypt the template files using the non-PHP file encryption feature, but the smarty engine needs to be modified in order to handle the decryption. 您可以使用非PHP文件加密功能对模板文件进行加密,但是需要修改smarty引擎才能处理解密。 Without doing so you will just see encrypted contents displayed. 不这样做,您只会看到显示的加密内容。 Use the ioncube_read_file() API function for this, which will seamlessly handle encrypted and non-encrypted files. 为此,请使用ioncube_read_file()API函数,该函数将无缝处理加密和未加密的文件。 Note that the file calling the function must be encoded as there would be no point in having an unprotected file calling a decryption routine. 请注意,必须对调用函数的文件进行编码,因为让未受保护的文件调用解密例程毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM