summaryrefslogtreecommitdiff
path: root/web/account-edit.php
blob: 5702a23a6fed640edab3bfefb0fb87507c1a153a (plain)
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
<?
require_once("../include/Namespace.inc");
require_once("../include/Tools.inc");
require_once("../include/Template.inc");

$namespace = new Namespace(
	array( have_database => 1, 
	       have_session => 1,
	       have_user => 1 )
) or
	die("Nobody loves me. I don't even have space for a name.");

if (! $namespace->user->check_login()) {
	redirect($namespace, 'login.php?form_id=login&goto=account-edit.php');
	$namespace->stop();
	exit;
};

$rules = Array();
$rules['commit']['name'] = array(type => 'string', optional => 1);
$rules['commit']['password1'] = array(type => 'string', optional => 1);
$rules['commit']['password2'] = array(type => 'string', optional => 1);

$rules['addr_enable']['address_ref'] = array(type => 'integer');
$rules['addr_enable']['enable'] = array(type => 'string', length=>200, discard=>1);

$rules['addr_disable']['address_ref'] = array(type => 'integer');
$rules['addr_disable']['disable'] = array(type => 'string', length=>200, discard=>1);

$rules['addr_verify']['address_ref'] = array(type => 'integer');
$rules['addr_verify']['code'] = array(type => 'string', length=>200);
$rules['addr_verify']['verify'] = array(type => 'string', length=>200, discard=>1);

$rules['addr_delete']['address_ref'] = array(type => 'integer');
$rules['addr_delete']['delete'] = array(type => 'string', length=>200, discard=>1);

$rules['addr_new']['address'] = array(type => 'string', length=>512);
$rules['addr_new']['addr_new'] = array(type => 'string', length=>200, discard=>1);

$arguments = param_check($GLOBALS, $rules, array(params_required=>false));

$message = '';
$created = 0;

unset($selected_address);
if (isset($arguments['address_ref'])) {
	$selected_address = $namespace->database->query_row('SELECT address.ref, address.address FROM address WHERE address.account_ref=? AND address.ref=?', array($namespace->session->data['user']['ref'], $arguments['address_ref']));
	if (! $selected_address)
		error('Insufficient priviliges');
};

switch($arguments['form_id']) {
	case 'commit':
		$namespace->session->set_id();
		$name = $arguments['name'];
		$password1 = $arguments['password1'];
		$password2 = $arguments['password2'];

		if ($password1 != $password2) {
			$message .= 'Pasword mismatch.';
		} else {
			$account = array();
			$account['name'] = $name;
			if ($password1 != '') {
				$account['password'] = $password1;
			};

			$result = $namespace->database->update('account', $namespace->session->data['user']['ref'], $account);
			if ($result)
				$message = 'Update successfull.';
			else
				$message = 'Error (do not ask me what happened. I would be the last to know.)';
		}
		break;

	case 'addr_enable':
		$result = $namespace->database->update('address', $arguments['address_ref'], array(enabled => 'T'));
		if ($result) $message = 'Update successfull.';
			else     $message = 'Error (do not ask me what happened. I would be the last to know.)';
		break;

	case 'addr_disable':
		$result = $namespace->database->update('address', $arguments['address_ref'], array(enabled => 'F'));
		if ($result) $message = 'Update successfull.';
			else     $message = 'Error (do not ask me what happened. I would be the last to know.)';
		break;

	case 'addr_verify':
		assert(isset($selected_address));
		assert(isset($selected_address['address']));
		$address = $selected_address['address'];
		$code = md5($address . $namespace->config->secret);
		$code = substr($code, 0, 8);
		if ($code == $arguments['code']) {
			$update = array();
			$update['verified'] = 'T';
			$result = $namespace->database->update('address', $arguments['address_ref'], $update);
			if ($result) $message = 'Update successfull.';
				else     $message = 'Error (do not ask me what happened. I would be the last to know.)';
		} else
			$message = 'Mismatch - Address not enabled';
		break;

	case 'addr_delete':
		$result = $namespace->database->delete_row('address', $arguments['address_ref']);
		if ($result) $message = 'Update successfull.';
			else     $message = 'Error (do not ask me what happened. I would be the last to know.)';
		break;

	case 'addr_new':
		$address = $arguments['address'];
		if (! preg_match('/^[a-zA-Z0-9+=_.-]+@[a-zA-Z0-9+=_.-]+$/', $address) )
			error("Address '$address' does not match ^[a-zA-Z0-9+=_.-]+@[a-zA-Z0-9+=_.-]+$");

		$code = md5($address.$namespace->config->secret);
		$code = substr($code, 0, 8);

		$addr = array();
		$addr['address'] = $address;
		$addr['account_ref'] = $namespace->session->data['user']['ref'];
		$addr['verified'] = 'F';
		$addr['enabled'] = 'T';

		$username = $namespace->session->data['user']['username'];
		$result = $namespace->database->insert('address', $addr);
		if ($result) {
			$message = "Someone, probably you, has requested the following address  be added to the $username\r\n".
			"account on www.noreply.org:\r\n".
			"\r\n".
			"Address: $address\r\n".
			"\r\n".
			"Your Code to activate that address is �".$code."�\r\n".
			"\r\n".
			"You can log in at\r\n".
			"http://www.noreply.org/login.php?form_id=login&username=$username\n\r\n".
			"\r\n".
			"Should you have problems please contact ".$namespace->config->mail_operator.".\r\n";
			mail($address, "Your Noreply Account", $message,
				"From: ".$namespace->config->mail_from."\r\n".
				"Sender: ".$namespace->config->mail_sender."\r\n".
				"Reply-To: ".$namespace->config->mail_replyto
				);
			$message = 'Update successfull. An email with the authorization code has been sent.';
		} else
			$message = 'Error (perhaps the username already exists).';
		break;

	case '':
		break;

	default:
		error('unknown form '.$form_id.' in switch statement');
} 

$row = $namespace->database->query_row('SELECT ref, username, name FROM account WHERE ref=?', array($namespace->session->data['user']['ref']));

$data = array();
$data['message'] = $message;
$data['user'] = $namespace->session->data['user']['username'];
$data['username_field'] = $row['username'];
$data['name_field'] = $row['name'];
$data['addresses'] = $namespace->database->query_all('SELECT address.*, true as t, false as f FROM address WHERE account_ref=?', array($row['ref']));
$data['PHP_SELF'] = $PHP_SELF;

$template = new Template('account-edit.html', $namespace->config->template_path);
$template->parse($data);
print $template->output();

$namespace->stop();


# vim:set ts=4:
# vim:set shiftwidth=4:
?>