-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdvancedFhirOntologyExternalModule.php
More file actions
661 lines (590 loc) · 31.2 KB
/
Copy pathAdvancedFhirOntologyExternalModule.php
File metadata and controls
661 lines (590 loc) · 31.2 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<?php
/**
*
*
* CSIRO Open Source Software Licence Agreement (variation of the BSD / MIT License)
* Copyright (c) 2018, Commonwealth Scientific and Industrial Research Organisation (CSIRO) ABN 41 687 119 230.
* All rights reserved. CSIRO is willing to grant you a licence to this FhirOntologyAutocompleteModule on the following terms, except where otherwise indicated for third party material.
* Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of CSIRO nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of CSIRO.
* EXCEPT AS EXPRESSLY STATED IN THIS AGREEMENT AND TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS". CSIRO MAKES NO REPRESENTATIONS, WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE.
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL CSIRO BE LIABLE ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION FOR BREACH OF CONTRACT, NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER LIABILITY HOWSOEVER INCURRED. WITHOUT LIMITING THE SCOPE OF THE PREVIOUS SENTENCE THE EXCLUSION OF LIABILITY SHALL INCLUDE: LOSS OF PRODUCTION OR OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN CONNECTION WITH THIS AGREEMENT, ACCESS OF THE SOFTWARE OR ANY OTHER DEALINGS WITH THE SOFTWARE, EVEN IF CSIRO HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS, DAMAGES OR OTHER LIABILITY.
* APPLICABLE LEGISLATION SUCH AS THE AUSTRALIAN CONSUMER LAW MAY APPLY REPRESENTATIONS, WARRANTIES, OR CONDITIONS, OR IMPOSES OBLIGATIONS OR LIABILITY ON CSIRO THAT CANNOT BE EXCLUDED, RESTRICTED OR MODIFIED TO THE FULL EXTENT SET OUT IN THE EXPRESS TERMS OF THIS CLAUSE ABOVE "CONSUMER GUARANTEES". TO THE EXTENT THAT SUCH CONSUMER GUARANTEES CONTINUE TO APPLY, THEN TO THE FULL EXTENT PERMITTED BY THE APPLICABLE LEGISLATION, THE LIABILITY OF CSIRO UNDER THE RELEVANT CONSUMER GUARANTEE IS LIMITED (WHERE PERMITTED AT CSIRO'S OPTION) TO ONE OF FOLLOWING REMEDIES OR SUBSTANTIALLY EQUIVALENT REMEDIES:
* (a) THE REPLACEMENT OF THE SOFTWARE, THE SUPPLY OF EQUIVALENT SOFTWARE, OR SUPPLYING RELEVANT SERVICES AGAIN;
* (b) THE REPAIR OF THE SOFTWARE;
* (c) THE PAYMENT OF THE COST OF REPLACING THE SOFTWARE, OF ACQUIRING EQUIVALENT SOFTWARE, HAVING THE RELEVANT SERVICES SUPPLIED AGAIN, OR HAVING THE SOFTWARE REPAIRED.
* IN THIS CLAUSE, CSIRO INCLUDES ANY THIRD PARTY AUTHOR OR OWNER OF ANY PART OF THE SOFTWARE OR MATERIAL DISTRIBUTED WITH IT. CSIRO MAY ENFORCE ANY RIGHTS ON BEHALF OF THE RELEVANT THIRD PARTY.
* Third Party Components
* The following third party components are distributed with the Software. You agree to comply with the licence terms for these components as part of accessing the Software. Other third party software may also be identified in separate files distributed with the Software.
*
*
*
*/
namespace AEHRC\AdvancedFhirOntologyExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
class AdvancedFhirOntologyExternalModule extends AbstractExternalModule implements \OntologyProvider
{
/** Fallback timeout (seconds) used when the 'fhir-timeout' setting is blank or invalid. */
const DEFAULT_TIMEOUT = 10;
public function __construct()
{
parent::__construct();
// register with OntologyManager
$manager = \OntologyManager::getOntologyManager();
$manager->addProvider($this);
}
public function redcap_every_page_before_render($project_id)
{
// don't need to do anything, just trigger the constructor so the provider is available.
}
public function validateSettings($settings)
{
$errors = '';
$ontologyIds = array();
$ontologyNames = array();
$ontologyIdValues = $settings['ontology-id'];
$ontologyNameValues = $settings['ontology-name'];
$codeTemplates = $settings['code-template'];
$displayTemplates = $settings['display-template'];
$rnrFlags = $settings['return-no-result'];
$rnrLabels = $settings['no-result-label'];
$rnrCodes = $settings['no-result-code'];
$fhirUrls = $settings['fhir-api-url'];
$authTypes = $settings['authentication-type'];
$authEndpoints = $settings['cc-token-endpoint'];
$clientIds = $settings['cc-client-id'];
$clientSecrets = $settings['cc-client-secret'];
$basicUserIds = $settings['basic-user-id'];
$basicUserPasswords = $settings['basic-user-password'];
$valueSetTypes = $settings['valueset-type'];
$valuesets = $settings['valueset'];
$priorityCodes = $settings['priority-codes'];
$priorityMaxFetches = $settings['priority-max-fetch'];
$bannedCodes = $settings['banned-codes'];
foreach ($ontologyIdValues as $key => $id) {
// check id is valid and not duplicated.
if ($id != strip_tags($id)
|| strpos($id, "'") !== false
|| strpos($id, '"') !== false
) {
$errors .= "Ontology ID has illegal characters - " . $id . "\n";
}
if (array_key_exists($id, $ontologyIds)){
$errors .= "Ontology ID has duplicates - " . $id . "\n";
}
else {
$ontologyIds[$id] = true;
}
// check name is valid and not duplicated.
$name = $ontologyNameValues[$key];
if ($name != strip_tags($name)) {
$errors .= "Ontology Id " . $id . " - Ontology Name has illegal characters - " . $name . "\n";
}
if (array_key_exists($name, $ontologyNames)){
$errors .= "Ontology Id " . $id . " - Ontology name is a duplicate - " . $name . "\n";
}
else {
$ontologyNames[$name] = true;
}
// check code template is valid and contains ${CODE}
$codeTemplate = $codeTemplates[$key];
if ($codeTemplate != strip_tags($codeTemplate)) {
$errors .= "Ontology Id " . $id . " - Code template has illegal characters - " . $codeTemplate . "\n";
}
if (strpos($codeTemplate, '${CODE}') === false){
$errors .= "Ontology Id " . $id . ' - Code template should contain "${CODE}" : ' . $codeTemplate . "\n";
}
// check display template is valid and contains ${CODE} or ${DISPLAY}
$displayTemplate = $displayTemplates[$key];
if ($displayTemplate != strip_tags($displayTemplate)) {
$errors .= "Ontology Id " . $id . " - Display template has illegal characters - " . $displayTemplate . "\n";
}
if (strpos($displayTemplate, '${CODE}') === false && strpos($displayTemplate, '${DISPLAY}') === false){
$errors .= "Ontology Id " . $id . ' - Display template should contain either "${CODE}" or "${DISPLAY}" : ' . $displayTemplate . "\n";
}
// test return no result settings
$rnr = $rnrFlags[$key];
if ($rnr) {
// check we have a code and label
$label = trim($rnrLabels[$key]);
$code = trim($rnrCodes[$key]);
if ($label === '') {
$errors .= "Ontology Id " . $id . " - No Result Label is required\n";
} else if ($label != strip_tags($label)) {
$errors .= "Ontology Id " . $id . " - No Results Label has illegal characters - " . $label . "\n";
}
if ($code === '') {
$errors .= "Ontology Id " . $id . " - No Result Code is required\n";
} else if ($code != strip_tags($code)
|| strpos($code, "'") !== false
|| strpos($code, '"') !== false
) {
$errors .= "Ontology Id " . $id . " - No Results Code has illegal characters - " . $code . "\n";
}
}
// test fhir urls settings
$fhirUrl = $fhirUrls[$key];
if (!$fhirUrl){
$errors .= "Ontology Id " . $id . " - FHIR API URL is required.\n";
}
else {
$strlen = strlen($fhirUrl);
if ('/' === $fhirUrl[$strlen - 1]) {
// remove trailing /
$fhirUrl = substr($fhirUrl, 0, $strlen - 1);
}
$headers = ['User-Agent: Redcap'];
$authType = $authTypes[$key];
if ($authType === 'basic') {
$authUser = $basicUserIds[$key];
$authPassword = $basicUserPasswords[$key];
$headers[] = 'Authorization: Basic ' . base64_encode($authUser . ':' . $authPassword);
}
$metadata = $this->httpGet($fhirUrl . '/metadata', $headers);
if ($metadata === FALSE) {
$errors .= "Ontology Id " . $ontologyIdValues[$key] . " - Failed to get metadata for fhir server at '" . $fhirUrl . "'\n";
}
if ($authType === 'cc') {
$authEndpoint = $authEndpoints[$key];
$clientId = $clientIds[$key];
$clientSecret = $clientSecrets[$key];
// get the access token
$params = array(
'grant_type' => 'client_credentials'
);
$headers = ['User-Agent: Redcap', 'Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret)];
try {
$response = $this->httpPost($authEndpoint, $params, 'application/x-www-form-urlencoded', $headers);
if ($response === false) {
$r = isset($http_response_header) ? implode("", $http_response_header) : '';
$errors .= "Ontology Id " . $id . " - Failed to get Authentication Token for fhir server at '" . $authEndpoint . "' response = false, r='" . $r . "'\n";
} else {
// a false or unparseable response decodes to null, and array_key_exists(null)
// is a fatal TypeError on PHP 8
$responseJson = is_string($response) ? json_decode($response, true) : null;
if (!is_array($responseJson)) {
$errors .= "Ontology Id " . $id . " - Failed to get Authentication Token for fhir server at '" . $authEndpoint . "' - no parseable response\n";
} else if (!array_key_exists('access_token', $responseJson)) {
$errors .= "Ontology Id " . $id . " - Failed to get Authentication Token for fhir server at '" . $authEndpoint . "'$response\n";
}
}
} catch (\Exception $e) {
$errors .= "Ontology Id " . $id . " - Failed to get Authentication Token for fhir server at '" . $authEndpoint . "' got exception $e\n";
}
}
}
// test valueset
$valueSetType = $valueSetTypes[$key];
$valueSetValue = $valuesets[$key];
if ($valueSetType === 'url'){
// valueset should not be blank.
if (!$valueSetValue){
$errors .= "Ontology Id " . $id . " - Valueset is required.";
}
} else if ($valueSetType === 'resource'){
// valueset should be valid json
$resource = json_decode($valueSetValue);
if (is_null($resource)) {
$errors .= "Ontology Id " . $id . " - Invalid JSon : " . json_last_error_msg() . "\n";
}
}
// test priority max fetch
$priorityMaxFetch = trim($priorityMaxFetches[$key]);
if ($priorityMaxFetch){
if (!!ctype_digit($priorityMaxFetch)){
$errors .= "Ontology Id " . $id . " - Priority Max Fetch should be an integer - " . $priorityMaxFetch . "\n";
}
}
}
return $errors;
}
/**
* return the name of the ontology service as it will be display on the service selection
* drop down.
*/
public function getProviderName()
{
return 'Advanced FHIR Ontologies';
}
/**
* return the prefex used to denote ontologies provided by this provider.
*/
public function getServicePrefix()
{
return 'ADVFHIR';
}
/**
* Search API with a search term for a given ontology
* Returns array of results with Notation as key and PrefLabel as value.
*/
public function searchOntology($category, $search_term, $result_limit)
{
$siteCategories = $this->getSubSettings('site-category-list');
$thisCategory = null;
foreach ($siteCategories as $cat) {
if ($cat['ontology-id'] === $category){
$thisCategory = $cat;
break;
}
}
$results = array();
// Set 20 as default limit
$result_limit = (is_numeric($result_limit) ? $result_limit : 20);
if ($thisCategory !== null){
$priorityFetchAdd = $thisCategory['priority-max-fetch'];
$fetchLimit = $result_limit + ($priorityFetchAdd ? (int)$priorityFetchAdd : 0);
$headers = ['User-Agent: Redcap'];
$fhirAuthType = $thisCategory['authentication-type'];
if ('cc' === $fhirAuthType){
$tokenEndpoint = $thisCategory['cc-token-endpoint'];
$clientId = $thisCategory['cc-client-id'];
$clientSecret = $thisCategory['cc-client-secret'];
$authToken = $this->getClientCredentialsToken($category, $tokenEndpoint, $clientId, $clientSecret);
if ($authToken !== false) {
$headers[] = 'Authorization: Bearer ' . $authToken;
}
}
elseif ('basic' === $fhirAuthType){
$userId = $thisCategory['basic-user-id'];
$userPassword = $thisCategory['basic-user-password'];
$headers[] = 'Authorization: Basic ' . base64_encode($userId . ':' . $userPassword);
}
$fhirServerUrl = $thisCategory['fhir-api-url'];
$valueSetType = $thisCategory['valueset-type'];
$valueSet = $thisCategory['valueset'];
$language = $thisCategory['fhir-display-language'];
if ('url' === $valueSetType) {
// Base URL + “/ValueSet/$expand?identifier=VS_ID&filter=SEARCH_TERM”
// need to escape the $expand in the url!
$expandParams = ['url' => $valueSet, 'filter' => $search_term, 'count' => $fetchLimit];
if (!empty($language)){
$expandParams['displayLanguage'] = $language;
}
$url = $fhirServerUrl . "/ValueSet/\$expand?" . http_build_query($expandParams);
$json = $this->httpGet($url, $headers);
}
else {
// valueset is json
$resource = json_decode($valueSet, true);
$contentType = "application/json";
$postData = [
"resourceType" => "Parameters",
"parameter" => [
["name" => "filter", "valueString" => $search_term],
["name" => "_count", "valueInteger" => $fetchLimit],
["name" => "valueSet", "resource" => $resource],
]
];
if (!empty($language)){
$postData['parameter'][] = ["name" => 'displayLanguage', "valueCode" => $language];
}
$postData = json_encode($postData, JSON_UNESCAPED_SLASHES);
$url = $fhirServerUrl . '/ValueSet/$expand';
$json = $this->httpPost($url, $postData, $contentType, $headers);
}
$codeTemplate = $thisCategory['code-template'];
$displayTemplate = $thisCategory['display-template'];
$templateKeys = ['${CODE}', '${SYSTEM}', '${DISPLAY}'];
$allPriorityCodes = $thisCategory['priority-codes'];
$priorityCodes = preg_split("/\r\n|\n|\r/", $allPriorityCodes);
$allBannedCodes = $thisCategory['banned-codes'];
$bannedCodes = preg_split("/\r\n|\n|\r/", $allBannedCodes);
// Parse the JSON into an array
$list = is_string($json) ? json_decode($json, true) : null;
if (is_array($list) && isset($list['expansion']['contains'])) {
$expansion = $list['expansion'];
// Loop through results
$core_results = array();
$key_results = array();
$hideChoice = $this->getHideChoice();
foreach ($expansion['contains'] as $this_item) {
// code, display and system are not guaranteed present by FHIR
$code = isset($this_item['code']) ? $this_item['code'] : '';
$display = isset($this_item['display']) ? $this_item['display'] : $code;
$system = isset($this_item['system']) ? $this_item['system'] : '';
if ('' === $code) {
// nothing storable without a code - skip rather than templating it in
continue;
}
if (in_array($code, $bannedCodes)){
// code is banned, skip it
continue;
}
if (in_array($code, $hideChoice)){
// code is in hide choide, skip it
continue;
}
$sortKey = array_search($code, $priorityCodes);
if ($sortKey === false){
$sortKey = count($priorityCodes);
}
$key_results[] = $sortKey;
$core_results[] = [$code, $system, $display];
}
// sort to put priority codes first
array_multisort($key_results, SORT_ASC, $core_results);
foreach ($core_results as $index=>$r) {
if ($index >= $result_limit){
// not interested in more results
break;
}
$finalCode = str_replace($templateKeys, $r, $codeTemplate);
$finalDisplay = str_replace($templateKeys, $r, $displayTemplate);
$results[$finalCode] = $finalDisplay;
}
}
}
if (!$results && $thisCategory !== null) {
// no results found - unknown category already returns empty above,
// nothing to fall back to
$return_no_result = $thisCategory['return-no-result'];
if ($return_no_result) {
$no_result_label = $thisCategory['no-result-label'];
$no_result_code = $thisCategory['no-result-code'];
$results[$no_result_code] = $no_result_label;
}
}
// Return array of results
return array_slice($results, 0, $result_limit, true);
}
function getHideChoice()
{
$codesToHide=[];
if (isset($_GET['field'])){
$field = $_GET['field'];
if (isset($Proj->metadata[$_GET['field']])) {
$annotations = $Proj->metadata[$field]['field_annotation'];
}
else if (isset($_GET['pid'])){
$project_id = $_GET['pid'];
$dd_array = \REDCap::getDataDictionary($project_id, 'array', false, array($field));
$annotations = $dd_array[$field]['field_annotation'];
}
if ($annotations) {
$offset = 0;
while (preg_match("/@HIDECHOICE='([^']*)'/", $annotations, $matches, PREG_OFFSET_CAPTURE, $offset) === 1){
$listedCodesStr = $matches[1][0];
$listedCodes = explode(',', $listedCodesStr);
foreach($listedCodes as $code){
array_push($codesToHide, trim($code));
}
$offset = $matches[0][1] + strlen($matches[0][0]);
}
}
}
return $codesToHide;
}
/**
* Return a string which will be placed in the online designer for
* selecting an ontology for the service.
* When an ontology is selected it should make a javascript call to
* update_ontology_selection($service, $category)
*
* The provider may include a javascript function
* <service>_ontology_changed(service, category)
* which will be called when the ontology selection is changed. This function
* would update any UI elements is the service matches or clear the UI elemements
* if they do not.
*/
public function getOnlineDesignerSection()
{
$siteCategories = $this->getSubSettings('site-category-list');
$categories = [];
$categoryList = '';
foreach ($siteCategories as $cat) {
$category = $cat['ontology-id'];
$name = $cat['ontology-name'];
$categoryList .= "<option value='{$category}'>{$name}</option>\n";
}
$onlineDesignerHtml = <<<EOD
<script type="text/javascript">
function ADVFHIR_ontology_changed(service, category){
var newSelection = ('ADVFHIR' === service) ? category : '';
$('#advfhir_ontology_category').val(newSelection);
}
</script>
<div style='margin-bottom:3px;'>
Select Advanced FHIR Ontology to use:
</div>
<select id='advfhir_ontology_category' name='advfhir_ontology_category'
onchange="update_ontology_selection('ADVFHIR', this.options[this.selectedIndex].value)"
class='x-form-text x-form-field' style='width:330px;max-width:330px;'>
{$categoryList}
</select>
EOD;
return $onlineDesignerHtml;
}
public function getLabelForValue($category, $value)
{
return $value;
}
/**
* Maximum number of seconds to wait on the FHIR server. Without a limit a slow
* or unavailable server holds a web server process open for the system default,
* which can exhaust the pool and take all of REDCap down with it.
*/
public function getFhirTimeout()
{
$timeout = $this->getSystemSetting('fhir-timeout');
if (is_numeric($timeout) && (int)$timeout > 0) {
return (int)$timeout;
}
return self::DEFAULT_TIMEOUT;
}
public function httpGet($fullUrl, $headers)
{
$timeout = $this->getFhirTimeout();
// if curl isn't install the default version of http_get in init_functions doesn't include the headers.
if (function_exists('curl_init') || empty($headers)) {
return http_get($fullUrl, $timeout, '', $headers, null);
}
if (ini_get('allow_url_fopen')) {
// Set http array for file_get_contents
$headerText = '';
foreach ($headers as $hvalue) {
$headerText .= $hvalue . "\r\n";
}
$http_array = array('method' => 'GET', 'header' => $headerText, 'timeout' => $timeout);
// If using a proxy
if (!sameHostUrl($fullUrl) && PROXY_HOSTNAME != '') {
$http_array['proxy'] = str_replace(array('http://', 'https://'), array('tcp://', 'tcp://'), PROXY_HOSTNAME);
$http_array['request_fulluri'] = true;
if (PROXY_USERNAME_PASSWORD != '') {
$proxy_auth = "Proxy-Authorization: Basic " . base64_encode(PROXY_USERNAME_PASSWORD);
if (isset($http_array['header'])) {
$http_array['header'] .= $proxy_auth . "\r\n";
} else {
$http_array['header'] = $proxy_auth . "\r\n";
}
}
}
// Use file_get_contents
$content = @file_get_contents($fullUrl, false, stream_context_create(array('http' => $http_array)));
} else {
$content = false;
}
// Return the response
return $content;
}
public function httpPost($fullUrl, $postData, $contentType, $headers)
{
$timeout = $this->getFhirTimeout();
// if curl isn't install the default version of http_post in init_functions doesn't include the headers.
// but the curl version will overwrite the content type header if other headers are included.
if (function_exists('curl_init') && !empty($headers)
&& $contentType && $contentType != 'application/x-www-form-urlencoded'){
$fullHeaders = $headers;
$fullHeaders[] = 'Content-type: '.$contentType;
return http_post($fullUrl, $postData, $timeout, $contentType, '', $fullHeaders);
}
else if (function_exists('curl_init') || empty($headers)) {
return http_post($fullUrl, $postData, $timeout, $contentType, '', $headers);
}
// If params are given as an array, then convert to query string format, else leave as is
if ($contentType == 'application/json') {
// Send as JSON data
$param_string = (is_array($postData)) ? json_encode($postData) : $postData;
} elseif ($contentType == 'application/x-www-form-urlencoded') {
// Send as Form encoded data
$param_string = (is_array($postData)) ? http_build_query($postData, '', '&') : $postData;
} else {
// Send params as is (e.g., Soap XML string)
$param_string = $postData;
}
if (ini_get('allow_url_fopen')) {
// Set http array for file_get_contents
// Set http array for file_get_contents
$headerText = '';
foreach ($headers as $hvalue) {
$headerText .= $hvalue . "\r\n";
}
$http_array = array('method' => 'POST',
'header' => "Content-type: $contentType" . "\r\n" . $headerText . "Content-Length: " . strlen($param_string) . "\r\n",
'content' => $param_string,
'timeout' => $timeout
);
// If using a proxy
if (!sameHostUrl($fullUrl) && PROXY_HOSTNAME != '') {
$http_array['proxy'] = str_replace(array('http://', 'https://'), array('tcp://', 'tcp://'), PROXY_HOSTNAME);
$http_array['request_fulluri'] = true;
if (PROXY_USERNAME_PASSWORD != '') {
$http_array['header'] .= "Proxy-Authorization: Basic " . base64_encode(PROXY_USERNAME_PASSWORD) . "\r\n";
}
}
// Use file_get_contents
$content = @file_get_contents($fullUrl, false, stream_context_create(array('http' => $http_array)));
// Return the content
if ($content !== false) {
return $content;
} // If no content, check the headers to see if it's hiding there (why? not sure, but it happens)
else {
$content = implode("", $http_response_header);
// If header is a true header, then return false, else return the content found in the header
return (substr($content, 0, 5) == 'HTTP/') ? false : $content;
}
}
return false;
}
public function getClientCredentialsToken($category, $tokenEndpoint, $clientId, $clientSecret)
{
$now = time();
$expireKey = 'ADVFHIR_' . $tokenEndpoint . '_TOKEN_EXPIRES';
$tokenKey = 'ADVFHIR_' . $tokenEndpoint . '_TOKEN';
if (array_key_exists($expireKey, $_SESSION) &&
array_key_exists($tokenKey, $_SESSION)) {
$expire = $_SESSION[$expireKey];
if ($now < $expire) {
// not expired.
return $_SESSION[$tokenKey];
}
}
// get the access token
$params = array(
'grant_type' => 'client_credentials'
);
$headers = ['User-Agent: Redcap', 'Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret)];
$clear = true;
try {
$response = $this->httpPost($tokenEndpoint, $params, 'application/x-www-form-urlencoded', $headers);
// a false or unparseable response decodes to null, and array_key_exists(null)
// is a fatal TypeError on PHP 8
$responseJson = is_string($response) ? json_decode($response, true) : null;
if (!is_array($responseJson)) {
error_log("Failed to negotiate auth token : no parseable response from " . $tokenEndpoint);
} elseif (array_key_exists('access_token', $responseJson)) {
$clear = false;
$_SESSION[$tokenKey] = $responseJson['access_token'];
// expires_in is SECONDS (RFC 6749) and $now is seconds - the previous
// * 1000 cached a 3600s token for roughly 41 days. Renew early by
// margin = min(60, floor(lifetime / 2)): a minute early for normal
// lifetimes, halfway through for very short ones, and never an expiry
// beyond the real one.
$lifetime = array_key_exists('expires_in', $responseJson)
? (int)$responseJson['expires_in']
: 3600;
if ($lifetime < 1) {
$lifetime = 1;
}
$margin = (int)min(60, floor($lifetime / 2));
$_SESSION[$expireKey] = $now + $lifetime - $margin;
} elseif (array_key_exists('error', $responseJson)) {
error_log("Failed to negotiate auth token : " . $responseJson['error'] . " - " . $responseJson['error_description']);
} else {
error_log("Failed to negotiate auth token : " . $response);
}
} catch (\Exception $e) {
$error_code = $e->getCode();
$error_message = $e->getMessage();
error_log("Failed to negotiate auth token : {$error_code} - {$error_message}");
}
if ($clear) {
unset($_SESSION[$expireKey]);
unset($_SESSION[$tokenKey]);
return false;
}
return $_SESSION[$tokenKey];
}
}