raspisms/controllers/internals/Quota.php

298 lines
9.9 KiB
PHP
Raw Normal View History

2021-06-08 02:00:48 +02: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;
class Quota extends StandardController
{
protected $model;
/**
* Create a new quota.
*
2021-06-17 00:51:33 +02:00
* @param int $id_user : User id
* @param int $credit : Credit for this quota
* @param int $additional : Additionals credits
* @param bool $report_unused : Should unused credits be re-credited
* @param bool $report_unused_additional : Should unused additional credits be re-credited
* @param bool $auto_renew : Should the quota be automatically renewed after expiration_date
* @param string $renew_interval : Period to use for setting new expiration_date on renewal (format ISO_8601#Durations)
* @param \DateTime $start_date : Starting date for the quota
* @param \DateTime $expiration_date : Ending date for the quota
2021-06-08 02:00:48 +02:00
*
2021-06-12 23:23:15 +02:00
* @return mixed bool|int : False if cannot create quota, id of the new quota else
2021-06-08 02:00:48 +02:00
*/
2021-06-12 23:23:15 +02:00
public function create(int $id_user, int $credit, int $additional, bool $report_unused, bool $report_unused_additional, bool $auto_renew, string $renew_interval, \DateTime $start_date, \DateTime $expiration_date)
2021-06-08 02:00:48 +02:00
{
$quota = [
'id_user' => $id_user,
'credit' => $credit,
'report_unused' => $report_unused,
'report_unused_additional' => $report_unused_additional,
2021-06-12 23:23:15 +02:00
'start_date' => $start_date->format('Y-m-d H:i:s'),
'expiration_date' => $expiration_date->format('Y-m-d H:i:s'),
2021-06-08 02:00:48 +02:00
'auto_renew' => $auto_renew,
'renew_interval' => $renew_interval,
'additional' => $additional,
];
return $this->get_model()->insert($quota);
}
/**
* Update a quota.
*
2021-06-17 00:51:33 +02:00
* @param int $id_user : User id
* @param int $id_quota : Quota to update id
* @param array $quota : Fields to update whith new values
2021-06-08 02:00:48 +02:00
*
2021-06-12 23:23:15 +02:00
* @return int : number of updated lines
2021-06-08 02:00:48 +02:00
*/
2021-06-12 23:23:15 +02:00
public function update_for_user(int $id_user, $id_quota, array $quota)
2021-06-08 02:00:48 +02:00
{
2021-06-10 01:31:53 +02:00
return $this->get_model()->update_for_user($id_user, $id_quota, $quota);
2021-06-08 02:00:48 +02:00
}
/**
2021-06-17 00:51:33 +02:00
* Check if we have enough credit.
*
2021-06-08 02:00:48 +02:00
* @param int $id_user : User id
2021-06-17 00:51:33 +02:00
* @param int $needed : Number of credits we need
*
2021-06-08 02:00:48 +02:00
* @return bool : true if we have enough credit, false else
*/
public function has_enough_credit(int $id_user, int $needed)
{
$remaining_credit = $this->get_model()->get_remaining_credit($id_user, new \DateTime());
2021-06-17 00:51:33 +02:00
2021-06-08 02:00:48 +02:00
return $remaining_credit >= $needed;
}
/**
2021-06-17 00:51:33 +02:00
* Consume some credit.
*
* @param int $id_user : User id
2021-06-08 02:00:48 +02:00
* @param int $quantity : Number of credits to consume
2021-06-17 00:51:33 +02:00
*
2021-06-08 02:00:48 +02:00
* @return bool : True on success, false else
*/
2021-06-17 00:51:33 +02:00
public function consume_credit(int $id_user, int $quantity)
2021-06-08 02:00:48 +02:00
{
$result = $this->get_model()->consume_credit($id_user, $quantity);
//Write event
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'QUOTA_CONSUME', 'Consume ' . $quantity . ' credits of SMS quota.');
2021-06-08 02:00:48 +02:00
return $result;
}
/**
2021-06-17 00:51:33 +02:00
* Get quota usage percentage.
*
2021-06-08 02:00:48 +02:00
* @param int $id_user : User id
2021-06-17 00:51:33 +02:00
*
2021-06-08 02:00:48 +02:00
* @return float : percentage of quota used
*/
2021-06-17 00:51:33 +02:00
public function get_usage_percentage(int $id_user)
2021-06-08 02:00:48 +02:00
{
return $this->get_model()->get_usage_percentage($id_user, new \DateTime());
}
/**
* Compute how many credit a message represent
2021-06-17 00:51:33 +02:00
* this function count 160 chars per SMS if it can be send as GSM 03.38 encoding and 70 chars per SMS if it can only be send as UTF8.
*
2021-06-08 02:00:48 +02:00
* @param string $text : Message to send
2021-06-17 00:51:33 +02:00
*
2021-06-08 02:00:48 +02:00
* @return int : Number of credit to send this message
*/
2021-06-17 00:51:33 +02:00
public static function compute_credits_for_message($text)
2021-06-08 02:00:48 +02:00
{
//Gsm 03.38 charset to detect if message is compatible or must use utf8
2021-06-17 00:51:33 +02:00
$gsm0338 = [
'@', 'Δ', ' ', '0', '¡', 'P', '¿', 'p',
'£', '_', '!', '1', 'A', 'Q', 'a', 'q',
'$', 'Φ', '"', '2', 'B', 'R', 'b', 'r',
'¥', 'Γ', '#', '3', 'C', 'S', 'c', 's',
'è', 'Λ', '¤', '4', 'D', 'T', 'd', 't',
'é', 'Ω', '%', '5', 'E', 'U', 'e', 'u',
'ù', 'Π', '&', '6', 'F', 'V', 'f', 'v',
'ì', 'Ψ', '\'', '7', 'G', 'W', 'g', 'w',
'ò', 'Σ', '(', '8', 'H', 'X', 'h', 'x',
'Ç', 'Θ', ')', '9', 'I', 'Y', 'i', 'y',
"\n", 'Ξ', '*', ':', 'J', 'Z', 'j', 'z',
'Ø', "\x1B", '+', ';', 'K', 'Ä', 'k', 'ä',
'ø', 'Æ', ',', '<', 'L', 'Ö', 'l', 'ö',
"\r", 'æ', '-', '=', 'M', 'Ñ', 'm', 'ñ',
'Å', 'ß', '.', '>', 'N', 'Ü', 'n', 'ü',
'å', 'É', '/', '?', 'O', '§', 'o', 'à',
];
2021-06-08 02:00:48 +02:00
$is_gsm0338 = true;
$len = mb_strlen($text);
2021-06-17 00:51:33 +02:00
for ($i = 0; $i < $len; ++$i)
2021-06-08 02:00:48 +02:00
{
2021-06-08 21:01:26 +02:00
if (!in_array(mb_substr($text, $i, 1), $gsm0338))
2021-06-08 02:00:48 +02:00
{
$is_gsm0338 = false;
2021-06-17 00:51:33 +02:00
2021-06-08 02:00:48 +02:00
break;
}
}
2021-06-17 00:51:33 +02:00
return $is_gsm0338 ? ceil($len / 160) : ceil($len / 70);
2021-06-08 02:00:48 +02:00
}
2021-06-08 21:01:26 +02:00
/**
2021-06-17 00:51:33 +02:00
* Do email alerting for quotas limit close and quotas limit reached.
2021-06-08 21:01:26 +02:00
*/
public function alerting_for_limit_close_and_reached()
{
$internal_user = new User($this->bdd);
$internal_event = new Event($this->bdd);
2021-06-17 00:51:33 +02:00
2021-06-08 21:01:26 +02:00
$quotas_limit_close = $this->get_model()->get_quotas_for_limit_close();
$quotas_limit_reached = $this->get_model()->get_quotas_for_limit_reached();
foreach ($quotas_limit_close as $quota)
{
$user = $internal_user->get($quota['id_user']);
if (!$user)
{
continue;
}
$quota_percentage = $quota['consumed'] / ($quota['credit'] + $quota['additional']);
$mailer = new \controllers\internals\Mailer();
$success = $mailer->enqueue($user['email'], EMAIL_QUOTA_LIMIT_CLOSE, ['percent' => $quota_percentage]);
if (!$success)
{
2021-06-17 00:51:33 +02:00
echo 'Cannot enqueue alert for quota limit close for quota : ' . $quota['id'] . "\n";
2021-06-08 21:01:26 +02:00
continue;
}
2021-06-17 00:51:33 +02:00
echo 'Enqueue alert for quota limit close for quota : ' . $quota['id'] . "\n";
2021-06-08 21:01:26 +02:00
$internal_event->create($quota['id_user'], 'QUOTA_LIMIT_CLOSE', round($quota_percentage * 100, 2) . '% of SMS quota limit reached.');
}
2021-06-17 00:51:33 +02:00
2021-06-08 21:01:26 +02:00
foreach ($quotas_limit_reached as $quota)
{
$user = $internal_user->get($quota['id_user']);
if (!$user)
{
continue;
}
$quota_percentage = $quota['consumed'] / ($quota['credit'] + $quota['additional']);
$mailer = new \controllers\internals\Mailer();
$success = $mailer->enqueue($user['email'], EMAIL_QUOTA_LIMIT_REACHED, ['expiration_date' => $quota['expiration_date']]);
if (!$success)
{
2021-06-17 00:51:33 +02:00
echo 'Cannot enqueue alert for quota limit reached for quota : ' . $quota['id'] . "\n";
2021-06-08 21:01:26 +02:00
continue;
}
2021-06-17 00:51:33 +02:00
echo 'Enqueue alert for quota limit reached for quota : ' . $quota['id'] . "\n";
2021-06-08 21:01:26 +02:00
$internal_event->create($quota['id_user'], 'QUOTA_LIMIT_REACHED', 'Reached SMS quota limit.');
}
}
2021-06-10 01:31:53 +02:00
/**
2021-06-17 00:51:33 +02:00
* Do quota renewing.
2021-06-10 01:31:53 +02:00
*/
2021-06-17 00:51:33 +02:00
public function renew_quotas()
2021-06-10 01:31:53 +02:00
{
$internal_user = new User($this->bdd);
$internal_event = new Event($this->bdd);
$quotas = $this->get_model()->get_quotas_to_be_renewed(new \DateTime());
2021-06-17 00:51:33 +02:00
2021-06-10 01:31:53 +02:00
foreach ($quotas as $quota)
{
$user = $internal_user->get($quota['id_user']);
if (!$user)
{
continue;
}
$unused_credit = $quota['credit'] - $quota['consumed'];
$unused_additional = $unused_credit > 0 ? $quota['additional'] : $quota['additional'] + $unused_credit;
$renew_interval = $quota['renew_interval'] ?? 'P0D';
2021-06-17 00:51:33 +02:00
$new_start_date = new \DateTime($quota['expiration_date']);
2021-06-10 01:31:53 +02:00
$new_expiration_date = clone $new_start_date;
$new_expiration_date->add(new \DateInterval($quota['renew_interval']));
2021-06-17 00:51:33 +02:00
2021-06-10 01:31:53 +02:00
$report = 0;
if ($quota['report_unused'] && $unused_credit > 0)
{
$report += $unused_credit;
}
if ($quota['report_unused_additional'] && $unused_additional > 0)
{
$report += $unused_additional;
}
2021-06-12 23:23:15 +02:00
$updated_fields = [
'start_date' => $new_start_date->format('Y-m-d H:i:s'),
'expiration_date' => $new_expiration_date->format('Y-m-d H:i:s'),
'additional' => $report,
'consumed' => 0,
];
$success = $this->update_for_user($user['id'], $quota['id'], $updated_fields);
2021-06-10 01:31:53 +02:00
if (!$success)
{
2021-06-17 00:51:33 +02:00
echo 'Cannot update quota : ' . $quota['id'] . "\n";
2021-06-10 01:31:53 +02:00
continue;
}
2021-06-17 00:51:33 +02:00
echo 'Update quota : ' . $quota['id'] . "\n";
$internal_event->create($quota['id_user'], 'QUOTA_RENEWAL', 'Renew quota and report ' . $report . ' credits.');
2021-06-10 01:31:53 +02:00
}
}
2021-06-17 00:51:33 +02:00
2021-06-12 23:23:15 +02:00
/**
* Return the quota for a user if it exists.
*
* @param int $id_user : user id
*
2021-06-17 00:51:33 +02:00
* @return array
2021-06-12 23:23:15 +02:00
*/
public function get_user_quota(int $id_user)
{
return $this->get_model()->get_user_quota($id_user);
}
2021-06-10 01:31:53 +02:00
2021-06-08 02:00:48 +02:00
/**
* Get the model for the Controller.
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Quota($this->bdd);
return $this->model;
}
}