2021-08-25 04:29:26 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-08-25 04:29:26 +09:00
|
|
|
/*
|
|
|
|
* This file is part of the ActivityPhp package.
|
|
|
|
*
|
|
|
|
* Copyright (c) landrok at github.com/landrok
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please see
|
|
|
|
* <https://github.com/landrok/activitypub/blob/master/LICENSE>.
|
|
|
|
*/
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
namespace Plugin\ActivityPub\Util\Type\Validator;
|
2021-08-25 04:29:26 +09:00
|
|
|
|
|
|
|
use Exception;
|
2021-10-05 01:00:58 +09:00
|
|
|
use Plugin\ActivityPub\Util\Type\Extended\AbstractActor;
|
|
|
|
use Plugin\ActivityPub\Util\Type\Util;
|
|
|
|
use Plugin\ActivityPub\Util\Type\ValidatorInterface;
|
2021-08-25 04:29:26 +09:00
|
|
|
|
|
|
|
/**
|
2021-10-05 01:00:58 +09:00
|
|
|
* \Plugin\ActivityPub\Util\Type\Validator\EndpointsValidator is a dedicated
|
2021-08-25 04:29:26 +09:00
|
|
|
* validator for endpoints attribute.
|
|
|
|
*/
|
|
|
|
class EndpointsValidator implements ValidatorInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Validate ENDPOINTS value
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function validate(mixed $value, mixed $container): bool
|
|
|
|
{
|
|
|
|
// Validate that container is an AbstractActor type
|
|
|
|
Util::subclassOf($container, AbstractActor::class, true);
|
|
|
|
|
|
|
|
// A link to a JSON-LD document
|
|
|
|
if (Util::validateUrl($value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A map
|
2021-10-10 17:26:18 +09:00
|
|
|
return \is_array($value) && $this->validateObject($value);
|
2021-08-25 04:29:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate endpoints mapping
|
|
|
|
*/
|
|
|
|
protected function validateObject(array $item): bool
|
|
|
|
{
|
|
|
|
foreach ($item as $key => $value) {
|
|
|
|
switch ($key) {
|
|
|
|
case 'proxyUrl':
|
|
|
|
case 'oauthAuthorizationEndpoint':
|
|
|
|
case 'oauthTokenEndpoint':
|
|
|
|
case 'provideClientKey':
|
|
|
|
case 'signClientKey':
|
|
|
|
case 'sharedInbox':
|
|
|
|
if (!Util::validateUrl($value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// All other keys are not allowed
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|