mmgdev /htdocs/wp-content/plugins/backupbuddy/pluginbuddy/classes/filesystem.php

Language PHP Lines 343
MD5 Hash f621efe8671d718f6a2a334c495fbf8b
Repository https://github.com/akosweb/mmgdev.git View Raw File View Project SPDX
  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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php



/*	class pluginbuddy_filesystem
 *	@author Dustin Bolton
 *	
 *	Handles interfacing with the file system.
 */
class pb_backupbuddy_filesystem {
	
	
	
	// ********** PUBLIC PROPERTIES **********
	
	
	
	// ********** PRIVATE PROPERTIES **********
	
	
	
	// ********** FUNCTIONS **********
	
	
	
	/*	pluginbuddy_filesystem->__construct()
	 *	
	 *	Default constructor.
	 *	
	 *	@return		null
	 */
	function __construct() {
		
	} // End __construct().
	
	
	
	/*	pb_backupbuddy::$filesystem->mkdir()
	 *	
	 *	mkdir that defaults to recursive behaviour. 99% of the time this is what we want.
	 *	
	 *	@param		$pathname		string		Path to create.
	 *	@param		$mode			int			Default: 0777. See PHP's mkdir() function for details.
	 *	@param		$recursive		boolean		Default: true. See PHP's mkdir() function for details.
	 *	@return						boolean		Returns TRUE on success or FALSE on failure.
	 */
	public static function mkdir( $pathname, $mode = 0777, $recursive = true) {
		return @mkdir( $pathname, $mode, $recursive );
	} // End mkdir().
	
	
	
	/*	pluginbuddy_filesystem->unlink_recursive()
	 *	
	 *	Unlink a directory recursively. Files all files and directories within. USE WITH CAUTION.
	 *	
	 *	@param		string		$dir		Directory to delete -- all contents within, subdirectories, files, etc will be permanently deleted.
	 *	@return		boolean					True on success; else false.
	 */
	function unlink_recursive( $dir ) {
		if ( defined( 'PB_DEMO_MODE' ) ) {
			return false;
		}
		
		if ( !file_exists( $dir ) ) {
			return true;
		}
		if ( !is_dir( $dir ) || is_link( $dir ) ) {
			return unlink($dir);
		}
		foreach ( scandir( $dir ) as $item ) {
			if ( $item == '.' || $item == '..' ) {
				continue;
			}
			if ( !$this->unlink_recursive( $dir . "/" . $item ) ) {
				@chmod( $dir . "/" . $item, 0777 );
				if ( !$this->unlink_recursive( $dir . "/" . $item ) ) {
					return false;
				}
			}
		}
		return @rmdir($dir);
	} // End unlink_recursive().
	
	
	
	/**
	 *	pluginbuddy_filesystem->deepglob()
	 *
	 *	Like the glob() function except walks down into paths to create a full listing of all results in the directory and all subdirectories.
	 *	This is essentially a recursive glob() although it does not use recursion to perform this.
	 *
	 *	@param		string		$dir		Path to pass to glob and walk through.
	 *	@return		array					Returns array of all matches found.
	 */
	function deepglob( $dir ) {
		$items = glob( $dir . '/*' );
		
		for ( $i = 0; $i < count( $items ); $i++ ) {
			if ( is_dir( $items[$i] ) ) {
				$add = glob( $items[$i] . '/*' );
				$items = array_merge( $items, $add );
			}
		}
		
		return $items;
	} // End deepglob().
	
	
	
	function recursive_copy( $src, $dst ) {
		//pb_backupbuddy::status( 'details', 'Copying `' . $src . '` to `' . $dst . '`.' );
		if ( is_dir( $src ) ) {
			pb_backupbuddy::status( 'details', 'Copying directory `' . $src . '` to `' . $dst . '` recursively.' );
			@$this->mkdir( $dst, 0777 );
			$files = scandir($src);
			foreach ( $files as $file ) {
				if ($file != "." && $file != "..") {
					$this->recursive_copy("$src/$file", "$dst/$file");
				}
			}
		} elseif ( file_exists( $src ) ) {
			@copy( $src, $dst ); // Todo: should this need suppression? Media copying was throwing $dst is directory errors.
		}
	}
	
	
	// RH added; from Chris?
	/*
	
	public function custom_copy( $source, $destination, $args = array() ) {
		$default_args = array(
			'max_depth'    => 100,
			'folder_mode'  => 0755,
			'file_mode'    => 0744,
			'ignore_files' => array(),
		);
		$args = array_merge( $default_args, $args );
		
		return $this->_custom_copy( $source, $destination, $args );
	} // End custom_copy().
	
	
	
	private function _custom_copy( $source, $destination, $args, $depth = 0 ) {
		if ( $depth > $args['max_depth'] )
			return true;
			
		if ( in_array( basename( $source ), $args[ 'ignore_files' ] ) ) return true;
		
		if ( is_file( $source ) ) {
			if ( is_dir( $destination ) || preg_match( '|/$|', $destination ) ) {
				$destination = preg_replace( '|/+$|', '', $destination );
				
				$destination = "$destination/" . basename( $source );
			}
			
			if ( false === $this->mkdir( dirname( $destination ), $args['folder_mode'] ) )
				return false;
			
			if ( false === @copy( $source, $destination ) )
				return false;
			
			@chmod( $destination, $args['file_mode'] );
			
			return true;
		}
		else if ( is_dir( $source ) || preg_match( '|/\*$|', $source ) ) {
			if ( preg_match( '|/\*$|', $source ) )
				$source = preg_replace( '|/\*$|', '', $source );
			else if ( preg_match( '|/$|', $destination ) )
				$destination = $destination . basename( $source );
			
			$destination = preg_replace( '|/$|', '', $destination );
			
			$files = array_diff( array_merge( glob( $source . '/.*' ), glob( $source . '/*' ) ), array( $source . '/.', $source . '/..' ) );
			
			if ( false === @mkdir( $destination ) )
				return false;
			
			$result = true;
			
			foreach ( (array) $files as $file ) {
				if ( false === $this->_custom_copy( $file, "$destination/", $args, $depth + 1 ) )
					$result = false;
			}
			
			return $result;
		}
		
		return false;
	} // End _copy().
	
	
	*/

	
	// todo: document
	// $exclusions is never modified so just use PHP's copy on modify default behaviour for memory management.
	/*	function_name()
	 *	
	 *	function description
	 *	@param		array/bool		Array of directory paths to exclude.  If true then this directory is excluded so no need to check with exclusion directory.
	 *	@return		array			array( TOTAL_DIRECTORY_SIZE, TOTAL_SIZE_WITH_EXCLUSIONS_TAKEN_INTO_ACCOUNT, OBJECTS_FOUND, OBJECTS_FOUND_WITH_EXCLUSIONS )
	 */
	function dir_size_map( $dir, $base, $exclusions, &$dir_array ) {
		$dir = rtrim( $dir, '/\\' ); // Force no trailing slash.
		
		if( !is_dir( $dir ) ) {
			return 0;
		}
		
		$ret = 0;
		$ret_with_exclusions = 0;
		$ret_objects = 0;
		$ret_objects_with_exclusions = 0;
		$exclusions_result = $exclusions;
		$sub = @opendir( $dir );
		if ( false === $sub ) { // Cannot access.
			pb_backupbuddy::alert( 'Error #568385: Unable to access directory: `' . $dir . '`. Verify proper permissions.', true );
			return 0;
		} else {
			while( $file = readdir( $sub ) ) {
				$exclusions_result = $exclusions;
				
				$dir_path = '/' . str_replace( $base, '', $dir . '/' . $file ); //str_replace( $base, '', $dir . $file . '/' );
				
				if ( ( $file == '.' ) || ( $file == '..' ) ) {
					
					// Do nothing.
					
				} elseif ( is_dir( $dir . '/' . $file ) ) { // DIRECTORY.
					
					if ( ( $exclusions === true ) || self::in_array_substr( $exclusions, $dir_path, '/' ) ) {
						$exclusions_result = true;
					}
					$result = $this->dir_size_map( $dir . '/' . $file . '/', $base, $exclusions, $dir_array );
					$this_size = $result[0];
					$this_objects = $result[2];
					
					if ( $exclusions_result === true ) { // If excluding then wipe excluded value.
						$this_size_with_exclusions = false;
						$this_objects_with_exclusions = 0;
					} else {
						$this_size_with_exclusions = $result[1]; // / 1048576 );
						$this_objects_with_exclusions = $result[3]; // / 1048576 );
					}
					
					$dir_array[ $dir_path ] = array( $this_size, $this_size_with_exclusions, $this_objects, $this_objects_with_exclusions ); // $dir_array[ DIRECTORY_PATH ] = DIRECTORY_SIZE;
					
					$ret += $this_size;
					$ret_objects += $this_objects;
					$ret_with_exclusions += $this_size_with_exclusions;
					$ret_objects_with_exclusions += $this_objects_with_exclusions;
					
					unset( $file );
					
				} else { // FILE.
					
					$stats = @stat( $dir . '/' . $file );
					if ( is_array( $stats ) ) {
						$ret += $stats['size'];
						$ret_objects++;
						if ( ( $exclusions !== true ) && !in_array( $dir_path, $exclusions ) ) { // Not excluding.
							$ret_with_exclusions += $stats['size'];
							$ret_objects_with_exclusions++;
						}
					}
					unset( $file );
					
				}
			}
			closedir( $sub );
			unset( $sub );
			return array( $ret, $ret_with_exclusions, $ret_objects, $ret_objects_with_exclusions );
		}
	} // End dir_size_map().
	
	
	
	public static function in_array_substr( $haystack, $needle, $trailing = '' ) {
		foreach( $haystack as $hay ) {
			if ( ( $hay . $trailing ) == substr( $needle . $trailing, 0, strlen( $hay . $trailing ) ) ) {
				//echo $needle . '~' . $hay . '<br>';
				return true;
			}
		}
		
		return false;
	}
	
	
	public function exit_code_lookup( $code ) {
		switch( (string)$code ) {
			case '0':
				return 'Command completed & returned normally.';
				break;
			case '127':
				return 'Command not found.';
				break;
			case '152':
				return 'SIGXCPU 152; CPU time limit exceeded.';
				break;
			case '153':
				return 'SIGXFSZ 153; File size limit exceeded. Verify enough free space exists & filesystem max size not exceeded.';
				break;
			case '158':
				return 'SIGXCPU 158; CPU time limit exceeded.';
				break;
			case '159':
				return 'SIGXFSZ 159; File size limit exceeded. Verify enough free space exists & filesystem max size not exceeded.';
				break;
			default:
				return '-No information available for this exit code- See: https://wiki.ncsa.illinois.edu/display/MRDPUB/Batch+Exit+Codes ';
				break;
		}
	}
	
	// Newest to oldest.
	function glob_by_date( $pattern ) {
		$file_array = array();
		$glob_result = glob( $pattern );
		if ( ! is_array( $glob_result ) ) {
			$glob_result = array();
		}
		foreach ( $glob_result as $filename ) {
			$ctime = filectime( $filename );
			while( isset( $file_array[$ctime] ) ) { // Avoid collisions.
				$ctime = $ctime + 0.1;
			}
			$file_array[$ctime] = $filename; // or just $filename
		}
		krsort( $file_array );
		return $file_array;
		
	} // End glob_by_date().
	
	
} // End class pluginbuddy_settings.



?>
Back to Top