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);
}
}