3 * Useful globals class for Rest
6 * @link http://www.open-emr.org
7 * @author Jerry Padgett <sjpadgett@gmail.com>
8 * @copyright Copyright (c) 2018 Jerry Padgett <sjpadgett@gmail.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
12 // also a handy place to add utility methods
16 /** @var set to true to send debug info to the browser */
17 public static $DEBUG_MODE = false;
19 /** @var default action is the controller.method fired when no route is specified */
20 public static $DEFAULT_ACTION = "";
22 /** @var routemap is an array of patterns and routes */
23 public static $ROUTE_MAP;
25 /** @var fhir routemap is an array of patterns and routes */
26 public static $FHIR_ROUTE_MAP;
28 /** @var app root is the root directory of the application */
29 public static $APP_ROOT;
31 /** @var root url of the application */
32 public static $ROOT_URL;
33 public static $REST_FULL_URL;
34 public static $VENDOR_DIR;
35 public static $webserver_root;
36 public static $web_root;
37 public static $server_document_root;
40 private static $INSTANCE;
41 private static $IS_INITIALIZED = false;
45 /** prevents external construction */
46 private function __construct()
50 /** prevents external cloning */
51 private function __clone()
56 * Initialize the RestConfig object
58 static function Init()
60 if (!self
::$IS_INITIALIZED) {
62 self
::$REST_FULL_URL = $_SERVER['REQUEST_SCHEME'] . "//" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // @todo unsure here!
63 self
::$ROOT_URL = self
::$web_root . "/apis";
64 self
::$VENDOR_DIR = self
::$webserver_root . "/vendor";
65 self
::$IS_INITIALIZED = true;
70 * Returns an instance of the RestConfig singleton
73 static function GetInstance()
75 if (!self
::$IS_INITIALIZED) {
79 if (!self
::$INSTANCE instanceof self
) {
80 self
::$INSTANCE = new self
;
83 return self
::$INSTANCE;
87 * Returns the api's context in form of token. e.g api called from OpenEMR authorized session.
88 * @return token or null if not local.
92 if ($this->context
== null) {
93 $local_auth = isset($_SERVER['HTTP_APPSECRET']) ?
$_SERVER['HTTP_APPSECRET'] : false;
95 session_id($local_auth); // a must for cURL. See oeHttp Client request.
97 session_name("OpenEMR"); // works for browser/ajax.
100 $app_token = isset($_SERVER['HTTP_APPTOKEN']) ?
$_SERVER['HTTP_APPTOKEN'] : false;
101 $session_verified = ($app_token === $local_auth); // @todo future may force any http client to pass session id
102 if (isset($_SESSION['authUserID']) && !empty($_SESSION['authUser'])) {
103 $this->context
= $_SESSION['csrf_token'];
109 return $this->context
;
113 * Basic paths when GLOBALS are not yet available.
116 static function SetPaths()
118 $isWindows = stripos(PHP_OS
, 'WIN') === 0;
119 self
::$webserver_root = dirname(__FILE__
);
121 //convert windows path separators
122 self
::$webserver_root = str_replace("\\", "/", self
::$webserver_root);
124 // Collect the apache server document root (and convert to windows slashes, if needed)
125 self
::$server_document_root = realpath($_SERVER['DOCUMENT_ROOT']);
127 //convert windows path separators
128 self
::$server_document_root = str_replace("\\", "/", self
::$server_document_root);
130 self
::$web_root = substr(self
::$webserver_root, strspn(self
::$webserver_root ^ self
::$server_document_root, "\0"));
131 // Ensure web_root starts with a path separator
132 if (preg_match("/^[^\/]/", self
::$web_root)) {
133 self
::$web_root = "/" . self
::$web_root;
137 function destroySession()
139 if (!isset($_SESSION)) {
143 if (ini_get("session.use_cookies")) {
144 $params = session_get_cookie_params();
157 static function getPostData($data)
161 } elseif ($post_data = file_get_contents('php://input')) {
162 if ($post_json = json_decode($post_data, true)) {
165 parse_str($post_data, $post_variables);
166 if (count($post_variables)) {
167 return $post_variables;
176 // Include our routes and init routes global
178 include_once("./../_rest_routes.inc.php");