312 lines
8.7 KiB
PHP
Executable File
312 lines
8.7 KiB
PHP
Executable File
<?php
|
|
define("LIMIT", 10*1024);
|
|
define("NO_OF_LINES", 100);
|
|
define("SEARCHFILE", ".search.txt");
|
|
define("CHILDPIDS", "childpids.txt");
|
|
define("WAITFOR", 2);
|
|
define("SHARED_FOLDER_COUNT_FOR_SEARCH", 10);
|
|
|
|
ini_set('display_errors', '1');
|
|
ini_set('track_errors', 1);
|
|
include 'sqliteDB.php';
|
|
|
|
$searchDir = trim(`pwd`,LINEFEED);
|
|
$searchPath = $searchDir.PATHSEPARATOR.SEARCHFILE;
|
|
$childpidfile = $searchDir . PATHSEPARATOR . CHILDPIDS;
|
|
$outputFilePath = $searchDir.PATHSEPARATOR.OUTPUTFILE;
|
|
|
|
$searchContent = file($searchPath);
|
|
if(empty($searchContent[0])){
|
|
traceLog("Search cannot complete, Reason: search item not found.");
|
|
exit;
|
|
}
|
|
|
|
$lastLine = "";
|
|
$pageCount = 0;
|
|
$flag = 0;
|
|
$buffer = "";
|
|
$count = 0;
|
|
$countChk = 0;
|
|
$type = "";
|
|
$size = "";
|
|
$dateTime = "";
|
|
$isOpened = 0;
|
|
$pidPath = $searchDir.PATHSEPARATOR.PIDFILE;
|
|
|
|
//traceChildPIDs(getmypid());
|
|
file_put_contents($pidPath, getMyParentID());
|
|
|
|
$pid = pcntl_fork();
|
|
if(!isset($pid)){
|
|
traceLog("Unable to fork() child process for search");
|
|
unlink($pidPath);
|
|
exit;
|
|
}
|
|
elseif ($pid === 0){
|
|
traceChildPIDs(getmypid());
|
|
getSearchResult($searchContent,$searchDir);
|
|
if(file_exists($outputFilePath)){
|
|
file_put_contents($outputFilePath,"FIND_END".LINEFEED,FILE_APPEND);
|
|
@chmod($outputFilePath,PERMISSIONVAL);
|
|
chmod($searchDir.PATHSEPARATOR.OUTPUTFILE,PERMISSIONVAL);
|
|
}
|
|
@unlink($childpidfile);
|
|
exit;
|
|
}
|
|
else {
|
|
$searchChilds[] = $pid;
|
|
while(!file_exists($outputFilePath) or !customFileSize($outputFilePath)){
|
|
clearstatcache();
|
|
if(!($countChk % 10) and !file_exists($pidPath))
|
|
userCancelCleanup();
|
|
sleep(WAITFOR);
|
|
++$countChk;
|
|
}
|
|
|
|
$outputFileHandle = fopen($outputFilePath,"r");
|
|
if(!$outputFileHandle){
|
|
traceLog("Unable to open $outputFilePath");
|
|
userCancelCleanup();
|
|
}
|
|
|
|
while(1){
|
|
if(!($countChk % 10) and !file_exists($pidPath))
|
|
userCancelCleanup();
|
|
++$countChk;
|
|
|
|
$buffer = fread($outputFileHandle,LIMIT);
|
|
if(empty($buffer)){
|
|
sleep(WAITFOR);
|
|
fseek($outputFileHandle, 0, 1); #to clear eof flag
|
|
continue;
|
|
}
|
|
|
|
if(!empty($lastLine)) # need to check appending partial record to packet or to first line of packet
|
|
$buffer = $lastLine . $buffer;
|
|
|
|
$resultList = explode(LINEFEED,$buffer);
|
|
array_pop($resultList);
|
|
$resIndex = count($resultList)-1;
|
|
|
|
if(!preg_match('/\n$/', $buffer)) { #keep last line of buffer only when it does not ends with newline. #keep last line of buffer only when it does not ends with newline.
|
|
$lastLine = $resultList[$resIndex];
|
|
}
|
|
else
|
|
$lastLine = "";
|
|
|
|
|
|
if($resultList[$resIndex] === "FIND_END"){
|
|
$flag = 1;
|
|
array_pop($resultList);
|
|
}
|
|
|
|
foreach($resultList as $key => $line){
|
|
if(empty($line))
|
|
continue;
|
|
|
|
if(0 === $isOpened){
|
|
usleep(1000);
|
|
++$pageCount;
|
|
if($searchFileHandle)
|
|
@fclose($searchFileHandle);
|
|
|
|
if(!$searchFileHandle = fopen("$searchDir/page$pageCount","w")){
|
|
traceLog("Unable to open $searchDir/page$pageCount");
|
|
userCancelCleanup();
|
|
}
|
|
chmod("$searchDir/page$pageCount",PERMISSIONVAL);
|
|
$isOpened = 1;
|
|
}
|
|
|
|
$type = "";
|
|
|
|
# Stat output:
|
|
# 7 => total size of file, in bytes
|
|
# 9 => last modify time in seconds since the epoch.
|
|
$statRes = stat($line);
|
|
if(count($statRes)-1) {
|
|
if(is_link($line))
|
|
continue;
|
|
if (is_file($line))
|
|
$type = "F";
|
|
elseif (is_dir($line))
|
|
$type = "D";
|
|
|
|
if(!empty($type)){
|
|
list($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($statRes[9]);
|
|
$year += 1900;
|
|
$mon += 1;
|
|
$mday = (($mday < 10)? "0" . $mday : $mday);
|
|
$mon = (($mon < 10)? "0" . $mon : $mon);
|
|
$dateTime = "$year/$mon/$mday $hour:$min:$sec";
|
|
if($statRes[7] < 0)
|
|
$statRes[7] = customFileSize($line);
|
|
|
|
$size = convertFileSize($statRes[7]);
|
|
fwrite($searchFileHandle, $line."\n".$size."\n".$dateTime."\n".$type."\n");
|
|
|
|
++$count;
|
|
if(0 === $count % NO_OF_LINES)
|
|
$isOpened = 0;
|
|
}
|
|
}
|
|
|
|
if(!($countChk % 10) and !file_exists($pidPath))
|
|
userCancelCleanup();
|
|
++$countChk;
|
|
}
|
|
if(1 === $flag)
|
|
break;
|
|
}
|
|
userCancelCleanup();
|
|
}
|
|
|
|
while(count($searchChilds) > 0) {
|
|
foreach($searchChilds as $key => $pid) {
|
|
$res = pcntl_waitpid($pid, $status, WNOHANG);
|
|
|
|
// If the process has already exited
|
|
if($res == -1 || $res > 0)
|
|
unset($searchChilds[$key]);
|
|
}
|
|
|
|
sleep(1);
|
|
}
|
|
|
|
exit;
|
|
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : userCancelCleanup
|
|
# Objective : This function completes the user cancel cleanup
|
|
# Added By : Pooja Havaldar
|
|
#*****************************************************************************************************/
|
|
function traceChildPIDs($pid)
|
|
{
|
|
GLOBAL $childpidfile;
|
|
file_put_contents($childpidfile, "{$pid}\n", FILE_APPEND);
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : userCancelCleanup
|
|
# Objective : This function completes the user cancel cleanup
|
|
# Added By : Pooja Havaldar
|
|
# Modified By : Sabin Cheruvattil
|
|
#*****************************************************************************************************/
|
|
function userCancelCleanup()
|
|
{
|
|
GLOBAL $searchFileHandle, $outputFileHandle, $pidPath, $searchDir, $searchPath, $outputFilePath, $childpidfile;
|
|
|
|
@fclose($searchFileHandle);
|
|
@fclose($outputFileHandle);
|
|
|
|
if(file_exists($childpidfile)) {
|
|
$cpids = file($childpidfile);
|
|
foreach($cpids as $cpid) {
|
|
$cpid = trim($cpid);
|
|
if($cpid != '' && (int)$cpid > 0) {
|
|
`kill $cpid`;
|
|
}
|
|
}
|
|
@unlink($childpidfile);
|
|
}
|
|
|
|
if(file_exists($pidPath)){
|
|
unlink($pidPath);
|
|
unlink($searchPath);
|
|
unlink($outputFilePath);
|
|
unlink($searchDir.PATHSEPARATOR.ERRORFILE);
|
|
}
|
|
else{
|
|
$searchDir = str_replace("'","'\''",$searchDir);
|
|
$searchDir = "'".$searchDir."'";
|
|
if($searchDir!="/")
|
|
removeItems($searchDir);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : getSearchResult
|
|
# Objective : This function search the pattern provided in full NAS machine and returns
|
|
# the output.
|
|
# Added By : Deepak Chaurasia
|
|
#*****************************************************************************************************/
|
|
function getSearchResult($searcharray,$searchDir)
|
|
{
|
|
GLOBAL $webDir,$machine;
|
|
$searchItem = trim($searcharray[0],LINEFEED);
|
|
$searchItem = phpQuoteMeta($searchItem);
|
|
$searchItem = str_replace("'","'\''",$searchItem);
|
|
$OnlySearch = "'".$searchItem."'";
|
|
$excludePath = getWebDirPath();
|
|
|
|
$searchHiddenItem = "";
|
|
if(preg_match("/^\\./m",$searchItem ))
|
|
$searchHiddenItem = "'"."$searchItem"."*'";
|
|
else
|
|
$searchHiddenItem = "'."."$searchItem"."*'";
|
|
|
|
$searchDir = str_replace("'","'\''",$searchDir);
|
|
$searchDir = "'".$searchDir."'";
|
|
$searchItem = "'*"."$searchItem"."*'";
|
|
|
|
$searcharray_size = count($searcharray);
|
|
if((SYNOLOGY === $machine) or (NETGEAR === $machine) or (ASUSTOR === $machine)){
|
|
$pattern = "-type d \\( -path $excludePath/".APPDIR." -o -path $excludePath/".SERVICE." \\) -prune -o -iname $searchItem -print -o -iname $searchHiddenItem -print -o -iname $OnlySearch -print >> ".$searchDir.PATHSEPARATOR.OUTPUTFILE." 2>>".$searchDir.PATHSEPARATOR.ERRORFILE.";";
|
|
$find = "find ";
|
|
for($i=1 ; $i<$searcharray_size ; $i++){
|
|
$searcharray[$i] = trim($searcharray[$i],LINEFEED);
|
|
$find .= "'$searcharray[$i]' ";
|
|
if($i%SHARED_FOLDER_COUNT_FOR_SEARCH === 0){
|
|
$cmd = $find.$pattern;
|
|
if(file_exists(PIDFILE))
|
|
exec($cmd);
|
|
else
|
|
break;
|
|
$find = "find ";
|
|
}
|
|
}
|
|
|
|
if($find !== "find "){
|
|
$cmd = $find.$pattern;
|
|
if(file_exists(PIDFILE))
|
|
exec($cmd);
|
|
}
|
|
}
|
|
elseif(QNAP == $machine){
|
|
$pattern1 = "-name $searchItem >> ".$searchDir.PATHSEPARATOR.OUTPUTFILE." 2>>".$searchDir.PATHSEPARATOR.ERRORFILE;
|
|
$pattern2 = "-name $searchHiddenItem >> ".$searchDir.PATHSEPARATOR.OUTPUTFILE." 2>>".$searchDir.PATHSEPARATOR.ERRORFILE;
|
|
$find = "find ";
|
|
for($i=1 ; $i<$searcharray_size ; $i++){
|
|
$searcharray[$i] = trim($searcharray[$i],LINEFEED);
|
|
$find .= "'$searcharray[$i]' ";
|
|
if($i%SHARED_FOLDER_COUNT_FOR_SEARCH === 0){
|
|
$cmd1 = $find.$pattern1;
|
|
if(file_exists(PIDFILE))
|
|
exec($cmd1);
|
|
else
|
|
break;
|
|
|
|
$cmd2 = $find.$pattern2;
|
|
if(file_exists(PIDFILE))
|
|
exec($cmd2);
|
|
else
|
|
break;
|
|
|
|
$find = "find ";
|
|
}
|
|
}
|
|
|
|
if($find !== "find "){
|
|
$cmd1 = $find.$pattern1;
|
|
if(file_exists(PIDFILE))
|
|
exec($cmd1);
|
|
|
|
$cmd2 = $find.$pattern2;
|
|
if(file_exists(PIDFILE))
|
|
exec($cmd2);
|
|
}
|
|
}
|
|
}
|