-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from philippderdiedas/pull-request
Big update (second try)
- Loading branch information
Showing
18 changed files
with
466 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
<?php | ||
include_once(__DIR__."/lib/lib.php"); | ||
|
||
if ($_SERVER['REQUEST_METHOD'] == 'GET') { | ||
|
||
|
||
if(array_key_exists("file", $_GET)) { | ||
$file = $_GET["file"]; | ||
if(str_contains($file, "..") || str_contains($file, "/")) { | ||
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK"); | ||
die("Error: Dont't be evil!"); | ||
} | ||
$filename="/scans/".$file; | ||
if(file_exists($filename)) { | ||
header("Content-type:application/pdf"); | ||
header("Content-Disposition:attachment;filename=\"scan.pdf\""); | ||
readfile($filename); | ||
$filename = $_GET["file"]; | ||
$filepath=$SCANS_DIR . $filename; | ||
if(file_exists($filepath) && is_sub_path($filepath, $SCANS_DIR)) { | ||
header("Content-type: " . (mime_content_type($filepath) || 'application/octet-stream')); | ||
header("Content-Disposition: attachment; filename=\"" . $filename . "\""); | ||
readfile($filepath); | ||
} else { | ||
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK"); | ||
http_response_code(404); | ||
die("Error: File does not exist!"); | ||
} | ||
} else { | ||
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK"); | ||
http_response_code(400); | ||
die("Error: No file provided!"); | ||
} | ||
} else { | ||
http_response_code(405); | ||
die("Error: Method not allowed!"); | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
include_once(__DIR__."/listfiles.php"); | ||
include_once(__DIR__."/subdircheck.php"); | ||
|
||
// constants | ||
$SCANS_DIR = "/scans"; | ||
$SCRIPTS_DIR = "/opt/brother/scanner/brscan-skey/script"; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
// Define constants for sorting flags | ||
define('GETFILELIST_SORT_NAME_ASC', 1); | ||
define('GETFILELIST_SORT_NAME_DESC', 2); | ||
define('GETFILELIST_SORT_CREATEDATE_ASC', 4); | ||
define('GETFILELIST_SORT_CREATEDATE_DESC', 8); | ||
define('GETFILELIST_SORT_MODIFYDATE_ASC', 16); | ||
define('GETFILELIST_SORT_MODIFYDATE_DESC', 32); | ||
define('GETFILELIST_SORT_DIRNAME_ASC', 64); | ||
define('GETFILELIST_SORT_DIRNAME_DESC', 128); | ||
|
||
function getFileList($path, $sortFlags = GETFILELIST_SORT_NAME_ASC) { | ||
// Check if the directory exists | ||
if (!is_dir($path)) { | ||
return []; | ||
} | ||
|
||
$files = []; | ||
|
||
// Helper function to sort by various criteria | ||
$sortFunction = function ($a, $b) use ($sortFlags) { | ||
if ($sortFlags & GETFILELIST_SORT_NAME_ASC || $sortFlags & GETFILELIST_SORT_NAME_DESC) { | ||
return strnatcasecmp($a, $b); | ||
} elseif ($sortFlags & GETFILELIST_SORT_CREATEDATE_ASC) { | ||
return filectime($a) - filectime($b); | ||
} elseif ($sortFlags & GETFILELIST_SORT_CREATEDATE_DESC) { | ||
return filectime($b) - filectime($a); | ||
} elseif ($sortFlags & GETFILELIST_SORT_MODIFYDATE_ASC) { | ||
return filemtime($a) - filemtime($b); | ||
} elseif ($sortFlags & GETFILELIST_SORT_MODIFYDATE_DESC) { | ||
return filemtime($b) - filemtime($a); | ||
} elseif ($sortFlags & GETFILELIST_SORT_DIRNAME_ASC) { | ||
return strnatcasecmp(dirname($a), dirname($b)); | ||
} elseif ($sortFlags & GETFILELIST_SORT_DIRNAME_DESC) { | ||
return strnatcasecmp(dirname($b), dirname($a)); | ||
} | ||
return strnatcasecmp($a, $b); // Default sort by name | ||
}; | ||
|
||
// Helper function to recursively iterate over directories | ||
$iterateDir = function ($dir) use (&$files, &$iterateDir, $sortFlags, $sortFunction) { | ||
$contents = scandir($dir); | ||
if ($contents === false) { | ||
return; | ||
} | ||
foreach ($contents as $item) { | ||
if ($item == '.' || $item == '..') { | ||
continue; | ||
} | ||
$fullPath = $dir . DIRECTORY_SEPARATOR . $item; | ||
if (is_dir($fullPath)) { | ||
$iterateDir($fullPath); | ||
} else { | ||
$files[] = $fullPath; | ||
} | ||
} | ||
}; | ||
|
||
$iterateDir($path); | ||
|
||
// Sorting | ||
usort($files, $sortFunction); | ||
|
||
return $files; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
function is_sub_path($path, $parent_folder) { | ||
$path = realpath($path); | ||
$parent_folder = realpath($parent_folder); | ||
|
||
if ($path !== false && $parent_folder !== false) { | ||
return strpos($path, $parent_folder) === 0; | ||
} | ||
|
||
return false; | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
<?php | ||
include_once(__DIR__."/lib/lib.php"); | ||
if ($_SERVER['REQUEST_METHOD'] == 'GET') { | ||
$files = scandir("/scans", SCANDIR_SORT_DESCENDING); | ||
if(array_key_exists("num", $_GET)) { | ||
$num = $_GET["num"]; | ||
} else { | ||
$num = count($files); | ||
} | ||
$files = getFileList($SCANS_DIR, GETFILELIST_SORT_CREATEDATE_DESC); | ||
|
||
$num = $_GET["num"] ?? count($files); | ||
for ($i = 0; $i < $num; $i++) { | ||
echo $files[$i]."<br>"; | ||
echo str_replace($SCANS_DIR."/", "", $files[$i])."<br>"; | ||
} | ||
} else { | ||
http_response_code(405); | ||
die("Error: Method not allowed!"); | ||
} | ||
?> | ||
?> |
Oops, something went wrong.