Browse code

Created repository.

DoubleBastionAdmin authored on 26/01/2022 20:32:42
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,104 @@
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
+if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {
13
+
14
+ define('ACCESSCONST', TRUE);
15
+
16
+ require('db-connect.php');
17
+
18
+    $username = $_POST['username'];
19
+    $currentEmail = $_POST['current_email'];
20
+    $newEmail = $_POST['new_email'];
21
+
22
+    // Get the email address of the current user from the 'app_users' table
23
+    $queryemail = $mysqli->query("SELECT id, username, emailaddress, enabled FROM app_users WHERE username = '$username' AND enabled = 1");
24
+    $emailfromdb = $queryemail->fetch_row();
25
+    $fetchedemailfromdb = $emailfromdb[2];
26
+
27
+    // Check if there is any other user with the same email address as the new email address
28
+    $getemails = $mysqli->query("SELECT emailaddress FROM app_users");
29
+
30
+    $duplicateemail = 0;
31
+
32
+    while ($row = $getemails->fetch_row()) {
33
+
34
+           if ($newEmail == $row[0]) {
35
+               $duplicateemail = 1;
36
+               break;
37
+           }
38
+    }
39
+
40
+
41
+    if ($fetchedemailfromdb == $currentEmail) {
42
+
43
+       if ($duplicateemail == 0) {
44
+
45
+           /**
46
+            *  Send the verification email
47
+            */
48
+
49
+           // Generate a random string to be used as the termination of the verification link
50
+           function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
51
+                    $pieces = [];
52
+                    $max = mb_strlen($keyspace, '8bit') - 1;
53
+
54
+                    for ($i = 0; $i < $length; ++$i) {
55
+                         $pieces []= $keyspace[random_int(0, $max)];
56
+                    }
57
+                    return implode('', $pieces);
58
+           }
59
+
60
+           $token = random_str(55);
61
+
62
+           // Enter the new token in the database
63
+           $entertokenquery = $mysqli->query("UPDATE app_users SET token = '$token' WHERE username = '$username' AND registered = 1 AND enabled = 1");
64
+
65
+           // Create the verification email
66
+           $verificationLink = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . "/change-email-verification.php?token=" . $token . "&newemail=" . $newEmail;
67
+
68
+           $domaininit = explode(".", $_SERVER['HTTP_HOST']);
69
+           array_shift($domaininit);
70
+           $domain = implode(".", $domaininit);
71
+
72
+           $headers = "MIME-Version: 1.0" . "\r\n";
73
+           $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
74
+
75
+           $headers .= "From: " . "no-reply@" . $domain . "\r\n";
76
+
77
+           $subject = "Roundpin email address verification";
78
+
79
+           $message = "Hello, <br><br>
80
+                       We have received your email address change request. To change your current Roundpin email address, please verify your address by clicking on
81
+                       the link from below: <br><br>
82
+                       <a href='".$verificationLink."'>".$verificationLink."</a><br><br>
83
+                       Alternatively, you can copy the link and paste it in the address bar of your browser.<br><br>
84
+                       After email address verification, your new email address will be assigned to your Roundpin account and saved to the database.<br><br>
85
+                       Thank you,<br>
86
+                       Roundpin<br>
87
+                       Host: '" . $_SERVER['HTTP_HOST'] . "'";
88
+
89
+           // Send the verification email
90
+           mail($newEmail, $subject, $message, $headers);
91
+
92
+           $emailchangemessage = "An email has been sent to your new email address. Please click on the link included in the received email to change your Roundpin user email address.";
93
+
94
+       } else { $emailchangemessage = "The new email address is already used by another Roundpin user. Please, choose a different email address!"; }
95
+        
96
+    } else { $emailchangemessage = "The email address you have entered in the 'Current Email' field doesn't match your current email address!"; }
97
+
98
+    echo json_encode($emailchangemessage);
99
+
100
+} else {
101
+        header("Location: roundpin-login.php");
102
+}
103
+
104
+?>
0 105
\ No newline at end of file