public function postRequest(\SS_HTTPRequest $request, \SS_HTTPResponse $response, \DataModel $model) { if ($this->convertUrls && $response && $response->getStatusCode() == 200) { // only convert if we have an HTML content type response $body = $response->getBody(); // find urls inserted in content if (strpos($body, 'cdnfileid') > 0 && preg_match_all('/data-cdnfileid="(\\d+)"/', $body, $matches)) { $files = CdnImage::get()->filter('ID', $matches[1]); $fileIds = array(); foreach ($files as $file) { $url = $file->getUrl(); $filename = $file->Filename; $body = str_replace("src=\"{$filename}\"", "src=\"{$url}\"", $body); $fileIds[] = $file->ID; } $assets = ContentServiceAsset::get()->filter('SourceID', $matches[1]); foreach ($assets as $asset) { $url = $asset->getUrl(); $filename = $asset->Filename; // note the extra forward slash here, image_cached inserts it $body = str_replace("src=\"/{$filename}\"", "src=\"{$url}\"", $body); } $response->setBody($body); } } }
/** * Filter executed AFTER a request * * @param SS_HTTPRequest $request Request container object * @param SS_HTTPResponse $response Response output object * @param DataModel $model Current DataModel * @return boolean Whether to continue processing other filters. Null or true will continue processing (optional) */ public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) { $code = $response->getStatusCode(); $error_page_path = Director::baseFolder() . "/errors_pages/ui/{$code}/index.html"; if (!$request->isAjax() && file_exists($error_page_path)) { //clean buffer ob_clean(); $page_file = fopen($error_page_path, "r") or die("Unable to open file!"); $body = fread($page_file, filesize($error_page_path)); fclose($page_file); // set content type $response->addHeader('Content-Type', 'text/html'); $response->setBody($body); $response->setStatusCode(200); return true; } return true; }
/** * Returns true if the last response was a 3xx redirection * * @return bool */ public function wasRedirected() { $code = $this->lastResponse->getStatusCode(); return $code >= 300 && $code < 400; }
/** * Assert that a response matches the given parameters * * @param int $code HTTP code * @param string $body Body expected for 200 responses * @param SS_HTTPResponse $response */ protected function assertResponseEquals($code, $body, SS_HTTPResponse $response) { $this->assertEquals($code, $response->getStatusCode()); if ($code === 200) { $this->assertFalse($response->isError()); $this->assertEquals($body, $response->getBody()); $this->assertEquals('text/plain', $response->getHeader('Content-Type')); } else { $this->assertTrue($response->isError()); } }
/** * Attempt to redirect towards the highest priority link mapping that may have been defined. * * @URLparameter direct <{BYPASS_LINK_MAPPINGS}> boolean */ public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) { // Bypass the request filter when requesting specific director rules such as "/admin" or "/dev". $requestURL = $request->getURL(); $configuration = Config::inst(); foreach ($configuration->get('Director', 'rules') as $segment => $controller) { // Retrieve the specific director rules. if (($position = strpos($segment, '$')) !== false) { $segment = rtrim(substr($segment, 0, $position), '/'); } // Determine if the current request matches a specific director rule. if ($segment && strpos($requestURL, $segment) === 0) { // Continue processing the response. return true; } } // Bypass the request filter when using the direct GET parameter. if ($request->getVar('direct')) { // Continue processing the response. return true; } // Determine the default automated URL handling response status. $status = $response->getStatusCode(); $success = $status >= 200 && $status < 300; $error = $status === 404; // Either hook into a page not found, or when enforced, replace the default automated URL handling. $enforce = $configuration->get('MisdirectionRequestFilter', 'enforce_misdirection'); $replace = $configuration->get('MisdirectionRequestFilter', 'replace_default'); if (($error || $enforce || $replace) && ($map = $this->service->getMappingByRequest($request))) { // Update the response code where appropriate. $responseCode = $map->ResponseCode; if ($responseCode == 0) { $responseCode = 303; } else { if ($responseCode == 301 && $map->ForwardPOSTRequest) { $responseCode = 308; } else { if ($responseCode == 303 && $map->ForwardPOSTRequest) { $responseCode = 307; } } } // Update the response using the link mapping redirection. $response->redirect($map->getLink(), $responseCode); } else { if ($error && ($fallback = $this->service->determineFallback($requestURL))) { // Update the response code where appropriate. $responseCode = $fallback['code']; if ($responseCode === 0) { $responseCode = 303; } // Update the response using the fallback, enforcing no further redirection. $response->redirect(HTTP::setGetVar('direct', true, Controller::join_links(Director::absoluteBaseURL(), $fallback['link'])), $responseCode); } else { if (!$error && !$success && $replace) { $response->setStatusCode(404); // Retrieve the appropriate page not found response. ClassInfo::exists('SiteTree') && ($page = ErrorPage::response_for(404)) ? $response->setBody($page->getBody()) : $response->setBody('No URL was matched!'); } } } // Continue processing the response. return true; }
/** * @param SS_HTTPResponse $response * @return string XML string containing the HTTP error message */ protected function getErrorMessage($response) { return "<error type=\"authentication\" code=\"" . $response->getStatusCode() . "\">" . $response->getStatusDescription() . "</error>"; }
/** * Action to handle removing a single file from the db relation * * @param SS_HTTPRequest $request * @return SS_HTTPResponse */ public function remove(SS_HTTPRequest $request) { // Check form field state if ($this->parent->isDisabled() || $this->parent->isReadonly()) { return $this->httpError(403); } // Protect against CSRF on destructive action $token = $this->parent->getForm()->getSecurityToken(); if (!$token->checkRequest($request)) { return $this->httpError(400); } $response = new SS_HTTPResponse(); $response->setStatusCode(500); $fieldName = $this->parent->getName(); $record = $this->parent->getRecord(); $id = $this->getItem()->ID; if ($id && $record && $record->exists()) { if (($record->has_many($fieldName) || $record->many_many($fieldName)) && ($file = $record->{$fieldName}()->byID($id))) { $record->{$fieldName}()->remove($file); $response->setStatusCode(200); } elseif ($record->has_one($fieldName) && $record->{$fieldName . 'ID'} == $id) { $record->{$fieldName . 'ID'} = 0; $record->write(); $response->setStatusCode(200); } } if ($response->getStatusCode() != 200) { $response->setStatusDescription(_t('UploadField.REMOVEERROR', 'Error removing file')); } return $response; }
public function setResponse(SS_HTTPResponse $response) { $body = json_encode(array('data' => array('status_code' => $response->getStatusCode(), 'message' => $response->getBody()))); $this->response = new JsonDataResponse($body, $response->getStatusCode(), $response->getStatusDescription()); }