From 6acf28e3ceb9df8adb9527a4917da5990f1104e4 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Sat, 27 Mar 2021 01:15:09 +0100 Subject: [PATCH] Properly working mms reception with public dir in data and valid rights, etc. --- adapters/TestAdapter.php | 2 +- controllers/internals/Media.php | 26 ++++++++++++++++++++++---- controllers/internals/Tool.php | 15 +++++++++++---- controllers/publics/Discussion.php | 2 +- templates/discussion/show.php | 4 ++-- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/adapters/TestAdapter.php b/adapters/TestAdapter.php index b92d78c..07abfcf 100644 --- a/adapters/TestAdapter.php +++ b/adapters/TestAdapter.php @@ -186,7 +186,7 @@ namespace adapters; /** * Read from a files to simulate sms reception. - * In the file we expect a json string representing an array of sms of format : + * In the file we expect a series of lines, each line beeing a SMS as a json string of format : * { * "at" : "2021-03-26 11:21:48", * "medias" : [ diff --git a/controllers/internals/Media.php b/controllers/internals/Media.php index e2f5dd5..3056e57 100644 --- a/controllers/internals/Media.php +++ b/controllers/internals/Media.php @@ -29,7 +29,12 @@ class Media extends StandardController public function create(int $id_user, string $tmpfile_path, ?string $extension = null) { $user_path = \controllers\internals\Tool::create_user_public_path($id_user); - if (!file_exists($tmpfile_path) || !is_readable($tmpfile_path)) + if (!file_exists($tmpfile_path)) + { + throw new \Exception('File ' . $tmpfile_path . ' does not exists.'); + } + + if (!is_readable($tmpfile_path)) { throw new \Exception('File ' . $tmpfile_path . ' is not readable.'); } @@ -41,6 +46,11 @@ class Media extends StandardController $new_file_path = $user_path . '/' . $new_file_name; $new_file_relpath = $id_user . '/' . $new_file_name; + if (!file_put_contents($new_file_path, 'a')) + { + throw new \Exception('pute de merde'); + } + if (!rename($tmpfile_path, $new_file_path)) { throw new \Exception('Cannot create file ' . $new_file_path); @@ -90,10 +100,18 @@ class Media extends StandardController throw new \Exception($upload_result['content']); } - return $this->create($id_user, $upload_result['tmp_name'], $upload_result['extension']); + //Move uploaded file to a tmp file + if (!$tmp_file = tempnam('/tmp', 'raspisms-media-')) + { + throw new \Exception('Cannot create tmp file in /tmp to store the uploaded file.'); + } - $new_filepath = 'medias/' . $id_user . '/' . $upload_result['content']; - return $this->create($id_user, $new_filepath); + if (!move_uploaded_file($upload_result['tmp_name'], $tmp_file)) + { + throw new \Exception('Cannot move uploaded file to : ' . $tmp_file); + } + + return $this->create($id_user, $tmp_file, $upload_result['extension']); } /** diff --git a/controllers/internals/Tool.php b/controllers/internals/Tool.php index b1a795e..e5b71f9 100644 --- a/controllers/internals/Tool.php +++ b/controllers/internals/Tool.php @@ -306,19 +306,26 @@ namespace controllers\internals; return $new_dir; } - if (!mkdir($new_dir, fileperms(PWD_DATA_PUBLIC))) + clearstatcache(); + if (!mkdir($new_dir)) { throw new \Exception('Cannot create dir ' . $new_dir); } - if (!chown($new_dir, fileowner(PWD_DATA_PUBLIC))) + //We do chmod in two times because else umask fuck mkdir permissions + if (!chmod($new_dir, fileperms(PWD_DATA_PUBLIC) & 0777)) //Fileperms return garbage in addition to perms. Perms are only in weak bytes. We must use an octet notation with 0 { - throw new \Exception('Cannot give dir ' . $new_dir . ' to user : ' . fileowner(PWD_DATA)); + throw new \Exception('Cannot give dir ' . $new_dir . ' rights : ' . decoct(fileperms(PWD_DATA_PUBLIC) & 0777)); //Show error in dec + } + + if (posix_getuid() === 0 && !chown($new_dir, fileowner(PWD_DATA_PUBLIC))) //If we are root, try to give the file to a proper user + { + throw new \Exception('Cannot give dir ' . $new_dir . ' to user : ' . fileowner(PWD_DATA_PUBLIC)); } if (!chgrp($new_dir, filegroup(PWD_DATA_PUBLIC))) { - throw new \Exception('Cannot give dir ' . $new_dir . ' to group : ' . filegroup(PWD_DATA)); + throw new \Exception('Cannot give dir ' . $new_dir . ' to group : ' . filegroup(PWD_DATA_PUBLIC)); } return $new_dir; diff --git a/controllers/publics/Discussion.php b/controllers/publics/Discussion.php index b8fae9a..6f26b80 100644 --- a/controllers/publics/Discussion.php +++ b/controllers/publics/Discussion.php @@ -144,7 +144,7 @@ namespace controllers\publics; } $medias = []; - if ($sended['mms']) + if ($received['mms']) { $medias = $this->internal_media->gets_for_received($received['id']); foreach ($medias as &$media) diff --git a/templates/discussion/show.php b/templates/discussion/show.php index 674474d..72636fc 100644 --- a/templates/discussion/show.php +++ b/templates/discussion/show.php @@ -87,7 +87,7 @@ } else { - return '
Voir le fichier ' + ((int)index + 1) + '
'; + return '
Voir le fichier ' + (Number(index) + 1) + '
'; } }); var medias_html = '
' + medias.join('') + '
'; @@ -99,7 +99,7 @@ '
' + '
' + '
' + message.text + '
' + - medias.html + + medias_html + '
' + message.date + '
' + '
' + '
';