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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
<?php /** * pluginbuddy_zbdir Class * * Provides a directory class for zipbuddy for building a directory tree for backup * * Version: 1.0.0 * Author: * Author URI: * * @param $parent object Optional parent object which can provide functions for reporting, etc. * @return null * */ if ( !class_exists( "pluginbuddy_zbdir" ) ) {
class pluginbuddy_zbdir { // status method type parameter values - would like a class for this const STATUS_TYPE_DETAILS = 'details'; const NORM_DIRECTORY_SEPARATOR = '/'; const DIRECTORY_SEPARATORS = '/\\';
public $_version = '1.0';
/** * The path of this directory node * * @var path string */ protected $_path = ""; /** * The absolute paths to be excluded, must be / terminated * * @var paths_to_exclude array of string */ protected $_paths_to_exclude = array();
/** * The directory listing items to be ignored * * @var items_to_ignore array of string */ protected $_items_to_ignore = array( ".", "..", ".DS_Store" );
/** * The items that are terminals and we can add directly for this directory (absolute paths) * * @var terminals array of string */ protected $_terminals = array();
/** * The branch nodes of subordinate directories that are on an exclusion path * * @var branches array of string => pluginbuddy_zbdir */ protected $_branches = array();
/** * Whether or not we can call a status calback * * @var have_status_callback bool */ protected $_have_status_callback = false; /** * Object->method array for status function * * @var status_callback array */ protected $_status_callback = array(); /** * __construct() * * Default constructor. * * @param string $path The path to form a node for * @param array $excludes The list of dirs/files to exclude (absolute paths with / terminator for dirs) * @return null * */ public function __construct( $path, $excludes = array() ) {
// Normalize the trailing directory separator on the path $path = rtrim( $path, self::DIRECTORY_SEPARATORS ) . self::NORM_DIRECTORY_SEPARATOR; // Normalize platform specific directory separators in path $this->_path = str_replace( DIRECTORY_SEPARATOR, self::NORM_DIRECTORY_SEPARATOR, $path ); $this->_paths_to_exclude = $excludes; $content = scandir( $this->_path ); // Get the directory content, will be simple names // Process each item for ignoring, treating as a terminal or as a branch foreach ( $content as &$item ) {
// Initially check the simple name if ( in_array( $item, $this->_items_to_ignore ) ) {
// This is just fluff in the directory listing continue; } elseif ( is_dir( ( $this->_path . $item ) ) ) {
// It's a directory, check for matching exclusion or being prefix of exclusion if ( in_array( ( $this->_path . $item . self::NORM_DIRECTORY_SEPARATOR ), $this->_paths_to_exclude ) ) { // Exact match to an exclusion, exclude this directory completely continue; } elseif ( $this->in_array_prefix( ( $this->_path . $item . self::NORM_DIRECTORY_SEPARATOR ), $this->_paths_to_exclude ) ) {
// Need a new node, add to the node array (absolute dir path is key) $this->_branches[ ( $this->_path . $item ) ] = new pluginbuddy_zbdir( ( $this->_path . $item . self::NORM_DIRECTORY_SEPARATOR ), $this->_paths_to_exclude, $this ); } else { // Neither exclusion nor exclusion prefix so well treat it as a terminal $this->_terminals[] = ( $this->_path . $item ); } } else {
// Assume it's a file, check for matching exclusion if ( in_array( ( $this->_path . $item ), $this->_paths_to_exclude ) ) { // Exact match to an exclusion, exclude this file completely continue; } else { // Not an exclusion so it's a terminal $this->_terminals[] = ( $this->_path . $item ); } } } } /** * __destruct() * * Default destructor. * * @return null * */ public function __destruct( ) {
} /** * set_status_callback() * * Sets a reference to the function to call for each status update. * Argument must at least be a non-empty array with 2 elements * * @param array $callback Object->method to call for status updates. * @return null * */ public function set_status_callback( $callback = array() ) { if ( is_array( $callback ) && !empty( $callback ) && ( 2 == count( $callback ) ) ) { $this->_status_callback = $callback; $this->_have_status_callback = true;
} } /** * status() * * Invoke status method of parent if it exists * Must be at least one parameter otherwise ignore the call * * @param string $type (Expected) Status message type. * @param string $message (Expected) Status message. * @return null * */ public function status() { if ( $this->_have_status_callback && ( func_num_args() > 0 ) ) {
$args = func_get_args(); call_user_func_array( $this->_status_callback, $args ); } } /** * get_terminals() * * Returns the array of terminals from this dir plus subordinates * * @return array Flat array of terminal filenames and directory names * */ public function get_terminals( ) { // Minimum is our terminals $all_terminals = $this->_terminals; // Now add terminals from each subordinate foreach ( $this->_branches as $branch ) { $all_terminals = array_merge( $all_terminals, $branch->get_terminals() ); } return $all_terminals; } /** * get_relative_excludes() * * Returns the array of exclusions with optional directory prefix removed * * @param string The base directory prefix to be removed * @return array Flat array of relative (to site root) excluded filenames and directory names * */ public function get_relative_excludes( $base = '' ) { // The basedir must have a trailing normalized directory separator $basedir = ( rtrim( trim( $base ), self::DIRECTORY_SEPARATORS ) ) . self::NORM_DIRECTORY_SEPARATOR; // Normalize platform specific directory separators in path $basedir = str_replace( DIRECTORY_SEPARATOR, self::NORM_DIRECTORY_SEPARATOR, $basedir ); $relative_excludes = $this->_paths_to_exclude; foreach ( $relative_excludes as &$exclude ) { // Remove base prefix but leave leading normalized directory separator $exclude = str_replace( rtrim( $basedir, self::NORM_DIRECTORY_SEPARATOR ), '', $exclude ); } return $relative_excludes; } /** * in_array_prefix() * * Check if the given string is a prefix of any string in the given array * * @param string $prefix The prefix string * @param array $candidates The array of strings * @return bool true if the string is a prefix, false otherwise * */ public function in_array_prefix( $prefix, array $candidates ) {
foreach ( $candidates as $candidate ) { if ( !( false === strpos( $candidate, $prefix ) ) ) {
// We found the prefix return true; } } // Got this far so not a prefix return false; } } // end pluginbuddy_zbdir class. } ?>
|