| 1 |
<?php |
| 2 |
/************************************************************ |
| 3 |
* This plugin was modified by Revmakx * |
| 4 |
* Copyright (c) 2012 Revmakx * |
| 5 |
* www.revmakx.com * |
| 6 |
* * |
| 7 |
************************************************************/ |
| 8 |
/************************************************************* |
| 9 |
* |
| 10 |
* iwp-file-iterator.php |
| 11 |
* |
| 12 |
* Manage Backups |
| 13 |
* |
| 14 |
* |
| 15 |
**************************************************************/ |
| 16 |
if ( ! defined('ABSPATH') ) |
| 17 |
die(); |
| 18 |
|
| 19 |
class IWP_Seek_Iterator{ |
| 20 |
|
| 21 |
public $iterator_common; |
| 22 |
public $external_obj; |
| 23 |
public $iterator_loop_limit; |
| 24 |
public $path; |
| 25 |
public $type; |
| 26 |
public $query; |
| 27 |
public $processed_files; |
| 28 |
public $app_functions; |
| 29 |
public $is_recursive; |
| 30 |
public $exclude_class_obj; |
| 31 |
public $seek; |
| 32 |
public $counter; |
| 33 |
|
| 34 |
public function __construct($type = false, $iterator_loop_limit = 1000){ |
| 35 |
$this->type = $type; |
| 36 |
$this->iterator_loop_limit = $iterator_loop_limit; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_seekable_files_obj($path){ |
| 40 |
|
| 41 |
$temp_path = $path; |
| 42 |
|
| 43 |
// IWP_add_abspath($path); |
| 44 |
|
| 45 |
$path = is_valid_path($path); |
| 46 |
|
| 47 |
if( is_array($path) ) { |
| 48 |
return $path; |
| 49 |
} |
| 50 |
|
| 51 |
$this->path = $temp_path; |
| 52 |
|
| 53 |
return new DirectoryIterator($path); |
| 54 |
} |
| 55 |
|
| 56 |
public function process_iterator($path, $offset = false, $is_recursive = false){ |
| 57 |
|
| 58 |
$iterator = $this->get_seekable_files_obj($path); |
| 59 |
|
| 60 |
if (empty($iterator)) { |
| 61 |
return ; |
| 62 |
} |
| 63 |
|
| 64 |
$this->seek = empty($offset) ? array() : explode('-', $offset); |
| 65 |
|
| 66 |
$this->counter = 0; |
| 67 |
$this->is_recursive = $is_recursive; |
| 68 |
|
| 69 |
if ($is_recursive) { |
| 70 |
$folder_list_result = $this->recursive_iterator($iterator, false); |
| 71 |
} else { |
| 72 |
$folder_list_result = $this->iterator($iterator); |
| 73 |
} |
| 74 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 75 |
return $folder_list_result; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function process_file($iterator, $key){ |
| 80 |
$folder_list_result = iwp_file_process_file($iterator, $this->is_recursive, $this->path, $key, $this->counter, $this->iterator_loop_limit); |
| 81 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 82 |
return $folder_list_result; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private function extra_check_query(){ |
| 87 |
if (!empty($this->query)) { |
| 88 |
insert_into_current_process($this->query); |
| 89 |
$this->query = ''; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function iterator($iterator){ |
| 94 |
//Moving satelite into position. |
| 95 |
$this->seek_offset($iterator); |
| 96 |
|
| 97 |
while ($iterator->valid()) { |
| 98 |
|
| 99 |
$this->counter++; |
| 100 |
|
| 101 |
$recursive_path = $iterator->getPathname(); |
| 102 |
|
| 103 |
//Dont recursive iterator if its a dir or dot |
| 104 |
if ($iterator->isDot() || !$iterator->isReadable() || $iterator->isDir()) { |
| 105 |
|
| 106 |
//move to next file |
| 107 |
$iterator->next(); |
| 108 |
|
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
$key = $iterator->key(); |
| 113 |
|
| 114 |
$folder_list_result = $this->process_file( $iterator, $key ); |
| 115 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 116 |
return $folder_list_result; |
| 117 |
} |
| 118 |
|
| 119 |
//move to next file |
| 120 |
$iterator->next(); |
| 121 |
} |
| 122 |
|
| 123 |
$this->extra_check_query(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public function recursive_iterator($iterator, $key_recursive) { |
| 128 |
|
| 129 |
$this->seek_offset($iterator); |
| 130 |
|
| 131 |
while ($iterator->valid()) { |
| 132 |
|
| 133 |
//Forming current path from iterator |
| 134 |
$recursive_path = $iterator->getPathname(); |
| 135 |
$recursive_path = wp_normalize_path($recursive_path); |
| 136 |
|
| 137 |
//Mapping keys |
| 138 |
$key = ($key_recursive !== false ) ? $key_recursive . '-' . $iterator->key() : $iterator->key() ; |
| 139 |
|
| 140 |
//Do recursive iterator if its a dir |
| 141 |
if (!$iterator->isDot() && $iterator->isReadable() && $iterator->isDir() ) { |
| 142 |
|
| 143 |
if (!skip_file($recursive_path)) {//exclude |
| 144 |
//create new object for new dir |
| 145 |
$sub_iterator = new DirectoryIterator($recursive_path); |
| 146 |
|
| 147 |
$folder_list_result = $this->recursive_iterator($sub_iterator, $key); |
| 148 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 149 |
return $folder_list_result; |
| 150 |
} |
| 151 |
|
| 152 |
} else{ |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
//Ignore dots paths |
| 158 |
if(!$iterator->isDot()){ |
| 159 |
$folder_list_result = $this->process_file( $iterator, $key ); |
| 160 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 161 |
return $folder_list_result; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
//move to next file |
| 166 |
$iterator->next(); |
| 167 |
} |
| 168 |
|
| 169 |
$this->extra_check_query(); |
| 170 |
} |
| 171 |
|
| 172 |
private function seek_offset(&$iterator){ |
| 173 |
|
| 174 |
if(!count($this->seek)){ |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
//Moving satelite into position. |
| 179 |
$iterator->seek($this->seek[0]); |
| 180 |
|
| 181 |
//remove positions from the array after moved satelite |
| 182 |
unset($this->seek[0]); |
| 183 |
|
| 184 |
//reset array index |
| 185 |
$this->seek = array_values($this->seek); |
| 186 |
|
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
function scan_entire_site($v_filedescr_list){ |
| 191 |
global $wpdb; |
| 192 |
|
| 193 |
$sql = "TRUNCATE TABLE ".$wpdb->base_prefix."iwp_processed_iterator"; |
| 194 |
$response = $wpdb->get_results($sql); |
| 195 |
// $dir = get_root_dir_folders(); |
| 196 |
// save_dir_list($dir); |
| 197 |
// $dir = get_wp_content_dir_folders(); |
| 198 |
// save_dir_list($dir); |
| 199 |
// $dir = get_uploads_dir_folders(); |
| 200 |
// save_dir_list($dir); |
| 201 |
// get_db_backup_file(); |
| 202 |
// save_dir_list(); |
| 203 |
save_deep_dir_list($v_filedescr_list); |
| 204 |
} |
| 205 |
|
| 206 |
function get_root_dir_folders(){ |
| 207 |
$files_obj = get_files_obj_by_path(ABSPATH); |
| 208 |
return add_dir_list($files_obj); |
| 209 |
} |
| 210 |
|
| 211 |
function add_dir_list($files_obj){ |
| 212 |
foreach ($files_obj as $key => $file_obj) { |
| 213 |
|
| 214 |
$file = $file_obj->getPathname(); |
| 215 |
|
| 216 |
if (!IWP_is_dir($file)) { |
| 217 |
/// $this->files[] = $file; |
| 218 |
} else { |
| 219 |
// IWP_remove_abspath($file); |
| 220 |
$dir[] = $file; |
| 221 |
} |
| 222 |
} |
| 223 |
return $dir; |
| 224 |
} |
| 225 |
|
| 226 |
function get_files_obj_by_path($path, $recursive = false){ |
| 227 |
|
| 228 |
// IWP_add_abspath($path); |
| 229 |
|
| 230 |
$path = is_valid_path($path); |
| 231 |
|
| 232 |
if( is_array($path) ) { |
| 233 |
return $path; |
| 234 |
} |
| 235 |
|
| 236 |
if($recursive){ |
| 237 |
return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path , RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD); |
| 238 |
} |
| 239 |
|
| 240 |
return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path , RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CATCH_GET_CHILD); |
| 241 |
} |
| 242 |
|
| 243 |
function save_dir_list($dir){ |
| 244 |
$qry = ''; |
| 245 |
$deep_dirs = array( |
| 246 |
ABSPATH |
| 247 |
); |
| 248 |
foreach ($dir as $dir) { |
| 249 |
if (in_array($dir, $deep_dirs)) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
$qry .= empty($qry) ? "('" : ",('" ; |
| 255 |
$qry .= wp_normalize_path($dir) . "', '0')"; |
| 256 |
|
| 257 |
} |
| 258 |
insert_into_iterator_process($qry); |
| 259 |
} |
| 260 |
|
| 261 |
function save_deep_dir_list($deep_dirs){ |
| 262 |
// $deep_dirs = array( |
| 263 |
// ABSPATH, |
| 264 |
// ); |
| 265 |
$qry = ''; |
| 266 |
foreach ($deep_dirs as $dir) { |
| 267 |
$qry .= empty($qry) ? "('" : ",('" ; |
| 268 |
$qry .= wp_normalize_path($dir['filename']) . "', '0')"; |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
insert_into_iterator_process($qry); |
| 273 |
} |
| 274 |
|
| 275 |
function insert_into_iterator_process($qry){ |
| 276 |
global $wpdb; |
| 277 |
|
| 278 |
$sql = "insert into ".$wpdb->base_prefix."iwp_processed_iterator (`name`,`offset`) values $qry"; |
| 279 |
$response = $wpdb->get_results($sql); |
| 280 |
} |
| 281 |
|
| 282 |
function iwp_iterator(){ |
| 283 |
$break = false; |
| 284 |
$deep_dirs_array = array( |
| 285 |
|
| 286 |
); |
| 287 |
|
| 288 |
while(!$break){ |
| 289 |
$dir_meta = get_unfnished_folder(); |
| 290 |
$deep_dirs = false; |
| 291 |
|
| 292 |
if (empty($dir_meta) || $dir_meta['offset'] === -1) { |
| 293 |
$break = true; |
| 294 |
continue; |
| 295 |
} |
| 296 |
|
| 297 |
if( array_search($dir_meta['name'], $deep_dirs_array) !== false ){ |
| 298 |
$deep_dirs = true; |
| 299 |
} |
| 300 |
|
| 301 |
$file = $dir_meta['name']; |
| 302 |
|
| 303 |
if ($deep_dirs === false && skip_file($file) === true) { |
| 304 |
update_iterator($dir_meta['name'], -1); |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
if(IWP_is_dir($file)){ |
| 309 |
$folder_list_result = iwp_iterate_dir($dir_meta['name'], $dir_meta['offset'], $deep_dirs); |
| 310 |
} else { |
| 311 |
$folder_list_result = iwp_iterate_file($dir_meta['name'], $update_status = true); |
| 312 |
} |
| 313 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 314 |
return $folder_list_result; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
function iwp_iterate_dir($live_path, $offset, $deep_dirs){ |
| 320 |
|
| 321 |
$is_recursive = ($deep_dirs) ? false : true; |
| 322 |
|
| 323 |
try{ |
| 324 |
$obj = new IWP_Seek_Iterator(); |
| 325 |
$folder_list_result = $obj->process_iterator($live_path, $offset, $is_recursive); |
| 326 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 327 |
return $folder_list_result; |
| 328 |
} |
| 329 |
} catch(Exception $e){ |
| 330 |
|
| 331 |
$exception_msg = $e->getMessage(); |
| 332 |
|
| 333 |
update_iterator($live_path, 0); |
| 334 |
return; |
| 335 |
|
| 336 |
} |
| 337 |
update_iterator($live_path, -1); |
| 338 |
} |
| 339 |
|
| 340 |
function iwp_iterate_file($live_file, $update_status = false){ |
| 341 |
// initFileSystem(); |
| 342 |
// IWP_add_abspath($live_file); |
| 343 |
global $old_next_file_index, $next_file_index,$iwp_v_options,$archive; |
| 344 |
$live_file = wp_normalize_path($live_file); |
| 345 |
$folder_list_result = $archive->fileDetailsExpandManual($live_file, $iwp_v_options, $next_file_index); |
| 346 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 347 |
return $folder_list_result; |
| 348 |
} |
| 349 |
if ($update_status) { |
| 350 |
update_iterator($live_file, -1); |
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
function iwp_file_process_file($iterator, $is_recursive, $path, $key, &$counter, $iterator_loop_limit){ |
| 356 |
$file = $iterator->getPathname(); |
| 357 |
|
| 358 |
if (!$iterator->isReadable()) { |
| 359 |
return ; |
| 360 |
} |
| 361 |
|
| 362 |
$file = wp_normalize_path($file); |
| 363 |
|
| 364 |
if (!$is_recursive && IWP_is_dir($file)){ |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
if (skip_file($file) === true) { |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
if(IWP_is_dir($file)){ |
| 373 |
return; |
| 374 |
} |
| 375 |
$counter++; |
| 376 |
$folder_list_result = iwp_iterate_file($file); |
| 377 |
if(!empty($folder_list_result) && $folder_list_result['break']){ |
| 378 |
update_iterator($path, $key); |
| 379 |
$next_file_index = $folder_list_result['loop_count']; |
| 380 |
return $folder_list_result; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
function get_unfnished_folder() { |
| 387 |
global $wpdb; |
| 388 |
$sql = "SELECT * FROM ".$wpdb->base_prefix."iwp_processed_iterator WHERE `offset` != -1 LIMIT 1"; |
| 389 |
$response = $wpdb->get_results($sql, ARRAY_A); |
| 390 |
// IWP_log($response, '--------$response--------'); |
| 391 |
|
| 392 |
return empty($response) ? false : $response[0]; |
| 393 |
} |
| 394 |
|
| 395 |
function IWP_is_dir($good_path){ |
| 396 |
$good_path = wp_normalize_path($good_path); |
| 397 |
|
| 398 |
if (is_dir($good_path)) { |
| 399 |
return true; |
| 400 |
} |
| 401 |
|
| 402 |
$ext = pathinfo($good_path, PATHINFO_EXTENSION); |
| 403 |
|
| 404 |
if (!empty($ext)) { |
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
if (is_file($good_path)) { |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
return true; |
| 413 |
} |
| 414 |
|
| 415 |
function skip_file($file){ |
| 416 |
global $iwp_v_options, $archive; |
| 417 |
if(!empty($iwp_v_options[IWP_PCLZIP_OPT_IWP_EXCLUDE])){ |
| 418 |
$exclude_data = $iwp_v_options[IWP_PCLZIP_OPT_IWP_EXCLUDE]; |
| 419 |
}else{ |
| 420 |
$exclude_data = array(); |
| 421 |
} |
| 422 |
|
| 423 |
if(!is_readable($file)){ |
| 424 |
return true; |
| 425 |
} |
| 426 |
if( $file == "." && $file == ".." ) { |
| 427 |
return true; |
| 428 |
} |
| 429 |
if ($archive->excludeDirFromScan($file, $exclude_data)) { |
| 430 |
return true; |
| 431 |
} |
| 432 |
|
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
function update_iterator($table, $offset) { |
| 437 |
upsert(array( |
| 438 |
'name' => $table, |
| 439 |
'offset' => $offset, |
| 440 |
)); |
| 441 |
} |
| 442 |
|
| 443 |
function upsert($data) { |
| 444 |
global $wpdb; |
| 445 |
$this_table_name = 'iwp_processed_iterator'; //in case, if we are changing table name. |
| 446 |
$exists = $wpdb->get_row("SELECT id FROM " . $wpdb->base_prefix. $this_table_name . " WHERE name = '". $data['name']."'" ); |
| 447 |
|
| 448 |
if ($exists) { |
| 449 |
$result = $wpdb->update($wpdb->base_prefix . $this_table_name, $data, array( 'name' => $data['name'])); |
| 450 |
}else{ |
| 451 |
$result = $wpdb->insert($wpdb->base_prefix . $this_table_name, $data); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
function is_valid_path($path){ |
| 456 |
$default = array(); |
| 457 |
|
| 458 |
if (empty($path)) { |
| 459 |
return $default; |
| 460 |
} |
| 461 |
|
| 462 |
$path = rtrim($path, '/'); |
| 463 |
|
| 464 |
$path = wp_normalize_path($path); |
| 465 |
|
| 466 |
if (empty($path)) { |
| 467 |
return $default; |
| 468 |
} |
| 469 |
|
| 470 |
$basename = basename($path); |
| 471 |
|
| 472 |
if ($basename == '..' || $basename == '.') { |
| 473 |
return $default; |
| 474 |
} |
| 475 |
|
| 476 |
if (!is_readable($path)) { |
| 477 |
return $default; |
| 478 |
} |
| 479 |
|
| 480 |
return $path; |
| 481 |
} |