Пример #1
0
 protected function negotiateLanguage()
 {
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         $found = false;
         $langList = Util::sortByPriority($_SERVER['HTTP_ACCEPT_LANGUAGE']);
         foreach ($langList as $lang => $quality) {
             foreach (Defaults::$supportedLanguages as $supported) {
                 if (strcasecmp($supported, $lang) == 0) {
                     $found = true;
                     Defaults::$language = $supported;
                     break 2;
                 }
             }
         }
         if (!$found) {
             if (strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], '*') !== false) {
                 //use default language
             } else {
                 //ignore
             }
         }
     }
 }
Пример #2
0
 /**
  * Parses the request to figure out the best format for response.
  * Extension, if present, overrides the Accept header
  *
  * @return iFormat any class that implements iFormat
  * @example JsonFormat
  */
 protected function getResponseFormat()
 {
     // check if client has specified an extension
     /**
      *
      * @var iFormat
      */
     $format = null;
     $extensions = explode('.', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
     while ($extensions) {
         $extension = array_pop($extensions);
         $extension = explode('/', $extension);
         $extension = array_shift($extension);
         if ($extension && isset($this->formatMap[$extension])) {
             $format = $this->formatMap[$extension];
             $format = is_string($format) ? new $format() : $format;
             $format->setExtension($extension);
             // echo "Extension $extension";
             return $format;
         }
     }
     // check if client has sent list of accepted data formats
     if (isset($_SERVER['HTTP_ACCEPT'])) {
         $acceptList = Util::sortByPriority($_SERVER['HTTP_ACCEPT']);
         foreach ($acceptList as $accept => $quality) {
             if (isset($this->formatMap[$accept])) {
                 $format = $this->formatMap[$accept];
                 $format = is_string($format) ? new $format() : $format;
                 //TODO: check if the string verfication above is needed
                 $format->setMIME($accept);
                 //echo "MIME $accept";
                 // Tell cache content is based on Accept header
                 @header('Vary: Accept');
                 return $format;
             } elseif (false !== ($index = strrpos($accept, '+'))) {
                 $mime = substr($accept, 0, $index);
                 if (is_string(Defaults::$apiVendor) && 0 === strpos($mime, 'application/vnd.' . Defaults::$apiVendor . '-v')) {
                     $extension = substr($accept, $index + 1);
                     if (isset($this->formatMap[$extension])) {
                         //check the MIME and extract version
                         $version = intVal(substr($mime, 18 + strlen(Defaults::$apiVendor)));
                         if ($version > 0 && $version <= $this->apiVersion) {
                             $this->requestedApiVersion = $version;
                             $format = $this->formatMap[$extension];
                             $format = is_string($format) ? new $format() : $format;
                             $format->setExtension($extension);
                             // echo "Extension $extension";
                             Defaults::$useVendorMIMEVersioning = true;
                             @header('Vary: Accept');
                             return $format;
                         }
                     }
                 }
             }
         }
     } else {
         // RFC 2616: If no Accept header field is
         // present, then it is assumed that the
         // client accepts all media types.
         $_SERVER['HTTP_ACCEPT'] = '*/*';
     }
     if (strpos($_SERVER['HTTP_ACCEPT'], '*') !== false) {
         if (strpos($_SERVER['HTTP_ACCEPT'], 'application/*') !== false) {
             $format = new JsonFormat();
         } elseif (strpos($_SERVER['HTTP_ACCEPT'], 'text/*') !== false) {
             $format = new XmlFormat();
         } elseif (strpos($_SERVER['HTTP_ACCEPT'], '*/*') !== false) {
             $format = $this->formatMap['default'];
             $format = new $format();
         }
     }
     if (empty($format)) {
         // RFC 2616: If an Accept header field is present, and if the
         // server cannot send a response which is acceptable according to
         // the combined Accept field value, then the server SHOULD send
         // a 406 (not acceptable) response.
         $format = $this->formatMap['default'];
         $format = new $format();
         $this->responseFormat = $format;
         $this->handleError(406, 'Content negotiation failed. ' . 'Try \'' . $format->getMIME() . '\' instead.');
     } else {
         // Tell cache content is based at Accept header
         @header("Vary: Accept");
         return $format;
     }
 }
Пример #3
0
 protected function negotiateLanguage()
 {
     $accept_language = null;
     if (is_null($this->requestFromObject)) {
         if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
             $accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
         }
     } else {
         $accept_language = $this->requestFromObject->getHeader()->get('Accept-Language');
         if (!is_null($accept_language)) {
             $accept_language = $accept_language->getValue();
         }
     }
     if (!is_null($accept_language)) {
         $found = false;
         $langList = Util::sortByPriority($accept_language);
         foreach ($langList as $lang => $quality) {
             foreach (Defaults::$supportedLanguages as $supported) {
                 if (strcasecmp($supported, $lang) == 0) {
                     $found = true;
                     Defaults::$language = $supported;
                     break 2;
                 }
             }
         }
         if (!$found) {
             if (strpos($accept_language, '*') !== false) {
                 //use default language
             } else {
                 //ignore
             }
         }
     }
 }