public static void FindFirefoxProfileInfosFromIniFile(string profilesIni, List <FirefoxProfileInfo> profiles) { // searcvh the profile for a profile entry that contains "Default=1" if (File.Exists(profilesIni)) { KeePassUtilities.LogMessage("File exists at " + profilesIni); StreamReader reader = File.OpenText(profilesIni); try { FirefoxProfileInfo profile = null; string line = reader.ReadLine(); while (line != null) { string lowerLine = line.ToLower().Replace(" ", ""); if (lowerLine.StartsWith("[")) // new section { if (lowerLine.StartsWith("[profile")) // new section { profile = new FirefoxProfileInfo(); profile.Code = line.Trim().TrimStart('[').TrimEnd(']'); profile.BasePath = profilesIni.Substring(0, profilesIni.LastIndexOf("\\")); profiles.Add(profile); } else { profile = null; } } if (profile != null) { if (lowerLine.StartsWith("name=")) // this is the default profile { profile.Name = line.Substring(5); } if (lowerLine.StartsWith("path=")) // this is the default profile { profile.Path = line.Substring(5); } if (lowerLine == "default=1") // this is the default profile { profile.Default = true; } if (lowerLine == "isrelative=1") // this is the default profile { profile.IsRelative = true; } if (lowerLine == "isrelative=0") // this is the default profile { profile.IsRelative = false; } } line = reader.ReadLine(); } } finally { reader.Close(); } } else { KeePassUtilities.LogMessage("File does not exist at " + profilesIni); } }
/// <summary> /// finds the contents of the title tag of a page /// results are cached based on the supplied result /// </summary> /// <param name="url">page to scrape</param> /// <returns>title</returns> public string ScrapeTitle(string url) { try { if (_TitleCache.ContainsKey(url.ToLower())) { return(_TitleCache[url.ToLower()]); } _TitleCache[url.ToLower()] = null; // so dont repeat failures string title = null; WebRequest request = WebRequest.Create(url); request.Timeout = 10 * 1000; // ms WebResponse response = request.GetResponse(); // makes request Stream s = response.GetResponseStream(); TextReader tr = new StreamReader(s); string titleBlock = ""; try { // read in all text from a line with the start title tag <title> to a line with the end title tag </title> // limit to 5 lines between the tags incase its invalid html! bool titleFound = false; // tracks if we have hit the opening tag string line = null; // current line int lines = 0; // number of title lines so far // this gathers all the required lines as one line. it includes the tags ond other outer info which will be stripped later while ((line = tr.ReadLine()) != null && lines < 5) { if (_TitleOpenRegex.IsMatch(line)) // has an opening tag { titleFound = true; } if (titleFound) // store all lines as a single trimmed line { titleBlock += " " + line.Trim(); lines++; } if (_TitleCloseRegex.IsMatch(line)) // has a closing tag so stop { break; } } } finally { tr.Close(); s.Close(); response.Close(); } titleBlock = titleBlock.Trim(); if (titleBlock.Length > 0) { // now capture the part of the titleBlick which is inside the title tags Match match = _TitleRegex.Match(titleBlock); if (match.Success) { title = HttpUtility.HtmlDecode(match.Groups[1].Captures[0].Value.Trim()); _TitleCache[url.ToLower()] = title; } } return(title); } catch (Exception ex) { KeePassUtilities.LogException(ex); return(null); } }