<commit_before><?
// The source code packaged with this file is Free Software, Copyright (C) 2007 by
// <NAME> :: Suki_ :: <david at sukiweb dot net>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

function check_ban_proxy() {
	global $globals;
	if (($ban = check_ban($globals['user_ip'], 'proxy'))) return $ban;
	else return check_ban($_SERVER['REMOTE_ADDR'], 'proxy');
}

function check_ban($ban_text, $ban_type, $check_valid = true, $first_level = false) {
	global $db, $globals;	
	
	$ban_text = $db->escape($ban_text);
	$ban_type = $db->escape($ban_type);
	
	// If check_valid == false does not check for validity of the address
	// in order to avoid problems with bad links in external pages
	switch ($ban_type) {
		case 'email':
		case 'hostname':
		case 'punished_hostname':
			// Clean protocol and path/arguments
			$ban_text = preg_replace('/^(https*|ftp):\/\//', '', $ban_text);
			// Delete double "/" that can be used to cheat the control
			$ban_text = preg_replace('/\/+/', '/', $ban_text);
			// It leaves up to second level path
			$ban_text = preg_replace('/(\/[^\/\?]+)(\/[^\/\?]+){0,1}[\/\?]+.*$/', '$1$2', $ban_text);
			$ban_text = preg_replace('/\.*$/', '', $ban_text);
			if ($check_valid  && ! preg_match('/^([\w_\-\.]+\.[\w]{2,4}(\/[a-z\.]+\/*){0,1}|[\w]{2,5})$/', $ban_text)) {
				$ban = array();
				$ban['match'] =  $ban_text;
				$ban['comment'] = _('No es un dominio correcto');
				return $ban;
			}
			$where= " ban_text IN (".subdomains_list($ban_text, $first_level).") AND ban_type='$ban_type' AND (ban_expire IS null OR ban_expire > now()) ";
			break;
		case 'ip':
		case 'proxy':
			//Quizá convendría revisar este preg_mach para revisar las IPs válidas mejor.
			if ($check_valid  && ! preg_match('/^[1-9]\d{0,2}\.(\d{1,3}\.){2}\d{1,3}$/s', $ban_text)) {
				$ban = array();
				$ban['match'] =  $ban_text;
				$ban['comment'] =_('No es una IP válida');
				return $ban;
			}
			$where="ban_text='$ban_text' AND ban_type='$ban_type' AND (ban_expire IS null OR ban_expire > now())"; 
			break;
		default:
			return false;
	}

	$match=$db->get_row("SELECT ban_text, ban_comment, UNIX_TIMESTAMP(ban_date) as date, UNIX_TIMESTAMP(ban_expire) as expire FROM bans WHERE $where LIMIT 1");
	if ($match) {
		$ban = array();
		$ban['date'] = $match->date;
		$ban['expire'] = $match->expire;
		$ban['text'] = htmlentities($ban_text);
		// For security
		$ban['match']  = htmlentities(trim($match->ban_text));
		$ban['comment'] = $match->ban_comment;
		return $ban;
	}
	return false;
}

function subdomains_list($domain_path, $first_level = false) {
	$paths = array();
	// search for the first part of the path
	if(preg_match('/^[^\/]+\/+([^\/\?]+)[\/\?]*/', $domain_path, $match) > 0) {
		$paths[0] = $match[1];
		// search for the second part of the path
		if($paths[0] && preg_match('/^[^\/]+\/+[^\/\?]+\/+([^\/\?]+)[\/\?]*/', $domain_path, $match) > 0) {
			$paths[1] = $paths[0].'/'.$match[1];
		}
	}

	$domain = preg_replace('/\/.*$/', '', $domain_path);
	$list = "'$domain'";
	foreach ($paths as $path) {
		$list .= ", '$domain/$path', '$domain/$path/'";
	}
	$array = explode('.', $domain);
	$size = count($array);
	if ($first_level) $domain_limit = $size;
	else $domain_limit = $size - 1;

	for($i=1; $i < $domain_limit; $i++) {
		$sub = array_slice($array, $i);<commit_msg>Agregado control de subclases de IP<commit_after><?
// The source code packaged with this file is Free Software, Copyright (C) 2007 by
// <NAME> :: Suki_ :: <david at sukiweb dot net>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

function check_ban_proxy() {
	global $globals;
	if (($ban = check_ban($globals['user_ip'], 'proxy'))) return $ban;
	else return check_ban($_SERVER['REMOTE_ADDR'], 'proxy');
}

function check_ban($ban_text, $ban_type, $check_valid = true, $first_level = false) {
	global $db, $globals;	
	
	$ban_text = $db->escape($ban_text);
	$ban_type = $db->escape($ban_type);
	
	// If check_valid == false does not check for validity of the address
	// in order to avoid problems with bad links in external pages
	switch ($ban_type) {
		case 'email':
		case 'hostname':
		case 'punished_hostname':
			// Clean protocol and path/arguments
			$ban_text = preg_replace('/^(https*|ftp):\/\//', '', $ban_text);
			// Delete double "/" that can be used to cheat the control
			$ban_text = preg_replace('/\/+/', '/', $ban_text);
			// It leaves up to second level path
			$ban_text = preg_replace('/(\/[^\/\?]+)(\/[^\/\?]+){0,1}[\/\?]+.*$/', '$1$2', $ban_text);
			$ban_text = preg_replace('/\.*$/', '', $ban_text);
			if ($check_valid  && ! preg_match('/^([\w_\-\.]+\.[\w]{2,4}(\/[a-z\.]+\/*){0,1}|[\w]{2,5})$/', $ban_text)) {
				$ban = array();
				$ban['match'] =  $ban_text;
				$ban['comment'] = _('No es un dominio correcto');
				return $ban;
			}
			$where= " ban_text IN (".subdomains_list($ban_text, $first_level).") AND ban_type='$ban_type' AND (ban_expire IS null OR ban_expire > now()) ";
			break;
		case 'ip':
		case 'proxy':
			//Quizá convendría revisar este preg_mach para revisar las IPs válidas mejor.
			if ($check_valid  && ! preg_match('/^\d+\.[\d\.]+$/s', $ban_text)) {
				$ban = array();
				$ban['match'] =  $ban_text;
				$ban['comment'] =_('No es una IP válida');
				return $ban;
			}
			$list = subclasses_list($ban_text);
			$where="ban_text IN ($list) AND ban_type='$ban_type' AND (ban_expire IS null OR ban_expire > now())"; 
			break;
		default:
			return false;
	}

	$match=$db->get_row("SELECT ban_text, ban_comment, UNIX_TIMESTAMP(ban_date) as date, UNIX_TIMESTAMP(ban_expire) as expire FROM bans WHERE $where LIMIT 1");
	if ($match) {
		$ban = array();
		$ban['date'] = $match->date;
		$ban['expire'] = $match->expire;
		$ban['text'] = htmlentities($ban_text);
		// For security
		$ban['match']  = htmlentities(trim($match->ban_text));
		$ban['comment'] = $match->ban_comment;
		return $ban;
	}
	return false;
}

function subclasses_list($ip) {
	$list = "'$ip'";
	$array = explode('.', $ip);
	$size = count($array) - 1;
	while ($size > 1)  {
		$new_class = $array[0];
		for ($i=1; $i < $size; $i++) {
			$new_class .= '.' . $array[$i];
		}
		$list .= ",'$new_class'";
		$size--;
	}
	return $list;
}

function subdomains_list($domain_path, $first_level = false) {
	$paths = array();
	// search for the first part of the path
	if(preg_match('/^[^\/]+\/+([^\/\?]+)[\/\?]*/', $domain_path, $match) > 0) {
		$paths[0] = $match[1];
		// search for the second part of the path
		if($paths[0] && preg_match('/^[^\/]+\/+[^\/\?]+\/+([^\/\?]+)[\/\?]*/', $domain_path, $match) > 0) {
			$paths[1] = $paths[0].'/'.$match[1];
		}
	}

	$domain = preg_replace('/\/.*$/', '', $domain_path);
	$list = "'$domain'";
	foreach ($paths as $path) {
		$list .= ", '$domain/$path', '$domain/$path/'";
	}
	$array = explode('.', $domain);
	$size = count($array);
	if ($first_level) $domain_limit = $size;
	else $domain_limit = $size - 1;

	for($i=1; $i < $domain_limit; $i++) {
		$sub = array_slice($array, $i);
{'fraction_non_alphanumeric': 0.22588555858310627, 'fraction_numerical': 0.008583106267029973, 'mean_word_length': 3.714836223506744, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4132212', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2764, 'n_words': 683}