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,127 @@
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['uploadedRecFile']['error']) || is_array($_FILES['uploadedRecFile']['error'])) {
21
+        throw new RuntimeException('Invalid parameters.');
22
+    }
23
+
24
+    // Check $_FILES['upfile']['error'] value
25
+    switch ($_FILES['uploadedRecFile']['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['uploadedRecFile']['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
+    $detMime = $finfo->file($_FILES['uploadedRecFile']['tmp_name']);
46
+    if ($detMime != 'video/webm' && $detMime != 'audio/webm') { throw new RuntimeException('Invalid file format.'); }
47
+
48
+    // Check if the uploaded file contains PHP or JavaScript code tags
49
+    $upFileContent = file_get_contents($_FILES['uploadedRecFile']['tmp_name']);
50
+    if ((strpos($upFileContent, "<?php") !== false) || (strpos($upFileContent, "<script") !== false)) { throw new RuntimeException('Invalid file type.'); }
51
+
52
+    // Create the directories if they don't exist
53
+    if (!is_dir('../textchat/' . $sipUser)) {
54
+        mkdir('../textchat/' . $sipUser, 0700);
55
+    }
56
+
57
+    if (!is_dir('../textchat/' . $sipUser . '/uploads')) {
58
+        mkdir('../textchat/' . $sipUser . '/uploads', 0700);
59
+    }
60
+
61
+    // Check if the name of the uploaded file is valid
62
+    $nameWithoutExt = pathinfo($_FILES["uploadedRecFile"]["name"], PATHINFO_FILENAME);
63
+
64
+    if (preg_match('/^[a-zA-Z0-9\-\_\.]+$/', $nameWithoutExt)) {
65
+
66
+        // Check if the name of the uploaded file is larger than 50 bytes
67
+        if (strlen(basename($_FILES["uploadedRecFile"]["name"])) <= 50) {
68
+
69
+            // Check if a file with the same name already exists in the 'uploads' directory of that respective user
70
+            if (!file_exists('../textchat/' . $sipUser . '/uploads/' . basename($_FILES["uploadedRecFile"]["name"]))) {
71
+
72
+                $upload_dir = '../textchat/' . $sipUser . '/uploads/';
73
+                $targetfile = $upload_dir . basename($_FILES["uploadedRecFile"]["name"]);
74
+
75
+                // Scan the uploaded file with ClamAV if ClamAV is installed
76
+                if (file_exists('/usr/bin/clamdscan')) {
77
+
78
+                    $clamdisrunning = exec("systemctl show -p ActiveState --value clamav-daemon");
79
+
80
+                    if ($clamdisrunning == 'active') {
81
+
82
+		        $safePath = escapeshellarg($_FILES["uploadedRecFile"]['tmp_name']);
83
+                        $runclamdscan = 'clamdscan --fdpass --quiet ' . $safePath . '';
84
+		        $resout = '';
85
+		        $rescheck = 1;
86
+		        exec($runclamdscan, $resout, $rescheck);
87
+
88
+		        if ($rescheck == 0) {
89
+
90
+                            if (move_uploaded_file($_FILES["uploadedRecFile"]["tmp_name"], $targetfile)) {
91
+                                chmod($targetfile, 0600);
92
+                            } else {
93
+                                throw new RuntimeException('There was an error while uploading the file.');
94
+                            }
95
+
96
+                        } else { unlink($safePath); throw new RuntimeException('The file contains a virus or other type of malware.'); }
97
+
98
+                    } 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!"); }
99
+
100
+                } else {
101
+
102
+                        if (move_uploaded_file($_FILES["uploadedRecFile"]["tmp_name"], $targetfile)) {
103
+                            chmod($targetfile, 0600);
104
+
105
+                        } else { throw new RuntimeException('There was an error while uploading the file.'); }
106
+                }
107
+
108
+            } else { throw new RuntimeException('A file with the same name, ' . basename($_FILES["uploadedRecFile"]["name"]) . ', has been already sent. Please send a different file or rename your file before sending it.'); }
109
+
110
+        } 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.'); }
111
+
112
+    } else { throw new RuntimeException('The name of the uploaded file is not valid. File names should contain only alphanumeric characters, hyphens, underscores and dots.'); }
113
+
114
+  } catch (RuntimeException $e) {
115
+
116
+        $resmessage = $e->getMessage();
117
+  }
118
+
119
+  $respdata = ['error' => $resmessage];
120
+
121
+  echo json_encode($respdata);
122
+
123
+} else {
124
+       header("Location: ../login.php");
125
+}
126
+
127
+?>