New and updated version of old code.

This commit is contained in:
2024-05-15 21:24:05 +02:00
commit cc1038047e
11 changed files with 448 additions and 0 deletions

25
api/discoverScanners.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
function discoverScanners()
{
$scannersList = shell_exec('sudo scanimage -f "%d|%v %m"');
if($scannersList != "")
{
session_start();
$_SESSION["ScannersList"][0] = explode('|', $scannersList);
return true;
}
else return false;
}
// TODO / DEBUG - Fix this function
if(discoverScanners() === true) echo "OK";
else echo "Failed";
// TEST code
// session_start();
// $_SESSION["ScannersList"][0] = [ "xerox_mfp:libusb:003:010", "Samsung M2070" ];
// echo "OK";
?>

26
api/imageDownload.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
if (isset($_GET['filename']))
{
require("../include/appConfig.php");
// Strip only file name form user string so he do not download system configs or whatnot
$file = $appConfig['imagesLocation'].basename($_GET['filename']);
if(!file_exists($file)) die('404 - File not found.');
else
{
// Set appropriate headers
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".basename($_GET['filename']));
header("Content-Type: ".mime_content_type($file));
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
}
else echo "404 - File name not found.";
?>

9
api/rediscover.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
session_start();
session_unset();
session_destroy();
header("Location: ../index.php");
exit;
?>

126
api/scan.php Normal file
View File

@@ -0,0 +1,126 @@
<?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;
?>