Laravel with ioncube and encoding

て烟熏妆下的殇ゞ 提交于 2019-12-07 14:16:20

问题


I work on Laravel framework for a short time, after finishing the website, I willing to encode it using Ioncube - but I face the problem where Ioncube can't translate the blade template engine code to PHP code before encoding and output the code as HTML normal text.


回答1:


Laravel's blade template files are not real PHP code, therefore the ionCube Encoder cannot encode them properly; however, the Encoder also offers file encryption which might help in your case, but it does require a bit of a setup:

  1. In Laravel, modify the module that reads in the Blade template files, replacing file_get_contents(<blade template files>) with ioncube_read_file(<blade template_files>).

    In my Laravel 4 installation, it seems like the file responsible for loading and compiling Blade Templates can be found in vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php, line 62: $contents = $this->compileString($this->files->get($path));.

    Here, $this->files->get($path) is 'just' a file_get_contents($path) with some error handling - you may find this function in bootstrap/compiled.php. However, you should be able to replace the line in BladeCompiler.php with:

    $contents = $this->compileString(ioncube_read_file($path));
    

    or (if you used a passphrase):

    $passphrase = "<my passphrase>";
    $contents = $this->compileString(ioncube_read_file($path, $is_encrypted, $passphrase));
    

    Please do note that you might want to make sure that the passphrase is secure within the file. You can find more information on these functions in the User Guide on page 54.

  2. To get ioncube_read_file working, you will need to encode the modified module. Furthermore, encode the rest of your application with the exception of your Blade template files.

  3. Encrypt (not encode!) your Blade template files, typically ending in .blade.php, and - if you use a passphrase - make sure it matches the one used for ioncube_read_file.

Please also keep in mind that this will generate standard PHP files from your encrypted ones, since the file is compiled and written back to the cache as plain text. If you want to change that it might be worth looking into the User Guide, p. 54 and override / extend the appropriate methods bootstrap/compiled.php such as get and put to detect if a file is encrypted, and to write an encrypted file if needed.

I should also mention, as this question is regularly asked on the ionCube HelpDesk: Despite all the encoding and encrypting, the HTML and JavaScript code will be displayed raw for all clients to see. I only mention this because it is a lot of effort to protect your Template files, which do consist in most cases mostly of HTML code. It does make any modification really hard (such as logo removal), but such things may also be achieved with some custom CSS.




回答2:


After coming across this issue I believe this answer will be the simple and easy way to solve it. With this methode you won't have to edit framework files every after update.

  1. Create your own new Service Provider to replace ViewServiceProvider. Name it IonCubeCompilerServiceProvider
  2. Create new Class to extend BladeCompiler. Name it IonCubeBladeCompiler
  3. Finally, swap the Illuminate\View\ViewServiceProvider::class out of the config/app.php providers array, and replace it with your own ViewServiceProvider.

    namespace App\Providers;
    
    use App\Libraries\IonCubeBladeCompiler;
    use Illuminate\View\Engines\CompilerEngine;
    use Illuminate\View\ViewServiceProvider;
    
    class IonCubeCompilerServiceProvider extends ViewServiceProvider
    {
        public function registerBladeEngine($resolver)
        {
            $this->app->singleton('blade.compiler', function () {
                return new IonCubeBladeCompiler(
                    $this->app['files'], $this->app['config']['view.compiled']
                );
            });
    
            $resolver->register('blade', function () {
                return new CompilerEngine($this->app['blade.compiler']);
            });
        }
    }
    

Second file.

    namespace App\Libraries;


    use Illuminate\View\Compilers\BladeCompiler;

    class IonCubeBladeCompiler extends BladeCompiler
    {
        public function compile($path = null)
        {
            if ($path) {
                $this->setPath($path);
            }

            if (! is_null($this->cachePath)) {
                if(function_exists('ioncube_read_file')){
                    //This is what I'm adding.
                    $contents = $this->compileString(ioncube_read_file($this->getPath()));
                }else{
                    //This is the original line.
                    $contents = $this->compileString($this->files->get($this->getPath()));
                }

                $this->files->put($this->getCompiledPath($this->getPath()), $contents);
            }
        }

    }

For more details you can check the following two links https://laracasts.com/discuss/channels/laravel/extend-the-compile-function-on-bladecompilerphp and http://blog.ioncube.com/2016/12/19/ioncube-encoding-laravel-project-controllers-models-templates/



来源:https://stackoverflow.com/questions/29070592/laravel-with-ioncube-and-encoding

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