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,203 @@
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['tcextensionpm']) && $_POST['tcextensionpm'] != '') {
13
+
14
+  $tcExtensionpm = $_POST['tcextensionpm'];
15
+  $resmessage = '';
16
+
17
+  try {
18
+
19
+    // Check for undefined error, multiple files, $_FILES corruption
20
+    if (!isset($_FILES['uploadedFileTCPM']['error']) || is_array($_FILES['uploadedFileTCPM']['error'])) {
21
+        throw new RuntimeException('Invalid parameters.');
22
+    }
23
+
24
+    // Check $_FILES['upfile']['error'] value
25
+    switch ($_FILES['uploadedFileTCPM']['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['uploadedFileTCPM']['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['uploadedFileTCPM']['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
+	'oga' => 'audio/ogg',
85
+	'ogv' => 'video/ogg',
86
+	'ogx' => 'application/ogg',
87
+	'opus' => 'audio/opus',
88
+	'otf' => 'font/otf',
89
+	'png' => 'image/png',
90
+	'pdf' => 'application/pdf',
91
+	'ppt' => 'application/vnd.ms-powerpoint',
92
+	'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
93
+	'rar' => 'application/vnd.rar',
94
+	'rtf' => 'application/rtf',
95
+	'sh' => 'application/x-sh',
96
+	'swf' => 'application/x-shockwave-flash',
97
+	'tar' => 'application/x-tar',
98
+	'tif' => 'image/tiff',
99
+	'tiff' => 'image/tiff',
100
+	'ts' => 'video/mp2t',
101
+	'ttf' => 'font/ttf',
102
+	'txt' => 'text/plain',
103
+	'vsd' => 'application/vnd.visio',
104
+	'wav' => 'audio/wav',
105
+	'weba' => 'audio/webm',
106
+	'webm' => 'video/webm',
107
+	'webp' => 'image/webp',
108
+	'woff' => 'font/woff',
109
+	'woff2' => 'font/woff2',
110
+	'xhtml' => 'application/xhtml+xml',
111
+	'xls' => 'application/vnd.ms-excel',
112
+	'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
113
+	'xml' => 'text/xml',
114
+	'xul' => 'application/vnd.mozilla.xul+xml',
115
+	'zip' => 'application/zip',
116
+	'3gp' => 'video/3gpp',
117
+	'3g2' => 'video/3gpp2',
118
+	'7z' => 'application/x-7z-compressed',
119
+        ),
120
+        true
121
+    )) {
122
+        throw new RuntimeException('Invalid file format.');
123
+    }
124
+
125
+    // Check if the uploaded file contains PHP or JavaScript code tags
126
+    $upFileContent = file_get_contents($_FILES['uploadedFileTCPM']['tmp_name']);
127
+    if ((strpos($upFileContent, "<?php") !== false) || (strpos($upFileContent, "<script") !== false)) { throw new RuntimeException('Invalid file type.'); }
128
+
129
+    // Create the directories if they don't exist
130
+    if (!is_dir('../textchat/' . $tcExtensionpm)) {
131
+        mkdir('../textchat/' . $tcExtensionpm, 0700);
132
+    }
133
+
134
+    if (!is_dir('../textchat/' . $tcExtensionpm . '/uploads')) {
135
+        mkdir('../textchat/' . $tcExtensionpm . '/uploads', 0700);
136
+    }
137
+
138
+    // Check if the name of the uploaded file is valid
139
+    $nameWithoutExt = pathinfo($_FILES["uploadedFileTCPM"]["name"], PATHINFO_FILENAME);
140
+
141
+    if (preg_match('/^[a-zA-Z0-9\-\_\.]+$/', $nameWithoutExt)) {
142
+
143
+        // Check if the name of the uploaded file is larger than 50 bytes
144
+        if (strlen(basename($_FILES["uploadedFileTCPM"]["name"])) <= 50) {
145
+
146
+            // Check if a file with the same name already exists in the 'uploads' directory of that respective user
147
+            if (!file_exists('../textchat/' . $tcExtensionpm . '/uploads/' . basename($_FILES["uploadedFileTCPM"]["name"]))) {
148
+
149
+                $upload_dir = '../textchat/' . $tcExtensionpm . '/uploads/';
150
+                $targetfile = $upload_dir . basename($_FILES["uploadedFileTCPM"]["name"]);
151
+
152
+                // Scan the uploaded file with ClamAV if ClamAV is installed
153
+                if (file_exists('/usr/bin/clamdscan')) {
154
+
155
+                    $clamdisrunning = exec("systemctl show -p ActiveState --value clamav-daemon");
156
+
157
+                    if ($clamdisrunning == 'active') {
158
+
159
+		        $safePath = escapeshellarg($_FILES["uploadedFileTCPM"]['tmp_name']);
160
+                        $runclamdscan = 'clamdscan --fdpass --quiet ' . $safePath . '';
161
+		        $resout = '';
162
+		        $rescheck = 1;
163
+		        exec($runclamdscan, $resout, $rescheck);
164
+
165
+		        if ($rescheck == 0) {
166
+
167
+                            if (move_uploaded_file($_FILES["uploadedFileTCPM"]["tmp_name"], $targetfile)) {
168
+                                chmod($targetfile, 0600);
169
+                            } else {
170
+                                throw new RuntimeException('There was an error while uploading the file.');
171
+                            }
172
+
173
+                        } else { unlink($safePath); throw new RuntimeException('The file contains a virus or other type of malware.'); }
174
+
175
+                    } 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!"); }
176
+
177
+                } else {
178
+
179
+                        if (move_uploaded_file($_FILES["uploadedFileTCPM"]["tmp_name"], $targetfile)) {
180
+                            chmod($targetfile, 0600);
181
+                        } else { throw new RuntimeException('There was an error while uploading the file.'); }
182
+                }
183
+
184
+            } else { throw new RuntimeException('A file with the same name, ' . basename($_FILES["uploadedFileTCPM"]["name"]) . ', has been already sent. Please send a different file or rename your file before sending it.'); }
185
+
186
+        } 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.'); }
187
+
188
+    } else { throw new RuntimeException('The name of the uploaded file is not valid. File names should contain only alphanumeric characters, hyphens, underscores and dots.'); }
189
+
190
+  } catch (RuntimeException $e) {
191
+
192
+           $resmessage = $e->getMessage();
193
+  }
194
+
195
+  $respdata = ['error' => $resmessage];
196
+
197
+  echo json_encode($respdata);
198
+
199
+} else {
200
+       header("Location: ../login.php");
201
+}
202
+
203
+?>