FilesystemHandler.class.php
6.2 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
This is phpSitemapNG, a php script that creates your personal google sitemap
It can be downloaded from http://enarion.net/google/
License: GPL
Tobias Kluge, enarion.net
*/
class FilesystemHandler {
var $files = array();
var $done = array();
var $todo = array();
var $forbidden_dir = array();
var $forbidden_files = array();
var $filesystem_base = '';
var $directory_offset = '';
var $cur_item = 0;
var $deadline;
/**
* public constructor, set initial values
*/
function FilesystemHandler($filesystem_base, $deadline = 0, $directory_offset = '') {
$this->filesystem_base = $filesystem_base;
$this->directory_offset = $directory_offset;
$this->deadline = $deadline;
}
/**
* returns number of files
*/
function size() {
return count($this->files);
}
/**
* returns true when the current item is not the last item
* behaves like in java
*/
function hasNext() {
if ($this->size() > $this->cur_item) return TRUE;
return FALSE;
}
function hasFinished() {
return (count($this->todo) == 0);
}
/**
* returns the current item
* behaves like in java
*/
function getNext() {
if ($this->hasNext()) {
$tmp = $this->files[$this->cur_item];
$this->cur_item++;
return $tmp;
}
return NULL;
}
function getTodo() {
return $this->todo;
}
function getFiles() {
return $this->files;
}
function getDone() {
return $this->done;
}
function setTodo($todo) {
$this->todo = $todo;
}
function setFiles($files) {
$this->files = $files;
}
function setDone($done) {
$this->done = $done;
}
/**
* set list of forbidden directories
*/
function setForbiddenDirectories($directories = array()) {
$this->forbidden_dir = $directories;
}
/**
* set list of forbidden files
*/
function setForbiddenFiles($files = array()) {
$this->forbidden_files = $files;
}
/**
* scans filesystem for files that match the setted conditions
*
* return number of files that have been found
*/
function start() {
reset($this->todo);
while(($this->deadline == 0) || (($this->microtime_float() - $this->deadline) <= 0)) {
$url = array_pop($this->todo);
if (is_null($url) || $url == '') break;
$this->_getFiles($url);
}
$this->files = $this->_changeOffset($this->done, $this->filesystem_base, $this->directory_offset);
reset($this->files);
ksort($this->files);
reset($this->files);
return count($this->files);
}
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* return last modification time
*/
function getLastModificationTime($filename) {
$lastmod = filemtime($filename);
// if filemtime failed (for any reason), set it to current time
if (!((!is_null($lastmod)) && is_integer($lastmod) && $lastmod > 0)) $lastmod = time();
return $lastmod;
}
/* some private functions */
/* this function changes a substring($old_offset) of each array element to $offset */
function _changeOffset($array, $old_offset, $offset) {
$res = array();
if (is_array($array) && count($array) > 0) {
foreach ($array as $id => $val) {
$res[] = str_replace($old_offset, $offset, $val);
}
}
return $res;
}
/**
* only allowed masking char: * (before and/or after search string)
*
* TODO check this with more data
*/
function checkFileName($filename) {
$filename = substr($filename, strrpos($filename, '/')+1);
if (is_array($this->forbidden_files) && count($this->forbidden_files) > 0) {
foreach ($this->forbidden_files as $id => $file) {
if ($file == '') continue;
$pos = strpos($filename, $file);
/* $file_search = '';
if (!(($as = strpos($file, '*')) === FALSE)) {
$file_search = str_replace('*', '', $file);
if ($as == 0) $pos = @strpos($filename, $file_search, (strlen($filename)-strlen($file_search)));
if ($as == strlen($file_search)) $pos = (@strpos($filename, $file_search) != 0);
} else {
$pos = ($filename === $file);
}
*/ if ($pos === FALSE) continue;
return TRUE;
}
}
return FALSE;
}
function checkDirectoryName($directory) {
$directory = substr($directory, 0, strrpos($directory, '/')); // with last "/"
// dirname($directory); //
if (is_array($this->forbidden_dir) && count($this->forbidden_dir) > 0) {
foreach ($this->forbidden_dir as $id => $dir) {
if ($dir == '') continue;
$pos = strpos($directory, $dir);
/* $dir_search = '';
if (!(($as = strpos($dir, '*')) === FALSE)) {
$dir_search = str_replace('*', '', $dir);
if ($as == 0) $pos = @strpos($directory, $dir_search, (strlen($directory)-strlen($dir_search)));
if ($as == strlen($dir_search)) $pos = (@strpos($directory, $dir_search) != 0);
} else {
$pos = ($directory === $dir);
}
// echo "directory: $directory, dir: $dir, dir_search: $dir_search, pos: $pos<br>\n";
*/ if ($pos === FALSE) continue;
return TRUE;
}
}
return FALSE;
}
/**
* searches for files and adds them to $this->files; adds directories to $this->todo
* algorithm: breadth first search (former algorithm: dfs)
*/
function _getFiles($directory) {
if($dir = opendir($directory)) {
while(FALSE !== ($file = readdir($dir))) {
if ($file == '..' || $file == '.' || $file[0] == '.') continue;
//TODO maybe adapt this to php running on windows
if (substr($directory, -1) != '/') {
$filename = $directory .'/' . $file;
} else {
$filename = $directory . $file;
}
// If $file is a directory, add it to the todo list
if(@is_dir($filename)) {
$filename = $filename .'/';
if ($this->checkDirectoryName($filename)) continue;
array_push($this->todo, $filename);
} else { // is a file, add it to done list
if ($this->checkFileName($filename)) continue;
array_push($this->done, $filename);
}
}
closedir($dir);
return TRUE;
}
}
}
?>