23 package org.sleuthkit.autopsy.recentactivity;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
31 import java.nio.MappedByteBuffer;
32 import java.nio.channels.FileChannel;
33 import java.nio.charset.Charset;
34 import java.sql.ResultSet;
35 import java.text.SimpleDateFormat;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.StringTokenizer;
39 import java.util.logging.Level;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
55 private static Logger logger = Logger.getLogger(Util.class.getName());
60 public static boolean pathexists(String path) {
61 File file =
new File(path);
62 boolean exists = file.exists();
66 public static String utcConvert(String utc) {
67 SimpleDateFormat formatter =
new SimpleDateFormat(
"MM-dd-yyyy HH:mm");
68 String tempconvert = formatter.format(
new Date(Long.parseLong(utc)));
72 public static String readFile(String path)
throws IOException {
73 FileInputStream stream =
new FileInputStream(
new File(path));
75 FileChannel fc = stream.getChannel();
76 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
80 return Charset.defaultCharset().decode(bb).toString();
86 public static String getBaseDomain(String url) {
89 String cleanUrl = url.replaceFirst(
"/.*:\\/\\//",
"");
92 String dirToks[] = cleanUrl.split(
"/\\//");
93 if (dirToks.length > 0) {
100 StringTokenizer tok =
new StringTokenizer(host,
".");
101 StringBuilder hostB =
new StringBuilder();
102 int toks = tok.countTokens();
104 for (
int count = 0; count < toks; ++count) {
105 String part = tok.nextToken();
106 int diff = toks - count;
115 return hostB.toString();
118 public static String extractDomain(String value) {
132 URL url =
new URL(value);
133 result = url.getHost();
134 }
catch (MalformedURLException ex) {
140 if (result == null || result.trim().isEmpty()) {
141 return getBaseDomain(value);
147 public static String getFileName(String value) {
148 String filename =
"";
149 String filematch =
"^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$";
151 Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
152 Matcher m = p.matcher(value);
154 filename = m.group(1);
157 int lastPos = value.lastIndexOf(
'\\');
158 filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
159 return filename.toString();
162 public static String getPath(String txt) {
166 String drive =
"([a-z]:\\\\\\S.+)";
167 Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
168 Matcher m = p.matcher(txt);
174 String network =
"(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)";
176 Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
177 Matcher m2 = p2.matcher(txt);
185 public static long findID(Content dataSource, String path) {
186 String parent_path = path.replace(
'\\',
'/');
187 if (parent_path.length() > 2 && parent_path.charAt(1) ==
':') {
188 parent_path = parent_path.substring(2);
190 int index = parent_path.lastIndexOf(
'/');
191 String name = parent_path.substring(++index);
192 parent_path = parent_path.substring(0, index);
193 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
194 List<AbstractFile> files = null;
196 files = fileManager.findFiles(dataSource, name, parent_path);
197 }
catch (TskCoreException ex) {
198 logger.log(Level.WARNING,
"Error fetching 'index.data' files for Internet Explorer history.");
201 if (files == null || files.isEmpty()) {
204 return files.get(0).getId();
207 public static boolean checkColumn(String column, String tablename, String connection) {
208 String query =
"PRAGMA table_info(" + tablename +
")";
209 boolean found =
false;
212 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
213 temprs = tempdbconnect.executeQry(query);
214 while (temprs.next()) {
215 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) {
219 }
catch (Exception ex) {
220 logger.log(Level.WARNING,
"Error while trying to get columns from sqlite db." + connection, ex);
225 public static ResultSet runQuery(String query, String connection) {
226 ResultSet results = null;
228 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
229 results = tempdbconnect.executeQry(query);
230 tempdbconnect.closeConnection();
231 }
catch (Exception ex) {
232 logger.log(Level.WARNING,
"Error while trying to run sql query: " + query +
" : " + connection, ex);