Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions wcfsetup/install/files/lib/http/Helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use wcf\data\DatabaseObject;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\ParentClassException;
use wcf\system\exception\UserInputException;
use wcf\util\StringUtil;
Expand Down Expand Up @@ -197,6 +198,48 @@ public static function fetchObjectFromRequestParameter(int|string $objectID, str
return $dbo;
}

/**
* Fetches a database object using the `id` parameter from GET parameters.
*
* If the value does not resolve to an object, i.e. its object id is not
* truthy, a IllegalLinkException is thrown.
*
* @template T of object
* @param class-string<T> $className
* @return T
* @throws IllegalLinkException
* @throws ParentClassException
* @since 6.3
*/
public static function fetchObjectFromQueryParameter(string $className): object
{
if (!\is_subclass_of($className, DatabaseObject::class)) {
throw new ParentClassException($className, DatabaseObject::class);
}

try {
$queryParameters = self::mapQueryParameters(
$_GET,
<<<'EOT'
array {
id: positive-int
}
EOT
);
} catch (MappingError) {
throw new IllegalLinkException();
}

/** @var DatabaseObject $dbo */
$dbo = new $className($queryParameters['id']);

if (!$dbo->getObjectID()) {
throw new IllegalLinkException();
}

return $dbo;
}

/**
* Forbid creation of Helper objects.
*/
Expand Down