|
@@ -17,6 +17,27 @@
|
|
|
*/
|
|
*/
|
|
|
class request
|
|
class request
|
|
|
{
|
|
{
|
|
|
|
|
+ /**
|
|
|
|
|
+ * MIME type for JSON
|
|
|
|
|
+ *
|
|
|
|
|
+ * @const string
|
|
|
|
|
+ */
|
|
|
|
|
+ const MIME_JSON = 'application/json';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * MIME type for HTML
|
|
|
|
|
+ *
|
|
|
|
|
+ * @const string
|
|
|
|
|
+ */
|
|
|
|
|
+ const MIME_HTML = 'text/html';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * MIME type for XHTML
|
|
|
|
|
+ *
|
|
|
|
|
+ * @const string
|
|
|
|
|
+ */
|
|
|
|
|
+ const MIME_XHTML = 'application/xhtml+xml';
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Input stream to use for PUT parameter parsing.
|
|
* Input stream to use for PUT parameter parsing.
|
|
|
*
|
|
*
|
|
@@ -66,15 +87,7 @@ class request
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// decide if we are in JSON API or HTML context
|
|
// decide if we are in JSON API or HTML context
|
|
|
- if (
|
|
|
|
|
- (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
|
|
|
|
|
- $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
|
|
|
|
|
- (array_key_exists('HTTP_ACCEPT', $_SERVER) &&
|
|
|
|
|
- strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)
|
|
|
|
|
- )
|
|
|
|
|
- {
|
|
|
|
|
- $this->_isJsonApi = true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $this->_isJsonApi = $this->_detectJsonRequest();
|
|
|
|
|
|
|
|
// parse parameters, depending on request type
|
|
// parse parameters, depending on request type
|
|
|
switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET')
|
|
switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET')
|
|
@@ -168,4 +181,80 @@ class request
|
|
|
{
|
|
{
|
|
|
self::$_inputStream = $input;
|
|
self::$_inputStream = $input;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * detect the clients supported media type and decide if its a JSON API call or not
|
|
|
|
|
+ *
|
|
|
|
|
+ * Adapted from: http://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
|
|
|
|
+ *
|
|
|
|
|
+ * @access private
|
|
|
|
|
+ * @return bool
|
|
|
|
|
+ */
|
|
|
|
|
+ private function _detectJsonRequest()
|
|
|
|
|
+ {
|
|
|
|
|
+ $hasAcceptHeader = array_key_exists('HTTP_ACCEPT', $_SERVER);
|
|
|
|
|
+ $acceptHeader = $hasAcceptHeader ? $_SERVER['HTTP_ACCEPT'] : '';
|
|
|
|
|
+
|
|
|
|
|
+ // simple cases
|
|
|
|
|
+ if (
|
|
|
|
|
+ (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
|
|
|
|
|
+ $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
|
|
|
|
|
+ ($hasAcceptHeader &&
|
|
|
|
|
+ strpos($acceptHeader, self::MIME_JSON) !== false &&
|
|
|
|
|
+ strpos($acceptHeader, self::MIME_HTML) === false &&
|
|
|
|
|
+ strpos($acceptHeader, self::MIME_XHTML) === false)
|
|
|
|
|
+ )
|
|
|
|
|
+ {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // advanced case: media type negotiation
|
|
|
|
|
+ $mediaTypes = array();
|
|
|
|
|
+ if ($hasAcceptHeader)
|
|
|
|
|
+ {
|
|
|
|
|
+ $mediaTypeRanges = explode(',', trim($acceptHeader));
|
|
|
|
|
+ foreach ($mediaTypeRanges as $mediaTypeRange)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (preg_match(
|
|
|
|
|
+ '#(\*/\*|[a-z\-]+/[a-z\-+*]+(?:\s*;\s*[^q]\S*)*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?#',
|
|
|
|
|
+ trim($mediaTypeRange), $match
|
|
|
|
|
+ ))
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!isset($match[2]))
|
|
|
|
|
+ {
|
|
|
|
|
+ $match[2] = '1.0';
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ $match[2] = (string) floatval($match[2]);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!isset($mediaTypes[$match[2]]))
|
|
|
|
|
+ {
|
|
|
|
|
+ $mediaTypes[$match[2]] = array();
|
|
|
|
|
+ }
|
|
|
|
|
+ $mediaTypes[$match[2]][] = strtolower($match[1]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ krsort($mediaTypes);
|
|
|
|
|
+ foreach ($mediaTypes as $acceptedQuality => $acceptedValues)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($acceptedQuality === 0.0) continue;
|
|
|
|
|
+ foreach ($acceptedValues as $acceptedValue)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (
|
|
|
|
|
+ strpos($acceptedValue, self::MIME_HTML) === 0 ||
|
|
|
|
|
+ strpos($acceptedValue, self::MIME_XHTML) === 0
|
|
|
|
|
+ )
|
|
|
|
|
+ {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ elseif (strpos($acceptedValue, self::MIME_JSON) === 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|