add new comment endpoint

This commit is contained in:
Jonas Leder 2022-03-08 15:10:52 +01:00
parent 92ce267baf
commit e305c030e6
No known key found for this signature in database
GPG key ID: 8A53DD45A7D7B44B
2 changed files with 44 additions and 0 deletions

View file

@ -2,6 +2,7 @@
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
use GuzzleHttp\Client;
include "lib/getGravatar.php";
$commentField = new ObjectType([
@ -29,4 +30,36 @@ function comments($article, $conn) {
}
return $response;
}
function newComment($conn, $article, $name, $email, $comment, $hCaptchaResponse) {
require "./lib/config.php";
$data = array(
'secret' => $secretkey,
'response' => $hCaptchaResponse
);
$client = new Client();
$response = $client->post("https://hcaptcha.com/siteverify", [
"form_params" => $data
]);
$responseData = json_decode($response->getBody());
if(! $responseData->success) {
return "Failed to verify Captcha";
$article = $conn->escape_string($article);
$name = $conn->escape_string($name);
$email = $conn->escape_string($email);
$comment = $conn->escape_string($comment);
$sql = "INSERT INTO comments (name, email, comment, article) VALUES ('$name', '$email', '$comment', '$article')";
if ($conn->query($sql) === TRUE) {
return "OK";
} else {
return "Error: " . $sql . "<br>" . $conn->error;
}
}
}

View file

@ -53,6 +53,17 @@ $queryType = new ObjectType([
],
'resolve' => fn ($rootValue, $args) => comments($args["article"], $rootValue["db"]),
],
"newComment" => [
"type" => Type::string(),
"args" => [
"article" => Type::string(),
"name" => Type::string(),
"email" => Type::string(),
"comment" => Type::string(),
"hCaptchaResponse" => Type::string()
],
'resolve' => fn ($rootValue, $args) => newComment($rootValue["db"], $args["article"], $args["name"], $args["email"], $args["comment"], $args["hCaptchaResponse"]),
],
'ebayKleinanzeigen' => [
"type" => $ebayKleinanzeigenFields,
"args" => [