Browse code

Created repository.

DoubleBastionAdmin authored on 29/11/2024 03:10:08
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,143 @@
1
+<?php
2
+
3
+/**
4
+ * @copyright 2024 Double Bastion LLC <www.doublebastion.com>
5
+ *
6
+ * @author Double Bastion LLC
7
+ *
8
+ * @license GNU AGPL version 3 or any later version
9
+ *
10
+ * This program is free software; you can redistribute it and/or
11
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
+ * License as published by the Free Software Foundation; either
13
+ * version 3 of the License, or any later version.
14
+ *
15
+ * This program is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
+ *
20
+ * You should have received a copy of the GNU Affero General Public
21
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
+ *
23
+ */
24
+
25
+session_start();
26
+
27
+if (isset($_POST['vd_ajax_call']) && ($_POST['vd_ajax_call'] == $_SESSION['validate_access'])) {
28
+
29
+define('ACCESSCONST', TRUE);
30
+
31
+require('db-connect.php');
32
+
33
+  if (isset($_POST['emailaddress']) && isset($_POST['login']) && isset($_POST['password']) && isset($_POST['selectrole']) && isset($_POST['currentmessage'])) {
34
+
35
+     $currentuseremail = $_POST['emailaddress'];
36
+     $currentusername = $_POST['login'];
37
+     $currentuserpswd = password_hash($_POST['password'], PASSWORD_DEFAULT);
38
+     $currentuserrole = $_POST['selectrole'];
39
+
40
+     $currentmessage = $_POST['currentmessage'];
41
+
42
+     if ($currentmessage == '' && $currentuseremail != '' && $currentusername != '' && $currentuserpswd != '' && $currentuserrole != '') {
43
+
44
+            // Check if there is any other user with the same username or email
45
+            $query0 = $mysqli->query("SELECT username, emailaddress FROM panelusers");
46
+
47
+            $duplicatename = 0;
48
+            $duplicateemail = 0;
49
+
50
+            while ($row = $query0->fetch_row()) {
51
+
52
+                   if ($currentusername == $row[0]) {
53
+                       $duplicatename = 1;
54
+                   }
55
+
56
+                   if ($currentuseremail == $row[1]) {
57
+                       $duplicateemail = 1;
58
+                   }
59
+            }
60
+
61
+            if ($duplicatename == 1 && $duplicateemail == 0) {
62
+                $result = 'failure';
63
+                $messageoninsert = "Your username is already in use. Please choose a different username !";
64
+            } elseif ($duplicatename == 0 && $duplicateemail == 1) {
65
+                $result = 'failure';
66
+                $messageoninsert = "Your email address is already in use. Please choose a different email address !";
67
+            } elseif ($duplicatename == 1 && $duplicateemail == 1) {
68
+                $result = 'failure';
69
+                $messageoninsert = "Your username and email address are already in use. Please choose a different username and email address !";
70
+            } else {
71
+
72
+                /**
73
+                 *  Send the verification email
74
+                 */
75
+
76
+                // Generate a random string to be used as the termination of the verification link
77
+                function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
78
+                         $pieces = [];
79
+                         $max = mb_strlen($keyspace, '8bit') - 1;
80
+
81
+                         for ($i = 0; $i < $length; ++$i) {
82
+                              $pieces []= $keyspace[random_int(0, $max)];
83
+                         }
84
+                         return implode('', $pieces);
85
+                }
86
+
87
+                $token = random_str(50);
88
+                $verificationLink = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . "/verification.php?key=" . $token;
89
+                $domaininit = explode(".", $_SERVER['HTTP_HOST']);
90
+                array_shift($domaininit);
91
+                $domain = implode(".", $domaininit);
92
+
93
+                // Mention the content-type, because it's an HTML email
94
+                $headers = "MIME-Version: 1.0" . "\r\n";
95
+                $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
96
+
97
+                $headers .= "From: " . "no-reply@" . $domain . "\r\n";
98
+
99
+                $subject = "RED SCARF Suite Panel email address verification";
100
+
101
+                $message = "Hello, <br><br>
102
+                            Thank you for signing up to RED SCARF Suite Panel. To complete the registration process, please click on the link below: <br><br>
103
+                            <a href='".$verificationLink."'>".$verificationLink."</a> <br><br>
104
+                            Alternatively, you can copy the link and paste it in the address bar of your browser.<br><br>
105
+                            Thank you,<br>
106
+                            RED SCARF Suite Panel<br>
107
+                            Host: '" . $_SERVER['HTTP_HOST'] . "'";
108
+
109
+                // Send the email
110
+                mail($currentuseremail, $subject, $message, $headers);
111
+
112
+                try {
113
+
114
+                   // Insert the email, username, password and user role in the 'panelusers' table
115
+                   $registered = '0';
116
+                   $temporary = '';
117
+                   $enabled = 1;
118
+                   $query1 = $mysqli->prepare("INSERT INTO panelusers (userrole, username, password, emailaddress, registered, token, temporary, enabled) VALUES (?, ?, ?, ?, ?,
119
+                                               ?, ?, ?)");
120
+                   $query1->bind_param("sssssssi", $currentuserrole, $currentusername, $currentuserpswd, $currentuseremail, $registered, $token, $temporary, $enabled);
121
+                   $query1->execute();
122
+                   $query1->close();
123
+
124
+                   $result = 'success';
125
+                   $messageoninsert = "A message has been sent to your email address ! Please follow the instructions in the received email to complete the registration process !";
126
+
127
+                } catch(mysqli_sql_exception $e) {
128
+                        $result = 'failure';
129
+                        $messageoninsert = "An error occurred while saving your data.";
130
+                  }
131
+            }
132
+
133
+            $response = array('result' => $result, 'messageoninsert' => $messageoninsert);
134
+            echo json_encode($response);
135
+     }
136
+  }
137
+
138
+} else {
139
+
140
+     header("Location: panel-login.php");
141
+}
142
+
143
+?>