mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Move to raspisms dir
This commit is contained in:
parent
34a6f7de65
commit
40fccf133c
278 changed files with 109 additions and 2020 deletions
|
@ -1,119 +0,0 @@
|
|||
<?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 to interact with adapters.
|
||||
*/
|
||||
class Adapter extends \descartes\InternalController
|
||||
{
|
||||
private const ADAPTERS_FILES_END = 'Adapter.php';
|
||||
private const ADAPTERS_META_START = 'meta_';
|
||||
|
||||
/**
|
||||
* List adapters using internal metas.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function list_adapters()
|
||||
{
|
||||
$adapters = [];
|
||||
|
||||
$files = $this->list_files();
|
||||
if (!$files)
|
||||
{
|
||||
return $adapters;
|
||||
}
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
$metas = $this->read_adapter_metas($file);
|
||||
if (!$metas)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$adapters[] = $metas;
|
||||
}
|
||||
|
||||
return $adapters;
|
||||
}
|
||||
|
||||
/**
|
||||
* List Adapters files.
|
||||
*
|
||||
* @return mixed (false|array) : array of adapters files path
|
||||
*/
|
||||
public function list_files()
|
||||
{
|
||||
if (!is_readable(PWD_ADAPTERS))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$adapters_files = [];
|
||||
|
||||
$files = scandir(PWD_ADAPTERS);
|
||||
foreach ($files as $filename)
|
||||
{
|
||||
$len = mb_strlen(self::ADAPTERS_FILES_END);
|
||||
$end = mb_substr($filename, -$len);
|
||||
if (self::ADAPTERS_FILES_END !== $end)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$adapters_files[] = PWD_ADAPTERS . '/' . $filename;
|
||||
}
|
||||
|
||||
return $adapters_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read constants of an adapter.
|
||||
*
|
||||
* @param mixed $adapter_file
|
||||
*
|
||||
* @return mixed(array|bool) : False on error, array of constants name => value
|
||||
*/
|
||||
public function read_adapter_metas($adapter_file)
|
||||
{
|
||||
$metas = [];
|
||||
|
||||
if (!is_readable($adapter_file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$adapter_classname = pathinfo($adapter_file, PATHINFO_FILENAME);
|
||||
$reflection_class = new \ReflectionClass('\adapters\\' . $adapter_classname);
|
||||
if (!$reflection_class)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$methods = $reflection_class->getMethods(\ReflectionMethod::IS_STATIC);
|
||||
foreach ($methods as $method)
|
||||
{
|
||||
$start_with = mb_substr($method->getName(), 0, mb_strlen(self::ADAPTERS_META_START));
|
||||
|
||||
if (self::ADAPTERS_META_START !== $start_with)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$metas[$method->getName()] = $method->invoke(null);
|
||||
}
|
||||
|
||||
return $metas;
|
||||
}
|
||||
}
|
|
@ -1,151 +0,0 @@
|
|||
<?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 Command extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new command.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Command name
|
||||
* @param string $script : Script file
|
||||
* @param bool $admin : Is command admin only
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create command, id of the new command else
|
||||
*/
|
||||
public function create(int $id_user, string $name, string $script, bool $admin)
|
||||
{
|
||||
$command = [
|
||||
'id_user' => $id_user,
|
||||
'name' => $name,
|
||||
'script' => $script,
|
||||
'admin' => $admin,
|
||||
];
|
||||
|
||||
$result = $this->get_model()->insert($command);
|
||||
if (!$result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$internal_event = new Event($this->bdd);
|
||||
$internal_event->create($id_user, 'COMMAND_ADD', 'Ajout commande : ' . $name . ' => ' . $script);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a command.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id : Command id
|
||||
* @param string $name : Command name
|
||||
* @param string $script : Script file
|
||||
* @param bool $admin : Is command admin only
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create command, id of the new command else
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id, string $name, string $script, bool $admin)
|
||||
{
|
||||
$datas = [
|
||||
'name' => $name,
|
||||
'script' => $script,
|
||||
'admin' => $admin,
|
||||
];
|
||||
|
||||
return $this->get_model()->update_for_user($id_user, $id, $datas);
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyse a message to check if it's a command and extract it.
|
||||
*
|
||||
* @param int $id_user : User id to search a command for
|
||||
* @param string $message : Text of the message to analyse
|
||||
*
|
||||
* @return mixed : false on error, array with new text and command to execute ['updated_text' => string, 'command' => string]
|
||||
*/
|
||||
public function check_for_command(int $id_user, string $message)
|
||||
{
|
||||
$extracted_command = [];
|
||||
|
||||
$decode_message = json_decode(trim($message), true);
|
||||
if (null === $decode_message)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($decode_message['login'], $decode_message['password'], $decode_message['command']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Check for user
|
||||
$internal_user = new \controllers\internals\User($this->bdd);
|
||||
$user = $internal_user->check_credentials($decode_message['login'], $decode_message['password']);
|
||||
if (!$user || $user['id'] !== $id_user)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Find command
|
||||
$commands = $this->gets_for_user($user['id']);
|
||||
$find_command = false;
|
||||
foreach ($commands as $command)
|
||||
{
|
||||
if ($decode_message['command'] === $command['name'])
|
||||
{
|
||||
$find_command = $command;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $find_command)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Check for admin rights
|
||||
if ($find_command['admin'] && !$user['admin'])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Forge command and return
|
||||
$decode_message['password'] = '******';
|
||||
$updated_text = json_encode($decode_message);
|
||||
|
||||
$generated_command = PWD_SCRIPTS . '/' . $find_command['script'];
|
||||
$args = $decode_message['args'] ?? '';
|
||||
$generated_command .= ' ' . escapeshellcmd($args);
|
||||
|
||||
return [
|
||||
'updated_text' => $updated_text,
|
||||
'command' => $generated_command,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Command($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,142 +0,0 @@
|
|||
<?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 ConditionalGroup extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new group for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $name : Group name
|
||||
* @param string $condition : Condition for forming group content
|
||||
*
|
||||
* @return mixed bool|int : false on error, new group id
|
||||
*/
|
||||
public function create(int $id_user, string $name, string $condition)
|
||||
{
|
||||
$conditional_group = [
|
||||
'id_user' => $id_user,
|
||||
'name' => $name,
|
||||
'condition' => $condition,
|
||||
];
|
||||
|
||||
$internal_ruler = new Ruler();
|
||||
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['datas' => (object) null]]);
|
||||
if (!$valid_condition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$id_group = $this->get_model()->insert($conditional_group);
|
||||
if (!$id_group)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$internal_event = new Event($this->bdd);
|
||||
$internal_event->create($id_user, 'CONDITIONAL_GROUP_ADD', 'Ajout du groupe conditionnel : ' . $name);
|
||||
|
||||
return $id_group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a group for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_group : Group id
|
||||
* @param string $name : Group name
|
||||
* @param string $condition : Condition for forming group content
|
||||
*
|
||||
* @return bool : False on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_group, string $name, string $condition)
|
||||
{
|
||||
$conditional_group = [
|
||||
'name' => $name,
|
||||
'condition' => $condition,
|
||||
];
|
||||
|
||||
$internal_ruler = new Ruler();
|
||||
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['datas' => (object) null]]);
|
||||
if (!$valid_condition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->get_model()->update_for_user($id_user, $id_group, $conditional_group);
|
||||
if (!$result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a group by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Group name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_for_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->get_model()->get_by_name_for_user($id_user, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's contacts that respects a condition.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $condition : Condition string to verify
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_contacts_for_condition_and_user(int $id_user, string $condition): array
|
||||
{
|
||||
$internal_contacts = new Contact($this->bdd);
|
||||
$contacts = $internal_contacts->gets_for_user($id_user);
|
||||
|
||||
$ruler = new Ruler();
|
||||
|
||||
foreach ($contacts as $key => $contact)
|
||||
{
|
||||
$contact['datas'] = json_decode($contact['datas']);
|
||||
$contact = (object) $contact;
|
||||
|
||||
$datas = ['contact' => $contact];
|
||||
$is_valid = $ruler->evaluate_condition($condition, $datas);
|
||||
if (!$is_valid)
|
||||
{
|
||||
unset($contacts[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $contacts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\ConditionalGroup($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
<?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 to call the console scripts.
|
||||
*/
|
||||
class Console extends \descartes\InternalController
|
||||
{
|
||||
/**
|
||||
* Start launcher daemon.
|
||||
*/
|
||||
public function launcher()
|
||||
{
|
||||
new \daemons\Launcher();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start sender daemon.
|
||||
*/
|
||||
public function sender()
|
||||
{
|
||||
new \daemons\Sender();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start webhook daemon.
|
||||
*/
|
||||
public function webhook()
|
||||
{
|
||||
new \daemons\Webhook();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a phone daemon.
|
||||
*
|
||||
* @param $id_phone : Phone id
|
||||
*/
|
||||
public function phone($id_phone)
|
||||
{
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$internal_phone = new \controllers\internals\Phone($bdd);
|
||||
|
||||
$phone = $internal_phone->get($id_phone);
|
||||
if (!$phone)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
new \daemons\Phone($phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction retourne la fenetre de connexion.
|
||||
*
|
||||
* @param mixed $id_phone
|
||||
*/
|
||||
public function test($id_phone)
|
||||
{
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$internal_phone = new \controllers\internals\Phone($bdd);
|
||||
$phone = $internal_phone->get($id_phone);
|
||||
if (!$phone)
|
||||
{
|
||||
echo "No phone for id : {$id_phone}\n";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
echo "Found phone for id : {$id_phone}\n";
|
||||
|
||||
$adapter_classname = $phone['adapter'];
|
||||
$adapter = new $adapter_classname($phone['number'], $phone['adapter_datas']);
|
||||
|
||||
//Try send a message
|
||||
/*
|
||||
$destination = '+33669529042';
|
||||
$text = "Coucou c'est pour un test !";
|
||||
$flash = false;
|
||||
$uid = $adapter->send($destination, $text, $flash);
|
||||
|
||||
if (!$uid)
|
||||
{
|
||||
echo "Cannot send message to $destination\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
echo "Send a message to $destination with uid $uid \n";
|
||||
*/
|
||||
$smss = $adapter->read();
|
||||
var_dump($smss);
|
||||
}
|
||||
}
|
|
@ -1,351 +0,0 @@
|
|||
<?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 Contact extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Return a contact for a user by a number.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $number : Contact number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number_and_user(int $id_user, string $number)
|
||||
{
|
||||
return $this->get_model()->get_by_number_and_user($id_user, $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a contact by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Contact name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_and_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->get_model()->get_by_name_and_user($id_user, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all contacts of a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
return $this->get_model()->gets_for_user($id_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new contact.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $number : Contact number
|
||||
* @param string $name : Contact name
|
||||
* @param string $datas : Contact datas
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create contact, id of the new contact else
|
||||
*/
|
||||
public function create($id_user, $number, $name, $datas)
|
||||
{
|
||||
$contact = [
|
||||
'id_user' => $id_user,
|
||||
'number' => $number,
|
||||
'name' => $name,
|
||||
'datas' => $datas,
|
||||
];
|
||||
|
||||
$result = $this->get_model()->insert($contact);
|
||||
if (!$result)
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
||||
$internal_event = new Event($this->bdd);
|
||||
$internal_event->create($id_user, 'CONTACT_ADD', 'Ajout contact : ' . $name . ' (' . \controllers\internals\Tool::phone_format($number) . ')');
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a contact.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id : Contact id
|
||||
* @param string $number : Contact number
|
||||
* @param string $name : Contact name
|
||||
* @param ?string $datas : Contact datas
|
||||
*
|
||||
* @return int : number of modified rows
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id, string $number, string $name, string $datas)
|
||||
{
|
||||
$contact = [
|
||||
'number' => $number,
|
||||
'name' => $name,
|
||||
'datas' => $datas,
|
||||
];
|
||||
|
||||
return $this->get_model()->update_for_user($id_user, $id, $contact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a list of contacts as csv.
|
||||
*
|
||||
* @param resource $file_handler : File handler to import contacts from
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return mixed : False on error, number of inserted contacts else
|
||||
*/
|
||||
public function import_csv(int $id_user, $file_handler)
|
||||
{
|
||||
if (!\is_resource($file_handler))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$nb_insert = 0;
|
||||
|
||||
$head = null;
|
||||
while ($line = fgetcsv($file_handler))
|
||||
{
|
||||
if (null === $head)
|
||||
{
|
||||
$head = $line;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$line = array_combine($head, $line);
|
||||
if (false === $line)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($line['name'], $line['number']))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$datas = [];
|
||||
foreach ($line as $key => $value)
|
||||
{
|
||||
if ('name' === $key || 'number' === $key)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('' === $value)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = mb_ereg_replace('[\W]', '', $key);
|
||||
$datas[$key] = $value;
|
||||
}
|
||||
$datas = json_encode($datas);
|
||||
|
||||
$success = $this->create($id_user, $line['number'], $line['name'], $datas);
|
||||
if ($success)
|
||||
{
|
||||
++$nb_insert;
|
||||
}
|
||||
}
|
||||
|
||||
return $nb_insert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a list of contacts as json.
|
||||
*
|
||||
* @param resource $file_handler : File handler to import contacts from
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return mixed : False on error, number of inserted contacts else
|
||||
*/
|
||||
public function import_json(int $id_user, $file_handler)
|
||||
{
|
||||
if (!\is_resource($file_handler))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_content = '';
|
||||
while ($line = fgets($file_handler))
|
||||
{
|
||||
$file_content .= $line;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$contacts = json_decode($file_content, true);
|
||||
|
||||
if (!\is_array($contacts))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$nb_insert = 0;
|
||||
foreach ($contacts as $contact)
|
||||
{
|
||||
if (!\is_array($contact))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($contact['name'], $contact['number']))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$datas = $contact['datas'] ?? [];
|
||||
$datas = json_encode($datas);
|
||||
|
||||
$success = $this->create($id_user, $contact['number'], $contact['name'], $datas);
|
||||
if ($success)
|
||||
{
|
||||
++$nb_insert;
|
||||
}
|
||||
}
|
||||
|
||||
return $nb_insert;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the contacts of a user as csv.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return array : ['headers' => array of headers to return, 'content' => the generated file]
|
||||
*/
|
||||
public function export_csv(int $id_user): array
|
||||
{
|
||||
$contacts = $this->get_model()->gets_for_user($id_user);
|
||||
|
||||
$columns = [
|
||||
'name',
|
||||
'number',
|
||||
];
|
||||
|
||||
foreach ($contacts as $contact)
|
||||
{
|
||||
$datas = json_decode($contact['datas'], true);
|
||||
foreach ($datas as $key => $value)
|
||||
{
|
||||
$columns[] = $key;
|
||||
}
|
||||
}
|
||||
$columns = array_unique($columns);
|
||||
|
||||
$lines = [];
|
||||
foreach ($contacts as $contact)
|
||||
{
|
||||
$datas = json_decode($contact['datas'], true);
|
||||
|
||||
$line = [];
|
||||
foreach ($columns as $column)
|
||||
{
|
||||
if (isset($contact[$column]))
|
||||
{
|
||||
$line[] = $contact[$column];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($datas[$column]))
|
||||
{
|
||||
$line[] = $datas[$column];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$line[] = null;
|
||||
}
|
||||
$lines[] = $line;
|
||||
}
|
||||
|
||||
//Php only support csv formatting to file. To get it in string we need to create a tmp in memory file, write in it, and then read the file into a var
|
||||
// output up to 5MB is kept in memory, if it becomes bigger it will automatically be written to a temporary file
|
||||
$csv_tmp_file = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+');
|
||||
fputcsv($csv_tmp_file, $columns);
|
||||
foreach ($lines as $line)
|
||||
{
|
||||
fputcsv($csv_tmp_file, $line);
|
||||
}
|
||||
rewind($csv_tmp_file);
|
||||
|
||||
$csv_string = stream_get_contents($csv_tmp_file);
|
||||
|
||||
return [
|
||||
'headers' => [
|
||||
'Content-Disposition: attachment; filename=contacts.csv',
|
||||
'Content-Type: text/csv',
|
||||
'Content-Length: ' . mb_strlen($csv_string),
|
||||
],
|
||||
'content' => $csv_string,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the contacts of a user as json.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return array : ['headers' => array of headers to return, 'content' => the generated file]
|
||||
*/
|
||||
public function export_json(int $id_user): array
|
||||
{
|
||||
$contacts = $this->get_model()->gets_for_user($id_user);
|
||||
|
||||
foreach ($contacts as &$contact)
|
||||
{
|
||||
unset($contact['id'], $contact['id_user']);
|
||||
|
||||
$contact['datas'] = json_decode($contact['datas']);
|
||||
}
|
||||
$content = json_encode($contacts);
|
||||
|
||||
return [
|
||||
'headers' => [
|
||||
'Content-Disposition: attachment; filename=contacts.json',
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . mb_strlen($content),
|
||||
],
|
||||
'content' => $content,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Contact($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
<?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 Event extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Disabled methods.
|
||||
*/
|
||||
public function update_for_user()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets lasts x events for a user order by date.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $nb_entry : Number of events to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
||||
{
|
||||
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new event.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param mixed $type
|
||||
* @param mixed $text
|
||||
*
|
||||
* @return mixed bool : false on fail, new event id else
|
||||
*/
|
||||
public function create($id_user, $type, $text)
|
||||
{
|
||||
$event = [
|
||||
'id_user' => $id_user,
|
||||
'type' => $type,
|
||||
'text' => $text,
|
||||
];
|
||||
|
||||
return $this->get_model()->insert($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Event($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
|
||||
class ExpressionProvider implements ExpressionFunctionProviderInterface
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
ExpressionFunction::fromPhp('is_null', 'exists'),
|
||||
ExpressionFunction::fromPhp('mb_strtolower', 'lower'),
|
||||
ExpressionFunction::fromPhp('mb_strtoupper', 'upper'),
|
||||
ExpressionFunction::fromPhp('mb_substr', 'substr'),
|
||||
ExpressionFunction::fromPhp('abs', 'abs'),
|
||||
ExpressionFunction::fromPhp('strtotime', 'date'),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Classe des groups.
|
||||
*/
|
||||
class Group extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new group for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param stirng $name : Group name
|
||||
* @param array $contacts_ids : Ids of the contacts of the group
|
||||
*
|
||||
* @return mixed bool|int : false on error, new group id
|
||||
*/
|
||||
public function create(int $id_user, string $name, array $contacts_ids)
|
||||
{
|
||||
$group = [
|
||||
'id_user' => $id_user,
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
$id_group = $this->get_model()->insert($group);
|
||||
if (!$id_group)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$internal_contact = new Contact($this->bdd);
|
||||
foreach ($contacts_ids as $contact_id)
|
||||
{
|
||||
$contact = $internal_contact->get_for_user($id_user, $contact_id);
|
||||
if (!$contact)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_group_contact_relation($id_group, $contact_id);
|
||||
}
|
||||
|
||||
$internal_event = new Event($this->bdd);
|
||||
$internal_event->create($id_user, 'GROUP_ADD', 'Ajout group : ' . $name);
|
||||
|
||||
return $id_group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a group for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_group : Group id
|
||||
* @param stirng $name : Group name
|
||||
* @param array $contacts_ids : Ids of the contacts of the group
|
||||
*
|
||||
* @return bool : False on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_group, string $name, array $contacts_ids)
|
||||
{
|
||||
$group = [
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
$result = $this->get_model()->update_for_user($id_user, $id_group, $group);
|
||||
|
||||
$this->get_model()->delete_group_contact_relations($id_group);
|
||||
|
||||
$internal_contact = new Contact($this->bdd);
|
||||
$nb_contact_insert = 0;
|
||||
foreach ($contacts_ids as $contact_id)
|
||||
{
|
||||
$contact = $internal_contact->get_for_user($id_user, $contact_id);
|
||||
if (!$contact)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->get_model()->insert_group_contact_relation($id_group, $contact_id))
|
||||
{
|
||||
++$nb_contact_insert;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$result && $nb_contact_insert !== \count($contacts_ids))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a group by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Group name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_for_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->get_model()->get_by_name_for_user($id_user, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups contacts.
|
||||
*
|
||||
* @param int $id_group : Group id
|
||||
*
|
||||
* @return array : Contacts of the group
|
||||
*/
|
||||
public function get_contacts($id_group)
|
||||
{
|
||||
return $this->get_model()->get_contacts($id_group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Group($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* 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 (false === $result_upload_media['success'])
|
||||
{
|
||||
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_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
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
<?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 Phone extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Return all phones of a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
return $this->get_model()->gets_for_user($id_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a phone by his number.
|
||||
*
|
||||
* @param string $number : Phone number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number(string $number)
|
||||
{
|
||||
return $this->get_model()->get_by_number($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a phone for a user by a number.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $number : Phone number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number_and_user(int $id_user, string $number)
|
||||
{
|
||||
return $this->get_model()->get_by_number_and_user($id_user, $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a phone.
|
||||
*
|
||||
* @param int $id_user : User to insert phone for
|
||||
* @param string $number : The number of the phone
|
||||
* @param string $adapter : The adapter to use the phone
|
||||
* @param string json $adapter_datas : A JSON string representing adapter's datas (for example credentials for an api)
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function create(int $id_user, string $number, string $adapter, string $adapter_datas): bool
|
||||
{
|
||||
$phone = [
|
||||
'id_user' => $id_user,
|
||||
'number' => $number,
|
||||
'adapter' => $adapter,
|
||||
'adapter_datas' => $adapter_datas,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->insert($phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a phone.
|
||||
*
|
||||
* @param int $id_user : User to insert phone for
|
||||
* @param int $id : Phone id
|
||||
* @param string $number : The number of the phone
|
||||
* @param string $adapter : The adapter to use the phone
|
||||
* @param array $adapter_datas : An array of the datas of the adapter (for example credentials for an api)
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id, string $number, string $adapter, array $adapter_datas): bool
|
||||
{
|
||||
$phone = [
|
||||
'id_user' => $id_user,
|
||||
'number' => $number,
|
||||
'adapter' => $adapter,
|
||||
'adapter_datas' => json_encode($adapter_datas),
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id, $phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Phone($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,242 +0,0 @@
|
|||
<?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 Received extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Return the list of unread messages for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param ?int $nb_entry : Number of entry to return
|
||||
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
|
||||
*
|
||||
* @return array : Entrys list
|
||||
*/
|
||||
public function list_unread_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null)
|
||||
{
|
||||
return $this->get_model()->list_unread_for_user($id_user, $nb_entry, $nb_entry * $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a received.
|
||||
*
|
||||
* @param $at : Reception date
|
||||
* @param $text : Text of the message
|
||||
* @param string $origin : Number of the sender
|
||||
* @param string $destination : Number of the receiver
|
||||
* @param string $status : Status of the received message
|
||||
* @param bool $command : Is the sms a command
|
||||
*
|
||||
* @return bool : false on error, new received id else
|
||||
*/
|
||||
public function create($at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
|
||||
{
|
||||
$received = [
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'origin' => $origin,
|
||||
'destination' => $destination,
|
||||
'status' => $status,
|
||||
'command' => $command,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->insert($received);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a received for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_received : received id
|
||||
* @param $at : Reception date
|
||||
* @param $text : Text of the message
|
||||
* @param string $origin : Number of the sender
|
||||
* @param string $destination : Number of the receiver
|
||||
* @param string $status : Status of the received message
|
||||
* @param bool $command : Is the sms a command
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_received, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
|
||||
{
|
||||
$received = [
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'origin' => $origin,
|
||||
'destination' => $destination,
|
||||
'status' => $status,
|
||||
'command' => $command,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a received message for a user to mark the message as read.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_received : received id
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function mark_as_read_for_user(int $id_user, int $id_received): bool
|
||||
{
|
||||
$received = [
|
||||
'status' => 'read',
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a received message for a user to mark the message as unread.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_received : received id
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function mark_as_unread_for_user(int $id_user, int $id_received): bool
|
||||
{
|
||||
$received = [
|
||||
'status' => 'unread',
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of unread messages for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function count_unread_for_user(int $id_user)
|
||||
{
|
||||
return $this->get_model()->count_unread_for_user($id_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return x last receiveds message for a user, order by date.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $nb_entry : Number of receiveds messages to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
||||
{
|
||||
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return receiveds for an origin and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $origin : Number who sent the message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_by_origin_and_user(int $id_user, string $origin)
|
||||
{
|
||||
return $this->get_model()->gets_by_origin_and_user($id_user, $origin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of sended SMS for every date since a date for a specific user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param \DateTime $date : Date since which we want the messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function count_by_day_since_for_user(int $id_user, $date)
|
||||
{
|
||||
$counts_by_day = $this->get_model()->count_by_day_since_for_user($id_user, $date);
|
||||
$return = [];
|
||||
|
||||
foreach ($counts_by_day as $count_by_day)
|
||||
{
|
||||
$return[$count_by_day['at_ymd']] = $count_by_day['nb'];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all discussions (ie : numbers we have a message received from or sended to) for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_discussions_for_user(int $id_user)
|
||||
{
|
||||
return $this->get_model()->get_discussions_for_user($id_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SMS received since a date for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
||||
*
|
||||
* @return array : Tableau avec tous les SMS depuis la date
|
||||
*/
|
||||
public function get_since_by_date_for_user(int $id_user, $date)
|
||||
{
|
||||
return $this->get_model()->get_since_by_date_for_user($id_user, $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find messages received since a date for a certain origin and user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param $date : Date we want messages sinces
|
||||
* @param string $origin : Origin number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_since_by_date_for_origin_and_user(int $id_user, $date, string $origin)
|
||||
{
|
||||
return $this->get_model()->get_since_by_date_for_origin_and_user($id_user, $date, $origin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find destination of last received message for an origin and user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $origin : Origin number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_last_for_origin_and_user(int $id_user, string $origin)
|
||||
{
|
||||
return $this->get_model()->get_last_for_origin_and_user($id_user, $origin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Received($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
||||
|
||||
/**
|
||||
* Class to analyse rules used by conditional groups.
|
||||
*/
|
||||
class Ruler extends \descartes\InternalController
|
||||
{
|
||||
private $expression_language;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->expression_language = new ExpressionLanguage();
|
||||
|
||||
//Add custom functions
|
||||
$this->expression_language->registerProvider(new ExpressionProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if a condition is valid. i.e we can evaluate it without error.
|
||||
*
|
||||
* @param string $condition : The condition to evaluate
|
||||
* @param array $datas : The datas to made available to condition
|
||||
*
|
||||
* @return bool : false if invalid, true else
|
||||
*/
|
||||
public function validate_condition(string $condition, array $datas = []): bool
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->expression_language->evaluate($condition, $datas);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a condition.
|
||||
*
|
||||
* @param string $condition : The condition to evaluate
|
||||
* @param array $datas : The datas to made available to condition
|
||||
*
|
||||
* @return ?bool : false if invalid, true else, null only on error
|
||||
*/
|
||||
public function evaluate_condition(string $condition, array $datas = []): ?bool
|
||||
{
|
||||
try
|
||||
{
|
||||
$result = $this->expression_language->evaluate($condition, $datas);
|
||||
|
||||
return (bool) $result;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,427 +0,0 @@
|
|||
<?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 Scheduled extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a scheduled.
|
||||
*
|
||||
* @param int $id_user : User to insert scheduled for
|
||||
* @param $at : Scheduled date to send
|
||||
* @param string $text : Text of the message
|
||||
* @param ?string $origin : Origin number of the message, null by default
|
||||
* @param bool $flash : Is the sms a flash sms, 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
|
||||
*
|
||||
* @return bool : false on error, new id on success
|
||||
*/
|
||||
public function create(int $id_user, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
|
||||
{
|
||||
$scheduled = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'origin' => $origin,
|
||||
'flash' => $flash,
|
||||
];
|
||||
|
||||
if ($origin)
|
||||
{
|
||||
$internal_phone = new Phone($this->bdd);
|
||||
$find_phone = $internal_phone->get_by_number_and_user($id_user, $origin);
|
||||
|
||||
if (!$find_phone)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$id_scheduled = $this->get_model()->insert($scheduled);
|
||||
if (!$id_scheduled)
|
||||
{
|
||||
$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 . '.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($numbers as $number)
|
||||
{
|
||||
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
|
||||
}
|
||||
|
||||
$internal_contact = new Contact($this->bdd);
|
||||
foreach ($contacts_ids as $contact_id)
|
||||
{
|
||||
$find_contact = $internal_contact->get_for_user($id_user, $contact_id);
|
||||
if (!$find_contact)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_scheduled_contact_relation($id_scheduled, $contact_id);
|
||||
}
|
||||
|
||||
$internal_group = new Group($this->bdd);
|
||||
foreach ($groups_ids as $group_id)
|
||||
{
|
||||
$find_group = $internal_group->get_for_user($id_user, $group_id);
|
||||
if (!$find_group)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
|
||||
}
|
||||
|
||||
$internal_conditional_group = new ConditionalGroup($this->bdd);
|
||||
foreach ($conditional_group_ids as $conditional_group_id)
|
||||
{
|
||||
$find_group = $internal_conditional_group->get_for_user($id_user, $conditional_group_id);
|
||||
if (!$find_group)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
|
||||
}
|
||||
|
||||
return $id_scheduled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a scheduled.
|
||||
*
|
||||
* @param int $id_user : User to insert scheduled for
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
* @param $at : Scheduled date to send
|
||||
* @param string $text : Text of the message
|
||||
* @param ?string $origin : Origin number of the message, null by default
|
||||
* @param bool $flash : Is the sms a flash sms, 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
|
||||
*
|
||||
* @return bool : false on error, new id on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_scheduled, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
|
||||
{
|
||||
$scheduled = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'origin' => $origin,
|
||||
'flash' => $flash,
|
||||
];
|
||||
|
||||
if ($origin)
|
||||
{
|
||||
$internal_phone = new Phone($this->bdd);
|
||||
$find_phone = $internal_phone->get_by_number_and_user($id_user, $origin);
|
||||
|
||||
if (!$find_phone)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$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);
|
||||
|
||||
foreach ($numbers as $number)
|
||||
{
|
||||
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
|
||||
}
|
||||
|
||||
$internal_contact = new Contact($this->bdd);
|
||||
foreach ($contacts_ids as $contact_id)
|
||||
{
|
||||
$find_contact = $internal_contact->get_for_user($id_user, $contact_id);
|
||||
if (!$find_contact)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_scheduled_contact_relation($id_scheduled, $contact_id);
|
||||
}
|
||||
|
||||
$internal_group = new Group($this->bdd);
|
||||
foreach ($groups_ids as $group_id)
|
||||
{
|
||||
$find_group = $internal_group->get_for_user($id_user, $group_id);
|
||||
if (!$find_group)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
|
||||
}
|
||||
|
||||
$internal_conditional_group = new ConditionalGroup($this->bdd);
|
||||
foreach ($conditional_group_ids as $conditional_group_id)
|
||||
{
|
||||
$find_group = $internal_conditional_group->get_for_user($id_user, $conditional_group_id);
|
||||
if (!$find_group)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages scheduled before a date for a number and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param $date : Date before which we want messages
|
||||
* @param string $number : Number for which we want messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_before_date_for_number_and_user(int $id_user, $date, string $number)
|
||||
{
|
||||
return $this->get_model()->gets_before_date_for_number_and_user($id_user, $date, $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all messages to send and the number to use to send theme.
|
||||
*
|
||||
* @return array : [['id_scheduled', 'text', 'origin', 'destination', 'flash'], ...]
|
||||
*/
|
||||
public function get_smss_to_send()
|
||||
{
|
||||
$smss_to_send = [];
|
||||
|
||||
$internal_templating = new \controllers\internals\Templating();
|
||||
$internal_setting = new \controllers\internals\Setting($this->bdd);
|
||||
$internal_group = new \controllers\internals\Group($this->bdd);
|
||||
$internal_conditional_group = new \controllers\internals\ConditionalGroup($this->bdd);
|
||||
$internal_phone = new \controllers\internals\Phone($this->bdd);
|
||||
|
||||
$users_settings = [];
|
||||
$users_phones = [];
|
||||
|
||||
$now = new \DateTime();
|
||||
$now = $now->format('Y-m-d H:i:s');
|
||||
$scheduleds = $this->get_model()->gets_before_date($now);
|
||||
foreach ($scheduleds as $scheduled)
|
||||
{
|
||||
if (!isset($users_settings[$scheduled['id_user']]))
|
||||
{
|
||||
$users_settings[$scheduled['id_user']] = [];
|
||||
|
||||
$settings = $internal_setting->gets_for_user($scheduled['id_user']);
|
||||
foreach ($settings as $name => $value)
|
||||
{
|
||||
$users_settings[$scheduled['id_user']][$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($users_phones[$scheduled['id_user']]))
|
||||
{
|
||||
$phones = $internal_phone->gets_for_user($scheduled['id_user']);
|
||||
$users_phones[$scheduled['id_user']] = $phones ? $phones : [];
|
||||
}
|
||||
|
||||
$messages = [];
|
||||
|
||||
//Add messages for numbers
|
||||
$numbers = $this->get_numbers($scheduled['id']);
|
||||
foreach ($numbers as $number)
|
||||
{
|
||||
$message = [
|
||||
'id_user' => $scheduled['id_user'],
|
||||
'id_scheduled' => $scheduled['id'],
|
||||
'origin' => $scheduled['origin'],
|
||||
'destination' => $number['number'],
|
||||
'flash' => $scheduled['flash'],
|
||||
];
|
||||
|
||||
if (null === $message['origin'])
|
||||
{
|
||||
$k = array_rand($users_phones[$scheduled['id_user']]);
|
||||
$rnd_phone = $users_phones[$scheduled['id_user']][$k];
|
||||
$message['origin'] = $rnd_phone['number'];
|
||||
}
|
||||
|
||||
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
|
||||
{
|
||||
$render = $internal_templating->render($scheduled['text']);
|
||||
|
||||
if (!$render['success'])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$message['text'] = $render['result'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$message['text'] = $scheduled['text'];
|
||||
}
|
||||
|
||||
$messages[] = $message;
|
||||
}
|
||||
|
||||
//Add messages for contacts
|
||||
$contacts = $this->get_contacts($scheduled['id']);
|
||||
|
||||
$groups = $this->get_groups($scheduled['id']);
|
||||
foreach ($groups as $group)
|
||||
{
|
||||
$contacts_to_add = $internal_group->get_contacts($group['id']);
|
||||
$contacts = array_merge($contacts, $contacts_to_add);
|
||||
}
|
||||
|
||||
$conditional_groups = $this->get_conditional_groups($scheduled['id']);
|
||||
foreach ($conditional_groups as $conditional_group)
|
||||
{
|
||||
$contacts_to_add = $internal_conditional_group->get_contacts_for_condition_and_user($scheduled['id_user'], $conditional_group['condition']);
|
||||
$contacts = array_merge($contacts, $contacts_to_add);
|
||||
}
|
||||
|
||||
$added_contacts = [];
|
||||
foreach ($contacts as $contact)
|
||||
{
|
||||
if ($added_contacts[$contact['id']] ?? false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$added_contacts[$contact['id']] = true;
|
||||
|
||||
$message = [
|
||||
'id_user' => $scheduled['id_user'],
|
||||
'id_scheduled' => $scheduled['id'],
|
||||
'origin' => $scheduled['origin'],
|
||||
'destination' => $number['number'],
|
||||
'flash' => $scheduled['flash'],
|
||||
];
|
||||
|
||||
if (null === $message['origin'])
|
||||
{
|
||||
$k = array_rand($users_phones[$scheduled['id_user']]);
|
||||
$rnd_phone = $users_phones[$scheduled['id_user']][$k];
|
||||
$message['origin'] = $rnd_phone['number'];
|
||||
}
|
||||
|
||||
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
|
||||
{
|
||||
$contact['datas'] = json_decode($contact['datas'], true);
|
||||
$datas = ['contact' => $contact];
|
||||
$render = $internal_templating->render($scheduled['text'], $datas);
|
||||
|
||||
if (!$render['success'])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$message['text'] = $render['result'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$message['text'] = $scheduled['text'];
|
||||
}
|
||||
|
||||
$messages[] = $message;
|
||||
}
|
||||
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
//Remove empty messages
|
||||
if ('' === trim($message['text']))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$smss_to_send[] = $message;
|
||||
}
|
||||
}
|
||||
|
||||
return $smss_to_send;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return numbers for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_numbers(int $id_scheduled)
|
||||
{
|
||||
return $this->get_model()->get_numbers($id_scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return contacts for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_contacts(int $id_scheduled)
|
||||
{
|
||||
return $this->get_model()->get_contacts($id_scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return groups for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_groups(int $id_scheduled)
|
||||
{
|
||||
return $this->get_model()->get_groups($id_scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return conditional groups for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_conditional_groups(int $id_scheduled)
|
||||
{
|
||||
return $this->get_model()->get_conditional_groups($id_scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Scheduled($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,199 +0,0 @@
|
|||
<?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 Sended extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a sended.
|
||||
*
|
||||
* @param $at : Reception date
|
||||
* @param $text : Text of the message
|
||||
* @param string $origin : Number of the sender
|
||||
* @param string $destination : Number of the receiver
|
||||
* @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 string $status : Status of a the sms. By default 'unknown'
|
||||
*
|
||||
* @return bool : false on error, new sended id else
|
||||
*/
|
||||
public function create($at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown'): bool
|
||||
{
|
||||
$sended = [
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'origin' => $origin,
|
||||
'destination' => $destination,
|
||||
'uid' => $uid,
|
||||
'adapter' => $adapter,
|
||||
'flash' => $flash,
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->insert($sended);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a sended for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_sended : Sended id
|
||||
* @param $at : Reception date
|
||||
* @param $text : Text of the message
|
||||
* @param string $origin : Number of the sender
|
||||
* @param string $destination : Number of the receiver
|
||||
* @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 ?string $status : Status of a the sms. By default null -> unknown
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_sended, $at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = null): bool
|
||||
{
|
||||
$sended = [
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'origin' => $origin,
|
||||
'destination' => $destination,
|
||||
'uid' => $uid,
|
||||
'adapter' => $adapter,
|
||||
'flash' => $flash,
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a sended status for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_sended : Sended id
|
||||
* @param string $status : Status of a the sms (unknown, delivered, failed)
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_status_for_user(int $id_user, int $id_sended, string $status): bool
|
||||
{
|
||||
$sended = [
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a sended status for a sended.
|
||||
*
|
||||
* @param int $id_sended : Sended id
|
||||
* @param string $status : Status of a the sms (unknown, delivered, failed)
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_status(int $id_sended, string $status): bool
|
||||
{
|
||||
$sended = [
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update($id_sended, $sended);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return x last sendeds message for a user, order by date.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $nb_entry : Number of sendeds messages to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
||||
{
|
||||
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sendeds for a destination and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $origin : Number who sent the message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_by_destination_and_user(int $id_user, string $origin)
|
||||
{
|
||||
return $this->get_model()->gets_by_destination_and_user($id_user, $origin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sended for an uid and an adapter.
|
||||
*
|
||||
* @param string $uid : Uid of the sended
|
||||
* @param string $adapter : Adapter used to send the message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_uid_and_adapter(string $uid, string $adapter)
|
||||
{
|
||||
return $this->get_model()->get_by_uid_and_adapter($uid, $adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of sended SMS for every date since a date for a specific user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param \DateTime $date : Date since which we want the messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function count_by_day_since_for_user(int $id_user, $date)
|
||||
{
|
||||
$counts_by_day = $this->get_model()->count_by_day_since_for_user($id_user, $date);
|
||||
$return = [];
|
||||
|
||||
foreach ($counts_by_day as $count_by_day)
|
||||
{
|
||||
$return[$count_by_day['at_ymd']] = $count_by_day['nb'];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find last sended message for a destination and user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $destination : Destination number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_last_for_destination_and_user(int $id_user, string $destination)
|
||||
{
|
||||
return $this->get_model()->get_last_for_destination_and_user($id_user, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Sended($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
<?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 Setting extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Return all settings of a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
$settings = $this->get_model()->gets_for_user($id_user);
|
||||
$settings_array = [];
|
||||
|
||||
foreach ($settings as $setting)
|
||||
{
|
||||
$settings_array[$setting['name']] = $setting['value'];
|
||||
}
|
||||
|
||||
return $settings_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a setting by his name and user id.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $name : setting name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return int : number of modified lines
|
||||
*/
|
||||
public function update_for_user(int $id_user, string $name, $value): bool
|
||||
{
|
||||
return (bool) $this->get_model()->update_by_name_for_user($id_user, $name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new setting.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $name : setting name
|
||||
* @param mixed $value : value of the setting
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function create(int $id_user, string $name, $value): bool
|
||||
{
|
||||
$setting = [
|
||||
'id_user' => $id_user,
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->insert($setting);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and insert default settings for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function create_defaults_for_user(int $id_user)
|
||||
{
|
||||
$all_success = true;
|
||||
foreach (USER_DEFAULT_SETTINGS as $name => $value)
|
||||
{
|
||||
$success = $this->create($id_user, $name, $value);
|
||||
$all_success = ($all_success && $success);
|
||||
}
|
||||
|
||||
return $all_success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Setting($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
<?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 SmsStop extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new smsstop.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $number : Number to stop smss for
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create smsstop, id of the new smsstop else
|
||||
*/
|
||||
public function create(int $id_user, string $number)
|
||||
{
|
||||
$smsstop = [
|
||||
'id_user' => $id_user,
|
||||
'number' => $number,
|
||||
];
|
||||
|
||||
return $this->get_model()->insert($smsstop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a smsstop.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_smsstop : SmsStop id
|
||||
* @param string $number : Number to stop smss for
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create smsstop, id of the new smsstop else
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_smsstop, string $number)
|
||||
{
|
||||
$datas = [
|
||||
'number' => $number,
|
||||
];
|
||||
|
||||
return $this->get_model()->update_for_user($id_user, $id_smsstop, $datas);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a smsstop by his number and user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $number : phone number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number_for_user(int $id_user, string $number)
|
||||
{
|
||||
return $this->get_model()->get_by_number_for_user($id_user, $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\SmsStop($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
<?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;
|
||||
|
||||
abstract class StandardController extends \descartes\InternalController
|
||||
{
|
||||
protected $bdd;
|
||||
|
||||
public function __construct(\PDO $bdd)
|
||||
{
|
||||
$this->bdd = $bdd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the entries.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_all()
|
||||
{
|
||||
return $this->get_model()->get_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a entry by his id.
|
||||
*
|
||||
* @param int $id : Entry id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get(int $id)
|
||||
{
|
||||
return $this->get_model()->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a entry by his id and a user.
|
||||
*
|
||||
* @param int $id_user : Entry id
|
||||
* @param int $id : Entry id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_for_user(int $id_user, int $id)
|
||||
{
|
||||
return $this->get_model()->get_for_user($id_user, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all entries for a user.
|
||||
*
|
||||
* @param int $id_user : Entry id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
return $this->get_model()->gets_for_user($id_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of entries for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param ?int $nb_entry : Number of entry to return
|
||||
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
|
||||
*
|
||||
* @return array : Entrys list
|
||||
*/
|
||||
public function list_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null)
|
||||
{
|
||||
return $this->get_model()->list_for_user($id_user, $nb_entry, $nb_entry * $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of entries in a group of ids and for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param array $ids : ids of entries to find
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_in_for_user(int $id_user, array $ids)
|
||||
{
|
||||
return $this->get_model()->gets_in_for_user($id_user, $ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a entry by his id 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)
|
||||
{
|
||||
return $this->get_model()->delete_for_user($id_user, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a entry by his id.
|
||||
*
|
||||
* @param int $id : Entry id
|
||||
*
|
||||
* @return int : Number of removed rows
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
return $this->get_model()->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of entry for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return int : number of entries
|
||||
*/
|
||||
public function count_for_user(int $id_user)
|
||||
{
|
||||
return $this->get_model()->count_for_user($id_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
abstract protected function get_model(): \descartes\Model;
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Templating questions relative class
|
||||
* Not a standard controller as it's not linked to a model in any way.
|
||||
*/
|
||||
class Templating
|
||||
{
|
||||
/**
|
||||
* Twig environment.
|
||||
*/
|
||||
private $sandbox;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$tags = [
|
||||
'if',
|
||||
'for',
|
||||
'apply',
|
||||
];
|
||||
|
||||
$filters = [
|
||||
'abs', 'capitalize', 'country_name', 'currency_name',
|
||||
'currency_symbol', 'date', 'date_modify', 'default',
|
||||
'first', 'format', 'format_currency', 'format_datetime',
|
||||
'format_number', 'join', 'keys', 'language_name',
|
||||
'last', 'length', 'locale_name', 'lower', 'number_format',
|
||||
'replace', 'reverse', 'round', 'slice',
|
||||
'sort', 'spaceless', 'split', 'timezone_name',
|
||||
'title', 'trim', 'upper', 'url_encode',
|
||||
];
|
||||
|
||||
$methods = [];
|
||||
$properties = [];
|
||||
$functions = [
|
||||
'date', 'max', 'min', 'random',
|
||||
'range',
|
||||
];
|
||||
|
||||
$policy = new \Twig\Sandbox\SecurityPolicy($tags, $filters, $methods, $properties, $functions);
|
||||
$this->sandbox = new \Twig\Extension\SandboxExtension($policy, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a string as a twig template.
|
||||
*
|
||||
* @param string $template : Template string
|
||||
* @param array $datas : Datas to pass to the template
|
||||
*
|
||||
* @return array : keys, success, error, result
|
||||
*/
|
||||
public function render(string $template, array $datas = [])
|
||||
{
|
||||
try
|
||||
{
|
||||
$loader = new \Twig\Loader\ArrayLoader([
|
||||
'template' => $template,
|
||||
]);
|
||||
|
||||
$twig = new \Twig\Environment($loader);
|
||||
$result = $twig->render('template', $datas);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'result' => $result,
|
||||
];
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return [
|
||||
'success' => false,
|
||||
'result' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,365 +0,0 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Some tools frequently used.
|
||||
* Not a standard controller as it's not linked to a model in any way.
|
||||
*/
|
||||
class Tool extends \descartes\InternalController
|
||||
{
|
||||
/**
|
||||
* Cette fonction parse un numéro pour le retourner sans espaces, etc.
|
||||
*
|
||||
* @param string $number : Le numéro de téléphone à parser
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
try
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
return $phone_number_util->format($phone_number_o, \libphonenumber\PhoneNumberFormat::E164);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction parse un numéro pour le retourner avec des espaces, etc.
|
||||
*
|
||||
* @param string $number : Le numéro de téléphone à parser
|
||||
*
|
||||
* @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 phone_format($number)
|
||||
{
|
||||
try
|
||||
{
|
||||
$phone_number_util = \libphonenumber\PhoneNumberUtil::getInstance();
|
||||
$phone_number_o = $phone_number_util->parse($number, null);
|
||||
|
||||
return $phone_number_util->format($phone_number_o, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return $number;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a number and make a link to a discussion with this number.
|
||||
*
|
||||
* @param string $number : Number to format and make a link for
|
||||
*
|
||||
* @return string : Link to the number
|
||||
*/
|
||||
public static function phone_link($number)
|
||||
{
|
||||
$number_format = self::phone_format($number);
|
||||
$url = \descartes\Router::url('Discussion', 'show', ['number' => $number]);
|
||||
|
||||
return '<a href="' . self::s($url, false, true, false) . '">' . self::s($number_format, false, true, false) . '</a>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction fait la correspondance entre un type d'evenement et une icone font awesome.
|
||||
*
|
||||
* @param string $type : Le type de l'évenement à analyser
|
||||
*
|
||||
* @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';
|
||||
|
||||
break;
|
||||
case 'CONTACT_ADD':
|
||||
$logo = 'fa-user';
|
||||
|
||||
break;
|
||||
case 'GROUP_ADD':
|
||||
$logo = 'fa-group';
|
||||
|
||||
break;
|
||||
case 'SCHEDULED_ADD':
|
||||
$logo = 'fa-calendar';
|
||||
|
||||
break;
|
||||
case 'COMMAND_ADD':
|
||||
$logo = 'fa-terminal';
|
||||
|
||||
break;
|
||||
default:
|
||||
$logo = 'fa-question';
|
||||
}
|
||||
|
||||
return $logo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction vérifie une date.
|
||||
*
|
||||
* @param string $date : La date a valider
|
||||
* @param string $format : Le format de la date
|
||||
*
|
||||
* @return bool : Vrai si la date et valide, faux sinon
|
||||
*/
|
||||
public static function validate_date($date, $format)
|
||||
{
|
||||
$objectDate = \DateTime::createFromFormat($format, $date);
|
||||
|
||||
return $objectDate && $objectDate->format($format) === $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction retourne un mot de passe généré aléatoirement.
|
||||
*
|
||||
* @param int $length : Taille du mot de passe à générer
|
||||
*
|
||||
* @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;
|
||||
while ($i < $length)
|
||||
{
|
||||
++$i;
|
||||
$password .= $chars[rand(0, $chars_length)];
|
||||
}
|
||||
|
||||
return $password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction vérifie si un utilisateur et connecté, et si il ne l'est pas, redirige sur la page de connexion.
|
||||
*/
|
||||
public static function verifyconnect()
|
||||
{
|
||||
if (!isset($_SESSION['connect']) || !$_SESSION['connect'])
|
||||
{
|
||||
header('Location: /');
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user connected.
|
||||
*
|
||||
* @return bool : True if connected, False else
|
||||
*/
|
||||
public static function is_connected()
|
||||
{
|
||||
return (bool) ($_SESSION['connect'] ?? false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user is admin.
|
||||
*
|
||||
* @return bool : True if admin, False else
|
||||
*/
|
||||
public static function is_admin()
|
||||
{
|
||||
return (bool) ($_SESSION['user']['admin'] ?? false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction s'occupe d'envoyer les emails.
|
||||
*
|
||||
* @param string $to : L'adresse mail à laquelle envoyer le mail
|
||||
* @param array $settings : Les settings du mail, type, sujet, template
|
||||
* @param array $datas : Les données à fournir au template du mail
|
||||
*/
|
||||
public static function send_email($to, $settings, $datas = [])
|
||||
{
|
||||
$controller = new \descartes\Controller();
|
||||
|
||||
ob_start();
|
||||
$controller->render($settings['template'], $datas);
|
||||
$content = ob_get_clean();
|
||||
|
||||
return @mail($to, $settings['subject'], $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow to read an uploaded file.
|
||||
*
|
||||
* @param array $file : The array extracted from $_FILES['file']
|
||||
*
|
||||
* @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,
|
||||
];
|
||||
|
||||
if (UPLOAD_ERR_OK !== $file['error'])
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
$result['mime_type'] = 'text/plain' === mime_content_type($tmp_filename) ? $file['type'] : mime_content_type($tmp_filename);
|
||||
|
||||
$file_handler = fopen($tmp_filename, 'r');
|
||||
$result['success'] = true;
|
||||
$result['content'] = $file_handler;
|
||||
|
||||
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 (UPLOAD_ERR_OK !== $file['error'])
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,235 +0,0 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Methods to manage user. Not a standard controller as it has nothing to do with user based restrictions and must be usable only by admin.
|
||||
*/
|
||||
class User extends \descartes\InternalController
|
||||
{
|
||||
private $model_user;
|
||||
private $internal_event;
|
||||
private $internal_setting;
|
||||
|
||||
public function __construct(\PDO $bdd)
|
||||
{
|
||||
$this->model_user = new \models\User($bdd);
|
||||
$this->internal_event = new \controllers\internals\Event($bdd);
|
||||
$this->internal_setting = new \controllers\internals\Setting($bdd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return list of users as an array.
|
||||
*
|
||||
* @param mixed(int|bool) $nb_entry : Number of entry to return
|
||||
* @param mixed(int|bool) $page : Numero of page
|
||||
*
|
||||
* @return array|bool : List of user or false
|
||||
*/
|
||||
public function list(?int $nb_entry = null, ?int $page = null)
|
||||
{
|
||||
return $this->model_user->list($nb_entry, $page * $nb_entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a user.
|
||||
*
|
||||
* @param array $ids : Les id des useres à supprimer
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return int : Number of users deleted
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
return $this->model_user->remove($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check user credentials.
|
||||
*
|
||||
* @param string $email : User email
|
||||
* @param string $password : User password
|
||||
*
|
||||
* @return mixed false | array : False if no user for thoses credentials, the user else
|
||||
*/
|
||||
public function check_credentials($email, $password)
|
||||
{
|
||||
$user = $this->model_user->get_by_email($email);
|
||||
if (!$user)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!password_verify($password, $user['password']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user password.
|
||||
*
|
||||
* @param string $id : User id
|
||||
* @param string $password : New password
|
||||
*
|
||||
* @return bool;
|
||||
*/
|
||||
public function update_password(int $id, string $password): bool
|
||||
{
|
||||
$password = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
return (bool) $this->model_user->update_password($id, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user email.
|
||||
*
|
||||
* @param string $id : user id
|
||||
* @param string $email : new mail
|
||||
*
|
||||
* @return boolean;
|
||||
*/
|
||||
public function update_email($id, $email)
|
||||
{
|
||||
return (bool) $this->model_user->update_email($id, $email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user api key.
|
||||
*
|
||||
* @param string $id : user id
|
||||
* @param ?string $api_key : new api key
|
||||
*
|
||||
* @return mixed : false on error, else new api key;
|
||||
*/
|
||||
public function update_api_key($id, ?string $api_key = null)
|
||||
{
|
||||
$api_key = $api_key ?? $this->generate_random_api_key();
|
||||
$success = $this->model_user->update($id, ['api_key' => $api_key]);
|
||||
|
||||
if (!$success)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $api_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user by his email address.
|
||||
*
|
||||
* @param string $email : User email
|
||||
*
|
||||
* @return mixed boolean | array : false if cannot find user for this email, the user else
|
||||
*/
|
||||
public function get_by_email($email)
|
||||
{
|
||||
return $this->model_user->get_by_email($email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a user by his id.
|
||||
*
|
||||
* @param string $id : User id
|
||||
*
|
||||
* @return mixed array
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
return $this->model_user->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user by his api_key address.
|
||||
*
|
||||
* @param string $api_key : User api key
|
||||
*
|
||||
* @return mixed boolean | array : false if cannot find user for this api key, the user else
|
||||
*/
|
||||
public function get_by_api_key(string $api_key)
|
||||
{
|
||||
return $this->model_user->get_by_api_key($api_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user by his id.
|
||||
*
|
||||
* @param mixed $id
|
||||
* @param mixed $email
|
||||
* @param mixed $password
|
||||
* @param mixed $admin
|
||||
* @param mixed $api_key
|
||||
*
|
||||
* @return int : Number of modified user
|
||||
*/
|
||||
public function update($id, $email, $password, $admin, $api_key)
|
||||
{
|
||||
$user = [
|
||||
'email' => $email,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT),
|
||||
'admin' => $admin,
|
||||
'api_key' => $api_key,
|
||||
];
|
||||
|
||||
return $this->model_user->update($id, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user.
|
||||
*
|
||||
* @param mixed $email
|
||||
* @param mixed $password
|
||||
* @param mixed $admin
|
||||
* @param ?string $api_key : The api key of the user, if null generate randomly
|
||||
*
|
||||
* @return mixed bool|int : false on error, id of the new user else
|
||||
*/
|
||||
public function create($email, $password, $admin, ?string $api_key = null)
|
||||
{
|
||||
$user = [
|
||||
'email' => $email,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT),
|
||||
'admin' => $admin,
|
||||
'api_key' => $api_key ?? $this->generate_random_api_key(),
|
||||
];
|
||||
|
||||
$new_user_id = $this->model_user->insert($user);
|
||||
|
||||
if (!$new_user_id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$success = $this->internal_setting->create_defaults_for_user($new_user_id);
|
||||
|
||||
if (!$success)
|
||||
{
|
||||
$this->delete($new_user_id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $new_user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random api key.
|
||||
*
|
||||
* @return string : The api key
|
||||
*/
|
||||
public function generate_random_api_key(): string
|
||||
{
|
||||
return bin2hex(random_bytes(16));
|
||||
}
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
<?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 $bdd;
|
||||
protected $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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all webhooks for a user and for a type of webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_type_and_user(int $id_user, string $type)
|
||||
{
|
||||
return $this->get_model()->gets_for_type_and_user($id_user, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue