|
9 | 9 | throw new Exception('You do not have permission. `lhfile`, `upload_new_file` is required.'); |
10 | 10 | } |
11 | 11 |
|
| 12 | + $chatId = (isset($_POST['chat_id']) && is_numeric($_POST['chat_id'])) ? (int)$_POST['chat_id'] : 0; |
| 13 | + |
| 14 | + // A file can be uploaded only to a chat the caller is able to read |
| 15 | + if ($chatId > 0) { |
| 16 | + $chatToUpload = erLhcoreClassModelChat::fetch($chatId); |
| 17 | + |
| 18 | + if (!($chatToUpload instanceof erLhcoreClassModelChat) || !erLhcoreClassRestAPIHandler::hasAccessToRead($chatToUpload)) { |
| 19 | + throw new Exception('You do not have permission to upload a file to this chat.'); |
| 20 | + } |
| 21 | + } |
| 22 | + |
12 | 23 | $errors = array(); |
13 | 24 | erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.before_admin_uploadfile.file_store', array('errors' => & $errors)); |
14 | 25 |
|
|
22 | 33 | erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.new.file_path', array('path' => & $path)); |
23 | 34 |
|
24 | 35 | $upload_handler = new erLhcoreClassFileUploadAdmin(array( |
25 | | - 'chat_id' => (isset($_POST['chat_id']) && is_numeric($_POST['chat_id']) ? $_POST['chat_id'] : 0), 'remove_meta' => (isset($data['remove_meta']) ? $data['remove_meta'] : false), |
| 36 | + 'chat_id' => $chatId, 'remove_meta' => (isset($data['remove_meta']) ? $data['remove_meta'] : false), |
26 | 37 | 'user_id' => erLhcoreClassRestAPIHandler::getUserId(), 'persistent' => (isset($_POST['persistent']) && $_POST['persistent'] == 'true'), |
27 | 38 | 'file_name_replace' => (isset($_POST['name_replace']) ? $_POST['name_replace'] : ''), |
28 | 39 | 'file_name_manual' => (isset($_POST['name_prepend']) ? $_POST['name_prepend'] : ''), |
|
56 | 67 | throw new Exception('File could not be found!'); |
57 | 68 | } |
58 | 69 |
|
| 70 | + // A file can be edited only from a chat the caller is able to read |
| 71 | + if ($file->chat_id > 0) { |
| 72 | + $fileChat = erLhcoreClassModelChat::fetch($file->chat_id); |
| 73 | + |
| 74 | + if (!($fileChat instanceof erLhcoreClassModelChat) || !erLhcoreClassRestAPIHandler::hasAccessToRead($fileChat)) { |
| 75 | + throw new Exception('You do not have permission to edit this file.'); |
| 76 | + } |
| 77 | + } |
| 78 | + |
59 | 79 | $requestBody = json_decode(file_get_contents('php://input'),true); |
60 | 80 |
|
61 | 81 | $whiteList = ['upload_name','persistent', 'chat_id', 'user_id', 'online_user_id']; |
62 | 82 |
|
63 | 83 | foreach ($requestBody as $attr => $attrValue) { |
64 | | - if (in_array($attr, $whiteList, true)) { |
65 | | - $file->{$attr} = $attrValue; |
66 | | - $file->updateThis(); |
| 84 | + if (!in_array($attr, $whiteList, true)) { |
| 85 | + continue; |
67 | 86 | } |
| 87 | + |
| 88 | + if ($attr === 'user_id' && (int)$attrValue !== erLhcoreClassRestAPIHandler::getUserId()) { |
| 89 | + throw new Exception('You do not have permission. A file can be assigned only to yourself.'); |
| 90 | + } |
| 91 | + |
| 92 | + if ($attr === 'chat_id') { |
| 93 | + if (!is_numeric($attrValue)) { |
| 94 | + throw new Exception('chat_id has to be a numeric value!'); |
| 95 | + } |
| 96 | + |
| 97 | + if ((int)$attrValue > 0) { |
| 98 | + $targetChat = erLhcoreClassModelChat::fetch((int)$attrValue); |
| 99 | + |
| 100 | + if (!($targetChat instanceof erLhcoreClassModelChat) || !erLhcoreClassRestAPIHandler::hasAccessToRead($targetChat)) { |
| 101 | + throw new Exception('You do not have permission to move the file to this chat.'); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + $file->{$attr} = $attrValue; |
| 107 | + $file->updateThis(); |
68 | 108 | } |
69 | 109 |
|
70 | 110 | $state = $file->getState(); |
|
0 commit comments