Add support for mms

This commit is contained in:
osaajani 2019-12-04 03:04:45 +01:00
parent 88b00e4e9f
commit fb10b9cdfd
20 changed files with 979 additions and 118 deletions

133
controllers/internals/Media.php Executable file
View file

@ -0,0 +1,133 @@
<?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 Media extends StandardController
{
protected $model = null;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Media($this->bdd);
return $this->model;
}
/**
* 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
* @return bool : false on error, new media id else
*/
public function create (int $id_user, int $id_scheduled, array $media) : 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 ($result_upload_media['success'] == false)
{
return false;
}
$datas = [
'id_scheduled' => $id_scheduled,
'path' => $result_upload_media['content'],
];
return (bool) $this->get_model()->insert($datas);
}
/**
* Update a media for a user
* @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
{
$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);
}
/**
* Delete a media for a user
* @param int $id_user : User id
* @param int $id : Entry id
* @return int : Number of removed rows
*/
public function delete_for_user (int $id_user, int $id_media) : bool
{
$media = $this->get_model()->get_for_user($id_user, $id_media);
if (!$media)
{
return false;
}
unlink($media['path']);
return $this->get_model()->delete_for_user($id_user, $id);
}
/**
* 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
* @return mixed : Medias || false
*/
public function get_for_scheduled_and_user (int $id_user, int $id_scheduled)
{
return $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
}
}

View file

@ -288,4 +288,89 @@ namespace controllers\internals;
return $result;
}
/**
* Allow to upload file
* @param array $file : The array extracted from $_FILES['file']
* @return array : ['success' => bool, 'content' => file path | error message, 'error_code' => $file['error']]
*/
public static function upload_file(array $file)
{
$result = [
'success' => false,
'content' => 'Une erreur inconnue est survenue.',
'error_code' => $file['error'] ?? 99,
];
if ($file['error'] !== UPLOAD_ERR_OK)
{
switch ($file['error'])
{
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;
}
return $result;
}
$tmp_filename = $file['tmp_name'] ?? false;
if (!$tmp_filename || !is_readable($tmp_filename))
{
return $result;
}
$md5_filename = md5_file($tmp_filename);
if (!$md5_filename)
{
return $result;
}
$new_file_path = PWD_DATAS . '/' . $md5_filename;
if (file_exists($new_file_path))
{
$result['success'] = true;
$result['content'] = $new_file_path;
return $result;
}
$success = move_uploaded_file($tmp_filename, $new_file_path);
if (!$success)
{
$result['content'] = 'Impossible d\'écrire le fichier sur le serveur.';
return $result;
}
$result['success'] = true;
$result['content'] = $new_file_path;
return $result;
}
}

View file

@ -0,0 +1,71 @@
<?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 Webhook extends StandardController
{
protected $model = null;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Webhook($this->bdd);
return $this->model;
}
/**
* Create a new webhook
* @param int $id_user : User id
* @param string $url : Webhook url
* @param string $type : Webhook type
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function create(int $id_user, string $url, string $type)
{
$webhook = [
'id_user' => $id_user,
'url' => $url,
'type' => $type,
];
$result = $this->get_model()->insert($webhook);
if (!$result)
{
return false;
}
return $result;
}
/**
* Update a webhook
* @param int $id_user : User id
* @param int $id : Webhook id
* @param string $url : Webhook url
* @param string $type : Webhook type
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function update_for_user(int $id_user, int $id, string $url, string $type)
{
$datas = [
'url' => $url,
'type' => $type,
];
return $this->get_model()->update_for_user($id_user, $id, $datas);
}
}

View file

@ -21,6 +21,7 @@ namespace controllers\publics;
private $internal_contact;
private $internal_group;
private $internal_conditional_group;
private $internal_media;
/**
* Cette fonction est appelée avant toute les autres :
@ -36,6 +37,7 @@ namespace controllers\publics;
$this->internal_contact = new \controllers\internals\Contact($bdd);
$this->internal_group = new \controllers\internals\Group($bdd);
$this->internal_conditional_group = new \controllers\internals\ConditionalGroup($bdd);
$this->internal_media = new \controllers\internals\Media($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -155,6 +157,8 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
$id_user = $_SESSION['user']['id'];
$all_contacts = $this->internal_contact->gets_for_user($_SESSION['user']['id']);
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
$scheduleds = $this->internal_scheduled->gets_in_for_user($_SESSION['user']['id'], $ids);
@ -190,6 +194,9 @@ namespace controllers\publics;
$scheduleds[$key]['groups'][] = (int) $group['id'];
}
$media = $this->internal_media->get_for_scheduled_and_user($id_user, $scheduled['id']);
$scheduleds[$key]['media'] = $media;
$conditional_groups = $this->internal_scheduled->get_conditional_groups($scheduled['id']);
foreach ($conditional_groups as $conditional_group)
{
@ -213,6 +220,7 @@ namespace controllers\publics;
* @param string $_POST['numbers'] : Les numeros de téléphone du scheduled
* @param string $_POST['contacts'] : Les contacts du scheduled
* @param string $_POST['groups'] : Les groups du scheduled
* @param array $_FILES['media'] : The media to link to a scheduled
*/
public function create($csrf)
{
@ -282,13 +290,28 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le '.$at.'.');
//If mms is enabled, try to process a media to link to the scheduled
$media = $_FILES['media'] ?? false;
if (!($_SESSION['user']['settings']['mms'] ?? false) || !$media)
{
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le '.$at.'.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
$success = $this->internal_media->create($id_user, $scheduled_id, $media);
if (!$success)
{
\FlashMessage\FlashMessage::push('success', 'Le SMS a bien été créé mais le média n\'as pas pu être enregistré.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le '.$at.'.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
/**
* Cette fonction met à jour une schedulede.
* Cette fonction met à jour un scheduled.
*
* @param $csrf : Le jeton CSRF
* @param array $_POST['scheduleds'] : Un tableau des scheduledes avec leur nouvelle valeurs + les numbers, contacts et groups liées
@ -306,8 +329,8 @@ namespace controllers\publics;
$scheduleds = $_POST['scheduleds'] ?? [];
$all_update_ok = true;
$nb_update = 0;
foreach ($scheduleds as $id_scheduled => $scheduled)
{
$id_user = $_SESSION['user']['id'];
@ -323,22 +346,17 @@ namespace controllers\publics;
$scheduled = $this->internal_scheduled->get($id_scheduled);
if (!$scheduled || $scheduled['id_user'] !== $id_user)
{
$all_update_ok = false;
continue;
}
if (empty($text))
{
$all_update_ok = false;
continue;
}
if (!\controllers\internals\Tool::validate_date($at, 'Y-m-d H:i:s') && !\controllers\internals\Tool::validate_date($at, 'Y-m-d H:i'))
{
$all_update_ok = false;
continue;
}
@ -357,30 +375,43 @@ namespace controllers\publics;
if (!$numbers && !$contacts && !$groups && !$conditional_groups)
{
$all_update_ok = false;
continue;
}
if ($origin && !$this->internal_phone->get_by_number_and_user($id_user, $origin))
{
\FlashMessage\FlashMessage::push('danger', 'Ce numéro n\'existe pas ou vous n\'en êtes pas propriétaire.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
continue;
}
$success = $this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $origin, $flash, $numbers, $contacts, $groups, $conditional_groups);
if (!$success)
{
$all_update_ok = false;
//Check for media
$current_media = $scheduled['current_media'] ?? false;
if (!$current_media)
{
$this->internal_media->delete_for_scheduled_and_user($id_user, $id_scheduled);
}
$media = $_FILES['media_' . $id_scheduled] ?? false;
if (!$media)
{
$nb_update += (int) $success;
continue;
}
$success = $this->internal_media->create($id_user, $id_scheduled, $media);
if (!$success)
{
continue;
}
$nb_update += 1;
}
if (!$all_update_ok)
if (!$nb_update != count($scheduleds))
{
\FlashMessage\FlashMessage::push('danger', 'Certains SMS n\'ont pas pu êtres mis à jour.');
\FlashMessage\FlashMessage::push('danger', 'Certains SMS n\'ont pas été mis à jour.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}

178
controllers/publics/Webhook.php Executable file
View file

@ -0,0 +1,178 @@
<?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\publics;
/**
* Page des webhooks.
*/
class Webhook extends \descartes\Controller
{
private $internal_webhook;
private $internal_event;
public function __construct()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_webhook = new \controllers\internals\Webhook($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
\controllers\internals\Tool::verifyconnect();
}
/**
* List all webhooks
*
* @param mixed $page
*/
public function list($page = 0)
{
$page = (int) $page;
$webhooks = $this->internal_webhook->list_for_user($_SESSION['user']['id'], 25, $page);
$this->render('webhook/list', ['webhooks' => $webhooks]);
}
/**
* Delete a list of webhooks
*
* @param array int $_GET['ids'] : Les id des webhooks à supprimer
* @param mixed $csrf
*
* @return boolean;
*/
public function delete($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
$this->redirect(\descartes\Router::url('Webhook', 'list'));
return false;
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$this->internal_webhook->delete_for_user($_SESSION['user']['id'], $id);
}
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
/**
* Cette fonction retourne la page d'ajout d'une webhook.
*/
public function add()
{
$this->render('webhook/add');
}
/**
* Edit a list of webhooks
*
* @param array int $_GET['ids'] : ids of webhooks to edit
*/
public function edit()
{
$ids = $_GET['ids'] ?? [];
$webhooks = $this->internal_webhook->gets_in_for_user($_SESSION['user']['id'], $ids);
$this->render('webhook/edit', [
'webhooks' => $webhooks,
]);
}
/**
* Insert a new webhook
*
* @param $csrf : Le jeton CSRF
* @param string $_POST['url'] : URL to call on webhook release
* @param string $_POST['type'] : Type of webhook, either 'send_sms' or 'receive_sms'
*
* @return boolean;
*/
public function create($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
$url = $_POST['url'] ?? false;
$type = $_POST['type'] ?? false;
if (!$url || !$type)
{
\FlashMessage\FlashMessage::push('danger', 'Renseignez au moins une URL et un type de webhook.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
if (!$this->internal_webhook->create($_SESSION['user']['id'], $url, $type))
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce webhook.');
return $this->redirect(\descartes\Router::url('webhooks', 'add'));
}
\FlashMessage\FlashMessage::push('success', 'La webhook a bien été créé.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
/**
* Cette fonction met à jour une webhook.
*
* @param $csrf : Le jeton CSRF
* @param array $_POST['webhooks'] : Un tableau des webhooks avec leur nouvelle valeurs
*
* @return boolean;
*/
public function update($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
$nb_update = 0;
foreach ($_POST['webhooks'] as $webhook)
{
$url = $webhook['url'] ?? false;
$type = $webhook['type'] ?? false;
if (!$url || !$type)
{
continue;
}
$success = $this->internal_webhook->update_for_user($_SESSION['user']['id'], $webhook['id'], $url, $type);
$nb_update += (int) $success;
}
if ($nb_update !== \count($_POST['webhooks']))
{
\FlashMessage\FlashMessage::push('info', 'Certains webhooks n\'ont pas pu êtres mis à jour.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Tous les webhooks ont été modifiés avec succès.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
}