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;
56 private static Logger logger = Logger.getLogger(Util.class.getName());
61 public static boolean pathexists(String path) {
62 File file =
new File(path);
63 boolean exists = file.exists();
67 public static String utcConvert(String utc) {
68 SimpleDateFormat formatter =
new SimpleDateFormat(
"MM-dd-yyyy HH:mm");
69 String tempconvert = formatter.format(
new Date(Long.parseLong(utc)));
73 public static String readFile(String path)
throws IOException {
74 FileInputStream stream =
new FileInputStream(
new File(path));
76 FileChannel fc = stream.getChannel();
77 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
81 return Charset.defaultCharset().decode(bb).toString();
87 public static String getBaseDomain(String url) {
90 String cleanUrl = url.replaceFirst(
"/.*:\\/\\//",
"");
93 String dirToks[] = cleanUrl.split(
"/\\//");
94 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;
116 return hostB.toString();
119 public static String extractDomain(String value) {
133 URL url =
new URL(value);
134 result = url.getHost();
135 }
catch (MalformedURLException ex) {
141 if (result == null || result.trim().isEmpty()) {
142 return getBaseDomain(value);
148 public static String getFileName(String value) {
149 String filename =
"";
150 String filematch =
"^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$";
152 Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
153 Matcher m = p.matcher(value);
155 filename = m.group(1);
158 int lastPos = value.lastIndexOf(
'\\');
159 filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
160 return filename.toString();
163 public static String getPath(String txt) {
167 String drive =
"([a-z]:\\\\\\S.+)";
168 Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
169 Matcher m = p.matcher(txt);
175 String network =
"(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)";
177 Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
178 Matcher m2 = p2.matcher(txt);
186 public static long findID(Content dataSource, String path) {
187 String parent_path = path.replace(
'\\',
'/');
188 if (parent_path.length() > 2 && parent_path.charAt(1) ==
':') {
189 parent_path = parent_path.substring(2);
191 int index = parent_path.lastIndexOf(
'/');
192 String name = parent_path.substring(++index);
193 parent_path = parent_path.substring(0, index);
194 List<AbstractFile> files = null;
196 FileManager fileManager = Case.getCurrentCaseThrows().getServices().getFileManager();
197 files = fileManager.findFiles(dataSource, name, parent_path);
198 }
catch (TskCoreException | NoCurrentCaseException ex) {
199 logger.log(Level.WARNING,
"Error fetching 'index.data' files for Internet Explorer history.");
202 if (files == null || files.isEmpty()) {
205 return files.get(0).getId();
208 public static boolean checkColumn(String column, String tablename, String connection) {
209 String query =
"PRAGMA table_info(" + tablename +
")";
210 boolean found =
false;
213 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
214 temprs = tempdbconnect.executeQry(query);
215 while (temprs.next()) {
216 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) {
220 }
catch (Exception ex) {
221 logger.log(Level.WARNING,
"Error while trying to get columns from sqlite db." + connection, ex);
226 public static ResultSet runQuery(String query, String connection) {
227 ResultSet results = null;
229 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
230 results = tempdbconnect.executeQry(query);
231 tempdbconnect.closeConnection();
232 }
catch (Exception ex) {
233 logger.log(Level.WARNING,
"Error while trying to run sql query: " + query +
" : " + connection, ex);