-
Notifications
You must be signed in to change notification settings - Fork 33
/
newemail.php
137 lines (112 loc) · 5.61 KB
/
newemail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
use src\Models\ApplicationContainer;
use src\Models\OcConfig\OcConfig;
use src\Utils\Database\XDb;
use src\Utils\Text\Validator;
global $absolute_server_URI;
require_once(__DIR__ . '/lib/common.inc.php');
$user = ApplicationContainer::GetAuthorizedUser();
if (!$user) {
$target = urlencode(tpl_get_current_page());
tpl_redirect('login.php?target=' . $target);
}
//set here the template to process
$tplname = 'newemail';
//load language specific variables
require_once(__DIR__ . '/src/Views/' . $tplname . '.inc.php');
tpl_set_var('new_email', '');
tpl_set_var('message', '');
tpl_set_var('email_message', '');
tpl_set_var('code_message', '');
tpl_set_var('change_email', $change_email);
tpl_set_var('reset', tr('reset'));
tpl_set_var('getcode', $get_code);
if (isset($_POST['submit_getcode']) || isset($_POST['submit_changeemail'])) {
$new_email = $_POST['newemail'];
tpl_set_var('new_email', htmlspecialchars($new_email, ENT_COMPAT, 'UTF-8'));
//validate the email
$email_exists = false;
$new_email_not_ok = false;
if (!Validator::isValidEmail($new_email)) {
$new_email_not_ok = true;
tpl_set_var('email_message', $error_email_not_ok);
} else {
//prüfen, ob email schon in der Datenbank vorhanden
$rs = XDb::xSql(
"SELECT `username` FROM `user` WHERE `email`= ? ", $new_email);
if (false !== XDb::xFetchArray($rs)) {
$email_exists = true;
tpl_set_var('email_message', $error_email_exists);
}
}
if ((!$email_exists) && (!$new_email_not_ok)) {
if (isset($_POST['submit_getcode'])) {
//send the secure code via email and store the new email in the database
$secure_code = uniqid('');
//code in DB eintragen
XDb::xSql(
"UPDATE `user` SET `new_email_date`=?, `new_email_code`=?, `new_email`=?
WHERE `user_id`=?", time(), $secure_code, $new_email, $user->getUserId());
$email_content = file_get_contents(__DIR__ . '/resources/email/newemail.email');
$email_content = mb_ereg_replace('{server}', $absolute_server_URI, $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_01}', tr('newEmailAddr_01'), $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_02}', tr('newEmailAddr_02'), $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_03}', tr('newEmailAddr_03'), $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_04}', tr('newEmailAddr_04'), $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_05}', tr('newEmailAddr_05'), $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_06}', tr('newEmailAddr_06'), $email_content);
$email_content = mb_ereg_replace('{newEmailAddr_07}', tr('newEmailAddr_07'), $email_content);
$email_content = mb_ereg_replace('{user}', $user->getUserName(), $email_content);
$email_content = mb_ereg_replace('{date}', strftime(
$GLOBALS['config']['datetimeformat']), $email_content);
$email_content = mb_ereg_replace('{code}', $secure_code, $email_content);
$email_content = mb_ereg_replace('{octeamEmailsSignature}', OcConfig::getOcteamEmailsSignature(), $email_content);
$emailheaders = "Content-Type: text/plain; charset=utf-8\r\n";
$emailheaders .= "Content-Transfer-Encoding: 8bit\r\n";
$emailheaders .= 'From: "' . OcConfig::getEmailAddrNoReply() . '" <' . OcConfig::getEmailAddrNoReply() . '>';
mb_send_mail($new_email, $email_subject, $email_content, $emailheaders);
tpl_set_var('message', $email_send);
} else if (isset($_POST['submit_changeemail'])) {
$secure_code = $_POST['code'];
$rs = XDb::xSql(
"SELECT `new_email_code`, `new_email`, `new_email_date` FROM `user` WHERE `user_id`=? ",
$user->getUserId());
$record = XDb::xFetchArray($rs);
if ($record !== false) {
$new_email = $record['new_email'];
$new_email_code = $record['new_email_code'];
$new_email_date = $record['new_email_date'];
} else {
$new_email = '';
}
if ($new_email == '') {
//no new email was entered
tpl_set_var('message', $error_no_new_email);
} else if ($new_email_code != $secure_code) {
//wrong code
tpl_set_var('code_message', $error_wrong_code);
} else if (time() - $new_email_date > 259200) {
//code timed out
tpl_set_var('code_message', $error_code_timed_out);
} else {
//check if email exists
$rs = XDb::xSql(
"SELECT `username` FROM `user` WHERE `email`= ? ", $new_email);
if (false !== XDb::xFetchArray($rs)) {
tpl_set_var('message', $error_email_exists);
} else {
//neue EMail eintragen
XDb::xSql(
"UPDATE `user` SET `new_email_date`=NULL, `new_email_code`=NULL,
`new_email`=NULL, `email`= ? , `last_modified`=NOW()
WHERE `user_id`= ? ",
$new_email, $user->getUserId());
//try to change the email
tpl_set_var('message', $email_changed);
}
}
}
}
}
//make the template and send it out
tpl_BuildTemplate();