<commit_before><?php
require_once __DIR__.'/../php/defaults.php';
require_once __DIR__.'/../php/config.php';

// Validate GET option

if (isset($_REQUEST['domain'])) {
	$domain = &$_REQUEST['domain'];
} else {
	fin_default();
}



// check login

require_once __DIR__.'/../php/functions/generic.php';

$db = open_db_session();

if(!user_check_access(false, true)){
	fin_default();
}


$html = CargaWebCurl('http://' . $domain);

$favicon = parseFavicon($html);
if ($favicon !== null) {
	if (preg_match('@^(?:https?:)?//@', $favicon)) {
		$faviconURL = $favicon;
	} else {
		$faviconURL = $domain . $favicon;
	}
} else {
	fin_default();
}

//echo $faviconURL;exit;
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $faviconURL);



function fin_default() {
	header('Location: //' . WEB_PATH . 'img/favicon/favicon-generic-32.png');
	exit;
}

function CargaWebCurl($url){
	$cabeceras = array();
	$cabeceras[] = 'Accept-Encoding: gzip';
	$cabeceras[] = 'Connection: Connection';
	$cabeceras[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0';
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_COOKIEFILE, ""); // Esto es para que en las redirecciones use las cookies que salgan durante las redirecciones
	
	curl_setopt($ch, CURLOPT_HTTPHEADER, $cabeceras);<commit_msg>Fix getfavicon downloading the image, not redirecting (to prevent problems with http <-> https)<commit_after><?php
require_once __DIR__.'/../php/defaults.php';
require_once __DIR__.'/../php/config.php';

// Validate GET option

if (isset($_REQUEST['domain'])) {
	$domain = &$_REQUEST['domain'];
} else {
	fin_default();
}



// check login

require_once __DIR__.'/../php/functions/generic.php';

$db = open_db_session();

if(!user_check_access(false, true)){
	fin_default();
}


$html = CargaWebCurl('http://' . $domain);

$favicon = parseFavicon($html);
if ($favicon !== null) {
	if (preg_match('@^(?:https?:)?//@', $favicon)) {
		$faviconURL = $favicon;
	} elseif (strpos($favicon, '/') === 0) {
		$faviconURL = 'http://' . $domain . $favicon;
	} else {
		$faviconURL = 'http://' . $domain . '/' . $favicon;
	}
} else {
	fin_default();
}

//echo $faviconURL;exit;
$imgHeaders = CargaWebCurl($faviconURL, false, true);
$img = CargaWebCurl($faviconURL, true, false);
if (preg_match("@Content-Type:{$s}(.*?)$@im", $imgHeaders)) {
	$type = $matches[1];
} else {
	$type = 'image/x-icon';
}

header('Cache-Control: max-age=2592000, public'); // 1 month

header('Content-Type: ' . $type);
echo $img;



function fin_default() {
	header('Location: //' . WEB_PATH . 'img/favicon/favicon-generic-32.png');
	exit;
}

function CargaWebCurl($url, $body = true, $header = false){
	$cabeceras = array();
	$cabeceras[] = 'Accept-Encoding: gzip';
	$cabeceras[] = 'Connection: Connection';
	$cabeceras[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0';
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_NOBODY, !$body);
	curl_setopt($ch, CURLOPT_HEADER, $header);
	
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_COOKIEFILE, ""); // Esto es para que en las redirecciones use las cookies que salgan durante las redirecciones
	
	curl_setopt($ch, CURLOPT_HTTPHEADER, $cabeceras);
{'fraction_non_alphanumeric': 0.19542203147353363, 'fraction_numerical': 0.018311874105865523, 'mean_word_length': 4.875630252100841, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968639', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1351, 'n_words': 265}