Google reCaptcha
This commit is contained in:
115
core/Api/ContactAPI.class.php
Normal file
115
core/Api/ContactAPI.class.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Api {
|
||||
class ContactAPI extends Request {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace Api\Contact {
|
||||
|
||||
use Api\ContactAPI;
|
||||
use Api\Parameter\Parameter;
|
||||
use Api\Parameter\StringType;
|
||||
use Api\VerifyCaptcha;
|
||||
use Objects\User;
|
||||
|
||||
class Request extends ContactAPI {
|
||||
|
||||
private int $notificationId;
|
||||
private int $contactRequestId;
|
||||
|
||||
public function __construct(User $user, bool $externalCall = false) {
|
||||
$parameters = array(
|
||||
'fromName' => new StringType('fromName', 32),
|
||||
'fromEmail' => new Parameter('fromEmail', Parameter::TYPE_EMAIL),
|
||||
'message' => new StringType('message', 512),
|
||||
);
|
||||
|
||||
$settings = $user->getConfiguration()->getSettings();
|
||||
if ($settings->isRecaptchaEnabled()) {
|
||||
$parameters["captcha"] = new StringType("captcha");
|
||||
}
|
||||
|
||||
parent::__construct($user, $externalCall, $parameters);
|
||||
}
|
||||
|
||||
public function execute($values = array()) {
|
||||
if (!parent::execute($values)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$settings = $this->user->getConfiguration()->getSettings();
|
||||
if ($settings->isRecaptchaEnabled()) {
|
||||
$captcha = $this->getParam("captcha");
|
||||
$req = new VerifyCaptcha($this->user);
|
||||
if (!$req->execute(array("captcha" => $captcha, "action" => "contact"))) {
|
||||
return $this->createError($req->getLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->insertContactRequest()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->createNotification();
|
||||
|
||||
if (!$this->success) {
|
||||
return $this->createError("The contact request was saved, but the server was unable to create a notification.");
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
|
||||
private function insertContactRequest() {
|
||||
$sql = $this->user->getSQL();
|
||||
$name = $this->getParam("fromName");
|
||||
$email = $this->getParam("fromEmail");
|
||||
$message = $this->getParam("message");
|
||||
|
||||
$res = $sql->insert("ContactRequest", array("from_name", "from_email", "message"))
|
||||
->addRow($name, $email, $message)
|
||||
->returning("uid")
|
||||
->execute();
|
||||
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
if ($this->success) {
|
||||
$this->contactRequestId = $sql->getLastInsertId();
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
|
||||
private function createNotification() {
|
||||
$sql = $this->user->getSQL();
|
||||
$name = $this->getParam("fromName");
|
||||
$email = $this->getParam("fromEmail");
|
||||
$message = $this->getParam("message");
|
||||
|
||||
$res = $sql->insert("Notification", array("title", "message", "type"))
|
||||
->addRow("New Contact Request from: $name", "$name ($email) wrote:\n$message", "message")
|
||||
->returning("uid")
|
||||
->execute();
|
||||
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
if ($this->success) {
|
||||
$this->notificationId = $sql->getLastInsertId();
|
||||
|
||||
$res = $sql->insert("GroupNotification", array("group_id", "notification_id"))
|
||||
->addRow(USER_GROUP_ADMIN, $this->notificationId)
|
||||
->addRow(USER_GROUP_SUPPORT, $this->notificationId)
|
||||
->execute();
|
||||
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Api {
|
||||
|
||||
class NotificationsAPI extends Request {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Api\Notifications {
|
||||
@@ -14,6 +12,9 @@ namespace Api\Notifications {
|
||||
use Api\Parameter\Parameter;
|
||||
use Api\Parameter\StringType;
|
||||
use Driver\SQL\Condition\Compare;
|
||||
use Driver\SQL\Condition\CondIn;
|
||||
use Driver\SQL\Query\Select;
|
||||
use Objects\User;
|
||||
|
||||
class Create extends NotificationsAPI {
|
||||
|
||||
@@ -144,67 +145,67 @@ namespace Api\Notifications {
|
||||
class Fetch extends NotificationsAPI {
|
||||
|
||||
private array $notifications;
|
||||
private array $notificationids;
|
||||
|
||||
public function __construct($user, $externalCall = false) {
|
||||
parent::__construct($user, $externalCall, array());
|
||||
parent::__construct($user, $externalCall, array(
|
||||
'new' => new Parameter('new', Parameter::TYPE_BOOLEAN, true, true)
|
||||
));
|
||||
$this->loginRequired = true;
|
||||
}
|
||||
|
||||
private function fetchUserNotifications() {
|
||||
$onlyNew = $this->getParam('new');
|
||||
$userId = $this->user->getId();
|
||||
$sql = $this->user->getSQL();
|
||||
$res = $sql->select($sql->distinct("Notification.uid"), "created_at", "title", "message")
|
||||
$query = $sql->select($sql->distinct("Notification.uid"), "created_at", "title", "message", "type")
|
||||
->from("Notification")
|
||||
->innerJoin("UserNotification", "UserNotification.notification_id", "Notification.uid")
|
||||
->where(new Compare("UserNotification.user_id", $userId))
|
||||
->where(new Compare("UserNotification.seen", false))
|
||||
->orderBy("created_at")->descending()
|
||||
->execute();
|
||||
->orderBy("created_at")->descending();
|
||||
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
if ($this->success) {
|
||||
foreach($res as $row) {
|
||||
$id = $row["uid"];
|
||||
if (!isset($this->notifications[$id])) {
|
||||
$this->notifications[$id] = array(
|
||||
"uid" => $id,
|
||||
"title" => $row["title"],
|
||||
"message" => $row["message"],
|
||||
"created_at" => $row["created_at"],
|
||||
);
|
||||
}
|
||||
}
|
||||
if ($onlyNew) {
|
||||
$query->where(new Compare("UserNotification.seen", false));
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
return $this->fetchNotifications($query);
|
||||
}
|
||||
|
||||
private function fetchGroupNotifications() {
|
||||
$onlyNew = $this->getParam('new');
|
||||
$userId = $this->user->getId();
|
||||
$sql = $this->user->getSQL();
|
||||
$res = $sql->select($sql->distinct("Notification.uid"), "created_at", "title", "message")
|
||||
$query = $sql->select($sql->distinct("Notification.uid"), "created_at", "title", "message", "type")
|
||||
->from("Notification")
|
||||
->innerJoin("GroupNotification", "GroupNotification.notification_id", "Notification.uid")
|
||||
->innerJoin("UserGroup", "GroupNotification.group_id", "UserGroup.group_id")
|
||||
->where(new Compare("UserGroup.user_id", $userId))
|
||||
->where(new Compare("GroupNotification.seen", false))
|
||||
->orderBy("created_at")->descending()
|
||||
->execute();
|
||||
->orderBy("created_at")->descending();
|
||||
|
||||
if ($onlyNew) {
|
||||
$query->where(new Compare("GroupNotification.seen", false));
|
||||
}
|
||||
|
||||
return $this->fetchNotifications($query);
|
||||
}
|
||||
|
||||
private function fetchNotifications(Select $query) {
|
||||
$sql = $this->user->getSQL();
|
||||
$res = $query->execute();
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
if ($this->success) {
|
||||
foreach($res as $row) {
|
||||
$id = $row["uid"];
|
||||
if (!isset($this->notifications[$id])) {
|
||||
if (!in_array($id, $this->notificationids)) {
|
||||
$this->notificationids[] = $id;
|
||||
$this->notifications[] = array(
|
||||
"uid" => $id,
|
||||
"title" => $row["title"],
|
||||
"message" => $row["message"],
|
||||
"created_at" => $row["created_at"],
|
||||
"type" => $row["type"]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -219,6 +220,7 @@ namespace Api\Notifications {
|
||||
}
|
||||
|
||||
$this->notifications = array();
|
||||
$this->notificationids = array();
|
||||
if ($this->fetchUserNotifications() && $this->fetchGroupNotifications()) {
|
||||
$this->result["notifications"] = $this->notifications;
|
||||
}
|
||||
@@ -227,4 +229,41 @@ namespace Api\Notifications {
|
||||
}
|
||||
}
|
||||
|
||||
class Seen extends NotificationsAPI {
|
||||
|
||||
public function __construct(User $user, bool $externalCall = false) {
|
||||
parent::__construct($user, $externalCall, array());
|
||||
$this->loginRequired = true;
|
||||
}
|
||||
|
||||
public function execute($values = array()) {
|
||||
if (!parent::execute($values)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sql = $this->user->getSQL();
|
||||
$res = $sql->update("UserNotification")
|
||||
->set("seen", true)
|
||||
->where(new Compare("user_id", $this->user->getId()))
|
||||
->execute();
|
||||
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
if ($this->success) {
|
||||
$res = $sql->update("GroupNotification")
|
||||
->set("seen", true)
|
||||
->where(new CondIn("group_id",
|
||||
$sql->select("group_id")
|
||||
->from("UserGroup")
|
||||
->where(new Compare("user_id", $this->user->getId()))))
|
||||
->execute();
|
||||
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,15 +48,17 @@ class Request {
|
||||
}
|
||||
|
||||
public function parseParams($values) {
|
||||
|
||||
foreach($this->params as $name => $param) {
|
||||
$value = $values[$name] ?? NULL;
|
||||
|
||||
if(!$param->optional && (is_null($value) || empty($value))) {
|
||||
$isEmpty = (is_string($value) || is_array($value)) && empty($value);
|
||||
if(!$param->optional && (is_null($value) || $isEmpty)) {
|
||||
$this->lastError = 'Missing parameter: ' . $name;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!is_null($value) && !empty($value)) {
|
||||
if(!is_null($value) && !$isEmpty) {
|
||||
if(!$param->parseParam($value)) {
|
||||
$value = print_r($value, true);
|
||||
$this->lastError = "Invalid Type for parameter: $name '$value' (Required: " . $param->getTypeName() . ")";
|
||||
|
||||
@@ -120,6 +120,7 @@ namespace Api\User {
|
||||
use Api\Parameter\StringType;
|
||||
use Api\SendMail;
|
||||
use Api\UserAPI;
|
||||
use Api\VerifyCaptcha;
|
||||
use DateTime;
|
||||
use Driver\SQL\Condition\Compare;
|
||||
use Driver\SQL\Condition\CondIn;
|
||||
@@ -531,13 +532,20 @@ namespace Api\User {
|
||||
private ?int $userId;
|
||||
private string $token;
|
||||
|
||||
public function __construct($user, $externalCall = false) {
|
||||
parent::__construct($user, $externalCall, array(
|
||||
public function __construct(User $user, bool $externalCall = false) {
|
||||
$parameters = array(
|
||||
"username" => new StringType("username", 32),
|
||||
'email' => new Parameter('email', Parameter::TYPE_EMAIL),
|
||||
"password" => new StringType("password"),
|
||||
"confirmPassword" => new StringType("confirmPassword"),
|
||||
));
|
||||
);
|
||||
|
||||
$settings = $user->getConfiguration()->getSettings();
|
||||
if ($settings->isRecaptchaEnabled()) {
|
||||
$parameters["captcha"] = new StringType("captcha");
|
||||
}
|
||||
|
||||
parent::__construct($user, $externalCall, $parameters);
|
||||
}
|
||||
|
||||
private function insertToken() {
|
||||
@@ -582,6 +590,15 @@ namespace Api\User {
|
||||
return $this->createError("User Registration is not enabled.");
|
||||
}
|
||||
|
||||
$settings = $this->user->getConfiguration()->getSettings();
|
||||
if ($settings->isRecaptchaEnabled()) {
|
||||
$captcha = $this->getParam("captcha");
|
||||
$req = new VerifyCaptcha($this->user);
|
||||
if (!$req->execute(array("captcha" => $captcha, "action" => "register"))) {
|
||||
return $this->createError($req->getLastError());
|
||||
}
|
||||
}
|
||||
|
||||
$username = $this->getParam("username");
|
||||
$email = $this->getParam('email');
|
||||
if (!$this->userExists($username, $email)) {
|
||||
|
||||
67
core/Api/verifyCaptcha.class.php
Normal file
67
core/Api/verifyCaptcha.class.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Api;
|
||||
|
||||
use Api\Parameter\StringType;
|
||||
use Objects\User;
|
||||
|
||||
class VerifyCaptcha extends Request {
|
||||
|
||||
public function __construct(User $user, bool $externalCall = false) {
|
||||
parent::__construct($user, $externalCall, array(
|
||||
"captcha" => new StringType("captcha"),
|
||||
"action" => new StringType("action"),
|
||||
));
|
||||
|
||||
$this->isPublic = false;
|
||||
}
|
||||
|
||||
public function execute($values = array()) {
|
||||
if(!parent::execute($values)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$settings = $this->user->getConfiguration()->getSettings();
|
||||
if (!$settings->isRecaptchaEnabled()) {
|
||||
return $this->createError("Google reCaptcha is not enabled.");
|
||||
}
|
||||
|
||||
$url = "https://www.google.com/recaptcha/api/siteverify";
|
||||
$secret = $settings->getRecaptchaSecretKey();
|
||||
$captcha = $this->getParam("captcha");
|
||||
$action = $this->getParam("action");
|
||||
|
||||
$params = array(
|
||||
"secret" => $secret,
|
||||
"response" => $captcha
|
||||
);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL,$url);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = @json_decode(curl_exec($ch), true);
|
||||
curl_close ($ch);
|
||||
|
||||
$this->success = false;
|
||||
$this->lastError = "Could not verify captcha: No response from google received.";
|
||||
|
||||
if($response) {
|
||||
$this->success = $response["success"];
|
||||
if(!$this->success) {
|
||||
$this->lastError = "Could not verify captcha: " . implode(";", $response["error-codes"]);
|
||||
} else {
|
||||
$score = $response["score"];
|
||||
if($action !== $response["action"]) {
|
||||
$this->createError("Could not verify captcha: Action does not match");
|
||||
}
|
||||
else if($score < 0.7) {
|
||||
$this->createError("Could not verify captcha: Google ReCaptcha Score < 0.7 (Your score: $score), you are likely a bot");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user