style validation fix

This commit is contained in:
osaajani 2021-06-17 00:51:33 +02:00
parent f1d47a25ed
commit 017c7fee53
42 changed files with 526 additions and 497 deletions

View file

@ -118,8 +118,8 @@ namespace controllers\internals;
}
/**
* List all adapters for a meta value
*
* List all adapters for a meta value.
*
* @param $search_name : Name of the meta
* @param $search_value : Value of the meta
*
@ -128,7 +128,9 @@ namespace controllers\internals;
public function list_adapters_with_meta_equal($search_name, $search_value)
{
$adapters = $this->list_adapters();
return array_filter($adapters, function($metas) use ($search_name, $search_value) {
return array_filter($adapters, function ($metas) use ($search_name, $search_value)
{
$match = false;
foreach ($metas as $name => $value)
{

View file

@ -18,11 +18,11 @@ namespace controllers\internals;
/**
* Create a call.
*
* @param int $id_user : Id of the user
* @param int $id_phone : Id of the phone that emitted (outbound) or received (inbound) the call
* @param string $uid : Uid of the phone call
* @param string $direction : Direction of the call, \models\Call::DIRECTION_INBOUND | \models\Call::DIRECTION_OUTBOUND
* @param string $start : Date of the call beginning
* @param int $id_user : Id of the user
* @param int $id_phone : Id of the phone that emitted (outbound) or received (inbound) the call
* @param string $uid : Uid of the phone call
* @param string $direction : Direction of the call, \models\Call::DIRECTION_INBOUND | \models\Call::DIRECTION_OUTBOUND
* @param string $start : Date of the call beginning
* @param ?string $end : Date of the call end
* @param ?string $origin : Origin of the call or null if outbound
* @param ?string $destination : Destination of the call or null if inbound
@ -49,15 +49,23 @@ namespace controllers\internals;
switch ($direction)
{
case \models\Call::DIRECTION_OUTBOUND :
if (null === $destination) { return false; }
break;
case \models\Call::DIRECTION_INBOUND :
if (null === $origin) { return false; }
case \models\Call::DIRECTION_OUTBOUND:
if (null === $destination)
{
return false;
}
break;
default :
case \models\Call::DIRECTION_INBOUND:
if (null === $origin)
{
return false;
}
break;
default:
return false;
}
@ -75,7 +83,7 @@ namespace controllers\internals;
{
return false;
}
$new_call_id = $this->get_model()->insert($call);
if (!$new_call_id)
{
@ -83,21 +91,20 @@ namespace controllers\internals;
}
$call['id'] = $new_call_id;
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_INBOUND_CALL, $call);
return $new_call_id;
}
/**
* End a call
* End a call.
*
* @param int $id_user : Id of the user to end call for
* @param int $id_phone : If of the phone to end call for
* @param string $uid : Uid of the call to end
* @param string $end : End date of the call, format Y-m-d H:i:s
* @param int $id_user : Id of the user to end call for
* @param int $id_phone : If of the phone to end call for
* @param string $uid : Uid of the call to end
* @param string $end : End date of the call, format Y-m-d H:i:s
*
* @return bool : False if cannot end phone call, true else
*/
@ -125,7 +132,7 @@ namespace controllers\internals;
return (bool) $this->get_model()->update_for_user($id_user, $call['id'], $datas);
}
/**
* Get the model for the Controller.
*/

View file

@ -109,7 +109,6 @@ namespace controllers\internals;
'admin' => $admin,
'api_key' => $api_key,
'status' => $status,
];
$success = $internal_user->update($user['id'], $user);
@ -161,9 +160,8 @@ namespace controllers\internals;
exit($success ? 0 : 1);
}
/**
* Delete medias that are no longer usefull
* Delete medias that are no longer usefull.
*/
public function clean_unused_medias()
{
@ -176,23 +174,23 @@ namespace controllers\internals;
{
$success = $internal_media->delete_for_user($media['id_user'], $media['id']);
echo ($success === false ? '[KO]' : '[OK]') . ' - ' . $media['path'] . "\n";
echo (false === $success ? '[KO]' : '[OK]') . ' - ' . $media['path'] . "\n";
}
}
/**
* Do alerting for quota limits
*/
* Do alerting for quota limits.
*/
public function quota_limit_alerting()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_quota = new \controllers\internals\Quota($bdd);
$internal_quota->alerting_for_limit_close_and_reached();
}
/**
* Do quota renewal
*/
* Do quota renewal.
*/
public function renew_quotas()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');

View file

@ -55,20 +55,20 @@ namespace controllers\internals;
return $this->get_model()->insert($event);
}
/**
* Gets events for a type, since a date and eventually until a date (both included)
* Gets events for a type, since a date and eventually until a date (both included).
*
* @param int $id_user : User id
* @param string $type : Event type we want
* @param \DateTime $since : Date to get events since
* @param ?\DateTime $until (optional) : Date until wich we want events, if not specified no limit
* @param int $id_user : User id
* @param string $type : Event type we want
* @param \DateTime $since : Date to get events since
* @param ?\DateTime $until (optional) : Date until wich we want events, if not specified no limit
*
* @return array
*/
public function get_events_by_type_and_date_for_user (int $id_user, string $type, \DateTime $since, ?\DateTime $until = null)
public function get_events_by_type_and_date_for_user(int $id_user, string $type, \DateTime $since, ?\DateTime $until = null)
{
$this->get_model()->get_events_by_type_and_date_for_user ($id_user, $type, $since, $until);
$this->get_model()->get_events_by_type_and_date_for_user($id_user, $type, $since, $until);
}
/**

View file

@ -20,8 +20,8 @@ class Media extends StandardController
/**
* Create a media.
*
* @param int $id_user : Id of the user
* @param string $tmpfile_path : Path of the temporary local copy of the media
* @param int $id_user : Id of the user
* @param string $tmpfile_path : Path of the temporary local copy of the media
* @param ?string $extension : Extension to use for the media
*
* @return int : Exception on error, new media id else
@ -33,13 +33,13 @@ class Media extends StandardController
{
throw new \Exception('File ' . $tmpfile_path . ' does not exists.');
}
if (!is_readable($tmpfile_path))
{
throw new \Exception('File ' . $tmpfile_path . ' is not readable.');
}
$mimey = new \Mimey\MimeTypes;
$mimey = new \Mimey\MimeTypes();
$extension = $extension ?? $mimey->getExtension(mime_content_type($tmpfile_path));
$new_file_name = \controllers\internals\Tool::random_uuid() . '.' . $extension;
@ -60,7 +60,7 @@ class Media extends StandardController
{
throw new \Exception('Cannot give file ' . $new_file_path . ' to user : ' . fileowner($user_path));
}
if (!chgrp($new_file_path, filegroup($user_path)))
{
throw new \Exception('Cannot give file ' . $new_file_path . ' to group : ' . filegroup($user_path));
@ -86,16 +86,17 @@ class Media extends StandardController
}
/**
* Upload and create a media
*
* @param int $id_user : Id of the user
* @param array $file : array representing uploaded file, extracted from $_FILES['yourfile']
* Upload and create a media.
*
* @param int $id_user : Id of the user
* @param array $file : array representing uploaded file, extracted from $_FILES['yourfile']
*
* @return int : Raise exception on error or return new media id on success
*/
public function create_from_uploaded_file_for_user(int $id_user, array $file)
{
$upload_result = \controllers\internals\Tool::read_uploaded_file($file);
if ($upload_result['success'] !== true)
if (true !== $upload_result['success'])
{
throw new \Exception($upload_result['content']);
}
@ -110,15 +111,16 @@ class Media extends StandardController
{
throw new \Exception('Cannot move uploaded file to : ' . $tmp_file);
}
return $this->create($id_user, $tmp_file, $upload_result['extension']);
}
/**
* Link a media to a scheduled, a received or a sended message
* @param int $id_media : Id of the media
* Link a media to a scheduled, a received or a sended message.
*
* @param int $id_media : Id of the media
* @param string $resource_type : Type of resource to link the media to ('scheduled', 'received' or 'sended')
* @param int $resource_id : Id of the resource to link the media to
* @param int $resource_id : Id of the resource to link the media to
*
* @return mixed bool|int : false on error, the new link id else
*/
@ -128,14 +130,17 @@ class Media extends StandardController
{
case 'scheduled':
return $this->get_model()->insert_media_scheduled($id_media, $resource_id);
break;
case 'received':
return $this->get_model()->insert_media_received($id_media, $resource_id);
break;
case 'sended':
return $this->get_model()->insert_media_sended($id_media, $resource_id);
break;
default:
@ -143,12 +148,12 @@ class Media extends StandardController
}
}
/**
* Unlink a media of a scheduled, a received or a sended message
* @param int $id_media : Id of the media
* Unlink a media of a scheduled, a received or a sended message.
*
* @param int $id_media : Id of the media
* @param string $resource_type : Type of resource to unlink the media of ('scheduled', 'received' or 'sended')
* @param int $resource_id : Id of the resource to unlink the media of
* @param int $resource_id : Id of the resource to unlink the media of
*
* @return mixed bool : false on error, true on success
*/
@ -158,14 +163,17 @@ class Media extends StandardController
{
case 'scheduled':
return $this->get_model()->delete_media_scheduled($id_media, $resource_id);
break;
case 'received':
return $this->get_model()->delete_media_received($id_media, $resource_id);
break;
case 'sended':
return $this->get_model()->delete_media_sended($id_media, $resource_id);
break;
default:
@ -174,9 +182,10 @@ class Media extends StandardController
}
/**
* Unlink all medias of a scheduled, a received or a sended message
* Unlink all medias of a scheduled, a received or a sended message.
*
* @param string $resource_type : Type of resource to unlink the media of ('scheduled', 'received' or 'sended')
* @param int $resource_id : Id of the resource to unlink the media of
* @param int $resource_id : Id of the resource to unlink the media of
*
* @return mixed bool : false on error, true on success
*/
@ -186,14 +195,17 @@ class Media extends StandardController
{
case 'scheduled':
return $this->get_model()->delete_all_for_scheduled($resource_id);
break;
case 'received':
return $this->get_model()->delete_all_for_received($resource_id);
break;
case 'sended':
return $this->get_model()->delete_all_for_sended($resource_id);
break;
default:
@ -204,9 +216,9 @@ class Media extends StandardController
/**
* Update a media for a user.
*
* @param int $id_user : user id
* @param int $id_media : Media id
* @param string $path : Path of the file
* @param int $id_user : user id
* @param int $id_media : Media id
* @param string $path : Path of the file
*
* @return bool : false on error, true on success
*/
@ -289,7 +301,8 @@ class Media extends StandardController
}
/**
* Find medias that are not used
* Find medias that are not used.
*
* @return array
*/
public function gets_unused()

View file

@ -44,13 +44,14 @@ namespace controllers\internals;
}
/**
* Check if a phone support mms
* Check if a phone support mms.
*
* @param int $id : id of the phone to check
* @param $type : type of sms support, a const from Phone, MMS_SENDING, MMS_RECEPTION or MMS_BOTH
* @param $type : type of sms support, a const from Phone, MMS_SENDING, MMS_RECEPTION or MMS_BOTH
*
* @return bool : true if support, false else
*/
public function support_mms (int $id, string $type)
public function support_mms(int $id, string $type)
{
$phone = $this->get_model()->get($id);
if (!$phone)
@ -60,16 +61,19 @@ namespace controllers\internals;
switch ($type)
{
case self::MMS_SENDING :
case self::MMS_SENDING:
return $phone['adapter']::meta_support_mms_sending();
break;
case self::MMS_RECEPTION :
case self::MMS_RECEPTION:
return $phone['adapter']::meta_support_mms_reception();
break;
case self::MMS_BOTH :
case self::MMS_BOTH:
return $phone['adapter']::meta_support_mms_sending() && $phone['adapter']::meta_support_mms_reception();
break;
default:
@ -78,13 +82,14 @@ namespace controllers\internals;
}
/**
* Get all phones supporting mms for a user
* Get all phones supporting mms for a user.
*
* @param int $id_user : id of the user
* @param $type : type of sms support, a const from Phone, MMS_SENDING, MMS_RECEPTION or MMS_BOTH
* @param $type : type of sms support, a const from Phone, MMS_SENDING, MMS_RECEPTION or MMS_BOTH
*
* @return array : array of phones supporting mms
*/
public function gets_phone_supporting_mms_for_user (int $id_user, string $type)
public function gets_phone_supporting_mms_for_user(int $id_user, string $type)
{
$phones = $this->get_model()->gets_for_user($id_user);

View file

@ -18,15 +18,15 @@ class Quota extends StandardController
/**
* Create a new quota.
*
* @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
* @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
*
* @return mixed bool|int : False if cannot create quota, id of the new quota else
*/
@ -50,10 +50,9 @@ class Quota extends StandardController
/**
* Update a quota.
*
*
* @param int $id_user : User id
* @param int $id_quota : Quota to update id
* @param array $quota : Fields to update whith new values
* @param int $id_user : User id
* @param int $id_quota : Quota to update id
* @param array $quota : Fields to update whith new values
*
* @return int : number of updated lines
*/
@ -63,24 +62,29 @@ class Quota extends StandardController
}
/**
* Check if we have enough credit
* Check if we have enough credit.
*
* @param int $id_user : User id
* @param int $needed : Number of credits we need
* @param int $needed : Number of credits we need
*
* @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());
return $remaining_credit >= $needed;
}
/**
* Consume some credit
* @param int $id_user : User id
* Consume some credit.
*
* @param int $id_user : User id
* @param int $quantity : Number of credits to consume
*
* @return bool : True on success, false else
*/
public function consume_credit (int $id_user, int $quantity)
public function consume_credit(int $id_user, int $quantity)
{
$result = $this->get_model()->consume_credit($id_user, $quantity);
@ -92,68 +96,71 @@ class Quota extends StandardController
}
/**
* Get quota usage percentage
* Get quota usage percentage.
*
* @param int $id_user : User id
*
* @return float : percentage of quota used
*/
public function get_usage_percentage (int $id_user)
public function get_usage_percentage(int $id_user)
{
return $this->get_model()->get_usage_percentage($id_user, new \DateTime());
}
/**
* Compute how many credit a message represent
* 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
* 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.
*
* @param string $text : Message to send
*
* @return int : Number of credit to send this message
*/
public static function compute_credits_for_message ($text)
public static function compute_credits_for_message($text)
{
//Gsm 03.38 charset to detect if message is compatible or must use utf8
$gsm0338 = array(
'@','Δ',' ','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','à'
);
$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', 'à',
];
$is_gsm0338 = true;
$len = mb_strlen($text);
for ($i = 0; $i < $len; $i++)
for ($i = 0; $i < $len; ++$i)
{
if (!in_array(mb_substr($text, $i, 1), $gsm0338))
{
$is_gsm0338 = false;
break;
}
}
return ($is_gsm0338 ? ceil($len / 160) : ceil($len / 70));
return $is_gsm0338 ? ceil($len / 160) : ceil($len / 70);
}
/**
* Do email alerting for quotas limit close and quotas limit reached
* Do email alerting for quotas limit close and quotas limit reached.
*/
public function alerting_for_limit_close_and_reached()
{
$internal_user = new User($this->bdd);
$internal_event = new Event($this->bdd);
$quotas_limit_close = $this->get_model()->get_quotas_for_limit_close();
$quotas_limit_reached = $this->get_model()->get_quotas_for_limit_reached();
@ -173,14 +180,15 @@ class Quota extends StandardController
if (!$success)
{
echo "Cannot enqueue alert for quota limit close for quota : " . $quota['id'] . "\n";
echo 'Cannot enqueue alert for quota limit close for quota : ' . $quota['id'] . "\n";
continue;
}
echo "Enqueue alert for quota limit close for quota : " . $quota['id'] . "\n";
echo 'Enqueue alert for quota limit close for quota : ' . $quota['id'] . "\n";
$internal_event->create($quota['id_user'], 'QUOTA_LIMIT_CLOSE', round($quota_percentage * 100, 2) . '% of SMS quota limit reached.');
}
foreach ($quotas_limit_reached as $quota)
{
$user = $internal_user->get($quota['id_user']);
@ -197,24 +205,25 @@ class Quota extends StandardController
if (!$success)
{
echo "Cannot enqueue alert for quota limit reached for quota : " . $quota['id'] . "\n";
echo 'Cannot enqueue alert for quota limit reached for quota : ' . $quota['id'] . "\n";
continue;
}
echo "Enqueue alert for quota limit reached for quota : " . $quota['id'] . "\n";
echo 'Enqueue alert for quota limit reached for quota : ' . $quota['id'] . "\n";
$internal_event->create($quota['id_user'], 'QUOTA_LIMIT_REACHED', 'Reached SMS quota limit.');
}
}
/**
* Do quota renewing
* Do quota renewing.
*/
public function renew_quotas ()
public function renew_quotas()
{
$internal_user = new User($this->bdd);
$internal_event = new Event($this->bdd);
$quotas = $this->get_model()->get_quotas_to_be_renewed(new \DateTime());
foreach ($quotas as $quota)
{
$user = $internal_user->get($quota['id_user']);
@ -228,10 +237,10 @@ class Quota extends StandardController
$unused_additional = $unused_credit > 0 ? $quota['additional'] : $quota['additional'] + $unused_credit;
$renew_interval = $quota['renew_interval'] ?? 'P0D';
$new_start_date = new \DateTime($quota['expiration_date']);
$new_start_date = new \DateTime($quota['expiration_date']);
$new_expiration_date = clone $new_start_date;
$new_expiration_date->add(new \DateInterval($quota['renew_interval']));
$report = 0;
if ($quota['report_unused'] && $unused_credit > 0)
{
@ -254,21 +263,22 @@ class Quota extends StandardController
if (!$success)
{
echo "Cannot update quota : " . $quota['id'] . "\n";
echo 'Cannot update quota : ' . $quota['id'] . "\n";
continue;
}
echo "Update quota : " . $quota['id'] . "\n";
echo 'Update quota : ' . $quota['id'] . "\n";
$internal_event->create($quota['id_user'], 'QUOTA_RENEWAL', 'Renew quota and report ' . $report . ' credits.');
}
}
/**
* Return the quota for a user if it exists.
*
* @param int $id_user : user id
*
* @return array
* @return array
*/
public function get_user_quota(int $id_user)
{

View file

@ -36,11 +36,11 @@ namespace controllers\internals;
* @param int $id_phone : Id of the number the message was send with
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @param bool $mms : Is the sms a mms
* @param array $media_ids : Ids of the medias to link to received
* @param string $origin : Number of the sender
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @param bool $mms : Is the sms a mms
* @param array $media_ids : Ids of the medias to link to received
*
* @return mixed : false on error, new received id else
*/
@ -64,6 +64,7 @@ namespace controllers\internals;
if (!$id_received)
{
$this->bdd->rollBack();
return false;
}
@ -75,6 +76,7 @@ namespace controllers\internals;
if (!$id_media_received)
{
$this->bdd->rollBack();
return false;
}
}
@ -167,6 +169,7 @@ namespace controllers\internals;
* @param int $id_user : User id
* @param string $since : Date we want messages since format Y-m-d H:i:s
* @param string $origin : Number who sent the message
*
* @return array
*/
public function gets_since_date_by_origin_and_user(int $id_user, string $since, string $origin)
@ -256,11 +259,11 @@ namespace controllers\internals;
* @param string $origin : Number of the sender
* @param ?string $at : Message reception date, if null use current date
* @param string $status : Status of a the sms. By default \models\Received::STATUS_UNREAD
* @param bool $mms : Is the sms a mms
* @param array $medias : Empty array if no medias, or medias to create and link to the received message. Format : [[
* string 'filepath' => local path to a readable copy of the media,
* ?string 'extension' => extension to use for the file or null
* ], ...]
* @param bool $mms : Is the sms a mms
* @param array $medias : Empty array if no medias, or medias to create and link to the received message. Format : [[
* string 'filepath' => local path to a readable copy of the media,
* ?string 'extension' => extension to use for the file or null
* ], ...]
*
* @return array : [
* bool 'error' => false if success, true else
@ -285,7 +288,7 @@ namespace controllers\internals;
$is_command = true;
$text = $response;
}
//We create medias to link to the sms
$internal_media = new Media($this->bdd);
$media_ids = [];
@ -301,6 +304,7 @@ namespace controllers\internals;
catch (\Throwable $t)
{
$return['error_message'] = $t->getMessage();
continue; //Better loose the media than the message
}
}

View file

@ -43,7 +43,7 @@ namespace controllers\internals;
'mms' => $mms,
];
if ($text === '')
if ('' === $text)
{
return false;
}
@ -66,6 +66,7 @@ namespace controllers\internals;
if (!$id_scheduled)
{
$this->bdd->rollBack();
return false;
}
@ -76,11 +77,11 @@ namespace controllers\internals;
if (!$id_media_scheduled)
{
$this->bdd->rollBack();
return false;
}
}
foreach ($numbers as $number)
{
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
@ -193,6 +194,7 @@ namespace controllers\internals;
if (!$id_media_scheduled)
{
$this->bdd->rollBack();
return false;
}
}
@ -254,7 +256,7 @@ namespace controllers\internals;
{
return $this->get_model()->gets_before_date_for_number_and_user($id_user, $date, $number);
}
/**
* Get messages scheduled after a date for a number and a user.
*
@ -349,7 +351,7 @@ namespace controllers\internals;
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
}
}
$message = [
'id_user' => $scheduled['id_user'],
'id_scheduled' => $scheduled['id'],
@ -405,7 +407,7 @@ namespace controllers\internals;
}
$added_contacts[$contact['id']] = true;
if (null === $phone_to_use)
{
if ($scheduled['mms'] && count($users_mms_phones))

View file

@ -27,7 +27,7 @@ namespace controllers\internals;
* @param string $adapter : Name of the adapter service used to send the message
* @param bool $flash : Is the sms a flash
* @param bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS.
* @param array $medias : Array of medias to link to the MMS
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
*
* @return mixed : false on error, new sended id else
@ -49,14 +49,15 @@ namespace controllers\internals;
//Ensure atomicity
$this->bdd->beginTransaction();
$id_sended = $this->get_model()->insert($sended);
if (!$id_sended)
{
$this->bdd->rollback();
return false;
}
//Link medias
$internal_media = new Media($this->bdd);
foreach ($medias as $media)
@ -132,13 +133,14 @@ namespace controllers\internals;
{
return $this->get_model()->gets_by_destination_and_user($id_user, $origin);
}
/**
* Return sendeds for a destination and a user since a date.
*
* @param int $id_user : User id
* @param string $since : Date we want messages since format Y-m-d H:i:s
* @param string $origin : Number who sent the message
*
* @return array
*/
public function gets_since_date_by_destination_and_user(int $id_user, string $since, string $origin)
@ -204,7 +206,7 @@ namespace controllers\internals;
* @param string $destination : Number of the receiver
* @param bool $flash : Is the sms a flash. By default false.
* @param bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS.
* @param array $medias : Array of medias to link to the MMS
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
*
* @return array : [
@ -230,7 +232,6 @@ namespace controllers\internals;
return $return;
}
$at = (new \DateTime())->format('Y-m-d H:i:s');
$media_uris = [];
foreach ($medias as $media)

View file

@ -164,11 +164,11 @@ namespace controllers\internals;
return $objectDate && $objectDate->format($format) === $date;
}
/**
* Check if a sting represent a valid PHP period for creating an interval.
*
* @param string $period : Period string to check
* @param string $period : Period string to check
*
* @return bool : True if valid period, false else
*/
@ -320,26 +320,27 @@ namespace controllers\internals;
}
/**
* Generate a highly random uuid based on timestamp and strong cryptographic random
* Generate a highly random uuid based on timestamp and strong cryptographic random.
*
* @return string
*/
public static function random_uuid()
{
$bytes = random_bytes(16);
return time() . '-' . bin2hex($bytes);
}
/**
* Create a user data public path
* Create a user data public path.
*
* @param int $id_user : The user id
*
* @return string : The created path
*
* @exception Raise exception on error
*/
public static function create_user_public_path (int $id_user)
public static function create_user_public_path(int $id_user)
{
$new_dir = PWD_DATA_PUBLIC . '/' . $id_user;
if (file_exists($new_dir))
@ -354,18 +355,18 @@ namespace controllers\internals;
}
//We do chmod in two times because else umask fuck mkdir permissions
if (!chmod($new_dir, fileperms(PWD_DATA_PUBLIC) & 0777)) //Fileperms return garbage in addition to perms. Perms are only in weak bytes. We must use an octet notation with 0
{
if (!chmod($new_dir, fileperms(PWD_DATA_PUBLIC) & 0777))
{ //Fileperms return garbage in addition to perms. Perms are only in weak bytes. We must use an octet notation with 0
throw new \Exception('Cannot give dir ' . $new_dir . ' rights : ' . decoct(fileperms(PWD_DATA_PUBLIC) & 0777)); //Show error in dec
}
if (posix_getuid() === 0 && !chown($new_dir, fileowner(PWD_DATA_PUBLIC))) //If we are root, try to give the file to a proper user
{
if (0 === posix_getuid() && !chown($new_dir, fileowner(PWD_DATA_PUBLIC)))
{ //If we are root, try to give the file to a proper user
throw new \Exception('Cannot give dir ' . $new_dir . ' to user : ' . fileowner(PWD_DATA_PUBLIC));
}
if (posix_getuid() === 0 && !chgrp($new_dir, filegroup(PWD_DATA_PUBLIC))) //If we are root, try to give the file to a proper group
{
if (0 === posix_getuid() && !chgrp($new_dir, filegroup(PWD_DATA_PUBLIC)))
{ //If we are root, try to give the file to a proper group
throw new \Exception('Cannot give dir ' . $new_dir . ' to group : ' . filegroup(PWD_DATA_PUBLIC));
}

View file

@ -32,9 +32,9 @@ namespace controllers\internals;
}
/**
* Return a list of users by their ids
* Return a list of users by their ids.
*
* @param array $ids : ids of entries to find
* @param array $ids : ids of entries to find
*
* @return array
*/
@ -194,8 +194,8 @@ namespace controllers\internals;
/**
* Update a user by his id.
*
* @param mixed $id : User id
* @param array $user : Array of fields to update for user
* @param mixed $id : User id
* @param array $user : Array of fields to update for user
* @param mixed (?array|bool) $quota : Quota to update for the user, by default null -> no update, if false, remove quota
*
* @return bool : True on success, false on error
@ -206,15 +206,16 @@ namespace controllers\internals;
$current_quota = $internal_quota->get_user_quota($id);
$this->bdd->beginTransaction();
$this->model_user->update($id, $user);
if ($current_quota && $quota === false)
if ($current_quota && false === $quota)
{
$success = $internal_quota->delete_for_user($id, $current_quota['id']);
if (!$success)
{
$this->bdd->rollback();
return false;
}
}
@ -237,7 +238,6 @@ namespace controllers\internals;
}
}
if (!$this->bdd->commit())
{
return false;
@ -277,7 +277,6 @@ namespace controllers\internals;
return false;
}
$success = $this->internal_setting->create_defaults_for_user($new_id_user);
if (!$success)
{
@ -286,8 +285,7 @@ namespace controllers\internals;
return false;
}
if ($quota !== null)
if (null !== $quota)
{
$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']);
@ -299,7 +297,6 @@ namespace controllers\internals;
}
}
if (!$this->bdd->commit())
{
return false;
@ -355,9 +352,9 @@ namespace controllers\internals;
}
$mailer = new Mailer();
$attachments = [];
foreach ($received['medias'] ?? [] as $media)
{
$attachments[] = PWD_DATA_PUBLIC . '/' . $media['path'];