<commit_before><?php

$title = __("Mood avatars");

$crumbs = new PipeMenu();
$crumbs->add(new PipeMenuLinkEntry(__("Mood avatars"), "editavatars"));
makeBreadcrumbs($crumbs);

AssertForbidden("editMoods");

if(!$loguserid)
	Kill(__("You must be logged in to edit your avatars."));

if(isset($_POST['action']))
{
	$mid = (int)$_POST['mid'];
	if($_POST['action'] == __("Rename"))
	{
		Query("update {moodavatars} set name={0} where mid={1} and uid={2}", $_POST['name'], $mid, $loguserid);
		Alert(__("Avatar renamed."), __("Okay"));
	}
	else if($_POST['action'] == __("Delete"))
	{
		Query("delete from {moodavatars} where uid={0} and mid={1}", $loguserid, $mid);
		Query("update {posts} set mood=0 where user={0} and mood={1}", $loguserid, $mid);
		if(file_exists("{$dataDir}avatars/".$loguserid."_".$mid))
			unlink("{$dataDir}avatars/".$loguserid."_".$mid);
		Alert(__("Avatar deleted."), __("Okay"));
	}
	else if($_POST['action'] == __("Add"))
	{
		$highest = FetchResult("select mid from {moodavatars} where uid={0} order by mid desc limit 1", $loguserid);
		if($highest < 1)
			$highest = 1;
		$mid = $highest + 1;

		//Begin copypasta from edituser/editprofile_avatar...
		if($fname = $_FILES['picture']['name'])
		{
			$fext = strtolower(substr($fname,-4));
			$error = "";

			$exts = array(".png",".jpg",".gif");
			$dimx = 100;
			$dimy = 100;
			$dimxs = 60;
			$dimys = 60;
			$size = 30720;

			$validext = false;
			$extlist = "";
			foreach($exts as $ext)
			{
				if($fext == $ext)
				$validext = true;
				$extlist .= ($extlist ? ", " : "").$ext;
			}
			if(!$validext)
				$error.="<li>".__("Invalid file type, must be one of:")." ".$extlist."</li>";

			if(!$error)
			{
				$tmpfile = $_FILES['picture']['tmp_name'];
				$file = "{$dataDir}avatars/".$loguserid."_".$mid;

				if($_POST['name'] == "")
					$_POST['name'] = "#".$mid;

				Query("insert into {moodavatars} (uid, mid, name) values ({0}, {1}, {2})", $loguserid, $mid, $_POST['name']);

				if($loguser['powerlevel'])	//Are we at least a local mod?
					copy($tmpfile,$file);	//Then ignore the 100x100 rule.
				else
				{
					list($width, $height, $type) = getimagesize($tmpfile);

					if($type == 1) $img1 = imagecreatefromgif ($tmpfile);
					if($type == 2) $img1 = imagecreatefromjpeg($tmpfile);
					if($type == 3) $img1 = imagecreatefrompng ($tmpfile);

					if($width <= $dimx && $height <= $dimy && $type<=3)
						copy($tmpfile,$file);
					elseif($type <= 3)
					{
						$r = imagesx($img1) / imagesy($img1);
						if($r > 1)
						{
							$img2=imagecreatetruecolor($dimx,floor($dimy / $r));
							imagecopyresampled($img2,$img1,0,0,0,0,$dimx,$dimy/$r,imagesx($img1),imagesy($img1));
						} else
						{
							$img2=imagecreatetruecolor(floor($dimx * $r), $dimy);
							imagecopyresampled($img2,$img1,0,0,0,0,$dimx*$r,$dimy,imagesx($img1),imagesy($img1));
						}
						imagepng($img2,$file);
					} else
						$error.="<li>Invalid format.</li>";
				}
				$usepic = $file;
			} else
				Kill(__("Could not update your avatar for the following reason(s):")."<ul>".$error."</ul>");
		}
	}
}

$moodRows = "";
$rMoods = Query("select mid, name from {moodavatars} where uid={0} order by mid asc", $loguserid);
while($mood = Fetch($rMoods))
{
	$cellClass = ($cellClass+1) % 2;
	$moodRows .= format(
"
		<tr class=\"cell{0}\">
			<td style=\"width: 100px;\">
				<img src=\"img/avatars/{1}_{2}\" alt=\"\">
			</td>
			<td><commit_msg>Fix avatars not showing up on the page (mistypo?)<commit_after><?php

$title = __("Mood avatars");

$crumbs = new PipeMenu();
$crumbs->add(new PipeMenuLinkEntry(__("Mood avatars"), "editavatars"));
makeBreadcrumbs($crumbs);

AssertForbidden("editMoods");

if(!$loguserid)
	Kill(__("You must be logged in to edit your avatars."));

if(isset($_POST['action']))
{
	$mid = (int)$_POST['mid'];
	if($_POST['action'] == __("Rename"))
	{
		Query("update {moodavatars} set name={0} where mid={1} and uid={2}", $_POST['name'], $mid, $loguserid);
		Alert(__("Avatar renamed."), __("Okay"));
	}
	else if($_POST['action'] == __("Delete"))
	{
		Query("delete from {moodavatars} where uid={0} and mid={1}", $loguserid, $mid);
		Query("update {posts} set mood=0 where user={0} and mood={1}", $loguserid, $mid);
		if(file_exists("{$dataDir}avatars/".$loguserid."_".$mid))
			unlink("{$dataDir}avatars/".$loguserid."_".$mid);
		Alert(__("Avatar deleted."), __("Okay"));
	}
	else if($_POST['action'] == __("Add"))
	{
		$highest = FetchResult("select mid from {moodavatars} where uid={0} order by mid desc limit 1", $loguserid);
		if($highest < 1)
			$highest = 1;
		$mid = $highest + 1;

		//Begin copypasta from edituser/editprofile_avatar...
		if($fname = $_FILES['picture']['name'])
		{
			$fext = strtolower(substr($fname,-4));
			$error = "";

			$exts = array(".png",".jpg",".gif");
			$dimx = 100;
			$dimy = 100;
			$dimxs = 60;
			$dimys = 60;
			$size = 30720;

			$validext = false;
			$extlist = "";
			foreach($exts as $ext)
			{
				if($fext == $ext)
				$validext = true;
				$extlist .= ($extlist ? ", " : "").$ext;
			}
			if(!$validext)
				$error.="<li>".__("Invalid file type, must be one of:")." ".$extlist."</li>";

			if(!$error)
			{
				$tmpfile = $_FILES['picture']['tmp_name'];
				$file = "{$dataDir}avatars/".$loguserid."_".$mid;

				if($_POST['name'] == "")
					$_POST['name'] = "#".$mid;

				Query("insert into {moodavatars} (uid, mid, name) values ({0}, {1}, {2})", $loguserid, $mid, $_POST['name']);

				if($loguser['powerlevel'])	//Are we at least a local mod?
					copy($tmpfile,$file);	//Then ignore the 100x100 rule.
				else
				{
					list($width, $height, $type) = getimagesize($tmpfile);

					if($type == 1) $img1 = imagecreatefromgif ($tmpfile);
					if($type == 2) $img1 = imagecreatefromjpeg($tmpfile);
					if($type == 3) $img1 = imagecreatefrompng ($tmpfile);

					if($width <= $dimx && $height <= $dimy && $type<=3)
						copy($tmpfile,$file);
					elseif($type <= 3)
					{
						$r = imagesx($img1) / imagesy($img1);
						if($r > 1)
						{
							$img2=imagecreatetruecolor($dimx,floor($dimy / $r));
							imagecopyresampled($img2,$img1,0,0,0,0,$dimx,$dimy/$r,imagesx($img1),imagesy($img1));
						} else
						{
							$img2=imagecreatetruecolor(floor($dimx * $r), $dimy);
							imagecopyresampled($img2,$img1,0,0,0,0,$dimx*$r,$dimy,imagesx($img1),imagesy($img1));
						}
						imagepng($img2,$file);
					} else
						$error.="<li>Invalid format.</li>";
				}
				$usepic = $file;
			} else
				Kill(__("Could not update your avatar for the following reason(s):")."<ul>".$error."</ul>");
		}
	}
}

$moodRows = "";
$rMoods = Query("select mid, name from {moodavatars} where uid={0} order by mid asc", $loguserid);
while($mood = Fetch($rMoods))
{
	$cellClass = ($cellClass+1) % 2;
	$moodRows .= format(
"
		<tr class=\"cell{0}\">
			<td style=\"width: 100px;\">
				<img src=\"{$dataDir}avatars/{1}_{2}\" alt=\"\">
			</td>
			<td>
{'fraction_non_alphanumeric': 0.23312083454071283, 'fraction_numerical': 0.02231237322515213, 'mean_word_length': 3.7804709141274238, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '315118', 'n_tokens_mistral': 3199, 'n_tokens_neox': 2659, 'n_words': 523}