mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-06-06 14:46:27 +02:00
start adding mms to a lot of places, no tests, not done
This commit is contained in:
parent
b8e587a59e
commit
ff6b3e79df
24 changed files with 1174 additions and 788 deletions
|
@ -20,9 +20,9 @@ namespace controllers\internals;
|
|||
private const ADAPTERS_META_START = 'meta_';
|
||||
|
||||
/**
|
||||
* List adapters using internal metas.
|
||||
* List adapters with filepath and internal metas.
|
||||
*
|
||||
* @return array
|
||||
* @return array : ['adapter_filepath' => ['meta...' => value, ...], ...]
|
||||
*/
|
||||
public function list_adapters()
|
||||
{
|
||||
|
@ -42,7 +42,7 @@ namespace controllers\internals;
|
|||
continue;
|
||||
}
|
||||
|
||||
$adapters[] = $metas;
|
||||
$adapters[$file] = $metas;
|
||||
}
|
||||
|
||||
return $adapters;
|
||||
|
@ -116,4 +116,29 @@ namespace controllers\internals;
|
|||
|
||||
return $metas;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all adapters for a meta value
|
||||
*
|
||||
* @param $search_name : Name of the meta
|
||||
* @param $search_value : Value of the meta
|
||||
*
|
||||
* @return array : Array with ['adapter filepath' => ['search_name' => value, ...], ...]
|
||||
*/
|
||||
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) {
|
||||
$match = false;
|
||||
foreach ($metas as $name => $value)
|
||||
{
|
||||
if ($name === $search_name && $value === $search_value)
|
||||
{
|
||||
$match = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $match;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,32 +19,105 @@ namespace controllers\internals;
|
|||
* Create a media.
|
||||
*
|
||||
* @param int $id_user : Id of the user
|
||||
* @param int $id_scheduled : Id of the scheduled
|
||||
* @param array $media : $_FILES media array
|
||||
* @param string $path : path of the media in data dir
|
||||
*
|
||||
* @return bool : false on error, new media id else
|
||||
* @return mixed bool|int : false on error, new media id else
|
||||
*/
|
||||
public function create(int $id_user, int $id_scheduled, array $media): bool
|
||||
public function create(int $id_user, string $path): bool
|
||||
{
|
||||
$internal_scheduled = new Scheduled($this->bdd);
|
||||
$scheduled = $internal_scheduled->get_for_user($id_user, $id_scheduled);
|
||||
if (!$scheduled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$result_upload_media = \controllers\internals\Tool::upload_file($media);
|
||||
if (false === $result_upload_media['success'])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id_scheduled' => $id_scheduled,
|
||||
'path' => $result_upload_media['content'],
|
||||
'path' => $path,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->insert($data);
|
||||
return $this->get_model()->insert($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return mixed bool|int : false on error, the new link id else
|
||||
*/
|
||||
public function link_to(int $id_media, int $resource_type, int $resource_id)
|
||||
{
|
||||
switch ($resource_type)
|
||||
{
|
||||
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:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return mixed bool : false on error, true on success
|
||||
*/
|
||||
public function unlink_of(int $id_media, int $resource_type, int $resource_id)
|
||||
{
|
||||
switch ($resource_type)
|
||||
{
|
||||
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:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return mixed bool : false on error, true on success
|
||||
*/
|
||||
public function unlink_all_of(int $resource_type, int $resource_id)
|
||||
{
|
||||
switch ($resource_type)
|
||||
{
|
||||
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:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,25 +125,16 @@ namespace controllers\internals;
|
|||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_media : Media id
|
||||
* @param int $id_scheduled : Id of the scheduled
|
||||
* @param string $path : Path of the file
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_media, int $id_scheduled, string $path): bool
|
||||
public function update_for_user(int $id_user, int $id_media, string $path): bool
|
||||
{
|
||||
$media = [
|
||||
'id_scheduled' => $id_scheduled,
|
||||
'path' => $path,
|
||||
];
|
||||
|
||||
$internal_scheduled = new Scheduled($this->bdd);
|
||||
$scheduled = $this->get_for_user($id_user, $id_scheduled);
|
||||
if (!$scheduled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id_media, $media);
|
||||
}
|
||||
|
||||
|
@ -95,36 +159,43 @@ namespace controllers\internals;
|
|||
return $this->get_model()->delete_for_user($id_user, $id_media);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a media for a scheduled and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_scheduled : Scheduled id to delete medias for
|
||||
*
|
||||
* @return int : Number of removed rows
|
||||
*/
|
||||
public function delete_for_scheduled_and_user(int $id_user, int $id_scheduled): bool
|
||||
{
|
||||
$media = $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
|
||||
if ($media)
|
||||
{
|
||||
unlink($media['path']);
|
||||
}
|
||||
|
||||
return $this->get_model()->delete_for_scheduled_and_user($id_user, $id_scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find medias for a scheduled and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_scheduled : Scheduled id to delete medias for
|
||||
* @param int $id_scheduled : Scheduled id to fin medias for
|
||||
*
|
||||
* @return mixed : Medias || false
|
||||
*/
|
||||
public function get_for_scheduled_and_user(int $id_user, int $id_scheduled)
|
||||
public function gets_for_scheduled_and_user(int $id_user, int $id_scheduled)
|
||||
{
|
||||
return $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
|
||||
return $this->get_model()->gets_for_scheduled_and_user($id_user, $id_scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find medias for a sended and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_sended : Scheduled id to fin medias for
|
||||
*
|
||||
* @return mixed : Medias || false
|
||||
*/
|
||||
public function gets_for_sended_and_user(int $id_user, int $id_sended)
|
||||
{
|
||||
return $this->get_model()->gets_for_sended_and_user($id_user, $id_sended);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find medias for a received and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_received : Scheduled id to fin medias for
|
||||
*
|
||||
* @return mixed : Medias || false
|
||||
*/
|
||||
public function gets_for_received_and_user(int $id_user, int $id_received)
|
||||
{
|
||||
return $this->get_model()->gets_for_received_and_user($id_user, $id_received);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,6 +13,10 @@ namespace controllers\internals;
|
|||
|
||||
class Phone extends StandardController
|
||||
{
|
||||
const MMS_SENDING = 'sending';
|
||||
const MMS_RECEPTION = 'reception';
|
||||
const MMS_BOTH = 'both';
|
||||
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
|
@ -39,6 +43,63 @@ namespace controllers\internals;
|
|||
return $this->get_model()->get_by_name($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return bool : true if support, false else
|
||||
*/
|
||||
public function support_mms (int $id, string $type)
|
||||
{
|
||||
$phone = $this->get_model()->get($id);
|
||||
if (!$phone)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($type)
|
||||
{
|
||||
case self::MMS_SENDING :
|
||||
return $phone['adapter']::meta_support_mms_sending();
|
||||
break;
|
||||
|
||||
case self::MMS_RECEPTION :
|
||||
return $phone['adapter']::meta_support_mms_reception();
|
||||
break;
|
||||
|
||||
case self::MMS_BOTH :
|
||||
return $phone['adapter']::meta_support_mms_sending() && $phone['adapter']::meta_support_mms_reception();
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return array : array of phones supporting mms
|
||||
*/
|
||||
public function gets_phone_supporting_mms_for_user (int $id_user, string $type)
|
||||
{
|
||||
$phones = $this->get_model()->gets_for_user($id_user);
|
||||
|
||||
$valid_phones = [];
|
||||
foreach ($phones as $phone)
|
||||
{
|
||||
if ($this->support_mms($phone['id'], $type))
|
||||
{
|
||||
$valid_phones[] = $phone;
|
||||
}
|
||||
}
|
||||
|
||||
return $valid_phones;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a phone for a user by a name.
|
||||
*
|
||||
|
|
|
@ -39,10 +39,12 @@ namespace controllers\internals;
|
|||
* @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
|
||||
*/
|
||||
public function create(int $id_user, int $id_phone, $at, string $text, string $origin, string $status = 'unread', bool $command = false)
|
||||
public function create(int $id_user, int $id_phone, $at, string $text, string $origin, string $status = 'unread', bool $command = false, bool $mms = false, array $media_ids = [])
|
||||
{
|
||||
$received = [
|
||||
'id_user' => $id_user,
|
||||
|
@ -52,9 +54,39 @@ namespace controllers\internals;
|
|||
'origin' => $origin,
|
||||
'status' => $status,
|
||||
'command' => $command,
|
||||
'mms' => $mms,
|
||||
];
|
||||
|
||||
return $this->get_model()->insert($received);
|
||||
//use a transaction to ensure received and media links are created at the same time
|
||||
$this->bdd->beginTransaction();
|
||||
|
||||
$id_received = $this->get_model()->insert($received);
|
||||
if (!$id_received)
|
||||
{
|
||||
$this->bdd->rollBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
//Link medias
|
||||
$internal_media = new Media($this->bdd);
|
||||
foreach ($media_ids as $media_id)
|
||||
{
|
||||
$id_media_received = $internal_media->link_to($media_id, 'received', $id_received);
|
||||
if (!$id_media_received)
|
||||
{
|
||||
$this->bdd->rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//All ok, commit
|
||||
$success = $this->bdd->commit();
|
||||
if (!$success)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $id_received;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -211,13 +243,15 @@ 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 $media_ids : Ids of the medias to link to received
|
||||
*
|
||||
* @return array : [
|
||||
* bool 'error' => false if success, true else
|
||||
* ?string 'error_message' => null if success, error message else
|
||||
* ]
|
||||
*/
|
||||
public function receive(int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD): array
|
||||
public function receive(int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD, bool $mms = false, array $media_ids = []): array
|
||||
{
|
||||
$return = [
|
||||
'error' => false,
|
||||
|
@ -236,7 +270,7 @@ namespace controllers\internals;
|
|||
$text = $response;
|
||||
}
|
||||
|
||||
$received_id = $this->create($id_user, $id_phone, $at, $text, $origin, $status, $is_command);
|
||||
$received_id = $this->create($id_user, $id_phone, $at, $text, $origin, $status, $is_command, $mms, $media_ids);
|
||||
if (!$received_id)
|
||||
{
|
||||
$return['error'] = true;
|
||||
|
@ -251,6 +285,9 @@ namespace controllers\internals;
|
|||
'text' => $text,
|
||||
'destination' => $id_phone,
|
||||
'origin' => $origin,
|
||||
'command' => $is_command,
|
||||
'mms' => $mms,
|
||||
'medias' => $media_ids,
|
||||
];
|
||||
|
||||
$internal_webhook = new Webhook($this->bdd);
|
||||
|
|
|
@ -23,14 +23,16 @@ namespace controllers\internals;
|
|||
* @param string $text : Text of the message
|
||||
* @param ?int $id_phone : Id of the phone to send message with, null by default
|
||||
* @param bool $flash : Is the sms a flash sms, by default false
|
||||
* @param bool $mms : Is the sms a mms, by default false
|
||||
* @param array $numbers : Numbers to send message to
|
||||
* @param array $contacts_ids : Contact ids to send message to
|
||||
* @param array $groups_ids : Group ids to send message to
|
||||
* @param array $conditional_group_ids : Conditional Groups ids to send message to
|
||||
* @param array $media_ids : Ids of the medias to link to scheduled message
|
||||
*
|
||||
* @return bool : false on error, new id on success
|
||||
*/
|
||||
public function create(int $id_user, $at, string $text, ?int $id_phone = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
|
||||
public function create(int $id_user, $at, string $text, ?int $id_phone = null, bool $flash = false, bool $mms = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [], array $media_ids = [])
|
||||
{
|
||||
$scheduled = [
|
||||
'id_user' => $id_user,
|
||||
|
@ -38,6 +40,7 @@ namespace controllers\internals;
|
|||
'text' => $text,
|
||||
'id_phone' => $id_phone,
|
||||
'flash' => $flash,
|
||||
'mms' => $mms,
|
||||
];
|
||||
|
||||
if (null !== $id_phone)
|
||||
|
@ -51,12 +54,28 @@ namespace controllers\internals;
|
|||
}
|
||||
}
|
||||
|
||||
//Use transaction to garanty atomicity
|
||||
$this->bdd->beginTransaction();
|
||||
|
||||
$id_scheduled = $this->get_model()->insert($scheduled);
|
||||
if (!$id_scheduled)
|
||||
{
|
||||
$this->bdd->rollBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
$internal_media = new Media($this->bdd);
|
||||
foreach ($media_ids as $media_id)
|
||||
{
|
||||
$id_media_scheduled = $internal_media->link_to($media_id, 'scheduled', $id_scheduled);
|
||||
if (!$id_media_scheduled)
|
||||
{
|
||||
$this->bdd->rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach ($numbers as $number)
|
||||
{
|
||||
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
|
||||
|
@ -98,6 +117,12 @@ namespace controllers\internals;
|
|||
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
|
||||
}
|
||||
|
||||
$success = $this->bdd->commit();
|
||||
if (!$success)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$date = date('Y-m-d H:i:s');
|
||||
$internal_event = new Event($this->bdd);
|
||||
$internal_event->create($id_user, 'SCHEDULED_ADD', 'Ajout d\'un Sms pour le ' . $date . '.');
|
||||
|
@ -114,20 +139,23 @@ namespace controllers\internals;
|
|||
* @param string $text : Text of the message
|
||||
* @param ?int $id_phone : Id of the phone to send message with, null by default
|
||||
* @param bool $flash : Is the sms a flash sms, by default false
|
||||
* @param bool $mms : Is the sms a mms, by default false
|
||||
* @param array $numbers : Numbers to send message to
|
||||
* @param array $contacts_ids : Contact ids to send message to
|
||||
* @param array $groups_ids : Group ids to send message to
|
||||
* @param array $conditional_group_ids : Conditional Groups ids to send message to
|
||||
* @param array $media_ids : Ids of the medias to link to scheduled message
|
||||
*
|
||||
* @return bool : false on error, new id on success
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_scheduled, $at, string $text, ?string $id_phone = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
|
||||
public function update_for_user(int $id_user, int $id_scheduled, $at, string $text, ?string $id_phone = null, bool $flash = false, bool $mms = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [], array $media_ids = [])
|
||||
{
|
||||
$scheduled = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'id_phone' => $id_phone,
|
||||
'mms' => $mms,
|
||||
'flash' => $flash,
|
||||
];
|
||||
|
||||
|
@ -142,12 +170,27 @@ namespace controllers\internals;
|
|||
}
|
||||
}
|
||||
|
||||
//Ensure atomicity
|
||||
$this->bdd->beginTransaction();
|
||||
|
||||
$success = (bool) $this->get_model()->update_for_user($id_user, $id_scheduled, $scheduled);
|
||||
|
||||
$this->get_model()->delete_scheduled_numbers($id_scheduled);
|
||||
$this->get_model()->delete_scheduled_contact_relations($id_scheduled);
|
||||
$this->get_model()->delete_scheduled_group_relations($id_scheduled);
|
||||
$this->get_model()->delete_scheduled_conditional_group_relations($id_scheduled);
|
||||
$internal_media = new Media($this->bdd);
|
||||
$internal_media->unlink_all_of('scheduled', $id_scheduled);
|
||||
|
||||
foreach ($media_ids as $media_id)
|
||||
{
|
||||
$id_media_scheduled = $internal_media->link_to($media_id, 'scheduled', $id_scheduled);
|
||||
if (!$id_media_scheduled)
|
||||
{
|
||||
$this->bdd->rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($numbers as $number)
|
||||
{
|
||||
|
@ -190,7 +233,7 @@ namespace controllers\internals;
|
|||
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
|
||||
}
|
||||
|
||||
return true;
|
||||
return $this->bdd->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -224,6 +267,7 @@ namespace controllers\internals;
|
|||
|
||||
$users_settings = [];
|
||||
$users_phones = [];
|
||||
$users_mms_phones = [];
|
||||
|
||||
$now = new \DateTime();
|
||||
$now = $now->format('Y-m-d H:i:s');
|
||||
|
@ -244,7 +288,16 @@ namespace controllers\internals;
|
|||
if (!isset($users_phones[$scheduled['id_user']]))
|
||||
{
|
||||
$phones = $internal_phone->gets_for_user($scheduled['id_user']);
|
||||
$mms_phones = $internal_phone->gets_phone_supporting_mms_for_user($scheduled['id_user'], $internal_phone::MMS_SENDING);
|
||||
$users_phones[$scheduled['id_user']] = $phones ?: [];
|
||||
$users_mms_phones[$scheduled['id_user']] = $mms_phones ?: [];
|
||||
}
|
||||
|
||||
//Add medias to mms
|
||||
if ($scheduled['mms'])
|
||||
{
|
||||
$internal_media = new Media($this->bdd);
|
||||
$scheduled['medias'] = $internal_media->gets_for_scheduled_and_user($scheduled['id_user'], $scheduled['id']);
|
||||
}
|
||||
|
||||
$phone_to_use = null;
|
||||
|
@ -266,8 +319,16 @@ namespace controllers\internals;
|
|||
{
|
||||
if (null === $phone_to_use)
|
||||
{
|
||||
$rnd_key = array_rand($users_phones[$scheduled['id_user']]);
|
||||
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
|
||||
if ($scheduled['mms'])
|
||||
{
|
||||
$rnd_key = array_rand($users_mms_phones[$scheduled['id_user']]);
|
||||
$random_phone = $users_mms_phones[$scheduled['id_user']][$rnd_key];
|
||||
}
|
||||
else
|
||||
{
|
||||
$rnd_key = array_rand($users_phones[$scheduled['id_user']]);
|
||||
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
|
||||
}
|
||||
}
|
||||
|
||||
$message = [
|
||||
|
@ -276,6 +337,8 @@ namespace controllers\internals;
|
|||
'id_phone' => $phone_to_use['id'] ?? $random_phone['id'],
|
||||
'destination' => $number['number'],
|
||||
'flash' => $scheduled['flash'],
|
||||
'mms' => $scheduled['mms'],
|
||||
'medias' => $scheduled['medias'],
|
||||
];
|
||||
|
||||
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
|
||||
|
@ -326,8 +389,16 @@ namespace controllers\internals;
|
|||
|
||||
if (null === $phone_to_use)
|
||||
{
|
||||
$rnd_key = array_rand($users_phones[$scheduled['id_user']]);
|
||||
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
|
||||
if ($scheduled['mms'])
|
||||
{
|
||||
$rnd_key = array_rand($users_mms_phones[$scheduled['id_user']]);
|
||||
$random_phone = $users_mms_phones[$scheduled['id_user']][$rnd_key];
|
||||
}
|
||||
else
|
||||
{
|
||||
$rnd_key = array_rand($users_phones[$scheduled['id_user']]);
|
||||
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
|
||||
}
|
||||
}
|
||||
|
||||
$message = [
|
||||
|
@ -336,6 +407,8 @@ namespace controllers\internals;
|
|||
'id_phone' => $phone_to_use['id'] ?? $random_phone['id'],
|
||||
'destination' => $contact['number'],
|
||||
'flash' => $scheduled['flash'],
|
||||
'mms' => $scheduled['mms'],
|
||||
'medias' => $scheduled['medias'],
|
||||
];
|
||||
|
||||
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
|
||||
|
|
|
@ -26,11 +26,13 @@ namespace controllers\internals;
|
|||
* @param string $uid : Uid of the sms on the adapter service used
|
||||
* @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 string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
|
||||
*
|
||||
* @return mixed : false on error, new sended id else
|
||||
*/
|
||||
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = \models\Sended::STATUS_UNKNOWN)
|
||||
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, bool $mms = false, array $medias = [], ?string $status = \models\Sended::STATUS_UNKNOWN)
|
||||
{
|
||||
$sended = [
|
||||
'id_user' => $id_user,
|
||||
|
@ -41,10 +43,33 @@ namespace controllers\internals;
|
|||
'uid' => $uid,
|
||||
'adapter' => $adapter,
|
||||
'flash' => $flash,
|
||||
'mms' => $mms,
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
return $this->get_model()->insert($sended);
|
||||
//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)
|
||||
{
|
||||
$internal_media->link_to($media['id'], 'sended', $id_sended); //No rollback on error, keeping track of mms is more important than integrity
|
||||
}
|
||||
|
||||
if (!$this->bdd->commit())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $id_sended;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,6 +190,8 @@ namespace controllers\internals;
|
|||
* @param $text : Text of the message
|
||||
* @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 string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
|
||||
*
|
||||
* @return array : [
|
||||
|
@ -172,7 +199,7 @@ namespace controllers\internals;
|
|||
* ?string 'error_message' => null if success, error message else
|
||||
* ]
|
||||
*/
|
||||
public function send(\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, string $status = \models\Sended::STATUS_UNKNOWN): array
|
||||
public function send(\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, bool $mms = false, array $medias = [], string $status = \models\Sended::STATUS_UNKNOWN): array
|
||||
{
|
||||
$return = [
|
||||
'error' => false,
|
||||
|
@ -180,19 +207,28 @@ namespace controllers\internals;
|
|||
];
|
||||
|
||||
$at = (new \DateTime())->format('Y-m-d H:i:s');
|
||||
$response = $adapter->send($destination, $text, $flash);
|
||||
$media_uris = [];
|
||||
foreach ($medias as $media)
|
||||
{
|
||||
$media_uris[] = [
|
||||
'http_url' => HTTP_PWD . '/data/' . $media['path'],
|
||||
'local_uri' => PWD_DATA . '/data/' . $media['path'],
|
||||
];
|
||||
}
|
||||
|
||||
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);
|
||||
|
||||
if ($response['error'])
|
||||
{
|
||||
$return['error'] = true;
|
||||
$return['error_message'] = $response['error_message'];
|
||||
$status = \models\Sended::STATUS_FAILED;
|
||||
$this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $status);
|
||||
$this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $status);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $status);
|
||||
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $status);
|
||||
|
||||
$sended = [
|
||||
'id' => $sended_id,
|
||||
|
|
|
@ -266,7 +266,8 @@ namespace controllers\internals;
|
|||
return $result;
|
||||
}
|
||||
|
||||
$result['extension'] = pathinfo($file['name'])['extension'];
|
||||
$result['tmp_name'] = $tmp_filename;
|
||||
$result['extension'] = pathinfo($file['name'], PATHINFO_EXTENSION);
|
||||
$result['mime_type'] = mime_content_type($tmp_filename);
|
||||
|
||||
$file_handler = fopen($tmp_filename, 'r');
|
||||
|
@ -277,13 +278,18 @@ namespace controllers\internals;
|
|||
}
|
||||
|
||||
/**
|
||||
* Allow to upload file.
|
||||
* Allow to save an uploaded file from the $_FILE['file'] array
|
||||
*
|
||||
* @param array $file : The array extracted from $_FILES['file']
|
||||
* @param string $dirpath : The directory to save the file in
|
||||
* @param bool $override : If true, override the file if another file with this name exists
|
||||
* @param ?string $filename : The name to use for the file, if null use a highly random name
|
||||
* @param ?string $extension : The extension to use for the file, if null try to determine it using original file extension, then mime_type
|
||||
* @param bool $use_mimetype : If true, ignore original file extension to determine final file extension and use file real mimetype instead
|
||||
*
|
||||
* @return array : ['success' => bool, 'content' => file path | error message, 'error_code' => $file['error']]
|
||||
* @return array : ['success' => bool, 'content' => new file name | error message, 'error_code' => $file['error']]
|
||||
*/
|
||||
public static function upload_file(array $file)
|
||||
public static function save_uploaded_file(array $file, string $dirpath, bool $override = false, ?string $filename = null, ?string $extension = null, bool $use_mimetype = false)
|
||||
{
|
||||
$result = [
|
||||
'success' => false,
|
||||
|
@ -291,82 +297,61 @@ namespace controllers\internals;
|
|||
'error_code' => $file['error'] ?? 99,
|
||||
];
|
||||
|
||||
if (UPLOAD_ERR_OK !== $file['error'])
|
||||
$upload_info = self::read_uploaded_file($file);
|
||||
if (!$upload_info['success'])
|
||||
{
|
||||
switch ($file['error'])
|
||||
$result['content'] = $upload_info['content'];
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($extension === null)
|
||||
{
|
||||
$extension = $upload_info['extension'];
|
||||
if ($extension === '' || $use_mimetype)
|
||||
{
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les ' . ini_get('upload_max_filesize') / (1000 * 1000) . ' Mégaoctets.';
|
||||
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
$result['content'] = 'Le fichier dépasse la limite de taille.';
|
||||
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
$result['content'] = 'L\'envoi du fichier a été interrompu.';
|
||||
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
$result['content'] = 'Aucun fichier n\'a été envoyé.';
|
||||
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
$result['content'] = 'Le serveur ne dispose pas de fichier temporaire permettant l\'envoi de fichiers.';
|
||||
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
$result['content'] = 'Impossible d\'envoyer le fichier car il n\'y a plus de place sur le serveur.';
|
||||
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
$result['content'] = 'Le serveur a interrompu l\'envoi du fichier.';
|
||||
|
||||
break;
|
||||
$mimey = new \Mimey\MimeTypes;
|
||||
$extension = $mimey->getExtension($upload_info['mime_type']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
$tmp_filename = $file['tmp_name'] ?? false;
|
||||
if (!$tmp_filename || !is_readable($tmp_filename))
|
||||
if ($filename === null)
|
||||
{
|
||||
return $result;
|
||||
$filename = self::random_uuid();
|
||||
}
|
||||
|
||||
$md5_filename = md5_file($tmp_filename);
|
||||
if (!$md5_filename)
|
||||
$filename = $filename . '.' . $extension;
|
||||
$filepath = $dirpath . '/' . $filename;
|
||||
|
||||
if (file_exists($filepath) && !$override)
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
||||
$new_file_path = PWD_DATA . '/' . $md5_filename;
|
||||
|
||||
if (file_exists($new_file_path))
|
||||
{
|
||||
$result['success'] = true;
|
||||
$result['content'] = $new_file_path;
|
||||
$result['content'] = 'Le fichier ' . $filepath . ' existe déjà.';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
$success = move_uploaded_file($tmp_filename, $new_file_path);
|
||||
$success = move_uploaded_file($upload_info['tmp_name'], $filepath);
|
||||
if (!$success)
|
||||
{
|
||||
$result['content'] = 'Impossible d\'écrire le fichier sur le serveur.';
|
||||
$result['content'] = 'Impossible de délplacer le fichier vers ' . $filepath;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['success'] = true;
|
||||
$result['content'] = $new_file_path;
|
||||
$result['content'] = $filename;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue