<commit_before><?
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// <NAME> <gallir at uib dot es>.
// 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 get_avatars_dir() {
	global $globals;
	return mnmpath.'/'.$globals['cache_dir'];
}

function is_avatars_enabled() {
	global $globals;
	return $globals['Amazon_S3_media_url'] || ($globals['cache_dir'] && is_writable(get_avatars_dir()));
}

function avatars_get_from_url($user, $url) {
	$res = get_url($url, $url, 1000000);
	if ($res && strlen($res['content']) < 1000000) { // Image is smaller than our limit
		$tmpfname = tempnam('/tmp', 'avatar');
		if ($tmpfname) {
			$bytes = file_put_contents($tmpfname, $res['content']);
			if($bytes) {
				avatars_manage_upload($user, false, $tmpfname);
			}
			@unlink($tmpfname);
			return true;
		}
	}
	return false;
}

function avatars_manage_upload($user, $name, $filename = false) {
	global $globals;

	$time = $globals['now'];

	if (!$globals['Amazon_S3_local_cache'] && $globals['Amazon_S3_media_bucket'] && is_writable('/tmp')) {
		$subdir = '/tmp';
	} else {
		$chain = get_cache_dir_chain($user);
		@mkdir(get_avatars_dir());
		create_cache_dir_chain(get_avatars_dir(), $chain);
		$subdir = get_avatars_dir() . '/'. $chain;
	}
	if (!is_writable($subdir)) return false;
	$file_base = $subdir . "/$user-$time";

	avatars_remove_user_files($user);
	if ($name) {
		// If $name is provided, the file was uploaded from a form
		move_uploaded_file($_FILES[$name]['tmp_name'], $file_base . '-orig.img');
	} elseif ($filename) {
		rename($filename, $file_base . '-orig.img');
	}


	$original_size = @getimagesize("$file_base-orig.img");
	if ($original_size == false) return false;


	avatar_resize("$file_base-orig.img", "$file_base-80.jpg", 80);
	$size = @getimagesize("$file_base-80.jpg");


	if (!($size[0] == 80 && $size[1] == 80 && ($mtime = avatars_db_store($user, "$file_base-80.jpg", $time)))) {
		// Mark FALSE in DB
		avatars_db_remove($user);
		avatars_remove_user_files($user);<commit_msg>Verificaciones mejores de avatar<commit_after><?
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// <NAME> <gallir at uib dot es>.
// 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 get_avatars_dir() {
	global $globals;
	return mnmpath.'/'.$globals['cache_dir'];
}

function is_avatars_enabled() {
	global $globals;
	return $globals['Amazon_S3_media_url'] || ($globals['cache_dir'] && is_writable(get_avatars_dir()));
}

function avatars_get_from_url($user, $url) {
	$res = get_url($url, $url, 1000000);
	if ($res && strlen($res['content']) < 1000000) { // Image is smaller than our limit
		$tmpfname = tempnam('/tmp', 'avatar');
		if ($tmpfname) {
			$bytes = file_put_contents($tmpfname, $res['content']);
			if($bytes) {
				avatars_manage_upload($user, false, $tmpfname);
			}
			@unlink($tmpfname);
			return true;
		}
	}
	return false;
}

function avatars_manage_upload($user, $name, $filename = false) {
	global $globals;

	$time = $globals['now'];

	if (!$globals['Amazon_S3_local_cache'] && $globals['Amazon_S3_media_bucket'] && is_writable('/tmp')) {
		$subdir = '/tmp';
	} else {
		$chain = get_cache_dir_chain($user);
		@mkdir(get_avatars_dir());
		create_cache_dir_chain(get_avatars_dir(), $chain);
		$subdir = get_avatars_dir() . '/'. $chain;
	}
	if (!is_writable($subdir)) return false;
	$file_base = $subdir . "/$user-$time";

	// First check the image is valid before moving and deleting
	// If $name is provided, the file was uploaded from a form
	if ($name) {
		$filename = $_FILES[$name]['tmp_name'];
	}
	$original_size = @getimagesize($filename);
	if ($original_size == false) {
		@unlink($filename);
		return false;
	}

	avatars_remove_user_files($user);

	// If $name is provided, the file was uploaded from a form
	if($name) {
		move_uploaded_file($filename, $file_base . '-orig.img');
	} else {
		rename($filename, $file_base . '-orig.img');
	}

	avatar_resize("$file_base-orig.img", "$file_base-80.jpg", 80);
	$size = @getimagesize("$file_base-80.jpg");


	if (!($size[0] == 80 && $size[1] == 80 && ($mtime = avatars_db_store($user, "$file_base-80.jpg", $time)))) {
		// Mark FALSE in DB
		avatars_db_remove($user);
		avatars_remove_user_files($user);
{'fraction_non_alphanumeric': 0.17358571735857173, 'fraction_numerical': 0.01505700150570015, 'mean_word_length': 4.7549504950495045, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '4923630', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1675, 'n_words': 388}