forked from jim/fitzgerald
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.php
87 lines (67 loc) · 2.64 KB
/
example.php
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
<?php
include('lib/fitzgerald.php');
class ApplicationWithLogin extends Fitzgerald {
// Redefine the constructor to setup the app
public function __construct($options=array()) {
session_set_cookie_params(3600);
parent::__construct($options);
}
// Basic get request
public function get_index() {
return $this->render('index');
}
// Login/logout
public function get_logout() {
$this->logout();
return $this->redirect('/login');
}
public function post_login() {
if ($this->login($this->request->username, $this->request->password)) {
return $this->redirect('/secret');
} else {
$this->error = 'Invalid username or password';
return $this->redirect('/login');
}
}
public function get_secret($page) {
$secretMessage = 'Psst!';
return $this->render($page, compact('secretMessage'));
}
// before filters
protected function verify_user() {
if (is_null($this->session->user) || @$this->isValidUser($this->session->user)) {
return $this->redirect('/login');
}
}
// Helper methods
private function isLoggedIn() {
if (!is_null($this->session->user) && $this->isValidUser($this->session->user)) {
return true;
} else {
$this->logout();
return false;
}
}
private function isValidUser($username) {
return $username == 'frank';
}
private function login($username, $password) {
return $username == 'frank' && $password == 'sinatra';
}
private function logout() {
$this->session->user = null;
session_destroy();
}
}
// Layout is the only option right now, but you can add your own via subclassing
$app = new ApplicationWithLogin(array('layout' => 'login'));
// Define a before filter to be executed before one or more actions
$app->before('get_secret|another_action', 'verify_user');
// Basic mappings specify which function is called for a matching URL
$app->get('/', 'get_index');
$app->post('/login', 'post_login');
// You can use placeholders in the URL that will be mapped to the specified function's arguments
// The optional third argument can be an array of regexs that the url must match for each placeholder
$app->get('/secret/:page', 'get_secret', array('page' => 'one|two|three'));
$app->run();
?>