Exemple #1
0
 /**
  * Sends request
  *
  * Method does not return result. It's exit from script.
  */
 public function send()
 {
     if (fn_notification_exists('extra', 'company_access_denied')) {
         $this->status = Response::STATUS_FORBIDDEN;
     } elseif (fn_notification_exists('extra', '404')) {
         $this->status = Response::STATUS_NOT_FOUND;
     }
     if ($this->status == self::STATUS_UNAUTHORIZED) {
         header('WWW-Authenticate: Basic realm="User email/API key"');
     }
     $this->sendStatusCode($this->status);
     if ($this->status == self::STATUS_NO_CONTENT) {
         exit;
     }
     header('Content-type: ' . $this->content_type);
     if (!self::isSuccessStatus($this->status)) {
         $messages = array();
         if (is_array($this->body)) {
             if (!empty($this->body['message'])) {
                 $messages = array($this->body['message']);
             } else {
                 $messages = $this->body;
             }
         } elseif (!empty($this->body)) {
             $messages = array($this->body);
         }
         $this->body = array();
         $codes = self::getAvailableCodes();
         $this->body['message'] = $codes[$this->status];
         $notifications = fn_get_notifications();
         foreach ($notifications as $notice) {
             if ($notice['type'] == 'E') {
                 $messages[] = $notice['message'];
             }
         }
         foreach ($notifications as $notice) {
             if ($notice['type'] == 'W') {
                 $messages[] = $notice['message'];
             }
         }
         if (!empty($messages)) {
             $this->body['message'] .= ': ' . implode('. ', $messages);
         }
         $this->body['status'] = $this->status;
     }
     $body = FormatManager::instance()->encode($this->body, $this->content_type);
     echo $body;
     exit;
 }
Exemple #2
0
 /**
  * Gets request data from current http request
  *
  * @return string Content type
  */
 protected function getDataFromRequestBody()
 {
     $params = array();
     $method = $this->getMethodFromRequestHeaders();
     $content_type = $this->getContentType();
     if ($method == "PUT" || $method == "DELETE" || $method == "POST") {
         $params = file_get_contents('php://input');
         if (!empty($content_type)) {
             list($params, $this->error) = FormatManager::instance()->decode($params, $content_type);
         }
     } elseif ($method == "GET") {
         $params = $_GET;
     }
     return $params;
 }
Exemple #3
0
 /**
  * Handles request.
  * Method gets request from entities and send it
  *
  * @param null|Request $request Request object if empty will be created and filled from current HTTP request automatically
  */
 public function handleRequest($request = null)
 {
     if ($request instanceof Request) {
         $this->request = $request;
     }
     $authorized = $this->authenticate();
     /**
      * Rewrite default API behavior
      *
      * @param object $this       Api instance
      * @param bool   $authorized Authorization flag
      */
     fn_set_hook('api_handle_request', $this, $authorized);
     if (!$authorized && $this->area == 'A') {
         $response = new Response(Response::STATUS_UNAUTHORIZED);
     } else {
         $content_type = $this->request->getContentType();
         $accept_type = $this->request->getAcceptType();
         $method = $this->request->getMethod();
         if (($method == "PUT" || $method == "POST") && !FormatManager::instance()->isMimeTypeSupported($content_type)) {
             $response = new Response(Response::STATUS_UNSUPPORTED_MEDIA_TYPE);
         } elseif (($method == "GET" || $method == "HEAD") && !FormatManager::instance()->isMimeTypeSupported($accept_type)) {
             $response = new Response(Response::STATUS_METHOD_NOT_ACCEPTABLE);
         } elseif ($this->request->getError()) {
             $response = new Response(Response::STATUS_BAD_REQUEST, $this->request->getError(), $accept_type);
         } else {
             $controller_result = $this->getResponse($this->request->getResource());
             if (is_a($controller_result, '\\Tygh\\Api\\Response')) {
                 $response = $controller_result;
             } else {
                 $response = new Response(Response::STATUS_INTERNAL_SERVER_ERROR);
             }
         }
     }
     $response->send();
 }