Billing Manager x12 partner fix. (#2134)
[openemr.git] / _rest_config.php
blobd426e6834f3eaa02db4ba02304c8cfe0f9685d72
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 * @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
14 class RestConfig
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;
38 public static $SITE;
40 private static $INSTANCE;
41 private static $IS_INITIALIZED = false;
43 private $context;
45 /** prevents external construction */
46 private function __construct()
50 /** prevents external cloning */
51 private function __clone()
55 /**
56 * Initialize the RestConfig object
58 static function Init()
60 if (!self::$IS_INITIALIZED) {
61 self::setPaths();
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;
69 /**
70 * Returns an instance of the RestConfig singleton
71 * @return RestConfig
73 static function GetInstance()
75 if (!self::$IS_INITIALIZED) {
76 self::Init();
79 if (!self::$INSTANCE instanceof self) {
80 self::$INSTANCE = new self;
83 return self::$INSTANCE;
86 /**
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.
90 function GetContext()
92 if ($this->context == null) {
93 $local_auth = isset($_SERVER['HTTP_APPSECRET']) ? $_SERVER['HTTP_APPSECRET'] : false;
94 if ($local_auth) {
95 session_id($local_auth); // a must for cURL. See oeHttp Client request.
96 } else {
97 session_name("OpenEMR"); // works for browser/ajax.
99 session_start();
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'];
104 } else {
105 session_destroy();
109 return $this->context;
113 * Basic paths when GLOBALS are not yet available.
114 * @return none
116 static function SetPaths()
118 $isWindows = stripos(PHP_OS, 'WIN') === 0;
119 self::$webserver_root = dirname(__FILE__);
120 if ($isWindows) {
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']);
126 if ($isWindows) {
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)) {
140 return;
142 $_SESSION = array();
143 if (ini_get("session.use_cookies")) {
144 $params = session_get_cookie_params();
145 setcookie(
146 session_name(),
148 time() - 42000,
149 $params["path"],
150 $params["domain"],
151 $params["secure"],
152 $params["httponly"]
157 static function getPostData($data)
159 if (count($_POST)) {
160 return $_POST;
161 } elseif ($post_data = file_get_contents('php://input')) {
162 if ($post_json = json_decode($post_data, true)) {
163 return $post_json;
164 } else {
165 parse_str($post_data, $post_variables);
166 if (count($post_variables)) {
167 return $post_variables;
172 return false;
176 // Include our routes and init routes global
178 include_once("./../_rest_routes.inc.php");