diff --git a/lib/Configuration.php b/lib/Configuration.php index 83c53f5e..b02a72e4 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -160,7 +160,7 @@ class Configuration $opts = '_options'; foreach (self::getDefaults() as $section => $values) { // fill missing sections with default values - if (!array_key_exists($section, $config) || count($config[$section]) == 0) { + if (!array_key_exists($section, $config) || count($config[$section]) === 0) { $this->_configuration[$section] = $values; if (array_key_exists('dir', $this->_configuration[$section])) { $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir']; @@ -169,7 +169,7 @@ class Configuration } // provide different defaults for database model elseif ( - $section == 'model_options' && + $section === 'model_options' && $this->_configuration['model']['class'] === 'Database' ) { $values = array( @@ -180,7 +180,7 @@ class Configuration 'opt' => array(), ); } elseif ( - $section == 'model_options' && + $section === 'model_options' && $this->_configuration['model']['class'] === 'GoogleCloudStorage' ) { $values = array( @@ -189,7 +189,7 @@ class Configuration 'uniformacl' => false, ); } elseif ( - $section == 'model_options' && + $section === 'model_options' && $this->_configuration['model']['class'] === 'S3Storage' ) { $values = array( @@ -218,11 +218,11 @@ class Configuration // check for missing keys and set defaults if necessary else { // preserve configured SRI hashes - if ($section == 'sri' && array_key_exists($section, $config)) { + if ($section === 'sri' && array_key_exists($section, $config)) { $this->_configuration[$section] = $config[$section]; } foreach ($values as $key => $val) { - if ($key == 'dir') { + if ($key === 'dir') { $val = PATH . $val; } $result = $val; diff --git a/lib/Controller.php b/lib/Controller.php index 056647de..cef82a61 100644 --- a/lib/Controller.php +++ b/lib/Controller.php @@ -204,7 +204,7 @@ class Controller $lang = $this->_conf->getKey('languagedefault'); I18n::setLanguageFallback($lang); // force default language, if language selection is disabled and a default is set - if (!$this->_conf->getKey('languageselection') && strlen($lang) == 2) { + if (!$this->_conf->getKey('languageselection') && strlen($lang) === 2) { $_COOKIE['lang'] = $lang; setcookie('lang', $lang, array('SameSite' => 'Lax', 'Secure' => true)); } @@ -421,7 +421,7 @@ class Controller // label all the expiration options $expire = array(); foreach ($this->_conf->getSection('expire_options') as $time => $seconds) { - $expire[$time] = ($seconds == 0) ? I18n::_(ucfirst($time)) : Filter::formatHumanReadableTime($time); + $expire[$time] = ($seconds === 0) ? I18n::_(ucfirst($time)) : Filter::formatHumanReadableTime($time); } // translate all the formatter options diff --git a/lib/Data/Filesystem.php b/lib/Data/Filesystem.php index 7df9be13..9edb9900 100644 --- a/lib/Data/Filesystem.php +++ b/lib/Data/Filesystem.php @@ -309,7 +309,7 @@ class Filesystem extends AbstractData $file = $this->_path . DIRECTORY_SEPARATOR . 'salt.php'; if (is_readable($file)) { $items = explode('|', file_get_contents($file)); - if (count($items) == 3) { + if (count($items) === 3) { return $items[1]; } } diff --git a/lib/Data/GoogleCloudStorage.php b/lib/Data/GoogleCloudStorage.php index 670dc52e..74fa690e 100644 --- a/lib/Data/GoogleCloudStorage.php +++ b/lib/Data/GoogleCloudStorage.php @@ -90,7 +90,7 @@ class GoogleCloudStorage extends AbstractData */ private function _getKey($pasteid) { - if ($this->_prefix != '') { + if (!empty($this->_prefix)) { return $this->_prefix . '/' . $pasteid; } return $pasteid; @@ -258,15 +258,12 @@ class GoogleCloudStorage extends AbstractData if (strlen($name) > strlen($path) && substr($name, strlen($path), 1) !== '/') { continue; } - $info = $object->info(); - if (key_exists('metadata', $info) && key_exists('value', $info['metadata'])) { - $value = $info['metadata']['value']; - if (is_numeric($value) && intval($value) < $time) { - try { - $object->delete(); - } catch (NotFoundException $e) { - // deleted by another instance. - } + $value = $object->info()['metadata']['value'] ?? ''; + if (is_numeric($value) && intval($value) < $time) { + try { + $object->delete(); + } catch (NotFoundException $e) { + // deleted by another instance. } } } @@ -282,14 +279,14 @@ class GoogleCloudStorage extends AbstractData */ public function setValue($value, $namespace, $key = '') { - if ($key === '') { + if (empty($key)) { $key = 'config/' . $namespace; } else { $key = 'config/' . $namespace . '/' . $key; } $metadata = array('namespace' => $namespace); - if ($namespace != 'salt') { + if ($namespace !== 'salt') { $metadata['value'] = strval($value); } try { @@ -340,17 +337,15 @@ class GoogleCloudStorage extends AbstractData $now = time(); $prefix = $this->_prefix; - if ($prefix != '') { + if (!empty($prefix)) { $prefix .= '/'; } try { foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $object) { $metadata = $object->info()['metadata']; - if ($metadata != null && array_key_exists('expire_date', $metadata)) { - $expire_at = intval($metadata['expire_date']); - if ($expire_at != 0 && $expire_at < $now) { - array_push($expired, basename($object->name())); - } + $expire_at = $metadata['expire_date'] ?? ''; + if (is_numeric($expire_at) && intval($expire_at) < $now) { + array_push($expired, basename($object->name())); } if (count($expired) > $batchsize) { @@ -370,7 +365,7 @@ class GoogleCloudStorage extends AbstractData { $pastes = array(); $prefix = $this->_prefix; - if ($prefix != '') { + if (!empty($prefix)) { $prefix .= '/'; } diff --git a/lib/Data/S3Storage.php b/lib/Data/S3Storage.php index 81b61f7f..6939b3d4 100644 --- a/lib/Data/S3Storage.php +++ b/lib/Data/S3Storage.php @@ -148,7 +148,7 @@ class S3Storage extends AbstractData */ private function _getKey($pasteid) { - if ($this->_prefix != '') { + if (!empty($this->_prefix)) { return $this->_prefix . '/' . $pasteid; } return $pasteid; @@ -317,7 +317,7 @@ class S3Storage extends AbstractData public function purgeValues($namespace, $time) { $path = $this->_prefix; - if ($path != '') { + if (!empty($path)) { $path .= '/'; } $path .= 'config/' . $namespace; @@ -332,17 +332,15 @@ class S3Storage extends AbstractData 'Bucket' => $this->_bucket, 'Key' => $name, )); - if ($head->get('Metadata') != null && array_key_exists('value', $head->get('Metadata'))) { - $value = $head->get('Metadata')['value']; - if (is_numeric($value) && intval($value) < $time) { - try { - $this->_client->deleteObject(array( - 'Bucket' => $this->_bucket, - 'Key' => $name, - )); - } catch (S3Exception $e) { - // deleted by another instance. - } + $value = $head->get('Metadata')['value'] ?? ''; + if (is_numeric($value) && intval($value) < $time) { + try { + $this->_client->deleteObject(array( + 'Bucket' => $this->_bucket, + 'Key' => $name, + )); + } catch (S3Exception $e) { + // deleted by another instance. } } } @@ -359,7 +357,7 @@ class S3Storage extends AbstractData public function setValue($value, $namespace, $key = '') { $prefix = $this->_prefix; - if ($prefix != '') { + if (!empty($prefix)) { $prefix .= '/'; } @@ -370,7 +368,7 @@ class S3Storage extends AbstractData } $metadata = array('namespace' => $namespace); - if ($namespace != 'salt') { + if ($namespace !== 'salt') { $metadata['value'] = strval($value); } try { @@ -395,7 +393,7 @@ class S3Storage extends AbstractData public function getValue($namespace, $key = '') { $prefix = $this->_prefix; - if ($prefix != '') { + if (!empty($prefix)) { $prefix .= '/'; } @@ -424,7 +422,7 @@ class S3Storage extends AbstractData $expired = array(); $now = time(); $prefix = $this->_prefix; - if ($prefix != '') { + if (!empty($prefix)) { $prefix .= '/'; } @@ -434,11 +432,9 @@ class S3Storage extends AbstractData 'Bucket' => $this->_bucket, 'Key' => $object['Key'], )); - if ($head->get('Metadata') != null && array_key_exists('expire_date', $head->get('Metadata'))) { - $expire_at = intval($head->get('Metadata')['expire_date']); - if ($expire_at != 0 && $expire_at < $now) { - array_push($expired, $object['Key']); - } + $expire_at = $metadata['expire_date'] ?? ''; + if (is_numeric($expire_at) && intval($expire_at) < $now) { + array_push($expired, $object['Key']); } if (count($expired) > $batchsize) { @@ -458,7 +454,7 @@ class S3Storage extends AbstractData { $pastes = array(); $prefix = $this->_prefix; - if ($prefix != '') { + if (!empty($prefix)) { $prefix .= '/'; } diff --git a/lib/FormatV2.php b/lib/FormatV2.php index 6660c4f3..7f1912fa 100644 --- a/lib/FormatV2.php +++ b/lib/FormatV2.php @@ -40,7 +40,7 @@ class FormatV2 } // Make sure no additionnal keys were added. - if (count(array_keys($message)) != count($required_keys)) { + if (count(array_keys($message)) !== count($required_keys)) { return false; } diff --git a/lib/I18n.php b/lib/I18n.php index e3be023b..1d25ea37 100644 --- a/lib/I18n.php +++ b/lib/I18n.php @@ -183,7 +183,7 @@ class I18n } // load translations - if (self::$_language == 'en') { + if (self::$_language === 'en') { self::$_translations = array(); } else { $data = file_get_contents(self::_getPath(self::$_language . '.json')); @@ -200,14 +200,14 @@ class I18n */ public static function getAvailableLanguages() { - if (count(self::$_availableLanguages) == 0) { + if (count(self::$_availableLanguages) === 0) { self::$_availableLanguages[] = 'en'; // en.json is not part of the release archive $languageIterator = new AppendIterator(); $languageIterator->append(new GlobIterator(self::_getPath('??.json'))); $languageIterator->append(new GlobIterator(self::_getPath('???.json'))); // for jbo foreach ($languageIterator as $file) { $language = $file->getBasename('.json'); - if ($language != 'en') { + if ($language !== 'en') { self::$_availableLanguages[] = $language; } } @@ -276,11 +276,11 @@ class I18n public static function getLanguageLabels($languages = array()) { $file = self::_getPath('languages.json'); - if (count(self::$_languageLabels) == 0 && is_readable($file)) { + if (count(self::$_languageLabels) === 0 && is_readable($file)) { $data = file_get_contents($file); self::$_languageLabels = Json::decode($data); } - if (count($languages) == 0) { + if (count($languages) === 0) { return self::$_languageLabels; } return array_intersect_key(self::$_languageLabels, array_flip($languages)); @@ -367,7 +367,7 @@ class I18n return $n === 1 ? 0 : (($n === 0 || ($n % 100 > 0 && $n % 100 < 20)) ? 1 : 2); case 'ru': case 'uk': - return $n % 10 === 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2); + return $n % 10 === 1 && $n % 100 !== 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2); case 'sl': return $n % 100 === 1 ? 1 : ($n % 100 === 2 ? 2 : ($n % 100 === 3 || $n % 100 === 4 ? 3 : 0)); default: diff --git a/lib/Model/Comment.php b/lib/Model/Comment.php index d7bbc938..2a55b588 100644 --- a/lib/Model/Comment.php +++ b/lib/Model/Comment.php @@ -148,13 +148,13 @@ class Comment extends AbstractModel { // we generate an icon based on a SHA512 HMAC of the users IP, if configured $icon = $this->_conf->getKey('icon'); - if ($icon != 'none') { + if ($icon !== 'none') { $pngdata = ''; $hmac = TrafficLimiter::getHash(); - if ($icon == 'identicon') { + if ($icon === 'identicon') { $identicon = new Identicon(); $pngdata = $identicon->getImageDataUri($hmac, 16); - } elseif ($icon == 'jdenticon') { + } elseif ($icon === 'jdenticon') { $jdenticon = new Jdenticon(array( 'hash' => $hmac, 'size' => 16, @@ -164,13 +164,13 @@ class Comment extends AbstractModel ), )); $pngdata = $jdenticon->getImageDataUri('png'); - } elseif ($icon == 'vizhash') { + } elseif ($icon === 'vizhash') { $vh = new Vizhash16x16(); $pngdata = 'data:image/png;base64,' . base64_encode( $vh->generate($hmac) ); } - if ($pngdata != '') { + if (!empty($pngdata)) { if (!array_key_exists('meta', $data)) { $data['meta'] = array(); } diff --git a/lib/Model/Paste.php b/lib/Model/Paste.php index 9b55cb71..884b5fb1 100644 --- a/lib/Model/Paste.php +++ b/lib/Model/Paste.php @@ -150,7 +150,7 @@ class Paste extends AbstractModel $comment = new Comment($this->_conf, $this->_store); $comment->setPaste($this); $comment->setParentId($parentId); - if ($commentId !== '') { + if (!empty($commentId)) { $comment->setId($commentId); } return $comment; diff --git a/lib/Persistence/TrafficLimiter.php b/lib/Persistence/TrafficLimiter.php index 5838a598..2c937205 100644 --- a/lib/Persistence/TrafficLimiter.php +++ b/lib/Persistence/TrafficLimiter.php @@ -73,7 +73,7 @@ class TrafficLimiter extends AbstractPersistence self::setExempted($conf->getKey('exempted', 'traffic')); self::setLimit($conf->getKey('limit', 'traffic')); - if (($option = $conf->getKey('header', 'traffic')) !== '') { + if (!empty($option = $conf->getKey('header', 'traffic'))) { $httpHeader = 'HTTP_' . $option; if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) { self::$_ipKey = $httpHeader; diff --git a/lib/Proxy/AbstractProxy.php b/lib/Proxy/AbstractProxy.php index 65a7bc2f..b09c609d 100644 --- a/lib/Proxy/AbstractProxy.php +++ b/lib/Proxy/AbstractProxy.php @@ -55,7 +55,7 @@ abstract class AbstractProxy } if (!str_starts_with($link, $conf->getKey('basepath') . '?') || - parse_url($link, PHP_URL_HOST) != parse_url($conf->getKey('basepath'), PHP_URL_HOST) + parse_url($link, PHP_URL_HOST) !== parse_url($conf->getKey('basepath'), PHP_URL_HOST) ) { $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.'; return; diff --git a/lib/Proxy/YourlsProxy.php b/lib/Proxy/YourlsProxy.php index 3eea7e15..f1762e57 100644 --- a/lib/Proxy/YourlsProxy.php +++ b/lib/Proxy/YourlsProxy.php @@ -65,7 +65,7 @@ class YourlsProxy extends AbstractProxy */ protected function _extractShortUrl(array $data): ?string { - if (($data['statusCode'] ?? 0) == 200) { + if (($data['statusCode'] ?? 0) === 200) { return $data['shorturl'] ?? 0; } return null; diff --git a/lib/Request.php b/lib/Request.php index 43e3abba..2e386e6f 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -141,7 +141,7 @@ class Request if (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid'])) { if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken'])) { $this->_operation = 'delete'; - } elseif ($this->_operation != 'create') { + } elseif ($this->_operation !== 'create') { $this->_operation = 'read'; } } elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) { @@ -187,7 +187,7 @@ class Request $data['meta'] = $meta; } foreach ($required_keys as $key) { - $data[$key] = $this->getParam($key, $key == 'v' ? 1 : ''); + $data[$key] = $this->getParam($key, $key === 'v' ? 1 : ''); } // forcing a cast to int or float $data['v'] = $data['v'] + 0; @@ -266,7 +266,7 @@ class Request // simple cases if ( - ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') == 'JSONHttpRequest' || + ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') === 'JSONHttpRequest' || ( str_contains($acceptHeader, self::MIME_JSON) && !str_contains($acceptHeader, self::MIME_HTML) && diff --git a/lib/Vizhash16x16.php b/lib/Vizhash16x16.php index 12c2efcc..8ac03be7 100644 --- a/lib/Vizhash16x16.php +++ b/lib/Vizhash16x16.php @@ -103,7 +103,7 @@ class Vizhash16x16 // First, create an image with a specific gradient background. $op = 'v'; - if (($this->getInt() % 2) == 0) { + if (($this->getInt() % 2) === 0) { $op = 'h'; } $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0)); @@ -179,7 +179,7 @@ class Vizhash16x16 */ private function degrade($img, $direction, $color1, $color2) { - if ($direction == 'h') { + if ($direction === 'h') { $size = imagesx($img); $sizeinv = imagesy($img); } else { @@ -195,7 +195,7 @@ class Vizhash16x16 $r = $color1[0] + ((int) $diffs[0] * $i); $g = $color1[1] + ((int) $diffs[1] * $i); $b = $color1[2] + ((int) $diffs[2] * $i); - if ($direction == 'h') { + if ($direction === 'h') { imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b)); } else { imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b)); diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index b219f713..eb759ae4 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -249,7 +249,7 @@ endif; foreach ($EXPIRE as $key => $value) : ?> @@ -327,7 +327,7 @@ if ($isCpct) : foreach ($FORMATTER as $key => $value) : ?> @@ -412,7 +412,7 @@ if (!$isCpct) : foreach ($FORMATTER as $key => $value) : ?> diff --git a/tpl/bootstrap5.php b/tpl/bootstrap5.php index 18975d15..3e226be4 100644 --- a/tpl/bootstrap5.php +++ b/tpl/bootstrap5.php @@ -206,7 +206,7 @@ endif; foreach ($EXPIRE as $key => $value) : ?> @@ -287,7 +287,7 @@ endif; foreach ($FORMATTER as $key => $value) : ?> diff --git a/tst/Bootstrap.php b/tst/Bootstrap.php index e905312c..0a9a1abb 100644 --- a/tst/Bootstrap.php +++ b/tst/Bootstrap.php @@ -212,20 +212,15 @@ class Helper public static function rmDir($path): void { if (is_dir($path)) { - $path .= DIRECTORY_SEPARATOR; - $dir = dir($path); - while (false !== ($file = $dir->read())) { - if ($file != '.' && $file != '..') { - if (is_dir($path . $file)) { - self::rmDir($path . $file); - } elseif (is_file($path . $file)) { - if (!unlink($path . $file)) { - throw new Exception('Error deleting file "' . $path . $file . '".'); - } + foreach (new DirectoryIterator($path) as $file) { + if ($file->isFile()) { + if (!unlink($file->getPathname())) { + throw new Exception('Error deleting file "' . $file->getPathname() . '".'); } + } elseif ($file->isDir() && !$file->isDot()) { + self::rmDir($file->getPathname()); } } - $dir->close(); if (!rmdir($path)) { throw new Exception('Error deleting directory "' . $path . '".'); } @@ -361,7 +356,7 @@ class Helper file_get_contents($file) ); file_put_contents($file, $content); - if ($counter != count(self::$hashes)) { + if ($counter !== count(self::$hashes)) { throw new Exception('Mismatch between ' . count(self::$hashes) . ' found js files and ' . $counter . ' SRI hashes in lib/Configuration.php, please update lib/Configuration.php to match the list of js files.'); } } @@ -404,7 +399,7 @@ class BucketStub extends Bucket public function upload($data, array $options = array()) { - if (!is_string($data) || !key_exists('name', $options)) { + if (!is_string($data) || !array_key_exists('name', $options)) { throw new BadMethodCallException('not supported by this stub'); } @@ -432,21 +427,17 @@ class BucketStub extends Bucket public function object($name, array $options = array()) { - if (key_exists($name, $this->_objects)) { - return $this->_objects[$name]; - } else { - return new StorageObjectStub($this->_connection, $name, $this, null, $options); - } + return $this->_objects[$name] ?? new StorageObjectStub($this->_connection, $name, $this, null, $options); } public function objects(array $options = array()) { - $prefix = key_exists('prefix', $options) ? $options['prefix'] : ''; + $prefix = $options['prefix'] ?? ''; return new CallbackFilterIterator( new ArrayIterator($this->_objects), function ($current, $key, $iterator) use ($prefix) { - return substr($key, 0, strlen($prefix)) == $prefix; + return substr($key, 0, strlen($prefix)) === $prefix; } ); } @@ -563,7 +554,7 @@ class StorageObjectStub extends StorageObject public function exists(array $options = array()) { - return key_exists($this->_name, $this->_bucket->_objects); + return array_key_exists($this->_name, $this->_bucket->_objects); } /** @@ -571,7 +562,7 @@ class StorageObjectStub extends StorageObject */ public function delete(array $options = array()) { - if (key_exists($this->_name, $this->_bucket->_objects)) { + if (array_key_exists($this->_name, $this->_bucket->_objects)) { unset($this->_bucket->_objects[$this->_name]); } else { throw new NotFoundException('key ' . $this->_name . ' not found.'); @@ -647,7 +638,7 @@ class StorageObjectStub extends StorageObject public function info(array $options = array()) { - return key_exists('metadata',$this->_info) ? $this->_info['metadata'] : array(); + return $this->_info['metadata'] ?? array(); } public function reload(array $options = array()) @@ -884,7 +875,7 @@ class StorageClientStub extends StorageClient public function bucket($name, $userProject = false, array $config = array()) { - if (!key_exists($name, self::$_buckets)) { + if (!array_key_exists($name, self::$_buckets)) { self::$_buckets[$name] = new BucketStub($this->_connection, $name, array(), $this); } return self::$_buckets[$name]; @@ -895,7 +886,7 @@ class StorageClientStub extends StorageClient */ public function deleteBucket($name) { - if (key_exists($name, self::$_buckets)) { + if (array_key_exists($name, self::$_buckets)) { unset(self::$_buckets[$name]); } else { throw new NotFoundException(); @@ -949,7 +940,7 @@ class StorageClientStub extends StorageClient public function createBucket($name, array $options = array()) { - if (key_exists($name, self::$_buckets)) { + if (array_key_exists($name, self::$_buckets)) { throw new BadRequestException('already exists'); } $b = new BucketStub($this->_connection, $name, array(), $this);