Laravel is a popular PHP framework but how do you encode a Laravel project? This post covers using the ionCube Encoder to protect a simple Laravel 5 project by encoding the Laravel controller and model files, encrypting the Laravel Blade templates.
The project in this example is based from:
and is using Laravel 5.3.
The tutorial gives:
- A MySQL table called cars with the columns “make”, “model” and “produced_on” and with one record within the table.
- A CarController.php within app/Http/Controllers
- A Blade template show.blade.php within resources/views/cars
- An added route within routes/web.php
and produces the following output in a web browser.
The project code is shown below, and is also included with this guide.
Protecting Controllers and Models (Basic Encoding)
When it comes to encoding the project files, if the CarController.php controller is encoded and no obfuscation is used, the Laravel project will work as expected.
The general form for encoding is:
ioncube_encoder [options] [source directory] -o [target directory]
For this example, the project is installed in /var/www/html/project and is encoded to /var/www/html/project_encoded. The Encoder command to do this would be:
./ioncube_encoder.sh \ --copy "@/*/" \ --encode /var/www/html/project/app/Http/Controllers/ \ /var/www/html/project/ -o /var/www/html/project_encoded
As we want to encode just the controllers directory, the ––copy option is used to tell the Encoder to copy all PHP files rather than encode, followed by the ––encode option to encode the controllers directory. Models could be encoded using the same method.
Encrypting Blade Templates (optional)
Laravel Blade templates, despite having the file extension .blade.php, are not pure .php files, and can contain Laravel specific syntax that Laravel expects to interpret. The Blade engine reads the contents of templates, processes any Blade syntax, and produces a final PHP file. The Encoder will normally encode anything ending in .php, so a .blade.php file would be encoded in the same way as any other .php file. With the template encoded, when viewed in the web browser the application now produces:
This is because Laravel will not see any Blade syntax as the file is encoded, however there is a solution.
The ionCube Encoder can also encrypt non-PHP files, and unlike encoded files, encrypted files can be decrypted back to their original contents by using an API function in the ionCube Loader. Blade templates can be protected using the encryption feature whilst still allowing them to run, provided that a small change is made to the Laravel core to decrypt when required.
Within the Laravel core the BladeCompiler.php needs to be edited so that it can decrypt ionCube encrypted files. The compiler can be found at:
Within the function compile the code needs to be changed, as seen below.
The ioncube_read_file() function decrypts the Blade template so that it can be interpreted by Laravel before being passed to the browser. The ioncube_read_file() function can only be used within an encoded file so BladeCompiler.php will need to be encoded.
The ––encrypt option can be used to specify files or directories to encrypt, so encrypting the templates can be done using the command:
ioncube_encoder –encrypt ‘*.blade.php’ [source directory] -o [target directory]
Specific to this example it would be:
./ioncube_encoder.sh \ --copy "@/*/" \ --encode /var/www/html/project/app/Http/Controllers/ \ --encrypt '*.blade.php' \ --replace \ /var/www/html/project/ -o /var/www/html/project_encoded
Note that the ––replace option is also used, and tells the Encoder to replace the already existing output from the first encoding.
More information about the ioncube_read_file() function can be found in section 6.4.1 of the user guide.
To conclude, you can encode a Laravel project, however simply encoding the whole of your Laravel project won’t allow the project to run as desired. The Laravel core is opensource so doesn’t need to be encoded. To encode a project, follow the simple steps below.
Encoding the project
- Encode the controllers and models
- Encrypt the Blade templates
- Edit the BladeCompiler.php to decrypt ionCube encrypted files
- Encode BladeCompiler.php
To include obfuscation within your project: ionCube Encoding a Laravel Project: Function Obfuscation