Revert "criada a classe curl para otimizar e simplificar o codigo"

This reverts commit 38e89e19b0.

# Conflicts:
#	app/inc/Curl.php
#	app/inc/URLAnalyzer.php
This commit is contained in:
Renan Bernordi 2024-12-05 11:41:08 -03:00
parent e729291991
commit a0b9a26dbf
2 changed files with 156 additions and 460 deletions

View file

@ -1,372 +0,0 @@
<?php
/**
* Classe para gerenciar requisições HTTP usando cURL
* Esta classe fornece uma interface simplificada para realizar requisições HTTP
*/
class Curl
{
/**
* @var array Configurações padrão do cURL
* Armazena as opções básicas que serão utilizadas em todas as requisições
*/
protected $defaultOptions = [];
/**
* @var array Cabeçalhos HTTP personalizados
* Armazena os headers que serão enviados com as requisições
*/
protected $headers = [];
/**
* @var array Cookies para a requisição
* Armazena os cookies que serão enviados com as requisições
*/
protected $cookies = [];
/**
* @var string Agente do usuário atual
* Identifica o cliente que está fazendo a requisição
*/
protected $userAgent;
/**
* @var array Configurações de proxy
* Armazena as configurações para uso de proxy nas requisições
*/
protected $proxy = [];
/**
* @var int Número máximo de tentativas
* Define quantas vezes a requisição será tentada em caso de falha
*/
protected $maxRetries = 3;
/**
* @var int Intervalo entre tentativas (em microssegundos)
* Define o tempo de espera entre tentativas consecutivas
*/
protected $retryDelay = 500000; // 0.5 segundos
/**
* Inicializa uma nova instância da classe Curl
*
* @param array $options Opções iniciais do cURL para personalização
*/
public function __construct(array $options = [])
{
$this->defaultOptions = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 30,
CURLOPT_ENCODING => '',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIESESSION => true,
CURLOPT_FRESH_CONNECT => true,
];
$this->setDefaultHeaders();
}
/**
* Define os cabeçalhos HTTP padrão
* Configura os headers básicos que serão utilizados em todas as requisições
*/
protected function setDefaultHeaders()
{
$this->headers = [
'Accept' => 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8',
'Accept-Language' => 'pt-BR, pt;q=0.9, en-US;q=0.8, en;q=0.7',
'Cache-Control' => 'no-cache',
'Pragma' => 'no-cache'
];
}
/**
* Define o agente do usuário para as requisições
*
* @param string $userAgent String que identifica o cliente
* @return $this Retorna a instância atual para encadeamento de métodos
*/
public function setUserAgent($userAgent)
{
if (is_string($userAgent)) {
$this->userAgent = $userAgent;
}
return $this;
}
/**
* Define cabeçalhos HTTP personalizados
*
* @param array $headers Array associativo com os cabeçalhos
* @return $this Retorna a instância atual para encadeamento de métodos
*/
public function setHeaders(array $headers)
{
// Redefine os headers para o padrão primeiro
$this->setDefaultHeaders();
// Adiciona os novos headers
foreach ($headers as $name => $value) {
if (!is_string($name) || !is_string($value)) {
continue;
}
$this->headers[trim($name)] = trim($value);
}
return $this;
}
/**
* Define cookies para a requisição
*
* @param array $cookies Array associativo com os cookies
* @return $this Retorna a instância atual para encadeamento de métodos
*/
public function setCookies(array $cookies)
{
$this->cookies = [];
foreach ($cookies as $name => $value) {
if (is_string($name) && $value !== null && is_string($value)) {
$this->cookies[] = trim($name) . '=' . trim($value);
}
}
return $this;
}
/**
* Configura um proxy para as requisições
*
* @param string $host Endereço do servidor proxy
* @param int $port Porta do servidor proxy
* @param string|null $username Nome de usuário para autenticação no proxy
* @param string|null $password Senha para autenticação no proxy
* @return $this Retorna a instância atual para encadeamento de métodos
*/
public function setProxy($host, $port, $username = null, $password = null)
{
if (is_string($host) && is_numeric($port)) {
$this->proxy = [
'host' => $host,
'port' => (int)$port,
'username' => is_string($username) ? $username : null,
'password' => is_string($password) ? $password : null
];
}
return $this;
}
/**
* Define o número máximo de tentativas para uma requisição
*
* @param int $maxRetries Número máximo de tentativas
* @return $this Retorna a instância atual para encadeamento de métodos
*/
public function setMaxRetries($maxRetries)
{
$this->maxRetries = max(1, (int)$maxRetries);
return $this;
}
/**
* Define o intervalo entre tentativas de requisição
*
* @param int $microseconds Tempo em microssegundos
* @return $this Retorna a instância atual para encadeamento de métodos
*/
public function setRetryDelay($microseconds)
{
$this->retryDelay = max(0, (int)$microseconds);
return $this;
}
/**
* Prepara as opções do cURL para a requisição
*
* @param string $url URL da requisição
* @param array $additionalOptions Opções adicionais do cURL
* @return array Retorna array com todas as opções configuradas
* @throws InvalidArgumentException Se a URL não for uma string válida
*/
protected function prepareOptions($url, array $additionalOptions = [])
{
if (!is_string($url)) {
throw new InvalidArgumentException('A URL deve ser uma string');
}
$options = [];
// Adiciona opções padrão
foreach ($this->defaultOptions as $key => $value) {
if (is_int($key)) {
$options[$key] = $value;
}
}
// Define a URL
$options[CURLOPT_URL] = $url;
// Define o User Agent
if ($this->userAgent) {
$options[CURLOPT_USERAGENT] = $this->userAgent;
}
// Converte array de headers para o formato do cURL
$headerLines = [];
foreach ($this->headers as $name => $value) {
$headerLines[] = $name . ': ' . $value;
}
if (!empty($headerLines)) {
$options[CURLOPT_HTTPHEADER] = $headerLines;
}
// Define os Cookies
if (!empty($this->cookies)) {
$cookieStr = implode('; ', array_filter($this->cookies, 'is_string'));
if (!empty($cookieStr)) {
$options[CURLOPT_COOKIE] = $cookieStr;
}
}
// Define o Proxy
if (!empty($this->proxy)) {
$options[CURLOPT_PROXY] = $this->proxy['host'] . ':' . $this->proxy['port'];
if (!empty($this->proxy['username']) && !empty($this->proxy['password'])) {
$options[CURLOPT_PROXYUSERPWD] = $this->proxy['username'] . ':' . $this->proxy['password'];
}
}
// Adiciona opções adicionais
foreach ($additionalOptions as $key => $value) {
if (is_int($key)) {
$options[$key] = $value;
}
}
return $options;
}
/**
* Executa uma requisição HTTP
*
* @param string $url URL da requisição
* @param array $options Opções adicionais do cURL
* @return array Retorna array com o conteúdo e informações da requisição
* @throws Exception Se a requisição falhar após todas as tentativas
*/
protected function execute($url, array $options = [])
{
$attempts = 0;
$lastError = null;
while ($attempts < $this->maxRetries) {
$ch = curl_init();
$curlOptions = $this->prepareOptions($url, $options);
if (!curl_setopt_array($ch, $curlOptions)) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("Falha ao definir opções do cURL: " . $error);
}
$content = curl_exec($ch);
$error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($content !== false && empty($error)) {
return [
'content' => $content,
'info' => $info
];
}
$lastError = $error ?: 'HTTP ' . $info['http_code'];
$attempts++;
if ($attempts < $this->maxRetries) {
usleep($this->retryDelay);
}
}
throw new Exception("Falha após {$this->maxRetries} tentativas. Último erro: " . $lastError);
}
/**
* Executa uma requisição GET
*
* @param string $url URL da requisição
* @param array $options Opções adicionais do cURL
* @return array Retorna array com o conteúdo e informações da requisição
*/
public function get($url, array $options = [])
{
return $this->execute($url, $options);
}
/**
* Executa uma requisição HEAD
*
* @param string $url URL da requisição
* @param array $options Opções adicionais do cURL
* @return array Retorna array com o conteúdo e informações da requisição
*/
public function head($url, array $options = [])
{
$options[CURLOPT_NOBODY] = true;
return $this->execute($url, $options);
}
/**
* Executa uma requisição POST
*
* @param string $url URL da requisição
* @param mixed $data Dados a serem enviados no corpo da requisição
* @param array $options Opções adicionais do cURL
* @return array Retorna array com o conteúdo e informações da requisição
*/
public function post($url, $data = null, array $options = [])
{
$options[CURLOPT_POST] = true;
if ($data !== null) {
$options[CURLOPT_POSTFIELDS] = is_array($data) ? http_build_query($data) : $data;
}
return $this->execute($url, $options);
}
/**
* Executa uma requisição PUT
*
* @param string $url URL da requisição
* @param mixed $data Dados a serem enviados no corpo da requisição
* @param array $options Opções adicionais do cURL
* @return array Retorna array com o conteúdo e informações da requisição
*/
public function put($url, $data = null, array $options = [])
{
$options[CURLOPT_CUSTOMREQUEST] = 'PUT';
if ($data !== null) {
$options[CURLOPT_POSTFIELDS] = is_array($data) ? http_build_query($data) : $data;
}
return $this->execute($url, $options);
}
/**
* Executa uma requisição DELETE
*
* @param string $url URL da requisição
* @param array $options Opções adicionais do cURL
* @return array Retorna array com o conteúdo e informações da requisição
*/
public function delete($url, array $options = [])
{
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
return $this->execute($url, $options);
}
}

View file

@ -14,39 +14,33 @@
require_once 'Rules.php'; require_once 'Rules.php';
require_once 'Cache.php'; require_once 'Cache.php';
require_once 'Curl.php';
class URLAnalyzer class URLAnalyzer
{ {
/** /**
* @var array Lista de User Agents disponíveis para requisições * @var array Lista de User Agents disponíveis para requisições
*/ */
protected $userAgents; private $userAgents;
/** /**
* @var int Número máximo de tentativas para obter conteúdo * @var int Número máximo de tentativas para obter conteúdo
*/ */
protected $maxAttempts; private $maxAttempts;
/** /**
* @var array Lista de servidores DNS para resolução * @var array Lista de servidores DNS para resolução
*/ */
protected $dnsServers; private $dnsServers;
/** /**
* @var Rules Instância da classe de regras * @var Rules Instância da classe de regras
*/ */
protected $rules; private $rules;
/** /**
* @var Cache Instância da classe de cache * @var Cache Instância da classe de cache
*/ */
protected $cache; private $cache;
/**
* @var Curl Instância da classe de curl
*/
protected $curl;
/** /**
* Construtor da classe * Construtor da classe
@ -59,7 +53,6 @@ class URLAnalyzer
$this->dnsServers = explode(',', DNS_SERVERS); $this->dnsServers = explode(',', DNS_SERVERS);
$this->rules = new Rules(); $this->rules = new Rules();
$this->cache = new Cache(); $this->cache = new Cache();
$this->curl = new Curl();
} }
/** /**
@ -70,22 +63,29 @@ class URLAnalyzer
*/ */
public function checkRedirects($url) public function checkRedirects($url)
{ {
$this->curl->setUserAgent($this->userAgents[array_rand($this->userAgents)]['user_agent']); $ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => $this->userAgents[array_rand($this->userAgents)]['user_agent'],
CURLOPT_SSL_VERIFYPEER => false
]);
$response = curl_exec($ch);
$finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
try {
$response = $this->curl->head($url);
return [ return [
'finalUrl' => $response['info']['url'], 'finalUrl' => $finalUrl,
'hasRedirect' => ($response['info']['url'] !== $url), 'hasRedirect' => ($finalUrl !== $url),
'httpCode' => $response['info']['http_code'] 'httpCode' => $httpCode
]; ];
} catch (Exception $e) {
return [
'finalUrl' => $url,
'hasRedirect' => false,
'httpCode' => 0
];
}
} }
/** /**
@ -153,6 +153,7 @@ class URLAnalyzer
* Tenta obter o conteúdo da URL com múltiplas tentativas * Tenta obter o conteúdo da URL com múltiplas tentativas
* *
* @param string $url URL para buscar conteúdo * @param string $url URL para buscar conteúdo
* @param string $resolvedIp IP resolvido do domínio
* @return string Conteúdo obtido * @return string Conteúdo obtido
* @throws Exception Se todas as tentativas falharem * @throws Exception Se todas as tentativas falharem
*/ */
@ -165,28 +166,14 @@ class URLAnalyzer
$userAgentKeys = array_keys($this->userAgents); $userAgentKeys = array_keys($this->userAgents);
$totalUserAgents = count($userAgentKeys); $totalUserAgents = count($userAgentKeys);
$this->curl->setMaxRetries($this->maxAttempts);
$this->curl->setRetryDelay(500000); // 0.5 segundo entre tentativas
while ($attempts < $this->maxAttempts) { while ($attempts < $this->maxAttempts) {
try { try {
// Seleciona um user agent de forma rotativa // Seleciona um user agent de forma rotativa
$currentUserAgentKey = $userAgentKeys[$attempts % $totalUserAgents]; $currentUserAgentKey = $userAgentKeys[$attempts % $totalUserAgents];
$result = $this->fetchContent($url, $currentUserAgentKey); $content = $this->fetchContent($url, $currentUserAgentKey);
// Se o HTTP code não for 200
if ($result['http_code'] !== 200) {
try {
$content = $this->fetchFromWaybackMachine($url);
if (!empty($content)) { if (!empty($content)) {
return $content; return $content;
} }
} catch (Exception $e) {
$errors[] = "Wayback Machine: " . $e->getMessage();
}
} else if (!empty($result['content'])) {
return $result['content'];
}
} catch (Exception $e) { } catch (Exception $e) {
$errors[] = $e->getMessage(); $errors[] = $e->getMessage();
} }
@ -195,7 +182,7 @@ class URLAnalyzer
usleep(500000); // 0.5 segundo de espera entre tentativas usleep(500000); // 0.5 segundo de espera entre tentativas
} }
// Se todas as tentativas falharem, tenta buscar do Wayback Machine uma última vez // Se todas as tentativas falharem, tenta buscar do Wayback Machine
try { try {
$content = $this->fetchFromWaybackMachine($url); $content = $this->fetchFromWaybackMachine($url);
if (!empty($content)) { if (!empty($content)) {
@ -222,11 +209,25 @@ class URLAnalyzer
// Primeiro, verifica a disponibilidade de snapshots // Primeiro, verifica a disponibilidade de snapshots
$availabilityUrl = "https://archive.org/wayback/available?url=" . urlencode($cleanUrl); $availabilityUrl = "https://archive.org/wayback/available?url=" . urlencode($cleanUrl);
try { $ch = curl_init();
$this->curl->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'); curl_setopt_array($ch, [
$response = $this->curl->get($availabilityUrl); CURLOPT_URL => $availabilityUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
CURLOPT_SSL_VERIFYPEER => false
]);
$data = json_decode($response['content'], true); $response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error || empty($response)) {
return null;
}
$data = json_decode($response, true);
if (!isset($data['archived_snapshots']['closest']['url'])) { if (!isset($data['archived_snapshots']['closest']['url'])) {
return null; return null;
} }
@ -234,10 +235,31 @@ class URLAnalyzer
// Obtém o snapshot mais recente // Obtém o snapshot mais recente
$archiveUrl = $data['archived_snapshots']['closest']['url']; $archiveUrl = $data['archived_snapshots']['closest']['url'];
$response = $this->curl->get($archiveUrl); // Busca o conteúdo do snapshot
$content = $response['content']; $ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $archiveUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 2,
CURLOPT_TIMEOUT => 30,
CURLOPT_ENCODING => '',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Cache-Control: no-cache',
'Pragma: no-cache'
]
]);
if (empty($content)) { $content = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error || $httpCode >= 400 || empty($content)) {
return null; return null;
} }
@ -245,17 +267,15 @@ class URLAnalyzer
$content = preg_replace('/<!-- BEGIN WAYBACK TOOLBAR INSERT -->.*?<!-- END WAYBACK TOOLBAR INSERT -->/s', '', $content); $content = preg_replace('/<!-- BEGIN WAYBACK TOOLBAR INSERT -->.*?<!-- END WAYBACK TOOLBAR INSERT -->/s', '', $content);
return $content; return $content;
} catch (Exception $e) {
return null;
}
} }
/** /**
* Realiza requisição HTTP usando cURL * Realiza requisição HTTP usando cURL
* *
* @param string $url URL para requisição * @param string $url URL para requisição
* @param string $resolvedIp IP resolvido do domínio
* @param string $userAgentKey Chave do user agent a ser utilizado * @param string $userAgentKey Chave do user agent a ser utilizado
* @return array Array com o conteúdo e código HTTP * @return string Conteúdo obtido
* @throws Exception Em caso de erro na requisição * @throws Exception Em caso de erro na requisição
*/ */
private function fetchContent($url, $userAgentKey) private function fetchContent($url, $userAgentKey)
@ -267,36 +287,84 @@ class URLAnalyzer
// Obtém a configuração do user agent // Obtém a configuração do user agent
$userAgentConfig = $this->userAgents[$userAgentKey]; $userAgentConfig = $this->userAgents[$userAgentKey];
$userAgent = $userAgentConfig['user_agent'];
// Configura o curl $curlOptions = [
$this->curl->setUserAgent($userAgentConfig['user_agent']); CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
// Adiciona headers específicos do user agent CURLOPT_FOLLOWLOCATION => true,
if (isset($userAgentConfig['headers'])) { CURLOPT_MAXREDIRS => 2,
$this->curl->setHeaders($userAgentConfig['headers']); CURLOPT_TIMEOUT => 10,
} CURLOPT_ENCODING => '',
CURLOPT_USERAGENT => $userAgent,
// Adiciona headers específicos do domínio CURLOPT_SSL_VERIFYPEER => false,
if ($domainRules !== null && isset($domainRules['headers'])) { CURLOPT_DNS_SERVERS => implode(',', $this->dnsServers)
$this->curl->setHeaders($domainRules['headers']);
}
// Adiciona cookies específicos do domínio
if ($domainRules !== null && isset($domainRules['cookies'])) {
$this->curl->setCookies($domainRules['cookies']);
}
try {
$response = $this->curl->get($url);
return [
'content' => $response['content'],
'http_code' => $response['info']['http_code']
]; ];
} catch (Exception $e) {
throw new Exception("Erro ao obter conteúdo: " . $e->getMessage()); // Prepara os headers
$headers = [
'Host: ' . $host,
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control: no-cache',
'Pragma: no-cache'
];
// Adiciona os headers específicos do user agent
if (isset($userAgentConfig['headers'])) {
foreach ($userAgentConfig['headers'] as $headerName => $headerValue) {
$headers[] = $headerName . ': ' . $headerValue;
} }
} }
// Adiciona headers específicos do domínio se existirem
if ($domainRules !== null && isset($domainRules['userAgent'])) {
$curlOptions[CURLOPT_USERAGENT] = $domainRules['userAgent'];
}
// Adiciona headers específicos do domínio se existirem
if ($domainRules !== null && isset($domainRules['customHeaders'])) {
foreach ($domainRules['customHeaders'] as $headerName => $headerValue) {
$headers[] = $headerName . ': ' . $headerValue;
}
}
$curlOptions[CURLOPT_HTTPHEADER] = $headers;
$curlOptions[CURLOPT_COOKIESESSION] = true;
$curlOptions[CURLOPT_FRESH_CONNECT] = true;
if ($domainRules !== null && isset($domainRules['cookies'])) {
$cookies = [];
foreach ($domainRules['cookies'] as $name => $value) {
if ($value !== null) {
$cookies[] = $name . '=' . $value;
}
}
if (!empty($cookies)) {
$curlOptions[CURLOPT_COOKIE] = implode('; ', $cookies);
}
}
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
$content = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new Exception("Erro CURL: " . $error);
}
if ($httpCode >= 400) {
throw new Exception("Erro HTTP: " . $httpCode);
}
return $content;
}
/** /**
* Limpa e normaliza uma URL * Limpa e normaliza uma URL
* *