APIKey = new Models\APIKey(); $this->User = new Models\User(); if (!array_key_exists('apiKey', $this->request)) { throw new Exception('No API Key provided'); } else if (!$this->APIKey->verifyKey($this->request['apiKey'], $origin)) { throw new Exception('Invalid API Key'); } try { $this->DataInterface = new Models\DataInterface(); $this->AdminInterface = new Models\AdminInterface($this->DataInterface); $this->CtAdminInterface = new Models\CtAdminInterface($this->DataInterface); $this->AccountInterface = new Models\AccountInterface($this->DataInterface); $this->HomeInterface = new Models\HomeInterface($this->DataInterface); $this->ProfileInterface = new Models\ProfileInterface($this->DataInterface); $this->PatientInterface = new Models\PatientInterface($this->DataInterface); $this->AcquireInterface = new Models\AcquireInterface($this->DataInterface); $this->ReportInterface = new Models\ReportInterface($this->DataInterface); $this->MeasureInterface = new Models\MeasureInterface($this->DataInterface); } catch (Exception $e) { throw $e; } } /** * Get: * /api/v1/test/?apiKey= */ protected function test($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); switch($this->method) { case 'GET': return $this->DataInterface->test(); default: throw new Exception('Not implemented.'); } } /** * */ protected function ray($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) { throw new Exception('Permission denied.'); } if($this->method != 'POST') { throw new Exception('Not implemented.'); } // ray $headers = getallheaders(); $id_ray = 0; if(array_key_exists('RayID', $headers)) { $id_ray = $headers['RayID']; } // ip $ip = $_SERVER['REMOTE_ADDR']; $ip_type = 'direct'; if(array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; $ip_type = 'x_forwarded_for'; } if($ip == '::1') { $ip = ''; $ip_type = 'localhost'; } $ip_data = array( 'ip' => $ip, 'type' => $ip_type ); // location try { if($ip_type=='localhost') { $location_data = json_decode(file_get_contents("http://www.geoplugin.net/json.gp")); } else { $location_data = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=$ip")); } $location_data = array( 'city' => $location_data->geoplugin_city, 'continentCode' => $location_data->geoplugin_continentCode, 'continentName' => $location_data->geoplugin_continentName, 'countryCode' => $location_data->geoplugin_countryCode, 'countryName' => $location_data->geoplugin_countryName, 'currencyCode' => $location_data->geoplugin_currencyCode, 'latitude' => $location_data->geoplugin_latitude, 'longitude' => $location_data->geoplugin_longitude, 'timezone' => $location_data->geoplugin_timezone, 'request' => $location_data->geoplugin_request ); } catch(Exception $e) { $location_data = []; } // store if($id_ray != 0) { $result = $this->DataInterface->rayUpdate( $id_ray, $this->request['userAgent'], $this->request['apiKey'], $ip_data, $location_data ); } else { $result = $this->DataInterface->rayCreate( $this->request['userAgent'], $this->request['apiKey'], $ip_data, $location_data ); } $result['country'] = $location_data['countryCode']; return $result; } /** * */ protected function log($args, $verb) { // ray $headers = getallheaders(); $id_ray = 0; if(array_key_exists('RayID', $headers)) { $id_ray = $headers['RayID']; try { $chkToken = $this->User->checkToken(); } catch(Exception $e) { $chkToken = 'denied'; } $user_data = array( 'ID' => $this->User->ID, 'firstname' => $this->User->firstname, 'lastname' => $this->User->lastname, 'email' => $this->User->email, 'token' => $chkToken ); $activity_data = array( 'method' => $this->method, 'endpoint' => $this->endpoint, 'args' => $args, 'verb' => $verb, 'request' => $this->request ); $this->DataInterface->auditLog($id_ray, $user_data, $activity_data); } } /** * Post: * /api/v1/mailer/send/ with data {from:from,to:to,subject:subject,message:message,apiKey:apiKey} */ protected function mailer($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); switch($this->method) { case 'POST': if ($verb == 'send') { return $this->DataInterface->sendMail( $this->request['from'], $this->request['to'], $this->request['subject'], $this->request['message'], null ); } else { throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * Put: * /api/v1/upload/?apiKey= */ protected function upload($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = array(); try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, array("newToken" => $chkToken)); } } catch(Exception $e) { return array('result' => 'ERROR', 'reason' => 'denied'); } switch($this->method) { case 'PUT': // Fetch content and determine boundary $raw_data = $this->file; $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n")); // Fetch each part $parts = array_slice(explode($boundary, $raw_data), 1); $data = array(); $files = array(); foreach ($parts as $part) { // If this is the last part, break if ($part == "--\r\n") break; // Separate content from headers $part = ltrim($part, "\r\n"); list($raw_headers, $body) = explode("\r\n\r\n", $part, 2); // Parse the headers list $raw_headers = explode("\r\n", $raw_headers); $headers = array(); foreach ($raw_headers as $header) { list($name, $value) = explode(':', $header); $headers[strtolower($name)] = ltrim($value, ' '); } // Parse the Content-Disposition to get the field name, etc. if (isset($headers['content-disposition'])) { $filename = null; preg_match( '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', $headers['content-disposition'], $matches ); list(, $type, $name) = $matches; isset($matches[4]) and $filename = $matches[4]; $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); $baseDir = $_SERVER['DOCUMENT_ROOT']."/storage/user"; $filename = \Tools\UUID::v4().'.'.$ext; // handle your fields here switch ($name) { // this is a file upload case 'file': $ID = $this->User->ID; // Convert PDF if($ext == 'pdf') { $pdfFile = "$baseDir/$filename"; file_put_contents($pdfFile, $body); $im = new \Imagick(); $im->setResolution( 300, 300 ); $im->readImage($pdfFile); $im->setImageFormat('jpeg'); $im->setImageCompressionQuality(100); $num_pages = $im->getNumberImages(); for($i = 0;$i < $num_pages; $i++) { // New filename $filename = \Tools\UUID::v4().'.jpg'; $prefix = substr($filename, 0, 2); $baseDir = $_SERVER['DOCUMENT_ROOT']."/storage/user/$ID/image/$prefix"; \Tools\FS::mkpath($baseDir); // Write file $im->setIteratorIndex($i); $files[] = $filename; $im->writeImage("$baseDir/$filename"); } $im->clear(); $im->destroy(); // Delete PDF unlink($pdfFile); } // Store image directly else if(in_array($ext, array('jpeg', 'jpg', 'png'))) { // New filename $filename = \Tools\UUID::v4().'.'.$ext; $prefix = substr($filename, 0, 2); $baseDir = $_SERVER['DOCUMENT_ROOT']."/storage/user/$ID/image/$prefix"; \Tools\FS::mkpath($baseDir); // Write file file_put_contents("$baseDir/$filename", $body); $files[] = $filename; } else { return array( 'result' => 'ERROR', 'reason' => 'invalid_input', 'message' => $ext ); } break; // default for all other files is to populate $data default: $data[$name] = substr($body, 0, strlen($body) - 2); break; } } } return array_merge($resArray, array('result' => 'OK', 'files' => $files)); default: throw new Exception('Not implemented.'); } } /** * */ protected function admin_($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); switch($this->method) { case 'GET': switch($verb) { case 'signout': return $this->AdminInterface->adminLogout($this->User); case 'signin': return $this->AdminInterface->adminLogin($this->User, $args[0], $args[1]); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * */ protected function admin($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = array(); try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, array("newToken" => $chkToken)); } } catch(Exception $e) { return array('result' => 'ERROR', 'reason' => 'denied'); } switch($this->method) { case 'GET': switch($verb) { case 'profile': return array_merge($resArray, $this->AdminInterface->adminProfileGet($this->User)); case 'common': return array_merge($resArray, $this->AdminInterface->adminCommonGet($this->User)); case 'credit': return array_merge($resArray, $this->AdminInterface->adminCreditGet($this->User, $args[0])); case 'customer': return array_merge($resArray, $this->AdminInterface->adminCustomerGet($this->User, $args[0])); case 'ctparams': return array_merge($resArray, $this->AdminInterface->adminCtParamsGet($this->User)); case 'ctusers': return array_merge($resArray, $this->AdminInterface->adminCtUsersGet($this->User)); case 'ctstats': return array_merge($resArray, $this->AdminInterface->adminCtStatsGet($this->User)); case 'phystats': return array_merge($resArray, $this->AdminInterface->adminPhyStatsGet($this->User)); case 'pacs': return array_merge($resArray, $this->AdminInterface->adminPacsGet($this->User, $args[0])); case 'export': return array_merge($resArray, $this->AdminInterface->adminExportGet($this->User)); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'common': return array_merge($resArray, $this->AdminInterface->adminCommonPost($this->User, $this->request)); case 'credit': return array_merge($resArray, $this->AdminInterface->adminCreditPost($this->User, $this->request)); case 'customer': return array_merge($resArray, $this->AdminInterface->adminCustomerPost($this->User, $this->request)); case 'pacs': return array_merge($resArray, $this->AdminInterface->adminPacsPost($this->User, $this->request)); case 'echo': return array_merge($resArray, $this->AdminInterface->adminEchoPost($this->User, $this->request)); case 'export': return array_merge($resArray, $this->AdminInterface->adminExportPost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * */ protected function ct_admin_($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); switch($this->method) { case 'GET': switch($verb) { case 'signout': return $this->CtAdminInterface->ctAdminLogout($this->User); case 'signin': return $this->CtAdminInterface->ctAdminLogin($this->User, $args[0], $args[1]); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * */ protected function ct_admin_account($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = array(); switch($this->method) { case 'GET': switch($verb) { default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'password': return array_merge($resArray, $this->CtAdminInterface->ctAdminPasswordPost($this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * */ protected function ct_admin($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = array(); try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, array("newToken" => $chkToken)); } } catch(Exception $e) { return array('result' => 'ERROR', 'reason' => 'denied'); } switch($this->method) { case 'GET': switch($verb) { case 'profile': return array_merge($resArray, $this->CtAdminInterface->ctAdminProfileGet($this->User)); case 'cros': return array_merge($resArray, $this->CtAdminInterface->ctAdminCROsGet($this->User)); case 'users': return array_merge($resArray, $this->CtAdminInterface->ctAdminUsersGet($this->User, $args[0])); case 'centers': return array_merge($resArray, $this->CtAdminInterface->ctAdminCentersGet($this->User)); case 'settings': return array_merge($resArray, $this->CtAdminInterface->ctAdminSettingsGet($this->User)); case 'patients': return array_merge($resArray, $this->CtAdminInterface->ctAdminPatientsGet($this->User, $args[0])); case 'investigators': return array_merge($resArray, $this->CtAdminInterface->ctAdminInvestigatorsGet($this->User, $args[0])); case 'readers': return array_merge($resArray, $this->CtAdminInterface->ctAdminReadersGet($this->User, $args[0])); case 'overview': return array_merge($resArray, $this->CtAdminInterface->ctAdminOverviewGet($this->User, $args[0])); case 'visits': return array_merge($resArray, $this->CtAdminInterface->ctAdminVisitsGet($this->User, $args[0])); case 'pacs': return array_merge($resArray, $this->CtAdminInterface->ctAdminPacsGet($this->User)); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'settings': return array_merge($resArray, $this->CtAdminInterface->ctAdminSettingsPost($this->User, $this->request)); case 'centers': return array_merge($resArray, $this->CtAdminInterface->ctAdminCentersPost($this->User, $this->request)); case 'users': return array_merge($resArray, $this->CtAdminInterface->ctAdminUsersPost($this->User, $this->request)); case 'cros': return array_merge($resArray, $this->CtAdminInterface->ctAdminCROsPost($this->User, $this->request)); case 'pacs': return array_merge($resArray, $this->CtAdminInterface->ctAdminPacsPost($this->User, $this->request)); case 'echo': return array_merge($resArray, $this->CtAdminInterface->ctAdminEchoPost($this->User, $this->request)); case 'auditlog': return array_merge($resArray, $this->CtAdminInterface->ctAdminAuditLogPost($this->User, $this->request)); case 'ecrf': return array_merge($resArray, $this->CtAdminInterface->ctAdminAuditECRFPost($this->User, $this->request)); case 'reader': return array_merge($resArray, $this->CtAdminInterface->ctAdminReaderPost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/profile/... */ protected function profile($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = []; try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, ["newToken" => $chkToken]); } else { return ['result' => 'ERROR', 'reason' => 'denied']; } } catch(Exception $e) { return ['result' => 'ERROR', 'reason' => 'denied']; } switch($this->method) { case 'GET': switch($verb) { case '': return array_merge($resArray, $this->ProfileInterface->profileGet($this->User, $this->request['lang'])); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case '': return array_merge($resArray, $this->ProfileInterface->profilePost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/home/... */ protected function home($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = []; try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, ["newToken" => $chkToken]); } else { return ['result' => 'ERROR', 'reason' => 'denied']; } } catch(Exception $e) { return ['result' => 'ERROR', 'reason' => 'denied']; } switch($this->method) { case 'GET': switch($verb) { case 'export': return array_merge($resArray, $this->AdminInterface->exportByID($this->User->ID)); case '': return array_merge($resArray, $this->HomeInterface->homeGet($this->User)); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/patient/... */ protected function patient($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = []; try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, ["newToken" => $chkToken]); } else { return ['result' => 'ERROR', 'reason' => 'denied']; } } catch(Exception $e) { return ['result' => 'ERROR', 'reason' => 'denied']; } switch($this->method) { case 'GET': switch($verb) { case 'files-existing': return array_merge($resArray, $this->PatientInterface->patientFilesExistingGet($this->User)); case 'files-new': return array_merge($resArray, $this->PatientInterface->patientFilesNewGet($this->User, $this->request['lang'])); case 'files-pacs': return array_merge($resArray, $this->PatientInterface->patientFilesPacsGet($this->User)); case 'fix': return array_merge($resArray, $this->PatientInterface->patientFixGet($this->User, $this->args[0], $this->args[1])); case 'risks': return array_merge($resArray, $this->PatientInterface->patientRisksGet($this->User, $this->args[0])); case 'history': return array_merge($resArray, $this->PatientInterface->patientHistoryGet($this->User, $this->args[0], $this->args[1])); case 'family': return array_merge($resArray, $this->PatientInterface->patientFamilyGet($this->User, $this->args[0])); case 'examination': return array_merge($resArray, $this->PatientInterface->patientExaminationGet($this->User, $this->args[0])); case 'treatments': return array_merge($resArray, $this->PatientInterface->patientTreatmentsGet($this->User, $this->args[0])); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'files-existing': return array_merge($resArray, $this->PatientInterface->patientFilesExistingPost($this->User, $this->request)); case 'create': return array_merge($resArray, $this->PatientInterface->patientCreatePost($this->User, $this->request)); case 'modify': return array_merge($resArray, $this->PatientInterface->patientModifyPost($this->User, $this->request)); case 'create-visit': return array_merge($resArray, $this->PatientInterface->patientCreateVisitPost($this->User, $this->request)); case 'context': return array_merge($resArray, $this->PatientInterface->patientContextPost($this->User, $this->request)); case 'pacs-query': return array_merge($resArray, $this->PatientInterface->patientPacsQueryPost($this->User, $this->request)); case 'pacs-retrieve': return array_merge($resArray, $this->PatientInterface->patientPacsRetrievePost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/report/... */ protected function report($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = []; try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, ["newToken" => $chkToken]); } else { return ['result' => 'ERROR', 'reason' => 'denied']; } } catch(Exception $e) { return ['result' => 'ERROR', 'reason' => 'denied']; } switch($this->method) { case 'GET': switch($verb) { case '': return array_merge($resArray, $this->ReportInterface->reportGet($this->User, $this->args[0], $this->args[1])); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'mail-add': return array_merge($resArray, $this->ReportInterface->reportMailAddPost($this->User, $this->request)); case 'mail-delete': return array_merge($resArray, $this->ReportInterface->reportMailDeletePost($this->User, $this->request)); case 'pdf-download': return array_merge($resArray, $this->ReportInterface->reportPdfDownloadPost($this->User, $this->request)); case 'pdf-pacs': return array_merge($resArray, $this->ReportInterface->reportPdfPACSPost($this->User, $this->request)); case 'pdf-mail': return array_merge($resArray, $this->ReportInterface->reportPdfMailPost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/acquire/... */ protected function acquire($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = []; try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, ["newToken" => $chkToken]); } else { return ['result' => 'ERROR', 'reason' => 'denied']; } } catch(Exception $e) { return ['result' => 'ERROR', 'reason' => 'denied']; } switch($this->method) { case 'GET': switch($verb) { case 'media': return array_merge($resArray, $this->AcquireInterface->acquireMediaGet($this->User, $this->args[0], $this->args[1])); case 'download': return array_merge($resArray, $this->AcquireInterface->acquireDownloadGet($this->User, $this->args[0], $this->args[1])); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'area': return array_merge($resArray, $this->AcquireInterface->acquireAreaPost($this->User, $this->request)); case 'upload': return array_merge($resArray, $this->AcquireInterface->acquireUploadPost($this->User, $this->request)); case 'lesion': return array_merge($resArray, $this->AcquireInterface->acquireLesionPost($this->User, $this->request)); case 'lesionDelete': return array_merge($resArray, $this->AcquireInterface->acquireLesionDeletePost($this->User, $this->request)); case 'media': return array_merge($resArray, $this->AcquireInterface->acquireMediaPost($this->User, $this->request)); case 'delete': return array_merge($resArray, $this->AcquireInterface->acquireDeletePost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/measure/... */ protected function measure($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); $resArray = []; try { $chkToken = $this->User->checkToken(); if($chkToken !== false) { $resArray = array_merge($resArray, ["newToken" => $chkToken]); } else { return ['result' => 'ERROR', 'reason' => 'denied']; } } catch(Exception $e) { return ['result' => 'ERROR', 'reason' => 'denied']; } switch($this->method) { case 'GET': switch($verb) { case '': return array_merge($resArray, $this->MeasureInterface->measureGet($this->User, $this->args[0], $this->args[1])); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'calibration': return array_merge($resArray, $this->MeasureInterface->measureCalibrationPost($this->User, $this->request)); case 'distance': return array_merge($resArray, $this->MeasureInterface->measureDistancePost($this->User, $this->request)); case 'area': return array_merge($resArray, $this->MeasureInterface->measureAreaPost($this->User, $this->request)); case 'imt': return array_merge($resArray, $this->MeasureInterface->measureImtPost($this->User, $this->request)); case 'plaque': return array_merge($resArray, $this->MeasureInterface->measurePlaquePost($this->User, $this->request)); case 'complete': return array_merge($resArray, $this->MeasureInterface->measureCompletePost($this->User, $this->request)); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } /** * /api/v1/account/... */ protected function account($args, $verb) { if( !$this->APIKey->isGranted(__FUNCTION__, $this->method) ) throw new Exception('Permission denied.'); switch($this->method) { case 'GET': switch($verb) { case 'signup': return $this->AccountInterface->accountSignupGet($this->request['lang']); case 'activate': return $this->AccountInterface->accountActivateGet($this->request['activation_token']); case 'signout': return $this->AccountInterface->accountLogoutGet($this->User); default: throw new Exception('Not implemented.'); } case 'POST': switch($verb) { case 'signup': return $this->AccountInterface->accountSignupPost($this->request); case 'signin': return $this->AccountInterface->accountSigninPost($this->User, $this->request); case 'reset': return $this->AccountInterface->accountResetPost($this->request); case 'reset2': return $this->AccountInterface->accountReset2Post($this->request); default: throw new Exception('Not implemented.'); } default: throw new Exception('Not implemented.'); } } } ?>