<?php

$profile = new user($seo[1], null, null, true);
if ($profile) :
	$userData = $profile->get_data();
	$me = (LOGGEDIN && $user['userId'] == $userData['userId'] ? true : false);
endif;

if ($me)
{

	if (isset($_POST["userEdit"]))
	{
		$name	= $_POST['userPublicName'];		if (!Check::name($name))						array_push($error, $_locale['edit_wrong_public_name']);
		$rname	= $_POST['userRealName'];		if (!Check::name($rname))						array_push($error, $_locale['edit_wrong_real_name']);
		$email	= $_POST['userEmail'];			if (strlen($email) && !Check::email($email))	array_push($error, $_locale['edit_wrong_email']);
		$web	= $_POST['userWeb'];			if (!Check::domain($web))						array_push($error, $_locale['edit_wrong_web']);
		$bio	= htmlspecialchars($_POST['userIntroduction']);	if (strlen($bio) > 200)			array_push($error, $_locale['edit_wrong_introduction']);

		$bio = sqlprot($bio);

		if (empty($error))
			if ($_sql->query("UPDATE users SET userPublicName = '$name',userRealName = '$rname',userEmail = '$email',userWeb = '$web',userIntroduction = '$bio' WHERE userId = $user[userId]"))
				redirect(get_profile_link());
			else
				array_push($error, $_locale['profile_not_updated']);
	}

	if (isset($_FILES["userPic"]))
	{
		$file = 'data/profile_pics/'.$user['userId'].'.jpg';
		if (file_exists($file)) unlink($file);
		if ($_FILES["userPic"]["error"] < 1 && in_array($_FILES["userPic"]["type"], explode(',', $_set['allowedPicTypes'])))
		{
			clear_cache();

			$thumb = new Imagick($_FILES["userPic"]["tmp_name"]);
			//$thumb->resizeImage(500, 500, Imagick::FILTER_POINT, 1, true);
			$thumb->cropThumbnailImage(500, 500);
			$thumb->setImageFormat('jpg');
			$thumb->writeImage($file);
			$thumb->destroy(); 

			$profile->setPic(true);

			redirect(get_current_link());
		} else
		{
			$profile->setPic(false);
		}
	} else
	{
		//$profile->setPic(false);
		//redirect(get_current_link());
	}
}

if (isset($seo[2]) && $seo[2] == 'edit')
{
	addTitle($_locale['profile_edit']);
	if (theme_component('profile_edit'))
		include theme_component('profile_edit');
	else
		include 'includes/main/profile_edit.php';
}
else
{
	addTitle($userData['userPublicName'].$_locale['s_profile']);

if (theme_component('profile')) :
	include theme_component('profile');
else :
	if ($profile) :
?>
	<div class="profile pic">
		<div id="profile_pic" style="background-image: url('<?=get_profile_picture($userData)?>')">
		<?php if ($me) : ?>
			<form action="<?=get_current_link()?>" method="post" name="userpic-upload" enctype="multipart/form-data">
				<input type="file" name="userPic" id="userPicInput" style="display: none" />
				<button type="button" onclick="$('#userPicInput').focus().click()">{locale:browse}</button><button type="submit" id="userPicSaveBtn" class="orange">{locale:delete}</button>
			</form>
			<script>
				$("#userPicInput").change(function() {
					$("#userPicSaveBtn").html('{locale:save}').removeClass('orange');
				});
			</script>
		<?php endif ?>
		</div>
	</div>
	<div class="profile details">
		<h1><?=$userData['userPublicName'].$_locale['s_profile']?></h1>
		<h3><?=$userData['userName'].($me ? ' <a href="'.get_current_link().'/edit" class="edit">{locale:profile_edit}</a>' : '')?></h3>

		<?php if (LOGGEDIN) : ?>
		<div class='box contact'>
			<p><strong>{locale:name}:</strong> <?=$userData['userRealName']?></p>
			<p><strong>{locale:email}:</strong> <?=$userData['userEmail']?></p>
			<p><strong>{locale:web}:</strong> <?=$userData['userWeb']?></p>
		</div>
		<div class='spacer'></div>
		<?php endif ?>

		<?php
		$recent = $_sql->query("SELECT entrySlug, entryTitle FROM entries WHERE entryBy = $userData[userId] AND entryPublished <= ".time()." ORDER BY entryPublished DESC LIMIT 5");
		if ($recent->num_rows) : ?>
		<div class='box recent'>
			<?php
			while ($data = $recent->fetch_assoc())
				echo "<p><a href='".get_entry_link($data['entrySlug'])."'>".trimlink($data['entryTitle'], 42)."</a></p>\n";
			?>
		</div>
		<?php endif; unset($recent); ?>

		<?php if ($userData['userIntroduction']) : ?>
		<div class='box introduction'>
			<p><?=$userData['userIntroduction']?></p>
		</div>
		<?php endif ?>

		<div class="clear"></div>
	</div>
	<div class="clear"></div>
<?php else : ?>
	<h1>{locale:profile}</h1>
	<p>{locale:profile_not_found}</p>
<?php endif; endif; } ?>