1<?php
2require_once 'engine/init.php';
3protect_page();
4theme_open();
5require_once('config.countries.php');
6
7if (empty($_POST) === false) {
8 // $_POST['']
9 /* Token used for cross site scripting security */
10 if (!Token::isValid($_POST['token'])) {
11 $errors[] = t('login.token_invalid');
12 }
13 $required_fields = array('new_email', 'new_flag');
14 foreach($_POST as $key=>$value) {
15 if (empty($value) && in_array($key, $required_fields) === true) {
16 $errors[] = t('reg.fill_all');
17 break 1;
18 }
19 }
20
21 if (empty($errors) === true) {
22 if (filter_var($_POST['new_email'], FILTER_VALIDATE_EMAIL) === false) {
23 $errors[] = t('reg.email_invalid');
24 } else if (user_email_exist($_POST['new_email']) === true && $user_data['email'] !== $_POST['new_email']) {
25 $errors[] = t('reg.email_taken');
26 }
27 }
28}
29
30/**
31 * What the view has to render: 'success', 'errors' or 'form'.
32 * The account write stays here - a theme must never carry it.
33 */
34$formState = 'form';
35
36if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
37 $formState = 'success';
38
39} elseif (empty($_POST) === false && empty($errors) === true) {
40
41 $update_data = array(
42 'email' => $_POST['new_email']
43 );
44
45 $update_znote_data = array(
46 'flag' => getValue($_POST['new_flag'] ?? null),
47 'active_email' => '0'
48 );
49
50 // If the address was previously verified, take back the bonus points.
51 if ($user_znote_data['active_email'] > 0) {
52 $update_znote_data['points'] = $user_znote_data['points'] - $config['mailserver']['verify_email_points'];
53 }
54
55 user_update_account($update_data);
56 user_update_znote_account($update_znote_data);
57
58 header('Location: settings.php?success');
59 exit;
60
61} elseif (empty($errors) === false) {
62 $formState = 'errors';
63}
64
65view('settings');
66
67theme_close();
68