Ticket #21403: wp-scandir.diff
File wp-scandir.diff, 16.4 KB (added by , 12 years ago) |
---|
-
wp-includes/class-wp-theme.php
966 966 if ( ! is_dir( $path ) ) 967 967 return false; 968 968 969 if ( $extensions ) {970 $extensions = (array) $extensions;971 $_extensions = implode( '|', $extensions );972 }973 974 969 $relative_path = trailingslashit( $relative_path ); 975 970 if ( '/' == $relative_path ) 976 971 $relative_path = ''; 977 972 978 $results = scandir( $path);973 $results = wp_scandir( $path, $extensions ); 979 974 $files = array(); 980 975 976 if ( empty( $results ) ) 977 return $files; 978 981 979 foreach ( $results as $result ) { 982 if ( '.' == $result[0] )983 continue; 984 if ( is_dir( $ path . '/' . $result ) ) {985 if ( ! $depth || 'CVS' == $result)980 $basename = basename( $result ); 981 982 if ( is_dir( $result ) ) { 983 if ( ! $depth ) 986 984 continue; 987 $found = self::scandir( $ path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result);985 $found = self::scandir( $result, $extensions, $depth - 1 , $relative_path . $basename ); 988 986 $files = array_merge_recursive( $files, $found ); 989 } else if ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ){990 $files[ $relative_path . $ result ] = $path . '/' .$result;987 } else { 988 $files[ $relative_path . $basename ] = $result; 991 989 } 992 990 } 993 991 -
wp-includes/theme.php
372 372 foreach ( $wp_theme_directories as $theme_root ) { 373 373 374 374 // Start with directories in the root of the current theme directory. 375 $dirs = @scandir( $theme_root );375 $dirs = wp_scandir( $theme_root ); 376 376 if ( ! $dirs ) 377 377 return false; 378 378 379 foreach ( $dirs as $dir ) { 379 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' ) 380 $dir = basename( $dir ); 381 382 if ( ! is_dir( $theme_root . '/' . $dir ) ) 380 383 continue; 384 381 385 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) { 382 386 // wp-content/themes/a-single-theme 383 387 // wp-content/themes is $theme_root, a-single-theme is $dir … … 389 393 $found_theme = false; 390 394 // wp-content/themes/a-folder-of-themes/* 391 395 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs 392 $sub_dirs = @scandir( $theme_root . '/' . $dir );396 $sub_dirs = wp_scandir( $theme_root . '/' . $dir ); 393 397 if ( ! $sub_dirs ) 394 398 return false; 399 395 400 foreach ( $sub_dirs as $sub_dir ) { 396 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' ) 401 $sub_dir = basename( $sub_dir ); 402 403 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) ) 397 404 continue; 398 405 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) ) 399 406 continue; -
wp-includes/load.php
439 439 } 440 440 441 441 /** 442 * Abstracts common implementation of PHP5 scandir 443 * 444 * @access public 445 * @since 3.5.0 446 * 447 * @param string $dir A directory to scan for files 448 * @param bool|$ext if set, check that files match extension(s) 449 * @param bool|$files_only whether to return directories 450 * 451 * @return null|array of absolute filepaths 452 */ 453 function wp_scandir( $dir, $ext = false, $files_only = false ) { 454 if ( !is_dir( $dir ) || !is_readable( $dir ) ) 455 return; 456 457 $paths = array(); 458 $scanned = @ scandir( $dir ); 459 460 if ( ! empty( $scanned ) ) { 461 $diffed = array_diff( $scanned, array( '..', '.', 'CVS' ) ); 462 if ( ! empty( $diffed ) ) { 463 464 foreach ( $diffed as $basename ) { 465 $path = $dir . DIRECTORY_SEPARATOR . $basename; 466 467 if ( is_file( $path ) ) { 468 if ( !empty( $ext ) ) { 469 $ext = (array) $ext; 470 $_ext = implode( '|', $ext ); 471 472 if ( ! preg_match( '~\.(' . $_ext . ')$~', $basename ) ) 473 continue; 474 } 475 } elseif ( !$files_only && !is_dir( $path ) ) { 476 continue; 477 } 478 479 $paths[] = $path; 480 } 481 } 482 } 483 484 return $paths; 485 } 486 487 /** 442 488 * Returns array of must-use plugin files to be included in global scope. 443 489 * 444 490 * The default directory is wp-content/mu-plugins. To change the default directory … … 450 496 * @return array Files to include 451 497 */ 452 498 function wp_get_mu_plugins() { 453 $mu_plugins = array(); 454 if ( !is_dir( WPMU_PLUGIN_DIR ) ) 455 return $mu_plugins; 456 if ( ! $dh = opendir( WPMU_PLUGIN_DIR ) ) 457 return $mu_plugins; 458 while ( ( $plugin = readdir( $dh ) ) !== false ) { 459 if ( substr( $plugin, -4 ) == '.php' ) 460 $mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin; 461 } 462 closedir( $dh ); 499 $mu_plugins = wp_scandir( WPMU_PLUGIN_DIR, 'php', true ); 500 if ( empty( $mu_plugins ) ) 501 return array(); 502 463 503 sort( $mu_plugins ); 464 504 465 505 return $mu_plugins; 466 506 } 467 507 -
wp-includes/ms-functions.php
1429 1429 * 1430 1430 * @since MU 1431 1431 * 1432 * @param string $dir ectory1432 * @param string $dir 1433 1433 * @return int 1434 1434 */ 1435 function recurse_dirsize( $dir ectory) {1435 function recurse_dirsize( $dir ) { 1436 1436 $size = 0; 1437 $dir = untrailingslashit( $dir ); 1437 1438 1438 $directory = untrailingslashit( $directory ); 1439 1440 if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) ) 1441 return false; 1442 1443 if ($handle = opendir($directory)) { 1444 while(($file = readdir($handle)) !== false) { 1445 $path = $directory.'/'.$file; 1446 if ($file != '.' && $file != '..') { 1447 if (is_file($path)) { 1448 $size += filesize($path); 1449 } elseif (is_dir($path)) { 1450 $handlesize = recurse_dirsize($path); 1451 if ($handlesize > 0) 1452 $size += $handlesize; 1453 } 1439 $paths = wp_scandir( $dir ); 1440 1441 if ( !empty( $paths ) ) { 1442 foreach ( $paths as $path ) { 1443 if ( is_file( $path ) ) { 1444 $size += filesize( $path ); 1445 } elseif ( is_dir( $path ) ) { 1446 $handlesize = recurse_dirsize( $path ); 1447 if ( $handlesize > 0 ) 1448 $size += $handlesize; 1454 1449 } 1455 1450 } 1456 closedir($handle);1457 1451 } 1458 1452 return $size; 1459 1453 } -
wp-admin/includes/plugin.php
179 179 * @param string $plugin Plugin ID 180 180 * @return array List of files relative to the plugin root. 181 181 */ 182 function get_plugin_files($plugin) { 182 function get_plugin_files( $plugin ) { 183 $plugin_files = array( $plugin ); 183 184 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin; 184 $dir = dirname($plugin_file); 185 $plugin_files = array($plugin); 186 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) { 187 $plugins_dir = @ opendir( $dir ); 188 if ( $plugins_dir ) { 189 while (($file = readdir( $plugins_dir ) ) !== false ) { 190 if ( substr($file, 0, 1) == '.' ) 191 continue; 192 if ( is_dir( $dir . '/' . $file ) ) { 193 $plugins_subdir = @ opendir( $dir . '/' . $file ); 194 if ( $plugins_subdir ) { 195 while (($subfile = readdir( $plugins_subdir ) ) !== false ) { 196 if ( substr($subfile, 0, 1) == '.' ) 197 continue; 198 $plugin_files[] = plugin_basename("$dir/$file/$subfile"); 199 } 200 @closedir( $plugins_subdir ); 201 } 202 } else { 203 if ( plugin_basename("$dir/$file") != $plugin ) 204 $plugin_files[] = plugin_basename("$dir/$file"); 205 } 206 } 207 @closedir( $plugins_dir ); 185 $dir = dirname( $plugin_file ); 186 if ( $dir === WP_PLUGIN_DIR ) 187 return $plugin_files; 188 189 $paths = wp_scandir( $dir ); 190 191 if ( !empty( $paths ) ) { 192 foreach ( $paths as $path ) { 193 if ( is_dir( $path ) ) { 194 $plugins_subdir = wp_scandir( $path ); 195 196 if ( !empty( $plugins_subdir ) ) 197 foreach ( $plugins_subdir as $subfile ) 198 $plugin_files[] = plugin_basename( $subfile ); 199 200 } else if ( plugin_basename( $path ) != $plugin ) 201 $plugin_files[] = plugin_basename( $path ); 208 202 } 209 203 } 210 204 211 205 return $plugin_files; 212 206 } 213 207 208 214 209 /** 215 210 * Check the plugins directory and retrieve all plugin files with plugin data. 216 211 * … … 231 226 * @param string $plugin_folder Optional. Relative path to single plugin folder. 232 227 * @return array Key is the plugin file path and the value is an array of the plugin data. 233 228 */ 234 function get_plugins( $plugin_folder = '') {229 function get_plugins( $plugin_folder = '' ) { 235 230 236 if ( ! $cache_plugins = wp_cache_get( 'plugins', 'plugins') )231 if ( ! $cache_plugins = wp_cache_get( 'plugins', 'plugins' ) ) 237 232 $cache_plugins = array(); 238 233 239 if ( isset( $cache_plugins[ $plugin_folder ]) )234 if ( isset( $cache_plugins[ $plugin_folder ] ) ) 240 235 return $cache_plugins[ $plugin_folder ]; 241 236 242 $wp_plugins = array 237 $wp_plugins = array(); 243 238 $plugin_root = WP_PLUGIN_DIR; 244 if ( !empty( $plugin_folder) )239 if ( !empty( $plugin_folder ) ) 245 240 $plugin_root .= $plugin_folder; 246 241 247 242 // Files in wp-content/plugins directory 248 $plugin s_dir = @ opendir( $plugin_root);243 $plugin_paths = wp_scandir( $plugin_root ); 249 244 $plugin_files = array(); 250 if ( $plugins_dir ) { 251 while (($file = readdir( $plugins_dir ) ) !== false ) { 252 if ( substr($file, 0, 1) == '.' ) 253 continue; 254 if ( is_dir( $plugin_root.'/'.$file ) ) { 255 $plugins_subdir = @ opendir( $plugin_root.'/'.$file ); 256 if ( $plugins_subdir ) { 257 while (($subfile = readdir( $plugins_subdir ) ) !== false ) { 258 if ( substr($subfile, 0, 1) == '.' ) 259 continue; 260 if ( substr($subfile, -4) == '.php' ) 261 $plugin_files[] = "$file/$subfile"; 262 } 263 closedir( $plugins_subdir ); 264 } 245 if ( !empty( $plugin_paths ) ) { 246 foreach ( $plugin_paths as $plugin_path ) { 247 if ( is_dir( $plugin_path ) ) { 248 $plugins_subdir = wp_scandir( $plugin_path, 'php', true ); 249 if ( !empty( $plugins_subdir ) ) 250 foreach ( $plugins_subdir as $subfile ) 251 $plugin_files[] = $subfile; 265 252 } else { 266 if ( substr($file, -4) == '.php')267 $plugin_files[] = $ file;268 } 253 if ( '.php' === substr( $plugin_path, -4 ) ) 254 $plugin_files[] = $plugin_path; 255 } 269 256 } 270 closedir( $plugins_dir );271 257 } 272 273 if ( empty( $plugin_files) )258 259 if ( empty( $plugin_files ) ) 274 260 return $wp_plugins; 275 261 276 262 foreach ( $plugin_files as $plugin_file ) { 277 if ( !is_readable( "$plugin_root/$plugin_file") )263 if ( !is_readable( $plugin_file ) ) 278 264 continue; 279 265 280 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.266 $plugin_data = get_plugin_data( $plugin_file, false, false ); //Do not apply markup/translate as it'll be cached. 281 267 282 if ( empty 268 if ( empty( $plugin_data['Name'] ) ) 283 269 continue; 284 270 285 271 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data; … … 304 290 function get_mu_plugins() { 305 291 $wp_plugins = array(); 306 292 // Files in wp-content/mu-plugins directory 307 $plugin_files = array(); 308 309 if ( ! is_dir( WPMU_PLUGIN_DIR ) ) 310 return $wp_plugins; 311 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) { 312 while ( ( $file = readdir( $plugins_dir ) ) !== false ) { 313 if ( substr( $file, -4 ) == '.php' ) 314 $plugin_files[] = $file; 315 } 316 } else { 317 return $wp_plugins; 318 } 319 320 @closedir( $plugins_dir ); 321 322 if ( empty($plugin_files) ) 323 return $wp_plugins; 324 293 $plugin_files = wp_scandir( WPMU_PLUGIN_DIR, 'php', true ); 294 295 if ( empty( $plugin_files ) ) 296 return array(); 297 325 298 foreach ( $plugin_files as $plugin_file ) { 326 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file") )299 if ( !is_readable( $plugin_file ) ) 327 300 continue; 328 301 329 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.302 $plugin_data = get_plugin_data( $plugin_file, false, false ); //Do not apply markup/translate as it'll be cached. 330 303 331 if ( empty 332 $plugin_data['Name'] = $plugin_file;304 if ( empty( $plugin_data['Name'] ) ) 305 $plugin_data['Name'] = basename( $plugin_file ); 333 306 334 $wp_plugins[ $plugin_file] = $plugin_data;307 $wp_plugins[ basename( $plugin_file ) ] = $plugin_data; 335 308 } 336 309 337 310 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden … … 364 337 365 338 $_dropins = _get_dropins(); 366 339 367 // These exist in the wp-content directory 368 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) { 369 while ( ( $file = readdir( $plugins_dir ) ) !== false ) { 370 if ( isset( $_dropins[ $file ] ) ) 371 $plugin_files[] = $file; 372 } 373 } else { 340 $paths = wp_scandir( WP_CONTENT_DIR, 'php', true ); 341 342 if ( empty( $paths ) ) 374 343 return $dropins; 344 345 // These exist in the wp-content directory 346 foreach ( $paths as $path ) { 347 $file = basename( $path ); 348 if ( isset( $_dropins[ $file ] ) ) 349 $plugin_files[] = $file; 375 350 } 376 351 377 @closedir( $plugins_dir );378 379 if ( empty($plugin_files) )380 return $dropins;381 382 352 foreach ( $plugin_files as $plugin_file ) { 383 353 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) 384 354 continue; 385 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. 355 356 $absolute = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $plugin_file; 357 $plugin_data = get_plugin_data( $absolute, false, false ); //Do not apply markup/translate as it'll be cached. 358 386 359 if ( empty( $plugin_data['Name'] ) ) 387 360 $plugin_data['Name'] = $plugin_file; 361 388 362 $dropins[ $plugin_file ] = $plugin_data; 389 363 } 390 364 -
wp-admin/includes/file.php
128 128 * @return bool|array False on failure, Else array of files 129 129 */ 130 130 function list_files( $folder = '', $levels = 100 ) { 131 if ( empty( $folder) )131 if ( empty( $folder ) ) 132 132 return false; 133 133 134 134 if ( ! $levels ) 135 135 return false; 136 136 137 137 $files = array(); 138 if ( $dir = @opendir( $folder ) ) {139 while (($file = readdir( $dir ) ) !== false ) {140 if ( in_array($file, array('.', '..') ) )141 continue;142 if ( is_dir( $folder . '/' . $file )) {143 $files2 = list_files( $folder . '/' . $file, $levels - 1);144 if ( $files2 )145 $files = array_merge($files, $files2 );146 else147 $files[] = $folder . '/' . $file . '/';148 } else {149 $files[] = $folder . '/' . $file;150 }138 $paths = wp_scandir( $folder ); 139 if ( empty( $paths ) ) 140 return $files; 141 142 foreach ( $paths as $path ) { 143 if ( is_dir( $path ) ) { 144 $found = list_files( $path, $levels - 1); 145 if ( !empty( $found ) ) 146 $files = array_merge( $files, $found ); 147 else 148 $files[] = trailingslashit( $path ); 149 } else { 150 $files[] = $path; 151 151 } 152 152 } 153 @closedir( $dir );153 154 154 return $files; 155 155 } 156 156 -
wp-admin/includes/ms.php
92 92 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) ); 93 93 94 94 $dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id ); 95 $dir = rtrim( $dir, DIRECTORY_SEPARATOR);95 $dir = untrailingslashit( $dir ); 96 96 $top_dir = $dir; 97 97 $stack = array($dir); 98 98 $index = 0; … … 101 101 # Get indexed directory from stack 102 102 $dir = $stack[$index]; 103 103 104 $dh = @opendir( $dir ); 105 if ( $dh ) { 106 while ( ( $file = @readdir( $dh ) ) !== false ) { 107 if ( $file == '.' || $file == '..' ) 108 continue; 109 110 if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) 111 $stack[] = $dir . DIRECTORY_SEPARATOR . $file; 112 else if ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) 113 @unlink( $dir . DIRECTORY_SEPARATOR . $file ); 114 } 104 $paths = wp_scandir( $dir ); 105 106 if ( !empty( $paths ) ) { 107 foreach ( $paths as $path ) { 108 if ( @is_dir( $path ) ) 109 $stack[] = $path; 110 else if ( @is_file( $path ) ) 111 @unlink( $path ); 112 } 115 113 } 116 114 $index++; 117 115 } -
wp-admin/plugin-editor.php
27 27 if ( empty($plugins) ) 28 28 wp_die( __('There are no plugins installed on this site.') ); 29 29 30 if ( isset( $_REQUEST['file']) )30 if ( isset( $_REQUEST['file'] ) && !isset( $_REQUEST['plugin'] ) ) 31 31 $plugin = stripslashes($_REQUEST['file']); 32 32 33 33 if ( empty($plugin) ) {