2019-12-08 02:33:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Class to call the console scripts.
|
2019-12-08 02:33:53 +01:00
|
|
|
*/
|
|
|
|
class Console extends \descartes\InternalController
|
|
|
|
{
|
2020-01-07 01:31:34 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Start launcher daemon.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function launcher()
|
2019-12-08 02:33:53 +01:00
|
|
|
{
|
2020-01-08 18:20:17 +01:00
|
|
|
new \daemons\Launcher();
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|
2019-12-17 14:38:16 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Start sender daemon.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function sender()
|
2019-12-17 14:38:16 +01:00
|
|
|
{
|
2020-01-07 01:31:34 +01:00
|
|
|
new \daemons\Sender();
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Start webhook daemon.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function webhook()
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
|
|
|
new \daemons\Webhook();
|
|
|
|
}
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-16 07:50:30 +02:00
|
|
|
/**
|
|
|
|
* Start mailer daemon.
|
|
|
|
*/
|
|
|
|
public function mailer()
|
|
|
|
{
|
|
|
|
new \daemons\Mailer();
|
|
|
|
}
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Start a phone daemon.
|
|
|
|
*
|
2020-01-07 01:31:34 +01:00
|
|
|
* @param $id_phone : Phone id
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function phone($id_phone)
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2020-01-07 01:31:34 +01:00
|
|
|
$internal_phone = new \controllers\internals\Phone($bdd);
|
|
|
|
|
|
|
|
$phone = $internal_phone->get($id_phone);
|
|
|
|
if (!$phone)
|
|
|
|
{
|
2020-03-04 03:28:34 +01:00
|
|
|
exit(1);
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
new \daemons\Phone($phone);
|
2019-12-17 14:38:16 +01:00
|
|
|
}
|
2020-02-19 02:32:52 +01:00
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
/**
|
2021-01-14 03:32:17 +01:00
|
|
|
* Check if a user exists based on email.
|
|
|
|
*
|
|
|
|
* @param string $email : User email
|
2020-10-22 20:31:01 +02:00
|
|
|
*/
|
|
|
|
public function user_exists(string $email)
|
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2020-10-22 20:31:01 +02:00
|
|
|
$internal_user = new \controllers\internals\User($bdd);
|
|
|
|
|
|
|
|
$user = $internal_user->get_by_email($email);
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
exit($user ? 0 : 1);
|
|
|
|
}
|
2022-03-15 02:24:28 +01:00
|
|
|
|
2021-08-05 17:14:53 +02:00
|
|
|
/**
|
|
|
|
* Check if a user exists based on id.
|
|
|
|
*
|
|
|
|
* @param string $id : User id
|
|
|
|
*/
|
|
|
|
public function user_id_exists(string $id)
|
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2021-08-05 17:14:53 +02:00
|
|
|
$internal_user = new \controllers\internals\User($bdd);
|
|
|
|
|
|
|
|
$user = $internal_user->get($id);
|
|
|
|
|
|
|
|
exit($user ? 0 : 1);
|
|
|
|
}
|
2020-10-22 20:31:01 +02:00
|
|
|
|
2020-02-19 02:32:52 +01:00
|
|
|
/**
|
2020-03-04 01:40:47 +01:00
|
|
|
* Create a user or update an existing user.
|
|
|
|
*
|
2020-02-19 02:32:52 +01:00
|
|
|
* @param $email : User email
|
|
|
|
* @param $password : User password
|
|
|
|
* @param $admin : Is user admin
|
2020-03-04 01:40:47 +01:00
|
|
|
* @param $api_key : User API key, if null random api key is generated
|
2020-03-30 01:52:53 +02:00
|
|
|
* @param $status : User status, default \models\User::STATUS_ACTIVE
|
2020-04-08 23:34:15 +02:00
|
|
|
* @param bool $encrypt_password : Should the password be encrypted, by default true
|
|
|
|
*
|
2020-06-22 19:49:59 +02:00
|
|
|
* exit code 0 on success | 1 on error
|
2020-02-19 02:32:52 +01:00
|
|
|
*/
|
2020-04-08 23:34:15 +02:00
|
|
|
public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE, bool $encrypt_password = true)
|
2020-02-19 02:32:52 +01:00
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2020-02-19 02:32:52 +01:00
|
|
|
$internal_user = new \controllers\internals\User($bdd);
|
|
|
|
|
2020-02-21 03:55:14 +01:00
|
|
|
$user = $internal_user->get_by_email($email);
|
|
|
|
if ($user)
|
|
|
|
{
|
|
|
|
$api_key = $api_key ?? $internal_user->generate_random_api_key();
|
2021-08-05 17:14:53 +02:00
|
|
|
$update_datas = [
|
2021-06-12 23:23:15 +02:00
|
|
|
'email' => $email,
|
|
|
|
'password' => $encrypt_password ? password_hash($password, PASSWORD_DEFAULT) : $password,
|
|
|
|
'admin' => $admin,
|
|
|
|
'api_key' => $api_key,
|
|
|
|
'status' => $status,
|
|
|
|
];
|
|
|
|
|
2021-08-05 17:14:53 +02:00
|
|
|
$success = $internal_user->update($user['id'], $update_datas);
|
2020-10-22 20:31:01 +02:00
|
|
|
echo json_encode(['id' => $user['id']]);
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-02-21 03:55:14 +01:00
|
|
|
exit($success ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
$new_user_id = $internal_user->create($email, $password, $admin, $api_key, $status, $encrypt_password);
|
|
|
|
echo json_encode(['id' => $new_user_id]);
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
exit($new_user_id ? 0 : 1);
|
2020-03-30 01:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-23 21:06:13 +02:00
|
|
|
* Update a user status.
|
2020-03-30 01:52:53 +02:00
|
|
|
*
|
2021-01-14 03:32:17 +01:00
|
|
|
* @param string $id : User id
|
2020-03-30 01:52:53 +02:00
|
|
|
* @param string $status : User status, default \models\User::STATUS_ACTIVE
|
|
|
|
*/
|
2020-10-22 20:31:01 +02:00
|
|
|
public function update_user_status(string $id, string $status)
|
2020-03-30 01:52:53 +02:00
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2020-03-30 01:52:53 +02:00
|
|
|
$internal_user = new \controllers\internals\User($bdd);
|
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
$user = $internal_user->get($id);
|
2020-03-30 01:52:53 +02:00
|
|
|
if (!$user)
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $internal_user->update_status($user['id'], $status);
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-02-21 03:55:14 +01:00
|
|
|
exit($success ? 0 : 1);
|
2020-02-19 02:32:52 +01:00
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
/**
|
2021-01-14 03:32:17 +01:00
|
|
|
* Delete a user.
|
2020-10-22 20:31:01 +02:00
|
|
|
*
|
2021-01-14 03:32:17 +01:00
|
|
|
* @param string $id : User id
|
2020-10-22 20:31:01 +02:00
|
|
|
*/
|
|
|
|
public function delete_user(string $id)
|
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2020-10-22 20:31:01 +02:00
|
|
|
$internal_user = new \controllers\internals\User($bdd);
|
|
|
|
|
|
|
|
$success = $internal_user->delete($id);
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-10-22 20:31:01 +02:00
|
|
|
exit($success ? 0 : 1);
|
|
|
|
}
|
2021-03-26 18:53:20 +01:00
|
|
|
|
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Delete medias that are no longer usefull.
|
2021-03-26 18:53:20 +01:00
|
|
|
*/
|
|
|
|
public function clean_unused_medias()
|
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2021-03-26 18:53:20 +01:00
|
|
|
$internal_media = new \controllers\internals\Media($bdd);
|
|
|
|
|
|
|
|
$medias = $internal_media->gets_unused();
|
|
|
|
|
|
|
|
foreach ($medias as $media)
|
|
|
|
{
|
|
|
|
$success = $internal_media->delete_for_user($media['id_user'], $media['id']);
|
|
|
|
|
2021-06-17 00:51:33 +02:00
|
|
|
echo (false === $success ? '[KO]' : '[OK]') . ' - ' . $media['path'] . "\n";
|
2021-03-26 18:53:20 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-08 21:01:26 +02:00
|
|
|
|
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Do alerting for quota limits.
|
|
|
|
*/
|
2021-06-08 21:01:26 +02:00
|
|
|
public function quota_limit_alerting()
|
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2021-06-08 21:01:26 +02:00
|
|
|
$internal_quota = new \controllers\internals\Quota($bdd);
|
|
|
|
$internal_quota->alerting_for_limit_close_and_reached();
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-06-10 01:31:53 +02:00
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Do quota renewal.
|
|
|
|
*/
|
2021-06-10 01:31:53 +02:00
|
|
|
public function renew_quotas()
|
|
|
|
{
|
2021-12-29 02:54:21 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2021-06-10 01:31:53 +02:00
|
|
|
$internal_quota = new \controllers\internals\Quota($bdd);
|
|
|
|
$internal_quota->renew_quotas();
|
|
|
|
}
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|