aboutsummaryrefslogtreecommitdiffstats
path: root/mod/settings.php
blob: 861be7946c089df4033eb9ec68a8634d83cfbfc5 (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php


function settings_init(&$a) {

	if((! x($_SESSION,'authenticated')) && (x($_SESSION,'uid'))) {
		$_SESSION['sysmsg'] .= "Permission denied." . EOL;
		$a->error = 404;
		return;
	}
	require_once("mod/profile.php");
	profile_load($a,$_SESSION['uid']);
}


function settings_post(&$a) {

	if((! x($_SESSION['authenticated'])) && (! (x($_SESSION,'uid')))) {
		$_SESSION['sysmsg'] .= "Permission denied." . EOL;
		return;
	}
	if(count($a->user) && x($a->user,'uid') && $a->user['uid'] != $_SESSION['uid']) {
		$_SESSION['sysmsg'] .= "Permission denied." . EOL;
		return;
	}
	if((x($_POST,'password')) || (x($_POST,'confirm'))) {

		$newpass = trim($_POST['password']);
		$confirm = trim($_POST['confirm']);

		$err = false;
		if($newpass != $confirm ) {
			$_SESSION['sysmsg'] .= "Passwords do not match. Password unchanged." . EOL;
			$err = true;
		}

		if((! x($newpass)) || (! x($confirm))) {
			$_SESSION['sysmsg'] .= "Empty passwords are not allowed. Password unchanged." . EOL;
			$err = true;
		}

		if(! $err) {
			$password = hash('whirlpool',$newpass);
			$r = q("UPDATE `user` SET `password` = '%s' WHERE `uid` = %d LIMIT 1",
				dbesc($password),
				intval($_SESSION['uid']));
			if($r)
				$_SESSION['sysmsg'] .= "Password changed." . EOL;
			else
				$_SESSION['sysmsg'] .= "Password update failed. Please try again." . EOL;
		}
	}

	$username = notags(trim($_POST['username']));
	$email = notags(trim($_POST['email']));
	if(x($_POST,'nick'))
		$nick = notags(trim($_POST['nick']));
	$timezone = notags(trim($_POST['timezone']));

	$username_changed = false;
	$email_changed = false;
	$nick_changed = false;
	$zone_changed = false;
	$err = '';

	if($username != $a->user['username']) {
		$username_changed = true;
        	if(strlen($username) > 40)
                	$err .= " Please use a shorter name.";
        	if(strlen($username) < 3)
                	$err .= " Name too short.";
	}
	if($email != $a->user['email']) {
		$email_changed = true;
        	if(!eregi('[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,6}',$email))
                	$err .= " Not valid email.";
        	$r = q("SELECT `uid` FROM `user`
                	WHERE `email` = '%s' LIMIT 1",
                	dbesc($email)
                	);
	        if($r !== NULL && count($r))
        	        $err .= " This email address is already registered." . EOL;
	}
	if((x($nick)) && ($nick != $a->user['nickname'])) {
		$nick_changed = true;
		if(! preg_match("/^[a-zA-Z][a-zA-Z0-9\-\_]*$/",$nick))
			$err .= " Nickname must start with a letter and contain only contain letters, numbers, dashes, and underscore.";
		$r = q("SELECT `uid` FROM `user`
                	WHERE `nickname` = '%s' LIMIT 1",
                	dbesc($nick)
                	);
	        if($r !== NULL && count($r))
        	        $err .= " Nickname is already registered. Try another." . EOL;
	}
	else
		$nick = $a->user['nickname'];

        if(strlen($err)) {
                $_SESSION['sysmsg'] .= $err . EOL;
                return;
        }
	if($timezone != $a->user['timezone']) {
		$zone_changed = true;
		if(strlen($timezone))
			date_default_timezone_set($timezone);
	}
	if($email_changed || $username_changed || $nick_changed || $zone_changed ) {
		$r = q("UPDATE `user` SET `username` = '%s', `email` = '%s', `nickname` = '%s', `timezone` = '%s'  WHERE `uid` = %d LIMIT 1",
			dbesc($username),
			dbesc($email),
			dbesc($nick),
			dbesc($timezone),
			intval($_SESSION['uid']));
		if($r)
			$_SESSION['sysmsg'] .= "Settings updated." . EOL;
	}
	if($email_changed && $a->config['register_policy'] == REGISTER_VERIFY) {

		// FIXME - set to un-verified, blocked and redirect to logout

	}
	if($nick_changed) {
		$r = q ("UPDATE `profile` SET `url` = '%s', `request` = '%s', `notify` = '%s', `poll` = '%s', `confirm` = '%s'
			WHERE `uid` = %d AND `self` = 1 LIMIT 1",
			dbesc( $a->get_baseurl() . '/profile/' . $nick ),
			dbesc( $a->get_baseurl() . '/dfrn_request/' . $nick ),
			dbesc( $a->get_baseurl() . '/dfrn_notify/' . $nick ),
			dbesc( $a->get_baseurl() . '/dfrn_poll/' . $nick ),
			dbesc( $a->get_baseurl() . '/dfrn_confirm/' . $nick ),
			intval($_SESSION['uid'])
		);
	}


	// Refresh the content display with new data

	$r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
		intval($_SESSION['uid']));
	if(count($r))
		$a->user = $r[0];
}
		

if(! function_exists('settings_content')) {
function settings_content(&$a) {

	if((! x($_SESSION['authenticated'])) && (! (x($_SESSION,'uid')))) {
		$_SESSION['sysmsg'] .= "Permission denied." . EOL;
		return;
	}


	$username = $a->user['username'];
	$email    = $a->user['email'];
	$nickname = $a->user['nickname'];
	$timezone = $a->user['timezone'];


	if(x($nickname))
		$nickname_block = file_get_contents("view/settings_nick_set.tpl");
	else
		$nickname_block = file_get_contents("view/settings_nick_unset.tpl");

	$nickname_block = replace_macros($nickname_block,array(
		'$nickname' => $nickname,
		'$baseurl' => $a->get_baseurl()));	

	$o = file_get_contents('view/settings.tpl');

	$o = replace_macros($o,array(
		'$baseurl' => $a->get_baseurl(),
		'$uid' => $_SESSION['uid'],
		'$username' => $username,
		'$email' => $email,
		'$nickname_block' => $nickname_block,
		'$timezone' => $timezone,
		'$zoneselect' => select_timezone($timezone)
		));

	return $o;

}}