1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\EmailBundle\Tests; |
4
|
|
|
|
5
|
|
|
//Author : de77 |
6
|
|
|
//Website : www.de77.com |
7
|
|
|
//License : MIT (http://en.wikipedia.org/wiki/MIT_License) |
8
|
|
|
//Class desc : http://de77.com/php-class-fast-find-text-string-in-files-recursively |
9
|
|
|
|
10
|
|
|
//------------------------------------------------------------------------------ |
11
|
|
|
// If you like this class- please leave a comment on my site, thanks! |
12
|
|
|
//------------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
class FindInFileUtil |
15
|
|
|
{ |
16
|
|
|
private $text; |
17
|
|
|
private $textlen; |
18
|
|
|
private $results; |
19
|
|
|
|
20
|
|
|
public $caseSensitive = true; |
21
|
|
|
public $error; |
22
|
|
|
public $excludeMode = true; |
23
|
|
|
public $formats = array('.jpg', '.gif', '.avi'); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $dir |
27
|
|
|
* @param string $text |
28
|
|
|
* |
29
|
|
|
* @return array of files with the search-text |
30
|
|
|
*/ |
31
|
|
|
public function find($dir, $text) |
32
|
|
|
{ |
33
|
|
|
$this->textlen = strlen($text); |
34
|
|
|
$this->text = $text; |
35
|
|
|
|
36
|
|
|
if ($this->textlen > 4096) { |
37
|
|
|
$this->error = 'You cannot search for such long text. Limit is 4096 Bytes'; |
38
|
|
|
|
39
|
|
|
return false; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->results = array(); |
43
|
|
|
if ('/' != substr($dir, -1, 1)) { |
44
|
|
|
$dir .= '/'; |
45
|
|
|
} |
46
|
|
|
$this->scandir2($dir); |
47
|
|
|
|
48
|
|
|
return $this->results; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $file |
53
|
|
|
*/ |
54
|
|
|
private function findTxtt($file) |
55
|
|
|
{ |
56
|
|
|
$ext = strrchr($file, '.'); |
57
|
|
|
|
58
|
|
|
if (true == $this->excludeMode) { |
|
|
|
|
59
|
|
|
if (in_array($ext, $this->formats)) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
if (!in_array($ext, $this->formats)) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (filesize($file) < 40960) { |
69
|
|
|
$data = file_get_contents($file); |
70
|
|
|
if ($this->strpos2($data, $this->text)) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
$f = fopen($file, 'r'); |
77
|
|
|
$currentPos = 0; |
78
|
|
|
$step = 40960; |
79
|
|
|
while (!feof($f)) { |
|
|
|
|
80
|
|
|
$data = fread($f, $step); |
|
|
|
|
81
|
|
|
if (!$data) { |
82
|
|
|
break; |
83
|
|
|
} elseif ($this->strpos2($data, $this->text)) { |
84
|
|
|
fclose($f); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
$currentPos = $currentPos + $step - $this->textlen; |
89
|
|
|
fseek($f, $currentPos, SEEK_SET); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
fclose($f); |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $haystack |
98
|
|
|
* @param string $keyword |
99
|
|
|
*/ |
100
|
|
|
private function strpos2($haystack, $keyword) |
101
|
|
|
{ |
102
|
|
|
if (!$this->caseSensitive) { |
103
|
|
|
return false !== mb_stripos($haystack, $keyword); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return false !== mb_strpos($haystack, $keyword); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $dir |
111
|
|
|
*/ |
112
|
|
|
private function scandir2($dir) |
113
|
|
|
{ |
114
|
|
|
if (is_dir($dir)) { |
115
|
|
|
if ($dh = opendir($dir)) { |
116
|
|
|
while (false !== ($file = readdir($dh))) { |
117
|
|
|
if ('.' != $file and '..' != $file) { |
118
|
|
|
if (is_dir($dir.$file)) { |
119
|
|
|
$this->scandir2($dir.$file.'/'); |
120
|
|
|
} else { |
121
|
|
|
if ($this->findTxtt($dir.$file)) { |
122
|
|
|
$this->results[] = $dir.$file; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
closedir($dh); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|