|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Fuel\FileSystem |
|
4
|
|
|
* @version 2.0 |
|
5
|
|
|
* @author Fuel Development Team |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2010 - 2015 Fuel Development Team |
|
8
|
|
|
* @link http://fuelphp.com |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fuel\FileSystem; |
|
12
|
|
|
|
|
13
|
|
|
class Filter |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $typeCache = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $filters = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Checks wether an path is of the correct type, dir or file |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $type |
|
29
|
|
|
* @param string $path |
|
30
|
|
|
* |
|
31
|
|
|
* @return boolean |
|
32
|
|
|
*/ |
|
33
|
|
|
public function isCorrectType($type, $path) |
|
34
|
|
|
{ |
|
35
|
|
|
if ( ! $type) |
|
36
|
|
|
{ |
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ( ! isset($this->typeCache[$path])) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->typeCache[$path] = is_file($path) ? 'file' : 'dir'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->typeCache[$path] === $type; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Filters a batch of filesystem entries |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $contents |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function filter(array $contents) |
|
56
|
|
|
{ |
|
57
|
|
|
$filtered = array(); |
|
58
|
|
|
$this->typeCache = array(); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($contents as $item) |
|
61
|
|
|
{ |
|
62
|
|
|
$passed = true; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->filters as $filter) |
|
65
|
|
|
{ |
|
66
|
|
|
$correctType = $this->isCorrectType($filter['type'], $item); |
|
67
|
|
|
|
|
68
|
|
|
if ($correctType and preg_match($filter['pattern'], $item) !== $expected) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$passed = false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($passed) |
|
75
|
|
|
{ |
|
76
|
|
|
$filtered[] = $item; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $contents; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Adds a filter |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $filter |
|
87
|
|
|
* @param boolean $expected |
|
88
|
|
|
* @param string $type |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addFilter($filter, $expected = true, $type = null) |
|
91
|
|
|
{ |
|
92
|
|
|
$filter = '#'.$filter.'#'; |
|
93
|
|
|
|
|
94
|
|
|
$this->filters[] = array( |
|
95
|
|
|
'pattern' => $filter, |
|
96
|
|
|
'expected' => $expected, |
|
97
|
|
|
'type' => $type, |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Ensures an extension |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $extension |
|
105
|
|
|
*/ |
|
106
|
|
|
public function hasExtension($extension) |
|
107
|
|
|
{ |
|
108
|
|
|
$filter = '\\.['.ltrim($extension, '.').']$'; |
|
109
|
|
|
|
|
110
|
|
|
$this->addFilter($filter, true, 'file'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Blocks by extension |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $extension |
|
117
|
|
|
*/ |
|
118
|
|
|
public function blockExtension($extension) |
|
119
|
|
|
{ |
|
120
|
|
|
$filter = '\\.['.ltrim($extension, '.').']$'; |
|
121
|
|
|
|
|
122
|
|
|
$this->addFilter($filter, false, 'file'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Blocks hidden files |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $type |
|
129
|
|
|
*/ |
|
130
|
|
|
public function blockHidden($type = null) |
|
131
|
|
|
{ |
|
132
|
|
|
$filter = '^\\.'; |
|
133
|
|
|
|
|
134
|
|
|
$this->addFilter($filter, false, $type); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Allows only hidden files |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $type |
|
141
|
|
|
*/ |
|
142
|
|
|
public function isHidden($type = null) |
|
143
|
|
|
{ |
|
144
|
|
|
$filter = '^\\.'; |
|
145
|
|
|
|
|
146
|
|
|
$this->addFilter($filter, true, $type); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.