/
action.php
executable file
·220 lines (192 loc) · 5.65 KB
/
action.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
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
214
215
216
217
218
219
220
<?php
/*
* A class that represents one controller action, which can process input and render output.
* Subclasses (defined in engine/action/) should override process_input() and render().
*
* Can be used like so:
* $action = new Action_Subclass($controller)
* $action->execute();
*
* Simple controller actions can just be defined as controller methods, without necessarily
* creating an Action subclass in a new file. But creating a Action subclass allows the logic
* to be more easily reused elsewhere (and prevents controller files from getting very large).
*/
abstract class Action
{
protected $controller;
static $view_types = array();
function __construct($controller)
{
$this->controller = $controller;
if (!$controller) { throw new Exception("controller is null"); }
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->controller, $name), $arguments);
}
function execute()
{
$this->before();
$this->allow_view_types();
$request_method = @$_SERVER['REQUEST_METHOD'];
$fn = "do_{$request_method}";
if (method_exists($this, $fn))
{
$this->$fn();
}
else
{
$this->handle_invalid_request_method();
}
$this->after();
}
function handle_invalid_request_method()
{
$this->set_status(405);
$this->set_content_type('text/plain');
$this->set_content("Invalid request method");
}
function do_POST()
{
try
{
$this->validate_security_token();
$this->process_input();
}
catch (ValidationException $ex)
{
$this->handle_validation_exception($ex);
}
$this->record_user_action();
}
function do_GET()
{
$this->render();
}
function do_HEAD()
{
$this->render();
$this->set_header('Content-Length', strlen($this->get_response()->content));
$this->set_content('');
}
protected function handle_validation_exception($ex)
{
$response = $this->get_response();
if (@$response->headers['Content-Type'] == 'text/javascript')
{
$this->set_status(500);
$this->render_error_js($ex);
}
else
{
if ($ex->is_html())
{
SessionMessages::add_error_html($ex->getMessage());
}
else
{
SessionMessages::add_error($ex->getMessage());
}
$this->render();
if (!$response->content)
{
throw new RedirectException();
}
}
}
protected function validate_security_token()
{
validate_security_token();
}
protected function record_user_action()
{
$user = Session::get_logged_in_user();
if ($user)
{
$user->last_action = timestamp();
$user->save();
}
}
/*
* Subclasses should override to process the input from a POST request.
* This function may forward the user to another page, or call render() on errors.
*/
function process_input()
{
}
/*
* Subclasses should override to render the page for a GET request (or POST request with errors)
*/
function render()
{
}
function before()
{
}
function after()
{
}
function allow_view_types()
{
$this->controller->allow_view_types(static::$view_types);
}
function render_captcha($vars = null)
{
Session::start(); // make sure that securitytoken is correct
if (method_exists($this->controller, 'use_public_layout'))
{
$this->controller->use_public_layout();
}
$this->page_draw(array(
'title' => __('captcha:title'),
'content' => view("captcha/captcha_form", $vars),
));
}
function needs_captcha()
{
$user = Session::get_logged_in_user();
// show captcha if not logged in
if (!$user)
return true;
// show captcha if not approved and using someone else's site.
if (!$user->is_approved())
{
$controller = $this->controller;
if (method_exists($controller, 'get_user'))
{
$site_user = $controller->get_user();
if ($site_user && $user->guid != $site_user->guid)
return true;
}
}
return false;
}
function check_captcha()
{
if (!$this->needs_captcha())
{
return true;
}
// after entering a correct captcha, we avoid prompting the user for a captcha again
// the next few times during the same session
$free_captchas = Session::get('free_captchas');
if ($free_captchas > 0)
{
Session::set('free_captchas', $free_captchas - 1);
return true;
}
if (get_input('captcha'))
{
if (Captcha::get_instance()->check_answer($_POST['captcha_response']))
{
Session::set('free_captchas', 3);
return true;
}
else
{
SessionMessages::add_error(__('captcha:invalid'));
}
}
return false;
}
}