Openemr fhir search (#4349)
[openemr.git] / src / Common / Http / HttpRestRequest.php
blob24b52b3f329b0ba3df4838baab45cde560af1d79
1 <?php
3 /**
4 * HttpRestRequest represents the current OpenEMR api request
5 * @package openemr
6 * @link http://www.open-emr.org
7 * @author Stephen Nielson <stephen@nielson.org>
8 * @copyright Copyright (c) 2021 Stephen Nielson <stephen@nielson.org>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
12 namespace OpenEMR\Common\Http;
14 use OpenEMR\Common\Logging\SystemLogger;
15 use OpenEMR\Common\System\System;
16 use OpenEMR\Common\Uuid\UuidRegistry;
18 class HttpRestRequest
21 /**
22 * @var \RestConfig
24 private $restConfig;
26 /**
27 * The Resource that is being requested in this http rest call.
28 * @var string
30 private $resource;
32 /**
33 * The FHIR operation that this request represents. FHIR operations are prefixed with a $ ie $export
34 * @var string
36 private $operation;
38 /**
39 * @var array
41 private $requestUser;
43 /**
44 * The binary string of the request user uuid
45 * @var string
47 private $requestUserUUID;
49 /**
50 * @var string
52 private $requestUserUUIDString;
54 /**
55 * @var 'patient'|'users'
57 private $requestUserRole;
59 /**
60 * @var array
62 private $accessTokenScopes;
64 /**
65 * @var string
67 private $requestSite;
69 /**
70 * @var string
72 private $clientId;
74 /**
75 * @var string
77 private $accessTokenId;
79 /**
80 * @var boolean
82 private $isLocalApi;
84 /**
85 * @var string
87 private $requestMethod;
89 /**
90 * The kind of REST api request this object represents
91 * @var string
93 private $apiType;
95 /**
96 * @var string
98 private $requestPath;
101 * @var string the URL for the api base full url
103 private $apiBaseFullUrl;
106 * @var string[] The request headers
108 private $headers;
111 * @var mixed[]
113 private $queryParams;
115 public function __construct($restConfig, $server)
117 $this->restConfig = $restConfig;
118 $this->requestSite = $restConfig::$SITE;
120 $this->requestMethod = $server["REQUEST_METHOD"];
121 $this->setRequestURI($server['REQUEST_URI'] ?? "");
122 $this->headers = $this->parseHeadersFromServer($server);
123 $this->queryParams = $_GET ?? [];
124 // remove the OpenEMR queryParams that our rewrite command injected so we don't mess stuff up.
125 if (isset($this->queryParams['_REWRITE_COMMAND'])) {
126 unset($this->queryParams['_REWRITE_COMMAND']);
130 public function getQueryParams()
132 return $this->queryParams;
135 public function getQueryParam($key)
137 return $this->queryParams[$key] ?? null;
141 * Return an array of HTTP request headers
142 * @return array|string[]
144 public function getHeaders()
146 return array_values($this->headers);
150 * Retrieve the value of the passed in request's HTTP header. Return's null if the value does not exist
151 * @param $headerName string the name of the header value to retrieve.
152 * @return mixed|string|null
154 public function getHeader($headerName)
156 return $this->headers[$headerName] ?? null;
160 * Checks if the current HTTP request has the passed in header
161 * @param $headerName The name of the header to check
162 * @return bool true if the header exists, false otherwise.
164 public function hasHeader($headerName)
166 return !empty($this->headers[$headerName]);
170 * @return \RestConfig
172 public function getRestConfig(): \RestConfig
174 return $this->restConfig;
178 * Return the Request URI (matches the $_SERVER['REQUEST_URI'])
179 * @return mixed|string
181 public function getRequestURI()
183 return $this->requestURI;
187 * Return the Request URI (matches the $_SERVER['REQUEST_URI'])
188 * @param mixed|string $requestURI
190 public function setRequestURI($requestURI): void
192 $this->requestURI = $requestURI;
196 * @return string
198 public function getResource(): ?string
200 return $this->resource;
204 * @param string $resource
206 public function setResource(?string $resource): void
208 $this->resource = $resource;
212 * Returns the operation name for this request if this request represents a FHIR operation.
213 * Operations are prefixed with a $
214 * @return string
216 public function getOperation(): ?string
218 return $this->operation;
222 * Sets the operation name for this request if this request represents a FHIR operation.
223 * Operations are prefixed with a $
224 * @param string $operation The operation name
226 public function setOperation(string $operation): void
228 $this->operation = $operation;
232 * @return array
234 public function getRequestUser(): array
236 return $this->requestUser;
240 * Returns the current user id if we have one
241 * @return int|null
243 public function getRequestUserId(): ?int
245 $user = $this->getRequestUser();
246 return $user['id'] ?? null;
250 * @param array $requestUser
252 public function setRequestUser($userUUIDString, array $requestUser): void
254 $this->requestUser = $requestUser;
256 // set up any other user context information
257 if (empty($requestUser)) {
258 $this->requestUserUUIDString = null;
259 $this->requestUserUUID = null;
260 } else {
261 $this->requestUserUUIDString = $userUUIDString ?? null;
262 $this->requestUserUUID = UuidRegistry::uuidToBytes($userUUIDString) ?? null;
267 * @return array
269 public function getAccessTokenScopes(): array
271 return $this->accessTokenScopes;
275 * @param array $scopes
277 public function setAccessTokenScopes(array $scopes): void
279 $this->accessTokenScopes = $scopes;
283 * @return string
285 public function getRequestSite(): ?string
287 return $this->requestSite;
291 * @param string $requestSite
293 public function setRequestSite(string $requestSite): void
295 $this->requestSite = $requestSite;
299 * @return string
301 public function getClientId(): ?string
303 return $this->clientId;
307 * @param string $clientId
309 public function setClientId(string $clientId): void
311 $this->clientId = $clientId;
315 * @return string
317 public function getAccessTokenId(): ?string
319 return $this->accessTokenId;
323 * @param string $accessTokenId
325 public function setAccessTokenId(string $accessTokenId): void
327 $this->accessTokenId = $accessTokenId;
331 * @return bool
333 public function isLocalApi(): bool
335 return $this->isLocalApi;
339 * @param bool $isLocalApi
341 public function setIsLocalApi(bool $isLocalApi): void
343 $this->isLocalApi = $isLocalApi;
347 * @return mixed
349 public function getRequestUserRole()
351 return $this->requestUserRole;
355 * @param string $requestUserRole either 'patients' or 'users'
357 public function setRequestUserRole($requestUserRole): void
359 if (!in_array($requestUserRole, ['patient', 'users', 'system'])) {
360 throw new \InvalidArgumentException("invalid user role found");
362 $this->requestUserRole = $requestUserRole;
365 public function getRequestUserUUID()
367 return $this->requestUserUUID;
370 public function getRequestUserUUIDString()
372 return $this->requestUserUUIDString;
375 public function getPatientUUIDString()
377 // we may change how this is set, it will depend on if a 'user' role type can still have
378 // patient/<resource>.* requests. IE patient/Patient.read
379 return $this->requestUserUUIDString;
383 * @return string
385 public function getApiType(): ?string
387 return $this->apiType;
391 * @param string $api
393 public function setApiType(string $apiType): void
395 if (!in_array($apiType, ['fhir', 'oemr', 'port'])) {
396 throw new \InvalidArgumentException("invalid api type found");
398 $this->apiType = $apiType;
402 * @return string
404 public function getRequestMethod(): ?string
406 return $this->requestMethod;
410 public function isPatientRequest()
412 return $this->requestUserRole === 'patient';
415 public function isFhir()
417 return $this->getApiType() === 'fhir';
421 * If this is a patient context request for write/modify of patient context resources
422 * @return bool
424 public function isPatientWriteRequest()
426 return $this->isFhir() && $this->isPatientRequest() && $this->getRequestMethod() != 'GET';
429 public function setRequestPath(string $requestPath)
431 $this->requestPath = $requestPath;
434 public function getRequestPath(): ?string
436 return $this->requestPath;
440 * Returns the full URL to the api server
441 * @return string
443 public function getApiBaseFullUrl(): string
445 return $this->apiBaseFullUrl;
449 * Set the full URL to the api server that api requests are appended to.
450 * @param string $apiBaseFullUrl
452 public function setApiBaseFullUrl(string $apiBaseFullUrl): void
454 $this->apiBaseFullUrl = $apiBaseFullUrl;
458 * Given an array of server variables (typically the $_SERVER superglobal) parse out all of the HTTP_X headers
459 * and convert them into a hashmap of header -> header
460 * @param $server array of server variables typically the $_SERVER superglobal
461 * @return array hashmap of header -> header
463 private function parseHeadersFromServer($server)
465 $headers = array();
466 foreach ($server as $key => $value) {
467 $prefix = substr($key, 0, 5);
469 if ($prefix != 'HTTP_') {
470 continue;
473 $serverHeader = strtolower(substr($key, 5));
474 $uppercasedServerHeader = ucwords(str_replace('_', ' ', $serverHeader));
476 $header = str_replace(' ', '-', $uppercasedServerHeader);
477 $headers[$header] = $value;
479 return $headers;