diff options
author | Simon Rettberg | 2023-11-15 17:53:47 +0100 |
---|---|---|
committer | Simon Rettberg | 2023-11-15 17:53:47 +0100 |
commit | aa49154e5a9ee135c3f0286ddda8a01a5290b1ed (patch) | |
tree | 0397403f11a8a890ee11458ea0b1887796480115 /inc | |
parent | idea: Always inspect as if strict_types is enabled (diff) | |
download | slx-admin-aa49154e5a9ee135c3f0286ddda8a01a5290b1ed.tar.gz slx-admin-aa49154e5a9ee135c3f0286ddda8a01a5290b1ed.tar.xz slx-admin-aa49154e5a9ee135c3f0286ddda8a01a5290b1ed.zip |
Fix more type errors, stricter typing
Diffstat (limited to 'inc')
-rw-r--r-- | inc/arrayutil.inc.php | 12 | ||||
-rw-r--r-- | inc/crypto.inc.php | 2 | ||||
-rw-r--r-- | inc/dashboard.inc.php | 2 | ||||
-rw-r--r-- | inc/database.inc.php | 4 | ||||
-rw-r--r-- | inc/dictionary.inc.php | 4 | ||||
-rw-r--r-- | inc/download.inc.php | 2 | ||||
-rw-r--r-- | inc/errorhandler.inc.php | 4 | ||||
-rw-r--r-- | inc/event.inc.php | 2 | ||||
-rw-r--r-- | inc/eventlog.inc.php | 2 | ||||
-rw-r--r-- | inc/fileutil.inc.php | 2 | ||||
-rw-r--r-- | inc/hook.inc.php | 4 | ||||
-rw-r--r-- | inc/iputil.inc.php | 2 | ||||
-rw-r--r-- | inc/mailer.inc.php | 2 | ||||
-rw-r--r-- | inc/message.inc.php | 3 | ||||
-rw-r--r-- | inc/module.inc.php | 2 | ||||
-rw-r--r-- | inc/paginate.inc.php | 2 | ||||
-rw-r--r-- | inc/permission.inc.php | 2 | ||||
-rw-r--r-- | inc/property.inc.php | 2 | ||||
-rw-r--r-- | inc/render.inc.php | 5 | ||||
-rw-r--r-- | inc/request.inc.php | 2 | ||||
-rw-r--r-- | inc/session.inc.php | 4 | ||||
-rw-r--r-- | inc/taskmanager.inc.php | 2 | ||||
-rw-r--r-- | inc/taskmanagercallback.inc.php | 2 | ||||
-rw-r--r-- | inc/trigger.inc.php | 2 | ||||
-rw-r--r-- | inc/user.inc.php | 2 | ||||
-rw-r--r-- | inc/util.inc.php | 32 |
26 files changed, 85 insertions, 21 deletions
diff --git a/inc/arrayutil.inc.php b/inc/arrayutil.inc.php index 7aded4db..3d93d7d5 100644 --- a/inc/arrayutil.inc.php +++ b/inc/arrayutil.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class ArrayUtil { @@ -68,4 +70,14 @@ class ArrayUtil return true; } + /** + * Force each element of given array to be of type $type. + */ + public static function forceType(array &$array, string $type): void + { + foreach ($array as &$elem) { + settype($elem, $type); + } + } + }
\ No newline at end of file diff --git a/inc/crypto.inc.php b/inc/crypto.inc.php index c82a5d82..acefcf67 100644 --- a/inc/crypto.inc.php +++ b/inc/crypto.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Crypto { diff --git a/inc/dashboard.inc.php b/inc/dashboard.inc.php index 716776d3..449fe7c0 100644 --- a/inc/dashboard.inc.php +++ b/inc/dashboard.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Dashboard { diff --git a/inc/database.inc.php b/inc/database.inc.php index 7805b508..83720baa 100644 --- a/inc/database.inc.php +++ b/inc/database.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Handle communication with the database * This is a very thin layer between you and PDO. @@ -163,7 +165,7 @@ class Database */ public static function lastInsertId(): int { - return self::$dbh->lastInsertId(); + return (int)self::$dbh->lastInsertId(); } /** diff --git a/inc/dictionary.inc.php b/inc/dictionary.inc.php index ee613d0b..3a2f9c2b 100644 --- a/inc/dictionary.inc.php +++ b/inc/dictionary.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Dictionary { @@ -100,6 +102,8 @@ class Dictionary if ($lang === null) $lang = LANG; $path = Util::safePath("modules/{$module}/lang/{$lang}/{$file}.json"); + if ($path === null) + ErrorHandler::traceError("Invalid path"); if (isset(self::$stringCache[$path])) return self::$stringCache[$path]; if (!file_exists($path)) diff --git a/inc/download.inc.php b/inc/download.inc.php index 49ebbfbc..578992a6 100644 --- a/inc/download.inc.php +++ b/inc/download.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Download { diff --git a/inc/errorhandler.inc.php b/inc/errorhandler.inc.php index 8e49353c..ce969966 100644 --- a/inc/errorhandler.inc.php +++ b/inc/errorhandler.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + use JetBrains\PhpStorm\NoReturn; class ErrorHandler @@ -146,6 +148,6 @@ SADFACE; } $arg = 'Array(' . count($arg) . ')'; } - return $arg; + return (string)$arg; } }
\ No newline at end of file diff --git a/inc/event.inc.php b/inc/event.inc.php index db5bf1f1..4d02b580 100644 --- a/inc/event.inc.php +++ b/inc/event.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Class with static functions that are called when a specific event * took place, like the server has been booted, or the interface address diff --git a/inc/eventlog.inc.php b/inc/eventlog.inc.php index 93ca5c5c..99585abd 100644 --- a/inc/eventlog.inc.php +++ b/inc/eventlog.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Class to add entries to the event log. Technically this class belongs to the * eventlog module, but since it is used in so many places, this helper resides diff --git a/inc/fileutil.inc.php b/inc/fileutil.inc.php index 86a5ebc5..e986b2de 100644 --- a/inc/fileutil.inc.php +++ b/inc/fileutil.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class FileUtil { diff --git a/inc/hook.inc.php b/inc/hook.inc.php index abeac68e..f7ca617d 100644 --- a/inc/hook.inc.php +++ b/inc/hook.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Generic helper for getting and executing hooks. */ @@ -73,7 +75,7 @@ class Hook try { return (include $this->file); } catch (Exception $e) { - error_log($e); + error_log($e->getMessage()); return false; } } diff --git a/inc/iputil.inc.php b/inc/iputil.inc.php index ecd8c198..caac7349 100644 --- a/inc/iputil.inc.php +++ b/inc/iputil.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class IpUtil { diff --git a/inc/mailer.inc.php b/inc/mailer.inc.php index b2fb7741..bfdcd320 100644 --- a/inc/mailer.inc.php +++ b/inc/mailer.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Mailer { diff --git a/inc/message.inc.php b/inc/message.inc.php index 482e4ba4..a26369f5 100644 --- a/inc/message.inc.php +++ b/inc/message.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Message { private static $list = array(); @@ -62,6 +64,7 @@ class Message default: $icon = ''; } + ArrayUtil::forceType($params, 'string'); self::$list[] = array( 'type' => $type, 'icon' => $icon, diff --git a/inc/module.inc.php b/inc/module.inc.php index 115c9e9a..b072c4a2 100644 --- a/inc/module.inc.php +++ b/inc/module.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Module { /* diff --git a/inc/paginate.inc.php b/inc/paginate.inc.php index b9a1ed78..7757228a 100644 --- a/inc/paginate.inc.php +++ b/inc/paginate.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Paginate { private $query; diff --git a/inc/permission.inc.php b/inc/permission.inc.php index 798f9350..f346f1da 100644 --- a/inc/permission.inc.php +++ b/inc/permission.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + class Permission { private static $permissions = array( diff --git a/inc/property.inc.php b/inc/property.inc.php index 5efcfe3a..dc1e4719 100644 --- a/inc/property.inc.php +++ b/inc/property.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Get or set simple key-value-pairs, backed by the database * to make them persistent. diff --git a/inc/render.inc.php b/inc/render.inc.php index 68a0cd19..a636382e 100644 --- a/inc/render.inc.php +++ b/inc/render.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + require_once('inc/util.inc.php'); require_once('Mustache/Autoloader.php'); @@ -187,7 +189,8 @@ class Render * Parse template with given params and return; do not add to body * @param string $template name of template, relative to templates/, without .html extension * @param array $params tags to render into template - * @param string $module name of module to load template from; defaults to currently active module + * @param ?string $module name of module to load template from; defaults to currently active module + * @param ?string $lang override language if not null * @return string Rendered template */ public static function parse(string $template, array $params = [], ?string $module = null, ?string $lang = null): string diff --git a/inc/request.inc.php b/inc/request.inc.php index c499a9d9..cd782d99 100644 --- a/inc/request.inc.php +++ b/inc/request.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Wrapper for getting fields from the request (GET, POST, ...) */ diff --git a/inc/session.inc.php b/inc/session.inc.php index 622eb320..ccb878cd 100644 --- a/inc/session.inc.php +++ b/inc/session.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + require_once('config.php'); class Session @@ -141,7 +143,7 @@ class Session } // Refresh cookie if appropriate self::setupSessionAccounting(Request::isGet() && $row['dateline'] + 86400 < $now + CONFIG_SESSION_TIMEOUT); - self::$userId = $row['userid']; + self::$userId = (int)$row['userid']; self::$data = @json_decode($row['data'], true); if (!is_array(self::$data)) { self::$data = []; diff --git a/inc/taskmanager.inc.php b/inc/taskmanager.inc.php index 6c9d63c2..d9396901 100644 --- a/inc/taskmanager.inc.php +++ b/inc/taskmanager.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Interface to the external task manager. */ diff --git a/inc/taskmanagercallback.inc.php b/inc/taskmanagercallback.inc.php index 321475ac..01301be4 100644 --- a/inc/taskmanagercallback.inc.php +++ b/inc/taskmanagercallback.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * Contains all callbacks for detached taskmanager tasks. */ diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php index 4776855c..d0d5d365 100644 --- a/inc/trigger.inc.php +++ b/inc/trigger.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * This is one giant class containing various functions that will generate * required config files, daemon instances and more, mostly through the Taskmanager. diff --git a/inc/user.inc.php b/inc/user.inc.php index 4d278e1b..01b4d210 100644 --- a/inc/user.inc.php +++ b/inc/user.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + use JetBrains\PhpStorm\NoReturn; require_once('inc/session.inc.php'); diff --git a/inc/util.inc.php b/inc/util.inc.php index 0c402a6a..a5ccdc77 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + use JetBrains\PhpStorm\NoReturn; class Util @@ -98,7 +100,7 @@ class Util * @param int $shift how many units to skip, i.e. if you pass in KiB or MiB * @return string human-readable string representing the given file size */ - public static function readableFileSize(float $bytes, int $decimals = -1, int $shift = 0): string + public static function readableFileSize($bytes, int $decimals = -1, int $shift = 0): string { // round doesn't reliably work for large floats, pick workaround depending on OS if (PHP_INT_SIZE === 4) { @@ -113,10 +115,10 @@ class Util } else { $bytes /= 1024 ** $factor; if ($decimals === -1) { - $decimals = 2 - floor(strlen((int)$bytes) - 1); + $decimals = 2 - strlen((string)floor($bytes)) - 1; } } - return Dictionary::number($bytes, $decimals) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); + return Dictionary::number((float)$bytes, $decimals) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); } public static function sanitizeFilename(string $name): string @@ -131,25 +133,24 @@ class Util * * @param string $path path to check for safety * @param string $prefix required prefix of $path - * @return false|string */ - public static function safePath(string $path, string $prefix = ''): string + public static function safePath(string $path, string $prefix = ''): ?string { if (empty($path)) - return false; + return null; $path = trim($path); if ($path[0] == '/' || preg_match('/[\x00-\x19?*]/', $path)) - return false; + return null; if (strpos($path, '..') !== false) - return false; + return null; if (substr($path, 0, 2) !== './') $path = "./$path"; - if (empty($prefix)) - return $path; - if (substr($prefix, 0, 2) !== './') - $prefix = "./$prefix"; - if (substr($path, 0, strlen($prefix)) !== $prefix) - return false; + if (!empty($prefix)) { + if (substr($prefix, 0, 2) !== './') + $prefix = "./$prefix"; + if (substr($path, 0, strlen($prefix)) !== $prefix) + return null; + } return $path; } @@ -381,7 +382,6 @@ class Util */ public static function formatDuration(int $seconds, bool $showSecs = true): string { - settype($seconds, 'int'); static $UNITS = ['y' => 31536000, 'mon' => 2592000, 'd' => 86400]; $parts = []; $prev = false; @@ -397,7 +397,7 @@ class Util $prev = true; } } - $parts[] = gmdate($showSecs ? 'H:i:s' : 'H:i', $seconds); + $parts[] = gmdate($showSecs ? 'H:i:s' : 'H:i', (int)$seconds); return implode(' ', $parts); } |