From afecfe439b217a22eb9f03d2a87eb7907436401c Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Sun, 12 Jul 2026 00:52:05 +0930 Subject: [PATCH] fix: avoid Passing null to parameter is deprecated messages trim() in PHP8 requires a string to be passed. Passing null is deprecated, and a deprecation message is emitted. For example: trim(): Passing null to parameter #1 ($string) of type string is deprecated at \/var\/www\/html\/server\/apps\/testing\/lib\/ServerFiles.php#143 Avoid these by ensuring that the value passed to trim() is always a string. Return appropriate "Bad Request" 400 status in the cases where an expected value was not provided. --- lib/ExpireShare.php | 3 +++ lib/LastLoginDate.php | 6 ++++++ lib/ServerFiles.php | 37 ++++++++++++++++++++++---------- lib/TestingSkeletonDirectory.php | 5 ++++- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/ExpireShare.php b/lib/ExpireShare.php index 65336a9..9b23629 100644 --- a/lib/ExpireShare.php +++ b/lib/ExpireShare.php @@ -60,6 +60,9 @@ private function getShareById($id) { } public function expireShare(array $param) { + if (!\array_key_exists('share_id', $param)) { + return new Result(null, 400, 'Share Id not provided'); + } $id = \trim($param["share_id"]); if (!$this->shareManager->shareApiEnabled()) { return new Result(null, 404, 'Share API is disabled'); diff --git a/lib/LastLoginDate.php b/lib/LastLoginDate.php index c5bb864..36cb7f5 100644 --- a/lib/LastLoginDate.php +++ b/lib/LastLoginDate.php @@ -61,6 +61,9 @@ public function __construct( * @return Result */ public function setLastLoginDate($param) { + if (!\array_key_exists('user', $param)) { + return new Result(null, 400, 'User id not provided'); + } $user = \trim($param['user']); try { $account = $this->accountMapper->getByUid($user); @@ -88,6 +91,9 @@ public function setLastLoginDate($param) { * @return Result */ public function getLastLoginDate($param) { + if (!\array_key_exists('user', $param)) { + return new Result(null, 400, 'User id not provided'); + } $user = \trim($param['user']); try { $account = $this->accountMapper->getByUid($user); diff --git a/lib/ServerFiles.php b/lib/ServerFiles.php index 8eae3c2..f79b998 100644 --- a/lib/ServerFiles.php +++ b/lib/ServerFiles.php @@ -55,7 +55,10 @@ public function __construct(IRequest $request) { * @return Result */ public function mkDir() { - $dir = \trim($this->request->getParam('dir'), '/'); + $dir = \trim($this->request->getParam('dir', ''), '/'); + if ($dir === "") { + return new Result(null, 400, "cannot create dir, dir not specified"); + } $targetDir = \OC::$SERVERROOT . "/$dir"; if (!\file_exists($targetDir)) { // Ask for the full mode, it will be masked by the current umask anyway @@ -79,7 +82,7 @@ public function mkDir() { * @return Result */ public function rmDir() { - $dir = \trim($this->request->getParam('dir'), '/'); + $dir = \trim($this->request->getParam('dir', ''), '/'); if ($dir === "") { return new Result(null, 400, "cannot delete dir, no dir name given"); } @@ -139,8 +142,11 @@ public function rmDir() { * @return Result */ public function readFile() { - $filePath = \trim($this->request->getParam('file'), '/'); - $isAbsolutePath = \trim($this->request->getParam('absolute')); + $filePath = \trim($this->request->getParam('file', ''), '/'); + if ($filePath === "") { + return new Result(null, 400, "cannot read file, file path not specified"); + } + $isAbsolutePath = \trim($this->request->getParam('absolute', '')); if ($isAbsolutePath === 'true') { $targetFile = "/$filePath"; } else { @@ -166,7 +172,10 @@ public function readFile() { * @return Result */ public function createFile() { - $filePath = \trim($this->request->getParam('file'), '/'); + $filePath = \trim($this->request->getParam('file', ''), '/'); + if ($filePath === '') { + return new Result(null, 400, "cannot create file, file path not specified"); + } $content = $this->request->getParam('content'); $targetFile = \OC::$SERVERROOT . "/$filePath"; $result = \file_put_contents($targetFile, $content); @@ -185,7 +194,7 @@ public function createFile() { * @return Result */ public function deleteFile() { - $filePath = \trim($this->request->getParam('file'), '/'); + $filePath = \trim($this->request->getParam('file', ''), '/'); if ($filePath === "") { return new Result(null, 400, "cannot delete file, no file name given"); } @@ -214,7 +223,7 @@ public function deleteFile() { */ public function listFiles() { $result = []; - $dir = \trim($this->request->getParam('dir'), '/'); + $dir = \trim($this->request->getParam('dir', ''), '/'); if ($dir === "") { return new Result(null, 400, "cannot list files in dir, no dir name given"); } @@ -248,16 +257,22 @@ public function listFiles() { * @return Result */ public function moveFile() { - $filePath = \trim($this->request->getParam('source'), '/'); - $isAbsolutePath = \trim($this->request->getParam('absolute')); + $filePath = \trim($this->request->getParam('source', ''), '/'); + if ($filePath === "") { + return new Result(null, 400, "cannot move file, file path not specified"); + } + $isAbsolutePath = \trim($this->request->getParam('absolute', '')); if ($isAbsolutePath === 'true') { $filePath = "/$filePath"; } else { $filePath = \OC::$SERVERROOT . "/$filePath"; } - $fileTarget = \trim($this->request->getParam('target'), '/'); - $isAbsolutePath = \trim($this->request->getParam('absolute')); + $fileTarget = \trim($this->request->getParam('target', ''), '/'); + if ($fileTarget === "") { + return new Result(null, 400, "cannot move file, file target not specified"); + } + $isAbsolutePath = \trim($this->request->getParam('absolute', '')); if ($isAbsolutePath === 'true') { $fileTarget = "/$fileTarget"; } else { diff --git a/lib/TestingSkeletonDirectory.php b/lib/TestingSkeletonDirectory.php index 1cf2007..0377bd0 100644 --- a/lib/TestingSkeletonDirectory.php +++ b/lib/TestingSkeletonDirectory.php @@ -58,7 +58,10 @@ public function get() { * @return Result */ public function set() { - $directory = \trim($this->request->getParam('directory'), '/'); + $directory = \trim($this->request->getParam('directory', ''), '/'); + if ($directory === '') { + return new Result(null, 400, "cannot set skeleton directory, dir not specified"); + } $folder = Filesystem::normalizePath($directory, true); if (Filesystem::isValidPath($folder) === false) { return new Result(null, 400, "invalid folder name");