Encrypting smarty tpl module file in WHMCS

孤街醉人 提交于 2019-12-12 01:35:51

问题


I am trying to encrypt a tpl file with ionCube in my module in WHMCS without modifying WHMCS smarty.class file. Anyone have any idea how can I do that?

For further information see http://www.ioncube.com/sa_encoder.php?page=smarty_patch


回答1:


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.




回答2:


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. 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. 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.



来源:https://stackoverflow.com/questions/24171753/encrypting-smarty-tpl-module-file-in-whmcs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!