Upgrading Magento 1.9.3.7 to PHP7.1: Replacing Deprecated mcrypt Functions
Magento 1.9.3.7 is a stable release that was designed for compatibility with PHP versions up through 5.4. However, as the PHP community focuses on maintaining security and stability, older versions are being phased out, and new features are being introduced in later versions. One of these changes affects the mcrypt extension, which has been deprecated since PHP7.0.
Understanding the Deprecation
The mcrypt module is no longer maintained or developed by its original authors, and it does not support modern cryptographic practices. In PHP7.1 and beyond, this functionality will be completely removed. If you encounter errors like “Deprecated functionality: Function mcrypt_module_open() is deprecated”, these are the messages indicating that the mcrypt extension is being phased out.
The General Procedure for Migration
The general procedure involves replacing all occurrences of mcrypt with its replacement, openssl. However, this requires a systematic approach to ensure compatibility across your entire Magento installation.
-
Identify Dependent Code: Run your unit tests and identify the specific files and functions that trigger the deprecated mcrypt error.
-
Create a Custom Encryption Class: In your local
Varien_Cryptdirectory underapp/code, create a new file namedOpenssl.php. This will contain your custom encryption logic using the OpenSSL extension. -
Replace mcrypt with openssl in Dependent Files: Update each dependent file to use your new
Opensslclass instead of the deprecatedmcrypt.
Creating Your Openssl.php Class
Here’s a simple example of how you might structure your Varien_Crypt/Openssl.php class:
<?php
class Varien_Crypt_Openssl extends Varien_Object
{
public function encrypt($data)
{
// Implementation here using openssl functions
// For example: return base64_encode(openssl_encrypt($data, 'AES-256-CBC', openssl_random_pseudo_bytes(16)));
}
public function decrypt($encryptedData)
{
// Implementation here using openssl functions
// For example: return openssl_decrypt(base64_decode($encryptedData), 'AES-256-CBC', $this->getKey());
}
}
Specifying the Encryption Method
In your Magento configuration, you’ll need to specify which encryption method to use. This isn’t done directly in code; instead, it’s configured through XML.
Open app/code/core/Mage/Crypt/etc/config.xml and look for the <crypt> node:
<config>
<global>
...
<crypt>
<key>
<type>mcrypt</type>
<value><![CDATA[456789abcdefghijk]]></value>
</key>
</crypt>
...
</global>
...
</config>
Replace mcrypt with openssl, and update the value as needed:
<type>openssl</type>
<value><![CDATA[1234567890abcdef]]></value>
Conclusion
The migration from mcrypt to openssl requires careful attention to your Magento installation’s specific dependencies. Always backup your data before making significant code changes.
For more detailed guides and best practices, I recommend exploring the official Magento 1 Developer Documentation and checking out the PHP: OpenSSL documentation.