<?php

$accession_number = trim($_GET["accession_number"]);
$dicomdir = "/tmp/".uniqid("dicomdir_");

if(strlen($accession_number)==0) {
   echo "Accession Number Error";
   exit(0);
}

// $accession_number = "MR.180505.026";

//header("Content-type: application/octet-stream");
//header('Content-Disposition: attachment; filename="'.$accession_number.'.iso"');

mkdir($dicomdir);
mkdir("$dicomdir/DICOMDIR");

$cmd = "/bin/cp -r /var/www/html/microdicom/* ${dicomdir}/";
exec($cmd, $outputRes);


/*
    CR  - Computed Radiography Image Storage
    CT  - CT Image Storage
    MR  - MRImageStorage
    US  - Ultrasound Image Storage
    NM  - Nuclear Medicine Image Storage
    PET - PET Image Storage
    SC  - Secondary Capture Image Storage
    XA  - XRay Angiographic Image Storage
    XRF - XRay Radiofluoroscopic Image Storage
    DX  - Digital X-Ray Image Storage for Presentation
    MG  - Digital Mammography X-Ray Image Storage for Presentation
    PR  - Grayscale Softcopy Presentation State Storage
    KO  - Key Object Selection Document Storage
    SR  - Basic Text Structured Report Document Storage
*/

$modalities["CR"] = 1;
$modalities["CT"] = 1;
$modalities["MR"] = 1;
$modalities["US"] = 1;
$modalities["NM"] = 1;
$modalities["PET"] = 1;
$modalities["SC"] = 1;
$modalities["XA"] = 1;
$modalities["XRF"] = 1;
$modalities["DX"] = 1;
$modalities["MG"] = 1;
$modalities["PR"] = 1;
$modalities["KO"] = 1;
$modalities["SR"] = 1;



foreach($modalities as $cstore=>$v) {
   $cmd = "JAVA_HOME=/usr/lib/jvm/jdk1.8.0_144 LANG=en_US.iso-8859-1 /usr/local/dcm4che/dcm4che2/bin/dcmqr -L CDRECORD:10104 ABPACS@localhost:11112 -cmove CDRECORD -qAccessionNumber=${accession_number} -cstore $cstore -cstoredest $dicomdir/DICOMDIR";
   exec($cmd, $outputRes);
}

//$cmd = "JAVA_HOME=/usr/lib/jvm/jdk1.7.0_80 LANG=en_US.iso-8859-1 /usr/local/dcm4che/dcm4che2/bin/dcmsend DCMSERVER@172.16.0.120:104 $dicomdir/DICOMDIR/";
$cmd = "/usr/bin/dcmsend +sd +rd 172.16.0.120 104 $dicomdir/DICOMDIR";
exec($cmd, $outputRes);

/*$cmd = "/bin/rm -f ${accession_number}.iso";
exec($cmd, $outputRes);

$cmd = "/usr/bin/genisoimage -iso-level 4 -r -allow-multidot -allow-lowercase -allow-leading-dots -V DICOM -o ${accession_number}.iso $dicomdir";
exec($cmd, $outputRes);

$cmd = "/bin/rm -rf $dicomdir";
exec($cmd, $outputRes);

readfile("${accession_number}.iso");
exit(0);*/
