package filerogue.server.ramdb; /** * Title: FileRogue * Description: Global unrestricted file sharing * Copyright: Copyright (c) 2000 * Company: ITP Web Solutions * @author Brian Cairns * @version 1.0 */ import filerogue.*; public abstract class SearchEngine { public abstract void doSearch( DBSearchRequest req, Connection con ); protected boolean isMatchingFile( FileData file, DBSearchRequest req ) { // tests if a target file (including its parent directory) matches the search criteria if( !matchingSize( file, req ) ) return false; if( !matchingDate( file, req ) ) return false; if( req.isPathLikeFile() ) { String[] terms = req.getFileKeys(); if( terms != null && terms.length > 0 ) { String filename = file.getName(); String path = file.getParent().getFullPath(); if( req.isExactFileMatch() && !terms[ 0 ].equalsIgnoreCase( filename ) && !terms[ 0 ].equalsIgnoreCase( path ) ) return false; for( int i = 0; i < terms.length; i++ ) // ALL of these terms must be present, otherwise return false { if( terms[ i ] != null ) { if( terms[ i ].startsWith( "-" ) ) { if( Utilities.indexOfIgnoreCase( filename, terms[ i ].substring( 1 ) ) != -1 || Utilities.indexOfIgnoreCase( path, terms[ i ].substring( 1 ) ) != -1 ) return false; } else { if( Utilities.indexOfIgnoreCase( filename, terms[ i ] ) == -1 && Utilities.indexOfIgnoreCase( path, terms[ i ] ) == -1 ) return false; } } } } } else { // path and file are separate if( !matchesAll( req.getFileKeys(), file.getName(), req.isExactFileMatch() ) ) return false; if( !matchesAll( req.getPathKeys(), file.getParent().getFullPath(), req.isExactPathMatch() ) ) return false; } if( !matchesAny( req.getExts(), file.getExtension(), false ) ) return false; return true; } protected static boolean matches( String[] terms, String field, boolean matchAll, boolean exactMatch ) { if( terms == null || terms.length == 0 ) return true; if( exactMatch ) return terms[ 0 ].equalsIgnoreCase( field ); boolean inclusive = false, found = false; for( int i = 0; i < terms.length; i++ ) { if( terms[ i ].startsWith( "-" ) ) { if( Utilities.indexOfIgnoreCase( field, terms[ i ].substring( 1 ) ) != -1 ) return false; } else if( !found || matchAll ) { inclusive = true; if( Utilities.indexOfIgnoreCase( field, terms[ i ] ) != -1 ) found = true; else if( matchAll ) return false; } } // System.out.println( "found: " + found + ", inclusive: " + inclusive + " => " + !( found ^ inclusive ) ); return !( found ^ inclusive ); } protected static boolean matchesAny( String[] terms, String field, boolean exactMatch ) { return matches( terms, field, false, exactMatch ); } protected static boolean matchesAll( String[] terms, String field, boolean exactMatch ) { return matches( terms, field, true, exactMatch ); } protected static boolean inRange( long value, long low, long high ) { if( low > -1 && value < low ) return false; if( high > -1 && value > high ) return false; return true; } protected static boolean matchingSize( FileData file, DBSearchRequest req ) { return inRange( file.getLength(), req.getLowSize(), req.getHighSize() ); } protected static boolean matchingDate( FileData file, DBSearchRequest req ) { return inRange( file.getDate(), req.getLowDate() == null ? -1 : req.getLowDate().getTime(), req.getHighDate() == null ? -1 : req.getHighDate().getTime() ); } protected static boolean isValidUser( User user, DBSearchRequest req ) { // tests if a target user matches the search criteria (proxy, owner filter) // System.out.println( "DBReq.IP: " + req.getIPAddress() + ", file.user.IP: " + user.getIP() ); if( user == null ) return false; // null check // System.out.println( "User det prox: " + user.determineProxy() + ", req IP: " + req.getIPAddress() + ", user IP: " + user.getIP() ); if( req.getIPAddress() != null && ( user.determineProxy() && !req.getIPAddress().equals( user.getIP() ) ) ) return false; // proxy info check if( !matchesAny( req.getOS(), user.getOS(), req.isExactOSMatch() ) ) return false; if( req.getOwnerID() != -1 ) return req.getOwnerID() == user.getID(); if( user.getGlobalSlots() == 0 ) return false; if( !matchesAny( req.getOwners(), user.getName(), req.isExactOwnerMatch() ) ) return false; return true; } }