diff --git a/adapters/TestAdapter.php b/adapters/TestAdapter.php new file mode 100644 index 0000000..ebd3002 --- /dev/null +++ b/adapters/TestAdapter.php @@ -0,0 +1,86 @@ +number = $number; + $this->datas = $datas; + } + + + /** + * Method called to send a SMS to a number + * @param string $destination : Phone number to send the sms to + * @param string $text : Text of the SMS to send + * @param bool $flash : Is the SMS a Flash SMS + * @return bool : True if send, False else + */ + public function send (string $destination, string $text, boolean $flash) : boolean + { + return true; + } + + + /** + * Method called to read SMSs of the number + * @param float $since : Unix microtime representation of the date from wich we want to read the SMSs + * @return array : Array of the sms reads + */ + public function read (float $since) : array + { + return []; + } + } diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index 3cc4705..d673b03 100755 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -217,7 +217,7 @@ namespace controllers\internals; /** * Get all messages to send and the number to use to send theme - * @return array : [['text', 'origin', 'destination', 'flash'], ...] + * @return array : [['id_scheduled', 'text', 'origin', 'destination', 'flash'], ...] */ public function get_smss_to_send () { @@ -266,6 +266,7 @@ namespace controllers\internals; foreach ($numbers as $number) { $message = [ + 'id_scheduled' => $scheduled['id'], 'origin' => $scheduled['origin'], 'destination' => $number['number'], 'flash' => $scheduled['flash'], @@ -326,6 +327,7 @@ namespace controllers\internals; $added_contacts[$contact['id']] = true; $message = [ + 'id_scheduled' => $scheduled['id'], 'origin' => $scheduled['origin'], 'destination' => $number['number'], 'flash' => $scheduled['flash'], @@ -373,8 +375,8 @@ namespace controllers\internals; return $smss_to_send; } - - + + /** * Return numbers for a scheduled message diff --git a/controllers/internals/Sended.php b/controllers/internals/Sended.php index 857e3a8..b424df4 100755 --- a/controllers/internals/Sended.php +++ b/controllers/internals/Sended.php @@ -32,10 +32,10 @@ namespace controllers\internals; * @param string $origin : Number of the sender * @param string $destination : Number of the receiver * @param bool $flash : Is the sms a flash - * @param ?string $status : Status of a the sms. By default null -> unknown + * @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, bool $flash = false, ?string $status = null) : bool + public function create ($at, string $text, string $origin, string $destination, bool $flash = false, ?string $status = 'unknown') : bool { $sended = [ 'at' => $at, diff --git a/controllers/internals/StandardController.php b/controllers/internals/StandardController.php index 8696a64..f7d61b3 100755 --- a/controllers/internals/StandardController.php +++ b/controllers/internals/StandardController.php @@ -93,6 +93,17 @@ namespace controllers\internals; { 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); + } /** diff --git a/controllers/publics/Scheduled.php b/controllers/publics/Scheduled.php index aeff877..cec54b2 100755 --- a/controllers/publics/Scheduled.php +++ b/controllers/publics/Scheduled.php @@ -234,7 +234,7 @@ namespace controllers\publics; $id_user = $_SESSION['user']['id']; $at = $_POST['at'] ?? false; $text = $_POST['text'] ?? false; - $flash = $_POST['flash'] ?? false; + $flash = (bool) ($_POST['flash'] ?? false); $origin = empty($_POST['origin']) ? null : $_POST['origin']; $numbers = $_POST['numbers'] ?? []; $contacts = $_POST['contacts'] ?? []; @@ -337,7 +337,7 @@ namespace controllers\publics; $at = $scheduled['at'] ?? false; $text = $scheduled['text'] ?? false; $origin = empty($scheduled['origin']) ? null : $scheduled['origin']; - $flash = $scheduled['flash'] ?? false; + $flash = (bool) ($scheduled['flash'] ?? false); $numbers = $scheduled['numbers'] ?? []; $contacts = $scheduled['contacts'] ?? []; $groups = $scheduled['groups'] ?? []; diff --git a/daemons/Phone.php b/daemons/Phone.php index 319747c..2242ae3 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -52,11 +52,23 @@ class Phone extends AbstractDaemon { return true; } + + $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); + $internal_sended = new \controllers\internals\Sended($bdd); //If message received, update last message time $this->last_message_at = microtime(true); - $this->logger->debug(json_encode($message)); + //Register message as sended + $now = new \DateTime(); + $now = $now->format('Y-m-d H:i:s'); + $internal_sended->create($now, $message['text'], $message['origin'], $message['destination'], $message['flash']); + + //Close bdd + $bdd = null; + $internal_sended = null; + + $this->logger->info('Send message : ' . json_encode($message)); } diff --git a/daemons/Server.php b/daemons/Server.php index 715d5f0..04cca34 100644 --- a/daemons/Server.php +++ b/daemons/Server.php @@ -52,10 +52,8 @@ class Server extends AbstractDaemon continue; } - exec('php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &'); - $this->logger->info('Command : ' . 'php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &'); //Create a new daemon for the phone - //$phone = new \daemons\Phone($phone); + exec('php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &'); } $queues = []; @@ -85,8 +83,10 @@ class Server extends AbstractDaemon ]; msg_send($queue, SEND_MSG, $msg); - } + //Delete the scheduled sms after sending + $this->internal_scheduled->delete($sms['id_scheduled']); + } sleep(0.5); } diff --git a/models/StandardModel.php b/models/StandardModel.php index 4049136..754c35d 100755 --- a/models/StandardModel.php +++ b/models/StandardModel.php @@ -121,6 +121,17 @@ namespace models; { return $this->_delete($this->get_table_name(), ['id_user' => $id_user, 'id' => $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->_delete($this->get_table_name(), ['id' => $id]); + } /**