• Resolved inovagora

    (@inovagora)


    Hi,

    I found a bug regarding the exclusion of the sites dir in multisite config.
    In the scan_dir function, full paths containing “/sites/” are excluded.
    This results in no directories found if the absolute path of the website allready contains a “sites” dir at a higher level, which is our case.

    To correct this, “sites” should be excluded only if it is a last-level dir and sub-dirs should be inspected only if the parent dir is not excluded.

    This results in this corrected function :

    	private function scan_dir( $dir ) {
    
    		$excludedir = 'media-from-ftp-tmp'; /* tmp dir */
    		global $blog_id;
    		if ( is_multisite() && is_main_site( $blog_id ) ) {
    			$excludedir .= '|\/sites$';
    		}
    
    		$files = scandir( $dir );
    		$list = array();
    		foreach ( $files as $file ) {
    			if ( '.' == $file || '..' == $file ) {
    				continue;
    			}
    			$fullpath = rtrim( $dir, '/' ) . '/' . $file;
    			if ( is_dir( $fullpath ) ) {
    				if ( ! preg_match( '/' . $excludedir . '/', $fullpath ) ) {
    					$list[] = $fullpath;
    					$list = array_merge( $list, $this->scan_dir( $fullpath ) );
    				}
    			}
    		}
    
    		arsort( $list );
    		return $list;
    
    	}

    Can you please include this bugfix in the next release ?

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter inovagora

    (@inovagora)

    The scan_file function should also be modified accordingly :

    	public function scan_file( $dir, $extpattern, $mediafromftp_settings ) {
    
    		/* for media-from-ftp-add-on-wpcron and mediafromftpcmd.php */
    		$cmdoptions = array();
    		if ( $this->is_add_on_activate['cli'] ) {
    			$cmdoptions = getopt( 's:d:a:e:t:x:p:f:c:i:b:r:y:hgmo' );
    		}
    
    		if ( isset( $cmdoptions['f'] ) ) {
    			$search_limit_number = $cmdoptions['f'];
    		} else {
    			$search_limit_number = $mediafromftp_settings['search_limit_number'];
    		}
    
    		$thumb_deep_search = false;
    		if ( isset( $cmdoptions['m'] ) ) {
    			$thumb_deep_search = true;
    		} else {
    			$thumb_deep_search = $mediafromftp_settings['thumb_deep_search'];
    		}
    		if ( $thumb_deep_search ) {
    			$excludefile = 'media-from-ftp-tmp';    /* tmp dir file */
    		} else {
    			$excludefile = '-[0-9]+x[0-9]+\.|media-from-ftp-tmp';   /* thumbnail & tmp dir file */
    		}
    
    		$recursive_search = true;
    		if ( isset( $cmdoptions['o'] ) ) {
    			$recursive_search = false;
    		} else {
    			$recursive_search = $mediafromftp_settings['recursive_search'];
    		}
    
    		global $blog_id;
    		if ( is_multisite() && is_main_site( $blog_id ) ) {
    			$excludefile .= '|\/sites$';
    		}
    		if ( isset( $cmdoptions['e'] ) ) {
    				$excludefile .= '|' . $cmdoptions['e'];
    		} else {
    			if ( ! empty( $mediafromftp_settings['exclude'] ) ) {
    				$excludefile .= '|' . $mediafromftp_settings['exclude'];
    			}
    		}
    
    		$ext2typefilter = $mediafromftp_settings['ext2typefilter'];
    		if ( isset( $cmdoptions['t'] ) ) {
    			$ext2typefilter = $cmdoptions['t'];
    		} else {
    			if ( isset( $_POST['ext2type'] ) && ! empty( $_POST['ext2type'] ) ) {
    				if ( check_admin_referer( 'mff_search', 'media_from_ftp_search' ) ) {
    					$ext2typefilter = sanitize_text_field( wp_unslash( $_POST['ext2type'] ) );
    				}
    			}
    		}
    
    		unset( $cmdoptions );
    
    		$searchtext = '.*';
    		if ( isset( $_POST['searchtext'] ) && ! empty( $_POST['searchtext'] ) ) {
    			if ( check_admin_referer( 'mff_search', 'media_from_ftp_search' ) ) {
    				$searchtext = $this->mb_encode_multibyte( sanitize_text_field( wp_unslash( $_POST['searchtext'] ) ), $mediafromftp_settings['character_code'] );
    			}
    		} elseif ( isset( $_GET['searchtext'] ) && ! empty( $_GET['searchtext'] ) ) {
    			if ( check_admin_referer( 'mff_search', 'media_from_ftp_search' ) ) {
    				$searchtext = $this->mb_encode_multibyte( sanitize_text_field( wp_unslash( $_GET['searchtext'] ) ), $mediafromftp_settings['character_code'] );
    			}
    		}
    
    		$files = scandir( $dir );
    		$list = array();
    		$count = 0;
    		foreach ( $files as $file ) {
    			if ( '.' == $file || '..' == $file ) {
    				continue;
    			}
    			$fullpath = rtrim( $dir, '/' ) . '/' . $file;
    			if ( is_file( $fullpath ) ) {
    				if ( ! preg_match( '/' . $excludefile . '/', $fullpath ) ) {
    					$exts = explode( '.', $file );
    					$ext = end( $exts );
    					$searchflag = @preg_match( '/' . $searchtext . '/', $fullpath );
    					if ( ! $searchflag ) {
    						/* for "preg_match error: Compilation failed: missing terminating ] for character class" */
    						$searchflag = preg_match( '/' . preg_quote( $searchtext, '/' ) . '/', $fullpath );
    					}
    					if ( $searchflag ) {
    						if ( preg_match( '/' . $extpattern . '/', $ext ) ) {
    							if ( wp_ext2type( $ext ) === $ext2typefilter || 'all' === $ext2typefilter ) {
    								++$count;
    								if ( $count > $search_limit_number ) {
    									break;
    								}
    								$list[] = $fullpath;
    							}
    						}
    					}
    					if ( is_dir( $fullpath ) && $recursive_search ) {
    						$list = array_merge( $list, $this->scan_file( $fullpath, $extpattern, $mediafromftp_settings ) );
    					}
    				}
    			}
    		}
    
    		return $list;
    
    	}
    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    Thanks. I’ll fix later.

    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    Fixed in Version 11.02.
    Please verify.

    Thread Starter inovagora

    (@inovagora)

    Hello,
    It works fine, thanks a lot.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Bug with sites dir exclusion on multisite’ is closed to new replies.