ServicesContext.inc in Services 7.3
File
servers/rest_server/includes/ServicesContext.inc
View source
<?php
interface ServicesContextInterface {
public function buildFromGlobals();
public function getEndpointPath();
public function getCanonicalPath();
public function getPostData();
public function getRequestBody();
public function getServerVariable($variable_name);
public function getGetVariable($variable_name = NULL);
}
class ServicesContext implements ServicesContextInterface {
protected $data = array();
function __construct($endpoint_path) {
$this->data['endpoint_path'] = $endpoint_path;
}
public function buildFromGlobals() {
$this->data['get'] = $_GET;
$this->data['server'] = $_SERVER;
$this->data['post'] = $_POST;
$this->data['request_body'] = '';
if ($this
->isRequestHasPostBody()) {
$this->data['request_body'] = $this
->getRequestBodyData();
}
}
public function getEndpointPath() {
return $this->data['endpoint_path'];
}
public function getCanonicalPath() {
if (!isset($this->data['canonical_path'])) {
$endpoint_path = $this
->getEndpointPath();
$endpoint_path_len = drupal_strlen($endpoint_path . '/');
$this->data['canonical_path'] = drupal_substr($this->data['get']['q'], $endpoint_path_len);
}
return $this->data['canonical_path'];
}
public function getRequestMethod() {
if (!isset($this->data['request_method'])) {
$this->data['request_method'] = $this
->getRequestMethodFromGlobals();
}
return $this->data['request_method'];
}
protected function getRequestMethodFromGlobals() {
$server =& $this->data['server'];
$get =& $this->data['get'];
$method = $server['REQUEST_METHOD'];
if ($method == 'POST' && isset($server['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$method = $server['HTTP_X_HTTP_METHOD_OVERRIDE'];
}
if ($method == 'POST' && isset($get['_method']) && $get['_method']) {
$method = $get['_method'];
}
if (isset($get['_method'])) {
unset($get['_method']);
}
return $method;
}
protected function isRequestHasPostBody() {
$method = $this
->getRequestMethod();
return in_array($method, array(
'POST',
'PUT',
));
}
protected function getRequestBodyData() {
return file_get_contents('php://input');
}
public function getPostData() {
return $this->data['post'];
}
public function getRequestBody() {
return $this->data['request_body'];
}
public function getServerVariable($variable_name) {
if (isset($this->data['server'][$variable_name])) {
return $this->data['server'][$variable_name];
}
else {
if ($variable_name == 'CONTENT_TYPE' && isset($this->data['server']['HTTP_CONTENT_TYPE'])) {
return $this->data['server']['HTTP_CONTENT_TYPE'];
}
}
}
public function getGetVariable($variable_name = NULL) {
if (empty($variable_name)) {
return $this->data['get'];
}
if (isset($this->data['get'][$variable_name])) {
return $this->data['get'][$variable_name];
}
}
}
Classes
Interfaces
Name |
Description |
ServicesContextInterface |
ServicesContext object is for storing all global variables. The idea is to prevent
RESTServer from accessing global variables directly and get them only from Context. |