summaryrefslogtreecommitdiffstats
path: root/application/controllers/user.php
blob: 204612b2dfb46f864956df7efea9a79604bee915 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

class User extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('migration');
		if ( ! $this->migration->current()) {
			show_error($this->migration->error_string());
		}

		$this->load->model("muser");
		$this->data["title"] = "FileBin";
		
		$this->load->helper(array('form', 'filebin'));

		$this->var->view_dir = "user/";
		$this->data['username'] = $this->muser->get_username();
	}
	
	function index()
	{
		$this->data["username"] = $this->muser->get_username();

		$this->load->view($this->var->view_dir.'header', $this->data);
		$this->load->view($this->var->view_dir.'index', $this->data);
		$this->load->view($this->var->view_dir.'footer', $this->data);
	}
	
	function login()
	{
		$this->session->keep_flashdata("uri");

		if ($this->input->post('process')) {
			$username = $this->input->post('username');
			$password = $this->input->post('password');

			$result = $this->muser->login($username, $password);

			if ($result !== true) {
				$data['login_error'] = true;
				$this->load->view($this->var->view_dir.'header', $this->data);
				$this->load->view($this->var->view_dir.'login', $this->data);
				$this->load->view($this->var->view_dir.'footer', $this->data);
			} else {
				$uri = $this->session->flashdata("uri");
				if ($uri) {
					redirect($uri);
				} else {
					redirect("/");
				}
			}
		} else {
			$this->load->view($this->var->view_dir.'header', $this->data);
			$this->load->view($this->var->view_dir.'login', $this->data);
			$this->load->view($this->var->view_dir.'footer', $this->data);
		}
	}

	function create_invitation_key()
	{
		$this->muser->require_access();

		$userid = $this->muser->get_userid();

		// TODO: count both, invited users and key
		$query = $this->db->query("
			SELECT count(*) as count
			FROM invitations
			WHERE user = ?
			", array($userid))->row_array();

		if ($query["count"] + 1 > 3) {
			// TODO: better message
			echo "You've reached your invitation limit.";
			return;
		}

		$key = random_alphanum(12, 16);

		$this->db->query("
			INSERT INTO invitations
			(`key`, `user`, `date`)
			VALUES (?, ?, ?)
		", array($key, $userid, time()));

		redirect("user/invite");
	}

	function invite()
	{
		$this->muser->require_access();

		$userid = $this->muser->get_userid();

		$query = $this->db->query("
			SELECT *
			FROM invitations
			WHERE user = ?
			", array($userid))->result_array();

		$this->data["query"] = $query;

		$this->load->view($this->var->view_dir.'header', $this->data);
		$this->load->view($this->var->view_dir.'invite', $this->data);
		$this->load->view($this->var->view_dir.'footer', $this->data);
	}

	function register()
	{
		$key = $this->uri->segment(3);
		$process = $this->input->post("process");
		$values = array(
			"username" => "",
			"email" => ""
		);
		$error = array();

		$query = $this->db->query("
			SELECT `user`, `key`
			FROM invitations
			WHERE `key` = ?
			", array($key))->row_array();

		if (!isset($query["key"]) || $key != $query["key"]) {
			// TODO: better message
			echo "Unknown key.";
			return;
		}

		$referrer = $query["user"];

		if ($process) {
			$username = $this->input->post("username");
			$email = $this->input->post("email");
			$password = $this->input->post("password");
			$password_confirm = $this->input->post("password_confirm");

			if (!$username || strlen($username) > 32 || !preg_match("/^[a-z0-9]+$/", $username)) {
				$error[]= "Invalid username (only a-z0-9 are allowed).";
			}

			$this->load->helper("email");
			if (!valid_email($email)) {
				$error[]= "Invalid email.";
			}

			if (!$password || $password != $password_confirm) {
				$error[]= "No password or passwords don't match.";
			}

			if (empty($error)) {
				$this->db->query("
					INSERT INTO users
					(`username`, `password`, `email`, `referrer`)
					VALUES(?, ?, ?, ?)
					", array(
						$username,
						$this->muser->hash_password($password),
						$email,
						$referrer
					));
				$this->db->query("
					DELETE FROM invitations
					WHERE `key` = ?
					", array($key));
				$this->load->view($this->var->view_dir.'header', $this->data);
				$this->load->view($this->var->view_dir.'registered', $this->data);
				$this->load->view($this->var->view_dir.'footer', $this->data);
				return;
			} else {
				$values["username"] = $username;
				$values["email"] = $email;
			}
		}

		$this->data["key"] = $key;
		$this->data["values"] = $values;
		$this->data["error"] = $error;

		$this->load->view($this->var->view_dir.'header', $this->data);
		$this->load->view($this->var->view_dir.'register', $this->data);
		$this->load->view($this->var->view_dir.'footer', $this->data);
	}
	
	function logout()
	{
		$this->muser->logout();
		redirect('/');
	}
	
	function hash_password()
	{
		$process = $this->input->post("process");
		$password = $this->input->post("password");
		$password_confirm = $this->input->post("password_confirm");
		$this->data["hash"] = false;
		$this->data["password"] = $password;

		if ($process) {
			if (!$password || $password != $password_confirm) {
				$error[]= "No password or passwords don't match.";
			} else {
				$this->data["hash"] = $this->muser->hash_password($password);
			}
		}

		$this->load->view($this->var->view_dir.'header', $this->data);
		$this->load->view($this->var->view_dir.'hash_password', $this->data);
		$this->load->view($this->var->view_dir.'footer', $this->data);
	}
}