' .
'' .
'
' .
'' . $title . '' .
'' .
'' .
'' . $title . '
' .
'' . $text . '
' .
'
' .
$motd .
'' .
'';
die();
}
function do_filesize($path, $is_dir){
if($is_dir){
return null;
}
$filesize = filesize($path);
if($filesize === 0){
echo "0.00 B";
}
$s = ["B", "KB", "MB", "GB", "TB", "PB"];
$e = floor(log($filesize, 1024));
return round($filesize / pow(1024, $e), 2) . " " . $s[$e];
}
function do_header($title, $search, $check, $is_search, $script_name, $up){
echo
'' .
'' .
'' .
'' . $title . '' .
'' .
'' .
'' . $title . '
' .
'
' .
($is_search === true ? '< Go back to index
' : "") .
($up !== false ? '^ Go up
' : "") .
'' .
'' .
($is_search === false ? ' | ' : "") .
'' .
'type' .
' | ' .
'' .
'name' .
' | ' .
'' .
'size' .
' | ' .
($is_search === false ? ' | ' : "") .
'
' .
'' .
'
| ' .
'
';
}
// handle search
if(isset($_GET["query"])){
$query =
(
isset($_GET["query"]) &&
!empty(trim($_GET["query"])) &&
is_string($_GET["query"])
) ? trim($_GET["query"]) : false;
$filter =
(
isset($_GET["filter"]) &&
(
$_GET["filter"] == "and" ||
$_GET["filter"] == "or" ||
$_GET["filter"] == "pcre"
)
) ? $_GET["filter"] : "and";
$folder_base_strlen = strlen($folder_base);
if($query === false){
do_error(
400,
"Search query is empty",
"You need to search for something you dumb shit!",
$motd,
$script_name
);
}
$query_escaped = htmlspecialchars($query);
do_header("search results for "" . $query_escaped . """, $query_escaped, $filter, true, $script_name, false);
// prepare search cmp
$query_arr =
explode(
" ",
preg_replace(
'/ +/',
" ",
$query
)
);
$dir_iterator = new RecursiveDirectoryIterator($folder_base, FilesystemIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
$matches = 0;
while(true){
$iterator->next();
if(!$iterator->valid()){
break;
}
$filename = $iterator->getFilename();
switch($filter){
case "and":
$check = true;
foreach($query_arr as $q){
if(stripos($filename, $q) === false){
$check = false;
break;
}
}
break;
case "or":
$check = false;
foreach($query_arr as $q){
if(stripos($filename, $q) !== false){
$check = true;
break;
}
}
break;
case "pcre":
$match = preg_match($query, $filename);
if($match === false){
if(preg_last_error() !== PREG_NO_ERROR){
// regex error
echo
'RegEx error: ' . preg_last_error_msg() . ' |
|
' .
'
' .
$motd .
'' .
'';
die();
}
continue 2;
}elseif($match === 1){
$check = true;
}else{
$check = false;
}
break;
}
if($check){
$matches++;
$internal_path = $iterator->getPath() . "/" . $filename;
$fullpath =
substr_replace(
$internal_path,
"",
0,
$folder_base_strlen
);
$is_dir = is_dir($internal_path);
echo
'' .
'' . ($is_dir ? "<DIR>" : "<FILE>") . ' | ' .
'' .
'' .
htmlspecialchars(
$filename
) .
'' .
' | ' .
'' . do_filesize($internal_path, $is_dir) . ' | ' .
'
';
}
if($matches === $max_results){
break;
}
}
// pagination
echo
'
|
' .
'Found ' . $matches . ' matches (max=' . $max_results . ') | ' .
'
' .
$motd .
'