This commit is contained in:
Netkas 2023-06-21 02:09:13 -04:00
parent 0a8f7589b9
commit 1728d607d7
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
16 changed files with 71 additions and 152 deletions

View file

@ -99,4 +99,23 @@
}
return false;
}
/**
* Same as gettype() but returns only what the user should see.
*
* @param mixed $value
* @return string
*/
public static function gettype(mixed $value): string
{
return match (strtolower(gettype($value)))
{
'boolean' => 'boolean',
'integer' => 'integer',
'double' => 'float',
'string' => 'string',
'array' => 'array',
default => 'null',
};
}
}

View file

@ -4,12 +4,8 @@
namespace FederationLib\Classes;
use Exception;
use FederationLib\Enums\SerializationMethod;
use FederationLib\Interfaces\PeerMetadataInterface;
use FederationLib\Interfaces\SerializableObjectInterface;
use InvalidArgumentException;
use LogLib\Log;
use Throwable;
class Utilities
@ -91,34 +87,6 @@
throw new InvalidArgumentException(sprintf('Invalid address provided: %s', $address));
}
/**
* Serializes an array into a string.
*
* @param array $data
* @param string $method
* @return string
*/
public static function serialize(array|SerializableObjectInterface $data, string $method): string
{
if($data instanceof SerializableObjectInterface)
{
$data = $data->toArray();
}
switch(strtolower($method))
{
case SerializationMethod::JSON:
return json_encode($data);
case SerializationMethod::MSGPACK:
return msgpack_pack($data);
default:
Log::warning('net.nosial.federationlib', sprintf('Unknown serialization method: %s, defaulting to msgpack', $method));
return msgpack_pack($data);
}
}
/**
* Recursively converts a Throwable into an array representation.
*

View file

@ -7,6 +7,7 @@
use FederationLib\Enums\Standard\PeerAssociationType;
use FederationLib\Enums\Standard\PermissionRole;
use FederationLib\Enums\Standard\UserPeerType;
use FederationLib\Exceptions\Standard\InvalidPeerMetadataException;
class Validate
{
@ -102,4 +103,37 @@
return (int)$role >= 0 && (int)$role <= 5;
}
/**
* Validates the given metadata for a peer.
*
* @param array $data
* @param array $required
* @param array $optional
* @return void
* @throws InvalidPeerMetadataException
*/
public static function validateMetadata(array $data, array $required, array $optional): void
{
foreach ($required as $property => $type)
{
if (!isset($data[$property]))
{
throw new InvalidPeerMetadataException(sprintf('The property "%s" is required in the metadata', $property));
}
if (gettype($data[$property]) !== $type)
{
throw new InvalidPeerMetadataException(sprintf('The property "%s" must be a %s in metadata, got %s', $property, $type, Security::gettype($data[$property])));
}
}
foreach ($optional as $property => $type)
{
if (isset($data[$property]) && gettype($data[$property]) !== $type)
{
throw new InvalidPeerMetadataException(sprintf('The property "%s" must be a %s in metadata, got %s', $property, $type, Security::gettype($data[$property])));
}
}
}
}

View file

@ -1,9 +0,0 @@
<?php
namespace FederationLib\Enums;
final class CacheDriver
{
public const MEMCACHED = 'memcached';
public const REDIS = 'redis';
}

View file

@ -1,22 +0,0 @@
<?php
namespace FederationLib\Enums;
use FederationLib\Classes\CommandApplets\DateCommand;
use FederationLib\Classes\CommandApplets\HostnameCommand;
use FederationLib\Classes\CommandApplets\WhoamiCommand;
final class CommandApplets
{
const WHOAMI = [WhoamiCommand::class, 'whoami'];
const HOSTNAME = [HostnameCommand::class, 'hostname'];
const DATE = [DateCommand::class, 'date'];
const ALL = [
self::WHOAMI,
self::HOSTNAME,
self::DATE
];
}

View file

@ -1,9 +0,0 @@
<?php
namespace FederationLib\Enums;
final class SerializationMethod
{
public const JSON = 'json';
public const MSGPACK = 'msgpack';
}

View file

@ -1,12 +0,0 @@
<?php
namespace FederationLib\Enums\Standard;
final class AttachmentDataType
{
public const RAW = 'raw';
public const BASE64 = 'base64';
public const URL = 'url';
}

View file

@ -1,15 +0,0 @@
<?php
namespace FederationLib\Enums\Standard;
final class ContentType
{
public const NULL = 'null';
public const TEXT = 'text/plain';
public const HTML = 'text/html';
public const JSON = 'application/json';
public const XML = 'application/xml';
public const EMAIL = 'message/rfc822';
public const BASE64 = 'application/base64';
public const HTTP = 'application/http';
}

View file

@ -1,12 +0,0 @@
<?php
namespace FederationLib\Enums\Standard;
final class DocumentSubjectType
{
public const RECON = 'RECON';
public const ALL = [
self::RECON
];
}

View file

@ -6,10 +6,7 @@
{
public const TELEGRAM_CHAT = 'telegram.chat';
public const TELEGRAM_CHANNEL = 'telegram.channel';
public const ALL = [
self::TELEGRAM_CHAT,
self::TELEGRAM_CHANNEL,
];
}

View file

@ -4,13 +4,13 @@
final class PeerType
{
const USER = 'user';
public const USER = 'user';
const INTERNET = 'internet';
public const INTERNET = 'internet';
const UNKNOWN = 'unknown';
public const UNKNOWN = 'unknown';
const ALL = [
public const ALL = [
self::USER,
self::INTERNET,
self::UNKNOWN

View file

@ -1,12 +0,0 @@
<?php
namespace FederationLib\Enums\Standard;
final class ScanMode
{
/**
* Means that the scanner should only scan the header of the document.
*/
public const DOCUMENT = 'document';
}

View file

@ -1,8 +0,0 @@
<?php
namespace FederationLib\Enums;
final class UserEntityRelationType
{
public const OWNER = 'owner';
}

View file

@ -1,5 +1,5 @@
<?php
//TODO: This is more of a internal error
namespace FederationLib\Exceptions\Standard;
use Exception;

View file

@ -91,7 +91,7 @@
try
{
$this->updateLastSeen($client_uuid, $this->getPeer($federated_address));
$this->updateLastSeen($client_uuid, $federated_address);
}
catch(PeerNotFoundException $e)
{
@ -357,9 +357,9 @@
$client_uuid = $client_uuid->getUuid();
}
if($federated_address instanceof ParsedFederatedAddress)
if(!($federated_address instanceof ParsedFederatedAddress))
{
$federated_address = $federated_address->getAddress();
$federated_address = new ParsedFederatedAddress($federated_address);
}
$qb = Database::getConnection()->createQueryBuilder();

View file

@ -140,6 +140,16 @@
return sprintf('%s:%s', UserPeerType::TELEGRAM_USER, $this->id);
}
/**
* Returns the Client UUID that last updated the record
*
* @return ?string
*/
public function getUpdatedClient(): ?string
{
return $this->updated_client;
}
/**
* Sets the client UUID that last updated the record
*
@ -172,16 +182,6 @@
$this->updated_timestamp = $timestamp;
}
/**
* Returns the Client UUID that last updated the record
*
* @return ?string
*/
public function getUpdatedClient(): ?string
{
return $this->updated_client;
}
/**
* Unique identifier for this user or bot. This number may have more than 32 significant bits and some
* programming languages may have difficulty/silent defects in interpreting it. But it has at most 52