1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,202 @@ |
1 |
+<?php |
|
2 |
+/** |
|
3 |
+ * Copyright (C) 2021 Double Bastion LLC |
|
4 |
+ * |
|
5 |
+ * This file is part of Roundpin, which is licensed under the |
|
6 |
+ * GNU Affero General Public License Version 3.0. The license terms |
|
7 |
+ * are detailed in the "LICENSE.txt" file located in the root directory. |
|
8 |
+ */ |
|
9 |
+ |
|
10 |
+session_start(); |
|
11 |
+ |
|
12 |
+if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access']) && isset($_POST['sipUser']) && $_POST['sipUser'] != '') { |
|
13 |
+ |
|
14 |
+ $sipUser = $_POST['sipUser']; |
|
15 |
+ |
|
16 |
+ try { |
|
17 |
+ |
|
18 |
+ $resmessage = ''; |
|
19 |
+ |
|
20 |
+ // Check for undefined error, multiple files, $_FILES corruption |
|
21 |
+ if (!isset($_FILES['uploadedFile']['error']) || is_array($_FILES['uploadedFile']['error'])) { |
|
22 |
+ throw new RuntimeException('Invalid parameters.'); |
|
23 |
+ } |
|
24 |
+ |
|
25 |
+ // Check $_FILES['upfile']['error'] value |
|
26 |
+ switch ($_FILES['uploadedFile']['error']) { |
|
27 |
+ case UPLOAD_ERR_OK: |
|
28 |
+ break; |
|
29 |
+ case UPLOAD_ERR_NO_FILE: |
|
30 |
+ throw new RuntimeException('No file sent.'); |
|
31 |
+ case UPLOAD_ERR_INI_SIZE: |
|
32 |
+ throw new RuntimeException('The file size limit was exceeded! Please choose a file with a smaller size!'); |
|
33 |
+ case UPLOAD_ERR_FORM_SIZE: |
|
34 |
+ throw new RuntimeException('The file size limit was exceeded! Please choose a file with a smaller size!'); |
|
35 |
+ default: |
|
36 |
+ throw new RuntimeException('Unknown error.'); |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ // Check file size (in bytes) |
|
40 |
+ if ($_FILES['uploadedFile']['size'] > 786432000) { |
|
41 |
+ throw new RuntimeException('Uploaded file size exceeds the limit of 750 MB.'); |
|
42 |
+ } |
|
43 |
+ |
|
44 |
+ // Check MIME Type |
|
45 |
+ $finfo = new finfo(FILEINFO_MIME_TYPE); |
|
46 |
+ if (false === $ext = array_search($finfo->file($_FILES['uploadedFile']['tmp_name']), |
|
47 |
+ array( |
|
48 |
+ 'aac' => 'audio/aac', |
|
49 |
+ 'abw' => 'application/x-abiword', |
|
50 |
+ 'arc' => 'application/x-freearc', |
|
51 |
+ 'avi' => 'video/x-msvideo', |
|
52 |
+ 'azw' => 'application/vnd.amazon.ebook', |
|
53 |
+ 'bin' => 'application/octet-stream', |
|
54 |
+ 'bmp' => 'image/bmp', |
|
55 |
+ 'bz' => 'application/x-bzip', |
|
56 |
+ 'bz2' => 'application/x-bzip2', |
|
57 |
+ 'cda' => 'application/x-cdf', |
|
58 |
+ 'csh' => 'application/x-csh', |
|
59 |
+ 'css' => 'text/css', |
|
60 |
+ 'csv' => 'text/csv', |
|
61 |
+ 'doc' => 'application/msword', |
|
62 |
+ 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
63 |
+ 'eot' => 'application/vnd.ms-fontobject', |
|
64 |
+ 'epub' => 'application/epub+zip', |
|
65 |
+ 'gz' => 'application/gzip', |
|
66 |
+ 'gif' => 'image/gif', |
|
67 |
+ 'htm' => 'text/html', |
|
68 |
+ 'html' => 'text/html', |
|
69 |
+ 'ico' => 'image/vnd.microsoft.icon', |
|
70 |
+ 'ics' => 'text/calendar', |
|
71 |
+ 'jar' => 'application/java-archive', |
|
72 |
+ 'jpeg' => 'image/jpeg', |
|
73 |
+ 'jpg' => 'image/jpeg', |
|
74 |
+ 'json' => 'application/json', |
|
75 |
+ 'jsonld' => 'application/ld+json', |
|
76 |
+ 'mid' => 'audio/midi', |
|
77 |
+ 'midi' => 'audio/midi', |
|
78 |
+ 'mp3' => 'audio/mpeg', |
|
79 |
+ 'mp4' => 'video/mp4', |
|
80 |
+ 'mpeg' => 'video/mpeg', |
|
81 |
+ 'mpkg' => 'application/vnd.apple.installer+xml', |
|
82 |
+ 'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
83 |
+ 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
84 |
+ 'odt' => 'application/vnd.oasis.opendocument.text', |
|
85 |
+ 'oga' => 'audio/ogg', |
|
86 |
+ 'ogv' => 'video/ogg', |
|
87 |
+ 'ogx' => 'application/ogg', |
|
88 |
+ 'opus' => 'audio/opus', |
|
89 |
+ 'otf' => 'font/otf', |
|
90 |
+ 'png' => 'image/png', |
|
91 |
+ 'pdf' => 'application/pdf', |
|
92 |
+ 'ppt' => 'application/vnd.ms-powerpoint', |
|
93 |
+ 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
94 |
+ 'rar' => 'application/vnd.rar', |
|
95 |
+ 'rtf' => 'application/rtf', |
|
96 |
+ 'sh' => 'application/x-sh', |
|
97 |
+ 'swf' => 'application/x-shockwave-flash', |
|
98 |
+ 'tar' => 'application/x-tar', |
|
99 |
+ 'tif' => 'image/tiff', |
|
100 |
+ 'tiff' => 'image/tiff', |
|
101 |
+ 'ts' => 'video/mp2t', |
|
102 |
+ 'ttf' => 'font/ttf', |
|
103 |
+ 'txt' => 'text/plain', |
|
104 |
+ 'vsd' => 'application/vnd.visio', |
|
105 |
+ 'wav' => 'audio/wav', |
|
106 |
+ 'weba' => 'audio/webm', |
|
107 |
+ 'webm' => 'video/webm', |
|
108 |
+ 'webp' => 'image/webp', |
|
109 |
+ 'woff' => 'font/woff', |
|
110 |
+ 'woff2' => 'font/woff2', |
|
111 |
+ 'xhtml' => 'application/xhtml+xml', |
|
112 |
+ 'xls' => 'application/vnd.ms-excel', |
|
113 |
+ 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
114 |
+ 'xml' => 'text/xml', |
|
115 |
+ 'xul' => 'application/vnd.mozilla.xul+xml', |
|
116 |
+ 'zip' => 'application/zip', |
|
117 |
+ '3gp' => 'video/3gpp', |
|
118 |
+ '3g2' => 'video/3gpp2', |
|
119 |
+ '7z' => 'application/x-7z-compressed', |
|
120 |
+ ), |
|
121 |
+ true |
|
122 |
+ )) { |
|
123 |
+ throw new RuntimeException('Invalid file format.'); |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ // Check if the uploaded file contains PHP or JavaScript code tags |
|
127 |
+ $upFileContent = file_get_contents($_FILES['uploadedFile']['tmp_name']); |
|
128 |
+ if ((strpos($upFileContent, "<?php") !== false) || (strpos($upFileContent, "<script") !== false)) { throw new RuntimeException('Invalid file type.'); } |
|
129 |
+ |
|
130 |
+ // Create the directories if they don't exist |
|
131 |
+ if (!is_dir('textchat')) { |
|
132 |
+ mkdir('textchat', 0700); |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ if (!is_dir('textchat/' . $sipUser)) { |
|
136 |
+ mkdir('textchat/' . $sipUser, 0700); |
|
137 |
+ } |
|
138 |
+ |
|
139 |
+ if (!is_dir('textchat/' . $sipUser . '/uploads')) { |
|
140 |
+ mkdir('textchat/' . $sipUser . '/uploads', 0700); |
|
141 |
+ } |
|
142 |
+ |
|
143 |
+ // Check if the name of the uploaded file is valid |
|
144 |
+ $nameWithoutExt = pathinfo($_FILES["uploadedFile"]["name"], PATHINFO_FILENAME); |
|
145 |
+ if (preg_match('/^[a-zA-Z0-9\-\_\.]+$/', $nameWithoutExt)) { |
|
146 |
+ |
|
147 |
+ // Check if a file with the same name already exists in the 'uploads' directory of that respective user |
|
148 |
+ if (!file_exists('textchat/' . $sipUser . '/uploads/' . basename($_FILES["uploadedFile"]["name"]))) { |
|
149 |
+ |
|
150 |
+ $upload_dir = 'textchat/' . $sipUser . '/uploads/'; |
|
151 |
+ $targetfile = $upload_dir . basename($_FILES["uploadedFile"]["name"]); |
|
152 |
+ |
|
153 |
+ // Scan the uploaded file with ClamAV if ClamAV is installed |
|
154 |
+ if (file_exists('/usr/bin/clamdscan')) { |
|
155 |
+ |
|
156 |
+ $clamdisrunning = exec("systemctl show -p ActiveState --value clamav-daemon"); |
|
157 |
+ |
|
158 |
+ if ($clamdisrunning == 'active') { |
|
159 |
+ |
|
160 |
+ $safePath = escapeshellarg($_FILES["uploadedFile"]['tmp_name']); |
|
161 |
+ $runclamdscan = 'clamdscan --fdpass --quiet ' . $safePath . ''; |
|
162 |
+ $resout = ''; |
|
163 |
+ $rescheck = 1; |
|
164 |
+ exec($runclamdscan, $resout, $rescheck); |
|
165 |
+ |
|
166 |
+ if ($rescheck == 0) { |
|
167 |
+ |
|
168 |
+ if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"], $targetfile)) { |
|
169 |
+ chmod($targetfile, 0600); |
|
170 |
+ } else { |
|
171 |
+ throw new RuntimeException('There was an error while uploading the file.'); |
|
172 |
+ } |
|
173 |
+ |
|
174 |
+ } else { unlink($safePath); throw new RuntimeException('The file contains a virus or other type of malware.'); } |
|
175 |
+ |
|
176 |
+ } else { throw new RuntimeException("The ClavAV antivirus is installed but clamav-daemon is not active and the uploaded file couldn't be scanned! Please check the status of clamav-daemon!"); } |
|
177 |
+ |
|
178 |
+ } else { |
|
179 |
+ |
|
180 |
+ if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"], $targetfile)) { |
|
181 |
+ chmod($targetfile, 0600); |
|
182 |
+ } else { throw new RuntimeException('There was an error while uploading the file.'); } |
|
183 |
+ } |
|
184 |
+ |
|
185 |
+ } else { throw new RuntimeException('A file with the same name, ' . basename($_FILES["uploadedFile"]["name"]) . ', has been already sent. Please send a different file or rename your file before sending it.'); } |
|
186 |
+ |
|
187 |
+ } else { throw new RuntimeException('The name of the uploaded file is not valid.'); } |
|
188 |
+ |
|
189 |
+ } catch (RuntimeException $e) { |
|
190 |
+ |
|
191 |
+ $resmessage = $e->getMessage(); |
|
192 |
+ } |
|
193 |
+ |
|
194 |
+ $respdata = ['error' => $resmessage]; |
|
195 |
+ |
|
196 |
+ echo json_encode($respdata); |
|
197 |
+ |
|
198 |
+} else { |
|
199 |
+ header("Location: roundpin-login.php"); |
|
200 |
+} |
|
201 |
+ |
|
202 |
+?> |