1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
Copyright (C) 2018-2020 KANOUN Salim |
4
|
|
|
This program is free software; you can redistribute it and/or modify |
5
|
|
|
it under the terms of the Affero GNU General Public v.3 License as published by |
6
|
|
|
the Free Software Foundation; |
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10
|
|
|
Affero GNU General Public Public for more details. |
11
|
|
|
You should have received a copy of the Affero GNU General Public Public along |
12
|
|
|
with this program; if not, write to the Free Software Foundation, Inc., |
13
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Validate the uploaded dicom, this script act as follow : |
18
|
|
|
* Unzip the recieved ZIPs |
19
|
|
|
* Send each dicom to Orthanc Exposed |
20
|
|
|
* Produce the Anonymize query in Orthanc Exposed |
21
|
|
|
* Delete the original import in Orthanc Exposed |
22
|
|
|
* Send the anonymized study in Orthanc PACS |
23
|
|
|
* Delete the anonymied dicom in Orthanc Exposed |
24
|
|
|
* Analyze the Orthanc PACS dicom and write DICOM details in database |
25
|
|
|
* Update Visit status and send email notifications |
26
|
|
|
* |
27
|
|
|
* Warning : The execution time of this script is long due to Orthanc heavy operations |
28
|
|
|
* Double check the timeout value that are set in Orthanc class and that database connexion is not timed up |
29
|
|
|
* Check also the max execution time (which is reset in Orthanc class) |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
require_once($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php'); |
34
|
|
|
|
35
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
36
|
|
|
|
37
|
|
|
Session::checkSession(); |
38
|
|
|
$linkpdo=Session::getLinkpdo(); |
39
|
|
|
|
40
|
|
|
$timeStamp = time(); |
41
|
|
|
$id_visit = $_POST['id_visit']; |
42
|
|
|
$nbOfInstances = $_POST['totalDicomFiles']; |
43
|
|
|
$anonFromOrthancId=$_POST['originalOrthancStudyID']; |
44
|
|
|
$username=$_SESSION['username']; |
45
|
|
|
$study=$_SESSION['study']; |
46
|
|
|
$role=$_SESSION['role']; |
47
|
|
|
$tusFilesID = $_POST['sucessIDsUploaded']; |
48
|
|
|
|
49
|
|
|
$unzipedPath = $_SERVER['DOCUMENT_ROOT'].'/data/upload/temp/'.$timeStamp.'_'.$id_visit; |
50
|
|
|
|
51
|
|
|
$visitObject=new Visit($id_visit, $linkpdo); |
52
|
|
|
$userObject=new User($username, $linkpdo); |
53
|
|
|
|
54
|
|
|
$accessCheck=$userObject->isVisitAllowed($id_visit, User::INVESTIGATOR); |
55
|
|
|
|
56
|
|
|
if ($accessCheck && $role == User::INVESTIGATOR && $visitObject->uploadStatus == Visit::NOT_DONE) { |
57
|
|
|
|
58
|
|
|
//Run as a background task even if the user leave the website |
59
|
|
|
ignore_user_abort(true); |
60
|
|
|
//Set Visit as upload processing status |
61
|
|
|
$visitObject->changeUploadStatus(Visit::UPLOAD_PROCESSING); |
62
|
|
|
$start_time=microtime(true); |
63
|
|
|
/** |
64
|
|
|
* Try block as each interruption of the proccess must make visit return as upload not done |
65
|
|
|
* To allow new upload |
66
|
|
|
*/ |
67
|
|
|
try { |
68
|
|
|
|
69
|
|
|
if (!is_dir($unzipedPath)) { |
70
|
|
|
mkdir($unzipedPath, 0755); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//Unzip each uploaded file and remove them from tus |
74
|
|
|
foreach($tusFilesID as $fileName){ |
75
|
|
|
$tempZipPath = get_tus_file($fileName); |
76
|
|
|
|
77
|
|
|
$zipSize=filesize($tempZipPath); |
78
|
|
|
$uncompressedzipSize=get_zip_originalsize($tempZipPath); |
79
|
|
|
if ($uncompressedzipSize/$zipSize > 50) { |
80
|
|
|
throw new Exception("Bomb Zip"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$zip=new ZipArchive; |
84
|
|
|
$zip->open($tempZipPath); |
85
|
|
|
$zip->extractTo($unzipedPath); |
86
|
|
|
$zip->close(); |
87
|
|
|
|
88
|
|
|
//Remove file from TUS and downloaded temporary zip |
89
|
|
|
delete_tus_file($fileName); |
90
|
|
|
unlink($tempZipPath); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
//Send unziped files to Orthanc |
97
|
|
|
$orthancExposedObject=new Orthanc(true); |
98
|
|
|
$importedMap=sendFolderToOrthanc($unzipedPath, $orthancExposedObject); |
|
|
|
|
99
|
|
|
|
100
|
|
|
//Anonymize, remove original and send anonymized to Orthanc PACS |
101
|
|
|
//Read imported map, it only have only one study |
102
|
|
|
foreach ($importedMap as $studyID=>$seriesIDs) { |
|
|
|
|
103
|
|
|
//Anonymize and store new anonymized study Orthanc ID |
104
|
|
|
$anonymizedIDArray[]=$orthancExposedObject->Anonymize($studyID, $visitObject->getVisitCharacteristics()->anonProfile, $visitObject->patientCode, $visitObject->visitType, $visitObject->study); |
105
|
|
|
//error_log("Anonymization done at ".(microtime(true)-$start_time)); |
106
|
|
|
//Delete original import |
107
|
|
|
$orthancExposedObject->deleteFromOrthanc("studies", $studyID); |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
//Send to Orthanc Pacs and fill the database |
111
|
|
|
$orthancExposedObject->sendToPeer("OrthancPacs", $anonymizedIDArray); |
112
|
|
|
//error_log("Peer sent at ".(microtime(true)-$start_time)); |
113
|
|
|
//erase transfered anonymized study from orthanc exposed |
114
|
|
|
$orthancExposedObject->deleteFromOrthanc("studies", $anonymizedIDArray[0]); |
115
|
|
|
|
116
|
|
|
//Fill the Orthanc study / series table |
117
|
|
|
//Reset the PDO object as the database connexion is likely to be timed out |
118
|
|
|
$linkpdo=Session::getLinkpdo(); |
119
|
|
|
$fillTable=new Fill_Orthanc_Table($visitObject->id_visit, $username, $linkpdo); |
120
|
|
|
$studyDetails=$fillTable->parseData($anonymizedIDArray[0]); |
121
|
|
|
|
122
|
|
|
//Check that nb on instances in Orthanc PACS still match the original number of sent instances |
123
|
|
|
if ($studyDetails['countInstances'] != $nbOfInstances) { |
124
|
|
|
throw new Exception("Error during Peer transfers"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//Fill Orthanc Tables in Database and update visit status |
128
|
|
|
$fillTable->fillDB($anonFromOrthancId); |
129
|
|
|
$logDetails['uploadedSeries']=$studyDetails['seriesInStudy']; |
130
|
|
|
$logDetails['patientNumber']=$visitObject->patientCode; |
131
|
|
|
$logDetails['visitType']=$visitObject->visitType; |
132
|
|
|
$logDetails['modality_visit']=$visitObject->visitGroupObject->groupModality; |
133
|
|
|
//Log import |
134
|
|
|
Tracker::logActivity($username, $role, $study, $visitObject->id_visit, "Upload Series", $logDetails); |
135
|
|
|
|
136
|
|
|
}catch (Throwable $e1) { |
137
|
|
|
$logDetails['patientNumber']=$visitObject->patientCode; |
138
|
|
|
$logDetails['visitType']=$visitObject->visitType; |
139
|
|
|
$logDetails['modality_visit']=$visitObject->visitGroupObject->groupModality; |
140
|
|
|
Tracker::logActivity($username, $role, $study, $visitObject->id_visit, "Upload Failure", $logDetails); |
141
|
|
|
|
142
|
|
|
error_log($e1->getMessage()); |
143
|
|
|
handleException($e1); |
144
|
|
|
header('HTTP/1.0 500 Internal Server Error'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
}else { |
149
|
|
|
header('HTTP/1.0 403 Forbidden'); |
150
|
|
|
die('You are not allowed to access this file.'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Send all folder content to Orthanc (recursively) |
156
|
|
|
* @param string $unzipedPath |
157
|
|
|
* @param Orthanc $orthancExposedObject |
158
|
|
|
* @throws Exception |
159
|
|
|
* @return mixed |
160
|
|
|
*/ |
161
|
|
|
function sendFolderToOrthanc(string $unzipedPath, Orthanc $orthancExposedObject) { |
162
|
|
|
|
163
|
|
|
global $nbOfInstances; |
164
|
|
|
//Recursive scann of the unzipped folder |
165
|
|
|
$rii=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($unzipedPath)); |
166
|
|
|
|
167
|
|
|
$files=array(); |
168
|
|
|
foreach ($rii as $file) { |
169
|
|
|
if ($file->isDir()) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
$files[]=$file->getPathname(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if(sizeof($files) != $nbOfInstances){ |
176
|
|
|
throw new Exception("Number Of Uploaded Files dosen't match expected instance number"); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$importedMap=null; |
180
|
|
|
$importedInstances=0; |
181
|
|
|
//$start_time=microtime(true); |
182
|
|
|
|
183
|
|
|
//Import dicom file one by one |
184
|
|
|
foreach ($files as $file) { |
185
|
|
|
$importAnswer=$orthancExposedObject->importFileGuzzle($file); |
186
|
|
|
if (!empty($importAnswer)) { |
187
|
|
|
$answerdetails=json_decode($importAnswer, true); |
188
|
|
|
$importedMap[$answerdetails['ParentStudy']][$answerdetails['ParentSeries']][]=$answerdetails['ID']; |
189
|
|
|
$importedInstances++; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
//Delete original file after import |
195
|
|
|
recursive_directory_delete($unzipedPath); |
196
|
|
|
|
197
|
|
|
//error_log("Imported ".$importedInstances." files in ".(microtime(true)-$start_time)); |
198
|
|
|
error_log('Imported Instances :'.$importedInstances); |
199
|
|
|
error_log('Announced number of Instances :'.$nbOfInstances); |
200
|
|
|
|
201
|
|
|
if (count($importedMap) == 1 && $importedInstances == $nbOfInstances) { |
|
|
|
|
202
|
|
|
return $importedMap; |
203
|
|
|
}else { |
204
|
|
|
//These error shall never occur |
205
|
|
|
if (count($importedMap) > 1) { |
206
|
|
|
throw new Exception("More than one study in Zip"); |
207
|
|
|
}else if ($importedInstances != $nbOfInstances) { |
208
|
|
|
throw new Exception("Imported DICOM not matching announced number of Instances"); |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Recursively delete unziped folder |
217
|
|
|
* @param string $directory |
218
|
|
|
*/ |
219
|
|
|
function recursive_directory_delete(string $directory) { |
220
|
|
|
$it=new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS); |
221
|
|
|
$it=new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); |
222
|
|
|
foreach ($it as $file) { |
223
|
|
|
if ($file->isDir()) rmdir($file->getPathname()); |
224
|
|
|
else unlink($file->getPathname()); |
225
|
|
|
} |
226
|
|
|
rmdir($directory); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* In case of a thrown exception, warn administrator and uploader and set upload to not done |
231
|
|
|
* @param Exception $e1 |
232
|
|
|
*/ |
233
|
|
|
function handleException(Throwable $e1) { |
234
|
|
|
global $visitObject; |
235
|
|
|
global $linkpdo; |
236
|
|
|
|
237
|
|
|
//If more than own study uploaded or difference of instance number an exception is thrown |
238
|
|
|
$visitObject->changeUploadStatus(Visit::NOT_DONE); |
239
|
|
|
warningAdminError($e1->getMessage(), $linkpdo); |
240
|
|
|
} |
241
|
|
|
/** |
242
|
|
|
* Warn supervisors and uploader that validation of uploaded DICOM has failed |
243
|
|
|
* @param string $errorMessage |
244
|
|
|
* @param PDO $linkpdo |
245
|
|
|
*/ |
246
|
|
|
function warningAdminError(string $errorMessage, PDO $linkpdo) { |
247
|
|
|
$sendEmails=new Send_Email($linkpdo); |
248
|
|
|
global $visitObject; |
249
|
|
|
global $unzipedPath; |
250
|
|
|
global $study; |
251
|
|
|
global $username; |
252
|
|
|
|
253
|
|
|
$sendEmails->addGroupEmails($study, User::SUPERVISOR)->addEmail($sendEmails->getUserEmails($username)); |
254
|
|
|
$sendEmails->sendUploadValidationFailure($visitObject->id_visit, $visitObject->patientCode, $visitObject->visitType, |
255
|
|
|
$study, $unzipedPath, $username, $errorMessage); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get uncompressed Size |
260
|
|
|
* @param string $filename |
261
|
|
|
* @return number |
262
|
|
|
*/ |
263
|
|
|
function get_zip_originalsize(string $filename) { |
264
|
|
|
$size=0; |
265
|
|
|
$resource=zip_open($filename); |
|
|
|
|
266
|
|
|
while ($dir_resource=zip_read($resource)) { |
|
|
|
|
267
|
|
|
$size+=zip_entry_filesize($dir_resource); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
zip_close($resource); |
|
|
|
|
270
|
|
|
|
271
|
|
|
return $size; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
function get_tus_file($fileName) { |
275
|
|
|
|
276
|
|
|
$client = new Client([ |
277
|
|
|
// Base URI is used with relative requests |
278
|
|
|
'base_uri' => TUS_SERVER.'/tus/', |
279
|
|
|
'headers' => ['Tus-Resumable' => '1.0.0'] |
280
|
|
|
]); |
281
|
|
|
$downloadedFileName = tempnam(sys_get_temp_dir(), 'dicom'); |
282
|
|
|
|
283
|
|
|
$resource = fopen( $downloadedFileName, 'r+'); |
284
|
|
|
|
285
|
|
|
$client->request('GET', $fileName, ['sink' => $resource]); |
286
|
|
|
|
287
|
|
|
return $downloadedFileName; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
function delete_tus_file($fileName){ |
291
|
|
|
|
292
|
|
|
$client = new Client([ |
293
|
|
|
// Base URI is used with relative requests |
294
|
|
|
'base_uri' => TUS_SERVER.'/tus/', |
295
|
|
|
'headers' => ['Tus-Resumable' => '1.0.0'] |
296
|
|
|
]); |
297
|
|
|
|
298
|
|
|
$client->request('DELETE', $fileName); |
299
|
|
|
|
300
|
|
|
//$code = $response->getStatusCode(); // 200 |
301
|
|
|
//$reason = $response->getReasonPhrase(); // OK |
302
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
|
306
|
|
|
|
307
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths