diff --git a/src/AutoLoader.php b/src/AutoLoader.php index 565f35c4..a6bb1d95 100644 --- a/src/AutoLoader.php +++ b/src/AutoLoader.php @@ -5,10 +5,10 @@ final class AutoLoader { public static function init() { - spl_autoload_register([__CLASS__, '_include']); + spl_autoload_register([__CLASS__, 'includeClassName']); } - public static function _include($className) + public static function includeClassName($className) { if (strpos($className, 'Szurubooru') === false) return; @@ -19,7 +19,7 @@ final class AutoLoader else $className = __DIR__ . DIRECTORY_SEPARATOR . $className; $className .= '.php'; - include $className; + include($className); } } diff --git a/src/Config.php b/src/Config.php index 051db826..d01eac94 100644 --- a/src/Config.php +++ b/src/Config.php @@ -38,14 +38,14 @@ class Config extends \ArrayObject foreach (explode('.', $section) as $subSection) { - if (!isset($ptr->$subSection)) - $ptr->$subSection = new self(); + if (!$ptr->offsetExists($subSection)) + $ptr->offsetSet($subSection, new self()); $ptr = $ptr->$subSection; } foreach ($value as $sectionKey => $sectionValue) - $ptr->$sectionKey = $sectionValue; + $ptr->offsetSet($sectionKey, $sectionValue); } } } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 1fa972da..885903ff 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -57,7 +57,7 @@ final class UserController extends AbstractController $this->privilegeService->assertPrivilege(\Szurubooru\Privilege::REGISTER); $formData = new \Szurubooru\FormData\RegistrationFormData($this->inputReader); $user = $this->userService->createUser($formData); - return array_merge((array) $this->userViewProxy->fromEntity($user), ['confirmed' => $user->emailUnconfirmed == null]); + return array_merge((array) $this->userViewProxy->fromEntity($user), ['confirmed' => !$user->emailUnconfirmed]); } public function updateUser($userNameOrEmail) diff --git a/src/Controllers/ViewProxies/AbstractViewProxy.php b/src/Controllers/ViewProxies/AbstractViewProxy.php index 5e532b9e..262f6465 100644 --- a/src/Controllers/ViewProxies/AbstractViewProxy.php +++ b/src/Controllers/ViewProxies/AbstractViewProxy.php @@ -7,6 +7,11 @@ abstract class AbstractViewProxy public function fromArray($entities) { - return array_map(function($entity) { return static::fromEntity($entity); }, $entities); + return array_map( + function($entity) + { + return static::fromEntity($entity); + }, + $entities); } } diff --git a/src/Dao/Services/AbstractSearchService.php b/src/Dao/Services/AbstractSearchService.php index 6e523eb7..80701584 100644 --- a/src/Dao/Services/AbstractSearchService.php +++ b/src/Dao/Services/AbstractSearchService.php @@ -39,17 +39,17 @@ abstract class AbstractSearchService $entities = []; foreach ($cursor as $arrayEntity) - $entities []= $this->entityConverter->toEntity($arrayEntity); + $entities[] = $this->entityConverter->toEntity($arrayEntity); return new \Szurubooru\Dao\SearchResult($searchFilter, $entities, $totalRecords); } - protected function decorateFilterWithBasicTokens(&$filter, $basicTokens) + protected function decorateFilterWithBasicTokens($filter, $basicTokens) { throw new \BadMethodCallException('Not supported'); } - protected function decorateFilterWithComplexTokens(&$filter, $complexTokens) + protected function decorateFilterWithComplexTokens($filter, $complexTokens) { throw new \BadMethodCallException('Not supported'); } @@ -76,9 +76,9 @@ abstract class AbstractSearchService foreach ($tokens as $token) { $token = preg_split('/,|\s+/', $token); - if (count($token) == 2) + if (count($token) === 2) { - $orderDir = $token[1] == 'desc' ? self::ORDER_DESC : self::ORDER_ASC; + $orderDir = $token[1] === 'desc' ? self::ORDER_DESC : self::ORDER_ASC; $orderToken = $token[0]; } else @@ -113,7 +113,7 @@ abstract class AbstractSearchService } else { - $basicTokens []= $token; + $basicTokens[] = $token; } } diff --git a/src/Dao/Services/UserSearchService.php b/src/Dao/Services/UserSearchService.php index f52dfda5..3e9b2ab1 100644 --- a/src/Dao/Services/UserSearchService.php +++ b/src/Dao/Services/UserSearchService.php @@ -10,22 +10,12 @@ class UserSearchService extends AbstractSearchService protected function getOrderColumn($token) { - switch ($token) - { - case 'name': - return 'name'; + if ($token === 'name') + return 'name'; - case 'registrationDate': - case 'registrationTime': - case 'registered': - case 'joinDate': - case 'joinTime': - case 'joined': - return 'registrationTime'; + if (in_array($token, ['registrationDate', 'registrationTime', 'registered', 'joinDate', 'joinTime', 'joined'])) + return 'registrationTime'; - default: - return null; - } + return null; } - } diff --git a/src/Helpers/EnumHelper.php b/src/Helpers/EnumHelper.php index d325e6ab..bf7c583d 100644 --- a/src/Helpers/EnumHelper.php +++ b/src/Helpers/EnumHelper.php @@ -5,40 +5,53 @@ class EnumHelper { public static function accessRankToString($accessRank) { - switch ($accessRank) - { - case \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS: return 'anonymous'; - case \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER: return 'regularUser'; - case \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER: return 'powerUser'; - case \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR: return 'moderator'; - case \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR: return 'administrator'; - default: - throw new \DomainException('Invalid access rank!'); - } + $map = + [ + \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS => 'anonymous', + \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER => 'regularUser', + \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER => 'powerUser', + \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR => 'moderator', + \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR => 'administrator', + ]; + + if (!isset($map[$accessRank])) + throw new \DomainException('Invalid access rank!'); + + return $map[$accessRank]; } public static function accessRankFromString($accessRankString) { - switch (trim(strtolower($accessRankString))) - { - case 'anonymous': return \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS; - case 'regularuser': return \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER; - case 'poweruser': return \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER; - case 'moderator': return \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR; - case 'administrator': return \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR; - default: - throw new \DomainException('Unrecognized access rank: ' . $accessRankString); - } + $map = + [ + 'anonymous' => \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS, + 'regularUser' => \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, + 'powerUser' => \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER, + 'moderator' => \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR, + 'administrator' => \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR, + ]; + + $key = trim(strtolower($accessRankString)); + if (!isset($map[$key])) + throw new \DomainException('Unrecognized access rank: ' . $accessRankString); + + return $map[$key]; } public static function avatarStyleFromString($avatarStyleString) { - switch (trim(strtolower($avatarStyleString))) - { - case 'gravatar': return \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR; - case 'manual': return \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL; - case 'none': - case 'blank': return \Szurubooru\Entities\User::AVATAR_STYLE_BLANK; - } + $map = + [ + 'gravatar' => \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR, + 'manual' => \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL, + 'none' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK, + 'blank' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK, + ]; + + $key = trim(strtolower($avatarStyleString)); + if (!isset($map[$key])) + throw new \DomainException('Unrecognized avatar style: ' . $avatarStyleString); + + return $map[$key]; } } diff --git a/src/Helpers/HttpHelper.php b/src/Helpers/HttpHelper.php index 1793c726..884cfa4e 100644 --- a/src/Helpers/HttpHelper.php +++ b/src/Helpers/HttpHelper.php @@ -10,7 +10,7 @@ class HttpHelper public function setHeader($key, $value) { - header("$key: $value"); + header($key . ': ' . $value); } public function output($data) diff --git a/src/Helpers/InputReader.php b/src/Helpers/InputReader.php index d8b72ccb..d3e5d568 100644 --- a/src/Helpers/InputReader.php +++ b/src/Helpers/InputReader.php @@ -1,23 +1,27 @@ $value) - $this->$key = $value; + $this->offsetSet($key, $value); } } - public function __get($key) + public function offsetGet($index) { - return null; + if (!parent::offsetExists($index)) + return null; + return parent::offsetGet($index); } } diff --git a/src/Router.php b/src/Router.php index fbc2fe49..1e28ec72 100644 --- a/src/Router.php +++ b/src/Router.php @@ -27,7 +27,7 @@ class Router private function route($method, $query, $route) { - $this->routes[$method] []= new Route($query, $route); + $this->routes[$method][] = new Route($query, $route); } public function handle($method, $request) @@ -40,7 +40,7 @@ class Router if ($route->handle($request, $output)) { return $output; - } + } } throw new \DomainException('Unhandled request address: ' . $request); diff --git a/src/Services/AuthService.php b/src/Services/AuthService.php index e4cc37b8..89c9f273 100644 --- a/src/Services/AuthService.php +++ b/src/Services/AuthService.php @@ -49,7 +49,7 @@ class AuthService $this->validateUser($user); $passwordHash = $this->passwordService->getHash($password); - if ($user->passwordHash != $passwordHash) + if ($user->passwordHash !== $passwordHash) throw new \InvalidArgumentException('Specified password is invalid.'); $this->loginToken = $this->createAndSaveLoginToken($user); @@ -60,7 +60,7 @@ class AuthService public function loginFromToken($loginTokenName) { $loginToken = $this->tokenService->getByName($loginTokenName); - if ($loginToken->purpose != \Szurubooru\Entities\Token::PURPOSE_LOGIN) + if ($loginToken->purpose !== \Szurubooru\Entities\Token::PURPOSE_LOGIN) throw new \Exception('This token is not a login token.'); $user = $this->userService->getById($loginToken->additionalData); diff --git a/src/Services/EmailService.php b/src/Services/EmailService.php index 956ac328..51859966 100644 --- a/src/Services/EmailService.php +++ b/src/Services/EmailService.php @@ -77,17 +77,17 @@ class EmailService $messageId = sha1(date('r') . uniqid()) . '@' . $domain; $headers = []; - $headers []= sprintf('MIME-Version: 1.0'); - $headers []= sprintf('Content-Transfer-Encoding: 7bit'); - $headers []= sprintf('Date: %s', date('r')); - $headers []= sprintf('Message-ID: <%s>', $messageId); - $headers []= sprintf('From: %s <%s>', $senderName, $senderEmail); - $headers []= sprintf('Reply-To: %s', $senderEmail); - $headers []= sprintf('Return-Path: %s', $senderEmail); - $headers []= sprintf('Subject: %s', $subject); - $headers []= sprintf('Content-Type: text/plain; charset=utf-8'); - $headers []= sprintf('X-Mailer: PHP/%s', phpversion()); - $headers []= sprintf('X-Originating-IP: %s', $clientIp); + $headers[] = sprintf('MIME-Version: 1.0'); + $headers[] = sprintf('Content-Transfer-Encoding: 7bit'); + $headers[] = sprintf('Date: %s', date('r')); + $headers[] = sprintf('Message-ID: <%s>', $messageId); + $headers[] = sprintf('From: %s <%s>', $senderName, $senderEmail); + $headers[] = sprintf('Reply-To: %s', $senderEmail); + $headers[] = sprintf('Return-Path: %s', $senderEmail); + $headers[] = sprintf('Subject: %s', $subject); + $headers[] = sprintf('Content-Type: text/plain; charset=utf-8'); + $headers[] = sprintf('X-Mailer: PHP/%s', phpversion()); + $headers[] = sprintf('X-Originating-IP: %s', $clientIp); $senderEmail = $this->config->basic->emailAddress; $encodedSubject = '=?UTF-8?B?' . base64_encode($subject) . '?='; diff --git a/src/Services/FileService.php b/src/Services/FileService.php index a9de8d50..630852a9 100644 --- a/src/Services/FileService.php +++ b/src/Services/FileService.php @@ -17,8 +17,9 @@ class FileService $finalSource = $this->getFullPath($source); $daysToLive = isset($options->daysToLive) - ? $options->daysToLive - : 7; + ? $options->daysToLive + : 7; + $secondsToLive = $daysToLive * 24 * 60 * 60; $lastModified = filemtime($finalSource); $eTag = md5(file_get_contents($finalSource)); //todo: faster @@ -29,7 +30,7 @@ class FileService $eTagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH'], "\" \t\r\n") - : false; + : null; $this->httpHelper->setHeader('ETag', '"' . $eTag . '"'); $this->httpHelper->setHeader('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $lastModified)); @@ -38,20 +39,15 @@ class FileService $this->httpHelper->setHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $secondsToLive)); if (isset($options->customFileName)) - { $this->httpHelper->setHeader('Content-Disposition', 'inline; filename="' . $options->customFileName . '"'); - } - if (isset($options->mimeType)) - { - $this->httpHelper->setHeader('Content-Type', $options->mimeType); - } - else - { - $this->httpHelper->setHeader('Content-Type', mime_content_type($finalSource)); - } + $this->httpHelper->setHeader( + 'Content-Type', + isset($options->mimeType) + ? $options->mimeType + : mime_content_type($finalSource)); - if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag) + if (strtotime($ifModifiedSince) === $lastModified or $eTagHeader === $eTag) { $this->httpHelper->setResponseCode(304); } diff --git a/src/Services/PrivilegeService.php b/src/Services/PrivilegeService.php index 8b6c29c1..3a27d44d 100644 --- a/src/Services/PrivilegeService.php +++ b/src/Services/PrivilegeService.php @@ -21,7 +21,7 @@ class PrivilegeService { if (!isset($this->privilegeMap[$allowedAccessRank])) $this->privilegeMap[$allowedAccessRank] = []; - $this->privilegeMap[$allowedAccessRank] []= $privilegeName; + $this->privilegeMap[$allowedAccessRank][] = $privilegeName; } } } @@ -58,16 +58,16 @@ class PrivilegeService $loggedInUser = $this->authService->getLoggedInUser(); if ($userIdentifier instanceof \Szurubooru\Entities\User) { - return $loggedInUser->name == $userIdentifier->name; + return $loggedInUser->name === $userIdentifier->name; } elseif (is_string($userIdentifier)) { if ($loggedInUser->email) { - if ($loggedInUser->email == $userIdentifier) + if ($loggedInUser->email === $userIdentifier) return true; } - return $loggedInUser->name == $userIdentifier; + return $loggedInUser->name === $userIdentifier; } else { diff --git a/src/Services/ThumbnailGenerators/ImageGdThumbnailGenerator.php b/src/Services/ThumbnailGenerators/ImageGdThumbnailGenerator.php index 506d029a..c63586ad 100644 --- a/src/Services/ThumbnailGenerators/ImageGdThumbnailGenerator.php +++ b/src/Services/ThumbnailGenerators/ImageGdThumbnailGenerator.php @@ -59,19 +59,19 @@ class ImageGdThumbnailGenerator implements IThumbnailGenerator if (($dstHeight / $dstWidth) > ($srcHeight / $srcWidth)) { - $h = $srcHeight; - $w = $h * $dstWidth / $dstHeight; + $cropHeight = $srcHeight; + $cropWidth = $srcHeight * $dstWidth / $dstHeight; } else { - $w = $srcWidth; - $h = $w * $dstHeight / $dstWidth; + $cropWidth = $srcWidth; + $cropHeight = $srcWidth * $dstHeight / $dstWidth; } - $x = ($srcWidth - $w) / 2; - $y = ($srcHeight - $h) / 2; + $cropX = ($srcWidth - $cropWidth) / 2; + $cropY = ($srcHeight - $cropHeight) / 2; $dstImage = imagecreatetruecolor($dstWidth, $dstHeight); - imagecopyresampled($dstImage, $srcImage, 0, 0, $x, $y, $dstWidth, $dstHeight, $w, $h); + imagecopyresampled($dstImage, $srcImage, 0, 0, $cropX, $cropY, $dstWidth, $dstHeight, $cropWidth, $cropHeight); return $dstImage; } @@ -82,17 +82,17 @@ class ImageGdThumbnailGenerator implements IThumbnailGenerator if (($dstHeight / $dstWidth) < ($srcHeight / $srcWidth)) { - $h = $dstHeight; - $w = $h * $srcWidth / $srcHeight; + $cropHeight = $dstHeight; + $cropWidth = $dstHeight * $srcWidth / $srcHeight; } else { - $w = $dstWidth; - $h = $w * $srcHeight / $srcWidth; + $cropWidth = $dstWidth; + $cropHeight = $dstWidth * $srcHeight / $srcWidth; } - $dstImage = imagecreatetruecolor($w, $h); - imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $w, $h, $srcWidth, $srcHeight); + $dstImage = imagecreatetruecolor($cropWidth, $cropHeight); + imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $cropWidth, $cropHeight, $srcWidth, $srcHeight); return $dstImage; } } diff --git a/src/Services/ThumbnailGenerators/ImageImagickThumbnailGenerator.php b/src/Services/ThumbnailGenerators/ImageImagickThumbnailGenerator.php index fe247072..7a37f819 100644 --- a/src/Services/ThumbnailGenerators/ImageImagickThumbnailGenerator.php +++ b/src/Services/ThumbnailGenerators/ImageImagickThumbnailGenerator.php @@ -41,19 +41,19 @@ class ImageImagickThumbnailGenerator implements IThumbnailGenerator if (($dstHeight / $dstWidth) > ($srcHeight / $srcWidth)) { - $h = $dstHeight; - $w = $h * $srcWidth / $srcHeight; + $cropHeight = $dstHeight; + $cropWidth = $dstHeight * $srcWidth / $srcHeight; } else { - $w = $dstWidth; - $h = $w * $srcHeight / $srcWidth; + $cropWidth = $dstWidth; + $cropHeight = $dstWidth * $srcHeight / $srcWidth; } - $x = ($srcWidth - $w) / 2; - $y = ($srcHeight - $h) / 2; + $cropX = ($cropWidth - $dstWidth) >> 1; + $cropY = ($cropHeight - $dstHeight) >> 1; - $srcImage->resizeImage($w, $h, \imagick::FILTER_LANCZOS, 0.9); - $srcImage->cropImage($dstWidth, $dstHeight, ($w - $dstWidth) >> 1, ($h - $dstHeight) >> 1); + $srcImage->resizeImage($cropWidth, $cropHeight, \imagick::FILTER_LANCZOS, 0.9); + $srcImage->cropImage($dstWidth, $dstHeight, $cropX, $cropY); $srcImage->setImagePage(0, 0, 0, 0); } @@ -64,15 +64,15 @@ class ImageImagickThumbnailGenerator implements IThumbnailGenerator if (($dstHeight / $dstWidth) < ($srcHeight / $srcWidth)) { - $h = $dstHeight; - $w = $h * $srcWidth / $srcHeight; + $cropHeight = $dstHeight; + $cropWidth = $dstHeight * $srcWidth / $srcHeight; } else { - $w = $dstWidth; - $h = $w * $srcHeight / $srcWidth; + $cropWidth = $dstWidth; + $cropHeight = $dstWidth * $srcHeight / $srcWidth; } - $srcImage->resizeImage($w, $h, \imagick::FILTER_LANCZOS, 0.9); + $srcImage->resizeImage($cropWidth, $cropHeight, \imagick::FILTER_LANCZOS, 0.9); } } diff --git a/src/Services/UserService.php b/src/Services/UserService.php index 5f4b5050..c0e6a8ee 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -67,7 +67,7 @@ class UserService public function getFiltered(\Szurubooru\FormData\SearchFormData $formData) { $pageSize = intval($this->config->users->usersPerPage); - $this->validator->validateNumber($formData->page); + $this->validator->validateNumber($formData->pageNumber); $searchFilter = new \Szurubooru\Dao\SearchFilter($pageSize, $formData); return $this->userSearchService->getFiltered($searchFilter); } @@ -95,7 +95,7 @@ class UserService $user->lastLoginTime = null; $user->avatarStyle = \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR; - $this->sendActivationEmailIfNeeded($user); + $user = $this->sendActivationEmailIfNeeded($user); return $this->userDao->save($user); } @@ -109,11 +109,11 @@ class UserService $this->fileService->saveFromBase64($formData->avatarContent, $this->getCustomAvatarSourcePath($user)); } - if ($formData->userName !== null and $formData->userName != $user->name) + if ($formData->userName !== null and $formData->userName !== $user->name) { $this->validator->validateUserName($formData->userName); $userWithThisEmail = $this->userDao->getByName($formData->userName); - if ($userWithThisEmail and $userWithThisEmail->id != $user->id) + if ($userWithThisEmail and $userWithThisEmail->id !== $user->id) throw new \DomainException('User with this name already exists.'); $user->name = $formData->userName; @@ -125,13 +125,14 @@ class UserService $user->passwordHash = $this->passwordService->getHash($formData->password); } - if ($formData->email !== null and $formData->email != $user->email) + if ($formData->email !== null and $formData->email !== $user->email) { $this->validator->validateEmail($formData->email); if ($this->userDao->getByEmail($formData->email)) throw new \DomainException('User with this e-mail already exists.'); $user->emailUnconfirmed = $formData->email; + $user = $this->sendActivationEmailIfNeeded($user); } if ($formData->accessRank !== null) @@ -148,9 +149,6 @@ class UserService $user->browsingSettings = $formData->browsingSettings; } - if ($formData->email !== null) - $this->sendActivationEmailIfNeeded($user); - return $this->userDao->save($user); } @@ -185,7 +183,7 @@ class UserService public function finishPasswordReset($tokenName) { $token = $this->tokenService->getByName($tokenName); - if ($token->purpose != \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET) + if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET) throw new \Exception('This token is not a password reset token.'); $user = $this->getByName($token->additionalData); @@ -205,27 +203,29 @@ class UserService public function finishActivation($tokenName) { $token = $this->tokenService->getByName($tokenName); - if ($token->purpose != \Szurubooru\Entities\Token::PURPOSE_ACTIVATE) + if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_ACTIVATE) throw new \Exception('This token is not an activation token.'); $user = $this->getByName($token->additionalData); - $this->confirmEmail($user); + $user = $this->confirmEmail($user); + $this->userDao->save($user); $this->tokenService->invalidateByName($token->name); } - private function sendActivationEmailIfNeeded(\Szurubooru\Entities\User &$user) + private function sendActivationEmailIfNeeded(\Szurubooru\Entities\User $user) { - if ($user->accessRank == \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR or !$this->config->security->needEmailActivationToRegister) + if ($user->accessRank === \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR or !$this->config->security->needEmailActivationToRegister) { - $this->confirmEmail($user); + $user = $this->confirmEmail($user); } else { $this->sendActivationEmail($user); } + return $user; } - private function confirmEmail(\Szurubooru\Entities\User &$user) + private function confirmEmail(\Szurubooru\Entities\User $user) { //security issue: //1. two users set their unconfirmed mail to godzilla@empire.gov @@ -240,6 +240,6 @@ class UserService $user->email = $user->emailUnconfirmed; $user->emailUnconfirmed = null; - $this->userDao->save($user); + return $user; } } diff --git a/src/Validator.php b/src/Validator.php index dda0248c..194d2f70 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -10,8 +10,10 @@ class Validator $this->config = $config; } - public function validateNumber(&$subject) { - $subject = intval($subject); + public function validateNumber($subject) + { + if (!preg_match('/^-?[0-9]+$/', $subject)) + throw new \DomainException(subject . ' does not look like a number.'); } public function validateNonEmpty($subject, $subjectName = 'Object') @@ -38,10 +40,8 @@ class Validator throw new \DomainException($subjectName . ' must have at most ' . $maxLength . ' character(s).'); } - public function validateUserName(&$userName) + public function validateUserName($userName) { - $userName = trim($userName); - $minUserNameLength = intval($this->config->users->minUserNameLength); $maxUserNameLength = intval($this->config->users->maxUserNameLength); $this->validateNonEmpty($userName, 'User name'); diff --git a/src/di.php b/src/di.php index 9e830bf1..9ad5558c 100644 --- a/src/di.php +++ b/src/di.php @@ -1,5 +1,6 @@ DI\object()->constructor([ $dataDirectory . DIRECTORY_SEPARATOR . 'config.ini', @@ -9,11 +10,11 @@ return [ \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), - 'controllers' => DI\factory(function (DI\container $c) { + 'controllers' => DI\factory(function (DI\container $container) { return [ - $c->get(\Szurubooru\Controllers\AuthController::class), - $c->get(\Szurubooru\Controllers\UserController::class), - $c->get(\Szurubooru\Controllers\UserAvatarController::class), + $container->get(\Szurubooru\Controllers\AuthController::class), + $container->get(\Szurubooru\Controllers\UserController::class), + $container->get(\Szurubooru\Controllers\UserAvatarController::class), ]; }), ]; diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 684ac484..825cea82 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -62,16 +62,6 @@ final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase $validator->validateUserName($userName); } - public function testUserNameWithSpaces() - { - $this->configMock->set('users/minUserNameLength', 0); - $this->configMock->set('users/maxUserNameLength', 100); - $userName = ' godzilla '; - $validator = $this->getValidator(); - $validator->validateUserName($userName); - $this->assertEquals('godzilla', $userName); - } - public function testUserNameWithInvalidCharacters() { $this->configMock->set('users/minUserNameLength', 0);