diff --git a/.gitignore b/.gitignore index 2dd1406..78ab9f5 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,6 @@ descartes/env.php data/test_write_sms.json data/test_read_sms.json -data/medias/ +data/public/ !*.dist diff --git a/.htaccess b/.htaccess index ad6df7b..ce35d68 100644 --- a/.htaccess +++ b/.htaccess @@ -1,5 +1,5 @@ RewriteEngine on RewriteRule ^assets - [L] RewriteRule ^.well-known - [L] -RewriteRule ^data/medias - [L] +RewriteRule ^data/public/ - [L] RewriteRule . index.php diff --git a/adapters/TestAdapter.php b/adapters/TestAdapter.php index 4ddb9c6..b92d78c 100644 --- a/adapters/TestAdapter.php +++ b/adapters/TestAdapter.php @@ -184,6 +184,20 @@ namespace adapters; return $response; } + /** + * Read from a files to simulate sms reception. + * In the file we expect a json string representing an array of sms of format : + * { + * "at" : "2021-03-26 11:21:48", + * "medias" : [ + * "https://unsplash.com/photos/q4DJVtxES0w/download?force=true&w=640", + * "/tmp/somelocalfile.jpg" + * ], + * "mms" : true, + * "origin" : "+33612345678", + * "text" : "SMS Text" + * } + */ public function read(): array { $response = [ @@ -223,7 +237,36 @@ namespace adapters; continue; } - $response['smss'][] = $decode_sms; + $clean_sms = [ + 'at' => $decode_sms['at'], + 'text' => $decode_sms['text'], + 'origin' => $decode_sms['origin'], + 'mms' => $decode_sms['mms'], + 'medias' => [], + ]; + + //In medias we want a media URI or URL + foreach ($decode_sms['medias'] ?? [] as $media) + { + $tempfile = tempnam('/tmp', 'raspisms-media-'); + if (!$tempfile) + { + continue; + } + + $copy = copy($media, $tempfile); + if (!$copy) + { + continue; + } + + $clean_sms['medias'][] = [ + 'filepath' => $tempfile, + 'extension' => pathinfo($media, PATHINFO_EXTENSION) ?: null, + ]; + } + + $response['smss'][] = $clean_sms; } return $response; diff --git a/controllers/internals/Media.php b/controllers/internals/Media.php index 5e33498..e2f5dd5 100644 --- a/controllers/internals/Media.php +++ b/controllers/internals/Media.php @@ -11,248 +11,281 @@ namespace controllers\internals; - class Media extends StandardController +class Media extends StandardController +{ + const DEFAULT_CHMOD = 0660; + + protected $model; + + /** + * Create a media. + * + * @param int $id_user : Id of the user + * @param string $tmpfile_path : Path of the temporary local copy of the media + * @param ?string $extension : Extension to use for the media + * + * @return int : Exception on error, new media id else + */ + public function create(int $id_user, string $tmpfile_path, ?string $extension = null) { - protected $model; - - /** - * Create a media. - * - * @param int $id_user : Id of the user - * @param string $path : path of the media in data dir - * - * @return mixed bool|int : false on error, new media id else - */ - public function create(int $id_user, string $path) + $user_path = \controllers\internals\Tool::create_user_public_path($id_user); + if (!file_exists($tmpfile_path) || !is_readable($tmpfile_path)) { - $data = [ - 'path' => $path, - 'id_user' => $id_user, - ]; - - return $this->get_model()->insert($data); + throw new \Exception('File ' . $tmpfile_path . ' is not readable.'); } - /** - * Upload and create a media - * - * @param int $id_user : Id of the user - * @param array $file : array representing uploaded file, extracted from $_FILES['yourfile'] - * @return mixed bool | int : False on error, or new media id on success - */ - public function upload_and_create_for_user(int $id_user, array $file) + $mimey = new \Mimey\MimeTypes; + $extension = $extension ?? $mimey->getExtension(mime_content_type($tmpfile_path)); + + $new_file_name = \controllers\internals\Tool::random_uuid() . '.' . $extension; + $new_file_path = $user_path . '/' . $new_file_name; + $new_file_relpath = $id_user . '/' . $new_file_name; + + if (!rename($tmpfile_path, $new_file_path)) { - $user_media_path = PWD_DATA . '/medias/' . $id_user; - - //Create user medias dir if not exists - if (!file_exists($user_media_path)) - { - if (!mkdir($user_media_path, fileperms(PWD_DATA), true)) - { - return false; - } - } - - $upload_result = \controllers\internals\Tool::save_uploaded_file($file, $user_media_path); - if ($upload_result['success'] !== true) - { - return false; - } - - $new_filepath = 'medias/' . $id_user . '/' . $upload_result['content']; - return $this->create($id_user, $new_filepath); + throw new \Exception('Cannot create file ' . $new_file_path); } - /** - * Link a media to a scheduled, a received or a sended message - * @param int $id_media : Id of the media - * @param string $resource_type : Type of resource to link the media to ('scheduled', 'received' or 'sended') - * @param int $resource_id : Id of the resource to link the media to - * - * @return mixed bool|int : false on error, the new link id else - */ - public function link_to(int $id_media, string $resource_type, int $resource_id) + if (!chown($new_file_path, fileowner($user_path))) { - switch ($resource_type) - { - case 'scheduled': - return $this->get_model()->insert_media_scheduled($id_media, $resource_id); - break; - - case 'received': - return $this->get_model()->insert_media_received($id_media, $resource_id); - break; - - case 'sended': - return $this->get_model()->insert_media_sended($id_media, $resource_id); - break; - - default: - return false; - } + throw new \Exception('Cannot give file ' . $new_file_path . ' to user : ' . fileowner($user_path)); } - - /** - * Unlink a media of a scheduled, a received or a sended message - * @param int $id_media : Id of the media - * @param string $resource_type : Type of resource to unlink the media of ('scheduled', 'received' or 'sended') - * @param int $resource_id : Id of the resource to unlink the media of - * - * @return mixed bool : false on error, true on success - */ - public function unlink_of(int $id_media, int $resource_type, int $resource_id) + if (!chgrp($new_file_path, filegroup($user_path))) { - switch ($resource_type) - { - case 'scheduled': - return $this->get_model()->delete_media_scheduled($id_media, $resource_id); - break; - - case 'received': - return $this->get_model()->delete_media_received($id_media, $resource_id); - break; - - case 'sended': - return $this->get_model()->delete_media_sended($id_media, $resource_id); - break; - - default: - return false; - } - } - - /** - * Unlink all medias of a scheduled, a received or a sended message - * @param string $resource_type : Type of resource to unlink the media of ('scheduled', 'received' or 'sended') - * @param int $resource_id : Id of the resource to unlink the media of - * - * @return mixed bool : false on error, true on success - */ - public function unlink_all_of(string $resource_type, int $resource_id) - { - switch ($resource_type) - { - case 'scheduled': - return $this->get_model()->delete_all_for_scheduled($resource_id); - break; - - case 'received': - return $this->get_model()->delete_all_for_received($resource_id); - break; - - case 'sended': - return $this->get_model()->delete_all_for_sended($resource_id); - break; - - default: - return false; - } + throw new \Exception('Cannot give file ' . $new_file_path . ' to group : ' . filegroup($user_path)); } - /** - * Update a media for a user. - * - * @param int $id_user : user id - * @param int $id_media : Media id - * @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, string $path): bool + if (!chmod($new_file_path, self::DEFAULT_CHMOD)) { - $media = [ - 'path' => $path, - ]; - - return (bool) $this->get_model()->update_for_user($id_user, $id_media, $media); + throw new \Exception('Cannot give file ' . $new_file_path . ' rights : ' . self::DEFAULT_CHMOD); } - /** - * Delete a media for a user. - * - * @param int $id_user : User id - * @param int $id : Entry id - * - * @return mixed bool|int : False on error, else number of removed rows - */ - public function delete_for_user(int $id_user, int $id_media): bool + $data = [ + 'path' => $new_file_relpath, + 'id_user' => $id_user, + ]; + + $new_media_id = $this->get_model()->insert($data); + if (!$new_media_id) { - $media = $this->get_model()->get_for_user($id_user, $id_media); - if (!$media) - { - return false; - } - - //Delete file - try - { - $filepath = PWD_DATA . '/' . $media['path']; - if (file_exists($filepath)) - { - unlink($filepath); - } - } - catch (\Throwable $t) - { - return false; - } - - return $this->get_model()->delete_for_user($id_user, $id_media); + throw new \Exception('Cannot insert media in database.'); } - /** - * Find medias for a scheduled. - * - * @param int $id_scheduled : Scheduled id to fin medias for - * - * @return mixed : Medias || false - */ - public function gets_for_scheduled(int $id_scheduled) + return $new_media_id; + } + + /** + * Upload and create a media + * + * @param int $id_user : Id of the user + * @param array $file : array representing uploaded file, extracted from $_FILES['yourfile'] + * @return int : Raise exception on error or return new media id on success + */ + public function create_from_uploaded_file_for_user(int $id_user, array $file) + { + $upload_result = \controllers\internals\Tool::read_uploaded_file($file); + if ($upload_result['success'] !== true) { - return $this->get_model()->gets_for_scheduled($id_scheduled); - } - - /** - * Find medias for a sended and a user. - * - * @param int $id_sended : Scheduled id to fin medias for - * - * @return mixed : Medias || false - */ - public function gets_for_sended(int $id_sended) - { - return $this->get_model()->gets_for_sended($id_sended); - } - - /** - * Find medias for a received and a user. - * - * @param int $id_received : Scheduled id to fin medias for - * - * @return mixed : Medias || false - */ - public function gets_for_received(int $id_received) - { - return $this->get_model()->gets_for_received($id_received); + throw new \Exception($upload_result['content']); } - /** - * Find medias that are not used - * @return array - */ - public function gets_unused() - { - return $this->get_model()->gets_unused(); - } + return $this->create($id_user, $upload_result['tmp_name'], $upload_result['extension']); - /** - * Get the model for the Controller. - */ - protected function get_model(): \descartes\Model - { - $this->model = $this->model ?? new \models\Media($this->bdd); + $new_filepath = 'medias/' . $id_user . '/' . $upload_result['content']; + return $this->create($id_user, $new_filepath); + } - return $this->model; + /** + * Link a media to a scheduled, a received or a sended message + * @param int $id_media : Id of the media + * @param string $resource_type : Type of resource to link the media to ('scheduled', 'received' or 'sended') + * @param int $resource_id : Id of the resource to link the media to + * + * @return mixed bool|int : false on error, the new link id else + */ + public function link_to(int $id_media, string $resource_type, int $resource_id) + { + switch ($resource_type) + { + case 'scheduled': + return $this->get_model()->insert_media_scheduled($id_media, $resource_id); + break; + + case 'received': + return $this->get_model()->insert_media_received($id_media, $resource_id); + break; + + case 'sended': + return $this->get_model()->insert_media_sended($id_media, $resource_id); + break; + + default: + return false; } } + + + /** + * Unlink a media of a scheduled, a received or a sended message + * @param int $id_media : Id of the media + * @param string $resource_type : Type of resource to unlink the media of ('scheduled', 'received' or 'sended') + * @param int $resource_id : Id of the resource to unlink the media of + * + * @return mixed bool : false on error, true on success + */ + public function unlink_of(int $id_media, int $resource_type, int $resource_id) + { + switch ($resource_type) + { + case 'scheduled': + return $this->get_model()->delete_media_scheduled($id_media, $resource_id); + break; + + case 'received': + return $this->get_model()->delete_media_received($id_media, $resource_id); + break; + + case 'sended': + return $this->get_model()->delete_media_sended($id_media, $resource_id); + break; + + default: + return false; + } + } + + /** + * Unlink all medias of a scheduled, a received or a sended message + * @param string $resource_type : Type of resource to unlink the media of ('scheduled', 'received' or 'sended') + * @param int $resource_id : Id of the resource to unlink the media of + * + * @return mixed bool : false on error, true on success + */ + public function unlink_all_of(string $resource_type, int $resource_id) + { + switch ($resource_type) + { + case 'scheduled': + return $this->get_model()->delete_all_for_scheduled($resource_id); + break; + + case 'received': + return $this->get_model()->delete_all_for_received($resource_id); + break; + + case 'sended': + return $this->get_model()->delete_all_for_sended($resource_id); + break; + + default: + return false; + } + } + + /** + * Update a media for a user. + * + * @param int $id_user : user id + * @param int $id_media : Media id + * @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, string $path): bool + { + $media = [ + 'path' => $path, + ]; + + 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 mixed bool|int : False on error, else 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; + } + + //Delete file + try + { + $filepath = PWD_DATA . '/' . $media['path']; + if (file_exists($filepath)) + { + unlink($filepath); + } + } + catch (\Throwable $t) + { + return false; + } + + return $this->get_model()->delete_for_user($id_user, $id_media); + } + + /** + * Find medias for a scheduled. + * + * @param int $id_scheduled : Scheduled id to fin medias for + * + * @return mixed : Medias || false + */ + public function gets_for_scheduled(int $id_scheduled) + { + return $this->get_model()->gets_for_scheduled($id_scheduled); + } + + /** + * Find medias for a sended and a user. + * + * @param int $id_sended : Scheduled id to fin medias for + * + * @return mixed : Medias || false + */ + public function gets_for_sended(int $id_sended) + { + return $this->get_model()->gets_for_sended($id_sended); + } + + /** + * Find medias for a received and a user. + * + * @param int $id_received : Scheduled id to fin medias for + * + * @return mixed : Medias || false + */ + public function gets_for_received(int $id_received) + { + return $this->get_model()->gets_for_received($id_received); + } + + /** + * Find medias that are not used + * @return array + */ + public function gets_unused() + { + return $this->get_model()->gets_unused(); + } + + /** + * Get the model for the Controller. + */ + protected function get_model(): \descartes\Model + { + $this->model = $this->model ?? new \models\Media($this->bdd); + + return $this->model; + } +} diff --git a/controllers/internals/Received.php b/controllers/internals/Received.php index c685adb..285a8ca 100644 --- a/controllers/internals/Received.php +++ b/controllers/internals/Received.php @@ -244,14 +244,17 @@ namespace controllers\internals; * @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 bool $mms : Is the sms a mms - * @param array $media_ids : Ids of the medias to link to received + * @param array $medias : Empty array if no medias, or medias to create and link to the received message. Format : [[ + * string 'filepath' => local path to a readable copy of the media, + * ?string 'extension' => extension to use for the file or null + * ], ...] * * @return array : [ * 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, bool $mms = false, array $media_ids = []): array + public function receive(int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD, bool $mms = false, array $medias = []): array { $return = [ 'error' => false, @@ -269,6 +272,26 @@ namespace controllers\internals; $is_command = true; $text = $response; } + + //We create medias to link to the sms + $internal_media = new Media($this->bdd); + $media_ids = []; + if ($mms) + { + foreach ($medias as $media) + { + try + { + $new_media_id = $internal_media->create($id_user, $media['filepath'], $media['extension']); + $media_ids[] = $new_media_id; + } + catch (\Throwable $t) + { + $return['error_message'] = $t->getMessage(); + continue; //Better loose the media than the message + } + } + } $received_id = $this->create($id_user, $id_phone, $at, $text, $origin, $status, $is_command, $mms, $media_ids); if (!$received_id) @@ -287,7 +310,7 @@ namespace controllers\internals; 'origin' => $origin, 'command' => $is_command, 'mms' => $mms, - 'medias' => $this->get_model()->gets_in_for_user($id_user, $media_ids), + 'medias' => $internal_media->gets_in_for_user($id_user, $media_ids), ]; $internal_webhook = new Webhook($this->bdd); diff --git a/controllers/internals/Sended.php b/controllers/internals/Sended.php index ee3ea04..c9575c9 100644 --- a/controllers/internals/Sended.php +++ b/controllers/internals/Sended.php @@ -211,8 +211,8 @@ namespace controllers\internals; foreach ($medias as $media) { $media_uris[] = [ - 'http_url' => HTTP_PWD_DATA . '/' . $media['path'], - 'local_uri' => PWD_DATA . '/' . $media['path'], + 'http_url' => HTTP_PWD_DATA_PUBLIC . '/' . $media['path'], + 'local_uri' => PWD_DATA_PUBLIC . '/' . $media['path'], ]; } diff --git a/controllers/internals/Tool.php b/controllers/internals/Tool.php index 0f42360..b1a795e 100644 --- a/controllers/internals/Tool.php +++ b/controllers/internals/Tool.php @@ -205,7 +205,7 @@ namespace controllers\internals; * * @param array $file : The array extracted from $_FILES['file'] * - * @return array : ['success' => bool, 'content' => file handler | error message, 'error_code' => $file['error']] + * @return array : ['success' => bool, 'content' => file handler | error message, 'error_code' => $file['error'], 'mime_type' => server side calculated mimetype, 'extension' => original extension, 'tmp_name' => name of the tmp_file] */ public static function read_uploaded_file(array $file) { @@ -213,8 +213,9 @@ namespace controllers\internals; 'success' => false, 'content' => 'Une erreur inconnue est survenue.', 'error_code' => $file['error'] ?? 99, - 'mime_type' => false, - 'extension' => false, + 'mime_type' => null, + 'extension' => null, + 'tmp_name' => null, ]; if (UPLOAD_ERR_OK !== $file['error']) @@ -277,73 +278,6 @@ namespace controllers\internals; return $result; } - /** - * Allow to save an uploaded file from the $_FILE['file'] array - * - * @param array $file : The array extracted from $_FILES['file'] - * @param string $dirpath : The directory to save the file in - * @param bool $override : If true, override the file if another file with this name exists - * @param ?string $filename : The name to use for the file, if null use a highly random name - * @param ?string $extension : The extension to use for the file, if null try to determine it using original file extension, then mime_type - * @param bool $use_mimetype : If true, ignore original file extension to determine final file extension and use file real mimetype instead - * - * @return array : ['success' => bool, 'content' => new file name | error message, 'error_code' => $file['error']] - */ - public static function save_uploaded_file(array $file, string $dirpath, bool $override = false, ?string $filename = null, ?string $extension = null, bool $use_mimetype = false) - { - $result = [ - 'success' => false, - 'content' => 'Une erreur inconnue est survenue.', - 'error_code' => $file['error'] ?? 99, - ]; - - $upload_info = self::read_uploaded_file($file); - if (!$upload_info['success']) - { - $result['content'] = $upload_info['content']; - return $result; - } - - if ($extension === null) - { - $extension = $upload_info['extension']; - if ($extension === '' || $use_mimetype) - { - $mimey = new \Mimey\MimeTypes; - $extension = $mimey->getExtension($upload_info['mime_type']); - } - } - - if ($filename === null) - { - $filename = self::random_uuid(); - } - - $filename = $filename . '.' . $extension; - $filepath = $dirpath . '/' . $filename; - - if (file_exists($filepath) && !$override) - { - $result['content'] = 'Le fichier ' . $filepath . ' existe déjà.'; - - return $result; - } - - $success = move_uploaded_file($upload_info['tmp_name'], $filepath); - if (!$success) - { - $result['content'] = 'Impossible de délplacer le fichier vers ' . $filepath; - - return $result; - } - - $result['success'] = true; - $result['content'] = $filename; - - return $result; - } - - /** * Generate a highly random uuid based on timestamp and strong cryptographic random * @@ -354,4 +288,39 @@ namespace controllers\internals; $bytes = random_bytes(16); return time() . '-' . bin2hex($bytes); } + + + /** + * Create a user data public path + * @param int $id_user : The user id + * + * @return string : The created path + + * @exception Raise exception on error + */ + public static function create_user_public_path (int $id_user) + { + $new_dir = PWD_DATA_PUBLIC . '/' . $id_user; + if (file_exists($new_dir)) + { + return $new_dir; + } + + if (!mkdir($new_dir, fileperms(PWD_DATA_PUBLIC))) + { + throw new \Exception('Cannot create dir ' . $new_dir); + } + + if (!chown($new_dir, fileowner(PWD_DATA_PUBLIC))) + { + throw new \Exception('Cannot give dir ' . $new_dir . ' to user : ' . fileowner(PWD_DATA)); + } + + if (!chgrp($new_dir, filegroup(PWD_DATA_PUBLIC))) + { + throw new \Exception('Cannot give dir ' . $new_dir . ' to group : ' . filegroup(PWD_DATA)); + } + + return $new_dir; + } } diff --git a/controllers/internals/User.php b/controllers/internals/User.php index 99a47cf..b0b8eeb 100644 --- a/controllers/internals/User.php +++ b/controllers/internals/User.php @@ -264,6 +264,7 @@ namespace controllers\internals; * string 'text' => sms content, * string 'destination' => id of phone the sms was sent to * string 'origin' => phone number that sent the sms + * bool 'mms' => is the sms a mms * ] * * @return bool : False if no transfer, true else diff --git a/controllers/publics/Api.php b/controllers/publics/Api.php index a726345..626764a 100644 --- a/controllers/publics/Api.php +++ b/controllers/publics/Api.php @@ -362,12 +362,15 @@ namespace controllers\publics; { foreach ($files_arrays as $file) { - $new_media_id = $this->internal_media->upload_and_create_for_user($this->user['id'], $file); - if (!$new_media_id) + try + { + $new_media_id = $this->internal_media->upload_and_create_for_user($this->user['id'], $file); + } + catch (\Exception $e) { $return = self::DEFAULT_RETURN; $return['error'] = self::ERROR_CODES['CANNOT_CREATE']; - $return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'] . ' : Cannot upload and create media file ' . $file['name']; + $return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'] . ' : Cannot upload and create media file ' . $file['name'] . ' : ' . $e->getMessage(); $this->auto_http_code(false); return $this->json($return); diff --git a/controllers/publics/Callback.php b/controllers/publics/Callback.php index 66de202..d97a2e6 100644 --- a/controllers/publics/Callback.php +++ b/controllers/publics/Callback.php @@ -183,58 +183,10 @@ use Monolog\Logger; } $sms = $response['sms']; - $mms = !empty($sms['mms']); + $mms = (bool) $sms['mms'] ?? false; $medias = empty($sms['medias']) ? [] : $sms['medias']; - $media_ids = []; - //We create medias to link to the sms - if ($mms) - { - foreach ($medias as $media) - { - try - { - $media['mimetype'] = empty($media['mimetype']) ? mime_content_type($media['filepath']) : $media['mimetype']; - - $mimey = new \Mimey\MimeTypes; - $extension = empty($media['extension']) ? $mimey->getExtension($media['mimetype']) : $media['extension']; - - $new_filename = \controllers\internals\Tool::random_uuid() . '.' . $extension; - $new_filedir = PWD_DATA . '/medias/' . $this->user['id']; - $new_filerelpath = 'medias/' . $this->user['id'] . '/' . $new_filename; - $new_filepath = $new_filedir . '/' . $new_filename; - - //Create user dir if not exists - if (!file_exists($new_filedir)) - { - if (!mkdir($new_filedir, fileperms(PWD_DATA), true)) - { - throw new \Exception('Cannot create dir ' . $new_filedir . ' to copy media : ' . json_encode($media)); - } - } - - if (!rename($media['filepath'], $new_filepath)) - { - throw new \Exception('Cannot copy media : ' . json_encode($media) . ' to ' . $new_filepath); - } - - $new_media_id = $this->internal_media->create($this->user['id'], $new_filerelpath); - if (!$new_media_id) - { - throw new \Exception('Cannot save into db media : ' . json_encode($media)); - } - - $media_ids[] = $new_media_id; - } - catch (\Throwable $t) - { - $this->logger->error($t->getMessage()); - continue; - } - } - } - - $response = $this->internal_received->receive($this->user['id'], $id_phone, $sms['text'], $sms['origin'], $sms['at'], \models\Received::STATUS_UNREAD, $mms, $media_ids); + $response = $this->internal_received->receive($this->user['id'], $id_phone, $sms['text'], $sms['origin'], $sms['at'], \models\Received::STATUS_UNREAD, $mms, $medias); if ($response['error']) { $this->logger->error('Failed receive message : ' . json_encode($sms) . ' with error : ' . $response['error_message']); diff --git a/controllers/publics/Discussion.php b/controllers/publics/Discussion.php index 42cc010..b8fae9a 100644 --- a/controllers/publics/Discussion.php +++ b/controllers/publics/Discussion.php @@ -120,7 +120,7 @@ namespace controllers\publics; $medias = $this->internal_media->gets_for_sended($sended['id']); foreach ($medias as &$media) { - $media = HTTP_PWD_DATA . '/' . $media['path']; + $media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path']; } } @@ -149,7 +149,7 @@ namespace controllers\publics; $medias = $this->internal_media->gets_for_received($received['id']); foreach ($medias as &$media) { - $media = HTTP_PWD_DATA . '/' . $media['path']; + $media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path']; } } @@ -170,7 +170,7 @@ namespace controllers\publics; $medias = $this->internal_media->gets_for_scheduled($scheduled['id']); foreach ($medias as &$media) { - $media = HTTP_PWD_DATA . '/' . $media['path']; + $media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path']; } } @@ -288,11 +288,14 @@ namespace controllers\publics; { foreach ($files_arrays as $file) { - $new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file); - if (!$new_media_id) + try + { + $new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file); + } + catch (\Exception $e) { $return['success'] = false; - $return['message'] = 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name']; + $return['message'] = $e->getMessage(); echo json_encode($return); return false; diff --git a/controllers/publics/Scheduled.php b/controllers/publics/Scheduled.php index 677da5e..b96c58d 100644 --- a/controllers/publics/Scheduled.php +++ b/controllers/publics/Scheduled.php @@ -332,10 +332,13 @@ namespace controllers\publics; { foreach ($files_arrays as $file) { - $new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file); - if (!$new_media_id) + try { - \FlashMessage\FlashMessage::push('danger', 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name']); + $new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file); + } + catch (\Exception $e) + { + \FlashMessage\FlashMessage::push('danger', 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name'] . ':' . $e->getMessage()); return $this->redirect(\descartes\Router::url('Scheduled', 'add')); } @@ -459,8 +462,11 @@ namespace controllers\publics; { foreach ($files_arrays as $file) { - $new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file); - if (!$new_media_id) + try + { + $new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file); + } + catch (\Exception $e) { continue 2; } diff --git a/daemons/Phone.php b/daemons/Phone.php index 69ef0f2..48b99e9 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -181,7 +181,7 @@ class Phone extends AbstractDaemon foreach ($response['smss'] as $sms) { $this->logger->info('Receive message : ' . json_encode($sms)); - $response = $internal_received->receive($this->phone['id_user'], $this->phone['id'], $sms['text'], $sms['origin']); + $response = $internal_received->receive($this->phone['id_user'], $this->phone['id'], $sms['text'], $sms['origin'], $sms['at'], \models\Received::STATUS_UNREAD, $sms['mms'] ?? false, $sms['medias'] ?? []); if ($response['error']) { diff --git a/templates/discussion/show.php b/templates/discussion/show.php index 2dbaa47..674474d 100644 --- a/templates/discussion/show.php +++ b/templates/discussion/show.php @@ -87,7 +87,7 @@ } else { - return '
Voir le fichier ' + (index + 1) + '
'; + return '
Voir le fichier ' + ((int)index + 1) + '
'; } }); var medias_html = '
' + medias.join('') + '
'; diff --git a/templates/received/list.php b/templates/received/list.php index 763a28e..bab2b53 100644 --- a/templates/received/list.php +++ b/templates/received/list.php @@ -110,7 +110,7 @@ jQuery(document).ready(function () if (row.mms == 1) { var medias = []; for (i = 0; i < row.medias.length; i++) { - medias.push('Fichier ' + (i + 1) + ''); + medias.push('Fichier ' + (i + 1) + ''); } html = data + '
' + medias.join(' - '); return html; diff --git a/templates/scheduled/edit.php b/templates/scheduled/edit.php index bfd788d..669d1a7 100644 --- a/templates/scheduled/edit.php +++ b/templates/scheduled/edit.php @@ -75,7 +75,7 @@ $media) { ?>

-
Voir le média Supprimer le média +
Voir le média Supprimer le média

diff --git a/templates/scheduled/list.php b/templates/scheduled/list.php index ecfd708..63576e2 100644 --- a/templates/scheduled/list.php +++ b/templates/scheduled/list.php @@ -93,7 +93,7 @@ jQuery(document).ready(function () if (row.mms == 1) { var medias = []; for (i = 0; i < row.medias.length; i++) { - medias.push('Fichier ' + (i + 1) + ''); + medias.push('Fichier ' + (i + 1) + ''); } html = data + '
' + medias.join(' - '); return html; diff --git a/templates/sended/list.php b/templates/sended/list.php index d8c6289..15e837c 100644 --- a/templates/sended/list.php +++ b/templates/sended/list.php @@ -100,7 +100,7 @@ jQuery(document).ready(function () if (row.mms == 1) { var medias = []; for (i = 0; i < row.medias.length; i++) { - medias.push('Fichier ' + (i + 1) + ''); + medias.push('Fichier ' + (i + 1) + ''); } html = data + '
' + medias.join(' - '); return html;