-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.php
159 lines (122 loc) · 6.13 KB
/
iterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Created by JetBrains PhpStorm.
* User: sandric
* Date: 23.11.11
* Time: 06:05
* To change this template use File | Settings | File Templates.
*/
const SEARCHING_FILE = "buf.txt";
const FOUND_FILE = "found.txt";
const SLEEPTIME = 50000;
//main search function
//taking as params path to starting directory, search string pattern, and bool variables for file, text and subdirectories search
function search($path, $searchString, $bFileSearch, $bTextSearch, $bSubDirectories, $bDynamicSearch = null)
{
$matches = array();
$dHandle = opendir($path); //opening base dir
if(!is_resource($dHandle))
throw new Exception("Error: can't open directory $path!");
while( false !== ($fName = readdir($dHandle)) )
{
if($fName != "." && $fName != "..") //script was made under linux, this may not be need in other OSes
{
if($bDynamicSearch)
{
usleep(SLEEPTIME); // this is only for seeing how script works, to show the differences in searching files in html
$hSearching = fopen(SEARCHING_FILE, "a");
if(!is_resource($hSearching))
throw new Exception("Error: can't open file " . SEARCHING_FILE . "!");
if(!fwrite($hSearching, "$path/$fName\n")) //opening and writing current searching file to buffer file (for it could be readen from searchData2JSON.php)
throw new Exception("Error: can't write to file $path/$fName!");
fclose($hSearching);
}
if(is_dir("$path/$fName"))
{
if($bSubDirectories) //recursively searching in subdirectories, if asked
$matches += search("$path/$fName", $searchString, $bFileSearch, $bTextSearch, $bSubDirectories, $bDynamicSearch);
}
else
{
if($bFileSearch)
{
if(preg_match("/^$searchString$/", $fName, $fileMatches) === 1) //searching in file name
{
$matches["$path/$fName"] = $fName;
if($bDynamicSearch)
{
$hFound = fopen(FOUND_FILE, "a");
if(!is_resource($hFound))
throw new Exception("Error: can't open file " . FOUND_FILE . "!");
if(!fwrite($hFound, "$path/$fName\n")) //and if success, write path to file to found buffer file
throw new Exception("Error: can't write to file $path/$fName!");
fclose($hFound);
}
}
}
if($bTextSearch && !isset($matches["$path/$fName"])) //searching in file text, if no result in file searching step before
{
if(filesize("$path/$fName") > 0 && filesize("$path/$fName") < 4000000) //I got some strange error due to no differences in linux in text and binary files, so its prevent such bugs
{
$hFile = fopen("$path/$fName", "r");
if(!is_resource($hFile))
throw new Exception("Error: can't open file $path/$fName!");
if(false === ($fileText = fread($hFile, filesize("$path/$fName")))) //reading searching file
throw new Exception("Error: can't read file $path/$fName!");
if(preg_match("/$searchString/", $fileText, $fileMatches) === 1) //searching in content
{
$matches["$path/$fName"] = $fName;
if($bDynamicSearch)
{
$hFound = fopen(FOUND_FILE, "a");
if(!is_resource($hFound))
throw new Exception("Error: can't open file " . FOUND_FILE . "!");
if(!fwrite($hFound, "$path/$fName\n")) //trying to write result to FOUND_FILES buffer
throw new Exception("Error: can't write to file $path/$fName!");
fclose($hFound);
}
}
fclose($hFile);
}
}
}
}
}
closedir($dHandle);
return $matches;
}
try
{
$fh = fopen( SEARCHING_FILE, 'w' ); //truncating current file and found files buffers
if(!is_resource($fh))
throw new Exception("Error: can't open file " . SEARCHING_FILE . "!");
fclose($fh);
$fh = fopen( FOUND_FILE, 'w' );
if(!is_resource($fh))
throw new Exception("Error: can't open file " . FOUND_FILE . "!");
fclose($fh);
$bFileNameSearch = false;
$bTextNameSearch = false;
$bSubDirectories = false;
if(isset($_POST["fileNameSearch"]))
$bFileNameSearch = true;
if(isset($_POST["fileTextSearch"]))
$bTextNameSearch = true;
if(isset($_POST["subDirectoriesSearch"]))
$bSubDirectories = true;
if(!$bFileNameSearch && !$bTextNameSearch)
throw new Exception("Error: need to select at least one search!");
$stringPattern = str_replace(array("\\*", "\\?"), array(".*", ".?"), quotemeta($_POST["stringPattern"])); //clearing (as possible) input string search patters
$files = search(".", $stringPattern, $bFileNameSearch, $bTextNameSearch, $bSubDirectories, true);
/*!!!if you dont want all this dynamic stuff, just comment 4 upper lines of code, and uncomment 4 below until try block ends
$files = search(".", "ololo", true, true, true);
print "<pre>";
print_r($files);
print "</pre>";
*/
}
catch(Exception $e)
{
print $e;
}
?>