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,173 @@
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
+ // header('Set-Cookie: PHPSESSID= ' . session_id() . '; SameSite=strict; Secure=true; HttpOnly=true;');
13
+
14
+if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {
15
+
16
+    define('ACCESSCONST', TRUE);
17
+
18
+    require('db-connect.php');
19
+
20
+    $username = $_POST['username'];
21
+    $extenForExternal = $_POST['exten_for_external'];
22
+    $extenForExternalPass = $_POST['exten_for_ext_pass'];
23
+    $confExtension = $_POST['conf_extension'];
24
+    $confTag = $_POST['conf_tag'];
25
+    $wssServer = $_POST['wss_server'];
26
+    $webSocketAndPath = $_POST['web_sock_path'];
27
+
28
+    /**
29
+     *  Encrypt the user and the extension used for external access
30
+     */
31
+
32
+    // Extract the secret from the configuration file
33
+    $configfilestring = file_get_contents(dirname(__FILE__) . '/roundpin-config.php');
34
+
35
+    if (preg_match_all('[include|include_once|require|require_once]', $configfilestring) != 0) {
36
+
37
+        if (strpos($configfilestring, "'") !== false) {
38
+            $continit = explode("'", $configfilestring);
39
+            $configfilepath = $continit[1];
40
+        } elseif (strpos($configfilestring, "\"") !== false) {
41
+            $continit = explode("\"", $configfilestring);
42
+            $configfilepath = $continit[1];
43
+          }
44
+
45
+        $configfilelines = file($configfilepath);
46
+
47
+        if (count($configfilelines) != 0) {
48
+
49
+           foreach ($configfilelines as $keyfile => $valuefile) {
50
+
51
+              if (strpos($valuefile, "\$secret") !== false) {
52
+                  $secret_init = explode("'", $valuefile);
53
+                  $secretfin = $secret_init[1];
54
+              }
55
+           }
56
+        }
57
+
58
+    } else {
59
+
60
+        $configfilelines = file(dirname(__FILE__) . '/roundpin-config.php');
61
+
62
+        if (count($configfilelines) != 0) {
63
+           foreach ($configfilelines as $keyfile => $valuefile) {
64
+              if (strpos($valuefile, "\$secret") !== false) {
65
+                  $secretinit = explode("'", $valuefile);
66
+                  $secretfin = $secretinit[1];
67
+              }
68
+           }
69
+        }
70
+      }
71
+
72
+    // Encrypt the username, extension name, conference extension, conference label and WebSocket port and server path
73
+    $userExtenConfExt = $username . "|" . $extenForExternal . "|" . $confExtension . "|" . $confTag . "|" . $webSocketAndPath;
74
+    $ivsep = substr(sha1((string) mt_rand()), 0, 16);
75
+    $encryptpwdin = openssl_encrypt($userExtenConfExt, 'AES-256-CBC', $secretfin, false, $ivsep);
76
+    $finUserExtEncinit = $encryptpwdin.':'.$ivsep;
77
+    $finUserExtEnc = bin2hex($finUserExtEncinit);
78
+
79
+    // Build the external access link
80
+    $confAccessLinkEnc = "https://".$wssServer."/videoconference/external.php?param=".$finUserExtEnc;
81
+
82
+    // Get the id of the user for which we want to insert the external user data
83
+    $enabled = 1;
84
+    $query1 = $mysqli->prepare("SELECT id, userrole, username, enabled FROM app_users WHERE (userrole = 'admin' OR userrole = 'superadmin') AND BINARY username = ? AND enabled = ?");
85
+    $query1->bind_param("si", $username, $enabled);
86
+    $query1->execute();
87
+    $queryres = $query1->get_result()->fetch_assoc();
88
+    $userID = $queryres['id'];
89
+
90
+    // Check if the extension has already been introduced in the 'external_users' table
91
+    $query2 = $mysqli->prepare("SELECT id, userid, exten_for_external FROM external_users WHERE exten_for_external = ?");
92
+    $query2->bind_param("s", $extenForExternal);
93
+    $query2->execute();
94
+    $extqueryres = $query2->get_result()->fetch_assoc();
95
+    $linkauthorID = $extqueryres['userid'];
96
+    $extensionExists = $extqueryres['id'];
97
+
98
+
99
+    if ($extenForExternalPass != '' && $extenForExternalPass != "%20%20%20%20%20%20%20") {
100
+
101
+        // Encrypt the SIP password for the extension used for external access
102
+
103
+        $keypasssp = substr(sha1((string) mt_rand()), 0, 32);
104
+        $keysaltsp = openssl_random_pseudo_bytes(24);
105
+        $keyLengthsp = 80;
106
+        $iterationssp = 100;
107
+        $generated_keysp = openssl_pbkdf2($keypasssp, $keysaltsp, $keyLengthsp, $iterationssp, 'sha256');
108
+        $psswdaddedsp = bin2hex($generated_keysp);
109
+
110
+        if (!is_dir('../restr')) {
111
+            mkdir('../restr', 0700);
112
+        }
113
+
114
+        if (!is_dir('../restr/'.$username.'')) {
115
+            mkdir('../restr/'.$username.'', 0700);
116
+        }
117
+
118
+        if (!is_dir('../restr/'.$username.'/externalext')) {
119
+            mkdir('../restr/'.$username.'/externalext', 0700);
120
+        }
121
+
122
+        file_put_contents('../restr/'.$username.'/externalext/'.$extenForExternal, $psswdaddedsp);
123
+        chmod('../restr/'.$username.'/externalext/'.$extenForExternal, 0600);
124
+
125
+        $ivsp = substr(sha1((string) mt_rand()), 0, 16);
126
+        $encpwdinsp = openssl_encrypt($extenForExternalPass, 'AES-256-CBC', $psswdaddedsp, false, $ivsp);
127
+        $extenForExternalPassEnc = $encpwdinsp.':'.$ivsp;
128
+
129
+    } elseif ($extenForExternalPass == "%20%20%20%20%20%20%20") {
130
+
131
+              $queryselextpass = $mysqli->prepare("SELECT id, userid, exten_for_external, exten_for_ext_pass FROM external_users WHERE userid = ? AND exten_for_external = ?");
132
+              $queryselextpass->bind_param("is", $userID, $extenForExternal);
133
+              $queryselextpass->execute();
134
+              $extpassarr = $queryselextpass->get_result()->fetch_assoc();
135
+
136
+              $extenForExternalPassEnc = $extpassarr['exten_for_ext_pass'];
137
+
138
+    } elseif ($extenForExternalPass == '') {
139
+              $extenForExternalPassEnc = '';
140
+    } else { $extenForExternalPassEnc = ''; }
141
+
142
+    $date = date("Y-m-d H:i:s");
143
+
144
+    // Update or insert the data in the 'external_users' table
145
+    if ($extensionExists != '') {
146
+
147
+        if ($linkauthorID == $userID) {
148
+	    $updatequery = $mysqli->prepare("UPDATE external_users SET exten_for_ext_pass = ?, conf_extension = ?, conf_tag = ?, conf_access_link = ?, date_modified = ? WHERE userid = ?
149
+                                             AND exten_for_external = ?");
150
+	    $updatequery->bind_param("sssssis", $extenForExternalPassEnc, $confExtension, $confTag, $confAccessLinkEnc, $date, $userID, $extenForExternal);
151
+
152
+	    if ($updatequery->execute()) { $messagetosend = 'The data has been successfully saved to the database !'; } else { $messagetosend = 'Error while updating the data !'; }
153
+
154
+        } else { $messagetosend = 'A different user has already created a link for this extension. Please choose a different extension!'; }
155
+
156
+    } else {
157
+	    $insertquery = $mysqli->prepare("INSERT INTO external_users (userid, exten_for_external, exten_for_ext_pass, conf_extension, conf_tag, conf_access_link, date_added, 
158
+                                             date_modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
159
+	    $insertquery->bind_param("isssssss", $userID, $extenForExternal, $extenForExternalPassEnc, $confExtension, $confTag, $confAccessLinkEnc, $date, $date);
160
+
161
+	    if ($insertquery->execute()) { 
162
+                $messagetosend = 'The data has been successfully saved to the database !';
163
+            } else { $messagetosend = 'Error while inserting the data into the database !'; }
164
+      }
165
+
166
+    $response = array('result' => $messagetosend);
167
+    echo json_encode($response);
168
+
169
+} else {
170
+    header("Location: ../login.php");
171
+}
172
+
173
+?>