2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Methods to manage user. Not a standard controller as it has nothing to do with user based restrictions and must be usable only by admin.
|
2019-11-14 02:02:50 +01:00
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
class User extends \descartes\InternalController
|
|
|
|
{
|
2021-06-12 23:23:15 +02:00
|
|
|
private $bdd;
|
2019-10-29 14:57:13 +01:00
|
|
|
private $model_user;
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_event;
|
2020-01-17 18:36:53 +01:00
|
|
|
private $internal_setting;
|
2020-04-03 03:38:35 +02:00
|
|
|
private $internal_phone;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
public function __construct(\PDO $bdd)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-06-12 23:23:15 +02:00
|
|
|
$this->bdd = $bdd;
|
2019-10-29 14:57:13 +01:00
|
|
|
$this->model_user = new \models\User($bdd);
|
|
|
|
$this->internal_event = new \controllers\internals\Event($bdd);
|
2020-01-06 23:33:27 +01:00
|
|
|
$this->internal_setting = new \controllers\internals\Setting($bdd);
|
2020-04-03 03:38:35 +02:00
|
|
|
$this->internal_phone = new Phone($bdd);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Return a list of users by their ids.
|
2021-06-12 23:23:15 +02:00
|
|
|
*
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param array $ids : ids of entries to find
|
2021-06-12 23:23:15 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_in_by_id(array $ids)
|
|
|
|
{
|
|
|
|
return $this->model_user->gets_in_by_id($ids);
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Return list of users as an array.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param mixed(int|bool) $nb_entry : Number of entry to return
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param mixed(int|bool) $page : Numero of page
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array|bool : List of user or false
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function list(?int $nb_entry = null, ?int $page = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-12 17:37:20 +01:00
|
|
|
return $this->model_user->list($nb_entry, $page * $nb_entry);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-11-14 02:02:50 +01:00
|
|
|
* Delete a user.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param array $ids : Les id des useres à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param mixed $id
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return int : Number of users deleted
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function delete($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
return $this->model_user->remove($id);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Check user credentials.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param string $email : User email
|
|
|
|
* @param string $password : User password
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return mixed false | array : False if no user for thoses credentials, the user else
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function check_credentials($email, $password)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$user = $this->model_user->get_by_email($email);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$user)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!password_verify($password, $user['password']))
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Update a user password.
|
|
|
|
*
|
|
|
|
* @param string $id : User id
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param string $password : New password
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return bool;
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-10-30 00:30:39 +01:00
|
|
|
public function update_password(int $id, string $password): bool
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$password = password_hash($password, PASSWORD_DEFAULT);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return (bool) $this->model_user->update_password($id, $password);
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Update user email.
|
|
|
|
*
|
|
|
|
* @param string $id : user id
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param string $email : new mail
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function update_email($id, $email)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return (bool) $this->model_user->update_email($id, $email);
|
|
|
|
}
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-03-30 01:52:53 +02:00
|
|
|
/**
|
|
|
|
* Update user status.
|
|
|
|
*
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param string $id : user id
|
2020-03-30 01:52:53 +02:00
|
|
|
* @param string $status : new status
|
|
|
|
*
|
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function update_status($id, $status)
|
|
|
|
{
|
|
|
|
return (bool) $this->model_user->update($id, ['status' => $status]);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
|
|
|
* Update user api key.
|
|
|
|
*
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param string $id : user id
|
2020-01-08 02:14:38 +01:00
|
|
|
* @param ?string $api_key : new api key
|
|
|
|
*
|
|
|
|
* @return mixed : false on error, else new api key;
|
|
|
|
*/
|
|
|
|
public function update_api_key($id, ?string $api_key = null)
|
|
|
|
{
|
|
|
|
$api_key = $api_key ?? $this->generate_random_api_key();
|
|
|
|
$success = $this->model_user->update($id, ['api_key' => $api_key]);
|
|
|
|
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $api_key;
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get a user by his email address.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param string $email : User email
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return mixed boolean | array : false if cannot find user for this email, the user else
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function get_by_email($email)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->model_user->get_by_email($email);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 14:14:40 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find a user by his id.
|
|
|
|
*
|
2020-01-08 14:14:40 +01:00
|
|
|
* @param string $id : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-08 14:14:40 +01:00
|
|
|
* @return mixed array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get($id)
|
2020-01-08 14:14:40 +01:00
|
|
|
{
|
|
|
|
return $this->model_user->get($id);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get a user by his api_key address.
|
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @param string $api_key : User api key
|
|
|
|
*
|
|
|
|
* @return mixed boolean | array : false if cannot find user for this api key, the user else
|
|
|
|
*/
|
|
|
|
public function get_by_api_key(string $api_key)
|
|
|
|
{
|
|
|
|
return $this->model_user->get_by_api_key($api_key);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a user by his id.
|
|
|
|
*
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param mixed $id : User id
|
|
|
|
* @param array $user : Array of fields to update for user
|
2021-06-12 23:23:15 +02:00
|
|
|
* @param mixed (?array|bool) $quota : Quota to update for the user, by default null -> no update, if false, remove quota
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2021-06-12 23:23:15 +02:00
|
|
|
* @return bool : True on success, false on error
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2021-06-12 23:23:15 +02:00
|
|
|
public function update($id, array $user, $quota = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-06-12 23:23:15 +02:00
|
|
|
$internal_quota = new Quota($this->bdd);
|
|
|
|
$current_quota = $internal_quota->get_user_quota($id);
|
|
|
|
|
|
|
|
$this->bdd->beginTransaction();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
$this->model_user->update($id, $user);
|
|
|
|
|
2021-06-17 00:51:33 +02:00
|
|
|
if ($current_quota && false === $quota)
|
2021-06-12 23:23:15 +02:00
|
|
|
{
|
|
|
|
$success = $internal_quota->delete_for_user($id, $current_quota['id']);
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
$this->bdd->rollback();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($quota)
|
|
|
|
{
|
|
|
|
if ($current_quota)
|
|
|
|
{
|
|
|
|
$internal_quota->update_for_user($id, $current_quota['id'], $quota);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$success = $internal_quota->create($id, $quota['credit'], $quota['additional'], $quota['report_unused'], $quota['report_unused_additional'], $quota['auto_renew'], $quota['renew_interval'], new \DateTime($quota['start_date']), new \DateTime($quota['expiration_date']));
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
$this->bdd->rollback();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->bdd->commit())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
return true;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Create a new user.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param mixed $email
|
|
|
|
* @param mixed $password
|
|
|
|
* @param mixed $admin
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param ?string $api_key : The api key of the user, if null generate randomly
|
|
|
|
* @param string $status : User status, default \models\User::STATUS_ACTIVE
|
|
|
|
* @param bool $encrypt_password : Should the password be encrypted, by default true
|
2021-06-12 23:23:15 +02:00
|
|
|
* @param ?array $quota : Quota to create for the user, by default null -> no quota
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return mixed bool|int : false on error, id of the new user else
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2021-06-12 23:23:15 +02:00
|
|
|
public function create($email, $password, $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE, bool $encrypt_password = true, ?array $quota = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$user = [
|
|
|
|
'email' => $email,
|
2020-04-08 23:34:15 +02:00
|
|
|
'password' => $encrypt_password ? password_hash($password, PASSWORD_DEFAULT) : $password,
|
2019-10-29 14:57:13 +01:00
|
|
|
'admin' => $admin,
|
2020-01-08 02:14:38 +01:00
|
|
|
'api_key' => $api_key ?? $this->generate_random_api_key(),
|
2020-03-30 01:52:53 +02:00
|
|
|
'status' => $status,
|
2019-10-29 14:57:13 +01:00
|
|
|
];
|
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
$this->bdd->beginTransaction();
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
$new_id_user = $this->model_user->insert($user);
|
2020-04-02 19:10:54 +02:00
|
|
|
if (!$new_id_user)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
$success = $this->internal_setting->create_defaults_for_user($new_id_user);
|
2020-01-06 23:33:27 +01:00
|
|
|
if (!$success)
|
|
|
|
{
|
2021-06-12 23:23:15 +02:00
|
|
|
$this->bdd->rollback();
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-06 23:33:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:51:33 +02:00
|
|
|
if (null !== $quota)
|
2021-06-12 23:23:15 +02:00
|
|
|
{
|
|
|
|
$internal_quota = new Quota($this->bdd);
|
|
|
|
$success = $internal_quota->create($new_id_user, $quota['credit'], $quota['additional'], $quota['report_unused'], $quota['report_unused_additional'], $quota['auto_renew'], $quota['renew_interval'], $quota['start_date'], $quota['expiration_date']);
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
$this->bdd->rollback();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->bdd->commit())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-02 19:10:54 +02:00
|
|
|
return $new_id_user;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2020-01-08 02:14:38 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Generate a random api key.
|
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @return string : The api key
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function generate_random_api_key(): string
|
2020-01-08 02:14:38 +01:00
|
|
|
{
|
|
|
|
return bin2hex(random_bytes(16));
|
|
|
|
}
|
2020-04-02 18:40:39 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-23 21:06:13 +02:00
|
|
|
* Transfer a received sms to user email.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2020-04-02 18:40:39 +02:00
|
|
|
* @param array $received : [
|
2020-06-23 21:06:13 +02:00
|
|
|
* int 'id' => sms id,
|
|
|
|
* string 'at' => sms reception date,
|
|
|
|
* string 'text' => sms content,
|
|
|
|
* string 'destination' => id of phone the sms was sent to
|
|
|
|
* string 'origin' => phone number that sent the sms
|
2021-03-26 23:32:29 +01:00
|
|
|
* bool 'mms' => is the sms a mms
|
2020-06-23 21:06:13 +02:00
|
|
|
* ]
|
2020-04-02 18:40:39 +02:00
|
|
|
*
|
|
|
|
* @return bool : False if no transfer, true else
|
|
|
|
*/
|
2020-06-23 21:06:13 +02:00
|
|
|
public function transfer_received(int $id_user, array $received): bool
|
2020-04-02 18:40:39 +02:00
|
|
|
{
|
2020-04-03 03:38:35 +02:00
|
|
|
$settings = $this->internal_setting->gets_for_user($id_user);
|
2020-04-02 18:40:39 +02:00
|
|
|
|
|
|
|
if (!$settings['transfer'] ?? false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->get($id_user);
|
|
|
|
if (!$user)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:49:36 +02:00
|
|
|
$phone = $this->internal_phone->get_for_user($id_user, $received['destination']);
|
2020-04-02 18:40:39 +02:00
|
|
|
if (!$phone)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mailer = new Mailer();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-04-03 18:19:08 +02:00
|
|
|
$attachments = [];
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-04-03 18:19:08 +02:00
|
|
|
foreach ($received['medias'] ?? [] as $media)
|
|
|
|
{
|
|
|
|
$attachments[] = PWD_DATA_PUBLIC . '/' . $media['path'];
|
|
|
|
}
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 18:40:39 +02:00
|
|
|
return $mailer->enqueue($user['email'], EMAIL_TRANSFER_SMS, [
|
|
|
|
'at' => $received['at'],
|
|
|
|
'origin' => $received['origin'],
|
|
|
|
'destination' => $phone['name'],
|
|
|
|
'text' => $received['text'],
|
2021-04-03 18:19:08 +02:00
|
|
|
'mms' => $received['mms'] ?? false,
|
|
|
|
], $attachments);
|
2020-04-02 18:40:39 +02:00
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|