OSDN Git Service

リファクタリングとメソッド戻り値の見直し
[coroid/inqubus.git] / frontend / src / saccubus / net / BrowserInfo.java
1 /**
2  * Inspired from Nicorank by rankingloid 2008 - 2009
3  */
4 package saccubus.net;
5
6 import java.util.Arrays;
7
8 /**
9 /**
10  * <p>
11  * タイトル: さきゅばす
12  * </p>
13  *
14  * <p>
15  * 説明: ニコニコ動画の動画をコメントつきで保存
16  * </p>
17  *
18  * @version 1.22r3e
19  * @author orz
20  *
21  */
22 public class BrowserInfo {
23
24     public enum BrowserCookieKind {
25
26         NONE, MSIE, IE6, Firefox3, Firefox, Chrome,
27         Opera, Chromium, Other,}
28     private BrowserCookieKind validBrowser;
29
30     public String getBrowserName() {
31         if (validBrowser == BrowserCookieKind.NONE) {
32             return "さきゅばす";
33         } else if (validBrowser == BrowserCookieKind.MSIE) {
34             return "Internet Exploror";
35         } else {
36             return validBrowser.toString();
37         }
38     }
39
40     public BrowserInfo() {
41         validBrowser = BrowserCookieKind.NONE;
42     }
43     private static final String NICOVIDEO_URL = "http://www.nicovideo.jp";
44
45     /**
46      *
47      * @param browserKind
48      * @return
49      */
50     public String getUserSession(BrowserCookieKind browserKind) {
51         String user_session = "";
52         switch (browserKind) {
53             case IE6:
54                 user_session = GetUserSessionFromIE6(NICOVIDEO_URL);
55                 break;
56             case MSIE:
57                 user_session = GetUserSessionFromMSIE();
58                 break;
59             case Firefox:
60                 user_session = GetUserSessionFromFilefox4();
61                 if (!user_session.isEmpty()) {
62                     break;
63                 }
64             case Firefox3:
65                 user_session = GetUserSessionFromFilefox3();
66                 break;
67             case Chrome:
68                 user_session = GetUserSesionChrome();
69                 break;
70             case Chromium:
71                 user_session = GetUserSesionChromium();
72                 break;
73             case Opera:
74                 user_session = GetUserSessionOpera();
75                 break;
76         }
77         if (!user_session.isEmpty()) {
78             validBrowser = browserKind;
79         }
80         return user_session;
81     }
82
83     /**
84      *
85      * @param fileOrDir fullname of file or directory
86      * @return
87      */
88     public String getUserSessionOther(String fileOrDir) {
89         String user_session = "";
90         try {
91             if (Path.isDirectory(fileOrDir)) {
92                 // Directory Type like MSIE
93                 user_session = GetUserSessionFromDirectory(fileOrDir);
94                 return user_session;
95             }
96             if (Path.isFile(fileOrDir)) {
97                 // File Type like Firefox3
98                 String dataStr = Path.ReadAllText(fileOrDir, "UTF-8");
99                 user_session = CutUserSession(dataStr, fileOrDir);
100                 return user_session;
101             }
102             return "";
103         } catch (Exception e) {
104             e.printStackTrace();
105             return "";
106         } finally {
107             if (!user_session.isEmpty()) {
108                 validBrowser = BrowserCookieKind.Other;
109             }
110         }
111     }
112
113     /// <summary>
114     /// Firefox3 から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
115     /// </summary>
116     /// <returns>user_session</returns>
117     private String GetUserSessionFromFilefox3() {
118         String user_session = "";
119         try {
120             String app_dir = System.getenv("APPDATA");
121             if (app_dir == null || app_dir.isEmpty()) {
122                 return "";
123             }
124             String sqlist_filename = app_dir + "\\Mozilla\\Firefox\\Profiles\\cookies.sqlite";
125             if (!Path.isFile(sqlist_filename)) {
126                 return "";
127             }
128             String dataStr = Path.ReadAllText(sqlist_filename, "US-ASCII");
129             user_session = CutUserSession(dataStr, sqlist_filename);
130             return user_session;
131         } catch (Exception e) {
132             e.printStackTrace();
133         }
134         return user_session;
135     }
136
137     /// <summary>
138     /// Firefox4, 5 から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
139     /// </summary>
140     /// <returns>user_session</returns>
141     private String GetUserSessionFromFilefox4() {
142         String user_session = "";
143         try {
144             String app_dir = System.getenv("APPDATA");
145             if (app_dir == null || app_dir.isEmpty()) {
146                 return "";
147             }
148             String[] userLists = Path.GetFiles(app_dir + "\\Mozilla\\Firefox\\Profiles\\");
149             for (String user_dir : userLists) {
150                 String sqlist_filename = user_dir + "\\cookies.sqlite";
151                 if (Path.isFile(sqlist_filename)) {
152                     String dataStr = Path.ReadAllText(sqlist_filename, "US-ASCII");
153                     user_session = CutUserSession(dataStr, sqlist_filename);
154                     if (!user_session.isEmpty()) {
155                         return user_session;
156                     }
157                     // else continue
158                 }
159             }
160             return "";  // not found
161         } catch (Exception e) {
162             e.printStackTrace();
163             return "";
164         }
165     }
166
167     /// <summary>
168     /// IE6 から user_session を取得
169     /// </summary>
170     /// <param name="url">サイト(ニコニコ動画)のURL</param>
171     /// <returns>user_session</returns>
172     private String GetUserSessionFromIE6(String url) {
173         return CutUserSession(GetCookieFromIE6(url), "");
174     }
175
176     /// <summary>
177     /// IE6 からクッキーを取得
178     /// </summary>
179     /// <param name="url">取得するクッキーに関連づけられたURL</param>
180     /// <returns>クッキー文字列</returns>
181     private String GetCookieFromIE6(String url) {
182         int size = 4096;
183         byte[] dummy = new byte[size];
184         Arrays.fill(dummy, (byte) ' ');
185         StringBuilder buff = new StringBuilder(new String(dummy));
186         int[] ref_size = new int[1];
187         ref_size[0] = size;
188         //InternetGetCookie(url, null, buff, /*ref*/ ref_size);
189         return buff.toString().replace(';', ',');
190     }
191     /*
192      *  [DllImport("wininet.dll")]
193      *  private extern static bool InternetGetCookie(string url, string name, StringBuilder data, ref uint size);
194      *
195      *  shuold use NLink.win32
196      */
197
198     /** <p>
199      *  IE7/IE8/IE9 から user_session を取得。<br/>
200      *  エラーが起こった場合、例外を投げずに空文字を返す
201      *  </p>
202      *  @return user_session
203      */
204     private String GetUserSessionFromMSIE() {
205         String user_session = "";
206
207         String profile_dir = System.getenv("USERPROFILE");
208         if (profile_dir == null || profile_dir.isEmpty()) {
209             return "";
210         }
211         String search_dir = profile_dir + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\";
212         user_session = GetUserSessionFromDirectory(search_dir);
213         if (user_session.isEmpty()) {
214             search_dir = profile_dir + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\";
215             user_session = GetUserSessionFromDirectory(search_dir);
216         }
217         if (user_session.isEmpty()) {
218             search_dir = profile_dir + "\\Cookies\\";
219             user_session = GetUserSessionFromDirectory(search_dir);
220         }
221         return user_session;
222     }
223
224     /**
225      * dir_name ディレクトリから MSIE のクッキーを見つけて user_session を返す
226      * @param dir_name
227      * @return
228      */
229     private String GetUserSessionFromDirectory(String dir_name) {
230         String user_session = "";
231         try {
232             if (Path.isDirectory(dir_name)) {
233                 String[] files = Path.GetFiles(dir_name);
234                 for (String fullname : files) {
235                     user_session = CutUserSession(Path.ReadAllText(fullname, "MS932"), fullname);
236                     if (!user_session.isEmpty()) {
237                         return user_session;
238                     }
239
240                     /*  obsolate after WindowsUpdate Aug 2011
241                     String name = Path.GetFileName(fullname);
242                     if (name.indexOf("nicovideo") >= 0 && name.indexOf("www") < 0)
243                     {
244                     user_session = CutUserSession(Path.ReadAllText(fullname, "MS932"), "");
245                     if (!user_session.isEmpty()){
246                     System.out.println("Found cookie in " + fullname.replace("\\", "/"));
247                     return user_session;
248                     }
249                     }
250                      */
251                 }
252                 return "";
253             }
254         } catch (Exception e) {
255             e.printStackTrace();
256         }
257         return "";
258     }
259
260     /** <p>
261      *  Chrome から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
262      *  </p>
263      *  @return user_session
264      */
265     private String GetUserSesionChrome() {
266         String user_session = "";
267         String cookie_file = "";
268         String googleChrome = "\\Google\\Chrome\\User Data\\Default\\Cookies";
269         try {
270             String local_Appdir = System.getenv("LOCALAPPDATA");
271             if (local_Appdir != null && !local_Appdir.isEmpty()) {
272                 // Win7 32bit
273                 cookie_file = local_Appdir + googleChrome;
274                 if (Path.isFile(cookie_file)) {
275                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
276                     user_session = CutUserSession(dataStr, cookie_file);
277                     if (!user_session.isEmpty()) {
278                         return user_session;
279                     }
280                 }
281             }
282             String profile_dir = System.getenv("USERPROFILE");
283             if (profile_dir != null && !profile_dir.isEmpty()) {
284                 // XP 32bit
285                 cookie_file = profile_dir
286                         + "\\Local Settings\\Application Data" + googleChrome;
287                 if (Path.isFile(cookie_file)) {
288                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
289                     user_session = CutUserSession(dataStr, cookie_file);
290                     return user_session;
291                 }
292             }
293             String app_dir = System.getenv("APPDATA");
294             if (app_dir != null && !app_dir.isEmpty()) {
295                 // ??? just try
296                 cookie_file = app_dir + googleChrome;
297                 if (Path.isFile(cookie_file)) {
298                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
299                     user_session = CutUserSession(dataStr, cookie_file);
300                     return user_session;
301                 }
302             }
303             return user_session;
304         } catch (Exception e) {
305             e.printStackTrace();
306             return user_session;
307         }
308     }
309
310     /** <p>
311      *  Chromium から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
312      *  </p>
313      *  @return user_session
314      */
315     private String GetUserSesionChromium() {
316         String user_session = "";
317         String cookie_file = "";
318         String chromium = "\\Chromium\\User Data\\Default\\Cookies";
319         try {
320             String local_Appdir = System.getenv("LOCALAPPDATA");
321             if (local_Appdir != null && !local_Appdir.isEmpty()) {
322                 // Win7 32bit
323                 cookie_file = local_Appdir + chromium;
324                 if (Path.isFile(cookie_file)) {
325                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
326                     user_session = CutUserSession(dataStr, cookie_file);
327                     return user_session;
328                 }
329             }
330             String profile_dir = System.getenv("USERPROFILE");
331             if (profile_dir != null && !profile_dir.isEmpty()) {
332                 // XP 32bit
333                 cookie_file = profile_dir
334                         + "\\Local Settings\\Application Data" + chromium;
335                 if (Path.isFile(cookie_file)) {
336                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
337                     user_session = CutUserSession(dataStr, cookie_file);
338                     return user_session;
339                 }
340             }
341             return user_session;
342         } catch (Exception e) {
343             e.printStackTrace();
344             return user_session;
345         }
346     }
347
348     /** <p>
349      *  Opera から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
350      *  </p>
351      *  @return user_session
352      */
353     private String GetUserSessionOpera() {
354         String user_session = "";
355         String cookie_file = "";
356         try {
357             String app_dir = System.getenv("APPDATA");
358             if (app_dir != null && !app_dir.isEmpty()) {
359                 // Win7/XP 32bit
360                 cookie_file = app_dir + "\\Opera\\Opera\\cookies4.dat";
361                 if (Path.isFile(cookie_file)) {
362                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
363                     user_session = CutUserSession(dataStr, cookie_file);
364                     return user_session;
365                 }
366             }
367             return "";
368         } catch (Exception e) {
369             e.printStackTrace();
370             return "";
371         }
372     }
373
374     /// <summary>
375     /// 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
376     /// </summary>
377     /// <param name="str">切り出す対象文字列</param>
378     /// <returns>user_session 文字列。見つからなければ空文字を返す</returns>
379     private String CutUserSession(String str, String filename) {
380         String ret = "";
381         int start = str.indexOf("user_session_");
382         if (start >= 0) {
383             int index = start + "user_session_".length();
384             while (index < str.length() && ('0' <= str.charAt(index) && str.charAt(index) <= '9'
385                     || str.charAt(index) == '_')) {
386                 ++index;
387             }
388             ret = str.substring(start, index);
389             // C# の string.SubString( , ) と Java の String.substring( , ) は違うので注意!
390             if (!ret.isEmpty() && !filename.isEmpty()) {
391                 System.out.println("Cookie found: " + filename);
392                 return ret;
393             }
394         }
395         return "";
396     }
397 }