From: Banana Date: Tue, 3 Oct 2023 09:28:22 +0000 (+0200) Subject: server php code cleanup and php8 X-Git-Tag: 1.5~7 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=b5ff43402b2077b328d2034f05b0af926427a492;p=selfpaste.git server php code cleanup and php8 --- diff --git a/webroot/index.php b/webroot/index.php index 2f252e7..29f4b94 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -1,41 +1,46 @@ _short = $short; } @@ -66,15 +85,16 @@ class Mancubus { /** * Either set _saveFilename to given string * or create from a random number. In this case _short needs this as a base + * * @param string $string - * @throws Exception + * @return void */ - public function setSaveFilename($string='') { + public function setSaveFilename(string $string=''): void { if($string != '') { $this->_saveFilename = $string; } else { - $r = random_int(1000, 9999); + $r = rand(1000, 9999); $this->_saveFilename = (string)$r; } } @@ -82,9 +102,10 @@ class Mancubus { /** * Set _shortURL to given string * or create based on SELFPASTE_URL and _short + * * @param string $string */ - public function setShortURL($string='') { + public function setShortURL(string $string=''): void { if($string != '') { $this->_shortURL = $string; } @@ -96,8 +117,10 @@ class Mancubus { /** * set the right storage path based on _saveFilename * and SELFPASTE_UPLOAD_DIR + * + * @return void */ - public function setStoragePath() { + public function setStoragePath(): void { $string = $this->_saveFilename; if(!empty($string)) { @@ -110,9 +133,10 @@ class Mancubus { /** * After setting importing stuff process the upload * return status and message + * * @return array */ - public function process() { + public function process(): array { $ret = array( 'message' => '', 'status' => false @@ -136,19 +160,22 @@ class Mancubus { /** * Cleans lifetime and floodfiles. - * @param boolean + * + * @param bool $verbose + * @return void */ - public function cleanupCronjob($verbose=false) { + public function cleanupCronjob(bool $verbose=false): void { $this->_cleanupFloodFiles($verbose); $this->_checkLifetime($verbose); } /** * Check if the POST upload worked + * * @return array message,status * @throws Exception */ - private function _checkFileUploadStatus() { + private function _checkFileUploadStatus(): array { $check = Summoner::checkFileUploadStatus($this->_uploadedData['error']); if($check['status'] === true) { @@ -162,10 +189,11 @@ class Mancubus { /** * Check if the uploaded file matches the allowed filetypes + * * @return array message,status * @throws Exception */ - private function _checkAllowedFiletype() { + private function _checkAllowedFiletype(): array { $message = "Filetype not supported"; $status = false; @@ -174,7 +202,7 @@ class Mancubus { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $workWith); finfo_close($finfo); - if(strpos(SELFPASTE_ALLOWED_FILETYPES,$mime) !== false) { + if(str_contains(SELFPASTE_ALLOWED_FILETYPES, $mime)) { $status = true; $message = "Filetype allowed"; } @@ -195,10 +223,11 @@ class Mancubus { /** * check if SELFPASTE_UPLOAD_DIR and _storagePath * is creatable. If so create _storagePath - * @return array + * + * @return array message,status * @throws Exception */ - private function _checkStorage() { + private function _checkStorage(): array { $message = "File storage failure"; $status = false; @@ -226,10 +255,11 @@ class Mancubus { /** * Move the tmp_file from _uploadedData to the new location * provided by _storagePath and _saveFilename - * @return array + * + * @return array message,status * @throws Exception */ - private function _moveUploadedFile() { + private function _moveUploadedFile(): array { $message = "File storage failure"; $status = false; @@ -237,7 +267,7 @@ class Mancubus { $workwithFilename = $this->_saveFilename; if(!empty($workwithPath) && !empty($workwithFilename)) { - $_newFilename = Summoner::endsWith($workwithPath,'/') ? $workwithPath : $workwithPath.'/'; + $_newFilename = str_ends_with($workwithPath,'/') ? $workwithPath : $workwithPath.'/'; $_newFilename .= $workwithFilename; if(move_uploaded_file($this->_uploadedData['tmp_name'], $_newFilename)) { $status = true; @@ -261,10 +291,11 @@ class Mancubus { /** * check if the current paste request is within limits * for this check if the file exists. If so just return the shortURL - * @return array + * + * @return array message,status * @throws Exception */ - private function _checkFlood() { + private function _checkFlood(): array { $message = "Failing flood requirements"; $status = false; @@ -299,12 +330,14 @@ class Mancubus { /** * clean up the flood tmp files. Everything older then 30 sec will be deleted. + * + * @param bool $verbose */ - private function _cleanupFloodFiles($verbose=false) { + private function _cleanupFloodFiles(bool $verbose=false): void { $iterator = new DirectoryIterator(SELFPASTE_UPLOAD_DIR); $now = time(); foreach ($iterator as $file) { - if($file->isDot() || $file->isDir() || Summoner::startsWith($file->getFilename(),'.')) continue; + if($file->isDot() || $file->isDir() || str_starts_with($file->getFilename(),'.')) continue; if ($now - $file->getCTime() >= SELFPASTE_FLOOD_LIFETIME) { if($verbose === true) echo "Delete ".$file->getFilename()."\n"; unlink(SELFPASTE_UPLOAD_DIR.'/'.$file->getFilename()); @@ -314,15 +347,17 @@ class Mancubus { /** * delete all pastes older than SELFPASTE_PASTE_LIFETIME + * + * @param bool $verbose */ - private function _checkLifetime($verbose=false) { + private function _checkLifetime(bool $verbose=false): void { $iterator = new RecursiveDirectoryIterator(SELFPASTE_UPLOAD_DIR); $datepointInThePastInSec = strtotime('-'.SELFPASTE_PASTE_LIFETIME.' days'); foreach (new RecursiveIteratorIterator($iterator) as $file) { $fname = $file->getFilename(); if($file->isDir() - || Summoner::startsWith($file->getFilename(),'.') + || str_starts_with($file->getFilename(),'.') || isset($fname[4]) ) continue; if ($file->getMTime() <= $datepointInThePastInSec) { @@ -333,4 +368,4 @@ class Mancubus { } } } -} \ No newline at end of file +} diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index 7c2ea3a..5eb9d24 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -1,28 +1,33 @@ 0xFFFFFFFF ? $id >> 32 : 0); // 32 bit big endian, top $idb = ($id & 0xFFFFFFFF); // 32 bit big endian, bottom @@ -205,10 +184,10 @@ class Summoner { * Decode a base64-encoded big-endian integer of up to 64 bits. * * @see https://www.jwz.org/base64-shortlinks/ - * @param $id - * @return false|int|string|string[] + * @param string $id + * @return int */ - static function b64sl_unpack_id($id) { + static function b64sl_unpack_id(string $id): int { $id = str_replace ('-', '+', $id); // decode URL-unsafe "+" "/" $id = str_replace ('_', '/', $id); $id = base64_decode ($id); @@ -222,10 +201,11 @@ class Summoner { * create based on the given string a path * each char in string is a dir * asdef -> a/s/d/e/f/ - * @param $string + * + * @param string $string * @return string */ - static function forwardslashStringToPath($string) { + static function forwardslashStringToPath(string $string): string { $ret = ''; if(!empty($string)) { for ($i = 0; $i < strlen($string); $i++) { diff --git a/webroot/view/created.inc.php b/webroot/view/created.inc.php index aee9608..e4a0c30 100644 --- a/webroot/view/created.inc.php +++ b/webroot/view/created.inc.php @@ -1,12 +1,18 @@ - + + selfpaste

Thank you for using selfpaste.