delegate data validation to the Valid library

This commit is contained in:
ghost 2023-09-15 20:41:13 +03:00
parent cb5cf02924
commit 8b771bbb4f

View file

@ -87,11 +87,7 @@ try
foreach (@json_decode(@file_get_contents($manifest->export->users)) as $remoteUser) foreach (@json_decode(@file_get_contents($manifest->export->users)) as $remoteUser)
{ {
// Validate required fields // Validate required fields
if (!isset($remoteUser->userId) || !is_int($remoteUser->userId) || if (!Valid::user($remoteUser))
!isset($remoteUser->address) || !is_string($remoteUser->address) ||
!isset($remoteUser->timeAdded) || !is_int($remoteUser->timeAdded) ||
!isset($remoteUser->timeUpdated) || !is_int($remoteUser->timeUpdated) ||
!isset($remoteUser->approved) || !is_bool($remoteUser->approved))
{ {
continue; continue;
} }
@ -102,12 +98,6 @@ try
continue; continue;
} }
// Yggdrasil connections only
else if (!preg_match(YGGDRASIL_HOST_REGEX, $remoteUser->address))
{
continue;
}
// Init session // Init session
else if (!$localUser = $db->getUser( else if (!$localUser = $db->getUser(
$db->initUserId($remoteUser->address, $db->initUserId($remoteUser->address,
@ -174,32 +164,13 @@ try
foreach (@json_decode(@file_get_contents($manifest->export->magnets)) as $remoteMagnet) foreach (@json_decode(@file_get_contents($manifest->export->magnets)) as $remoteMagnet)
{ {
// Validate required fields by protocol // Validate required fields by protocol
if (!isset($remoteMagnet->magnetId) || !is_int($remoteMagnet->magnetId) || if (!Valid::magnet($remoteMagnet))
!isset($remoteMagnet->userId) || !is_int($remoteMagnet->userId) || {
!isset($aliasUserId[$remoteMagnet->userId]) || continue;
!$db->getUser($aliasUserId[$remoteMagnet->userId]) || }
!isset($remoteMagnet->title) || !is_string($remoteMagnet->title) || // Aliases check
!isset($remoteMagnet->preview) || !is_string($remoteMagnet->preview) || if (!isset($aliasUserId[$remoteMagnet->userId]))
!isset($remoteMagnet->description) || !is_string($remoteMagnet->description) ||
!isset($remoteMagnet->comments) || !is_bool($remoteMagnet->comments) ||
!isset($remoteMagnet->sensitive) || !is_bool($remoteMagnet->sensitive) ||
!isset($remoteMagnet->approved) || !is_bool($remoteMagnet->approved) ||
!isset($remoteMagnet->timeAdded) || !is_int($remoteMagnet->timeAdded) ||
!isset($remoteMagnet->timeUpdated) || !is_int($remoteMagnet->timeUpdated) ||
!isset($remoteMagnet->dn) || mb_strlen($remoteMagnet->dn) < MAGNET_TITLE_MIN_LENGTH ||
mb_strlen($remoteMagnet->dn) > MAGNET_TITLE_MAX_LENGTH ||
!isset($remoteMagnet->xl) || !(is_int($remoteMagnet->xl) || is_float($remoteMagnet->xl)) ||
!isset($remoteMagnet->xt) || !is_object($remoteMagnet->xt) ||
!isset($remoteMagnet->kt) || !is_object($remoteMagnet->kt) ||
!isset($remoteMagnet->tr) || !is_object($remoteMagnet->tr) ||
!isset($remoteMagnet->as) || !is_object($remoteMagnet->as) ||
!isset($remoteMagnet->xs) || !is_object($remoteMagnet->xs))
{ {
continue; continue;
} }
@ -235,7 +206,7 @@ try
if ($localMagnet->timeAdded < $remoteMagnet->timeAdded) if ($localMagnet->timeAdded < $remoteMagnet->timeAdded)
{ {
$db->updateMagnetTimeAdded( $db->updateMagnetTimeAdded(
$localUser->userId, $localMagnet->magnetId,
$remoteMagnet->timeAdded $remoteMagnet->timeAdded
); );
} }
@ -246,25 +217,9 @@ try
// Magnet fields // Magnet fields
$db->updateMagnetXl($localMagnet->magnetId, $remoteMagnet->xl, $remoteMagnet->timeUpdated); $db->updateMagnetXl($localMagnet->magnetId, $remoteMagnet->xl, $remoteMagnet->timeUpdated);
$db->updateMagnetDn($localMagnet->magnetId, $remoteMagnet->dn, $remoteMagnet->timeUpdated); $db->updateMagnetDn($localMagnet->magnetId, $remoteMagnet->dn, $remoteMagnet->timeUpdated);
if (mb_strlen($remoteMagnet->title) >= MAGNET_TITLE_MIN_LENGTH &&
mb_strlen($remoteMagnet->title) <= MAGNET_TITLE_MAX_LENGTH)
{
$db->updateMagnetTitle($localMagnet->magnetId, $remoteMagnet->title, $remoteMagnet->timeUpdated); $db->updateMagnetTitle($localMagnet->magnetId, $remoteMagnet->title, $remoteMagnet->timeUpdated);
}
if (mb_strlen($remoteMagnet->preview) >= MAGNET_PREVIEW_MIN_LENGTH &&
mb_strlen($remoteMagnet->preview) <= MAGNET_PREVIEW_MAX_LENGTH)
{
$db->updateMagnetPreview($localMagnet->magnetId, $remoteMagnet->preview, $remoteMagnet->timeUpdated); $db->updateMagnetPreview($localMagnet->magnetId, $remoteMagnet->preview, $remoteMagnet->timeUpdated);
}
if (mb_strlen($remoteMagnet->description) >= MAGNET_DESCRIPTION_MIN_LENGTH &&
mb_strlen($remoteMagnet->description) <= MAGNET_DESCRIPTION_MAX_LENGTH)
{
$db->updateMagnetDescription($localMagnet->magnetId, $remoteMagnet->description, $remoteMagnet->timeUpdated); $db->updateMagnetDescription($localMagnet->magnetId, $remoteMagnet->description, $remoteMagnet->timeUpdated);
}
$db->updateMagnetComments($localMagnet->magnetId, $remoteMagnet->comments, $remoteMagnet->timeUpdated); $db->updateMagnetComments($localMagnet->magnetId, $remoteMagnet->comments, $remoteMagnet->timeUpdated);
$db->updateMagnetSensitive($localMagnet->magnetId, $remoteMagnet->sensitive, $remoteMagnet->timeUpdated); $db->updateMagnetSensitive($localMagnet->magnetId, $remoteMagnet->sensitive, $remoteMagnet->timeUpdated);
@ -351,10 +306,6 @@ try
// tr // tr
foreach ($remoteMagnet->tr as $tr) foreach ($remoteMagnet->tr as $tr)
{
if ($url = Yggverse\Parser\Url::parse($tr))
{
if (preg_match(YGGDRASIL_HOST_REGEX, str_replace(['[',']'], false, $url->host->name)))
{ {
$db->initMagnetToAddressTrackerId( $db->initMagnetToAddressTrackerId(
$localMagnet->magnetId, $localMagnet->magnetId,
@ -366,15 +317,9 @@ try
) )
); );
} }
}
}
// as // as
foreach ($remoteMagnet->as as $as) foreach ($remoteMagnet->as as $as)
{
if ($url = Yggverse\Parser\Url::parse($as))
{
if (preg_match(YGGDRASIL_HOST_REGEX, str_replace(['[',']'], false, $url->host->name)))
{ {
$db->initMagnetToAcceptableSourceId( $db->initMagnetToAcceptableSourceId(
$localMagnet->magnetId, $localMagnet->magnetId,
@ -386,15 +331,9 @@ try
) )
); );
} }
}
}
// xs // xs
foreach ($remoteMagnet->xs as $xs) foreach ($remoteMagnet->xs as $xs)
{
if ($url = Yggverse\Parser\Url::parse($xs))
{
if (preg_match(YGGDRASIL_HOST_REGEX, str_replace(['[',']'], false, $url->host->name)))
{ {
$db->initMagnetToExactSourceId( $db->initMagnetToExactSourceId(
$localMagnet->magnetId, $localMagnet->magnetId,
@ -408,8 +347,6 @@ try
} }
} }
} }
}
}
// Magnet comments // Magnet comments
if (API_IMPORT_MAGNET_COMMENTS_ENABLED) if (API_IMPORT_MAGNET_COMMENTS_ENABLED)
@ -422,15 +359,13 @@ try
foreach (@json_decode(@file_get_contents($manifest->export->magnetComments)) as $remoteMagnetComment) foreach (@json_decode(@file_get_contents($manifest->export->magnetComments)) as $remoteMagnetComment)
{ {
// Validate // Validate
if ( if (!Valid::magnetComment($remoteMagnetComment))
empty($remoteMagnetComment->magnetId) || !is_int($remoteMagnetComment->magnetId) || !isset($aliasMagnetId[$remoteMagnetComment->magnetId]) || {
empty($remoteMagnetComment->userId) || !is_int($remoteMagnetComment->userId) || !isset($aliasUserId[$remoteMagnetComment->userId]) || continue;
empty($remoteMagnetComment->timeAdded) || !is_int($remoteMagnetComment->timeAdded) || }
empty($remoteMagnetComment->approved) || !is_bool($remoteMagnetComment->approved) ||
!isset($remoteMagnetComment->value) || !is_string($remoteMagnetComment->value) || mb_strlen($remoteMagnetComment->value) < MAGNET_COMMENT_MIN_LENGTH || mb_strlen($remoteMagnetComment->value) > MAGNET_COMMENT_MAX_LENGTH ||
!isset($remoteMagnetComment->magnetCommentIdParent) || !(is_bool($remoteMagnetComment->magnetCommentIdParent) || is_int($remoteMagnetComment->magnetCommentIdParent)) // Aliases check
) if (!isset($aliasMagnetId[$remoteMagnetComment->magnetId]) || !isset($aliasUserId[$remoteMagnetComment->userId]))
{ {
continue; continue;
} }
@ -481,11 +416,7 @@ try
foreach (@json_decode(@file_get_contents($manifest->export->magnetDownloads)) as $remoteMagnetDownload) foreach (@json_decode(@file_get_contents($manifest->export->magnetDownloads)) as $remoteMagnetDownload)
{ {
// Validate // Validate
if ( if (!Valid::magnetDownload($remoteMagnetDownload))
empty($remoteMagnetDownload->magnetId) || !is_int($remoteMagnetDownload->magnetId) || !isset($aliasMagnetId[$remoteMagnetDownload->magnetId]) ||
empty($remoteMagnetDownload->userId) || !is_int($remoteMagnetDownload->userId) || !isset($aliasUserId[$remoteMagnetDownload->userId]) ||
empty($remoteMagnetDownload->timeAdded) || !is_int($remoteMagnetDownload->timeAdded)
)
{ {
continue; continue;
} }
@ -515,11 +446,7 @@ try
foreach (@json_decode(@file_get_contents($manifest->export->magnetViews)) as $remoteMagnetView) foreach (@json_decode(@file_get_contents($manifest->export->magnetViews)) as $remoteMagnetView)
{ {
// Validate // Validate
if ( if (!Valid::magnetView($remoteMagnetView))
empty($remoteMagnetView->magnetId) || !is_int($remoteMagnetView->magnetId) || !isset($aliasMagnetId[$remoteMagnetView->magnetId]) ||
empty($remoteMagnetView->userId) || !is_int($remoteMagnetView->userId) || !isset($aliasUserId[$remoteMagnetView->userId]) ||
empty($remoteMagnetView->timeAdded) || !is_int($remoteMagnetView->timeAdded)
)
{ {
continue; continue;
} }
@ -549,12 +476,7 @@ try
foreach (@json_decode(@file_get_contents($manifest->export->magnetStars)) as $remoteMagnetStar) foreach (@json_decode(@file_get_contents($manifest->export->magnetStars)) as $remoteMagnetStar)
{ {
// Validate // Validate
if ( if (!Valid::magnetStar($remoteMagnetStar))
empty($remoteMagnetStar->magnetId) || !is_int($remoteMagnetStar->magnetId) || !isset($aliasMagnetId[$remoteMagnetStar->magnetId]) ||
empty($remoteMagnetStar->userId) || !is_int($remoteMagnetStar->userId) || !isset($aliasUserId[$remoteMagnetStar->userId]) ||
empty($remoteMagnetStar->timeAdded) || !is_int($remoteMagnetStar->timeAdded) ||
!isset($remoteMagnetStar->value) || !is_bool($remoteMagnetStar->value)
)
{ {
continue; continue;
} }