Example #1
0
        public static IEnumerable <PhpVersion> GetAvailableVersions()
        {
            //TODO Maybe I should make this local... meh

            HtmlDocument doc   = new HtmlDocument();
            var          brick = new HtmlWebException("Errors while parsing versions.");

            foreach (var versionEndpoint in PhpVersionEndpoints)
            {
                using (var client = new WebClient())
                    using (var stream = client.OpenRead(versionEndpoint))
                        doc.Load(stream);

                if ((doc.ParseErrors != null && doc.ParseErrors.Count() > 0) || doc.DocumentNode == null)
                {
                    throw brick;
                }

                var pre = doc.DocumentNode.SelectSingleNode("//body/pre");

                if (pre == null)
                {
                    throw brick;
                }

                foreach (var item in pre.SelectNodes("//a"))
                {
                    var href = item.GetAttributeValue("href", null);

                    if (href == null)
                    {
                        throw brick;
                    }
                    //^(?!.*-nts-).*(?<=-Win32-)(VC.*)(?=-x86\.zip)
                    var regex   = Regex.Match(href, @"(?<=php-)((\d+)\.(\d+)\.(\d+))(?=-Win32-VC\d+-x86\.zip)");
                    var vcRegex = Regex.Match(href, @"^(?!.*-nts-).*(?<=-Win32-)(VC.*)(?=-x86\.zip)");

                    if (regex.Success && vcRegex.Success && Version.TryParse(regex.Value, out var ver))
                    {
                        string filePath = "php/" + ver + "/php.exe";
                        string vc       = vcRegex.Groups[1].Value.Substring(2);

                        if (!int.TryParse(vc, out int vcVersion))
                        {
                            continue;
                        }

                        bool installed = System.IO.File.Exists(filePath);

                        yield return(new PhpVersion(ver, "http://windows.php.net" + href,
                                                    installed, VcRedistChecker.GetVC(vcVersion)));
                    }
                }
            }
        }
Example #2
0
        private static List <Dictionary <string, string> > BuscaEnderecos(
            string cep,
            string metodo,
            string numPagina,
            string regTotal,
            CookieContainer cookies = null)
        {
            try
            {
                //Efetua a consulta no site móvel dos correios
                cookies = cookies ?? new CookieContainer();
                string response = HTTPRequest.Request("http://m.correios.com.br/movel/buscaCepConfirma.do",
                                                      string.Format("metodo={0}&cepEntrada={1}&tipoCep=&cepTemp=", metodo, cep) + (numPagina != null ? string.Format("&numPagina={0}&regTotal={1}", numPagina, regTotal) : ""),
                                                      HTTPRequest.HttpMethods.POST,
                                                      cookies);

                //Carrega o HTML obtido no parser
                var html = new HtmlDocument {
                    OptionFixNestedTags = true
                };
                html.LoadHtml(response);
                if (html.ParseErrors != null && html.ParseErrors.Any())
                {
                    var ex = new HtmlWebException("O HTML obtido é inválido");
                    ex.Data.Add("cep", cep);
                    ex.Data.Add("errors", html.ParseErrors);
                    throw ex;
                }
                if (html.DocumentNode == null)
                {
                    var ex = new HtmlWebException("O HTML obtido não possui um nó de documento válido.");
                    ex.Data.Add("cep", cep);
                    throw ex;
                }

                //Verifica se foi retornado um nó de erro
                if (html.DocumentNode.SelectSingleNode(@"//div[@class='erro']") != null)
                {
                    return(null);
                }

                //Obtém os nós de resultado
                HtmlNodeCollection nodesBrancos = html.DocumentNode.SelectNodes(@"//div[@class='caixacampobranco']");
                HtmlNodeCollection nodesAzuis   = html.DocumentNode.SelectNodes(@"//div[@class='caixacampoazul']");
                var nodes = new List <HtmlNode>();
                if (nodesBrancos != null)
                {
                    nodes = nodes.Concat(nodesBrancos).ToList();
                }
                if (nodesAzuis != null)
                {
                    nodes = nodes.Concat(nodesAzuis).ToList();
                }

                //Verifica se foi obtido algum resultado
                if (!nodes.Any())
                {
                    var ex = new HtmlWebException("O HTML obtido não está na estrutura correta. Talvez o site dos correios tenha sofrido modificações.");
                    ex.Data.Add("cep", cep);
                    throw ex;
                }

                //Processa os resultados obtidos
                var ret = (from node in nodes
                           select node.SelectNodes(@"//span[@class='respostadestaque']")
                           into detailNodes
                           where detailNodes.Any()
                           let cidadeUF = detailNodes[2].InnerText.Split(new[] { '/' })
                                          select new Dictionary <string, string> {
                    { "logradouro", detailNodes[0].InnerText.Trim() },
                    { "bairro", detailNodes[1].InnerText.Trim() },
                    { "cidade", cidadeUF[0].Trim().Trim() },
                    { "uf", cidadeUF[1].Trim().Trim() },
                }).ToList();

                //Verifica se há mais páginas de retorno, e se houver, as verifica
                if (html.DocumentNode.SelectSingleNode(@"//input[@type='button' and @class='botao' and @value='Próximo']") != null)
                {
                    string valNumPagina = html.DocumentNode.SelectSingleNode(@"//input[@type='hidden' and @name='numPagina']").Attributes["value"].Value;
                    string valRegTotal  = html.DocumentNode.SelectSingleNode(@"//input[@type='hidden' and @name='regTotal']").Attributes["value"].Value;
                    ret.AddRange(BuscaEnderecos(cep, "proximo", valNumPagina, valRegTotal, cookies));
                }

                //Retorna os resultados obtidos
                return(ret);
            }
            catch (HtmlWebException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new Exception("Ocorreu um erro inesperado. Verifique a InnerException para mais detalhes.", e);
            }
        }