fix merge
[openemr.git] / _rest_config.php
blobad97b2556f1958bea174e52eb363d543e9e55bf9
1 <?php
2 /**
3 * Useful globals class for Rest
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Jerry Padgett <sjpadgett@gmail.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2018 Jerry Padgett <sjpadgett@gmail.com>
10 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 require_once(dirname(__FILE__) . "/src/Common/Session/SessionUtil.php");
16 use OpenEMR\Common\Acl\AclMain;
17 use OpenEMR\RestControllers\AuthRestController;
19 // also a handy place to add utility methods
21 class RestConfig
23 /** @var set to true to send debug info to the browser */
24 public static $DEBUG_MODE = false;
26 /** @var default action is the controller.method fired when no route is specified */
27 public static $DEFAULT_ACTION = "";
29 /** @var routemap is an array of patterns and routes */
30 public static $ROUTE_MAP;
32 /** @var fhir routemap is an array of patterns and routes */
33 public static $FHIR_ROUTE_MAP;
35 /** @var app root is the root directory of the application */
36 public static $APP_ROOT;
38 /** @var root url of the application */
39 public static $ROOT_URL;
40 public static $REST_FULL_URL;
41 public static $VENDOR_DIR;
42 public static $webserver_root;
43 public static $web_root;
44 public static $server_document_root;
45 public static $SITE;
47 private static $INSTANCE;
48 private static $IS_INITIALIZED = false;
50 /** @var set to true if local api call */
51 private static $localCall = false;
53 /** @var set to true if not rest call */
54 private static $notRestCall = false;
56 /** prevents external construction */
57 private function __construct()
61 /** prevents external cloning */
62 private function __clone()
66 /**
67 * Initialize the RestConfig object
69 static function Init()
71 if (!self::$IS_INITIALIZED) {
72 self::setPaths();
73 self::$REST_FULL_URL = $_SERVER['REQUEST_SCHEME'] . "//" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // @todo unsure here!
74 self::$ROOT_URL = self::$web_root . "/apis";
75 self::$VENDOR_DIR = self::$webserver_root . "/vendor";
76 self::$IS_INITIALIZED = true;
80 /**
81 * Returns an instance of the RestConfig singleton
82 * @return RestConfig
84 static function GetInstance()
86 if (!self::$IS_INITIALIZED) {
87 self::Init();
90 if (!self::$INSTANCE instanceof self) {
91 self::$INSTANCE = new self;
94 return self::$INSTANCE;
98 /**
99 * Basic paths when GLOBALS are not yet available.
100 * @return none
102 static function SetPaths()
104 $isWindows = stripos(PHP_OS, 'WIN') === 0;
105 self::$webserver_root = dirname(__FILE__);
106 if ($isWindows) {
107 //convert windows path separators
108 self::$webserver_root = str_replace("\\", "/", self::$webserver_root);
110 // Collect the apache server document root (and convert to windows slashes, if needed)
111 self::$server_document_root = realpath($_SERVER['DOCUMENT_ROOT']);
112 if ($isWindows) {
113 //convert windows path separators
114 self::$server_document_root = str_replace("\\", "/", self::$server_document_root);
116 self::$web_root = substr(self::$webserver_root, strspn(self::$webserver_root ^ self::$server_document_root, "\0"));
117 // Ensure web_root starts with a path separator
118 if (preg_match("/^[^\/]/", self::$web_root)) {
119 self::$web_root = "/" . self::$web_root;
123 static function destroySession()
125 OpenEMR\Common\Session\SessionUtil::apiSessionCookieDestroy();
128 static function getPostData($data)
130 if (count($_POST)) {
131 return $_POST;
132 } elseif ($post_data = file_get_contents('php://input')) {
133 if ($post_json = json_decode($post_data, true)) {
134 return $post_json;
135 } else {
136 parse_str($post_data, $post_variables);
137 if (count($post_variables)) {
138 return $post_variables;
143 return false;
146 static function authorization_check($section, $value)
148 if (self::$notRestCall || self::$localCall) {
149 $result = AclMain::aclCheckCore($section, $value, $_SESSION['authUser']);
150 } else {
151 $authRestController = new AuthRestController();
152 $result = $authRestController->aclCheck($_SERVER["HTTP_X_API_TOKEN"], $section, $value);
154 if (!$result) {
155 if (!self::$notRestCall) {
156 http_response_code(401);
158 exit();
162 static function setLocalCall()
164 self::$localCall = true;
167 static function setNotRestCall()
169 self::$notRestCall = true;
172 static function is_authentication($resource)
174 return ($resource === "/api/auth" || $resource === "/fhir/auth");
177 static function get_bearer_token()
179 $parse = preg_split("/[\s,]+/", $_SERVER["HTTP_AUTHORIZATION"]);
180 if (strtoupper(trim($parse[0])) !== 'BEARER') {
181 return false;
184 return trim($parse[1]);
187 static function is_fhir_request($resource)
189 return (stripos(strtolower($resource), "/fhir/") !== false) ? true : false;
192 static function verify_api_request($resource, $api)
194 $api = strtolower(trim($api));
195 if (self::is_fhir_request($resource)) {
196 if ($api !== 'fhir') {
197 http_response_code(401);
198 exit();
200 } elseif ($api !== 'oemr') {
201 http_response_code(401);
202 exit();
205 return;
208 static function authentication_check($resource)
210 if (!self::is_authentication($resource)) {
211 $token = $_SERVER["HTTP_X_API_TOKEN"];
212 $authRestController = new AuthRestController();
213 if (!$authRestController->isValidToken($token)) {
214 http_response_code(401);
215 exit();
216 } else {
217 $authRestController->optionallyAddMoreTokenTime($token);
223 // Include our routes and init routes global
225 require_once(dirname(__FILE__) . "/_rest_routes.inc.php");