/** * Registers a service to be used with the service chain * * @return Void * * @since 2015-12-17 * @author Deac Karns <*****@*****.**> */ public function register($name = NULL) { $headers = \Rhonda\Headers::getallheaders(); $config = \Rhonda\Config::get('system'); if (empty($name) && !empty($config)) { // see if service is set in the config if (!empty($config->host)) { $service = $config->host; } } else { if (!empty($name)) { $service = $name; } else { $service = "Generic-Service"; } } $chain = isset($headers['Service-Chain']) ? json_decode($headers['Service-Chain']) : array(); if (count($chain) < 1) { // Add the referrer to the chain if it exists if (isset($_SERVER['HTTP_REFERER']) && !is_null($_SERVER['HTTP_REFERER'])) { $ref = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); } else { if (!empty($_SERVER['HTTP_HOST'])) { $ref = parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST); } else { $ref = "External-Request"; } } $chain[] = $ref; } $chain[] = $service; self::$chain = json_encode($chain); $_SERVER['HTTP_SERVICE_CHAIN'] = self::$chain; }
/** * Allow CORS request * DO NOT forget to add a OPTIONS route to your router else CORS will still deny your request * * @example * <code> * \Rhonda\CORS::allow_headers(); * </code> * * @example * <code> * $cors = new \Rhonda\CORS(); * $cors->allow_headers(); * </code> * * @return void * * @since 2016-02-09 * @author Wesley Dekkers <*****@*****.**> **/ public static function allow_headers() { $allow_headers = \Rhonda\Headers::getallheaders()['Access-Control-Request-Headers']; if (!empty($allow_headers)) { header("Access-Control-Allow-Headers: {$allow_headers}"); } header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: POST, PUT, DELETE, GET, OPTIONS"); header("Access-Control-Max-Age: 1728000"); }
/** * Class constructor for api connection tool. The constructor will forward all existing headers. * * @param String - GET,POST,PUT,DELETE * @param String - API route to be called * @param Object - Data to be sent along with the request - (optional) * @param Array - Custom additional headers - (optional) * * @example * <code> * $post_body = (object) array("name"=>"John", "pass"=>"doe"); * $headers = array("Domain"=>"some string", "Authorization"=>"some string"); * $api = new \Rhonda\APIGateway('GET','http://example.com', $post_body, $headers); * $result = $api->run(); * </code> * * @since 2015-11-05 * @author Deac Karns <*****@*****.**> **/ function __construct($verb, $url, $data = NULL, $additional_headers = array()) { $this->verb = $verb; $this->url = $url; $this->data = $data; // Look for existing headers and forward them along as well. Existing headers will not be overwritten. $headers = array_merge($additional_headers, \Rhonda\Headers::getallheaders()); unset($headers['Host']); unset($headers['Content-Length']); $this->headers = $headers; $this->makeContext(); }
/** * Package incoming data along with the error summary from \Ronda\Error::summary() * * In order an error to be populated it uses \Rhonda\Error::add_summary_item(); and \Rhonda\Error::summary(); * * @param Parameter - The result you want to return * * @example * <code> * \Rhonda\Response:: package($result); * </code> * * @return Return - **Object** * { * "errors": ARRAY of error OBJECTS, * "data": STRING/OBJECT/ARRAY, * "request": { * "url": "REQUEST URL", * "method": "POST/PUT/DELETE/GET/PATCH.. etcetera" * } * } * * @since 2016-07-29 * @author Deac Karns <*****@*****.**> * @author Wesley Dekkers <*****@*****.**> **/ public static function package($data, $response_code = false) { $result = new \stdClass(); $result->errors = \Rhonda\Error::summary(); $result->data = $data; $result->request = new \stdClass(); $result->request->url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $result->request->method = $_SERVER['REQUEST_METHOD']; // When error not empty and data not empty change headers 206 if (!$response_code && !empty($result->errors) && !empty($data)) { $response_code = 209; } // when error not empty and data empty return 400 if (!$response_code && !empty($result->errors) && empty($data)) { $response_code = 400; } \Rhonda\Headers::set_response_code($response_code); return $result; }
<?php echo "<h3>\\Rhonda\\Headers</h3>"; // Get all request headers echo "<pre>"; print_r(\Rhonda\Headers::getallheaders()); echo "</pre>";