Hackernoon logoHow to Retrofit ExpressionEngine 1 and 2 for PHP 7 by@robhoward

How to Retrofit ExpressionEngine 1 and 2 for PHP 7

Rob Howard Hacker Noon profile picture

@robhowardRob Howard

CEO of Howard Development & Consulting, the web development firm creative agencies trust when every pixel matters.

More than a decade ago, I discovered ExpressionEngine — an extraordinary PHP- and MySQL-based content management system.

For years, it was my go-to CMS. And although it had its issues in later versions and has now been (in my opinion) fully eclipsed by WordPress, it was always solid software, and I continue to use CodeIgniter, the framework on which it was based, to this day. 

In the past few months, I’ve fielded a handful of requests from legacy clients who are still on EE 1.x and 2.x — and whose sites have broken due to their hosts forcing an upgrade to PHP 7. Nowadays, we set up the vast majority of our clients on our WordPress Assurance plans for continuous upgrades, but there are always some clients who choose not to make that investment and thus end up with outdated sites like these. That said, I think it’s a testament to EE’s security and stability that many of these sites have made it 10 years without a meaningful software update!

So EE 1.x and 2.x are really solid, but they definitely do not work on PHP 7. Because I went through this process a couple times, I’ve now documented it to help you solve the same problem on your legacy sites. (And of course, you should build your client a new site — but this will at least hold them over for a while.)

Adding back functions that no longer exist in PHP 7

Legacy ExpressionEngine relies on a bunch of functions that no longer exist in PHP 7 and above – including critical ones like mysql_ functions (which have simply been renamed in PHP 7), as well as annoying ones like the magic quotes function.

The quickest way to fix this problem is simply to paste these functions into your index.php file in your root directory and your system folder, as well as admin.php on EE2. Like everything in this guide, it’s kind of a “hack,” but it gets your site back online.

First, add this function near the top of your index.php files (plus admin.php in EE2) to get the Magic Quotes function to stop throwing an error:

if (!function_exists('set_magic_quotes_runtime')) {
    function set_magic_quotes_runtime($new_setting) {
        return true;
    }
}

Right after that, paste the contents of this file into your index.php and admin.php files, just below the Magic Quotes function above. This will cover all the major MySQL functions that have been renamed in PHP 7, so your database connections will magically work again. (Thanks to rubo77 for this fix.)

Disabling Error Reporting

Despite fixing all the deprecated functions, you’ll still find that your front-end site and/or your administrative backend throw a bunch of lower-level errors. While these aren’t a problem in and of themselves, they will mess up rendering of styles and prevent some pages from redirecting properly – not to mention just freaking out your client when they see a laundry list of bizarre error messages. To fix this, add the following near the top of your index.php files in your root directory and system directory:

error_reporting(0);
ini_set('display_errors', 0);

Note that this will prevent you from easily debugging, so if you get a blank page, that means an error occurred but it isn’t being displayed due to this setting. You can temporarily comment these lines out to get that resolved.

In ExpressionEngine 2 only, you’ll need to add a few more lines of code to universally disable error reporting.

In your EE2 admin.php file in your root directory, make sure the $debug variable is set to 0 around Line 69.

$debug = 0;

In the same file, you may also need to use some harsher settings on Line 170, by changing this:

error_reporting(0);

To this:

error_reporting(E_NONE);	
ini_set('display_errors', 0);

In short, you can’t get too intense with adding these functions to disable errors.

Lastly, head over to your EE2 file at /system/expressionengine/libraries/Core.php. Change lines 561-562, within the _enable_debugging() function, to the following:

error_reporting(E_NONE);
@ini_set('display_errors', 0);

That should silence all your errors! If you find more, you can liberally paste the error-hiding functions above into any PHP file.

Teaching EE1 how to tell time all over again

The most bizarre issue we’ve discovered is that, once moved to PHP 7, ExpressionEngine 1 is no longer able to generate a proper timestamp, which means all your weblog entries get a date of “0” and your weblog queries break because of issues with dates being generated as blank strings.

The quickest fix for this is to modify the /system/core/core.localize.php file. Around line 57, change this code:

$this->now = $this->set_gmt($this->server_now);

To say this instead:

$this->now = time();

When you do this, you’ll get a timestamp in UTC time instead of a blank string or a zero, which should get most things working. However, depending on your time zone settings in ExpressionEngine’s control panel, you may find that dates on new weblog entries are several hours too far in the future or the past. If you are having issues with entries appearing in the future (or as expired) inappropriately, you may want to add the show_future_entries=”yes” parameter to your weblog:entries tags. This should get it working again, even though your control panel will still have some odd time-zone disparities. Here’s an example from one of our sites:

{exp:weblog:entries weblog="pages" limit="1" show_future_entries="yes"}

Note that the other parameters will be unique to your template, but show_future_entries=”yes” is the key to fixing the time zone issues. Also, watch out for the “curly quotes” if you’re pasting directly out of this blog post.

Rob Howard is the founder and CEO of Howard Development & Consulting, the web development firm that creative agencies trust when every pixel matters. His startups have been featured in Entertainment Weekly and Newsweek, and his clients have included The World Bank, Harvard and MIT.

Also published at: https://howarddc.com/expressionengine-1-and-2-for-php-7/

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.