fix codestyle

This commit is contained in:
osaajani 2020-06-23 21:06:13 +02:00
parent 350dbb5b59
commit adef27f862
37 changed files with 556 additions and 488 deletions

View file

@ -69,14 +69,14 @@ namespace controllers\internals;
}
/**
* Analyse a message to check if it's a command so execute it
* Analyse a message to check if it's a command so execute it.
*
* @param int $id_user : User id to search a command for
* @param string $message : Message to analyse
*
* @return mixed bool|string : false if not a valid command, anonymized message if valid command
*/
public function analyze_and_process (int $id_user, string $message)
public function analyze_and_process(int $id_user, string $message)
{
if (!ENABLE_COMMAND)
{

View file

@ -39,7 +39,7 @@ namespace controllers\internals;
{
new \daemons\Webhook();
}
/**
* Start mailer daemon.
*/
@ -98,13 +98,12 @@ namespace controllers\internals;
}
/**
* Update a user status.
*
* Update a user status
*
* @param string $email : User email
* @param string $email : User email
* @param string $status : User status, default \models\User::STATUS_ACTIVE
*/
public function update_user_status (string $email, string $status)
public function update_user_status(string $email, string $status)
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_user = new \controllers\internals\User($bdd);

View file

@ -148,7 +148,7 @@ namespace controllers\internals;
$i = 0;
foreach ($line as $key => $value)
{
$i++;
++$i;
if ($i < 3)
{
continue;
@ -164,7 +164,7 @@ namespace controllers\internals;
}
$datas = json_encode($datas);
try
try
{
$success = $this->create($id_user, $line[array_keys($line)[1]], $line[array_keys($line)[0]], $datas);
if ($success)

View file

@ -20,20 +20,23 @@ class ExpressionProvider implements ExpressionFunctionProviderInterface
{
//Override default constant() function to make it return null
//This will prevent the use of constant() func to read constants with security impact (such as session, db credentials, etc.)
$neutralized_constant = new ExpressionFunction('constant', function ($str) {
$neutralized_constant = new ExpressionFunction('constant', function ($str)
{
return null;
}, function ($arguments, $str) {
}, function ($arguments, $str)
{
return null;
});
//Exists must be personnalized because it inverse is_null
$exists = new ExpressionFunction('exists', function ($var) {
$exists = new ExpressionFunction('exists', function ($var)
{
return sprintf('!is_null(%1$s)', $str);
}, function ($arguments, $var) {
return !is_null($var);
}, function ($arguments, $var)
{
return null !== $var;
});
return [
$neutralized_constant,
$exists,

View file

@ -27,9 +27,9 @@ namespace controllers\internals;
}
/**
* Return unknown error page
* Return unknown error page.
*/
public function unknown ()
public function unknown()
{
http_response_code(500);
$this->render('error/unknown');

View file

@ -1,35 +1,43 @@
<?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 Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
/**
* Mailing class
* Mailing class.
*/
class Mailer extends \descartes\Controller
{
private $log;
private $mail;
public function __construct ()
public function __construct()
{
$this->log = new Logger('Mailer');
$this->log->pushHandler(new StreamHandler(PWD_LOGS . '/mail.log', Logger::DEBUG));
$this->mail = new PHPMailer(true);
$this->mail->CharSet = 'utf-8';
$this->mail->SMTPDebug = SMTP::DEBUG_OFF;
$this->mail->CharSet = 'utf-8';
$this->mail->SMTPDebug = SMTP::DEBUG_OFF;
$this->mail->isSMTP();
$this->mail->Host = MAIL['SMTP']['HOST'];
$this->mail->SMTPAuth = true;
$this->mail->Username = MAIL['SMTP']['USER'];
$this->mail->Password = MAIL['SMTP']['PASS'];
$this->mail->Port = MAIL['SMTP']['PORT'];
$this->mail->Host = MAIL['SMTP']['HOST'];
$this->mail->SMTPAuth = true;
$this->mail->Username = MAIL['SMTP']['USER'];
$this->mail->Password = MAIL['SMTP']['PASS'];
$this->mail->Port = MAIL['SMTP']['PORT'];
$this->mail->setFrom(MAIL['FROM']);
if (MAIL['SMTP']['TLS'])
@ -39,15 +47,17 @@ class Mailer extends \descartes\Controller
}
/**
* Send email
* @param array $destinations : Destinations address
* @param string $subject : Message subject
* @param string $message : Message
* @param ?string $alt_message : Alt Message if no html support. Null if message is not html.
* @param array $attachments : List of path to attachment files
* Send email.
*
* @param array $destinations : Destinations address
* @param string $subject : Message subject
* @param string $message : Message
* @param ?string $alt_message : Alt Message if no html support. Null if message is not html.
* @param array $attachments : List of path to attachment files
*
* @return bool : false on error, true else
*/
public function send (array $destinations, string $subject, string $message, ?string $alt_message = null, array $attachments = [])
public function send(array $destinations, string $subject, string $message, ?string $alt_message = null, array $attachments = [])
{
try
{
@ -66,7 +76,7 @@ class Mailer extends \descartes\Controller
$mail->Subject = $subject;
$mail->Body = $message;
if ($alt_message)
{
$mail->isHTML($html);
@ -74,32 +84,61 @@ class Mailer extends \descartes\Controller
}
$mail->send();
return true;
}
catch (\Throwable $t)
{
$this->log->error('Error sending mail : ' . $t);
return false;
}
}
/**
* Generate an email body
* @param array $settings : [
* string 'type' => Internal RaspiSMS email type,
* string 'subject' => Email subject,
* string 'template' => Email template to use
* ?string 'alt_template' => Template to use for alt message, if null ignore
* ]
* Enqueue an email for later sending.
*
* @param string $destination : email address to send email to
* @param array $settings : Email settings
* @param array $datas : Datas to inject into email template
*
* @return bool : true on success, false on error
*/
public function enqueue(string $destination, array $settings, array $datas): bool
{
$response = $this->generate_body($settings, $datas);
$message = [
'destinations' => [$destination],
'subject' => $settings['subject'],
'body' => $response['body'],
'alt_body' => $response['alt_body'],
];
$error_code = null;
$queue = msg_get_queue(QUEUE_ID_EMAIL);
$success = msg_send($queue, QUEUE_TYPE_EMAIL, $message, true, true, $error_code);
return (bool) $success;
}
/**
* Generate an email body.
*
* @param array $settings : [
* string 'type' => Internal RaspiSMS email type,
* string 'subject' => Email subject,
* string 'template' => Email template to use
* ?string 'alt_template' => Template to use for alt message, if null ignore
* ]
* @param array : Datas to inject into email template
*
* @return array [
* string 'body' => email body
* ?string 'alt_body' => email alternative body if needed
* ]
* string 'body' => email body
* ?string 'alt_body' => email alternative body if needed
* ]
*/
private function generate_body(array $settings, array $datas) : array
private function generate_body(array $settings, array $datas): array
{
//Generate body of email
ob_start();
@ -114,34 +153,10 @@ class Mailer extends \descartes\Controller
$this->render($settings['alt_template'], $datas);
$alt_body = ob_get_clean();
}
return [
'body' => $body,
'body' => $body,
'alt_body' => $alt_body,
];
}
/**
* Enqueue an email for later sending
* @param string $destination : email address to send email to
* @param array $settings : Email settings
* @param array $datas : Datas to inject into email template
* @return bool : true on success, false on error
*/
public function enqueue (string $destination, array $settings, array $datas) : bool
{
$response = $this->generate_body($settings, $datas);
$message = [
'destinations' => [$destination],
'subject' => $settings['subject'],
'body' => $response['body'],
'alt_body' => $response['alt_body'],
];
$error_code = null;
$queue = msg_get_queue(QUEUE_ID_EMAIL);
$success = msg_send($queue, QUEUE_TYPE_EMAIL, $message, true, true, $error_code);
return (bool) $success;
}
}

View file

@ -28,7 +28,7 @@ namespace controllers\internals;
}
/**
* Return a phone by his name
* Return a phone by his name.
*
* @param string $name : Phone name
*
@ -40,10 +40,10 @@ namespace controllers\internals;
}
/**
* Return a phone for a user by a name
* Return a phone for a user by a name.
*
* @param int $id_user : user id
* @param string $name : Phone name
* @param string $name : Phone name
*
* @return array
*/
@ -56,7 +56,7 @@ namespace controllers\internals;
* Create a phone.
*
* @param int $id_user : User to insert phone for
* @param string $name : The name of the phone
* @param string $name : The name 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)
*
@ -79,7 +79,7 @@ namespace controllers\internals;
*
* @param int $id_user : User to insert phone for
* @param int $id : Phone id
* @param string $name : The name of the phone
* @param string $name : The name 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)
*

View file

@ -36,9 +36,9 @@ namespace controllers\internals;
* @param int $id_phone : Id of the number the message was send with
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @param string $origin : Number of the sender
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
*
* @return mixed : false on error, new received id else
*/
@ -203,31 +203,21 @@ namespace controllers\internals;
}
/**
* Get the model for the Controller.
* Receive a SMS message.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Received($this->bdd);
return $this->model;
}
/**
* Receive a SMS message
* @param int $id_user : Id of user to create sended message for
* @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the phone the message was sent to
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param ?string $at : Message reception date, if null use current date
* @param string $status : Status of a the sms. By default \models\Received::STATUS_UNREAD
* @param string $origin : Number of the sender
* @param ?string $at : Message reception date, if null use current date
* @param string $status : Status of a the sms. By default \models\Received::STATUS_UNREAD
*
* @return array : [
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
*/
public function receive (int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD) : array
public function receive(int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD): array
{
$return = [
'error' => false,
@ -240,8 +230,8 @@ namespace controllers\internals;
//Process the message to check plus potentially execute command and anonymize text
$internal_command = new Command($this->bdd);
$response = $internal_command->analyze_and_process($id_user, $text);
if ($response !== false) //Received sms is a command an we must use anonymized text
{
if (false !== $response)
{ //Received sms is a command an we must use anonymized text
$is_command = true;
$text = $response;
}
@ -251,6 +241,7 @@ namespace controllers\internals;
{
$return['error'] = true;
$return['error_message'] = 'Impossible to insert the sms in database.';
return $return;
}
@ -267,6 +258,19 @@ namespace controllers\internals;
$internal_user = new User($this->bdd);
$internal_user->transfer_received($id_user, $received);
return $return;
}
/**
* 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;
}
}

View file

@ -18,7 +18,7 @@ namespace controllers\internals;
/**
* Create a sended.
*
* @param int $id_user : Id of user to create sended message for
* @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the number the message was send with
* @param $at : Reception date
* @param $text : Text of the message
@ -111,7 +111,7 @@ namespace controllers\internals;
/**
* Return sended for an uid and an adapter.
*
* @param int $id_user : user id
* @param int $id_user : user id
* @param string $uid : Uid of the sended
* @param string $adapter : Adapter used to send the message
*
@ -157,32 +157,22 @@ namespace controllers\internals;
}
/**
* Get the model for the Controller.
* Send a SMS message.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Sended($this->bdd);
return $this->model;
}
/**
* Send a SMS message
* @param \adapters\AdapterInterface $adapter : Adapter object to use to send the message
* @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the phone the message was send with
* @param \adapters\AdapterInterface $adapter : Adapter object to use to send the message
* @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the phone the message was send with
* @param $text : Text of the message
* @param string $destination : Number of the receiver
* @param bool $flash : Is the sms a flash. By default false.
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
*
* @return array : [
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
*/
public function send (\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, string $status = \models\Sended::STATUS_UNKNOWN) : array
public function send(\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, string $status = \models\Sended::STATUS_UNKNOWN): array
{
$return = [
'error' => false,
@ -198,6 +188,7 @@ namespace controllers\internals;
$return['error_message'] = $response['error_message'];
$status = \models\Sended::STATUS_FAILED;
$this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $status);
return $return;
}
@ -213,7 +204,19 @@ namespace controllers\internals;
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND, $sended);
return $return;
}
/**
* 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;
}
}

View file

@ -17,7 +17,7 @@ namespace controllers\internals;
public function __construct(?\PDO $bdd = null)
{
if ($bdd === null)
if (null === $bdd)
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
}

View file

@ -106,12 +106,11 @@ namespace controllers\internals;
{
return (bool) $this->model_user->update_email($id, $email);
}
/**
* Update user status.
*
* @param string $id : user id
* @param string $id : user id
* @param string $status : new status
*
* @return boolean;
@ -181,13 +180,13 @@ namespace controllers\internals;
/**
* Update a user by his id.
*
* @param mixed $id
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param mixed $api_key
* @param string $status : User status
* @param bool $encrypt_password : Should the password be encrypted, by default true
* @param mixed $id
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param mixed $api_key
* @param string $status : User status
* @param bool $encrypt_password : Should the password be encrypted, by default true
*
* @return int : Number of modified user
*/
@ -210,9 +209,9 @@ namespace controllers\internals;
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param ?string $api_key : The api key of the user, if null generate randomly
* @param string $status : User status, default \models\User::STATUS_ACTIVE
* @param bool $encrypt_password : Should the password be encrypted, by default true
* @param ?string $api_key : The api key of the user, if null generate randomly
* @param string $status : User status, default \models\User::STATUS_ACTIVE
* @param bool $encrypt_password : Should the password be encrypted, by default true
*
* @return mixed bool|int : false on error, id of the new user else
*/
@ -255,21 +254,21 @@ namespace controllers\internals;
return bin2hex(random_bytes(16));
}
/**
* Transfer a received sms to user email
* @param int $id_user : User id
* Transfer a received sms to user email.
*
* @param int $id_user : User id
* @param array $received : [
* int 'id' => sms id,
* string 'at' => sms reception date,
* string 'text' => sms content,
* string 'destination' => id of phone the sms was sent to
* string 'origin' => phone number that sent the sms
* ]
* int 'id' => sms id,
* string 'at' => sms reception date,
* string 'text' => sms content,
* string 'destination' => id of phone the sms was sent to
* string 'origin' => phone number that sent the sms
* ]
*
* @return bool : False if no transfer, true else
*/
public function transfer_received (int $id_user, array $received) : bool
public function transfer_received(int $id_user, array $received): bool
{
$settings = $this->internal_setting->gets_for_user($id_user);
@ -291,6 +290,7 @@ namespace controllers\internals;
}
$mailer = new Mailer();
return $mailer->enqueue($user['email'], EMAIL_TRANSFER_SMS, [
'at' => $received['at'],
'origin' => $received['origin'],

View file

@ -87,33 +87,22 @@ class Webhook extends StandardController
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;
}
/**
* Trigger a webhook and transmit the signal to webhook daemon if needed.
* @param int $id_user : User to trigger the webhook for
* @param string $type : Type of webhook to trigger
* @param array $sms : The sms [
* int 'id' => SMS id,
* string 'at' => SMS date,
* string 'text' => sms body,
* string 'origin' => sms origin (number or phone id)
* string 'destination' => sms destination (number or phone id)
* ]
*
* @param int $id_user : User to trigger the webhook for
* @param string $type : Type of webhook to trigger
* @param array $sms : The sms [
* int 'id' => SMS id,
* string 'at' => SMS date,
* string 'text' => sms body,
* string 'origin' => sms origin (number or phone id)
* string 'destination' => sms destination (number or phone id)
* ]
*
* @return bool : False if no trigger, true else
*/
public function trigger (int $id_user, string $type, array $sms)
public function trigger(int $id_user, string $type, array $sms)
{
$internal_setting = new Setting($this->bdd);
$settings = $internal_setting->gets_for_user($id_user);
@ -141,7 +130,20 @@ class Webhook extends StandardController
$error_code = null;
$queue = msg_get_queue(QUEUE_ID_WEBHOOK);
$success = msg_send($queue, QUEUE_TYPE_WEBHOOK, $message, true, true, $error_code);
return (bool) $success;
}
}
/**
* 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;
}
}