{"id":399,"date":"2014-07-04T17:11:43","date_gmt":"2014-07-04T17:11:43","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=399"},"modified":"2017-07-28T13:31:24","modified_gmt":"2017-07-28T05:31:24","slug":"%e5%85%b3%e9%94%ae%e5%ad%97%e6%9f%a5%e6%89%be%e7%9a%84php%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/399.html","title":{"rendered":"\u5173\u952e\u5b57\u67e5\u627e\u7684PHP\u811a\u672c"},"content":{"rendered":"<p>=Start=<\/p>\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"http:\/\/www.ar5ch.com\/programming\/527.arc\" target=\"_blank\" rel=\"noopener\">http:\/\/www.ar5ch.com\/programming\/527.arc<\/a><\/p>\n<h6>\u5173\u952e\u5b57\u641c\u7d22\u811a\u672c\uff1a<\/h6>\n<pre class=\"lang:php decode:true\">&lt;?php\r\ndefine ('DB_SOURCE', 'D:DATA');\/\/\u5b9a\u4e49\u6570\u636e\u76ee\u5f55\uff0c\u53ef\u4ee5\u904d\u5386\u76ee\u5f55\u4e0b\u6240\u6709\u5b50\u76ee\u5f55\r\ndefine ('CACHE_LIMIT', 67108863);\r\ndefine ('RESULT_LIMIT', 1000);\r\ndefine ('TIME_LIMIT', 600);\r\n\r\n$begin = microtime(true);\r\nset_time_limit(TIME_LIMIT + 100);\r\nini_set(\"memory_limit\",\"-1\");  \/\/\u8bbe\u7f6e\u4e0d\u9650\u5236\u5185\u5b58\r\nob_end_flush();\r\n\r\necho &lt;&lt;&lt; EOF\r\nEOF;\r\nflush();\r\n\r\n$keyword = $argv[1];\r\n$filelist = array();\r\nget_file_list(DB_SOURCE . '\\*');\r\n$count = 0;\r\n\r\necho 'Search ' . $keyword . ' in ' . count($filelist) . \" leak databases ...n\";\r\nflush();\r\n\r\nforeach ($filelist as $filepath) {\r\n    $fp = fopen($filepath, 'r');\r\n    if (!$fp) continue;\r\n    $basename = basename($filepath);\r\n    $fp_start_pos = 0;\r\n    echo 'Searching ' . $filepath . \" n\";\r\n    while(!feof($fp)) {\r\n        fseek($fp, $fp_start_pos);\r\n        $content = fread($fp, CACHE_LIMIT);\r\n        $content_length = strrpos($content, \"n\") + 1;\r\n        $content = substr($content, 0, $content_length);\r\n        $fp_start_pos += $content_length;\r\n        $keyword_pos = 0;\r\n        while (($keyword_pos = strpos($content, $keyword, $keyword_pos)) !== false)\r\n        {\r\n            $start_pos = strrpos($content, \"n\", -$content_length + $keyword_pos);\r\n            $end_pos = strpos($content, \"n\", $keyword_pos);\r\n            echo  trim(substr($content, $start_pos, $end_pos - $start_pos)) . \"n\";\r\n            flush();\r\n            $keyword_pos = $end_pos;\r\n            $count++;\r\n            if ($count &gt;= RESULT_LIMIT) break;\r\n        }\r\n        if ($count &gt;= RESULT_LIMIT) break;\r\n    }\r\n    fclose($fp);\r\n    if ($count &gt;= RESULT_LIMIT) break;\r\n    if ((microtime(true) - $begin) &gt;= TIME_LIMIT) break;\r\n}\r\n\r\nif ($count &gt;= RESULT_LIMIT)\r\n    echo \"Too many results, give upn\";\r\nif ((microtime(true) - $begin) &gt;= TIME_LIMIT)\r\n    echo \"Search time out, give upn\";\r\necho 'Search complete, get ' . $count . ' results, cost ' . (microtime(true) - $begin) . \" secondsn\";\r\nflush();\r\n\r\nfunction get_file_list($dbsource) {\r\n    global $filelist;\r\n    $current_file_list = glob($dbsource);\r\n    foreach ($current_file_list as $each) {\r\n        if (strpos($each, 'search.php') === true)\r\n            continue;\r\n        if (is_file($each))\r\n            $filelist[] = $each;\r\n        if (is_dir($each))\r\n            get_file_list($each . '\\*');\r\n    }\r\n}\r\n?&gt;<\/pre>\n<h6>\u4fee\u6539\u4e86\u4e00\u4e0b\uff0c\u4eceWeb\u9875\u9762\u8bbf\u95ee\u7684\u7248\u672c\u5982\u4e0b\uff1a<\/h6>\n<pre class=\"lang:php decode:true \">&lt;?php\r\ndefine ('DB_SOURCE', 'd:\\data');\r\ndefine ('CACHE_LIMIT', 4194304);\r\ndefine ('RESULT_LIMIT', 1000);\r\ndefine ('TIME_LIMIT', 600);\r\n\r\n$begin = microtime(true);\r\nset_time_limit(TIME_LIMIT + 100);\r\nob_end_flush();\r\n\r\necho &lt;&lt;&lt; EOF\r\n&lt;html&gt;&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=GBK\" \/&gt;\r\n&lt;title&gt;Full text search&lt;\/title&gt;\r\n&lt;\/head&gt;&lt;body&gt;\r\n&lt;form method=\"get\" action=\"\"&gt;\r\n&lt;input type=\"text\" name=\"keyword\" \/&gt;\r\n&lt;input type=\"submit\" \/&gt;\r\n&lt;\/form&gt;\r\nEOF;\r\nflush();\r\n\r\n$keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';\r\nif (empty($keyword)) exit('&lt;\/body&gt;&lt;\/html&gt;');\r\n\r\n$filelist = array();\r\nget_file_list(DB_SOURCE . '\\*');\r\n$count = 0;\r\n\r\necho 'Search ' . $keyword . ' in ' . count($filelist) . \" leak databases ...&lt;br \/&gt;rn\";\r\nflush();\r\n\r\nforeach ($filelist as $filepath) {\r\n\t$fp = fopen($filepath, 'r');\r\n\tif (!$fp) continue;\r\n\r\n\t$basename = basename($filepath);\r\n\t$filesize = filesize($filepath);\r\n\t$fp_start_pos = 0;\r\n\r\n\twhile($fp_start_pos !== $filesize) {\r\n\t\tfseek($fp, $fp_start_pos);\r\n\t\t$content = fread($fp, CACHE_LIMIT);\r\n\t\t$content_length = strlen($content);\r\n\t\tif ($fp_start_pos + $content_length !== $filesize) {\r\n\t\t\t$content_length = strrpos($content, \"n\") + 1;\r\n\t\t\t$content = substr($content, 0, $content_length);\r\n\t\t}\r\n\t\t$fp_start_pos += $content_length;\r\n\r\n\t\t$keyword_pos = 0;\r\n\t\twhile (($keyword_pos = strpos($content, $keyword, $keyword_pos)) !== false)\r\n\t\t{\r\n\t\t\t$start_pos = strrpos($content, \"n\", -$content_length + $keyword_pos);\r\n\t\t\t$end_pos = strpos($content, \"n\", $keyword_pos);\r\n\t\t\tif ($end_pos === FALSE) $end_pos = $content_length;\r\n\t\t\techo $basename . ' | ' . trim(substr($content, $start_pos, $end_pos - $start_pos)) . \"&lt;br \/&gt;rn\";\r\n\t\t\tflush();\r\n\t\t\t$keyword_pos = $end_pos;\r\n\t\t\t$count++;\r\n\t\t\tif ($count &gt;= RESULT_LIMIT) break;\r\n\t\t}\r\n\t\tif ($count &gt;= RESULT_LIMIT) break;\r\n\t}\r\n\tfclose($fp);\r\n\tif ($count &gt;= RESULT_LIMIT) break;\r\n\tif ((microtime(true) - $begin) &gt;= TIME_LIMIT) break;\r\n}\r\n\r\nif ($count &gt;= RESULT_LIMIT)\r\n\techo \"Too many results, give up&lt;br \/&gt;rn\";\r\nif ((microtime(true) - $begin) &gt;= TIME_LIMIT)\r\n\techo \"Search time out, give up&lt;br \/&gt;rn\";\r\necho 'Search complete, get ' . $count . ' results, cost ' . (microtime(true) - $begin) . \" seconds&lt;br \/&gt;rn\";\r\necho '&lt;\/body&gt;&lt;\/html&gt;';\r\nflush();\r\n\r\nfunction get_file_list($dbsource) {\r\n\tglobal $filelist;\r\n\t$current_file_list = glob($dbsource);\r\n\tforeach ($current_file_list as $each) {\r\n\t\tif (strpos($each, 'search.php') === true)\r\n\t\t\tcontinue;\r\n\t\tif (is_file($each))\r\n\t\t\t$filelist[] = $each;\r\n\t\tif (is_dir($each))\r\n\t\t\tget_file_list($each . '\\*');\r\n\t}\r\n}\r\n?&gt;<\/pre>\n<p>\u811a\u672c\u672c\u8eab\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u662f\u5728\u641c\u7d22\u5927\u6587\u4ef6\u3001\u591a\u6587\u4ef6\u7684\u65f6\u5019\u6548\u679c\u8fd8\u662f\u5f88\u597d\u7684\uff0c\u4e4b\u524d\u6709\u4e2a\u7528Python\u641c\u7d22\u7684\u811a\u672c\uff0c\u4f46\u662f\u8003\u8651\u7684\u4e0d\u662f\u592a\u7ec6\uff0c\u5f53\u5173\u952e\u5b57\u662f\u6362\u884c\u51fa\u73b0\u65f6\u5c31\u65e0\u6cd5\u5b8c\u6210\u641c\u7d22\u4e86\uff0c\u4e0d\u8fc7\u6709\u6539\u8fdb\u7684\u7a7a\u95f4\u5c31\u662f\u4e86\uff0c\u6709\u65f6\u95f4\u4e86\u518d\u6539\u6539o(\u256f\u25a1\u2570)o<\/p>\n<h6>\u8fd8\u6709\u4e00\u79cd\u66f4\u4e3a\u76f4\u89c2\u7684\u65b9\u5f0f\uff1a<\/h6>\n<pre class=\"lang:php decode:true\">&lt;?php\r\n@ini_set('memory_limit', '-1');\r\n\r\n$start=microtime(true);\r\n$files=getDirFiles(\"D:\/data\/\");\r\n\r\nfor($i=0; $i&lt;count($files); $i++) {\r\n\tloadfile($files[$i]);\r\n}\r\n\r\necho microtime(true)-$start . \"n\";\r\n\r\nfunction loadfile($file) {\r\n\t$fp=fopen($file,\"r\");\r\n\tfor($i=0; $i&lt;9999; $i++) {\r\n\t\t$temp=fread($fp,1024*1024*10);\r\n\t\tif(strlen($temp)==0) {\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\t$temp2=fgets($fp);\r\n\t\tif(strlen($temp2)!=0) {\r\n\t\t\t$temp.=$temp2;\r\n\t\t}\r\n\t\t$index=strpos($temp,\"keyword\");\r\n\t}\r\n}\r\nfunction getDirFiles($path,$subDir=false,$addDir=false) {\r\n\t$mydir=dir($path);\r\n\t$all=array();\r\n\twhile( ($file=$mydir-&gt;read())!==false){\r\n\t\tif($file==\".\" || $file==\"..\"){\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tif ( is_dir( $path.$file .\"\/\") ) {\r\n\t\t\tif($addDir) {\r\n\t\t\t\t$all=$path.$file .\"\/\";\r\n\t\t\t}\r\n\t\t\tif($subDir) {\r\n\t\t\t\t$temp=getDirFiles( $path.$file .\"\/\" ,$sub );\r\n\t\t\t\t$all=array_merge($all,$temp);\r\n\t\t\t}\r\n\r\n\t\t} else {\r\n\t\t\t$all[]= $path.$file ;\r\n\t\t}\r\n\t}\r\n\treturn $all;\r\n}\r\n?&gt;<\/pre>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u539f\u6587\u94fe\u63a5\uff1ahttp:\/\/www.ar5ch.com\/programming\/527.arc \u5173 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,25,12],"tags":[192,48,193,194,195],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-programing","category-security","category-tools","tag-fread","tag-php","tag-strpos","tag-strrpos","tag-substr"],"views":3001,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":3449,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions\/3449"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}