Made message signing in Cryptography use SHA512 as the message content for... #1
3 changed files with 142 additions and 0 deletions
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\StandardMethods;
|
||||
|
||||
use Exception;
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Classes\Validator;
|
||||
use Socialbox\Enums\Flags\SessionFlags;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Exceptions\StandardException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\RegisteredPeerManager;
|
||||
use Socialbox\Managers\SessionManager;
|
||||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
|
||||
class SettingsSetBirthday extends Method
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
if(!$rpcRequest->containsParameter('email_address'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'email_address' parameter");
|
||||
}
|
||||
|
||||
if(!Validator::validateEmailAddress($rpcRequest->getParameter('email_address')))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'email_address' parameter, must be a valid email address");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Set the password
|
||||
RegisteredPeerManager::updateEmailAddress($request->getPeer(), $rpcRequest->getParameter('email_address'));
|
||||
|
||||
// Check & update the session flow
|
||||
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_EMAIL]);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
throw new StandardException('Failed to set email address due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
return $rpcRequest->produceResponse(true);
|
||||
}
|
||||
}
|
|
@ -55,4 +55,17 @@
|
|||
{
|
||||
return preg_match("/^\+[0-9]{1,15}$/", $phoneNumber) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates whether the given date is a valid gregorian calendar date.
|
||||
*
|
||||
* @param int $month The month component of the date (1 through 12).
|
||||
* @param int $day The day component of the date.
|
||||
* @param int $year The year component of the date.
|
||||
* @return bool Returns true if the provided date is valid, otherwise false.
|
||||
*/
|
||||
public static function validateDate(int $month, int $day, int $year): bool
|
||||
{
|
||||
return checkdate($month, $day, $year);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Socialbox\Managers;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use PDO;
|
||||
|
@ -670,4 +671,83 @@
|
|||
throw new DatabaseOperationException('Failed to delete the phone number of the peer in the database', $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the birthday of a registered peer record based on the provided date components.
|
||||
*
|
||||
* @param string|RegisteredPeerRecord $peer The unique identifier of the peer or an instance of RegisteredPeerRecord.
|
||||
* @param int $year The year component of the birthday.
|
||||
* @param int $month The month component of the birthday.
|
||||
* @param int $day The day component of the birthday.
|
||||
* @return void
|
||||
* @throws InvalidArgumentException If the peer is external or the provided date is invalid.
|
||||
* @throws DatabaseOperationException If there is an error during the database operation.
|
||||
*/
|
||||
public static function setBirthday(string|RegisteredPeerRecord $peer, int $year, int $month, int $day): void
|
||||
{
|
||||
if(is_string($peer))
|
||||
{
|
||||
$peer = self::getPeer($peer);
|
||||
}
|
||||
|
||||
if($peer->isExternal())
|
||||
{
|
||||
throw new InvalidArgumentException('Cannot set the birthday of an external peer');
|
||||
}
|
||||
|
||||
if(!Validator::validateDate($month, $day, $year))
|
||||
{
|
||||
throw new InvalidArgumentException('The provided date is not valid');
|
||||
}
|
||||
|
||||
Logger::getLogger()->verbose(sprintf("Setting birthday of peer %s to %d-%d-%d", $peer->getUuid(), $year, $month, $day));
|
||||
|
||||
try
|
||||
{
|
||||
$statement = Database::getConnection()->prepare('UPDATE `registered_peers` SET birthday=? WHERE uuid=?');
|
||||
$birthday = (new DateTime())->setDate($year, $month, $day)->format('Y-m-d');
|
||||
$statement->bindParam(1, $birthday);
|
||||
$uuid = $peer->getUuid();
|
||||
$statement->bindParam(2, $uuid);
|
||||
$statement->execute();
|
||||
}
|
||||
catch(PDOException $e)
|
||||
{
|
||||
throw new DatabaseOperationException('Failed to set the birthday of the peer in the database', $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the birthday of a registered peer based on the given unique identifier or RegisteredPeerRecord object.
|
||||
*
|
||||
* @param string|RegisteredPeerRecord $peer The unique identifier of the registered peer, or an instance of RegisteredPeerRecord.
|
||||
* @throws InvalidArgumentException If the peer is marked as external and cannot have its birthday deleted.
|
||||
* @throws DatabaseOperationException If there is an error during the database operation.
|
||||
*/
|
||||
public static function deleteBirthday(string|RegisteredPeerRecord $peer): void
|
||||
{
|
||||
if(is_string($peer))
|
||||
{
|
||||
$peer = self::getPeer($peer);
|
||||
}
|
||||
|
||||
if($peer->isExternal())
|
||||
{
|
||||
throw new InvalidArgumentException('Cannot delete the birthday of an external peer');
|
||||
}
|
||||
|
||||
Logger::getLogger()->verbose(sprintf("Deleting birthday of peer %s", $peer->getUuid()));
|
||||
|
||||
try
|
||||
{
|
||||
$statement = Database::getConnection()->prepare('UPDATE `registered_peers` SET birthday=NULL WHERE uuid=?');
|
||||
$uuid = $peer->getUuid();
|
||||
$statement->bindParam(1, $uuid);
|
||||
$statement->execute();
|
||||
}
|
||||
catch(PDOException $e)
|
||||
{
|
||||
throw new DatabaseOperationException('Failed to delete the birthday of the peer in the database', $e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue