Browse code

Changed majority of files.

DoubleBastionAdmin authored on 30/11/2024 06:56:40
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,204 @@
1
+<?php
2
+/**
3
+ *  Copyright (C) 2022, 2024  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
+  $resmessage = '';
16
+
17
+  try {
18
+
19
+    // Check for undefined error, multiple files, $_FILES corruption
20
+    if (!isset($_FILES['uploadedFile']['error']) || is_array($_FILES['uploadedFile']['error'])) {
21
+        throw new RuntimeException('Invalid parameters.');
22
+    }
23
+
24
+    // Check $_FILES['upfile']['error'] value
25
+    switch ($_FILES['uploadedFile']['error']) {
26
+        case UPLOAD_ERR_OK:
27
+             break;
28
+        case UPLOAD_ERR_NO_FILE:
29
+             throw new RuntimeException('No file sent.');
30
+        case UPLOAD_ERR_INI_SIZE:
31
+             throw new RuntimeException('The file size limit was exceeded! Please choose a file with a smaller size!');
32
+        case UPLOAD_ERR_FORM_SIZE:
33
+             throw new RuntimeException('The file size limit was exceeded! Please choose a file with a smaller size!');
34
+        default:
35
+             throw new RuntimeException('Unknown error.');
36
+    }
37
+
38
+    // Check file size (in bytes)
39
+    if ($_FILES['uploadedFile']['size'] > 786432000) {
40
+        throw new RuntimeException('Uploaded file size exceeds the limit of 750 MB.');
41
+    }
42
+
43
+    // Check MIME Type
44
+    $finfo = new finfo(FILEINFO_MIME_TYPE);
45
+    if (false === $ext = array_search($finfo->file($_FILES['uploadedFile']['tmp_name']),
46
+        array(
47
+	'aac' => 'audio/aac',
48
+	'abw' => 'application/x-abiword',
49
+	'arc' => 'application/x-freearc',
50
+	'avi' => 'video/x-msvideo',
51
+	'azw' => 'application/vnd.amazon.ebook',
52
+	'bin' => 'application/octet-stream',
53
+        'bmp' => 'image/x-ms-bmp',
54
+	'bz' => 'application/x-bzip',
55
+	'bz2' => 'application/x-bzip2',
56
+	'cda' => 'application/x-cdf',
57
+	'csh' => 'application/x-csh',
58
+	'css' => 'text/css',
59
+	'csv' => 'text/csv',
60
+	'doc' => 'application/msword',
61
+	'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
62
+	'eot' => 'application/vnd.ms-fontobject',
63
+	'epub' => 'application/epub+zip',
64
+	'gz' => 'application/gzip',
65
+	'gif' => 'image/gif',
66
+	'htm' => 'text/html',
67
+	'html' => 'text/html',
68
+	'ico' => 'image/vnd.microsoft.icon',
69
+	'ics' => 'text/calendar',
70
+	'jar' => 'application/java-archive',
71
+	'jpeg' => 'image/jpeg',
72
+	'jpg' => 'image/jpeg',
73
+	'json' => 'application/json',
74
+	'jsonld' => 'application/ld+json',
75
+	'mid' => 'audio/midi',
76
+	'midi' => 'audio/midi',
77
+	'mp3' => 'audio/mpeg',
78
+	'mp4' => 'video/mp4',
79
+	'mpeg' => 'video/mpeg',
80
+	'mpkg' => 'application/vnd.apple.installer+xml',
81
+	'odp' => 'application/vnd.oasis.opendocument.presentation',
82
+	'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
83
+	'odt' => 'application/vnd.oasis.opendocument.text',
84
+	'ogg' => 'audio/ogg',
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/' . $sipUser)) {
132
+        mkdir('../textchat/' . $sipUser, 0700);
133
+    }
134
+
135
+    if (!is_dir('../textchat/' . $sipUser . '/uploads')) {
136
+        mkdir('../textchat/' . $sipUser . '/uploads', 0700);
137
+    }
138
+
139
+    // Check if the name of the uploaded file is valid
140
+    $nameWithoutExt = pathinfo($_FILES["uploadedFile"]["name"], PATHINFO_FILENAME);
141
+
142
+    if (preg_match('/^[a-zA-Z0-9\-\_\.]+$/', $nameWithoutExt)) {
143
+
144
+        // Check if the name of the uploaded file is larger than 50 bytes
145
+        if (strlen(basename($_FILES["uploadedFile"]["name"])) <= 50) {
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 too long. File name length should not exceed 50 (Latin) characters. You can give a shorter name to your file and try again.'); }
188
+
189
+    } else { throw new RuntimeException('The name of the uploaded file is not valid. File names should contain only alphanumeric characters, hyphens, underscores and dots.'); }
190
+
191
+  } catch (RuntimeException $e) {
192
+
193
+        $resmessage = $e->getMessage();
194
+  }
195
+
196
+  $respdata = ['error' => $resmessage];
197
+
198
+  echo json_encode($respdata);
199
+
200
+} else {
201
+       header("Location: ../login.php");
202
+}
203
+
204
+?>