Skip to content

Commit b07423d

Browse files
authored
Merge pull request #103 from mambax7/feature/improvements
2 parents 92a66fb + b46b280 commit b07423d

File tree

5 files changed

+73
-24
lines changed

5 files changed

+73
-24
lines changed

src/FilterInput.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @copyright 2005 Daniel Morris
3030
* @copyright 2005 - 2013 Open Source Matters, Inc. All rights reserved.
3131
* @copyright 2011-2023 XOOPS Project (https://xoops.org)
32-
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
32+
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
3333
* @link https://xoops.org
3434
*/
3535
class FilterInput
@@ -146,7 +146,7 @@ public static function getInstance(
146146
*
147147
* @param mixed $source - input string/array-of-string to be 'cleaned'
148148
*
149-
* @return string $source - 'cleaned' version of input parameter
149+
* @return string|array $source - 'cleaned' version of input parameter
150150
*/
151151
public function process($source)
152152
{
@@ -159,7 +159,8 @@ public function process($source)
159159
}
160160
}
161161
return $source;
162-
} elseif (is_string($source)) {
162+
}
163+
if (is_string($source)) {
163164
// clean this string
164165
return $this->remove($this->decode($source));
165166
} else {
@@ -421,7 +422,7 @@ protected function filterTags($source)
421422
// appears in array specified by user
422423
$tagFound = in_array(strtolower($tagName), $this->tagsArray);
423424
// remove this tag on condition
424-
if ((!$tagFound && $this->tagsMethod) || ($tagFound && !$this->tagsMethod)) {
425+
if ($tagFound !== (bool) $this->tagsMethod) {
425426
// reconstruct tag with allowed attributes
426427
if (!$isCloseTag) {
427428
$attrSet = $this->filterAttr($attrSet);
@@ -512,7 +513,7 @@ protected function filterAttr($attrSet)
512513
// if matches user defined array
513514
$attrFound = in_array(strtolower($attrSubSet[0]), $this->attrArray);
514515
// keep this attr on condition
515-
if ((!$attrFound && $this->attrMethod) || ($attrFound && !$this->attrMethod)) {
516+
if ($attrFound !== (bool) $this->attrMethod) {
516517
if ($attrSubSet[1]) {
517518
// attr has value
518519
$newSet[] = $attrSubSet[0] . '="' . $attrSubSet[1] . '"';

src/Jwt/JsonWebToken.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public function decode($jwtString, $assertClaims = array())
9090
foreach ($assertClaims as $claim => $assert) {
9191
if (!property_exists($values, $claim)) {
9292
return false;
93-
} elseif ($values->$claim != $assert) {
93+
}
94+
95+
if ($values->$claim != $assert) {
9496
return false;
9597
}
9698
}

src/ProxyCheck.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @package Xmf
1919
* @author Richard Griffith <richard@geekwright.com>
2020
* @copyright 2019-2020 XOOPS Project (https://xoops.org)
21-
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
21+
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
2222
*/
2323
class ProxyCheck
2424
{
@@ -108,10 +108,12 @@ protected function getProxyEnvConfig()
108108
*/
109109
protected function getProxyHeader()
110110
{
111-
if (!isset($_SERVER[$this->proxyHeaderName]) || empty($_SERVER[$this->proxyHeaderName])) {
111+
if (false === $this->proxyHeaderName || empty($_SERVER[$this->proxyHeaderName])) {
112112
return false;
113113
}
114-
return $_SERVER[$this->proxyHeaderName];
114+
115+
// Use PHP 5.3 compatible type casting
116+
return (string)$_SERVER[$this->proxyHeaderName];
115117
}
116118

117119
/**

src/Random.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @package Xmf
1919
* @author Richard Griffith <richard@geekwright.com>
2020
* @copyright 2015-2018 XOOPS Project (https://xoops.org)
21-
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
21+
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
2222
* @link https://xoops.org
2323
*/
2424
class Random
@@ -37,7 +37,29 @@ class Random
3737
*/
3838
public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
3939
{
40-
$token = hash($hash, random_bytes($bytes));
40+
if (function_exists('random_bytes')) {
41+
$randomData = random_bytes($bytes);
42+
} elseif (function_exists('openssl_random_pseudo_bytes')) {
43+
$crypto_strong = false;
44+
$randomData = openssl_random_pseudo_bytes($bytes, $crypto_strong);
45+
46+
if ($randomData === false) {
47+
throw new Exception("Could not generate secure random bytes.");
48+
}
49+
50+
if (!$crypto_strong) {
51+
throw new Exception("Non-cryptographically strong algorithm used for random bytes.");
52+
}
53+
} else {
54+
$randomData = md5(uniqid(mt_rand(), true));
55+
}
56+
57+
if ($randomData === null) {
58+
throw new Exception("Failed to generate random data.");
59+
}
60+
61+
$token = hash($hash, $randomData);
62+
4163
return $token;
4264
}
4365

@@ -55,7 +77,28 @@ public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
5577
*/
5678
public static function generateKey($hash = 'sha512', $bytes = 128)
5779
{
58-
$token = hash($hash, random_bytes($bytes));
80+
if (function_exists('random_bytes')) {
81+
$randomData = random_bytes($bytes);
82+
} elseif (function_exists('openssl_random_pseudo_bytes')) {
83+
$crypto_strong = false;
84+
$randomData = openssl_random_pseudo_bytes($bytes, $crypto_strong);
85+
86+
if ($randomData === false) {
87+
throw new Exception("Could not generate secure random bytes.");
88+
}
89+
90+
if (!$crypto_strong) {
91+
throw new Exception("Non-cryptographically strong algorithm used for random bytes.");
92+
}
93+
} else {
94+
$randomData = md5(uniqid(mt_rand(), true));
95+
}
96+
97+
if ($randomData === null) {
98+
throw new Exception("Failed to generate random data.");
99+
}
100+
101+
$token = hash($hash, $randomData);
59102
return $token;
60103
}
61104
}

src/Request.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @author trabis <lusopoemas@gmail.com>
2525
* @author Joomla!
2626
* @copyright 2011-2023 XOOPS Project (https://xoops.org)
27-
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27+
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
2828
* @link https://xoops.org
2929
*/
3030
class Request
@@ -338,6 +338,7 @@ public static function getIP($name, $default = '', $hash = 'default')
338338
*/
339339
public static function getHeader($headerName, $default = '')
340340
{
341+
/** @var string[] $headers */
341342
static $headers = null;
342343

343344
if (null === $headers) {
@@ -348,10 +349,10 @@ public static function getHeader($headerName, $default = '')
348349
$headers[strtolower($name)] = $value;
349350
}
350351
} else {
351-
// From joyview - http://php.net/manual/en/function.getallheaders.php
352+
// From joyview - https://php.net/manual/en/function.getallheaders.php
352353
foreach ($_SERVER as $name => $value) {
353354
if ('HTTP_' === substr($name, 0, 5)) {
354-
$translatedName = str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))));
355+
$translatedName = (string)str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))));
355356
$headers[$translatedName] = $value;
356357
}
357358
}
@@ -371,7 +372,7 @@ public static function getHeader($headerName, $default = '')
371372
* @param string $name variable to look for
372373
* @param string $hash hash to check
373374
*
374-
* @return boolean True if hash has an element 'name', otherwise false
375+
* @return bool True if hash has an element 'name', otherwise false
375376
*/
376377
public static function hasVar($name, $hash = 'default')
377378
{
@@ -391,10 +392,10 @@ public static function hasVar($name, $hash = 'default')
391392
/**
392393
* Set a variable in one of the request variables
393394
*
394-
* @param string $name Name
395-
* @param string $value Value
396-
* @param string $hash Hash
397-
* @param boolean $overwrite Boolean
395+
* @param string $name Name
396+
* @param string $value Value
397+
* @param string $hash Hash
398+
* @param bool $overwrite Boolean
398399
*
399400
* @return string Previous value
400401
*/
@@ -516,9 +517,9 @@ public static function get($hash = 'default', $mask = 0)
516517
/**
517518
* Sets a request variable
518519
*
519-
* @param array $array An associative array of key-value pairs
520-
* @param string $hash The request variable to set (POST, GET, FILES, METHOD)
521-
* @param boolean $overwrite If true and an existing key is found, the value is overwritten,
520+
* @param array $array An associative array of key-value pairs
521+
* @param string $hash The request variable to set (POST, GET, FILES, METHOD)
522+
* @param bool $overwrite If true and an existing key is found, the value is overwritten,
522523
* otherwise it is ignored
523524
*
524525
* @return void
@@ -575,7 +576,7 @@ protected static function cleanVar($var, $mask = 0, $type = null)
575576
if (null === $noHtmlFilter) {
576577
$noHtmlFilter = FilterInput::getInstance();
577578
}
578-
$var = $noHtmlFilter->clean($var, $type);
579+
$var = $noHtmlFilter::clean($var, $type);
579580
}
580581
}
581582

0 commit comments

Comments
 (0)