Created
November 15, 2016 15:38
-
-
Save vibbow/a56d54fa08bb66501f852b6dfd2e9d49 to your computer and use it in GitHub Desktop.
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
<?php | |
define ('DB_SOURCE', 'd:\\leakdb'); | |
define ('CACHE_LIMIT', 67108864); | |
define ('RESULT_LIMIT', 1000); | |
define ('TIME_LIMIT', 600); | |
$begin = microtime(true); | |
set_time_limit(TIME_LIMIT + 100); | |
ob_end_flush(); | |
echo <<< EOF | |
<html><head> | |
<meta http-equiv="Content-Type" content="text/html; charset=GBK" /> | |
<title>Full text search</title> | |
</head><body> | |
<form method="get" action=""> | |
<input type="text" name="keyword" /> | |
<input type="submit" /> | |
</form> | |
EOF; | |
flush(); | |
$keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : ''; | |
if (empty($keyword)) exit('</body></html>'); | |
$filelist = array(); | |
get_file_list(DB_SOURCE . '\\*'); | |
$count = 0; | |
echo 'Search ' . $keyword . ' in ' . count($filelist) . " leak databases ...<br />\r\n"; | |
flush(); | |
foreach ($filelist as $filepath) { | |
$fp = fopen($filepath, 'r'); | |
if (!$fp) continue; | |
$basename = basename($filepath); | |
$fp_start_pos = 0; | |
while(!feof($fp)) { | |
fseek($fp, $fp_start_pos); | |
$content = fread($fp, CACHE_LIMIT); | |
$content_length = strrpos($content, "\n"); | |
$content = substr($content, 0, $content_length); | |
$fp_start_pos += $content_length; | |
$keyword_pos = 0; | |
while (($keyword_pos = strpos($content, $keyword, $keyword_pos)) !== false) | |
{ | |
$start_pos = strrpos($content, "\n", -$content_length + $keyword_pos); | |
$end_pos = strpos($content, "\n", $keyword_pos); | |
if ($start_pos === false) $start_pos = 0; | |
echo $basename . ' | ' . trim(substr($content, $start_pos, $end_pos - $start_pos)) . "<br />\r\n"; | |
flush(); | |
$keyword_pos = $end_pos; | |
$count++; | |
if ($count >= RESULT_LIMIT) break; | |
} | |
if ($count >= RESULT_LIMIT) break; | |
} | |
fclose($fp); | |
if ($count >= RESULT_LIMIT) break; | |
if ((microtime(true) - $begin) >= TIME_LIMIT) break; | |
} | |
if ($count >= RESULT_LIMIT) | |
echo "Too many results, give up<br />\r\n"; | |
if ((microtime(true) - $begin) >= TIME_LIMIT) | |
echo "Search time out, give up<br />\r\n"; | |
echo 'Search complete, get ' . $count . ' results, cost ' . (microtime(true) - $begin) . " seconds<br />\r\n"; | |
echo '</body></html>'; | |
flush(); | |
function get_file_list($dbsource) { | |
global $filelist; | |
$current_file_list = glob($dbsource); | |
foreach ($current_file_list as $each) { | |
if (strpos($each, 'search.php') === true) | |
continue; | |
if (is_file($each)) | |
$filelist[] = $each; | |
if (is_dir($each)) | |
get_filelist($each . '\\*'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment