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) {
101 StringTokenizer tok =
new StringTokenizer(host,
".");
102 StringBuilder hostB =
new StringBuilder();
103 int toks = tok.countTokens();
105 for (
int count = 0; count < toks; ++count) {
106 String part = tok.nextToken();
107 int diff = toks - count;
117 return hostB.toString();
121 public static String extractDomain(String value) {
136 URL url =
new URL(value);
137 result = url.getHost();
138 }
catch (MalformedURLException ex) {
145 if (result == null || result.trim().isEmpty()) {
146 return getBaseDomain(value);
152 public static String getFileName(String value) {
153 String filename =
"";
154 String filematch =
"^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$";
156 Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
157 Matcher m = p.matcher(value);
159 filename = m.group(1);
162 int lastPos = value.lastIndexOf(
'\\');
163 filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
164 return filename.toString();
167 public static String getPath(String txt) {
171 String drive =
"([a-z]:\\\\\\S.+)";
172 Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
173 Matcher m = p.matcher(txt);
179 String network =
"(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)";
181 Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
182 Matcher m2 = p2.matcher(txt);
190 public static long findID(Content dataSource, String path) {
191 String parent_path = path.replace(
'\\',
'/');
192 if (parent_path.length() > 2 && parent_path.charAt(1) ==
':') {
193 parent_path = parent_path.substring(2);
195 int index = parent_path.lastIndexOf(
'/');
196 String name = parent_path.substring(++index);
197 parent_path = parent_path.substring(0, index);
200 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
201 List<AbstractFile> files = null;
203 files = fileManager.findFiles(dataSource, name, parent_path);
204 }
catch (TskCoreException ex) {
205 logger.log(Level.WARNING,
"Error fetching 'index.data' files for Internet Explorer history.");
208 if (files == null || files.isEmpty()) {
211 return files.get(0).getId();
214 public static boolean checkColumn(String column, String tablename, String connection) {
215 String query =
"PRAGMA table_info(" + tablename +
")";
216 boolean found =
false;
219 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
220 temprs = tempdbconnect.executeQry(query);
221 while (temprs.next()) {
222 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) {
226 }
catch (Exception ex) {
227 logger.log(Level.WARNING,
"Error while trying to get columns from sqlite db." + connection, ex);
232 public static ResultSet runQuery(String query, String connection) {
233 ResultSet results = null;
235 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
236 results = tempdbconnect.executeQry(query);
237 tempdbconnect.closeConnection();
238 }
catch (Exception ex) {
239 logger.log(Level.WARNING,
"Error while trying to run sql query: " + query +
" : " + connection, ex);