Esempio n. 1
0
 public function testCanNotSendANewResponseAfterAjaxErrorResponseIsSent()
 {
     $service = $this->createService();
     ob_start();
     $json_response = $this->ajax->respondWithError('error');
     ob_get_clean();
     $this->assertEquals($json_response, $service->send($service->prepareResponse('bar')));
 }
Esempio n. 2
0
 /**
  * Prepares a redirect response
  *
  * @param string $forward_url Redirection URL
  * @param mixed  $status_code HTTP status code or forward reason
  * @return SymfonyRedirectResponse
  * @throws InvalidParameterException
  */
 public function redirect($forward_url = REFERRER, $status_code = ELGG_HTTP_FOUND)
 {
     if ($forward_url === REFERRER) {
         $forward_url = $this->request->headers->get('Referer');
     }
     $forward_url = elgg_normalize_url($forward_url);
     // allow plugins to rewrite redirection URL
     $current_page = current_page_url();
     $params = ['current_url' => $current_page, 'forward_url' => $forward_url];
     $forward_reason = (string) $status_code;
     $forward_url = $this->hooks->trigger('forward', $forward_reason, $params, $forward_url);
     if ($this->response_sent) {
         // Response was sent from a forward hook
         // Clearing handlers to void infinite loops
         return $this->response_sent;
     }
     if ($forward_url === REFERRER) {
         $forward_url = $this->request->headers->get('Referer');
     }
     if (!is_string($forward_url)) {
         throw new InvalidParameterException("'forward', '{$forward_reason}' hook must return a valid redirection URL");
     }
     $forward_url = elgg_normalize_url($forward_url);
     switch ($status_code) {
         case 'system':
         case 'csrf':
             $status_code = ELGG_HTTP_OK;
             break;
         case 'admin':
         case 'login':
         case 'member':
         case 'walled_garden':
         default:
             $status_code = (int) $status_code;
             if (!$status_code || $status_code < 100 || $status_code > 599) {
                 $status_code = ELGG_HTTP_SEE_OTHER;
             }
             break;
     }
     if ($this->isXhr()) {
         if ($status_code < 100 || $status_code >= 300 && $status_code <= 399 || $status_code > 599) {
             // We only want to preserve OK and error codes
             // Redirect responses should be converted to OK responses as this is an XHR request
             $status_code = ELGG_HTTP_OK;
         }
         $output = ob_get_clean();
         if (!$this->isAction() && !$this->ajax->isAjax2Request()) {
             // legacy ajax calls are always OK
             // actions are wrapped by ResponseFactory::respond()
             $status_code = ELGG_HTTP_OK;
             $output = $this->wrapLegacyAjaxResponse($output, $forward_url);
         }
         $response = new OkResponse($output, $status_code, $forward_url);
         $headers = $response->getHeaders();
         $headers['Content-Type'] = 'application/json; charset=UTF-8';
         $response->setHeaders($headers);
         return $this->respond($response);
     }
     if ($this->isAction()) {
         // actions should always redirect on non xhr-calls
         if (!is_int($status_code) || $status_code < 300 || $status_code > 399) {
             $status_code = ELGG_HTTP_SEE_OTHER;
         }
     }
     $response = new OkResponse('', $status_code, $forward_url);
     if ($response->isRedirection()) {
         return $this->send($this->prepareRedirectResponse($forward_url, $status_code));
     }
     return $this->respond($response);
 }