Merge pull request #1998 from bradymiller/api_2
[openemr.git] / _rest_config.php
blob72c4145276c2da008cc384994eeace93c19994b5
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
13 // also a handy place to add utility methods
15 class RestConfig
17 /** @var set to true to send debug info to the browser */
18 public static $DEBUG_MODE = false;
20 /** @var default action is the controller.method fired when no route is specified */
21 public static $DEFAULT_ACTION = "";
23 /** @var routemap is an array of patterns and routes */
24 public static $ROUTE_MAP;
26 /** @var app root is the root directory of the application */
27 public static $APP_ROOT;
29 /** @var root url of the application */
30 public static $ROOT_URL;
31 public static $REST_FULL_URL;
32 public static $VENDOR_DIR;
33 public static $webserver_root;
34 public static $web_root;
35 public static $server_document_root;
36 public static $SITE;
38 private static $INSTANCE;
39 private static $IS_INITIALIZED = false;
41 private $context;
43 /** prevents external construction */
44 private function __construct()
48 /** prevents external cloning */
49 private function __clone()
53 /**
54 * Initialize the RestConfig object
56 static function Init()
58 if (!self::$IS_INITIALIZED) {
59 self::setPaths();
60 self::$REST_FULL_URL = $_SERVER['REQUEST_SCHEME'] . "//" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // @todo unsure here!
61 self::$ROOT_URL = self::$web_root . "/apis";
62 self::$VENDOR_DIR = self::$webserver_root . "/vendor";
63 self::$IS_INITIALIZED = true;
67 /**
68 * Returns an instance of the RestConfig singleton
69 * @return RestConfig
71 static function GetInstance()
73 if (!self::$IS_INITIALIZED) {
74 self::Init();
77 if (!self::$INSTANCE instanceof self) {
78 self::$INSTANCE = new self;
81 return self::$INSTANCE;
84 /**
85 * Returns the context, used for storing session information
86 * @return Context
88 function GetContext()
90 if ($this->context == null) {
93 return $this->context;
96 /**
97 * Basic paths when GLOBALS are not yet available.
98 * @return none
100 static function SetPaths()
102 $isWindows = stripos(PHP_OS, 'WIN') === 0;
103 self::$webserver_root = dirname(__FILE__);
104 if ($isWindows) {
105 //convert windows path separators
106 self::$webserver_root = str_replace("\\", "/", self::$webserver_root);
108 // Collect the apache server document root (and convert to windows slashes, if needed)
109 self::$server_document_root = realpath($_SERVER['DOCUMENT_ROOT']);
110 if ($isWindows) {
111 //convert windows path separators
112 self::$server_document_root = str_replace("\\", "/", self::$server_document_root);
114 self::$web_root = substr(self::$webserver_root, strspn(self::$webserver_root ^ self::$server_document_root, "\0"));
115 // Ensure web_root starts with a path separator
116 if (preg_match("/^[^\/]/", self::$web_root)) {
117 self::$web_root = "/" . self::$web_root;
121 function destroySession()
123 if (!isset($_SESSION)) {
124 return;
126 $_SESSION = array();
127 if (ini_get("session.use_cookies")) {
128 $params = session_get_cookie_params();
129 setcookie(
130 session_name(),
132 time() - 42000,
133 $params["path"],
134 $params["domain"],
135 $params["secure"],
136 $params["httponly"]
142 // Include our routes and init routes global
144 include_once("./../_rest_routes.inc.php");