1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package File manager |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\file_manager\models; |
11
|
|
|
|
12
|
|
|
use FilesystemIterator; |
13
|
|
|
use gplcart\core\Hook; |
14
|
|
|
use gplcart\core\Module; |
15
|
|
|
use gplcart\modules\file_manager\helpers\Filter; |
16
|
|
|
use LimitIterator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Manages basic behaviors and data related to File manager module |
20
|
|
|
*/ |
21
|
|
|
class Scanner |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Hook class instance |
26
|
|
|
* @var \gplcart\core\Hook $hook |
27
|
|
|
*/ |
28
|
|
|
protected $hook; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Module class instance |
32
|
|
|
* @var \gplcart\core\Module $module |
33
|
|
|
*/ |
34
|
|
|
protected $module; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Hook $hook |
38
|
|
|
* @param Module $module |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Hook $hook, Module $module) |
41
|
|
|
{ |
42
|
|
|
$this->hook = $hook; |
43
|
|
|
$this->module = $module; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns an array of scanned files or counts them |
48
|
|
|
* @param string $directory |
49
|
|
|
* @param array $options |
50
|
|
|
* @return array|integer |
51
|
|
|
*/ |
52
|
|
|
public function scan($directory, array $options = array()) |
53
|
|
|
{ |
54
|
|
|
set_time_limit(0); |
55
|
|
|
|
56
|
|
|
$options += array( |
57
|
|
|
'sort' => null, |
58
|
|
|
'order' => null, |
59
|
|
|
'filter_key' => null, |
60
|
|
|
'filter_value' => null |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$result = null; |
64
|
|
|
$this->hook->attach('module.file_manager.scan.before', $directory, $options, $result, $this); |
65
|
|
|
|
66
|
|
|
if (isset($result)) { |
67
|
|
|
return $result; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$filters = $this->getFilters(); |
71
|
|
|
$iterator = new Filter(new FilesystemIterator($directory), $options, $filters); |
72
|
|
|
|
73
|
|
|
if (!empty($options['count'])) { |
74
|
|
|
return iterator_count($iterator); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!empty($options['limit'])) { |
78
|
|
|
list($page, $limit) = $options['limit']; |
79
|
|
|
$iterator = new LimitIterator($iterator, $page, $limit); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$result = iterator_to_array($iterator); |
83
|
|
|
$this->sort($result, $options['sort'], $options['order']); |
84
|
|
|
|
85
|
|
|
$this->hook->attach('module.file_manager.scan.after', $directory, $options, $result, $this); |
86
|
|
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sorts an array of files |
91
|
|
|
* @param array $files |
92
|
|
|
* @param string $sort |
93
|
|
|
* @param string $order |
94
|
|
|
*/ |
95
|
|
|
protected function sort(array &$files, $sort, $order) |
96
|
|
|
{ |
97
|
|
|
$sorters = $this->getSorters(); |
98
|
|
|
|
99
|
|
|
if (isset($sorters[$sort]['handlers']['sort'])) { |
100
|
|
|
$function = $sorters[$sort]['handlers']['sort']; |
101
|
|
|
// Suppress errors, see https://bugs.php.net/bug.php?id=50688 |
102
|
|
|
@uasort($files, function ($a, $b) use ($function, $order) { |
|
|
|
|
103
|
|
|
return $function($a, $b, $order); |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns an array of supported filters |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getFilters() |
113
|
|
|
{ |
114
|
|
|
$filters = &gplcart_static('module.file_manager.filter.list'); |
115
|
|
|
|
116
|
|
|
if (isset($filters)) { |
117
|
|
|
return $filters; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$filters = (array) gplcart_config_get(__DIR__ . '/../config/filters.php'); |
121
|
|
|
$this->hook->attach('module.file_manager.filter.list', $filters, $this); |
122
|
|
|
return $filters; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns an array of file sorters |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getSorters() |
130
|
|
|
{ |
131
|
|
|
$sorters = &gplcart_static('module.file_manager.sorter.list'); |
132
|
|
|
|
133
|
|
|
if (isset($sorters)) { |
134
|
|
|
return $sorters; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$sorters = (array) gplcart_config_get(__DIR__ . '/../config/sorters.php'); |
138
|
|
|
$this->hook->attach('module.file_manager.sorter.list', $sorters, $this); |
139
|
|
|
return $sorters; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns a string with initial path to scan files |
144
|
|
|
* @param bool|null|string $absolute |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getInitialPath($absolute = false) |
148
|
|
|
{ |
149
|
|
|
$path = $this->module->getSettings('file_manager', 'initial_path'); |
150
|
|
|
return $absolute ? gplcart_file_absolute($path) : $path; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: