Files
saneAdmin/api/scan.php

126 lines
2.5 KiB
PHP

<?php
require("../include/appConfig.php");
session_start();
////
//
/// Functions
//
////
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function checkInput()
{
global $appConfig;
$checkOK = true;
if (isset($_GET['outputImageResolution']))
{
$found = false;
foreach ($appConfig['outputImageResolution'] as $item)
{
if($item[0] == $_GET['outputImageResolution']) $found = true;
}
if(!$found) $checkOK = false;
}
else $checkOK = false;
if (isset($_GET['outputImageColorDepth']))
{
$found = false;
foreach ($appConfig['outputImageColorDepth'] as $item)
{
if($item[0] == $_GET['outputImageColorDepth']) $found = true;
}
if(!$found) $checkOK = false;
}
else $checkOK = false;
if(isset($_GET['outputFileFormat']))
{
$found = false;
foreach ($appConfig['outputImageFormats'] as $item)
{
if($item[0] == $_GET['outputFileFormat']) $found = true;
}
if(!$found) $checkOK = false;
}
else $checkOK = false;
if (isset($_GET['inputPageSize']))
{
$found = false;
foreach ($appConfig['inputPageSizes'] as $item)
{
if($item[0] == $_GET['inputPageSize']) $found = true;
}
if(!$found) $checkOK = false;
}
else $checkOK = false;
if (isset($_GET['deviceID']))
{
$found = false;
foreach ($_SESSION['ScannersList'] as $item)
{
if($item[0] == $_GET['deviceID']) $found = true;
}
if(!$found) $checkOK = false;
}
else $checkOK = false;
return $checkOK;
}
function getNewFileName()
{
return 'scan-'.date('Ymd').'-'.generateRandomString().'.'.$_GET['outputFileFormat'];
}
////
//
/// Logic
//
////
if(checkInput() !== true)
{
echo "400 - Bad request";
http_response_code(400);
exit;
}
//Generate file name for scanned image
$filename = getNewFileName();
$scanCommand = "sudo scanimage --device-name ".$_GET['deviceID']." --format ".$_GET['outputFileFormat']." --resolution ".$_GET['outputImageResolution']." --mode ".$_GET['outputImageColorDepth']." > ".$appConfig['imagesLocation'].$filename;
////
//
/// Scanning start
//
////
//Prepare directory for scanned image
if(!is_dir($appConfig['imagesLocation'])) mkdir($appConfig['imagesLocation'], 777, true);
//Scan image
shell_exec($scanCommand);
//Output scanned image name indicating that scanning finished
echo $filename;
?>