summaryrefslogtreecommitdiff
path: root/src/controllers/Auth.class.php
blob: 86bd83fce9a176603a3c24093eed4ab2a997c0c4 (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
<?php

Router::register('auth', 'Auth');

class Auth extends Controller {
	public function index($routed, $remainder) {
		// So if $_POST['action'] isn't set, it will trip on '', which
		// is great, so we don't have to handle GET and PUT separately.
		@$action = $_POST['action'];
		switch ($action) {
		case 'login' : $this->login(); break;
		case 'logout': $this->logout(); break;
		case ''      : $this->maybe_login(); break;
		default      : $this->badrequest(); break;
		}
	}
	private function login() {
		$username = '';
		$password = '';
		
		$login = -1;
		if ( isset($_POST['username']) && isset($_POST['password'])) {
			$username = $_POST['username'];
			$password = $_POST['password'];
			$login = $mm->login($username, $password);
		}
		
		$vars = array();
		$vars['login_code'] = $login;
		$vars['username'] = $username;
		$vars['password'] = $password;
		if (isset($_POST['url'])) {
			$vars['url'] = $_POST['url'];
		}
		
		$this->showView('auth/login', $vars);
	}
	private function logout() {
		global $mm;
		$mm->logout();
		$this->showView('auth/logout');
	}
	private function maybe_login() {
		global $mm;
		$uid = $mm->isLoggedIn();
		if ($uid===false) {
			$this->login();
		} else {
			$username = $mm->getUsername($uid);
			$this->showView('auth/index',
			                array('username'=>$username));
		}
	}
	private function badrequest() {
		$this->showView('auth/badrequest');
	}
}