Skip to content

Commit

Permalink
Merge pull request #106 from creative-commoners/pulls/2/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
GuySartorelli authored Apr 26, 2022
2 parents f3d9ef3 + 8b21ecb commit 8487b6b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Control/CwpBasicAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -80,7 +80,7 @@ protected function ipMatchesWhitelist()
}

$userIp = $_SERVER['REMOTE_ADDR'];
if (in_array($userIp, $whitelist)) {
if (in_array($userIp, $whitelist ?? [])) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Control/InitialisationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [])));
}
}
2 changes: 1 addition & 1 deletion src/Extension/CWPVersionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion src/Extension/LoginAttemptNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
12 changes: 6 additions & 6 deletions src/Extension/RichLinksExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public function RichLinks()
$content = $this->owner->value;

// Find all file links for processing.
preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches);
preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/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 </a> 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)
. "<span class='fileExt'> [$ext, $size]</span></a>";
$content = str_replace($matches[0][$i], $newLink, $content);
$content = str_replace($matches[0][$i] ?? '', $newLink ?? '', $content ?? '');
}
}

Expand All @@ -57,7 +57,7 @@ public function RichLinks()
'$1class="external" rel="external" $2<span class="nonvisual-indicator">(%s)</span>$3',
_t(__CLASS__ . '.ExternalLink', 'external link')
);
$content = preg_replace($pattern, $replacement, $content, -1);
$content = preg_replace($pattern ?? '', $replacement ?? '', $content ?? '', -1);

return $content;
}
Expand Down
7 changes: 6 additions & 1 deletion src/PasswordEncryptor/PBKDF2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
2 changes: 1 addition & 1 deletion tests/PasswordEncryptor/PBKDF2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
Expand Down

0 comments on commit 8487b6b

Please sign in to comment.