OSDN Git Service

firefox3 cookie 処理クラス実装開始
[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     /// IE6 から user_session を取得
115     /// </summary>
116     /// <param name="url">サイト(ニコニコ動画)のURL</param>
117     /// <returns>user_session</returns>
118     private String GetUserSessionFromIE6(String url) {
119         return CutUserSession(GetCookieFromIE6(url), "");
120     }
121
122     /// <summary>
123     /// IE6 からクッキーを取得
124     /// </summary>
125     /// <param name="url">取得するクッキーに関連づけられたURL</param>
126     /// <returns>クッキー文字列</returns>
127     private String GetCookieFromIE6(String url) {
128         int size = 4096;
129         byte[] dummy = new byte[size];
130         Arrays.fill(dummy, (byte) ' ');
131         StringBuilder buff = new StringBuilder(new String(dummy));
132         int[] ref_size = new int[1];
133         ref_size[0] = size;
134         //InternetGetCookie(url, null, buff, /*ref*/ ref_size);
135         return buff.toString().replace(';', ',');
136     }
137     /*
138      *  [DllImport("wininet.dll")]
139      *  private extern static bool InternetGetCookie(string url, string name, StringBuilder data, ref uint size);
140      *
141      *  shuold use NLink.win32
142      */
143
144     /** <p>
145      *  IE7/IE8/IE9 から user_session を取得。<br/>
146      *  エラーが起こった場合、例外を投げずに空文字を返す
147      *  </p>
148      *  @return user_session
149      */
150     private String GetUserSessionFromMSIE() {
151         String user_session = "";
152
153         String profile_dir = System.getenv("USERPROFILE");
154         if (profile_dir == null || profile_dir.isEmpty()) {
155             return "";
156         }
157         String search_dir = profile_dir + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\";
158         user_session = GetUserSessionFromDirectory(search_dir);
159         if (user_session.isEmpty()) {
160             search_dir = profile_dir + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\";
161             user_session = GetUserSessionFromDirectory(search_dir);
162         }
163         if (user_session.isEmpty()) {
164             search_dir = profile_dir + "\\Cookies\\";
165             user_session = GetUserSessionFromDirectory(search_dir);
166         }
167         return user_session;
168     }
169
170     /**
171      * dir_name ディレクトリから MSIE のクッキーを見つけて user_session を返す
172      * @param dir_name
173      * @return
174      */
175     private String GetUserSessionFromDirectory(String dir_name) {
176         String user_session = "";
177         try {
178             if (Path.isDirectory(dir_name)) {
179                 String[] files = Path.GetFiles(dir_name);
180                 for (String fullname : files) {
181                     user_session = CutUserSession(Path.ReadAllText(fullname, "MS932"), fullname);
182                     if (!user_session.isEmpty()) {
183                         return user_session;
184                     }
185
186                     /*  obsolate after WindowsUpdate Aug 2011
187                     String name = Path.GetFileName(fullname);
188                     if (name.indexOf("nicovideo") >= 0 && name.indexOf("www") < 0)
189                     {
190                     user_session = CutUserSession(Path.ReadAllText(fullname, "MS932"), "");
191                     if (!user_session.isEmpty()){
192                     System.out.println("Found cookie in " + fullname.replace("\\", "/"));
193                     return user_session;
194                     }
195                     }
196                      */
197                 }
198                 return "";
199             }
200         } catch (Exception e) {
201             e.printStackTrace();
202         }
203         return "";
204     }
205
206     /** <p>
207      *  Chromium から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
208      *  </p>
209      *  @return user_session
210      */
211     private String GetUserSesionChromium() {
212         String user_session = "";
213         String cookie_file = "";
214         String chromium = "\\Chromium\\User Data\\Default\\Cookies";
215         try {
216             String local_Appdir = System.getenv("LOCALAPPDATA");
217             if (local_Appdir != null && !local_Appdir.isEmpty()) {
218                 // Win7 32bit
219                 cookie_file = local_Appdir + chromium;
220                 if (Path.isFile(cookie_file)) {
221                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
222                     user_session = CutUserSession(dataStr, cookie_file);
223                     return user_session;
224                 }
225             }
226             String profile_dir = System.getenv("USERPROFILE");
227             if (profile_dir != null && !profile_dir.isEmpty()) {
228                 // XP 32bit
229                 cookie_file = profile_dir
230                         + "\\Local Settings\\Application Data" + chromium;
231                 if (Path.isFile(cookie_file)) {
232                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
233                     user_session = CutUserSession(dataStr, cookie_file);
234                     return user_session;
235                 }
236             }
237             return user_session;
238         } catch (Exception e) {
239             e.printStackTrace();
240             return user_session;
241         }
242     }
243
244     /** <p>
245      *  Opera から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
246      *  </p>
247      *  @return user_session
248      */
249     private String GetUserSessionOpera() {
250         String user_session = "";
251         String cookie_file = "";
252         try {
253             String app_dir = System.getenv("APPDATA");
254             if (app_dir != null && !app_dir.isEmpty()) {
255                 // Win7/XP 32bit
256                 cookie_file = app_dir + "\\Opera\\Opera\\cookies4.dat";
257                 if (Path.isFile(cookie_file)) {
258                     String dataStr = Path.ReadAllText(cookie_file, "UTF-8");
259                     user_session = CutUserSession(dataStr, cookie_file);
260                     return user_session;
261                 }
262             }
263             return "";
264         } catch (Exception e) {
265             e.printStackTrace();
266             return "";
267         }
268     }
269
270     /// <summary>
271     /// 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
272     /// </summary>
273     /// <param name="str">切り出す対象文字列</param>
274     /// <returns>user_session 文字列。見つからなければ空文字を返す</returns>
275     private String CutUserSession(String str, String filename) {
276         String ret = "";
277         int start = str.indexOf("user_session_");
278         if (start >= 0) {
279             int index = start + "user_session_".length();
280             while (index < str.length() && ('0' <= str.charAt(index) && str.charAt(index) <= '9'
281                     || str.charAt(index) == '_')) {
282                 ++index;
283             }
284             ret = str.substring(start, index);
285             // C# の string.SubString( , ) と Java の String.substring( , ) は違うので注意!
286             if (!ret.isEmpty() && !filename.isEmpty()) {
287                 System.out.println("Cookie found: " + filename);
288                 return ret;
289             }
290         }
291         return "";
292     }
293 }