| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class Processor_Response extends Sabel_Bus_Processor |
|---|
| 13 | { |
|---|
| 14 | protected $afterEvents = array("action" => "afterAction"); |
|---|
| 15 | |
|---|
| 16 | public function execute($bus) |
|---|
| 17 | { |
|---|
| 18 | $bus->set("response", new Sabel_Response_Object()); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | public function afterAction($bus) |
|---|
| 22 | { |
|---|
| 23 | $response = $bus->get("response"); |
|---|
| 24 | $response->setResponses(array_merge( |
|---|
| 25 | $response->getResponses(), |
|---|
| 26 | $bus->get("controller")->getAttributes() |
|---|
| 27 | )); |
|---|
| 28 | |
|---|
| 29 | if ($response->getStatus()->isServerError()) { |
|---|
| 30 | $exception = Sabel_Context::getContext()->getException(); |
|---|
| 31 | if (!is_object($exception)) return; |
|---|
| 32 | |
|---|
| 33 | $eol = ((ENVIRONMENT & DEVELOPMENT) > 0) ? "<br />" : PHP_EOL; |
|---|
| 34 | $msg = get_class($exception) . ": " |
|---|
| 35 | . $exception->getMessage() . $eol |
|---|
| 36 | . "At: " . date("r") . $eol . $eol |
|---|
| 37 | . Sabel_Exception_Printer::printTrace($exception, $eol, true); |
|---|
| 38 | |
|---|
| 39 | if ((ENVIRONMENT & PRODUCTION) > 0) { |
|---|
| 40 | |
|---|
| 41 | } else { |
|---|
| 42 | $response->setResponse("exception_message", $msg); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | l(PHP_EOL . str_replace("<br />", PHP_EOL, $msg), SBL_LOG_ERR); |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | public function shutdown($bus) |
|---|
| 50 | { |
|---|
| 51 | $response = $bus->get("response"); |
|---|
| 52 | $redirector = $response->getRedirector(); |
|---|
| 53 | |
|---|
| 54 | if ($redirector->isRedirected()) { |
|---|
| 55 | if (($url = $redirector->getUrl()) !== "") { |
|---|
| 56 | $response->setLocation($url); |
|---|
| 57 | } else { |
|---|
| 58 | $session = $bus->get("session"); |
|---|
| 59 | $token = $bus->get("request")->getValueWithMethod("token"); |
|---|
| 60 | $hasToken = !empty($token); |
|---|
| 61 | $hasParams = $redirector->hasParameters(); |
|---|
| 62 | |
|---|
| 63 | if (!$hasToken) { |
|---|
| 64 | $to = $redirector->getUri(); |
|---|
| 65 | } elseif ($hasParams) { |
|---|
| 66 | $to = $redirector->getUri() . "&token={$token}"; |
|---|
| 67 | } else { |
|---|
| 68 | $to = $redirector->getUri() . "?token={$token}"; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | if ($session->isStarted() && !$session->isCookieEnabled()) { |
|---|
| 72 | $glue = ($hasToken || $hasParams) ? "&" : "?"; |
|---|
| 73 | $to .= $glue . $session->getName() . "=" . $session->getId(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | $ignored = ""; |
|---|
| 77 | if (defined("URI_IGNORE")) { |
|---|
| 78 | $ignored = ltrim($_SERVER["SCRIPT_NAME"], "/") . "/"; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $response->setLocation($ignored . $to, $_SERVER["SERVER_NAME"]); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | $response->outputHeader(); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|