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,226 @@
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['sipusername']) && $_POST['sipusername'] != '' && isset($_POST['encextenpass']) && $_POST['encextenpass'] != '' && isset($_POST['vcextension']) && 
13
+    $_POST['vcextension'] != '') {
14
+
15
+  define('ACCESSCONST', TRUE);
16
+
17
+  require('db-connect.php');
18
+
19
+  $vconfExtension = $_POST['sipusername'];
20
+  $extenPassEnc = $_POST['encextenpass'];
21
+
22
+  $vcExtension = $_POST['vcextension'];
23
+  $resmessage = '';
24
+
25
+  // Check if the received external user extension, the corresponding encrypted password and the extension of the conference, match the data in the 'external_users' table
26
+  $query = $mysqli->prepare("SELECT id, exten_for_external, exten_for_ext_pass, conf_extension FROM external_users WHERE exten_for_external = ? AND exten_for_ext_pass = ? AND conf_extension = ?");
27
+  $query->bind_param("sss", $vconfExtension, $extenPassEnc, $vcExtension);
28
+  $query->execute();
29
+  $extqueryres = $query->get_result()->fetch_array();
30
+
31
+  if (!$extqueryres) {
32
+
33
+      http_response_code(400);
34
+      exit();
35
+
36
+  } else {
37
+
38
+	  try {
39
+
40
+	    // Check for undefined error, multiple files, $_FILES corruption
41
+	    if (!isset($_FILES['uploadedFileVC']['error']) || is_array($_FILES['uploadedFileVC']['error'])) {
42
+		throw new RuntimeException('Invalid parameters.');
43
+	    }
44
+
45
+	    // Check $_FILES['upfile']['error'] value
46
+	    switch ($_FILES['uploadedFileVC']['error']) {
47
+		case UPLOAD_ERR_OK:
48
+		     break;
49
+		case UPLOAD_ERR_NO_FILE:
50
+		     throw new RuntimeException('No file sent.');
51
+		case UPLOAD_ERR_INI_SIZE:
52
+		     throw new RuntimeException('The file size limit was exceeded! Please choose a file with a smaller size!');
53
+		case UPLOAD_ERR_FORM_SIZE:
54
+		     throw new RuntimeException('The file size limit was exceeded! Please choose a file with a smaller size!');
55
+		default:
56
+		     throw new RuntimeException('Unknown error.');
57
+	    }
58
+
59
+	    // Check file size (in bytes)
60
+	    if ($_FILES['uploadedFileVC']['size'] > 786432000) {
61
+		throw new RuntimeException('Uploaded file size exceeds the limit of 750 MB.');
62
+	    }
63
+
64
+	    // Check MIME Type
65
+	    $finfo = new finfo(FILEINFO_MIME_TYPE);
66
+	    if (false === $ext = array_search($finfo->file($_FILES['uploadedFileVC']['tmp_name']),
67
+		array(
68
+		'aac' => 'audio/aac',
69
+		'abw' => 'application/x-abiword',
70
+		'arc' => 'application/x-freearc',
71
+		'avi' => 'video/x-msvideo',
72
+		'azw' => 'application/vnd.amazon.ebook',
73
+		'bin' => 'application/octet-stream',
74
+                'bmp' => 'image/x-ms-bmp',
75
+		'bz' => 'application/x-bzip',
76
+		'bz2' => 'application/x-bzip2',
77
+		'cda' => 'application/x-cdf',
78
+		'csh' => 'application/x-csh',
79
+		'css' => 'text/css',
80
+		'csv' => 'text/csv',
81
+		'doc' => 'application/msword',
82
+		'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
83
+		'eot' => 'application/vnd.ms-fontobject',
84
+		'epub' => 'application/epub+zip',
85
+		'gz' => 'application/gzip',
86
+		'gif' => 'image/gif',
87
+		'htm' => 'text/html',
88
+		'html' => 'text/html',
89
+		'ico' => 'image/vnd.microsoft.icon',
90
+		'ics' => 'text/calendar',
91
+		'jar' => 'application/java-archive',
92
+		'jpeg' => 'image/jpeg',
93
+		'jpg' => 'image/jpeg',
94
+		'json' => 'application/json',
95
+		'jsonld' => 'application/ld+json',
96
+		'mid' => 'audio/midi',
97
+		'midi' => 'audio/midi',
98
+		'mp3' => 'audio/mpeg',
99
+		'mp4' => 'video/mp4',
100
+		'mpeg' => 'video/mpeg',
101
+		'mpkg' => 'application/vnd.apple.installer+xml',
102
+		'odp' => 'application/vnd.oasis.opendocument.presentation',
103
+		'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
104
+		'odt' => 'application/vnd.oasis.opendocument.text',
105
+		'oga' => 'audio/ogg',
106
+		'ogv' => 'video/ogg',
107
+		'ogx' => 'application/ogg',
108
+		'opus' => 'audio/opus',
109
+		'otf' => 'font/otf',
110
+		'png' => 'image/png',
111
+		'pdf' => 'application/pdf',
112
+		'ppt' => 'application/vnd.ms-powerpoint',
113
+		'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
114
+		'rar' => 'application/vnd.rar',
115
+		'rtf' => 'application/rtf',
116
+		'sh' => 'application/x-sh',
117
+		'swf' => 'application/x-shockwave-flash',
118
+		'tar' => 'application/x-tar',
119
+		'tif' => 'image/tiff',
120
+		'tiff' => 'image/tiff',
121
+		'ts' => 'video/mp2t',
122
+		'ttf' => 'font/ttf',
123
+		'txt' => 'text/plain',
124
+		'vsd' => 'application/vnd.visio',
125
+		'wav' => 'audio/wav',
126
+		'weba' => 'audio/webm',
127
+		'webm' => 'video/webm',
128
+		'webp' => 'image/webp',
129
+		'woff' => 'font/woff',
130
+		'woff2' => 'font/woff2',
131
+		'xhtml' => 'application/xhtml+xml',
132
+		'xls' => 'application/vnd.ms-excel',
133
+		'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
134
+		'xml' => 'text/xml',
135
+		'xul' => 'application/vnd.mozilla.xul+xml',
136
+		'zip' => 'application/zip',
137
+		'3gp' => 'video/3gpp',
138
+		'3g2' => 'video/3gpp2',
139
+		'7z' => 'application/x-7z-compressed',
140
+		),
141
+		true
142
+	    )) {
143
+		throw new RuntimeException('Invalid file format.');
144
+	    }
145
+
146
+	    // Check if the uploaded file contains PHP or JavaScript code tags
147
+	    $upFileContent = file_get_contents($_FILES['uploadedFileVC']['tmp_name']);
148
+	    if ((strpos($upFileContent, "<?php") !== false) || (strpos($upFileContent, "<script") !== false)) { throw new RuntimeException('Invalid file type.'); }
149
+
150
+	    // Create the directories if they don't exist
151
+	    if (!is_dir('../textchat/' . $vcExtension)) {
152
+		mkdir('../textchat/' . $vcExtension, 0700);
153
+	    }
154
+
155
+	    if (!is_dir('../textchat/' . $vcExtension . '/uploads')) {
156
+		mkdir('../textchat/' . $vcExtension . '/uploads', 0700);
157
+	    }
158
+
159
+	    // Check if the name of the uploaded file is valid
160
+	    $nameWithoutExt = pathinfo($_FILES["uploadedFileVC"]["name"], PATHINFO_FILENAME);
161
+
162
+	    if (preg_match('/^[a-zA-Z0-9\-\_\.]+$/', $nameWithoutExt)) {
163
+
164
+		// Check if the name of the uploaded file is larger than 50 bytes
165
+		if (strlen(basename($_FILES["uploadedFileVC"]["name"])) <= 50) {
166
+
167
+		    // Check if a file with the same name already exists in the 'uploads' directory of that respective user
168
+		    if (!file_exists('../textchat/' . $vcExtension . '/uploads/' . basename($_FILES["uploadedFileVC"]["name"]))) {
169
+
170
+		        $upload_dir = '../textchat/' . $vcExtension . '/uploads/';
171
+		        $targetfile = $upload_dir . basename($_FILES["uploadedFileVC"]["name"]);
172
+
173
+		        // Scan the uploaded file with ClamAV if ClamAV is installed
174
+		        if (file_exists('/usr/bin/clamdscan')) {
175
+
176
+		            $clamdisrunning = exec("systemctl show -p ActiveState --value clamav-daemon");
177
+
178
+		            if ($clamdisrunning == 'active') {
179
+
180
+				$safePath = escapeshellarg($_FILES["uploadedFileVC"]['tmp_name']);
181
+		                $runclamdscan = 'clamdscan --fdpass --quiet ' . $safePath . '';
182
+				$resout = '';
183
+				$rescheck = 1;
184
+				exec($runclamdscan, $resout, $rescheck);
185
+
186
+				if ($rescheck == 0) {
187
+
188
+		                    if (move_uploaded_file($_FILES["uploadedFileVC"]["tmp_name"], $targetfile)) {
189
+		                        chmod($targetfile, 0600);
190
+		                    } else {
191
+		                        throw new RuntimeException('There was an error while uploading the file.');
192
+		                    }
193
+
194
+		                } else { unlink($safePath); throw new RuntimeException('The file contains a virus or other type of malware.'); }
195
+
196
+		            } 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!"); }
197
+
198
+		        } else {
199
+
200
+		                if (move_uploaded_file($_FILES["uploadedFileVC"]["tmp_name"], $targetfile)) {
201
+		                    chmod($targetfile, 0600);
202
+		                } else { throw new RuntimeException('There was an error while uploading the file.'); }
203
+		        }
204
+
205
+		    } else { throw new RuntimeException('A file with the same name, ' . basename($_FILES["uploadedFileVC"]["name"]) . ', has been already sent. Please send a different file or rename your file before sending it.'); }
206
+
207
+		} 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.'); }
208
+
209
+	    } else { throw new RuntimeException('The name of the uploaded file is not valid. File names should contain only alphanumeric characters, hyphens, underscores and dots.'); }
210
+
211
+	  } catch (RuntimeException $e) {
212
+
213
+		  $resmessage = $e->getMessage();
214
+	  }
215
+
216
+  }
217
+
218
+  $respdata = ['error' => $resmessage];
219
+
220
+  echo json_encode($respdata);
221
+
222
+} else {
223
+       header("Location: ../login.php");
224
+}
225
+
226
+?>