2020-06-20 20:13:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
use Driver\SQL\Condition\Compare;
|
|
|
|
|
|
|
|
abstract class UserAPI extends Request {
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
protected function userExists(?string $username, ?string $email = null): bool {
|
2020-06-23 17:55:52 +02:00
|
|
|
|
|
|
|
$conditions = array();
|
2021-04-02 21:58:06 +02:00
|
|
|
if ($username) {
|
2020-06-23 17:55:52 +02:00
|
|
|
$conditions[] = new Compare("User.name", $username);
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
if ($email) {
|
2020-06-23 17:55:52 +02:00
|
|
|
$conditions[] = new Compare("User.email", $email);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($conditions)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select("User.name", "User.email")
|
|
|
|
->from("User")
|
2020-06-23 17:55:52 +02:00
|
|
|
->where(...$conditions)
|
2020-06-22 21:50:58 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success && !empty($res)) {
|
|
|
|
$row = $res[0];
|
|
|
|
if (strcasecmp($username, $row['name']) === 0) {
|
|
|
|
return $this->createError("This username is already taken.");
|
2020-07-01 22:13:50 +02:00
|
|
|
} else if (strcasecmp($email, $row['email']) === 0) {
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->createError("This email address is already in use.");
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
2020-06-22 21:50:58 +02:00
|
|
|
|
|
|
|
return $this->success;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
protected function checkPasswordRequirements($password, $confirmPassword): bool {
|
|
|
|
if ((($password === null) !== ($confirmPassword === null)) || strcmp($password, $confirmPassword) !== 0) {
|
2020-06-29 16:47:02 +02:00
|
|
|
return $this->createError("The given passwords do not match");
|
|
|
|
} else if(strlen($password) < 6) {
|
|
|
|
return $this->createError("The password should be at least 6 characters long");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
protected function checkUsernameRequirements($username): bool {
|
|
|
|
if (strlen($username) < 5 || strlen($username) > 32) {
|
2020-06-29 16:47:02 +02:00
|
|
|
return $this->createError("The username should be between 5 and 32 characters long");
|
2021-11-11 14:25:26 +01:00
|
|
|
} else if (!preg_match("/[a-zA-Z0-9_\-]+/", $username)) {
|
|
|
|
return $this->createError("The username should only contain the following characters: a-z A-Z 0-9 _ -");
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkRequirements($username, $password, $confirmPassword): bool {
|
|
|
|
return $this->checkUsernameRequirements($username) &&
|
|
|
|
$this->checkPasswordRequirements($password, $confirmPassword);
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 18:31:54 +01:00
|
|
|
protected function insertUser($username, $email, $password, $confirmed, $fullName = "") {
|
2020-06-22 21:50:58 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2020-06-23 18:40:43 +02:00
|
|
|
$hash = $this->hashPassword($password);
|
2022-02-20 16:53:26 +01:00
|
|
|
$res = $sql->insert("User", array("name", "password", "email", "confirmed", "fullName"))
|
2022-02-20 18:31:54 +01:00
|
|
|
->addRow($username, $hash, $email, $confirmed, $fullName ?? "")
|
2020-06-22 21:50:58 +02:00
|
|
|
->returning("uid")
|
|
|
|
->execute();
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
$this->success = ($res !== FALSE);
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if ($this->success) {
|
|
|
|
return $sql->getLastInsertId();
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->success;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:40:43 +02:00
|
|
|
protected function hashPassword($password) {
|
|
|
|
return password_hash($password, PASSWORD_BCRYPT);
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-23 15:31:09 +02:00
|
|
|
|
2020-06-23 17:55:52 +02:00
|
|
|
protected function getUser($id) {
|
|
|
|
$sql = $this->user->getSQL();
|
2021-12-08 16:53:43 +01:00
|
|
|
$res = $sql->select("User.uid as userId", "User.name", "User.fullName", "User.email",
|
|
|
|
"User.registered_at", "User.confirmed", "User.last_online", "User.profilePicture",
|
2022-02-20 16:53:26 +01:00
|
|
|
"User.gpg_id", "GpgKey.confirmed as gpg_confirmed", "GpgKey.fingerprint as gpg_fingerprint",
|
|
|
|
"GpgKey.expires as gpg_expires", "GpgKey.algorithm as gpg_algorithm",
|
2020-06-23 17:55:52 +02:00
|
|
|
"Group.uid as groupId", "Group.name as groupName", "Group.color as groupColor")
|
|
|
|
->from("User")
|
|
|
|
->leftJoin("UserGroup", "User.uid", "UserGroup.user_id")
|
|
|
|
->leftJoin("Group", "Group.uid", "UserGroup.group_id")
|
2022-02-20 16:53:26 +01:00
|
|
|
->leftJoin("GpgKey", "GpgKey.uid", "User.gpg_id")
|
2020-06-23 17:55:52 +02:00
|
|
|
->where(new Compare("User.uid", $id))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
return ($this->success && !empty($res) ? $res : array());
|
|
|
|
}
|
2020-06-26 18:24:23 +02:00
|
|
|
|
2020-06-29 16:50:36 +02:00
|
|
|
protected function invalidateToken($token) {
|
|
|
|
$this->user->getSQL()
|
|
|
|
->update("UserToken")
|
|
|
|
->set("used", true)
|
|
|
|
->where(new Compare("token", $token))
|
|
|
|
->execute();
|
|
|
|
}
|
2021-11-11 14:25:26 +01:00
|
|
|
|
|
|
|
protected function insertToken(int $userId, string $token, string $tokenType, int $duration): bool {
|
|
|
|
$validUntil = (new \DateTime())->modify("+$duration hour");
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->insert("UserToken", array("user_id", "token", "token_type", "valid_until"))
|
|
|
|
->addRow($userId, $token, $tokenType, $validUntil)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
|
|
|
|
protected function formatDuration(int $count, string $string): string {
|
|
|
|
if ($count === 1) {
|
|
|
|
return $string;
|
|
|
|
} else {
|
|
|
|
return "the next $count ${string}s";
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Api\User {
|
|
|
|
|
|
|
|
use Api\Parameter\Parameter;
|
|
|
|
use Api\Parameter\StringType;
|
2021-12-08 16:53:43 +01:00
|
|
|
use Api\Template\Render;
|
2020-06-20 20:13:51 +02:00
|
|
|
use Api\UserAPI;
|
2020-06-26 23:32:45 +02:00
|
|
|
use Api\VerifyCaptcha;
|
2020-06-20 20:13:51 +02:00
|
|
|
use DateTime;
|
2021-12-08 16:53:43 +01:00
|
|
|
use Driver\SQL\Column\Column;
|
2020-06-20 20:13:51 +02:00
|
|
|
use Driver\SQL\Condition\Compare;
|
2020-06-29 16:47:02 +02:00
|
|
|
use Driver\SQL\Condition\CondBool;
|
2020-06-23 22:24:56 +02:00
|
|
|
use Driver\SQL\Condition\CondIn;
|
2021-12-08 16:53:43 +01:00
|
|
|
use Driver\SQL\Condition\CondNot;
|
|
|
|
use Driver\SQL\Expression\JsonArrayAgg;
|
|
|
|
use ImagickException;
|
2022-02-20 16:53:26 +01:00
|
|
|
use Objects\GpgKey;
|
|
|
|
use Objects\TwoFactor\KeyBasedTwoFactorToken;
|
|
|
|
use Objects\TwoFactor\TwoFactorToken;
|
2020-06-23 17:55:52 +02:00
|
|
|
use Objects\User;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Create extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'username' => new StringType('username', 32),
|
2020-06-24 01:09:08 +02:00
|
|
|
'email' => new Parameter('email', Parameter::TYPE_EMAIL, true, NULL),
|
2020-06-22 21:50:58 +02:00
|
|
|
'password' => new StringType('password'),
|
|
|
|
'confirmPassword' => new StringType('confirmPassword'),
|
|
|
|
));
|
2020-06-23 15:31:09 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->loginRequired = true;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$username = $this->getParam('username');
|
|
|
|
$email = $this->getParam('email');
|
2020-06-23 18:40:43 +02:00
|
|
|
$password = $this->getParam('password');
|
|
|
|
$confirmPassword = $this->getParam('confirmPassword');
|
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
if (!$this->checkRequirements($username, $password, $confirmPassword)) {
|
|
|
|
return false;
|
2020-06-23 18:40:43 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 17:55:52 +02:00
|
|
|
if (!$this->userExists($username, $email)) {
|
2020-06-22 21:50:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-23 21:18:45 +02:00
|
|
|
// prevent duplicate keys
|
|
|
|
$email = (!is_null($email) && empty($email)) ? null : $email;
|
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
$id = $this->insertUser($username, $email, $password, true);
|
2020-06-23 18:40:43 +02:00
|
|
|
if ($this->success) {
|
|
|
|
$this->result["userId"] = $id;
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-23 18:40:43 +02:00
|
|
|
return $this->success;
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Fetch extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
private int $userCount;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
2020-06-23 15:31:09 +02:00
|
|
|
'page' => new Parameter('page', Parameter::TYPE_INT, true, 1),
|
2020-06-23 16:26:04 +02:00
|
|
|
'count' => new Parameter('count', Parameter::TYPE_INT, true, 20)
|
2020-06-22 21:50:58 +02:00
|
|
|
));
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
private function getUserCount(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select($sql->count())->from("User")->execute();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if ($this->success) {
|
|
|
|
$this->userCount = $res[0]["count"];
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->success;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:24:56 +02:00
|
|
|
private function selectIds($page, $count) {
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select("User.uid")
|
|
|
|
->from("User")
|
|
|
|
->limit($count)
|
|
|
|
->offset(($page - 1) * $count)
|
|
|
|
->orderBy("User.uid")
|
|
|
|
->ascending()
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== NULL);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($this->success && is_array($res)) {
|
|
|
|
return array_map(function ($row) {
|
|
|
|
return intval($row["uid"]);
|
|
|
|
}, $res);
|
2020-06-23 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$page = $this->getParam("page");
|
|
|
|
if ($page < 1) {
|
|
|
|
return $this->createError("Invalid page count");
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 15:31:09 +02:00
|
|
|
$count = $this->getParam("count");
|
|
|
|
if ($count < 1 || $count > 50) {
|
|
|
|
return $this->createError("Invalid fetch count");
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if (!$this->getUserCount()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-23 22:24:56 +02:00
|
|
|
$userIds = $this->selectIds($page, $count);
|
|
|
|
if ($userIds === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2020-07-02 00:47:45 +02:00
|
|
|
$res = $sql->select("User.uid as userId", "User.name", "User.email", "User.registered_at", "User.confirmed",
|
2021-12-08 16:53:43 +01:00
|
|
|
"User.profilePicture", "User.fullName", "Group.uid as groupId", "User.last_online",
|
|
|
|
"Group.name as groupName", "Group.color as groupColor")
|
2020-06-22 21:50:58 +02:00
|
|
|
->from("User")
|
|
|
|
->leftJoin("UserGroup", "User.uid", "UserGroup.user_id")
|
|
|
|
->leftJoin("Group", "Group.uid", "UserGroup.group_id")
|
2021-12-08 16:53:43 +01:00
|
|
|
->where(new CondIn(new Column("User.uid"), $userIds))
|
2020-06-22 21:50:58 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
$this->result["users"] = array();
|
|
|
|
foreach ($res as $row) {
|
|
|
|
$userId = intval($row["userId"]);
|
|
|
|
$groupId = intval($row["groupId"]);
|
|
|
|
$groupName = $row["groupName"];
|
2020-06-23 15:31:09 +02:00
|
|
|
$groupColor = $row["groupColor"];
|
2021-12-08 16:53:43 +01:00
|
|
|
|
|
|
|
$fullInfo = ($userId === $this->user->getId()) ||
|
|
|
|
($this->user->hasGroup(USER_GROUP_ADMIN) || $this->user->hasGroup(USER_GROUP_SUPPORT));
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if (!isset($this->result["users"][$userId])) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$user = array(
|
2020-06-22 21:50:58 +02:00
|
|
|
"uid" => $userId,
|
|
|
|
"name" => $row["name"],
|
2021-12-08 16:53:43 +01:00
|
|
|
"fullName" => $row["fullName"],
|
|
|
|
"profilePicture" => $row["profilePicture"],
|
2020-06-22 21:50:58 +02:00
|
|
|
"email" => $row["email"],
|
2020-07-02 00:47:45 +02:00
|
|
|
"confirmed" => $sql->parseBool($row["confirmed"]),
|
2020-06-22 21:50:58 +02:00
|
|
|
"groups" => array(),
|
|
|
|
);
|
2021-12-08 16:53:43 +01:00
|
|
|
|
|
|
|
if ($fullInfo) {
|
|
|
|
$user["registered_at"] = $row["registered_at"];
|
|
|
|
$user["last_online"] = $row["last_online"];
|
|
|
|
} else if (!$sql->parseBool($row["confirmed"])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->result["users"][$userId] = $user;
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if (!is_null($groupId)) {
|
2020-06-23 15:31:09 +02:00
|
|
|
$this->result["users"][$userId]["groups"][$groupId] = array(
|
|
|
|
"name" => $groupName,
|
|
|
|
"color" => $groupColor
|
|
|
|
);
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 15:31:09 +02:00
|
|
|
$this->result["pageCount"] = intval(ceil($this->userCount / $count));
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->result["totalCount"] = $this->userCount;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->success;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 16:26:04 +02:00
|
|
|
class Get extends UserAPI {
|
|
|
|
|
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'id' => new Parameter('id', Parameter::TYPE_INT)
|
|
|
|
));
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->loginRequired = true;
|
2020-06-23 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-23 16:26:04 +02:00
|
|
|
|
2020-07-02 00:47:45 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2021-12-08 16:53:43 +01:00
|
|
|
$userId = $this->getParam("id");
|
|
|
|
$user = $this->getUser($userId);
|
2020-06-23 16:26:04 +02:00
|
|
|
if ($this->success) {
|
2020-06-23 17:55:52 +02:00
|
|
|
if (empty($user)) {
|
2020-06-23 16:26:04 +02:00
|
|
|
return $this->createError("User not found");
|
|
|
|
} else {
|
2021-12-08 16:53:43 +01:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
$gpgFingerprint = null;
|
|
|
|
if ($user[0]["gpg_id"] && $sql->parseBool($user[0]["gpg_confirmed"])) {
|
|
|
|
$gpgFingerprint = $user[0]["gpg_fingerprint"];
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$queriedUser = array(
|
|
|
|
"uid" => $userId,
|
2020-06-23 17:55:52 +02:00
|
|
|
"name" => $user[0]["name"],
|
2021-12-08 16:53:43 +01:00
|
|
|
"fullName" => $user[0]["fullName"],
|
2020-06-23 17:55:52 +02:00
|
|
|
"email" => $user[0]["email"],
|
|
|
|
"registered_at" => $user[0]["registered_at"],
|
2021-12-08 16:53:43 +01:00
|
|
|
"last_online" => $user[0]["last_online"],
|
|
|
|
"profilePicture" => $user[0]["profilePicture"],
|
2020-07-02 00:47:45 +02:00
|
|
|
"confirmed" => $sql->parseBool($user["0"]["confirmed"]),
|
2021-12-08 16:53:43 +01:00
|
|
|
"groups" => array(),
|
2022-02-20 16:53:26 +01:00
|
|
|
"gpgFingerprint" => $gpgFingerprint,
|
2020-06-23 16:26:04 +02:00
|
|
|
);
|
|
|
|
|
2020-06-23 17:55:52 +02:00
|
|
|
foreach($user as $row) {
|
2020-06-23 22:24:56 +02:00
|
|
|
if (!is_null($row["groupId"])) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$queriedUser["groups"][$row["groupId"]] = array(
|
2020-06-23 22:24:56 +02:00
|
|
|
"name" => $row["groupName"],
|
|
|
|
"color" => $row["groupColor"],
|
|
|
|
);
|
|
|
|
}
|
2020-06-23 16:26:04 +02:00
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
// either we are querying own info or we are support / admin
|
|
|
|
$canView = ($userId === $this->user->getId()) ||
|
|
|
|
($this->user->hasGroup(USER_GROUP_ADMIN) ||
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->user->hasGroup(USER_GROUP_SUPPORT));
|
|
|
|
|
|
|
|
// full info only when we have administrative privileges, or we are querying ourselves
|
|
|
|
$fullInfo = ($userId === $this->user->getId()) ||
|
|
|
|
($this->user->hasGroup(USER_GROUP_ADMIN) || $this->user->hasGroup(USER_GROUP_SUPPORT));
|
|
|
|
|
|
|
|
if (!$canView) {
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
// check if user posted something publicly
|
|
|
|
$res = $sql->select(new JsonArrayAgg(new Column("publishedBy"), "publisherIds"))
|
|
|
|
->from("News")
|
|
|
|
->execute();
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if (!$this->success ) {
|
|
|
|
return false;
|
2022-02-20 16:53:26 +01:00
|
|
|
} else {
|
|
|
|
$canView = in_array($userId, json_decode($res[0]["publisherIds"], true));
|
2021-12-08 16:53:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$canView) {
|
|
|
|
return $this->createError("No permissions to access this user");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$fullInfo) {
|
|
|
|
if (!$queriedUser["confirmed"]) {
|
|
|
|
return $this->createError("No permissions to access this user");
|
|
|
|
}
|
|
|
|
unset($queriedUser["registered_at"]);
|
|
|
|
unset($queriedUser["confirmed"]);
|
|
|
|
unset($queriedUser["last_online"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->result["user"] = $queriedUser;
|
2020-06-23 16:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Info extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array());
|
2020-06-23 15:31:09 +02:00
|
|
|
$this->csrfTokenRequired = false;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if (!$this->user->isLoggedIn()) {
|
|
|
|
$this->result["loggedIn"] = false;
|
|
|
|
} else {
|
|
|
|
$this->result["loggedIn"] = true;
|
2021-11-11 14:25:26 +01:00
|
|
|
$userGroups = array_keys($this->user->getGroups());
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select("method", "groups")
|
|
|
|
->from("ApiPermission")
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$permissions = [];
|
|
|
|
if (is_array($res)) {
|
|
|
|
foreach ($res as $row) {
|
|
|
|
$requiredGroups = json_decode($row["groups"], true);
|
|
|
|
if (empty($requiredGroups) || !empty(array_intersect($requiredGroups, $userGroups))) {
|
|
|
|
$permissions[] = $row["method"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->result["permissions"] = $permissions;
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->result["user"] = $this->user->jsonSerialize();
|
|
|
|
return $this->success;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Invite extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'username' => new StringType('username', 32),
|
|
|
|
'email' => new StringType('email', 64),
|
|
|
|
));
|
2020-06-23 15:31:09 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->loginRequired = true;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-22 21:50:58 +02:00
|
|
|
|
|
|
|
$username = $this->getParam('username');
|
|
|
|
$email = $this->getParam('email');
|
|
|
|
if (!$this->userExists($username, $email)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
// Create user
|
|
|
|
$id = $this->insertUser($username, $email, "", false);
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Token
|
2020-06-22 21:50:58 +02:00
|
|
|
$token = generateRandomString(36);
|
2021-12-08 16:53:43 +01:00
|
|
|
$validDays = 7;
|
|
|
|
$valid_until = (new DateTime())->modify("+$validDays day");
|
2020-06-22 21:50:58 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2020-06-29 16:47:02 +02:00
|
|
|
$res = $sql->insert("UserToken", array("user_id", "token", "token_type", "valid_until"))
|
|
|
|
->addRow($id, $token, "invite", $valid_until)
|
2020-06-22 21:50:58 +02:00
|
|
|
->execute();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
//send validation mail
|
|
|
|
if ($this->success) {
|
2020-06-26 18:24:23 +02:00
|
|
|
|
|
|
|
$settings = $this->user->getConfiguration()->getSettings();
|
2021-12-08 16:53:43 +01:00
|
|
|
$baseUrl = $settings->getBaseUrl();
|
|
|
|
$siteName = $settings->getSiteName();
|
|
|
|
|
|
|
|
$req = new Render($this->user);
|
|
|
|
$this->success = $req->execute([
|
|
|
|
"file" => "mail/accept_invite.twig",
|
|
|
|
"parameters" => [
|
|
|
|
"link" => "$baseUrl/acceptInvite?token=$token",
|
|
|
|
"site_name" => $siteName,
|
|
|
|
"base_url" => $baseUrl,
|
|
|
|
"username" => $username,
|
|
|
|
"valid_time" => $this->formatDuration($validDays, "day")
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$this->lastError = $req->getLastError();
|
2020-06-26 18:24:23 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($this->success) {
|
|
|
|
$messageBody = $req->getResult()["html"];
|
|
|
|
$request = new \Api\Mail\Send($this->user);
|
|
|
|
$this->success = $request->execute(array(
|
|
|
|
"to" => $email,
|
|
|
|
"subject" => "[$siteName] Account Invitation",
|
|
|
|
"body" => $messageBody
|
|
|
|
));
|
2020-06-26 18:24:23 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->lastError = $request->getLastError();
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
2020-06-26 18:24:23 +02:00
|
|
|
|
|
|
|
if (!$this->success) {
|
|
|
|
$this->lastError = "The invitation was created but the confirmation email could not be sent. " .
|
|
|
|
"Please contact the server administration. Reason: " . $this->lastError;
|
|
|
|
}
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-29 16:47:02 +02:00
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AcceptInvite extends UserAPI {
|
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'token' => new StringType('token', 36),
|
|
|
|
'password' => new StringType('password'),
|
|
|
|
'confirmPassword' => new StringType('confirmPassword'),
|
|
|
|
));
|
2020-07-02 00:47:45 +02:00
|
|
|
$this->csrfTokenRequired = false;
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
private function updateUser($uid, $password): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->update("User")
|
|
|
|
->set("password", $this->hashPassword($password))
|
|
|
|
->set("confirmed", true)
|
|
|
|
->where(new Compare("uid", $uid))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
|
|
|
|
if ($this->user->isLoggedIn()) {
|
|
|
|
return $this->createError("You are already logged in.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->getParam("token");
|
|
|
|
$password = $this->getParam("password");
|
|
|
|
$confirmPassword = $this->getParam("confirmPassword");
|
|
|
|
|
|
|
|
$req = new CheckToken($this->user);
|
|
|
|
$this->success = $req->execute(array("token" => $token));
|
|
|
|
$this->lastError = $req->getLastError();
|
|
|
|
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $req->getResult();
|
|
|
|
if (strcasecmp($result["token"]["type"], "invite") !== 0) {
|
|
|
|
return $this->createError("Invalid token type");
|
|
|
|
} else if($result["user"]["confirmed"]) {
|
|
|
|
return $this->createError("Your email address is already confirmed.");
|
|
|
|
} else if (!$this->checkPasswordRequirements($password, $confirmPassword)) {
|
|
|
|
return false;
|
|
|
|
} else if (!$this->updateUser($result["user"]["uid"], $password)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
// Invalidate token
|
|
|
|
$this->user->getSQL()
|
|
|
|
->update("UserToken")
|
|
|
|
->set("used", true)
|
|
|
|
->where(new Compare("token", $token))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConfirmEmail extends UserAPI {
|
|
|
|
|
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'token' => new StringType('token', 36)
|
|
|
|
));
|
2021-11-11 14:25:26 +01:00
|
|
|
$this->csrfTokenRequired = false;
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
private function updateUser($uid): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->update("User")
|
|
|
|
->set("confirmed", true)
|
|
|
|
->where(new Compare("uid", $uid))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->success;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
2020-06-29 16:47:02 +02:00
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
|
|
|
|
if ($this->user->isLoggedIn()) {
|
|
|
|
return $this->createError("You are already logged in.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->getParam("token");
|
|
|
|
$req = new CheckToken($this->user);
|
|
|
|
$this->success = $req->execute(array("token" => $token));
|
|
|
|
$this->lastError = $req->getLastError();
|
|
|
|
|
2020-07-01 23:07:00 +02:00
|
|
|
if ($this->success) {
|
|
|
|
$result = $req->getResult();
|
|
|
|
if (strcasecmp($result["token"]["type"], "email_confirm") !== 0) {
|
|
|
|
return $this->createError("Invalid token type");
|
|
|
|
} else if($result["user"]["confirmed"]) {
|
|
|
|
return $this->createError("Your email address is already confirmed.");
|
|
|
|
} else if (!$this->updateUser($result["user"]["uid"])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$this->invalidateToken($token);
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
2020-07-01 23:07:00 +02:00
|
|
|
|
|
|
|
return $this->success;
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Login extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
private int $startedAt;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
2021-12-08 16:53:43 +01:00
|
|
|
'username' => new StringType('username'),
|
2020-06-22 21:50:58 +02:00
|
|
|
'password' => new StringType('password'),
|
2022-02-20 16:53:26 +01:00
|
|
|
'stayLoggedIn' => new Parameter('stayLoggedIn', Parameter::TYPE_BOOLEAN, true, false)
|
2020-06-22 21:50:58 +02:00
|
|
|
));
|
|
|
|
$this->forbidMethod("GET");
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
private function wrongCredentials(): bool {
|
2020-06-22 21:50:58 +02:00
|
|
|
$runtime = microtime(true) - $this->startedAt;
|
|
|
|
$sleepTime = round(3e6 - $runtime);
|
|
|
|
if ($sleepTime > 0) usleep($sleepTime);
|
|
|
|
return $this->createError(L('Wrong username or password'));
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if ($this->user->isLoggedIn()) {
|
|
|
|
$this->lastError = L('You are already logged in');
|
|
|
|
$this->success = true;
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->startedAt = microtime(true);
|
|
|
|
$this->success = false;
|
|
|
|
$username = $this->getParam('username');
|
|
|
|
$password = $this->getParam('password');
|
|
|
|
$stayLoggedIn = $this->getParam('stayLoggedIn');
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2022-02-20 16:53:26 +01:00
|
|
|
$res = $sql->select("User.uid", "User.password", "User.confirmed",
|
|
|
|
"User.2fa_id", "2FA.type as 2fa_type", "2FA.confirmed as 2fa_confirmed", "2FA.data as 2fa_data")
|
2020-06-22 21:50:58 +02:00
|
|
|
->from("User")
|
2021-12-08 16:53:43 +01:00
|
|
|
->where(new Compare("User.name", $username), new Compare("User.email", $username))
|
2022-02-20 16:53:26 +01:00
|
|
|
->leftJoin("2FA", "2FA.uid", "User.2fa_id")
|
2021-12-08 16:53:43 +01:00
|
|
|
->limit(1)
|
2020-06-22 21:50:58 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
2021-11-11 14:25:26 +01:00
|
|
|
if (!is_array($res) || count($res) === 0) {
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->wrongCredentials();
|
|
|
|
} else {
|
|
|
|
$row = $res[0];
|
|
|
|
$uid = $row['uid'];
|
2020-06-29 16:47:02 +02:00
|
|
|
$confirmed = $sql->parseBool($row["confirmed"]);
|
2022-02-20 16:53:26 +01:00
|
|
|
$token = $row["2fa_id"] ? TwoFactorToken::newInstance($row["2fa_type"], $row["2fa_data"], $row["2fa_id"], $sql->parseBool($row["2fa_confirmed"])) : null;
|
2020-06-23 18:40:43 +02:00
|
|
|
if (password_verify($password, $row['password'])) {
|
2020-06-29 16:47:02 +02:00
|
|
|
if (!$confirmed) {
|
2021-11-11 14:25:26 +01:00
|
|
|
$this->result["emailConfirmed"] = false;
|
2020-06-29 16:47:02 +02:00
|
|
|
return $this->createError("Your email address has not been confirmed yet.");
|
|
|
|
} else if (!($this->success = $this->user->createSession($uid, $stayLoggedIn))) {
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->createError("Error creating Session: " . $sql->getLastError());
|
|
|
|
} else {
|
|
|
|
$this->result["loggedIn"] = true;
|
2020-06-26 14:58:17 +02:00
|
|
|
$this->result["logoutIn"] = $this->user->getSession()->getExpiresSeconds();
|
|
|
|
$this->result["csrf_token"] = $this->user->getSession()->getCsrfToken();
|
2022-02-20 16:53:26 +01:00
|
|
|
if ($token && $token->isConfirmed()) {
|
|
|
|
$this->result["2fa"] = ["type" => $token->getType()];
|
|
|
|
if ($token instanceof KeyBasedTwoFactorToken) {
|
|
|
|
$challenge = base64_encode(generateRandomString(32, "raw"));
|
|
|
|
$this->result["2fa"]["challenge"] = $challenge;
|
|
|
|
$_SESSION["challenge"] = $challenge;
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->success = true;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
} else {
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->wrongCredentials();
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->success;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Logout extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall);
|
2022-02-20 16:53:26 +01:00
|
|
|
$this->loginRequired = false;
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->apiKeyAllowed = false;
|
2022-02-20 16:53:26 +01:00
|
|
|
$this->forbidMethod("GET");
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
if (!$this->user->isLoggedIn()) {
|
|
|
|
return $this->createError("You are not logged in.");
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$this->success = $this->user->logout();
|
|
|
|
$this->lastError = $this->user->getSQL()->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class Register extends UserAPI {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
private ?int $userId;
|
|
|
|
private string $token;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-26 23:32:45 +02:00
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
$parameters = array(
|
2020-06-22 21:50:58 +02:00
|
|
|
"username" => new StringType("username", 32),
|
2020-06-24 01:09:08 +02:00
|
|
|
'email' => new Parameter('email', Parameter::TYPE_EMAIL),
|
2020-06-22 21:50:58 +02:00
|
|
|
"password" => new StringType("password"),
|
|
|
|
"confirmPassword" => new StringType("confirmPassword"),
|
2020-06-26 23:32:45 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$settings = $user->getConfiguration()->getSettings();
|
|
|
|
if ($settings->isRecaptchaEnabled()) {
|
|
|
|
$parameters["captcha"] = new StringType("captcha");
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($user, $externalCall, $parameters);
|
2020-07-01 22:13:50 +02:00
|
|
|
$this->csrfTokenRequired = false;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if ($this->user->isLoggedIn()) {
|
2020-06-26 01:47:43 +02:00
|
|
|
return $this->createError(L('You are already logged in'));
|
|
|
|
}
|
|
|
|
|
2020-06-27 22:47:12 +02:00
|
|
|
$registrationAllowed = $this->user->getConfiguration()->getSettings()->isRegistrationAllowed();
|
2020-06-26 01:47:43 +02:00
|
|
|
if(!$registrationAllowed) {
|
|
|
|
return $this->createError("User Registration is not enabled.");
|
|
|
|
}
|
|
|
|
|
2020-06-26 23:32:45 +02:00
|
|
|
$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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
$username = $this->getParam("username");
|
|
|
|
$email = $this->getParam('email');
|
2020-06-29 16:47:02 +02:00
|
|
|
$password = $this->getParam("password");
|
|
|
|
$confirmPassword = $this->getParam("confirmPassword");
|
2020-06-22 21:50:58 +02:00
|
|
|
if (!$this->userExists($username, $email)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
if(!$this->checkRequirements($username, $password, $confirmPassword)) {
|
|
|
|
return false;
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
$fullName = substr($email, 0, strrpos($email, "@"));
|
|
|
|
$fullName = implode(" ", array_map(function ($part) {
|
|
|
|
return ucfirst(strtolower($part));
|
|
|
|
}, explode(".", $fullName))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->userId = $this->insertUser($username, $email, $password, false, $fullName);
|
2021-11-11 14:25:26 +01:00
|
|
|
if (!$this->success) {
|
2020-06-22 21:50:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$validHours = 48;
|
|
|
|
$this->token = generateRandomString(36);
|
|
|
|
if ($this->insertToken($this->userId, $this->token, "email_confirm", $validHours)) {
|
2020-06-26 18:24:23 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$settings = $this->user->getConfiguration()->getSettings();
|
|
|
|
$baseUrl = $settings->getBaseUrl();
|
|
|
|
$siteName = $settings->getSiteName();
|
|
|
|
$req = new Render($this->user);
|
|
|
|
$this->success = $req->execute([
|
|
|
|
"file" => "mail/confirm_email.twig",
|
|
|
|
"parameters" => [
|
2020-07-01 22:13:50 +02:00
|
|
|
"link" => "$baseUrl/confirmEmail?token=$this->token",
|
|
|
|
"site_name" => $siteName,
|
|
|
|
"base_url" => $baseUrl,
|
2021-12-08 16:53:43 +01:00
|
|
|
"username" => $username,
|
|
|
|
"valid_time" => $this->formatDuration($validHours, "hour")
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$this->lastError = $req->getLastError();
|
2020-06-26 18:24:23 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($this->success) {
|
|
|
|
$messageBody = $req->getResult()["html"];
|
2020-07-01 22:13:50 +02:00
|
|
|
$request = new \Api\Mail\Send($this->user);
|
|
|
|
$this->success = $request->execute(array(
|
2020-06-26 18:24:23 +02:00
|
|
|
"to" => $email,
|
|
|
|
"subject" => "[$siteName] E-Mail Confirmation",
|
2020-06-29 16:47:02 +02:00
|
|
|
"body" => $messageBody
|
2020-07-01 22:13:50 +02:00
|
|
|
));
|
|
|
|
$this->lastError = $request->getLastError();
|
|
|
|
}
|
2020-06-26 18:24:23 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
if (!$this->success) {
|
|
|
|
$this->lastError = "Your account was registered but the confirmation email could not be sent. " .
|
2020-06-26 18:24:23 +02:00
|
|
|
"Please contact the server administration. Reason: " . $this->lastError;
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
return $this->success;
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:50:58 +02:00
|
|
|
class CheckToken extends UserAPI {
|
2020-06-29 16:47:02 +02:00
|
|
|
|
2020-06-22 21:03:30 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
2020-06-22 21:50:58 +02:00
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'token' => new StringType('token', 36),
|
|
|
|
));
|
2020-06-22 21:03:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
private function checkToken($token) {
|
|
|
|
$sql = $this->user->getSQL();
|
2020-08-09 14:23:36 +02:00
|
|
|
$res = $sql->select("UserToken.token_type", "User.uid", "User.name", "User.email", "User.confirmed")
|
2020-06-29 16:47:02 +02:00
|
|
|
->from("UserToken")
|
|
|
|
->innerJoin("User", "UserToken.user_id", "User.uid")
|
|
|
|
->where(new Compare("UserToken.token", $token))
|
|
|
|
->where(new Compare("UserToken.valid_until", $sql->now(), ">"))
|
|
|
|
->where(new Compare("UserToken.used", 0))
|
|
|
|
->execute();
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
|
|
|
|
if ($this->success && !empty($res)) {
|
|
|
|
return $res[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-22 21:50:58 +02:00
|
|
|
|
|
|
|
$token = $this->getParam('token');
|
2020-06-23 15:31:09 +02:00
|
|
|
$tokenEntry = $this->checkToken($token);
|
2020-06-22 21:50:58 +02:00
|
|
|
|
|
|
|
if ($this->success) {
|
2020-06-23 15:31:09 +02:00
|
|
|
if (!empty($tokenEntry)) {
|
2020-06-29 16:47:02 +02:00
|
|
|
$this->result["token"] = array(
|
|
|
|
"type" => $tokenEntry["token_type"]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->result["user"] = array(
|
|
|
|
"name" => $tokenEntry["name"],
|
|
|
|
"email" => $tokenEntry["email"],
|
2020-08-09 14:23:36 +02:00
|
|
|
"uid" => $tokenEntry["uid"],
|
|
|
|
"confirmed" => $tokenEntry["confirmed"]
|
2020-06-29 16:47:02 +02:00
|
|
|
);
|
2020-06-22 21:50:58 +02:00
|
|
|
} else {
|
|
|
|
return $this->createError("This token does not exist or is no longer valid");
|
2020-06-22 21:03:30 +02:00
|
|
|
}
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
|
|
|
return $this->success;
|
2020-06-22 21:03:30 +02:00
|
|
|
}
|
2020-06-22 21:50:58 +02:00
|
|
|
}
|
2020-06-22 21:03:30 +02:00
|
|
|
|
2020-06-23 17:55:52 +02:00
|
|
|
class Edit extends UserAPI {
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
2020-06-23 17:55:52 +02:00
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'id' => new Parameter('id', Parameter::TYPE_INT),
|
|
|
|
'username' => new StringType('username', 32, true, NULL),
|
2021-12-08 16:53:43 +01:00
|
|
|
'fullName' => new StringType('fullName', 64, true, NULL),
|
2020-06-24 01:09:08 +02:00
|
|
|
'email' => new Parameter('email', Parameter::TYPE_EMAIL, true, NULL),
|
2020-06-23 17:55:52 +02:00
|
|
|
'password' => new StringType('password', -1, true, NULL),
|
|
|
|
'groups' => new Parameter('groups', Parameter::TYPE_ARRAY, true, NULL),
|
2020-07-02 00:47:45 +02:00
|
|
|
'confirmed' => new Parameter('confirmed', Parameter::TYPE_BOOLEAN, true, NULL)
|
2020-06-23 17:55:52 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->loginRequired = true;
|
2021-11-11 14:25:26 +01:00
|
|
|
$this->forbidMethod("GET");
|
2020-06-23 17:55:52 +02:00
|
|
|
}
|
2020-06-22 21:15:41 +02:00
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-23 17:55:52 +02:00
|
|
|
|
|
|
|
$id = $this->getParam("id");
|
|
|
|
$user = $this->getUser($id);
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
if (empty($user)) {
|
|
|
|
return $this->createError("User not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$username = $this->getParam("username");
|
2021-12-08 16:53:43 +01:00
|
|
|
$fullName = $this->getParam("fullName");
|
2020-06-23 17:55:52 +02:00
|
|
|
$email = $this->getParam("email");
|
|
|
|
$password = $this->getParam("password");
|
|
|
|
$groups = $this->getParam("groups");
|
2020-07-02 00:47:45 +02:00
|
|
|
$confirmed = $this->getParam("confirmed");
|
2020-06-23 17:55:52 +02:00
|
|
|
|
2020-06-23 21:18:45 +02:00
|
|
|
$email = (!is_null($email) && empty($email)) ? null : $email;
|
|
|
|
|
2020-06-23 20:57:54 +02:00
|
|
|
$groupIds = array();
|
2020-06-23 17:55:52 +02:00
|
|
|
if (!is_null($groups)) {
|
2020-06-23 20:57:54 +02:00
|
|
|
$param = new Parameter('groupId', Parameter::TYPE_INT);
|
|
|
|
|
|
|
|
foreach($groups as $groupId) {
|
|
|
|
if (!$param->parseParam($groupId)) {
|
|
|
|
$value = print_r($groupId, true);
|
|
|
|
return $this->createError("Invalid Type for groupId in parameter groups: '$value' (Required: " . $param->getTypeName() . ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupIds[] = $param->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($id === $this->user->getId() && !in_array(USER_GROUP_ADMIN, $groupIds)) {
|
2020-06-23 17:55:52 +02:00
|
|
|
return $this->createError("Cannot remove Administrator group from own user.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicate username, email
|
2021-11-11 14:25:26 +01:00
|
|
|
$usernameChanged = !is_null($username) && strcasecmp($username, $user[0]["name"]) !== 0;
|
2021-12-08 16:53:43 +01:00
|
|
|
$fullNameChanged = !is_null($fullName) && strcasecmp($fullName, $user[0]["fullName"]) !== 0;
|
2021-11-11 14:25:26 +01:00
|
|
|
$emailChanged = !is_null($email) && strcasecmp($email, $user[0]["email"]) !== 0;
|
2020-06-23 17:55:52 +02:00
|
|
|
if($usernameChanged || $emailChanged) {
|
|
|
|
if (!$this->userExists($usernameChanged ? $username : NULL, $emailChanged ? $email : NULL)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$query = $sql->update("User");
|
|
|
|
|
|
|
|
if ($usernameChanged) $query->set("name", $username);
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($fullNameChanged) $query->set("fullName", $fullName);
|
2020-06-23 17:55:52 +02:00
|
|
|
if ($emailChanged) $query->set("email", $email);
|
2020-06-23 18:40:43 +02:00
|
|
|
if (!is_null($password)) $query->set("password", $this->hashPassword($password));
|
2020-06-23 17:55:52 +02:00
|
|
|
|
2020-07-02 00:47:45 +02:00
|
|
|
if (!is_null($confirmed)) {
|
|
|
|
if ($id === $this->user->getId() && $confirmed === false) {
|
|
|
|
return $this->createError("Cannot make own account unconfirmed.");
|
|
|
|
} else {
|
|
|
|
$query->set("confirmed", $confirmed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:57:54 +02:00
|
|
|
if (!empty($query->getValues())) {
|
|
|
|
$query->where(new Compare("User.uid", $id));
|
|
|
|
$res = $query->execute();
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
}
|
2020-06-23 17:55:52 +02:00
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
if ($this->success) {
|
2020-06-23 17:55:52 +02:00
|
|
|
|
|
|
|
$deleteQuery = $sql->delete("UserGroup")->where(new Compare("user_id", $id));
|
|
|
|
$insertQuery = $sql->insert("UserGroup", array("user_id", "group_id"));
|
|
|
|
|
2020-06-23 20:57:54 +02:00
|
|
|
foreach($groupIds as $groupId) {
|
|
|
|
$insertQuery->addRow($id, $groupId);
|
2020-06-23 17:55:52 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
$this->success = ($deleteQuery->execute() !== FALSE) && (empty($groupIds) || $insertQuery->execute() !== FALSE);
|
2020-06-23 17:55:52 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 22:24:56 +02:00
|
|
|
|
|
|
|
class Delete extends UserAPI {
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
2020-06-23 22:24:56 +02:00
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'id' => new Parameter('id', Parameter::TYPE_INT)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-23 22:24:56 +02:00
|
|
|
|
|
|
|
$id = $this->getParam("id");
|
|
|
|
if ($id === $this->user->getId()) {
|
|
|
|
return $this->createError("You cannot delete your own user.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->getUser($id);
|
|
|
|
if ($this->success) {
|
|
|
|
if (empty($user)) {
|
|
|
|
return $this->createError("User not found");
|
|
|
|
} else {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
2020-06-23 22:24:56 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->delete("User")->where(new Compare("uid", $id))->execute();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 16:47:02 +02:00
|
|
|
|
2020-07-02 00:47:45 +02:00
|
|
|
class RequestPasswordReset extends UserAPI {
|
2020-06-29 16:47:02 +02:00
|
|
|
public function __construct(User $user, $externalCall = false) {
|
|
|
|
$parameters = array(
|
|
|
|
'email' => new Parameter('email', Parameter::TYPE_EMAIL),
|
|
|
|
);
|
|
|
|
|
|
|
|
$settings = $user->getConfiguration()->getSettings();
|
|
|
|
if ($settings->isRecaptchaEnabled()) {
|
|
|
|
$parameters["captcha"] = new StringType("captcha");
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($user, $externalCall, $parameters);
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
|
|
|
|
if ($this->user->isLoggedIn()) {
|
|
|
|
return $this->createError("You already logged in.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings = $this->user->getConfiguration()->getSettings();
|
|
|
|
if ($settings->isRecaptchaEnabled()) {
|
|
|
|
$captcha = $this->getParam("captcha");
|
|
|
|
$req = new VerifyCaptcha($this->user);
|
|
|
|
if (!$req->execute(array("captcha" => $captcha, "action" => "resetPassword"))) {
|
|
|
|
return $this->createError($req->getLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = $this->getParam("email");
|
|
|
|
$user = $this->findUser($email);
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($this->success === false) {
|
2020-06-29 16:47:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user !== null) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$validHours = 1;
|
2020-06-29 16:47:02 +02:00
|
|
|
$token = generateRandomString(36);
|
2021-12-08 16:53:43 +01:00
|
|
|
if (!$this->insertToken($user["uid"], $token, "password_reset", $validHours)) {
|
2020-06-29 16:47:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$baseUrl = $settings->getBaseUrl();
|
|
|
|
$siteName = $settings->getSiteName();
|
2020-06-29 16:47:02 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$req = new Render($this->user);
|
|
|
|
$this->success = $req->execute([
|
|
|
|
"file" => "mail/reset_password.twig",
|
|
|
|
"parameters" => [
|
|
|
|
"link" => "$baseUrl/resetPassword?token=$token",
|
|
|
|
"site_name" => $siteName,
|
|
|
|
"base_url" => $baseUrl,
|
|
|
|
"username" => $user["name"],
|
|
|
|
"valid_time" => $this->formatDuration($validHours, "hour")
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$this->lastError = $req->getLastError();
|
2020-06-29 16:47:02 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($this->success) {
|
|
|
|
$messageBody = $req->getResult()["html"];
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
$gpgFingerprint = null;
|
|
|
|
if ($user["gpg_id"] && $user["gpg_confirmed"]) {
|
|
|
|
$gpgFingerprint = $user["gpg_fingerprint"];
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$request = new \Api\Mail\Send($this->user);
|
|
|
|
$this->success = $request->execute(array(
|
|
|
|
"to" => $email,
|
|
|
|
"subject" => "[$siteName] Password Reset",
|
2022-02-20 16:53:26 +01:00
|
|
|
"body" => $messageBody,
|
|
|
|
"gpgFingerprint" => $gpgFingerprint
|
2021-12-08 16:53:43 +01:00
|
|
|
));
|
|
|
|
$this->lastError = $request->getLastError();
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
private function findUser($email): ?array {
|
2020-06-29 16:47:02 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2022-02-20 16:53:26 +01:00
|
|
|
$res = $sql->select("User.uid", "User.name",
|
|
|
|
"User.gpg_id", "GpgKey.confirmed as gpg_confirmed", "GpgKey.fingerprint as gpg_fingerprint")
|
2020-07-02 00:47:45 +02:00
|
|
|
->from("User")
|
2022-02-20 16:53:26 +01:00
|
|
|
->leftJoin("GpgKey", "GpgKey.uid", "User.gpg_id")
|
2020-06-29 16:47:02 +02:00
|
|
|
->where(new Compare("User.email", $email))
|
|
|
|
->where(new CondBool("User.confirmed"))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if ($this->success) {
|
2021-12-08 16:53:43 +01:00
|
|
|
if (!empty($res)) {
|
2020-06-29 16:47:02 +02:00
|
|
|
return $res[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
return null;
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
2021-11-11 14:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ResendConfirmEmail extends UserAPI {
|
|
|
|
public function __construct(User $user, $externalCall = false) {
|
|
|
|
$parameters = array(
|
|
|
|
'email' => new Parameter('email', Parameter::TYPE_EMAIL),
|
|
|
|
);
|
|
|
|
|
|
|
|
$settings = $user->getConfiguration()->getSettings();
|
|
|
|
if ($settings->isRecaptchaEnabled()) {
|
|
|
|
$parameters["captcha"] = new StringType("captcha");
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($user, $externalCall, $parameters);
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2021-11-11 14:25:26 +01:00
|
|
|
|
|
|
|
if ($this->user->isLoggedIn()) {
|
|
|
|
return $this->createError("You already logged in.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings = $this->user->getConfiguration()->getSettings();
|
|
|
|
if ($settings->isRecaptchaEnabled()) {
|
|
|
|
$captcha = $this->getParam("captcha");
|
|
|
|
$req = new VerifyCaptcha($this->user);
|
|
|
|
if (!$req->execute(array("captcha" => $captcha, "action" => "resendConfirmation"))) {
|
|
|
|
return $this->createError($req->getLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = $this->getParam("email");
|
2020-06-29 16:47:02 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2021-11-11 14:25:26 +01:00
|
|
|
$res = $sql->select("User.uid", "User.name", "UserToken.token", "UserToken.token_type", "UserToken.used")
|
|
|
|
->from("User")
|
|
|
|
->leftJoin("UserToken", "User.uid", "UserToken.user_id")
|
|
|
|
->where(new Compare("User.email", $email))
|
|
|
|
->where(new Compare("User.confirmed", false))
|
2020-06-29 16:47:02 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
2021-11-11 14:25:26 +01:00
|
|
|
if (!$this->success) {
|
|
|
|
return $this->createError($sql->getLastError());
|
|
|
|
} else if (!is_array($res) || empty($res)) {
|
|
|
|
// user does not exist
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $res[0]["uid"];
|
|
|
|
$token = current(
|
|
|
|
array_map(function ($row) {
|
|
|
|
return $row["token"];
|
|
|
|
}, array_filter($res, function ($row) use ($sql) {
|
|
|
|
return !$sql->parseBool($row["used"]) && $row["token_type"] === "email_confirm";
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$validHours = 48;
|
2021-11-11 14:25:26 +01:00
|
|
|
if (!$token) {
|
|
|
|
// no token generated yet, let's generate one
|
|
|
|
$token = generateRandomString(36);
|
2021-12-08 16:53:43 +01:00
|
|
|
if (!$this->insertToken($userId, $token, "email_confirm", $validHours)) {
|
2021-11-11 14:25:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
} else {
|
|
|
|
$sql->update("UserToken")
|
|
|
|
->set("valid_until", (new DateTime())->modify("+$validHours hour"))
|
|
|
|
->where(new Compare("token", $token))
|
|
|
|
->execute();
|
2021-11-11 14:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$username = $res[0]["name"];
|
2021-12-08 16:53:43 +01:00
|
|
|
$baseUrl = $settings->getBaseUrl();
|
|
|
|
$siteName = $settings->getSiteName();
|
|
|
|
|
|
|
|
$req = new Render($this->user);
|
|
|
|
$this->success = $req->execute([
|
|
|
|
"file" => "mail/confirm_email.twig",
|
|
|
|
"parameters" => [
|
|
|
|
"link" => "$baseUrl/confirmEmail?token=$token",
|
|
|
|
"site_name" => $siteName,
|
|
|
|
"base_url" => $baseUrl,
|
|
|
|
"username" => $username,
|
|
|
|
"valid_time" => $this->formatDuration($validHours, "hour")
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$this->lastError = $req->getLastError();
|
2021-11-11 14:25:26 +01:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($this->success) {
|
|
|
|
$messageBody = $req->getResult()["html"];
|
|
|
|
$request = new \Api\Mail\Send($this->user);
|
|
|
|
$this->success = $request->execute(array(
|
|
|
|
"to" => $email,
|
|
|
|
"subject" => "[$siteName] E-Mail Confirmation",
|
|
|
|
"body" => $messageBody
|
|
|
|
));
|
2021-11-11 14:25:26 +01:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->lastError = $request->getLastError();
|
|
|
|
}
|
2021-11-11 14:25:26 +01:00
|
|
|
|
2020-06-29 16:47:02 +02:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResetPassword extends UserAPI {
|
|
|
|
|
|
|
|
public function __construct(User $user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'token' => new StringType('token', 36),
|
|
|
|
'password' => new StringType('password'),
|
|
|
|
'confirmPassword' => new StringType('confirmPassword'),
|
|
|
|
));
|
2020-07-02 00:47:45 +02:00
|
|
|
|
|
|
|
$this->csrfTokenRequired = false;
|
2020-06-29 16:47:02 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
private function updateUser($uid, $password): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->update("User")
|
|
|
|
->set("password", $this->hashPassword($password))
|
|
|
|
->where(new Compare("uid", $uid))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2020-06-29 16:47:02 +02:00
|
|
|
|
|
|
|
if ($this->user->isLoggedIn()) {
|
|
|
|
return $this->createError("You are already logged in.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->getParam("token");
|
|
|
|
$password = $this->getParam("password");
|
|
|
|
$confirmPassword = $this->getParam("confirmPassword");
|
|
|
|
|
|
|
|
$req = new CheckToken($this->user);
|
|
|
|
$this->success = $req->execute(array("token" => $token));
|
|
|
|
$this->lastError = $req->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $req->getResult();
|
2020-07-02 00:47:45 +02:00
|
|
|
if (strcasecmp($result["token"]["type"], "password_reset") !== 0) {
|
2020-06-29 16:47:02 +02:00
|
|
|
return $this->createError("Invalid token type");
|
|
|
|
} else if (!$this->checkPasswordRequirements($password, $confirmPassword)) {
|
|
|
|
return false;
|
|
|
|
} else if (!$this->updateUser($result["user"]["uid"], $password)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2020-06-29 16:50:36 +02:00
|
|
|
$this->invalidateToken($token);
|
2020-06-29 16:47:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 14:25:26 +01:00
|
|
|
|
|
|
|
class UpdateProfile extends UserAPI {
|
|
|
|
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'username' => new StringType('username', 32, true, NULL),
|
2021-12-08 16:53:43 +01:00
|
|
|
'fullName' => new StringType('fullName', 64, true, NULL),
|
2021-11-11 14:25:26 +01:00
|
|
|
'password' => new StringType('password', -1, true, NULL),
|
2021-12-08 16:53:43 +01:00
|
|
|
'confirmPassword' => new StringType('confirmPassword', -1, true, NULL),
|
|
|
|
'oldPassword' => new StringType('oldPassword', -1, true, NULL),
|
2021-11-11 14:25:26 +01:00
|
|
|
));
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->csrfTokenRequired = true;
|
|
|
|
$this->forbidMethod("GET");
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2021-11-11 14:25:26 +01:00
|
|
|
|
|
|
|
$newUsername = $this->getParam("username");
|
2021-12-08 16:53:43 +01:00
|
|
|
$oldPassword = $this->getParam("oldPassword");
|
2021-11-11 14:25:26 +01:00
|
|
|
$newPassword = $this->getParam("password");
|
2021-12-08 16:53:43 +01:00
|
|
|
$newPasswordConfirm = $this->getParam("confirmPassword");
|
|
|
|
$newFullName = $this->getParam("fullName");
|
2021-11-11 14:25:26 +01:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($newUsername === null && $newPassword === null && $newPasswordConfirm === null && $newFullName === null) {
|
|
|
|
return $this->createError("You must either provide an updated username, fullName or password");
|
2021-11-11 14:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
2021-12-08 16:53:43 +01:00
|
|
|
$query = $sql->update("User")->where(new Compare("uid", $this->user->getId()));
|
2021-11-11 14:25:26 +01:00
|
|
|
if ($newUsername !== null) {
|
|
|
|
if (!$this->checkUsernameRequirements($newUsername) || $this->userExists($newUsername)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$query->set("name", $newUsername);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
if ($newFullName !== null) {
|
|
|
|
$query->set("fullName", $newFullName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($newPassword !== null || $newPasswordConfirm !== null) {
|
|
|
|
if (!$this->checkPasswordRequirements($newPassword, $newPasswordConfirm)) {
|
2021-11-11 14:25:26 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2021-12-08 16:53:43 +01:00
|
|
|
$res = $sql->select("password")
|
|
|
|
->from("User")
|
|
|
|
->where(new Compare("uid", $this->user->getId()))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password_verify($oldPassword, $res[0]["password"])) {
|
|
|
|
return $this->createError("Wrong password");
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
$query->set("password", $this->hashPassword($newPassword));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->success = $query->execute();
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
class ImportGPG extends UserAPI {
|
|
|
|
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
"pubkey" => new StringType("pubkey")
|
|
|
|
));
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->forbidMethod("GET");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function testKey(string $keyString) {
|
|
|
|
$res = GpgKey::getKeyInfo($keyString);
|
|
|
|
if (!$res["success"]) {
|
|
|
|
return $this->createError($res["error"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$keyData = $res["data"];
|
|
|
|
$keyType = $keyData["type"];
|
|
|
|
$expires = $keyData["expires"];
|
|
|
|
|
|
|
|
if ($keyType === "sec#") {
|
|
|
|
return self::createError("ATTENTION! It seems like you've imported a PGP PRIVATE KEY instead of a public key.
|
|
|
|
It is recommended to immediately revoke your private key and create a new key pair.");
|
|
|
|
} else if ($keyType !== "pub") {
|
|
|
|
return self::createError("Unknown key type: $keyType");
|
|
|
|
} else if (isInPast($expires)) {
|
|
|
|
return self::createError("It seems like the gpg key is already expired.");
|
|
|
|
} else {
|
|
|
|
return $keyData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
$gpgKey = $this->user->getGPG();
|
|
|
|
if ($gpgKey) {
|
|
|
|
return $this->createError("You already added a GPG key to your account.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// fix key first, enforce a newline after
|
|
|
|
$keyString = $this->getParam("pubkey");
|
|
|
|
$keyString = preg_replace("/(-{2,})\n([^\n])/", "$1\n\n$2", $keyString);
|
|
|
|
$keyData = $this->testKey($keyString);
|
|
|
|
if ($keyData === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = GpgKey::importKey($keyString);
|
|
|
|
if (!$res["success"]) {
|
|
|
|
return $this->createError($res["error"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->insert("GpgKey", ["fingerprint", "algorithm", "expires"])
|
|
|
|
->addRow($keyData["fingerprint"], $keyData["algorithm"], $keyData["expires"])
|
|
|
|
->returning("uid")
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gpgKeyId = $sql->getLastInsertId();
|
|
|
|
$res = $sql->update("User")
|
|
|
|
->set("gpg_id", $gpgKeyId)
|
|
|
|
->where(new Compare("uid", $this->user->getId()))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = generateRandomString(36);
|
|
|
|
$res = $sql->insert("UserToken", ["user_id", "token", "token_type", "valid_until"])
|
|
|
|
->addRow($this->user->getId(), $token, "gpg_confirm", (new DateTime())->modify("+1 hour"))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = htmlspecialchars($this->user->getFullName());
|
|
|
|
if (!$name) {
|
|
|
|
$name = htmlspecialchars($this->user->getUsername());
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings = $this->user->getConfiguration()->getSettings();
|
|
|
|
$baseUrl = htmlspecialchars($settings->getBaseUrl());
|
|
|
|
$token = htmlspecialchars(urlencode($token));
|
|
|
|
$mailBody = "Hello $name,<br><br>" .
|
|
|
|
"you imported a GPG public key for end-to-end encrypted mail communication. " .
|
|
|
|
"To confirm the key and verify, you own the corresponding private key, please click on the following link. " .
|
|
|
|
"The link is active for one hour.<br><br>" .
|
|
|
|
"<a href='$baseUrl/confirmGPG?token=$token'>$baseUrl/settings?confirmGPG&token=$token</a><br>
|
|
|
|
Best Regards<br>
|
|
|
|
ilum:e Security Lab";
|
|
|
|
|
|
|
|
$sendMail = new \Api\Mail\Send($this->user);
|
|
|
|
$this->success = $sendMail->execute(array(
|
|
|
|
"to" => $this->user->getEmail(),
|
|
|
|
"subject" => "Security Lab - Confirm GPG-Key",
|
|
|
|
"body" => $mailBody,
|
|
|
|
"gpgFingerprint" => $keyData["fingerprint"]
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->lastError = $sendMail->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
$this->result["gpg"] = array(
|
|
|
|
"fingerprint" => $keyData["fingerprint"],
|
|
|
|
"confirmed" => false,
|
|
|
|
"algorithm" => $keyData["algorithm"],
|
|
|
|
"expires" => $keyData["expires"]->getTimestamp()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemoveGPG extends UserAPI {
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
"password" => new StringType("password")
|
|
|
|
));
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->forbidMethod("GET");
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
$gpgKey = $this->user->getGPG();
|
|
|
|
if (!$gpgKey) {
|
|
|
|
return $this->createError("You have not added a GPG public key to your account yet.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select("password")
|
|
|
|
->from("User")
|
|
|
|
->where(new Compare("User.uid", $this->user->getId()))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success && is_array($res)) {
|
|
|
|
$hash = $res[0]["password"];
|
|
|
|
$password = $this->getParam("password");
|
|
|
|
if (!password_verify($password, $hash)) {
|
|
|
|
return $this->createError("Incorrect password.");
|
|
|
|
} else {
|
|
|
|
$res = $sql->delete("GpgKey")
|
|
|
|
->where(new Compare("uid",
|
|
|
|
$sql->select("User.gpg_id")
|
|
|
|
->from("User")
|
|
|
|
->where(new Compare("User.uid", $this->user->getId()))
|
|
|
|
))->execute();
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConfirmGPG extends UserAPI {
|
|
|
|
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, [
|
|
|
|
"token" => new StringType("token", 36)
|
|
|
|
]);
|
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
$gpgKey = $this->user->getGPG();
|
|
|
|
if (!$gpgKey) {
|
|
|
|
return $this->createError("You have not added a GPG key yet.");
|
|
|
|
} else if ($gpgKey->isConfirmed()) {
|
|
|
|
return $this->createError("Your GPG key is already confirmed");
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->getParam("token");
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select($sql->count())
|
|
|
|
->from("UserToken")
|
|
|
|
->where(new Compare("token", $token))
|
|
|
|
->where(new Compare("valid_until", $sql->now(), ">="))
|
|
|
|
->where(new Compare("user_id", $this->user->getId()))
|
|
|
|
->where(new Compare("token_type", "gpg_confirm"))
|
|
|
|
->where(new CondNot(new CondBool("used")))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success && is_array($res)) {
|
|
|
|
if ($res[0]["count"] === 0) {
|
|
|
|
return $this->createError("Invalid token");
|
|
|
|
} else {
|
|
|
|
$res = $sql->update("GpgKey")
|
|
|
|
->set("confirmed", 1)
|
|
|
|
->where(new Compare("uid", $gpgKey->getId()))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $sql->update("UserToken")
|
|
|
|
->set("used", 1)
|
|
|
|
->where(new Compare("token", $token))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DownloadGPG extends UserAPI {
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
"id" => new Parameter("id", Parameter::TYPE_INT, true, null),
|
|
|
|
"format" => new StringType("format", 16, true, "ascii")
|
|
|
|
));
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->csrfTokenRequired = false;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
$allowedFormats = ["json", "ascii", "gpg"];
|
|
|
|
$format = $this->getParam("format");
|
|
|
|
if (!in_array($format, $allowedFormats)) {
|
|
|
|
return $this->getParam("Invalid requested format. Allowed formats: " . implode(",", $allowedFormats));
|
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $this->getParam("id");
|
|
|
|
if ($userId === null || $userId == $this->user->getId()) {
|
|
|
|
$gpgKey = $this->user->getGPG();
|
|
|
|
if (!$gpgKey) {
|
|
|
|
return $this->createError("You did not add a gpg key yet.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = $this->user->getEmail();
|
|
|
|
$gpgFingerprint = $gpgKey->getFingerprint();
|
|
|
|
} else {
|
|
|
|
$req = new Get($this->user);
|
|
|
|
$this->success = $req->execute(["id" => $userId]);
|
|
|
|
$this->lastError = $req->getLastError();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $req->getResult()["user"];
|
|
|
|
$email = $res["email"];
|
|
|
|
$gpgFingerprint = $res["gpgFingerprint"];
|
|
|
|
if (!$gpgFingerprint) {
|
|
|
|
return $this->createError("This user has not added a gpg key yet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = GpgKey::export($gpgFingerprint, $format !== "gpg");
|
|
|
|
if (!$res["success"]) {
|
|
|
|
return $this->createError($res["error"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $res["data"];
|
|
|
|
if ($format === "json") {
|
|
|
|
$this->result["key"] = $key;
|
|
|
|
return true;
|
|
|
|
} else if ($format === "ascii") {
|
|
|
|
$contentType = "application/pgp-keys";
|
|
|
|
$ext = "asc";
|
|
|
|
} else if ($format === "gpg") {
|
|
|
|
$contentType = "application/octet-stream";
|
|
|
|
$ext = "gpg";
|
|
|
|
} else {
|
|
|
|
die("Invalid format");
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = "$email.$ext";
|
|
|
|
header("Content-Type: $contentType");
|
|
|
|
header("Content-Length: " . strlen($key));
|
|
|
|
header("Content-Disposition: attachment; filename=\"$fileName\"");
|
|
|
|
die($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
class UploadPicture extends UserAPI {
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, [
|
|
|
|
"scale" => new Parameter("scale", Parameter::TYPE_FLOAT, true, NULL),
|
|
|
|
]);
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->forbidMethod("GET");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ImagickException
|
|
|
|
*/
|
|
|
|
protected function onTransform(\Imagick $im, $uploadDir) {
|
|
|
|
|
|
|
|
$minSize = 75;
|
|
|
|
$maxSize = 500;
|
|
|
|
|
|
|
|
$width = $im->getImageWidth();
|
|
|
|
$height = $im->getImageHeight();
|
|
|
|
$doResize = false;
|
|
|
|
|
|
|
|
if ($width < $minSize || $height < $minSize) {
|
|
|
|
if ($width < $height) {
|
|
|
|
$newWidth = $minSize;
|
|
|
|
$newHeight = intval(($minSize / $width) * $height);
|
|
|
|
} else {
|
|
|
|
$newHeight = $minSize;
|
|
|
|
$newWidth = intval(($minSize / $height) * $width);
|
|
|
|
}
|
|
|
|
|
|
|
|
$doResize = true;
|
|
|
|
} else if ($width > $maxSize || $height > $maxSize) {
|
|
|
|
if ($width > $height) {
|
|
|
|
$newWidth = $maxSize;
|
|
|
|
$newHeight = intval($height * ($maxSize / $width));
|
|
|
|
} else {
|
|
|
|
$newHeight = $maxSize;
|
|
|
|
$newWidth = intval($width * ($maxSize / $height));
|
|
|
|
}
|
|
|
|
|
|
|
|
$doResize = true;
|
|
|
|
} else {
|
|
|
|
$newWidth = $width;
|
|
|
|
$newHeight = $height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($width < $minSize || $height < $minSize) {
|
|
|
|
return $this->createError("Error processing image. Bad dimensions.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($doResize) {
|
|
|
|
$width = $newWidth;
|
|
|
|
$height = $newHeight;
|
|
|
|
$im->resizeImage($width, $height, \Imagick::FILTER_SINC, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$size = $this->getParam("size");
|
|
|
|
if (is_null($size)) {
|
|
|
|
$size = min($width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = [$this->getParam("offsetX"), $this->getParam("offsetY")];
|
|
|
|
if ($size < $minSize or $size > $maxSize) {
|
|
|
|
return $this->createError("Invalid size. Must be in range of $minSize-$maxSize.");
|
|
|
|
}/* else if ($offset[0] < 0 || $offset[1] < 0 || $offset[0]+$size > $width || $offset[1]+$size > $height) {
|
|
|
|
return $this->createError("Offsets out of bounds.");
|
|
|
|
}*/
|
|
|
|
|
|
|
|
if ($offset[0] !== 0 || $offset[1] !== 0 || $size !== $width || $size !== $height) {
|
|
|
|
$im->cropImage($size, $size, $offset[0], $offset[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = uuidv4() . ".jpg";
|
|
|
|
$im->writeImage("$uploadDir/$fileName");
|
|
|
|
$im->destroy();
|
|
|
|
return $fileName;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2021-12-08 16:53:43 +01:00
|
|
|
|
|
|
|
$userId = $this->user->getId();
|
|
|
|
$uploadDir = WEBROOT . "/img/uploads/user/$userId";
|
|
|
|
list ($fileName, $imageName) = $this->processImageUpload($uploadDir, ["png","jpg","jpeg"], "onTransform");
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldPfp = $this->user->getProfilePicture();
|
|
|
|
if ($oldPfp) {
|
|
|
|
$path = "$uploadDir/$oldPfp";
|
|
|
|
if (is_file($path)) {
|
|
|
|
@unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$this->success = $sql->update("User")
|
|
|
|
->set("profilePicture", $fileName)
|
|
|
|
->where(new Compare("uid", $userId))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
if ($this->success) {
|
|
|
|
$this->result["profilePicture"] = $fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemovePicture extends UserAPI {
|
|
|
|
public function __construct(User $user, bool $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, []);
|
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2021-12-08 16:53:43 +01:00
|
|
|
|
|
|
|
$pfp = $this->user->getProfilePicture();
|
|
|
|
if (!$pfp) {
|
|
|
|
return $this->createError("You did not upload a profile picture yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $this->user->getId();
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$this->success = $sql->update("User")
|
|
|
|
->set("profilePicture", NULL)
|
|
|
|
->where(new Compare("uid", $userId))
|
|
|
|
->execute();
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
$path = WEBROOT . "/img/uploads/user/$userId/$pfp";
|
|
|
|
if (is_file($path)) {
|
|
|
|
@unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|