Skip to content

Commit

Permalink
Webservice: code conventions + use Request class - refs BT#19868
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Apr 6, 2022
1 parent 3e5ef89 commit 67c8349
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
10 changes: 5 additions & 5 deletions main/inc/lib/webservices/RestResponse.php
Expand Up @@ -47,10 +47,7 @@ public function setErrorMessage($message)
$this->errorMessage = $message;
}

/**
* @return string
*/
public function format()
public function format(): string
{
$json = ['error' => $this->error];

Expand All @@ -60,6 +57,9 @@ public function format()
$json['data'] = $this->data;
}

return json_encode($json, JSON_PRETTY_PRINT);
return json_encode(
$json,
'test' === api_get_setting('server_type') ? JSON_PRETTY_PRINT : 0
);
}
}
33 changes: 17 additions & 16 deletions main/webservices/api/v2.php
Expand Up @@ -34,11 +34,15 @@
}
}

$action = $httpRequest->get('action');
$username = Security::remove_XSS($httpRequest->get('username'));
$apiKey = Security::remove_XSS($httpRequest->get('api_key'));
$course = (int) $httpRequest->get('course', 0);
$session = (int) $httpRequest->get('session', 0);
$action = $httpRequest->query->get('action') ?: $httpRequest->request->get('action');
$username = Security::remove_XSS(
$httpRequest->query->get('username') ?: $httpRequest->request->get('username')
);
$apiKey = Security::remove_XSS(
$httpRequest->query->get('api_key') ?: $httpRequest->request->get('api_key')
);
$course = $httpRequest->query->getInt('course') ?: $httpRequest->request->getInt('course');
$session = $httpRequest->query->getInt('session') ?: $httpRequest->request->getInt('session');

$restResponse = new RestResponse();

Expand All @@ -63,7 +67,7 @@
case Rest::GET_AUTH:
Rest::init();

$password = isset($_POST['password']) ? $_POST['password'] : null;
$password = $_POST['password'] ?? null;
$isValid = Rest::isValidUser($username, $password);
if (!$isValid) {
throw new Exception(get_lang('InvalideUserDetected'));
Expand Down Expand Up @@ -91,8 +95,7 @@
$restResponse->setData($messages);
break;
case Rest::GET_USER_MESSAGES_RECEIVED:
$lastMessageId = isset($_POST['last']) ? (int) $_POST['last'] : 0;
$messages = $restApi->getUserReceivedMessages($lastMessageId);
$messages = $restApi->getUserReceivedMessages();
$restResponse->setData($messages);
break;
case Rest::DELETE_USER_MESSAGE:
Expand All @@ -102,8 +105,7 @@
$restResponse->setData(['status' => true]);
break;
case Rest::GET_USER_MESSAGES_SENT:
$lastMessageId = isset($_POST['last']) ? (int) $_POST['last'] : 0;
$messages = $restApi->getUserSentMessages($lastMessageId);
$messages = $restApi->getUserSentMessages();
$restResponse->setData($messages);
break;
case Rest::GET_COUNT_NEW_MESSAGES:
Expand Down Expand Up @@ -141,7 +143,7 @@
$restResponse->setData($data);
break;
case Rest::SAVE_USER_MESSAGE:
$receivers = isset($_POST['receivers']) ? $_POST['receivers'] : [];
$receivers = $_POST['receivers'] ?? [];
$subject = !empty($_POST['subject']) ? $_POST['subject'] : null;
$text = !empty($_POST['text']) ? $_POST['text'] : null;
$data = $restApi->saveUserMessage($subject, $text, $receivers);
Expand Down Expand Up @@ -266,9 +268,9 @@
throw new Exception(get_lang('NoData'));
}

$forumId = isset($_POST['forum']) ? (int) $_POST['forum'] : 0;
$notify = !empty($_POST['notify']);
$parentId = !empty($_POST['parent']) ? (int) $_POST['parent'] : null;
$forumId = $httpRequest->request->getInt('forum');
$notify = $httpRequest->request->has('notify');
$parentId = $httpRequest->request->getInt('parent') ?: null;

$postValues = [
'post_title' => $_POST['title'],
Expand All @@ -287,7 +289,7 @@
throw new Exception(get_lang('NoData'));
}

$forumId = isset($_POST['forum']) ? (int) $_POST['forum'] : 0;
$forumId = $httpRequest->request->getInt('forum');
$notify = !empty($_POST['notify']);

$threadInfo = [
Expand Down Expand Up @@ -632,7 +634,6 @@

echo json_encode($data, JSON_PRETTY_PRINT);
exit;
break;

case Rest::UPDATE_USER_PAUSE_TRAINING:
$allow = api_get_plugin_setting('pausetraining', 'tool_enable') === 'true';
Expand Down

0 comments on commit 67c8349

Please sign in to comment.