2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
|
|
|
* Some tools frequently used.
|
2020-01-17 18:19:25 +01:00
|
|
|
* Not a standard controller as it's not linked to a model in any way.
|
2019-11-14 02:02:50 +01:00
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
class Tool extends \descartes\InternalController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cette fonction parse un numéro pour le retourner sans espaces, etc.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $number : Le numéro de téléphone à parser
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return mixed : Si le numéro est bien un numéro de téléphone, on retourne le numéro parsé. Sinon, on retourne faux
|
|
|
|
*/
|
|
|
|
public static function parse_phone($number)
|
|
|
|
{
|
2019-11-30 05:31:20 +01:00
|
|
|
try
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-11-30 05:31:20 +01:00
|
|
|
$phone_number_util = \libphonenumber\PhoneNumberUtil::getInstance();
|
|
|
|
$phone_number_o = $phone_number_util->parse($number, null);
|
|
|
|
|
|
|
|
$valid = $phone_number_util->isValidNumber($phone_number_o);
|
|
|
|
|
|
|
|
if (!$valid)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
return $phone_number_util->format($phone_number_o, \libphonenumber\PhoneNumberFormat::E164);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
catch (\Exception $e)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
|
|
|
* Cette fonction parse un numéro pour le retourner avec des espaces, etc.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $number : Le numéro de téléphone à parser
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return mixed : Si le numéro est bien un numéro de téléphone, on retourne le numéro parsé. Sinon, on retourne faux
|
|
|
|
*/
|
2019-11-10 01:17:28 +01:00
|
|
|
public static function phone_format($number)
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-11-30 05:31:20 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$phone_number_util = \libphonenumber\PhoneNumberUtil::getInstance();
|
|
|
|
$phone_number_o = $phone_number_util->parse($number, null);
|
2019-11-10 17:36:42 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
return $phone_number_util->format($phone_number_o, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
catch (\Exception $e)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
2020-04-03 03:38:35 +02:00
|
|
|
return false;
|
2019-11-30 05:31:20 +01:00
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-29 05:29:03 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Format a number and make a link to a discussion with this number.
|
|
|
|
*
|
2019-11-29 05:29:03 +01:00
|
|
|
* @param string $number : Number to format and make a link for
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-29 05:29:03 +01:00
|
|
|
* @return string : Link to the number
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function phone_link($number)
|
2019-11-29 05:29:03 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$number_format = self::phone_format($number);
|
2019-11-29 05:29:03 +01:00
|
|
|
$url = \descartes\Router::url('Discussion', 'show', ['number' => $number]);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-17 18:47:08 +01:00
|
|
|
return '<a href="' . self::s($url, false, true, false) . '">' . self::s($number_format, false, true, false) . '</a>';
|
2019-11-29 05:29:03 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
|
|
|
* Cette fonction fait la correspondance entre un type d'evenement et une icone font awesome.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $type : Le type de l'évenement à analyser
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return string : Le nom de l'icone à afficher (ex : fa-user)
|
|
|
|
*/
|
|
|
|
public static function event_type_to_icon($type)
|
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case 'USER_ADD':
|
|
|
|
$logo = 'fa-user';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
break;
|
|
|
|
case 'CONTACT_ADD':
|
|
|
|
$logo = 'fa-user';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
break;
|
|
|
|
case 'GROUP_ADD':
|
|
|
|
$logo = 'fa-group';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2020-09-27 02:01:29 +02:00
|
|
|
break;
|
|
|
|
case 'CONDITIONAL_GROUP_ADD':
|
|
|
|
$logo = 'fa-bullseye';
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
break;
|
|
|
|
case 'SCHEDULED_ADD':
|
|
|
|
$logo = 'fa-calendar';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
break;
|
|
|
|
case 'COMMAND_ADD':
|
|
|
|
$logo = 'fa-terminal';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$logo = 'fa-question';
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return $logo;
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction vérifie une date.
|
|
|
|
*
|
|
|
|
* @param string $date : La date a valider
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $format : Le format de la date
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
|
|
|
* @return bool : Vrai si la date et valide, faux sinon
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public static function validate_date($date, $format)
|
|
|
|
{
|
|
|
|
$objectDate = \DateTime::createFromFormat($format, $date);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
return $objectDate && $objectDate->format($format) === $date;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne un mot de passe généré aléatoirement.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param int $length : Taille du mot de passe à générer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return string : Le mot de passe aléatoire
|
|
|
|
*/
|
|
|
|
public static function generate_password($length)
|
|
|
|
{
|
|
|
|
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-@()?.:!%*$&/';
|
|
|
|
$password = '';
|
|
|
|
$chars_length = mb_strlen($chars) - 1;
|
|
|
|
$i = 0;
|
2019-10-30 00:30:39 +01:00
|
|
|
while ($i < $length)
|
|
|
|
{
|
|
|
|
++$i;
|
2019-10-29 18:36:25 +01:00
|
|
|
$password .= $chars[rand(0, $chars_length)];
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction vérifie si un utilisateur et connecté, et si il ne l'est pas, redirige sur la page de connexion.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public static function verifyconnect()
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!isset($_SESSION['connect']) || !$_SESSION['connect'])
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
header('Location: /');
|
|
|
|
die();
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-11 00:22:40 +01:00
|
|
|
/**
|
|
|
|
* Check if the user connected.
|
|
|
|
*
|
|
|
|
* @return bool : True if connected, False else
|
|
|
|
*/
|
|
|
|
public static function is_connected()
|
|
|
|
{
|
|
|
|
return (bool) ($_SESSION['connect'] ?? false);
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
|
|
|
* Check if the user is admin.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
|
|
|
* @return bool : True if admin, False else
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public static function is_admin()
|
|
|
|
{
|
2019-11-10 00:27:42 +01:00
|
|
|
return (bool) ($_SESSION['user']['admin'] ?? false);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Allow to read an uploaded file.
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @param array $file : The array extracted from $_FILES['file']
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @return array : ['success' => bool, 'content' => file handler | error message, 'error_code' => $file['error']]
|
|
|
|
*/
|
|
|
|
public static function read_uploaded_file(array $file)
|
|
|
|
{
|
|
|
|
$result = [
|
|
|
|
'success' => false,
|
|
|
|
'content' => 'Une erreur inconnue est survenue.',
|
|
|
|
'error_code' => $file['error'] ?? 99,
|
|
|
|
'mime_type' => false,
|
|
|
|
];
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (UPLOAD_ERR_OK !== $file['error'])
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
switch ($file['error'])
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
2020-01-17 18:47:08 +01:00
|
|
|
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les ' . ini_get('upload_max_filesize') / (1000 * 1000) . ' Mégaoctets.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
2019-11-30 05:31:20 +01:00
|
|
|
$result['content'] = 'Le fichier dépasse la limite de taille.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_PARTIAL:
|
2019-11-30 05:31:20 +01:00
|
|
|
$result['content'] = 'L\'envoi du fichier a été interrompu.';
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_NO_FILE:
|
2019-11-30 05:31:20 +01:00
|
|
|
$result['content'] = 'Aucun fichier n\'a été envoyé.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
2019-11-30 05:31:20 +01:00
|
|
|
$result['content'] = 'Le serveur ne dispose pas de fichier temporaire permettant l\'envoi de fichiers.';
|
|
|
|
|
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
$result['content'] = 'Impossible d\'envoyer le fichier car il n\'y a plus de place sur le serveur.';
|
2019-11-30 05:31:20 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
2019-11-30 05:31:20 +01:00
|
|
|
$result['content'] = 'Le serveur a interrompu l\'envoi du fichier.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
return $result;
|
2019-11-30 05:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$tmp_filename = $file['tmp_name'] ?? false;
|
|
|
|
if (!$tmp_filename || !is_readable($tmp_filename))
|
|
|
|
{
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
$result['mime_type'] = 'text/plain' === mime_content_type($tmp_filename) ? $file['type'] : mime_content_type($tmp_filename);
|
2019-11-30 05:31:20 +01:00
|
|
|
|
|
|
|
$file_handler = fopen($tmp_filename, 'r');
|
|
|
|
$result['success'] = true;
|
|
|
|
$result['content'] = $file_handler;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Allow to upload file.
|
|
|
|
*
|
2019-12-04 03:04:45 +01:00
|
|
|
* @param array $file : The array extracted from $_FILES['file']
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-12-04 03:04:45 +01:00
|
|
|
* @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,
|
|
|
|
];
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (UPLOAD_ERR_OK !== $file['error'])
|
2019-12-04 03:04:45 +01:00
|
|
|
{
|
|
|
|
switch ($file['error'])
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
2020-01-17 18:47:08 +01:00
|
|
|
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les ' . ini_get('upload_max_filesize') / (1000 * 1000) . ' Mégaoctets.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
2019-12-04 03:04:45 +01:00
|
|
|
$result['content'] = 'Le fichier dépasse la limite de taille.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_PARTIAL:
|
2019-12-04 03:04:45 +01:00
|
|
|
$result['content'] = 'L\'envoi du fichier a été interrompu.';
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_NO_FILE:
|
2019-12-04 03:04:45 +01:00
|
|
|
$result['content'] = 'Aucun fichier n\'a été envoyé.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
2019-12-04 03:04:45 +01:00
|
|
|
$result['content'] = 'Le serveur ne dispose pas de fichier temporaire permettant l\'envoi de fichiers.';
|
|
|
|
|
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
$result['content'] = 'Impossible d\'envoyer le fichier car il n\'y a plus de place sur le serveur.';
|
2019-12-04 03:04:45 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
2019-12-04 03:04:45 +01:00
|
|
|
$result['content'] = 'Le serveur a interrompu l\'envoi du fichier.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
return $result;
|
2019-12-04 03:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:47:08 +01:00
|
|
|
$new_file_path = PWD_DATAS . '/' . $md5_filename;
|
2019-12-04 03:04:45 +01:00
|
|
|
|
|
|
|
if (file_exists($new_file_path))
|
|
|
|
{
|
|
|
|
$result['success'] = true;
|
|
|
|
$result['content'] = $new_file_path;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = move_uploaded_file($tmp_filename, $new_file_path);
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
$result['content'] = 'Impossible d\'écrire le fichier sur le serveur.';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-04 03:04:45 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result['success'] = true;
|
|
|
|
$result['content'] = $new_file_path;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|