Curl command in php to post LUA commands

291 views
Skip to first unread message

Julio César Luciano da Silva

unread,
Jul 6, 2021, 1:18:29 PM7/6/21
to Orthanc Users
Hi, I'm trying to play a POST command to a lua script, but I get a "bool(false)" response on my system, any help on what's wrong?
$ curl -X POST http://localhost:8042/tools/execute-script --data-binary @script.lua





<?php

$orthanc = "http://localhsot:8042/";
$url = $orthanc.'/tools/execute-script';

$uploadFilePath = __DIR__ . '/script.lua';
//$uploadFilePath="@$file[tmp_name]";


if (!file_exists($uploadFilePath)) {
    throw new Exception('File not found: ' . $uploadFilePath);
}

$uploadFileMimeType = mime_content_type($uploadFilePath);
$uploadFilePostKey = 'file';

$uploadFile = new CURLFile(
    $uploadFilePath,
    $uploadFileMimeType,
    $uploadFilePostKey
);

$curlHandler = curl_init();

curl_setopt_array($curlHandler, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,

    /**
     * Specify POST method
     */
    CURLOPT_POST => true,

    /**
     * Specify array of form fields
     */
    CURLOPT_POSTFIELDS => [
        $uploadFilePostKey => $uploadFile,
    ],
]);

$response = curl_exec($curlHandler);

curl_close($curlHandler);

echo($response);

Sébastien Jodogne

unread,
Jul 6, 2021, 1:57:25 PM7/6/21
to Orthanc Users
Hello,

You should *not* use "CURLFile()" as the argument to "CURLOPT_POSTFIELDS".

Instead, the "CURLOPT_POSTFIELDS" must simply contain a plain string containing your Lua script. For instance:

Also, make sure that the configuration option "ExecuteLuaEnabled" of Orthanc is set to "true":

Julio César Luciano da Silva

unread,
Jul 6, 2021, 3:23:05 PM7/6/21
to Orthanc Users
Thanks Sebastian , I've changed my code to the following structure: although it's not clear to me if it's really pure script injection, or if I have to have a file, my system still doesn't work.
This has been confirmed: "ExecuteLuaEnabled" of Orthanc is set to "true"

<?php

$orthanc = "http://localhsot:8042/";
$url = $orthanc.'tools/execute-script';
//echo $url;
....

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

//script lua
$data = "function OnStoredInstance(instanceId, tags, metadata)
  if tags['Modality'] == 'DX' then
      SendToModality(instanceId, 'gepacs')
  end
  if tags['Modality'] == 'MR' then
      SendToModality(instanceId, 'gepacs')
  end   
  if tags['Modality'] == 'CT' then
      SendToModality(instanceId, 'gepacs')
  end   
end";

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

?>
Reply all
Reply to author
Forward
0 new messages