From 8b21ecb348511b1b7a3da152b20d0cc1bd88f455 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 14:04:05 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- src/Control/CwpBasicAuthMiddleware.php | 6 +++--- src/Control/InitialisationMiddleware.php | 4 ++-- src/Extension/CWPVersionExtension.php | 2 +- src/Extension/LoginAttemptNotifications.php | 2 +- src/Extension/RichLinksExtension.php | 12 ++++++------ src/PasswordEncryptor/PBKDF2.php | 7 ++++++- tests/PasswordEncryptor/PBKDF2Test.php | 2 +- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Control/CwpBasicAuthMiddleware.php b/src/Control/CwpBasicAuthMiddleware.php index 875101d..8cb1395 100644 --- a/src/Control/CwpBasicAuthMiddleware.php +++ b/src/Control/CwpBasicAuthMiddleware.php @@ -43,12 +43,12 @@ public function setWhitelistedIps($whitelistedIps) if (!$ipList) { continue; } - $ips = array_map('trim', explode(',', $ipList)); + $ips = array_map('trim', explode(',', $ipList ?? '')); $whitelistedIps = array_merge($whitelistedIps, $ips); } // Return unique values with keys reset - $this->whitelistedIps = array_values(array_unique($whitelistedIps)); + $this->whitelistedIps = array_values(array_unique($whitelistedIps ?? [])); return $this; } @@ -80,7 +80,7 @@ protected function ipMatchesWhitelist() } $userIp = $_SERVER['REMOTE_ADDR']; - if (in_array($userIp, $whitelist)) { + if (in_array($userIp, $whitelist ?? [])) { return true; } diff --git a/src/Control/InitialisationMiddleware.php b/src/Control/InitialisationMiddleware.php index 1ad597a..309d40c 100644 --- a/src/Control/InitialisationMiddleware.php +++ b/src/Control/InitialisationMiddleware.php @@ -142,13 +142,13 @@ protected function configureProxyDomainExclusions() // Merge with exsiting if needed. if (Environment::getEnv('NO_PROXY')) { - $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); + $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY') ?? ''), $noProxy); } /* * Set the environment varial for NO_PROXY the same way the * proxy variables are set above */ - putenv('NO_PROXY=' . implode(',', array_unique($noProxy))); + putenv('NO_PROXY=' . implode(',', array_unique($noProxy ?? []))); } } diff --git a/src/Extension/CWPVersionExtension.php b/src/Extension/CWPVersionExtension.php index 2825319..cebbfba 100644 --- a/src/Extension/CWPVersionExtension.php +++ b/src/Extension/CWPVersionExtension.php @@ -27,6 +27,6 @@ public function getCWPVersionNumber() // Example: "2.2.x-dev" $cwpCore = $modules['cwp/cwp-core']; - return (string) substr($cwpCore, 0, strpos($cwpCore, '.', 2)); + return (string) substr($cwpCore ?? '', 0, strpos($cwpCore ?? '', '.', 2)); } } diff --git a/src/Extension/LoginAttemptNotifications.php b/src/Extension/LoginAttemptNotifications.php index 0b8bc96..74c4937 100644 --- a/src/Extension/LoginAttemptNotifications.php +++ b/src/Extension/LoginAttemptNotifications.php @@ -51,7 +51,7 @@ public function init() $lastVisitedObj->setValue($sessionLastVisited); $elapsed = $lastVisitedObj->TimeDiff(); $failures = $meantimeLoginAttempts->filter(['Status' => 'Failure'])->count(); - $IPs = array_unique($meantimeLoginAttempts->column('IP')); + $IPs = array_unique($meantimeLoginAttempts->column('IP') ?? []); if ($attempts == 1) { $statusString = $failures ? "a failed" : "a successful"; diff --git a/src/Extension/RichLinksExtension.php b/src/Extension/RichLinksExtension.php index 92f9887..1d2b56f 100644 --- a/src/Extension/RichLinksExtension.php +++ b/src/Extension/RichLinksExtension.php @@ -36,18 +36,18 @@ public function RichLinks() $content = $this->owner->value; // Find all file links for processing. - preg_match_all('/.*<\/a>/U', $content, $matches); + preg_match_all('/.*<\/a>/U', $content ?? '', $matches); // Attach the file type and size to each of the links. - for ($i = 0; $i < count($matches[0]); $i++) { + for ($i = 0; $i < count($matches[0] ?? []); $i++) { $file = DataObject::get_by_id(File::class, $matches[1][$i]); if ($file) { $size = $file->getSize(); - $ext = strtoupper($file->getExtension()); + $ext = strtoupper($file->getExtension() ?? ''); // Replace the closing tag with the size span (and reattach the closing tag). - $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4) + $newLink = substr($matches[0][$i] ?? '', 0, strlen($matches[0][$i] ?? '') - 4) . " [$ext, $size]"; - $content = str_replace($matches[0][$i], $newLink, $content); + $content = str_replace($matches[0][$i] ?? '', $newLink ?? '', $content ?? ''); } } @@ -57,7 +57,7 @@ public function RichLinks() '$1class="external" rel="external" $2(%s)$3', _t(__CLASS__ . '.ExternalLink', 'external link') ); - $content = preg_replace($pattern, $replacement, $content, -1); + $content = preg_replace($pattern ?? '', $replacement ?? '', $content ?? '', -1); return $content; } diff --git a/src/PasswordEncryptor/PBKDF2.php b/src/PasswordEncryptor/PBKDF2.php index b831628..db1fcfe 100644 --- a/src/PasswordEncryptor/PBKDF2.php +++ b/src/PasswordEncryptor/PBKDF2.php @@ -44,6 +44,11 @@ public function getIterations(): int public function encrypt($password, $salt = null, $member = null) { - return hash_pbkdf2($this->getAlgorithm(), (string) $password, (string) $salt, $this->getIterations()); + return hash_pbkdf2( + $this->getAlgorithm() ?? '', + (string) $password, + (string) $salt, + $this->getIterations() ?? 0 + ); } } diff --git a/tests/PasswordEncryptor/PBKDF2Test.php b/tests/PasswordEncryptor/PBKDF2Test.php index f491e42..e69fc24 100644 --- a/tests/PasswordEncryptor/PBKDF2Test.php +++ b/tests/PasswordEncryptor/PBKDF2Test.php @@ -21,7 +21,7 @@ public function testEncrypt() $result = $encryptor->encrypt('opensesame', $salt); $this->assertSame( '6bafcacb90', - substr($result, 0, 10), + substr($result ?? '', 0, 10), 'Hashed password with predictable salt did not match fixtured expectation' ); }