-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.php
executable file
·129 lines (107 loc) · 3.9 KB
/
Processor.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
<?php
/**
* Extroller_Processor
*
* @category Addon
* @package addon.extroller
* @author Ebine Yutaka <ebine.yutaka@sabel.jp>
* @copyright 2004-2008 Mori Reo <mori.reo@sabel.jp>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/
class Extroller_Processor extends Sabel_Bus_Processor
{
public function execute(Sabel_Bus $bus)
{
$response = $bus->get("response");
$status = $response->getStatus();
if ($status->isFailure()) return;
$controller = $bus->get("controller");
$controller->mixin("Extroller_Mixin");
$request = $bus->get("request");
$gets = $request->fetchGetValues();
$posts = $request->fetchPostValues();
$params = $request->fetchParameterValues();
$files = $request->getFiles();
$values = array_merge($gets, $posts, $params, $files);
if (count($values) !== (count($gets) + count($posts) + count($params) + count($files))) {
l("Extroller: request key overlaps.", SBL_LOG_DEBUG);
return $status->setCode(Sabel_Response::BAD_REQUEST);
}
if ($files) {
foreach ($files as $name => $file) {
$posts[$name] = $file;
}
}
$session = $bus->get("session");
if ($session && $flash_message = $session->read(Extroller_Mixin::FLASH_SESSION_KEY)) {
$controller->flash_message = $flash_message;
$session->delete(Extroller_Mixin::FLASH_SESSION_KEY);
}
$action = $bus->get("destination")->getAction();
if ($controller->hasMethod($action)) {
$reader = Sabel_Annotation_Reader::create();
$annots = $reader->readMethodAnnotation($controller, $action);
if (isset($annots["httpMethod"])) {
$allows = $annots["httpMethod"][0];
if (!$this->isMethodAllowed($request, $allows)) {
$response->setHeader("Allow", implode(",", array_map("strtoupper", $allows)));
return $status->setCode(Sabel_Response::METHOD_NOT_ALLOWED);
}
}
if (isset($annots["trim"]) && !realempty($annots["trim"][0])) {
$trimfunc = (extension_loaded("mbstring")) ? "mb_trim" : "trim";
foreach ($annots["trim"][0] as $rkey) {
if (isset($values[$rkey]) && is_string($values[$rkey])) {
$_value = $trimfunc($values[$rkey]);
$_value = (realempty($_value)) ? null : $_value;
$values[$rkey] = $_value;
if (isset($gets[$rkey])) {
$gets[$rkey] = $_value;
} elseif (isset($posts[$rkey])) {
$posts[$rkey] = $_value;
}
}
}
}
if (isset($annots["check"]) && ($request->isGet() || $request->isPost())) {
if (!$result = $this->validate($controller, $values, $request, $annots["check"])) {
return $status->setCode(Sabel_Response::BAD_REQUEST);
}
}
}
foreach ($values as $name => $value) {
$controller->setAttribute($name, $value);
}
$controller->setAttribute("REQUEST_VARS", $values);
$controller->setAttribute("GET_VARS", $gets);
$controller->setAttribute("POST_VARS", $posts);
}
protected function isMethodAllowed($request, $allows)
{
$result = true;
foreach ($allows as $method) {
if (!($result = $request->{"is" . $method}())) break;
}
return $result;
}
protected function validate($controller, $values, $request, $checks)
{
$validator = new Validator();
foreach ($checks as $check) {
$name = array_shift($check);
foreach ($check as $method) {
$validator->add($name, $method);
}
}
$controller->setAttribute("validator", $validator);
$result = true;
if (!$validator->validate($values)) {
if ($request->isPost()) {
$controller->setAttribute("errors", $validator->getErrors());
} else {
$result = false;
}
}
return $result;
}
}