forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_rest_config.php
178 lines (157 loc) · 5.36 KB
/
_rest_config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Useful globals class for Rest
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2018 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
// also a handy place to add utility methods
//
class RestConfig
{
/** @var set to true to send debug info to the browser */
public static $DEBUG_MODE = false;
/** @var default action is the controller.method fired when no route is specified */
public static $DEFAULT_ACTION = "";
/** @var routemap is an array of patterns and routes */
public static $ROUTE_MAP;
/** @var fhir routemap is an array of patterns and routes */
public static $FHIR_ROUTE_MAP;
/** @var app root is the root directory of the application */
public static $APP_ROOT;
/** @var root url of the application */
public static $ROOT_URL;
public static $REST_FULL_URL;
public static $VENDOR_DIR;
public static $webserver_root;
public static $web_root;
public static $server_document_root;
public static $SITE;
private static $INSTANCE;
private static $IS_INITIALIZED = false;
private $context;
/** prevents external construction */
private function __construct()
{
}
/** prevents external cloning */
private function __clone()
{
}
/**
* Initialize the RestConfig object
*/
static function Init()
{
if (!self::$IS_INITIALIZED) {
self::setPaths();
self::$REST_FULL_URL = $_SERVER['REQUEST_SCHEME'] . "//" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // @todo unsure here!
self::$ROOT_URL = self::$web_root . "/apis";
self::$VENDOR_DIR = self::$webserver_root . "/vendor";
self::$IS_INITIALIZED = true;
}
}
/**
* Returns an instance of the RestConfig singleton
* @return RestConfig
*/
static function GetInstance()
{
if (!self::$IS_INITIALIZED) {
self::Init();
}
if (!self::$INSTANCE instanceof self) {
self::$INSTANCE = new self;
}
return self::$INSTANCE;
}
/**
* Returns the api's context in form of token. e.g api called from OpenEMR authorized session.
* @return token or null if not local.
*/
function GetContext()
{
if ($this->context == null) {
$local_auth = isset($_SERVER['HTTP_APPSECRET']) ? $_SERVER['HTTP_APPSECRET'] : false;
if ($local_auth) {
session_id($local_auth); // a must for cURL. See oeHttp Client request.
} else {
session_name("OpenEMR"); // works for browser/ajax.
}
session_start();
$app_token = isset($_SERVER['HTTP_APPTOKEN']) ? $_SERVER['HTTP_APPTOKEN'] : false;
$session_verified = ($app_token === $local_auth); // @todo future may force any http client to pass session id
if (isset($_SESSION['authUserID']) && !empty($_SESSION['authUser'])) {
$this->context = $_SESSION['csrf_token'];
} else {
session_destroy();
}
}
return $this->context;
}
/**
* Basic paths when GLOBALS are not yet available.
* @return none
*/
static function SetPaths()
{
$isWindows = stripos(PHP_OS, 'WIN') === 0;
self::$webserver_root = dirname(__FILE__);
if ($isWindows) {
//convert windows path separators
self::$webserver_root = str_replace("\\", "/", self::$webserver_root);
}
// Collect the apache server document root (and convert to windows slashes, if needed)
self::$server_document_root = realpath($_SERVER['DOCUMENT_ROOT']);
if ($isWindows) {
//convert windows path separators
self::$server_document_root = str_replace("\\", "/", self::$server_document_root);
}
self::$web_root = substr(self::$webserver_root, strspn(self::$webserver_root ^ self::$server_document_root, "\0"));
// Ensure web_root starts with a path separator
if (preg_match("/^[^\/]/", self::$web_root)) {
self::$web_root = "/" . self::$web_root;
}
}
function destroySession()
{
if (!isset($_SESSION)) {
return;
}
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 42000,
$params["path"],
$params["domain"],
$params["secure"],
$params["httponly"]
);
}
}
static function getPostData($data)
{
if (count($_POST)) {
return $_POST;
} elseif ($post_data = file_get_contents('php://input')) {
if ($post_json = json_decode($post_data, true)) {
return $post_json;
} else {
parse_str($post_data, $post_variables);
if (count($post_variables)) {
return $post_variables;
}
}
}
return false;
}
}
// Include our routes and init routes global
//
include_once("./../_rest_routes.inc.php");