Skip to content

Commit

Permalink
GH-126 Move ignore system checks to a separate validator function
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Sep 5, 2020
1 parent 7df4fdb commit 2f9f4e3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 22 deletions.
44 changes: 22 additions & 22 deletions messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,29 +280,29 @@
}
}

if($FormData['uid'] > 0 && !CheckAuth('user', AUTHCHECK_HIGHER) AND !CheckAuth('user', AUTHCHECK_HIGHER, $FormData))
{
$Query_IgnoreSystem = '';
$Query_IgnoreSystem .= "SELECT `OwnerID` FROM {{table}} WHERE ";
$Query_IgnoreSystem .= "(`OwnerID` = {$_User['id']} AND `IgnoredID` = {$FormData['uid']}) OR ";
$Query_IgnoreSystem .= "(`OwnerID` = {$FormData['uid']} AND `IgnoredID` = {$_User['id']}) ";
$Query_IgnoreSystem .= "LIMIT 2; -- messages.php|IgnoreSystem";

$Result_IgnoreSystem = doquery($Query_IgnoreSystem, 'ignoresystem');

if($Result_IgnoreSystem->num_rows > 0)
{
if ($FormData['uid'] > 0) {
$ignoreSystemValidationResult = Messages\Validators\validateWithIgnoreSystem([
'senderUser' => &$_User,
'recipientUser' => [
'id' => $FormData['uid'],
'authlevel' => $FormData['authlevel'],
],
]);

if (!$ignoreSystemValidationResult['isValid']) {
$AllowSend = false;
while($IgnoreData = $Result_IgnoreSystem->fetch_assoc())
{
if($IgnoreData['OwnerID'] == $_User['id'])
{
$MsgBox[] = array('color' => 'red', 'text' => $_Lang['Errors_IgnoreYour']);
}
else
{
$MsgBox[] = array('color' => 'red', 'text' => $_Lang['Errors_IgnoreHis']);
}

if ($ignoreSystemValidationResult['errors']['isRecipientIgnored']) {
$MsgBox[] = [
'color' => 'red',
'text' => $_Lang['Errors_IgnoreYour'],
];
}
if ($ignoreSystemValidationResult['errors']['isSenderIgnored']) {
$MsgBox[] = [
'color' => 'red',
'text' => $_Lang['Errors_IgnoreHis'],
];
}
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/messages/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
include($includePath . './input/batchActions.userCommands.php');
include($includePath . './utils/batchDeleteMessages.utils.php');
include($includePath . './utils/batchMessageUpdates.utils.php');
include($includePath . './validators/validateWithIgnoreSystem.validators.php');

});

Expand Down
5 changes: 5 additions & 0 deletions modules/messages/validators/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace UniEngine\Engine\Modules\Messages\Validators;

/**
* @param array $params
* @param &array $params['senderUser']
* @param &array $params['recipientUser']
*/
function validateWithIgnoreSystem($params) {
$isValid = function () {
return [
'isValid' => true,
];
};
$isInvalid = function ($errors) {
return [
'isValid' => false,
'errors' => $errors,
];
};

$senderUser = &$params['senderUser'];
$recipientUser = &$params['recipientUser'];

if (
CheckAuth('user', AUTHCHECK_HIGHER, $senderUser) ||
CheckAuth('user', AUTHCHECK_HIGHER, $recipientUser)
) {
return $isValid();
}

$Query_IgnoreSystem = '';
$Query_IgnoreSystem .= "SELECT `OwnerID` FROM {{table}} WHERE ";
$Query_IgnoreSystem .= "(`OwnerID` = {$senderUser['id']} AND `IgnoredID` = {$recipientUser['id']}) OR ";
$Query_IgnoreSystem .= "(`OwnerID` = {$recipientUser['id']} AND `IgnoredID` = {$senderUser['id']}) ";
$Query_IgnoreSystem .= "LIMIT 2; -- messages.php|IgnoreSystem";

$Result_IgnoreSystem = doquery($Query_IgnoreSystem, 'ignoresystem');

if ($Result_IgnoreSystem->num_rows == 0) {
return $isValid();
}

$validationErrors = [
'isRecipientIgnored' => false,
'isSenderIgnored' => false,
];

while ($IgnoreData = $Result_IgnoreSystem->fetch_assoc()) {
if ($IgnoreData['OwnerID'] == $senderUser['id']) {
$validationErrors['isRecipientIgnored'] = true;
}
if ($IgnoreData['OwnerID'] == $recipientUser['id']) {
$validationErrors['isSenderIgnored'] = true;
}
}

return $isInvalid($validationErrors);
}

?>

0 comments on commit 2f9f4e3

Please sign in to comment.