web-base/Core/API/ContactAPI.class.php

291 lines
8.8 KiB
PHP
Raw Normal View History

2020-06-26 23:32:45 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2021-04-10 00:44:34 +02:00
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2021-04-10 00:44:34 +02:00
2020-07-01 21:10:25 +02:00
abstract class ContactAPI extends Request {
2020-06-26 23:32:45 +02:00
2021-04-10 00:44:34 +02:00
protected ?string $messageId;
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall, array $params) {
parent::__construct($context, $externalCall, $params);
2021-04-10 00:44:34 +02:00
$this->messageId = null;
$this->csrfTokenRequired = false;
2021-04-10 00:44:34 +02:00
}
protected function sendMail(string $name, ?string $fromEmail, string $subject, string $message, ?string $to = null): bool {
2022-11-18 18:06:46 +01:00
$request = new \Core\API\Mail\Send($this->context);
2021-04-10 00:44:34 +02:00
$this->success = $request->execute(array(
"subject" => $subject,
"body" => $message,
"replyTo" => $fromEmail,
"replyName" => $name,
"to" => $to
));
$this->lastError = $request->getLastError();
2021-04-10 00:44:34 +02:00
if ($this->success) {
$this->messageId = $request->getResult()["messageId"];
}
return $this->success;
}
2020-06-26 23:32:45 +02:00
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Contact {
2020-06-26 23:32:45 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\ContactAPI;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\API\VerifyCaptcha;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondNot;
use Core\Driver\SQL\Expression\CaseWhen;
use Core\Driver\SQL\Expression\Sum;
use Core\Objects\Context;
2020-06-26 23:32:45 +02:00
class Request extends ContactAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
2020-06-26 23:32:45 +02:00
$parameters = array(
'fromName' => new StringType('fromName', 32),
'fromEmail' => new Parameter('fromEmail', Parameter::TYPE_EMAIL),
'message' => new StringType('message', 512),
);
2022-06-20 19:52:31 +02:00
$settings = $context->getSettings();
2020-06-26 23:32:45 +02:00
if ($settings->isRecaptchaEnabled()) {
$parameters["captcha"] = new StringType("captcha");
}
2022-06-20 19:52:31 +02:00
parent::__construct($context, $externalCall, $parameters);
2020-06-26 23:32:45 +02:00
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$settings = $this->context->getSettings();
2020-06-26 23:32:45 +02:00
if ($settings->isRecaptchaEnabled()) {
$captcha = $this->getParam("captcha");
2022-06-20 19:52:31 +02:00
$req = new VerifyCaptcha($this->context);
2020-06-26 23:32:45 +02:00
if (!$req->execute(array("captcha" => $captcha, "action" => "contact"))) {
return $this->createError($req->getLastError());
}
}
2021-04-10 00:44:34 +02:00
// parameter
$message = $this->getParam("message");
$name = $this->getParam("fromName");
$email = $this->getParam("fromEmail");
$sendMail = $this->sendMail($name, $email, "Contact Request", $message);
2021-04-09 16:05:36 +02:00
$insertDB = $this->insertContactRequest();
if (!$sendMail && !$insertDB) {
return $this->createError("The contact request could not be sent. The Administrator is already informed. Please try again later.");
2020-06-26 23:32:45 +02:00
}
return $this->success;
}
2021-04-10 00:44:34 +02:00
private function insertContactRequest(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-26 23:32:45 +02:00
$name = $this->getParam("fromName");
$email = $this->getParam("fromEmail");
$message = $this->getParam("message");
2021-04-09 16:05:36 +02:00
$messageId = $this->messageId ?? null;
2020-06-26 23:32:45 +02:00
2021-04-09 16:05:36 +02:00
$res = $sql->insert("ContactRequest", array("from_name", "from_email", "message", "messageId"))
->addRow($name, $email, $message, $messageId)
2022-06-20 19:52:31 +02:00
->returning("id")
2020-06-26 23:32:45 +02:00
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
2021-04-10 00:44:34 +02:00
return $this->success;
}
}
class Respond extends ContactAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
2021-04-10 00:44:34 +02:00
"requestId" => new Parameter("requestId", Parameter::TYPE_INT),
'message' => new StringType('message', 512),
));
$this->loginRequired = true;
}
private function getSenderMail(): ?string {
$requestId = $this->getParam("requestId");
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-10 00:44:34 +02:00
$res = $sql->select("from_email")
->from("ContactRequest")
2022-06-20 19:52:31 +02:00
->where(new Compare("id", $requestId))
2021-04-10 00:44:34 +02:00
->execute();
$this->success = ($res !== false);
$this->lastError = $sql->getLastError();
2020-06-26 23:32:45 +02:00
if ($this->success) {
2021-04-10 00:44:34 +02:00
if (empty($res)) {
return $this->createError("Request does not exist");
} else {
return $res[0]["from_email"];
}
2020-06-26 23:32:45 +02:00
}
2021-04-10 00:44:34 +02:00
return null;
2020-06-26 23:32:45 +02:00
}
2021-04-10 00:44:34 +02:00
private function insertResponseMessage(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-26 23:32:45 +02:00
$message = $this->getParam("message");
2021-04-10 00:44:34 +02:00
$requestId = $this->getParam("requestId");
2020-06-26 23:32:45 +02:00
2021-04-10 00:44:34 +02:00
$this->success = $sql->insert("ContactMessage", ["request_id", "user_id", "message", "messageId", "read"])
2022-06-20 19:52:31 +02:00
->addRow($requestId, $this->context->getUser()->getId(), $message, $this->messageId, true)
2020-06-26 23:32:45 +02:00
->execute();
$this->lastError = $sql->getLastError();
2021-04-10 00:44:34 +02:00
return $this->success;
}
2020-06-26 23:32:45 +02:00
2021-04-10 00:44:34 +02:00
private function updateEntity() {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-10 00:44:34 +02:00
$requestId = $this->getParam("requestId");
$sql->update("EntityLog")
->set("modified", $sql->now())
->where(new Compare("entityId", $requestId))
->execute();
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-10 00:44:34 +02:00
$message = $this->getParam("message");
$senderMail = $this->getSenderMail();
if (!$this->success) {
return false;
}
2022-06-20 19:52:31 +02:00
$user = $this->context->getUser();
$fromName = $user->getUsername();
$fromEmail = $user->getEmail();
2021-04-10 00:44:34 +02:00
if (!$this->sendMail($fromName, $fromEmail, "Re: Contact Request", $message, $senderMail)) {
return false;
}
2020-06-26 23:32:45 +02:00
2021-04-10 00:44:34 +02:00
if (!$this->insertResponseMessage()) {
return false;
2020-06-26 23:32:45 +02:00
}
2021-04-10 00:44:34 +02:00
$this->updateEntity();
2020-06-26 23:32:45 +02:00
return $this->success;
}
2021-04-10 00:44:34 +02:00
}
2021-01-07 15:54:19 +01:00
2021-04-10 00:44:34 +02:00
class Fetch extends ContactAPI {
2021-04-09 16:05:36 +02:00
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array());
2021-04-10 00:44:34 +02:00
$this->loginRequired = true;
$this->csrfTokenRequired = false;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$res = $sql->select("ContactRequest.id", "from_name", "from_email", "from_name",
new Sum(new CaseWhen(new CondNot("ContactMessage.read"), 1, 0), "unread"))
2021-04-10 00:44:34 +02:00
->from("ContactRequest")
2022-06-20 19:52:31 +02:00
->groupBy("ContactRequest.id")
->leftJoin("ContactMessage", "ContactRequest.id", "ContactMessage.request_id")
2021-04-10 00:44:34 +02:00
->execute();
$this->success = ($res !== false);
$this->lastError = $sql->getLastError();
2021-04-09 16:05:36 +02:00
if ($this->success) {
2021-05-02 01:22:54 +02:00
$this->result["contactRequests"] = [];
foreach ($res as $row) {
$this->result["contactRequests"][] = array(
2022-06-20 19:52:31 +02:00
"id" => intval($row["id"]),
2021-05-02 01:22:54 +02:00
"from_name" => $row["from_name"],
"from_email" => $row["from_email"],
"unread" => intval($row["unread"]),
);
}
2021-04-09 16:05:36 +02:00
}
return $this->success;
2021-01-07 15:54:19 +01:00
}
2020-06-26 23:32:45 +02:00
}
2021-04-10 00:44:34 +02:00
class Get extends ContactAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
2021-04-10 00:44:34 +02:00
"requestId" => new Parameter("requestId", Parameter::TYPE_INT),
));
$this->loginRequired = true;
$this->csrfTokenRequired = false;
}
private function updateRead() {
$requestId = $this->getParam("requestId");
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-10 00:44:34 +02:00
$sql->update("ContactMessage")
->set("read", 1)
->where(new Compare("request_id", $requestId))
->execute();
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-10 00:44:34 +02:00
$requestId = $this->getParam("requestId");
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-10 00:44:34 +02:00
$res = $sql->select("from_name", "from_email", "message", "created_at")
->from("ContactRequest")
2022-06-20 19:52:31 +02:00
->where(new Compare("id", $requestId))
2021-04-10 00:44:34 +02:00
->execute();
$this->success = ($res !== false);
$this->lastError = $sql->getLastError();
if ($this->success) {
if (empty($res)) {
return $this->createError("Request does not exist");
} else {
$row = $res[0];
$this->result["request"] = array(
"from_name" => $row["from_name"],
"from_email" => $row["from_email"],
"messages" => array(
["sender_id" => null, "message" => $row["message"], "timestamp" => $row["created_at"]]
)
);
$res = $sql->select("user_id", "message", "created_at")
->from("ContactMessage")
->where(new Compare("request_id", $requestId))
->orderBy("created_at")
->execute();
$this->success = ($res !== false);
$this->lastError = $sql->getLastError();
if ($this->success) {
foreach ($res as $row) {
$this->result["request"]["messages"][] = array(
"sender_id" => $row["user_id"], "message" => $row["message"], "timestamp" => $row["created_at"]
);
}
$this->updateRead();
}
}
}
return $this->success;
}
}
2020-06-26 23:32:45 +02:00
}