Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mcrypt fatal - Removed on PHP 7.2 #159

Merged
merged 25 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a844294
Fix mcrypt fatal on data encryption
vaurdan Jul 5, 2021
e89712c
Fix error with unset `push_syndicate_settings`
vaurdan Jul 8, 2021
2997500
Add initial version of a test suite for the encryption functions.
vaurdan Jul 8, 2021
671576d
Make push_syndicate_decrypt return an array by default
vaurdan Jul 8, 2021
f3ad7a1
Use `is_string` if `assertIsString` is not available.
vaurdan Jul 8, 2021
4d5d0f4
Use `is_string` if `assertIsString` is not available.
vaurdan Jul 8, 2021
9927d42
Merge branch 'vip/deprecated-mcrypto' of github.com:Automattic/syndic…
vaurdan Jul 8, 2021
22ae798
Use `is_array` if `assertIsArray` is not available.
vaurdan Jul 8, 2021
ffa394f
Only test with `mcrypt` if the PHP version is < 7.1
vaurdan Jul 8, 2021
1f67318
Extend the PHP 7.1 validation to the encrypt and decrypt functions.
vaurdan Jul 8, 2021
5024eaa
Use yoast/wp-test-utils for compatibility with different phpunit vers…
vaurdan Jul 12, 2021
b1690a9
Refactor encryption to use Syndication_Encryption class
vaurdan Jul 19, 2021
02f0aa8
Fix tests failing on PHP 7.1
vaurdan Jul 19, 2021
8a4d2e7
Move imports and initialization to plugin root file
vaurdan Jul 20, 2021
6f09b04
Refactor tests to have individual tests for each encryptor
vaurdan Jul 20, 2021
1e6d373
Refactor tests to use a abstract test class for Encryptors
vaurdan Jul 20, 2021
8663284
Change Syndication_Encryptor to interface
vaurdan Jul 20, 2021
18fa88c
Remove leftover require_once
vaurdan Jul 20, 2021
fdb506a
Change Syndication_Encryption from a static class to an instantiable …
vaurdan Jul 21, 2021
d418b9c
Add extra validation for older PHP versions (<5.2.7), just in case.
vaurdan Jul 21, 2021
4e3873b
Remove encryptor strategy getters and setters
vaurdan Jul 23, 2021
16ec5b8
Address feedback on Encryption_Test
vaurdan Jul 23, 2021
c9f684e
Correction on the tests PHPDOC
vaurdan Jul 23, 2021
1f6b87c
Change EncryptionTest to test the `encrypt` and `decrypt` methods.
vaurdan Jul 28, 2021
9a3434d
Fix DocBlock
GaryJones Feb 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions includes/class-syndication-encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,36 @@ class Syndication_Encryption {
*
* @var Syndication_Encryptor
*/
private static $encryptor;
private $encryptor;

/**
* Returns the best possible Encryptor, given the current environment.
* Syndication_Encryption constructor.
*
* @return Syndication_Encryptor|false
* @param Syndication_Encryptor $encryptor Encryptor to be used.
*/
public static function get_encryptor() {
if ( isset( self::$encryptor ) && self::$encryptor instanceof Syndication_Encryptor ) {
return self::$encryptor;
}
public function __construct( Syndication_Encryptor $encryptor ) {
$this->encryptor = $encryptor;
}

return false;
/**
* Returns the best possible Encryptor, given the current environment.
*
* @return Syndication_Encryptor
*/
public function get_encryptor() {
return $this->encryptor;
}

vaurdan marked this conversation as resolved.
Show resolved Hide resolved
/**
* Set the Encryptor that will be used for the encryption and decryption operations.
*
* @param Syndication_Encryptor $encryptor Encryptor to be used in the encryption.
*
* @return Syndication_Encryptor|false Returns the encryptor
* @return Syndication_Encryptor Returns the encryptor
*/
public static function set_encryptor( $encryptor ) {
if ( $encryptor instanceof Syndication_Encryptor ) {
self::$encryptor = $encryptor;
return self::$encryptor;
}

return false;
public function set_encryptor( Syndication_Encryptor $encryptor ) {
$this->encryptor = $encryptor;
return $encryptor;
}
GaryJones marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand All @@ -48,8 +49,8 @@ public static function set_encryptor( $encryptor ) {
*
* @return false|string
*/
public static function encrypt( $data ) {
$encryptor = self::get_encryptor();
public function encrypt( $data ) {
$encryptor = $this->get_encryptor();
return $encryptor->encrypt( $data );
vaurdan marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -61,8 +62,8 @@ public static function encrypt( $data ) {
*
* @return false|array|object
*/
public static function decrypt( $data, $associative = true ) {
$encryptor = self::get_encryptor();
public function decrypt( $data, $associative = true ) {
$encryptor = $this->get_encryptor();
return $encryptor->decrypt( $data, $associative );
vaurdan marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
6 changes: 4 additions & 2 deletions includes/push-syndicate-encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* @return false|string
*/
function push_syndicate_encrypt( $data ) {
vaurdan marked this conversation as resolved.
Show resolved Hide resolved
return Syndication_Encryption::encrypt( $data );
global $push_syndication_encryption; // @todo: move from global to WP_Push_Syndication_Server attribute
return $push_syndication_encryption->encrypt( $data );
}

/**
Expand All @@ -20,5 +21,6 @@ function push_syndicate_encrypt( $data ) {
* @return array|false|object
*/
function push_syndicate_decrypt( $data, $associative = true ) {
return Syndication_Encryption::decrypt( $data, $associative );
global $push_syndication_encryption; // @todo: move from global to WP_Push_Syndication_Server attribute
return $push_syndication_encryption->decrypt( $data, $associative );
}
11 changes: 7 additions & 4 deletions push-syndication.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@
require __DIR__ . '/includes/class-syndication-site-auto-retry.php';
new Failed_Syndication_Auto_Retry();

// Load encryption classes
// Load encryption classes.
require_once dirname( __FILE__ ) . '/includes/class-syndication-encryption.php';
require_once dirname( __FILE__ ) . '/includes/interface-syndication-encryptor.php';
require_once dirname( __FILE__ ) . '/includes/class-syndication-encryptor-mcrypt.php';
require_once dirname( __FILE__ ) . '/includes/class-syndication-encryptor-openssl.php';

// On PHP 7.1 mcrypt is available, but will throw a deprecated error if its used. Therefore, checking for the
// PHP version, instead of checking for mcrypt is a better approach.
if ( version_compare( PHP_VERSION, '7.1', '<' ) ) {
Syndication_Encryption::set_encryptor( new Syndication_Encryptor_MCrypt() );
if ( PHP_VERSION_ID < 70100 ) {
$syndication_encryption = new Syndication_Encryption( new Syndication_Encryptor_MCrypt() );
} else {
Syndication_Encryption::set_encryptor( new Syndication_Encryptor_OpenSSL() );
$syndication_encryption = new Syndication_Encryption( new Syndication_Encryptor_OpenSSL() );
}

// @TODO: instead of saving this as a global, have it as an attribute of WP_Push_Syndication_Server
$GLOBALS['push_syndication_encryption'] = $syndication_encryption;
17 changes: 11 additions & 6 deletions tests/test-encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class EncryptionTest extends WPIntegrationTestCase {
private $simple_string;
private $complex_array;

private $syndication_encryption;

/**
* Runs before the test, set-up.
Expand All @@ -30,6 +31,9 @@ public function setUp() {
1 => 20.04,
3 => true,
);

global $push_syndication_encryption;
$this->syndication_encryption = $push_syndication_encryption;
vaurdan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -38,7 +42,7 @@ public function setUp() {
* @requires PHP < 7.1
*/
public function test_get_encryptor_before_php_71() {
$encryptor = \Syndication_Encryption::get_encryptor();
$encryptor = $this->syndication_encryption->get_encryptor();

// If PHP < 7.1, it should be using the mcrypt encryptor.
self::assertInstanceOf( \Syndication_Encryptor_MCrypt::class, $encryptor );
Expand All @@ -50,7 +54,7 @@ public function test_get_encryptor_before_php_71() {
* @requires PHP >= 7.1
*/
public function test_get_encryptor_after_php_71() {
$encryptor = \Syndication_Encryption::get_encryptor();
$encryptor = $this->syndication_encryption->get_encryptor();

// Test if the Encryptor being used is the OpenSSL.
self::assertInstanceOf( \Syndication_Encryptor_OpenSSL::class, $encryptor );
Expand All @@ -60,11 +64,12 @@ public function test_get_encryptor_after_php_71() {
* Test if setting the encryptor works as expected
*/
public function test_set_encryptor() {
$encryptor = \Syndication_Encryption::set_encryptor( new \Syndication_Encryptor_OpenSSL() );
self::assertInstanceOf( \Syndication_Encryptor_OpenSSL::class, $encryptor, 'assert if the encryptor is set' );
$new_encryptor = $this->syndication_encryption->set_encryptor( new \Syndication_Encryptor_OpenSSL() );
self::assertInstanceOf( \Syndication_Encryptor_OpenSSL::class, $new_encryptor, 'assert if the encryptor is set' );

$encryptor = \Syndication_Encryption::set_encryptor( new \stdClass() );
self::assertFalse( $encryptor, 'assert if invalid encryptor returns false' );
$encryptor = $this->syndication_encryption->get_encryptor();
self::assertInstanceOf( \Syndication_Encryptor_OpenSSL::class, $encryptor, 'assert if the encryptor is set' );
self::assertEquals( $new_encryptor, $encryptor, 'assert if the set encryptor is the same as the one retrieved' );
}

/**
Expand Down