###############################################################################
# Profile.pl                                                                  #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2.3.1                                                  #
# Packaged:       January 04, 2009                                            #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2009 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by:  The YaBB Development Team                                     #
#               with assistance from the YaBB community.                      #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$profileplver = 'YaBB 2.3.1 $Revision: 1.109.2.30.2.42 $';
if ($action eq 'detailedversion') { return 1; }

require "$sourcedir/Decoder.pl";

&LoadLanguage('Profile');
if (!$parseflash) { &LoadLanguage('Display'); }

if (-e "$vardir/gmodsettings.txt" && $iamgmod) { require "$vardir/gmodsettings.txt"; }

# If someone registers with a '+' in their name It causes problems.
# Get's turned into a <space> in the query string Change it back here.
# Users who register with spaces get them replaced with _
# So no problem there.
$INFO{'username'} =~ tr/ /+/;

sub ProfileCheck {
	my $user = $INFO{'username'};
	if ($do_scramble_id) { &decloak($user); }
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	if ($iamguest) { &fatal_error('no_access'); }
	if ($user =~ m~/~)  { &fatal_error('no_user_slash'); }
	if ($user =~ m~\\~) { &fatal_error('no_user_backslash'); }

	my $sid_descript = $mycenter_profile_txt{siddescript};
	if ($_[0]) {
		$sid_descript = $mycenter_profile_txt{timeoutdescript};
		$redirsid = $_[0];
	} else {
		$redirsid = $INFO{'page'} || 'profile';
	}

	if ($user ne $username && !$iamadmin && !$iamgmod) { &fatal_error('not_allowed_profile_change'); }
	if ($iamgmod && ${$uid.$user}{'position'} eq 'Administrator') { &fatal_error('not_allowed_profile_change'); }
	if ($iamgmod && $user ne $username && !$allow_gmod_profile) { &fatal_error('not_allowed_profile_change'); }
	if ($user eq 'admin' && $username ne 'admin') { &fatal_error('not_allowed_profile_change'); }

	if (!-e ("$memberdir/$user.vars")) { &fatal_error("no_profile_exists"); }
	&ToHTML($user);

	$yymain .= qq~
<div class="bordercolor" style="width: 500px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">
<table cellpadding="4" cellspacing="1" border="0" width="100%" align="center">
	<tr><td class="titlebg" colspan="2"><b>$profile_txt{'901'}</b></td></tr>
	<tr>
		<td class="windowbg2" align="center">
			<span class="small"><br />$sid_descript<br /><br /></span>
		</td>
	</tr>
	<tr>
		<td class="windowbg2" valign="middle">
			<form action="$scripturl?action=profileCheck2;username=$useraccount{$user}" method="post" name="confirmform">
			<input type="hidden" name="redir" value="$redirsid" />
			<div style="clear: both; padding-top: 4px; margin-left: auto; margin-right: auto; width: 370px;">
				<span style="float: left; width: 100%; text-align: center; align: center;">
					<input type="password" name="passwrd" size="15" style="width: 150px;" />
				</span>
			</div>
			<div style="clear: both; margin-top: 25px; margin-left: auto; margin-right: auto; width: 310px;">
				<span style="float: left; width: 100%; text-align: center;">
					<input type="submit" value="$profile_txt{'900'}" class="button" />
				</span>
			</div>
			</form>
		</td>
	</tr>
</table>
</div>
<script type="text/javascript" language="JavaScript">
<!--
	document.confirmform.passwrd.focus();
//-->
</script>
~;

	$yynavigation = qq~&rsaquo; $profile_txt{'900'}~;
	$yytitle = $profile_txt{'900'};
	&template;
}

sub ProfileCheck2 {
	if ($iamguest) { &fatal_error('no_access'); }

	my $user = $INFO{'username'};
	&FromHTML($user);
	if ($do_scramble_id) { &decloak($user); }
	if ($user =~ m~\/~)  { &fatal_error('no_user_slash'); }
	if ($user =~ m~\\~)  { &fatal_error('no_user_backslash'); }
	if (!-e "$memberdir/$user.vars") { &fatal_error('no_profile_exists'); }
	if (($user ne $username && !$iamadmin && (!$iamgmod || ($iamgmod && !$allow_gmod_profile))) || ($user eq 'admin' && $username ne 'admin')) { &fatal_error('not_allowed_profile_change'); }

	&LoadUser($user);
	my $password = &encode_password($FORM{'passwrd'});
	if ($user eq $username && $password ne ${$uid.$username}{'password'}) {
		&fatal_error('current_password_wrong');
	}
	if (($iamadmin || ($iamgmod && $allow_gmod_profile)) && $password ne ${$uid.$username}{'password'}) {
		&fatal_error('no_admin_password');
	}
	# Update the sessionID too
	${$uid.$username}{'session'} = &encode_password($user_ip);
	&UserAccount($username, "update");

	# update only this cookie since we don't know when the others will expire
	$yySetCookies3 = &write_cookie(
			-name    => "$cookiesession_name",
			-value   => "${$uid.$username}{'session'}",
			-path    => "/",
			-expires => "Sunday, 17-Jan-2038 00:00:00 GMT");

	# Get a semi-secure SID - only for profile changes
	# cloak the sid -> no point giving anyone the means.
	$yySetLocation = "$scripturl?action=" . ($FORM{'redir'} || 'profile') . ";username=$useraccount{$user};sid=" . &cloak(reverse(substr($date, 5, 5)));
	&redirectexit;
}

sub PrepareProfile {
	my $user = $INFO{'username'};
	if ($do_scramble_id) { &decloak($user); }
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	if ($iamguest) { &fatal_error('no_access'); }
	if ($iamgmod && ${$uid.$user}{'position'} eq 'Administrator') { &fatal_error('not_allowed_profile_change'); }
	if ($user =~ m~/~)  { &fatal_error('no_user_slash'); }
	if ($user =~ m~\\~) { &fatal_error('no_user_backslash'); }
	if ($user ne $username && !$iamadmin && !$iamgmod) { &fatal_error('not_allowed_profile_change'); }
	if ($iamgmod && $user ne $username && !$allow_gmod_profile) { &fatal_error('not_allowed_profile_change'); }
	if ($user eq 'admin' && $username ne 'admin') { &fatal_error('not_allowed_profile_change'); }
	if (!-e ("$memberdir/$user.vars")) { &fatal_error('no_profile_exists'); }

	if ($allowpics) {
		opendir(DIR, "$facesdir") || fatal_error("cannot_open_dir","($facesdir)!<br \/>$profile_txt{'681'}", 1);
		closedir(DIR);
	}

	($dr_secund, $dr_minute, $dr_hour, $dr_day, $dr_month, $dr_year, undef, undef, undef) = localtime(${$uid.$user}{'regtime'} ? ${$uid.$user}{'regtime'} : $forumstart);
	$dr_month++;

	if (${$uid.$user}{'gender'} eq 'Male')   { $GenderMale   = ' selected="selected" '; }
	if (${$uid.$user}{'gender'} eq 'Female') { $GenderFemale = ' selected="selected" '; }

	$signature = ${$uid.$user}{'signature'};
	$signature =~ s/<br.*?>/\n/g;

	&CalcAge($user, "parse");
	${$uid.$user}{'aim'} =~ tr/+/ /;
	${$uid.$user}{'yim'} =~ tr/+/ /;

	if    (${$uid.$user}{'timeselect'} == 7) { $tsl7 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 6) { $tsl6 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 5) { $tsl5 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 4) { $tsl4 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 3) { $tsl3 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 2) { $tsl2 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 1) { $tsl1 = ' selected="selected" '; }
	elsif (${$uid.$user}{'timeselect'} == 0) { $tsl0 = ' selected="selected" '; }
	elsif ($timeselected == 7) { $tsl7 = ' selected="selected" '; }
	elsif ($timeselected == 6) { $tsl6 = ' selected="selected" '; }
	elsif ($timeselected == 5) { $tsl5 = ' selected="selected" '; }
	elsif ($timeselected == 4) { $tsl4 = ' selected="selected" '; }
	elsif ($timeselected == 3) { $tsl3 = ' selected="selected" '; }
	elsif ($timeselected == 2) { $tsl2 = ' selected="selected" '; }
	elsif ($timeselected == 1) { $tsl1 = ' selected="selected" '; }
	else { $tsl0 = ' selected="selected" '; }

	$dayormonthm = qq~$profile_txt{'564'}<input type="text" name="bday1" size="2" maxlength="2" value="$umonth" />~;
	$dayormonthd = qq~$profile_txt{'565'}<input type="text" name="bday2" size="2" maxlength="2" value="$uday" />~;
	if ($tsl2 || $tsl3 || $tsl6) { $dayormonth = $dayormonthd . $dayormonthm; }
	else { $dayormonth = $dayormonthm . $dayormonthd; }

	@menucolors = qw(catbg catbg catbg catbg catbg catbg);
}

sub ProfileMenu {
	my $user = $INFO{'username'};

	$yymain .= qq~
	<table cellspacing="1" cellpadding="4" width="100%" border="0" class="bordercolor">
		<tr>
			<td class="$menucolors[0]" valign="bottom" align="center" width="16%"><span class="small"><b><a href="$scripturl?action=profile;username=$useraccount{$user};sid=$INFO{'sid'}">$profile_txt{79}</a></b></span></td>
			<td class="$menucolors[1]" valign="bottom" align="center" width="16%"><span class="small"><b><a href="$scripturl?action=profileContacts;username=$useraccount{$user};sid=$INFO{'sid'}">$profile_txt{819}</a></b></span></td>
			<td class="$menucolors[2]" valign="bottom" align="center" width="16%"><span class="small"><b><a href="$scripturl?action=profileOptions;username=$useraccount{$user};sid=$INFO{'sid'}">$profile_txt{818}</a></b></span></td>~;

	if ($buddyListEnabled){
		$yymain .= qq~
			<td class="$menucolors[3]" valign="bottom" align="center" width="16%"><span class="small"><b><a href="$scripturl?action=profileBuddy;username=$useraccount{$user};sid=$INFO{'sid'}">$profile_buddy_list{'buddylist'}</a></b></span></td>~;
	}

	if ($PM_level == 1 || ($PM_level == 2 && ($iamadmin || $iamgmod || $iammod)) || ($PM_level == 3 && ($iamadmin || $iamgmod))) {
		$yymain .= qq~
			<td class="$menucolors[4]" valign="bottom" align="center" width="16%"><span class="small"><b><a href="$scripturl?action=profileIM;username=$useraccount{$user};sid=$INFO{'sid'}">$profile_imtxt{56} $profile_txt{323}</a></b></span></td>~;
	}

	if ($iamadmin || ($iamgmod && $allow_gmod_profile && $gmod_access2{'profileAdmin'} eq 'on')) {
		$yymain .= qq~
			<td class="$menucolors[5]" valign="bottom" width="16%" align="center"><span class="small"><b><a href="$scripturl?action=profileAdmin;username=$useraccount{$user};sid=$INFO{'sid'}">$profile_txt{820}</a></b></span></td>~;
	}
	$yymain .= qq~
        </tr>
      </table>
<br />
~;
}

sub ModifyProfile {
	&SidCheck($action);

	&PrepareProfile;
	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }
	$menucolors[0] = "titlebg";
	if (!$view) { &ProfileMenu; }
	if ($iamadmin) {
		$confdel_text = qq~$profile_txt{'775'} $profile_txt{'777'} $user $profile_txt{'778'}~;
		if ($user eq $username) {
			$passtext = $profile_txt{'821'};
		} else {
			$passtext = qq~$profile_txt{'2'} $profile_txt{'36'}~;
		}
	} else {
		$confdel_text = qq~$profile_txt{'775'} $profile_txt{'776'} $profile_txt{'778'}~;
		$passtext = $profile_txt{'821'};
	}

	$passtext .= qq~<br /><span class="small" style="font-weight: normal;">$profile_txt{'895'}</span>~;

	my $scriptAction = qq~profile2~;
	if ($view) {
		$scriptAction = qq~myprofile2~;
		$yytitle = $profile_txt{'editmyprofile'};
		$profiletitle = qq~$profile_txt{'editmyprofile'} ($user)~;
		$yynavigation = qq~&rsaquo; <a href="$scripturl?action=mycenter" class="nav">$img_txt{'mycenter'}</a> &rsaquo; $profiletitle~;
	} else { 
		$yytitle = $profile_txt{'79'};
		$profiletitle = qq~$profile_txt{'79'} ($user)~;
		$yynavigation = qq~&rsaquo; $profiletitle~;
	}

	&LoadLanguage('Register');
	$showProfile   .= qq~
<form action="$scripturl?action=$scriptAction;username=$useraccount{$INFO{'username'}};sid=$INFO{'sid'}" method="post" name="creator">
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" border="0">
  <tr>
    <td class="catbg" colspan="2"><img src="$imagesdir/profile.gif" alt="" border="0" /> <b>$profiletitle</b><br /><span class="small">$profile_txt{'698'}</span></td>
  </tr><tr class="windowbg">
   <td width="220" align="left"><b>$profile_txt{81}: </b><br />
<span class="small">$profile_txt{'896'}</span>
</td>
   <td align="left">
	<div style="float:left;"><input type="password" maxlength="30" name="passwrd1" id="passwrd1" size="20" onkeyup="runPassword(this.value);" /> &nbsp; </div>
	<div style="float:left; width: 150px; height: 20px;">
		<div id="password-strength-meter" style="background: transparent url($imagesdir/empty_bar.gif) repeat-x center left; height: 4px"></div>
		<div class="pstrength-bar" id="passwrd1_bar" style="border: 1px solid #FFFFFF; height: 4px"></div>
		<div class="pstrength-info" id="passwrd1_text">&nbsp;</div>
	</div>
   </td>
 </tr><tr class="windowbg">
   <td width="220" align="left"><b>$profile_txt{82}: </b><br />
<span class="small">$profile_txt{'896'}</span>
</td>
   <td align="left"><input type="password" maxlength="30" name="passwrd2" size="20" /></td>
 </tr><tr class="windowbg">
   <td width="220" align="left"><b>$profile_txt{68}: </b><br />~;
	if($name_cannot_be_userid) {
		$showProfile .= qq~
<span class="small">$profile_txt{'8'}</span>~;
	}
	&ToChars(${$uid.$user}{'location'});
	$showProfile .= qq~
   </td>
   <td align="left"><input type="text" maxlength="30" name="name" size="30" value="${$uid.$user}{'realname'}" /></td>
 </tr><tr class="windowbg">
   <td width="220" align="left"><b>$profile_txt{231}: </b></td>
   <td align="left"><select name="gender" size="1"><option value=""></option><option value="Male"$GenderMale>$profile_txt{'238'}</option><option value="Female"$GenderFemale>$profile_txt{'239'}</option></select></td>
 </tr><tr class="windowbg">
   <td width="220" align="left"><b>$profile_txt{'563'}: </b></td>
   <td align="left"><span class="small">$dayormonth$profile_txt{'566'}<input type="text" name="bday3" size="4" maxlength="4" value="$uyear" /></span></td>
 </tr><tr class="windowbg">
   <td width="220" align="left"><b>$profile_txt{'227'}: </b></td>
   <td align="left"><input type="text" maxlength="30" name="location" size="30" value="${$uid.$user}{'location'}" /></td>
 </tr>
~;
	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$showProfile .= &ext_editprofile($user, "edit");
	}
	if ($sessions == 1 && $sessionvalid == 1 && ($iamadmin || $iamgmod || $iammod) && $username eq $user) {
		&LoadLanguage('Sessions');
		require "$sourcedir/Decoder.pl";
		my $decanswer = &descramble(${$uid.$user}{'sesanswer'}, $user);
		$questsel = qq~<select name="sesquest" size="1">\n~;
		while (($key, $val) = each %sesquest_txt) {
			if (${$uid.$user}{'sesquest'} eq $key && ${$uid.$user}{'sesquest'} ne "") {
				$sessel = qq~ selected="selected"~;
			} elsif ($key eq "password" && ${$uid.$user}{'sesquest'} eq "") {
				$sessel = qq~ selected="selected"~;
			} else {
				$sessel = "";
			}
			$questsel .= qq~<option value="$key"$sessel>$val</option>\n~;
		}
		$questsel .= qq~</select>\n~;
		$showProfile   .= qq~
  <tr>
    <td class="catbg" colspan="2"><img src="$imagesdir/session.gif" alt="" border="0" /> <b>$img_txt{'34a'}</b><br /><span class="small">$session_txt{'9'}<br />$session_txt{'9a'}</span></td>
  </tr><tr class="windowbg">
	   <td width="220" align="left">$questsel</td>
	   <td align="left"><input type="text" maxlength="30" name="sesanswer" size="20" value="$decanswer" /></td>
	 </tr>~;
	}
	$showProfile .= qq~
<tr class="catbg">
   <td height="50" valign="middle" align="center" colspan="2"><input type="submit" name="moda" value="$profile_txt{'88'}" class="button" />~;
	if (($iamadmin && ($username ne $user)) || ($username ne "admin")) {
		$showProfile .= qq~<input type="submit" name="moda" value="$profile_txt{'89'}" onclick="return confirm('$confdel_text')" class="button" />~;
	}
	$showProfile .= qq~<br /><span class="small">$profile_txt{'sid_expires_1'} $sid_expires $profile_txt{'sid_expires_2'}</span>
    </td>
 </tr>
</table>
</form>

<script language="JavaScript1.2" type="text/javascript">
<!--
	// Password_strength_meter start
	var verdects = new Array("$pwstrengthmeter_txt{'1'}","$pwstrengthmeter_txt{'2'}","$pwstrengthmeter_txt{'3'}","$pwstrengthmeter_txt{'4'}","$pwstrengthmeter_txt{'5'}","$pwstrengthmeter_txt{'6'}","$pwstrengthmeter_txt{'7'}","$pwstrengthmeter_txt{'8'}");
	var colors = new Array("#8F8F8F","#BF0000","#FF0000","#00A0FF","#33EE00","#339900");
	var scores = new Array($pwstrengthmeter_scores);
	var common = new Array($pwstrengthmeter_common);
	var minchar = $pwstrengthmeter_minchar;

	function runPassword(D) {
		var nPerc = checkPassword(D);
		if (nPerc > -199 && nPerc < 0) {
			strColor = colors[0];
			strText = verdects[1];
			strWidth = "5%";
		} else if (nPerc == -200) {
			strColor = colors[1];
			strText = verdects[0];
			strWidth = "0%";
		} else if (scores[0] == -1 && scores[1] == -1 && scores[2] == -1 && scores[3] == -1) {
			strColor = colors[4];
			strText = verdects[7];
			strWidth = "100%";
		} else if (nPerc <= scores[0]) {
			strColor = colors[1];
			strText = verdects[2];
			strWidth = "10%";
		} else if (nPerc > scores[0] && nPerc <= scores[1]) {
			strColor = colors[2];
			strText = verdects[3];
			strWidth = "25%";
		} else if (nPerc > scores[1] && nPerc <= scores[2]) {
			strColor = colors[3];
			strText = verdects[4];
			strWidth = "50%";
		} else if (nPerc > scores[2] && nPerc <= scores[3]) {
			strColor = colors[4];
			strText = verdects[5];
			strWidth = "75%";
		} else {
			strColor = colors[5];
			strText = verdects[6];
			strWidth = "100%";
		}
		document.getElementById("passwrd1_bar").style.width = strWidth;
		document.getElementById("passwrd1_bar").style.backgroundColor = strColor;
		document.getElementById("passwrd1_text").style.color = strColor;
		document.getElementById("passwrd1_text").childNodes[0].nodeValue = strText;
	}

	function checkPassword(C) {
		if (C.length == 0 || C.length < minchar) return -100;

		for (var D = 0; D < common.length; D++) {
			if (C.toLowerCase() == common[D]) return -200;
		}

		var F = 0;
		if (C.length >= minchar && C.length <= (minchar+2)) {
			F = (F + 6)
		} else if (C.length >= (minchar + 3) && C.length <= (minchar + 4)) {
			F = (F + 12)
		} else if (C.length >= (minchar + 5)) {
			F = (F + 18)
		}

		if (C.match(/[a-z]/)) {
			F = (F + 1)
		}
		if (C.match(/[A-Z]/)) {
			F = (F + 5)
		}
		if (C.match(/d+/)) {
			F = (F + 5)
		}
		if (C.match(/(.*[0-9].*[0-9].*[0-9])/)) {
			F = (F + 7)
		}
		if (C.match(/.[!,\@,#,\$,\%,^,&,*,?,_,\~]/)) {
			F = (F + 5)
		}
		if (C.match(/(.*[!,\@,#,\$,\%,^,&,*,?,_,\~].*[!,\@,#,\$,\%,^,&,*,?,_,\~])/)) {
			F = (F + 7)
		}
		if (C.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){
			F = (F + 2)
		}
		if (C.match(/([a-zA-Z])/) && C.match(/([0-9])/)) {
			F = (F + 3)
		}
		if (C.match(/([a-zA-Z0-9].*[!,\@,#,\$,\%,^,&,*,?,_,\~])|([!,\@,#,\$,\%,^,&,*,?,_,\~].*[a-zA-Z0-9])/)) {
			F = (F + 3)
		}
		return F;
	}
	// Password_strength_meter end
// -->
</script>
~;

	if (!$view) {
		$yymain .= $showProfile;
		&template;
	}
}

sub ModifyProfileContacts {
	&SidCheck($action);

	&PrepareProfile;
	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }
	$menucolors[1] = "titlebg";
	if (!$view) { &ProfileMenu; }

	my $scriptAction = qq~profileContacts2~;
	if ($view) { 
		$scriptAction = qq~myprofileContacts2~;
		$yytitle = qq~$profile_txt{'editmyprofile'} &rsaquo; $profile_txt{'819'}~;
		$profiletitle = qq~$profile_txt{'editmyprofile'} ($user) &rsaquo; $profile_txt{'819'}~;
		$yynavigation = qq~&rsaquo; <a href="$scripturl?action=mycenter" class="nav">$img_txt{'mycenter'}</a> &rsaquo; $profiletitle~;
	} else {
		$yytitle = qq~$profile_txt{'79'} &rsaquo; $profile_txt{'819'}~;
		$profiletitle = qq~$profile_txt{'79'} ($user) &rsaquo; $profile_txt{'819'}~;
		$yynavigation = qq~&rsaquo; $profiletitle~;
	}

	$showProfile .= qq~
<form action="$scripturl?action=$scriptAction;username=$useraccount{$INFO{'username'}};sid=$INFO{'sid'}" method="post" name="creator">
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" border="0">
  <tr>
    <td colspan="2" class="catbg"><img src="$imagesdir/profile.gif" alt="" border="0" /> <b>$profiletitle</b></td>
  </tr><tr class="windowbg">
    <td width="320" align="left"><b>$profile_txt{'69'}: </b><br /><span class="small">$profile_txt{'679'} </span></td>
    <td align="left"><input type="text" maxlength="60" name="email" size="40" value="${$uid.$user}{'email'}" /></td>
  </tr>~;
	if ($allow_hide_email) {
		my $checked = '';
		if (${$uid.$user}{'hidemail'} eq 'checked') { $checked = ' checked="checked"'; }
		$showProfile .= qq~<tr class="windowbg">
          <td width="320" align="left"><b>$profile_txt{'721'}</b></td>
          <td align="left"><input type="checkbox" name="hideemail" value="checked"$checked /></td>
        </tr>~;
	}
	&ToChars(${$uid.$user}{'webtitle'});
	$showProfile .= qq~<tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'513'}: </b><br /><span class="small">$profile_txt{'600'}</span></td>
     <td align="left"><input type="text" maxlength="10" name="icq" size="40" value="${$uid.$user}{'icq'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'603'}: </b><br /><span class="small">$profile_txt{'601'}</span></td>
     <td align="left"><input type="text" maxlength="30" name="aim" size="40" value="${$uid.$user}{'aim'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'604'}: </b><br /><span class="small">$profile_txt{'602'}</span></td>
     <td align="left"><input type="text" maxlength="30" name="yim" size="40" value="${$uid.$user}{'yim'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'823'}: </b><br /><span class="small">$profile_txt{'824'}</span></td>
     <td align="left"><input type="text" maxlength="50" name="msn" size="40" value="${$uid.$user}{'msn'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'825'}: </b><br /><span class="small">$profile_txt{'826'}</span></td>
     <td align="left"><input type="text" maxlength="50" name="gtalk" size="40" value="${$uid.$user}{'gtalk'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'827'}: </b><br /><span class="small">$profile_txt{'828'}</span></td>
     <td align="left"><input type="text" maxlength="50" name="skype" size="40" value="${$uid.$user}{'skype'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'83'}: </b><br /><span class="small">$profile_txt{'598'}</span></td>
     <td align="left"><input type="text" maxlength="30" name="webtitle" size="40" value="${$uid.$user}{'webtitle'}" /></td>
   </tr><tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'84'}: </b><br /><span class="small">$profile_txt{'599'}</span></td>
     <td align="left"><input type="text" name="weburl" size="40" value="${$uid.$user}{'weburl'}" /></td>
   </tr>
   ~;

	if (($PM_level == 1 || ($PM_level == 2 && ($iamadmin || $iamgmod || $iammod)) || ($PM_level == 3 && ($iamadmin || $iamgmod))) && ($enable_MCaway > 2 || ($enable_MCaway && (${$uid.$user}{'position'} eq 'Administrator' || ${$uid.$user}{'position'} eq 'Global Moderator' || &is_moderator($user))))) {
		my $offChecked = qq~ selected="selected"~;
		my $awayChecked = '';

		if (${$uid.$user}{'offlinestatus'} eq 'away') {
			$offChecked = '';
			$awayChecked = qq~ selected="selected"~;
		}

		$showProfile .= qq~
   <tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'showstatus'}: </b><br /><span class="small">$profile_txt{'statusexplain'}<br />$profile_txt{'awaydescription'}</span></td>
     <td align="left">
		<select name="offlinestatus" id="offlinestatus">
		<option value="offline"$offChecked>$maintxt{'61'}</option>
		<option value="away"$awayChecked>$maintxt{'away'}</option>
		</select><br /><br />
		<span class="small">$profile_txt{'asubj'}</span><br />
		<input type="text" name="awaysubj" size="50" maxlength="50" value="${$uid.$user}{'awaysubj'}" /><br /><br />
		<span class="small">$profile_txt{'amess'}</span><br />
		<textarea name="awayreply" rows="4" cols="50" id="awayreply">${$uid.$user}{'awayreply'}</textarea><br />

		<span class="small">$profile_txt{'664a'} <input value="$MaxAwayLen" size="3" name="msgCL" class="windowbg" style="border: 0px; width: 40px; padding: 1px; font-size: 11px;" readonly="readonly" /></span><br />
		<script type="text/javascript" language="JavaScript">
		<!--
		var supportsKeys = false;
		function tick() {
			calcCharLeft(document.forms[0]);
			if (!supportsKeys) { timerID = setTimeout("tick()",200); }
		}
		function calcCharLeft(sig) {
			clipped = false;
			maxLength = $MaxAwayLen;
			if (document.creator.awayreply.value.length > maxLength) {
				document.creator.awayreply.value = document.creator.awayreply.value.substring(0,maxLength);
				charleft = 0;
				clipped = true;
			} else {
				charleft = maxLength - document.creator.awayreply.value.length;
			}
			document.creator.msgCL.value = charleft;
			return clipped;
		}
		tick();
		//-->
		</script>
     </td>
   </tr>
		~;
	}

	if ((${$uid.$user}{'position'} eq 'Administrator' || ${$uid.$user}{'position'} eq 'Global Moderator') && $enable_MCstatusStealth) {
		my $stealthChecked = '';
		if (${$uid.$user}{'stealth'}) { $stealthChecked = ' checked="checked"'; }
		$showProfile .= qq~
	<tr class="windowbg">
		<td width="320" align="left"><b>$profile_txt{'stealth'}: </b><br /><span class="small">$profile_txt{'stealthexplain'}</span></td>
		<td align="left"><input type="checkbox" name="stealth" value="1"$stealthChecked /></td>
	</tr>
		~;
	}

	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$showProfile .= &ext_editprofile($user,"contact");
	}
	$showProfile .= qq~
      <tr class="catbg">
     <td height="50" valign="middle" align="center" colspan="2"><input type="submit" name="moda" value="$profile_txt{'88'}" class="button" /><br /><span class="small">$profile_txt{'sid_expires_1'} $sid_expires $profile_txt{'sid_expires_2'}</span></td>
   </tr>
</table>
</form>
	~;

	if (!$view) { 
		$yymain .= $showProfile;
		&template;
	}
}

sub ModifyProfileOptions {
	&SidCheck($action);

	&PrepareProfile;
	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }
	$menucolors[2] = "titlebg";
	if (!$view) { &ProfileMenu; }
	&ToChars(${$uid.$user}{'usertext'});

	my $scriptAction = qq~profileOptions2~;
	if ($view) { 
		$scriptAction = qq~myprofileOptions2~;
		$yytitle = qq~$profile_txt{'editmyprofile'} &rsaquo; $profile_txt{'818'}~;
		$profiletitle = qq~$profile_txt{'editmyprofile'} ($user) &rsaquo; $profile_txt{'818'}~;
		$yynavigation = qq~&rsaquo; <a href="$scripturl?action=mycenter" class="nav">$img_txt{'mycenter'}</a> &rsaquo; $profiletitle~;
	} else {
		$yytitle = qq~$profile_txt{'79'} &rsaquo; $profile_txt{'818'}~;
		$profiletitle = qq~$profile_txt{'79'} ($user) &rsaquo; $profile_txt{'818'}~;
		$yynavigation = qq~&rsaquo; $profiletitle~;
	}

	if ($allowpics && $upload_useravatar && $upload_avatargroup) {
		$upload_useravatar = 0;
		foreach my $av_gr (split(/\, /, $upload_avatargroup)) {
			if ($av_gr eq ${$uid.$user}{'position'}) { $upload_useravatar = 1; last; }
			foreach (split(/\, /, ${$uid.$user}{'addgroups'})) {
				if ($av_gr eq $_) { $upload_useravatar = 1; last; }
			}
		}
	}

	$showProfile .= qq~
<form action="$scripturl?action=$scriptAction;username=$useraccount{$INFO{'username'}};sid=$INFO{'sid'}" method="post" name="creator"~ . (($allowpics && $upload_useravatar) ? qq~ enctype="multipart/form-data"~ : "") . qq~>
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" border="0">
  <tr>
    <td colspan="2" class="catbg"><img src="$imagesdir/profile.gif" alt="" border="0" /> <b>$profiletitle</b></td>
  </tr><tr class="windowbg">~;

	if ($allowpics) {
		opendir(DIR, "$facesdir") || fatal_error("cannot_open_dir","($facesdir)!<br />$profile_txt{'681'}", 1);
		@contents = readdir(DIR);
		closedir(DIR);
		$images = '';
		foreach $line (sort @contents) {
			($name, $extension) = split(/\./, $line);
			$checked = '';
			if ($line eq ${$uid.$user}{'userpic'}) { $checked = ' selected="selected"'; }
			if (${$uid.$user}{'userpic'} =~ m~\Ahttps?://~ && $line eq 'blank.gif') { $checked = ' selected="selected" '; }
			if ($extension =~ /gif/i || $extension =~ /jpg/i || $extension =~ /jpeg/i || $extension =~ /png/i) {
				if ($line eq 'blank.gif') {
					$images = qq~              <option value="$line"$checked>$profile_txt{'422'}</option>\n$images~;
				} else {
					$images .= qq~              <option value="$line"$checked>$name</option>\n~;
				}
			}
		}
		my ($pic,$tmp,$s,$alt);
		$tmp = $facesurl;
		$tmp =~ /^(http(s?):\/\/)/;
		($tmp,$s) = ($1,$2);
		if (${$uid.$user}{'userpic'} =~ m~\Ahttps?://~) {
			$pic = ${$uid.$user}{'userpic'};
			$checked = ' checked="checked" ';
			$tmp = ${$uid.$user}{'userpic'};
			$alt = $profile_txt{'473'} if $upload_useravatar;
		} else {
			$pic = "$facesurl/${$uid.$user}{'userpic'}";
		}

		$showProfile .= qq~
	<td align="left"><b>$profile_txt{'229'}:</b><br /><span class="small">$profile_txt{'474'}~ . ($upload_useravatar ? qq~<br />
		$profile_txt{'475'} $avatar_limit KB~ : "") . qq~<br />
		$profile_txt{'476'}</span></td>
	<td align="left">
		<script language="JavaScript1.2" type="text/javascript">
			function showimage(x) {
				if (!document.images) return;
				var source;
				if (x == 1 && document.getElementsByName('userpicpersonalcheck')[0].checked == true) {
					UserPicUrl = document.getElementsByName('userpicpersonal')[0].value;
					document.getElementsByName('userpicpersonal')[0].value = 'http$s://';
					document.getElementsByName('userpicpersonalcheck')[0].checked = false;
				}
				if (x == 1) {
					source = "$facesurl/"+document.creator.userpic.options[document.creator.userpic.selectedIndex].value;
				} else {
					document.creator.userpic.options[0].selected = true;
					source = document.getElementsByName('userpicpersonal')[0].value;
					if (!source || source == 'http$s://') source = "$facesurl/blank.gif";
				}
				document.images.icons.style.display = 'none';
				document.images.icons.width = '';
				document.images.icons.height = '';
				document.images.icons.src = source;
				resize_time = 2;
				img_resize_names = new Array ('avatar_img_resize_1');
				resize_images();
			}
		</script>
		<select name="userpic" size="6" onchange="showimage(1);">
		$images
		</select>&nbsp;&nbsp;<img src="$pic" id="icons" name="avatar_img_resize" alt="$alt" border="0" hspace="15" style="display:none" /><br />
		<br />
		<input type="checkbox" name="userpicpersonalcheck"$checked onclick="if(this.checked==false){UserPicUrl=document.getElementsByName('userpicpersonal')[0].value;document.getElementsByName('userpicpersonal')[0].value='http$s://';}else{document.getElementsByName('userpicpersonal')[0].value=UserPicUrl;}showimage(2);" />&nbsp;<input type="text" name="userpicpersonal" size="40" value="$tmp" onkeyup="document.getElementsByName('userpicpersonalcheck')[0].checked=true;showimage(2);" />~ . ($upload_useravatar ? qq~<br />
		<br />
		<input type="file" name="file_avatar" size="50" />~ : "") . qq~
	</td>
  </tr>~;
	}

	$showProfile .= qq~<tr class="windowbg">
          <td align="left"><b>$profile_txt{'228'}: </b></td>
          <td align="left"><input type="text" name="usertext" size="40" value="${$uid.$user}{'usertext'}" maxlength="50" /></td>
        </tr><tr class="windowbg">
          <td align="left"><b>$profile_txt{'85'}:</b><br /><span class="small">$profile_txt{'606'}</span></td>
          <td align="left"><textarea name="signature" rows="4" cols="30" style="width: 100%">$signature</textarea><br />
            <span class="small">$profile_txt{'664'} <input value="$MaxSigLen" size="3" name="msgCL" class="windowbg" style="border: 0px; width: 40px; padding: 1px; font-size: 11px;" readonly="readonly" /></span><br /><br />
            <script type="text/javascript" language="JavaScript">
            <!--
            var supportsKeys = false;
            function tick() {
              calcCharLeft(document.forms[0]);
              if (!supportsKeys) { timerID = setTimeout("tick()", 1500); }
            }

            function calcCharLeft(sig) {
              clipped = false;
              maxLength = $MaxSigLen;
              if (document.creator.signature.value.length > maxLength) {
                document.creator.signature.value = document.creator.signature.value.substring(0,maxLength);
                charleft = 0;
                clipped = true;
              } else {
                charleft = maxLength - document.creator.signature.value.length;
              }
              document.creator.msgCL.value = charleft;
              return clipped;
            }
            tick();
            //-->
            </script>
          </td>
        </tr>~;

	if ($addmemgroup_enabled == 2 || $addmemgroup_enabled == 3) {
		foreach (@nopostorder) {
			chomp $_;
			($title, undef, undef, undef, undef, undef, undef, undef, undef, undef, $additional) = split(/\|/, $NoPost{$_});
			if ($additional == 1) {
				my $admg;
				if (${$uid.$user}{'addgroups'} == $_) { $admg = qq~ selected="selected"~; }
				$addmemgroup .= qq~<option value="$_"$admg>$title</option>~;
			}
		}
		$showProfile .= qq~
		<tr class="windowbg">
			<td align="left"><b>$profile_txt{'910'}:</b></td>
			<td align="left">
				<select name="joinmemgroup">
				<option value=""></option>
				$addmemgroup
				</select>
			</td>
		</tr>~;
	}

	my @usertimeoffset = split(/\./, ${$uid.$user}{'timeoffset'});
	$showProfile .= qq~
        <tr class="windowbg">
          <td align="left"><b>$profile_txt{'486'}:</b><br />
            <span class="small">$profile_txt{'479'}</span></td>
          <td align="left">
            <select name="usertimeselect" size="1">
              <option value="1"$tsl1>$profile_txt{'480'}</option>
              <option value="5"$tsl5>$profile_txt{'484'}</option>
              <option value="4"$tsl4>$profile_txt{'483'}</option>
              <option value="2"$tsl2>$profile_txt{'481'}</option>
              <option value="3"$tsl3>$profile_txt{'482'}</option>
              <option value="6"$tsl6>$profile_txt{'485'}</option>
              <option value="7"$tsl7>$profile_txt{'480a'}</option>
            </select>
          </td>
        </tr>
        <tr class="windowbg">
          <td align="left"><b>$profile_txt{'486a'}:</b></td>
           <td align="left"><input type="text" name="timeformat" size="40" value="${$uid.$user}{'timeformat'}" /></td>
        </tr><tr class="windowbg">
          <td align="left"><b>$profile_txt{'371'}:</b><br /><span class="small">$profile_txt{'372'}</span></td>
          <td align="left"><span class="small">$profile_txt{'373'}:<br /><b>~ . &timeformat($date,1) . qq~<br /><br /></span><select name="usertimesign"><option value="">+</option><option value="-"~ . ($usertimeoffset[0] < 0 ? ' selected="selected"' : '') . qq~>-</option></select>
            <select name="usertimehour">~;
	for (my $i = 0; 15 > $i; $i++) {
		$i = sprintf("%02d", $i);
		$showProfile .= qq~<option value="$i"~ . (($usertimeoffset[0] == $i || $usertimeoffset[0] == -$i) ? ' selected="selected"' : '') . qq~>$i</option>~;
	}
	$showProfile .= qq~</select> : <select name="usertimemin">~;
	for (my $i = 0; 60 > $i; $i++) {
		my $j = $i / 60;
		$j = (split(/\./, $j))[1] || 0;
		$showProfile .= qq~<option value="$j"~ . ($usertimeoffset[1] eq $j ? ' selected="selected"' : '') . qq~>~ . sprintf("%02d", $i) . qq~</option>~;
	}
	$showProfile .= qq~</select>
          </td>
        </tr><tr class="windowbg">
          <td align="left"><b>$profile_txt{'519'}</b></td>
          <td align="left"><input type="checkbox" name="dsttimeoffset" value="1"~ . (${$uid.$user}{'dsttimeoffset'} ? ' checked="checked"' : '') . qq~ /></td>
        </tr>
	~;

	# This is only for update, when comming from YaBB lower or equal version 2.2.3
	# I think it can be deleted around version 2.4.0 without causing mayor issues (deti).
	if ($enable_notifications eq '') { $enable_notifications = $enable_notification ? 3 : 0; }
	# End update workaround
	if ($NewNotificationAlert || $enable_notifications == 1 || $enable_notifications == 3) {
		$showProfile .= qq~
        <tr class="windowbg">
          <td align="left" valign="top"><b>$profile_txt{'onlinealert'}:</b></td>
          <td align="left">~;
		$showProfile .= qq~
            <input type="checkbox" value="1" name="onlinealert" id="onlinealert"~ . (${$uid.$user}{'onlinealert'} ? ' checked="checked"' : '') . qq~ />
            $profile_txt{'onlinealertexplain'}~ if $NewNotificationAlert;

		if ($enable_notifications == 1 || $enable_notifications == 3) {
			$showProfile .= qq~<br />
            <br />~ if $NewNotificationAlert;
			$showProfile .= qq~
            $profile_txt{'326'}?&nbsp;<select name="notify_N">
              <option value="0"~ . ((!${$uid.$user}{'notify_me'} || ${$uid.$user}{'notify_me'} == 2) ? '' : ' selected="selected"') . qq~>$profile_txt{'164'}</option>
              <option value="1"~ . ((${$uid.$user}{'notify_me'} == 1 || ${$uid.$user}{'notify_me'} == 3) ? ' selected="selected"' : '') . qq~>$profile_txt{'163'}</option>
            </select>~;
		}

		$showProfile .= qq~
          </td>
        </tr>~;
	}

	if ($ttsureverse) {
		${$uid.$user}{'reversetopic'} = $ttsreverse unless exists(${$uid.$user}{'reversetopic'});
		$showProfile .= qq~
        <tr class="windowbg">
          <td align="left"><b>$profile_txt{'810'}</b><br /><span class="small">$profile_txt{'811'}</span></td>
          <td align="left" valign="top"><input type="checkbox" name="reversetopic"~ . (${$uid.$user}{'reversetopic'} ? qq~ checked="checked"~ : '') . qq~ /></td>
        </tr>~;
	}

	&CheckNewTemplates;
	unless ($templatesloaded == 1) { require "$vardir/template.cfg"; }
	my @templateset = sort{ $templateset{$a} cmp $templateset{$b} } keys %templateset;
	foreach my $curtemplate (@templateset) {
		$selected = '';
		if ($curtemplate eq ${$uid.$user}{'template'}) { $selected = qq~ selected="selected"~; $akttemplate = $curtemplate; }
		$drawndirs .= qq~<option value="$curtemplate"$selected>$curtemplate</option>\n~;
	}

	$showProfile .= qq~
        <tr class="windowbg">
          <td align="left"><b>$profile_txt{'814'}</b><br /><span class="small">$profile_txt{'815'}</span></td>
          <td align="left"><select name="usertemplate">$drawndirs</select></td>
        </tr>~;

	opendir(DIR, $langdir);
	my @lfilesanddirs = readdir(DIR);
	close(DIR);
	foreach my $fld (sort {lc($a) cmp lc($b)} @lfilesanddirs) {
		if (-e "$langdir/$fld/Main.lng") {
			if (${$uid.$user}{'language'} eq $fld) { $drawnldirs .= qq~<option value="$fld" selected="selected">$fld</option>\n~; }
			else { $drawnldirs .= qq~<option value="$fld">$fld</option>\n~; }
		}
	}

	$showProfile .= qq~<tr class="windowbg">
          <td align="left"><b>$profile_txt{'817'}</b><br /><span class="small">$profile_txt{'815'}</span></td>
          <td align="left"><select name="userlanguage">$drawnldirs</select></td>
        </tr>
        ~;
	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$showProfile .= &ext_editprofile($user,"options");
	}
	$showProfile .= qq~<tr class="catbg">
          <td height="50" valign="middle" align="center" colspan="2"><input type="submit" name="moda" value="$profile_txt{'88'}" class="button" /><br /><span class="small">$profile_txt{'sid_expires_1'} $sid_expires $profile_txt{'sid_expires_2'}</span></td>
        </tr>
      </table>
</form>
	~;

	if (!$view) {
		$yymain .= $showProfile;
		&template;
	}
}

sub ModifyProfileBuddy {
	&SidCheck($action);

	&PrepareProfile;
	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }
	$menucolors[3] = "titlebg";
	if (!$view) { &ProfileMenu; }

	my $scriptAction = qq~profileBuddy2~;
	if ($view) {
		$scriptAction = qq~myprofileBuddy2~;
		$yytitle = qq~$profile_txt{'editmyprofile'} &rsaquo; $profile_buddy_list{'buddylist'}~;
		$profiletitle = qq~$profile_txt{'editmyprofile'} ($user) &rsaquo; $profile_buddy_list{'buddylist'}~;
		$yynavigation = qq~&rsaquo; <a href="$scripturl?action=mycenter" class="nav">$img_txt{'mycenter'}</a> &rsaquo; $profiletitle~;
	} else {
		$yytitle = qq~$profile_txt{'79'} &rsaquo; $profile_buddy_list{'buddylist'}~;
		$profiletitle = qq~$profile_txt{'79'} ($user) &rsaquo; $profile_buddy_list{'buddylist'}~;
		$yynavigation = qq~&rsaquo; $profiletitle~;
	}

	if (!$yyjavascript) { $yyjavascript = ''; }
	$yyjavascript .= qq~
	function imWin() {
		window.open('$scripturl?action=imlist;sort=mlletter;toid=buddylist','Blist','status=no,height=345,width=464,menubar=no,toolbar=no,top=50,left=50,scrollbars=no');
	}
	// removes a user from the list
	function removeUser(oElement) {
		var oList = oElement.options;
		var indexToRemove = oList.selectedIndex;
		if (oList.length > 1 || (oList.length == 1 && oList[0].value != '0')) {
			//alert('element [' + oElement.options[indexToRemove].value + ']');
			if (confirm("$profile_buddy_list{'removealert'}")) {
				oElement.remove(indexToRemove);
			}
		}
	}
	function selectblNames() {
		var oList = document.getElementById('buddylist');
		for (var i = 0; i < oList.options.length; i++) {
			oList.options[i].selected = true;
		}
	}
	~;

	$showProfile .= qq~
<form action="$scripturl?action=$scriptAction;username=$useraccount{$INFO{'username'}};sid=$INFO{'sid'}" method="post" name="creator" onsubmit="javascript: selectblNames();">
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" border="0">
  <tr>
    <td colspan="2" class="catbg"><img src="$imagesdir/profile.gif" alt="" border="0" /> <b>$profiletitle</b></td>
  </tr>~;

	my $buildBuddyList = '';
	if(${$uid.$user}{'buddylist'}) {
		my @buddies = split(/\|/, ${$uid.$user}{'buddylist'});
		chomp @buddies;
		foreach my $buddy (@buddies) {
			&LoadUser($buddy);
			$buildBuddyList .= qq~<option value="$buddy">${$uid.$buddy}{'realname'}</option>\n~ if ${$uid.$buddy}{'realname'};
		}
	}

	$showProfile .= qq~
	<tr class="windowbg">
		<td width="320" align="left"><b>$profile_buddy_list{'buddylist'}</b><br /><span class="small">$profile_buddy_list{'explain'}</span></td>
		<td align="left"><select name="buddylist" id="buddylist" multiple="multiple" size="3" style="width: 250px; height: 150px;" ondblclick="removeUser(this);">$buildBuddyList</select>
		<br /><span class="small"><a href="javascript: void(0);" onclick="imWin();">$profile_buddy_list{'add'}</a></span>
		</td>
	</tr>
	~;

	$showProfile .= qq~<tr class="catbg">
			<td height="50" valign="middle" align="center" colspan="2"><input type="submit" name="moda" value="$profile_txt{'88'}" class="button" /><br /><span class="small">$profile_txt{'sid_expires_1'} $sid_expires $profile_txt{'sid_expires_2'}</span></td>
		</tr>
	</table>
</form>
	~;

	if (!$view) {
		$yymain .= $showProfile;
		&template;
	}
}

sub ModifyProfileIM {
	&SidCheck($action);

	if (!$yyjavascript) { $yyjavascript = ''; }
	$yyjavascript .= qq~
	function imWin() {
		window.open('$scripturl?action=imlist;sort=mlletter;toid=ignore','Ilist','status=no,height=345,width=464,menubar=no,toolbar=no,top=50,left=50,scrollbars=no');
	}
	// removes a user from the list
	function removeUser(oElement) {
		var oList = oElement.options;
		var indexToRemove = oList.selectedIndex;
		if (oList.length > 1 || (oList.length == 1 && oList[0].value != '0')) {
			//alert('element [' + oElement.options[indexToRemove].value + ']');
			if (confirm("$profile_buddy_list{'removealert'}")) {
				oElement.remove(indexToRemove);
			}
		}
	}
	function selectINames()	{
		var oList = document.getElementById('ignore');
		for (var i = 0; i < oList.options.length; i++) {
			oList.options[i].selected = true;
			}
		}
	~;
	&PrepareProfile;
	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }
	$menucolors[4] = "titlebg";
	if (!$view) { &ProfileMenu; }

	my $scriptAction = qq~profileIM2~;
	if ($view) { 
		$scriptAction = qq~myprofileIM2~;
		$yytitle = qq~$profile_txt{'editmyprofile'} &rsaquo; $profile_imtxt{'38'}~;
		$profiletitle = qq~$profile_txt{'editmyprofile'} ($user) &rsaquo; $profile_imtxt{'38'}~;
		$yynavigation = qq~&rsaquo; <a href="$scripturl?action=mycenter" class="nav">$img_txt{'mycenter'}</a> &rsaquo; $profiletitle~;
	} else {
		$yytitle = qq~$profile_txt{79} &rsaquo; $profile_imtxt{'38'}~;
		$profiletitle = qq~$profile_txt{79} ($user) &rsaquo; $profile_imtxt{'38'}~;
		$yynavigation = qq~&rsaquo; $profiletitle~;
	}

	$showProfile .= qq~
<form action="$scripturl?action=$scriptAction;username=$useraccount{$INFO{'username'}};sid=$INFO{'sid'}" method="post" name="creator" onsubmit="javascript:selectINames();" >
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" border="0">
 <tr>
   <td colspan="2" class="catbg" align="left"><img src="$imagesdir/profile.gif" alt="" border="0" /> <b>$profiletitle</b></td>
 </tr><tr class="windowbg">
   <td width="320" valign="top" align="left"><b>$profile_txt{'325'}:</b><br /><span class="small">$profile_txt{'ignoreexplain'}</span></td>
   <td align="left">
   <select name="ignore" id="ignore" size="4" multiple="multiple" style="width:250px;" ondblclick="removeUser(this);" >
	~;
	my $ignoreallChecked = "";
	if (${$uid.$user}{'im_ignorelist'} eq "*") { $ignoreallChecked = ' checked="checked"'; }
	if (${$uid.$user}{'im_ignorelist'} && ${$uid.$user}{'im_ignorelist'} ne "*") {
		my @ignoreList = split('\|', ${$uid.$user}{'im_ignorelist'});
		chomp @ignoreList;
		foreach my $ignoreName (@ignoreList) {
			&LoadUser($ignoreName);
			my $ignoreUser;
			if(${$uid.$ignoreName}{'realname'}) {$ignoreUser = ${$uid.$ignoreName}{'realname'};}
			else {$ignoreUser = $ignoreName;}
			$ignoreName = &cloak($ignoreName);
			$showProfile .= qq~<option value="$ignoreName">$ignoreUser</option>~;
		}
	}
	$showProfile .= qq~</select>
	<br />
	<input type="checkbox" name="ignoreall" id="ignoreall"$ignoreallChecked /> $profile_txt{'ignoreall'}<br />
	<span class="small"><a href="javascript:void(0);" onclick="imWin();">$profile_txt{'ignorelistadd'}</a></span>
   </td>
 </tr>~;

	# This is only for update, when comming from YaBB lower or equal version 2.2.3
	# I think it can be deleted around version 2.4.0 without causing mayor issues (deti).
	if ($enable_notifications eq '') { $enable_notifications = $enable_notification ? 3 : 0; }
	# End update workaround

	if ($enable_notifications > 1) {
		$showProfile .= qq~
 <tr class="windowbg">
   <td align="left" valign="top"><b>$profile_txt{'327'}:</b></td>
   <td align="left">
     <select name="notify_PM">
       <option value="0"~ . (${$uid.$user}{'notify_me'} < 2 ? '' : ' selected="selected"') . qq~>$profile_txt{'164'}</option>
       <option value="1"~ . (${$uid.$user}{'notify_me'} > 1 ? ' selected="selected"' : '') . qq~>$profile_txt{'163'}</option>
     </select>
   </td>
 </tr>~;
	}

	chomp(${$uid.$user}{'im_popup'});
	chomp(${$uid.$user}{'im_imspop'});
	if (${$uid.$user}{'im_popup'}  eq "on") { $enable_userimpopup = ' checked="checked"'; }
	if (${$uid.$user}{'im_imspop'} eq "on") { $popup_userim = 'checked="checked"'; }
	$showProfile .= qq~
<tr class="windowbg">
    <td width="320" align="left"><b>$profile_imtxt{'05'}</b></td>
    <td align="left"><input type="checkbox" name="userpopup"$enable_userimpopup /></td>
  </tr><tr class="windowbg">
    <td width="320" align="left"><b>$profile_imtxt{'53'}</b></td>
    <td align="left"><input type="checkbox" name="popupims"$popup_userim /></td>
  </tr>~;
	if($enable_PMcontrols || $enable_PMprev) {
		my $pmmessprevChecked = "";
		if(${$uid.$user}{'pmmessprev'} eq "on") { $pmmessprevChecked = ' checked="checked"'; }
		$showProfile .= qq~
   <tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'enabprev'}: </b><br /><span class="small">$profile_txt{'prevexplain'}</span></td>
     <td align="left"><input type="checkbox" name="pmmessprev" id="pmmessprev"$pmmessprevChecked /></td>
   </tr>
		~;
	}
	if($enable_PMcontrols || $enable_PMviewMess) {
	my $pmviewMessChecked = "";
		if (${$uid.$user}{'pmviewMess'} eq "on") { $pmviewMessChecked = ' checked="checked"'; }
		$showProfile .= qq~
   <tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'viewmess'}: </b><br /><span class="small">$profile_txt{'viewmessexplain'}</span></td>
     <td align="left"><input type="checkbox" name="pmviewMess" id="pmviewMess"$pmviewMessChecked /></td>
   </tr>
		~;
	}
	if ($enable_PMcontrols || $enable_PMActprev) {
		my $pmactprevChecked = "";
		if (${$uid.$user}{'pmactprev'} eq "on") { $pmactprevChecked = ' checked="checked"'; }
		$showProfile .= qq~
   <tr class="windowbg">
     <td width="320" align="left"><b>$profile_txt{'actprev'}: </b><br /><span class="small">$profile_txt{'actprevexplain'}</span></td>
     <td align="left"><input type="checkbox" name="pmactprev" id="pmactprev"$pmactprevChecked /></td>
   </tr>
		~;
	}
	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$showProfile .= &ext_editprofile($user,"im");
	}
	$showProfile .= qq~
  <tr class="catbg">
    <td height="50" valign="middle" align="center" colspan="2"><input type="submit" name="moda" value="$profile_txt{'88'}" class="button" /><br /><span class="small">$profile_txt{'sid_expires_1'} $sid_expires $profile_txt{'sid_expires_2'}</span></td>
 </tr>
</table>
</form>
	~;

	if (!$view) {
		$yymain .= $showProfile;
		&template;
	}
}

sub ModifyProfileAdmin {
	&SidCheck($action);

	&is_admin_or_gmod;
	&PrepareProfile;

	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	$menucolors[5] = "titlebg";
	if (!$view) { &ProfileMenu; }

	($MemStatAdmin, $MemStarNumAdmin, $MemStarPicAdmin, $MemTypeColAdmin) = split(/\|/, $Group{"Administrator"});
	($MemStatGMod,  $MemStarNumGMod,  $MemStarPicGMod,  $MemTypeColGMod)  = split(/\|/, $Group{"Global Moderator"});
	($MemStatMod,   $MemStarNumMod,   $MemStarPicMod,   $MemTypeColMod)   = split(/\|/, $Group{"Moderator"});

	if    (${$uid.$user}{'position'} eq 'Administrator') { $tt = $MemStatAdmin; }
	elsif (${$uid.$user}{'position'} eq 'Global Moderator') { $tt = $MemStatGMod; }
	elsif (${$uid.$user}{'position'}) { $ttgrp = ${$uid.$user}{'position'}; ($tt, undef) = split(/\|/, $NoPost{$ttgrp}, 2); }
	else { $tt = ${$uid.$user}{'position'}; }

	$regreason = ${$uid.$user}{'regreason'};
	&FromHTML($regreason);
	$regreason =~ s~<br \/>~\n~g;

	$tta = "";
	if (%NoPost) {
		$tta = &DrawGroups(${$uid.$user}{'addgroups'}, ${$uid.$user}{'position'});
		$selsize = $k;
		if ($selsize > 6) { $selsize = 6; }
	}

	$userlastlogin = &timeformat(${$uid.$user}{'lastonline'});
	$userlastpost  = &timeformat(${$uid.$user}{'lastpost'});
	$userlastim    = &timeformat(${$uid.$user}{'lastim'});
	if ($userlastlogin eq '') { $userlastlogin = $profile_txt{'470'}; }
	if ($userlastpost eq '') { $userlastpost = $profile_txt{'470'}; }
	if ($userlastim eq '') { $userlastim = $profile_txt{'470'}; }
	if (${$uid.$user}{'postcount'} > 100000) { ${$uid.$user}{'postcount'} = $profile_txt{'683'}; }

	my $scriptAction = qq~profileAdmin2~;
	if ($view) { 
		$scriptAction = qq~myprofileAdmin2~;
		$yytitle = qq~$profile_txt{'editmyprofile'} &rsaquo; $profile_txt{'820'}~;
		$profiletitle = qq~$profile_txt{'editmyprofile'} ($user) &rsaquo; $profile_txt{'820'}~;
		$yynavigation = qq~&rsaquo; <a href="$scripturl?action=mycenter" class="nav">$img_txt{'mycenter'}</a> &rsaquo; $profiletitle~;
	} else {
		$yytitle = qq~$profile_txt{'79'} &rsaquo; $profile_txt{'820'}~;
		$profiletitle = qq~$profile_txt{'79'} ($user) &rsaquo; $profile_txt{'820'}~;
		$yynavigation = qq~&rsaquo; $profiletitle~;
	}

	$showProfile .= qq~
<form action="$scripturl?action=$scriptAction;username=$useraccount{$user};sid=$INFO{'sid'}" method="post" name="creator">
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" border="0">
 <tr>
   <td colspan="2" class="catbg"><img src="$imagesdir/profile.gif" alt="" border="0" /> <b>$profiletitle</b><input type="hidden" name="username" value="$INFO{'username'}" /></td>
 </tr><tr class="windowbg">
   <td width="320" align="left"><b>$profile_txt{'21'}: </b></td>
   <td align="left"><input type="text" name="settings6" size="4" value="${$uid.$user}{'postcount'}" /></td>
 </tr><tr class="windowbg">
   <td width="320" align="left"><b>$profile_txt{'87'}: </b><br /><span class="small">$profile_txt{'87c'}</span></td>
   <td align="left">
           <select name="settings7">
           <option value="${$uid.$user}{'position'}">$tt</option>
           <option value="${$uid.$user}{'position'}">---------------</option>
           <option value=""></option>~;
	$z = 0;

	unless ($iamgmod) {
		($title, $stars, $starpic, $color) = split(/\|/, $Group{"Administrator"});
		$showProfile .= qq~
           <option value="Administrator">$title</option>~;
		($title, $stars, $starpic, $color) = split(/\|/, $Group{"Global Moderator"});
		$showProfile .= qq~
           <option value="Global Moderator">$title</option>~;
	}

	foreach (@nopostorder) {
		chomp $_;
		($title, $stars, $starpic, $color, undef) = split(/\|/, $NoPost{$_}, 5);
		$showProfile .= qq~<option value="$_">$title</option>~;
		$z++;
	}
	$showProfile .= qq~
           </select>
   </td>
 </tr>
~;
	if ($tta ne "") {
		$showProfile .= qq~
 <tr class="windowbg">
   <td width="320" align="left" valign="top"><b>$profile_txt{'87a'}: </b><br /><span class="small">$profile_txt{'87b'}</span></td>
   <td align="left">
           <select name="addgroup" size="$selsize" multiple="multiple">
           $tta
 </td>
 </tr>
		~;
	}

	if($dr_month > 12) { $dr_month = 12; } ## month cannot be above 12!
	if($dr_month < 1) { $dr_month = 1; } ## neither can it be less than 1
	if($dr_day > 31) { $dr_day = 31; }  ## day of month over 31
	if($dr_day < 1) { $dr_day = 1; }
	if(length($dr_year) > 2) { $dr_year = substr($dr_year , length($dr_year) - 2, 2); }
	if($dr_year < 90 && $dr_year > 50) { $dr_year = 90; } ## a year over 50 is taken to be 1990
	if($dr_year > 20 && $dr_year < 51) { $dr_year = 20; } ## a year 50 or lower is taken to be 2020 
	if($dr_hour > 23) { $dr_hour = 23; }
	if($dr_minute > 59) { $dr_minute = 59; }
	if($dr_secund > 59) { $dr_secund = 59; }

	$sel_day = qq~
	<select name="dr_day">\n~;
	for($i = 1; $i <= 31; $i++) {
		$day_val = sprintf("%02d", $i);
		if($dr_day == $i) {
			$sel_day .= qq~<option value="$day_val" selected="selected">$i</option>\n~;
		} else {
			$sel_day .= qq~<option value="$day_val">$i</option>\n~;
		}
	}
	$sel_day .= qq~</select>\n~;

	$sel_month = qq~
	<select name="dr_month">\n~;
	for($i = 0; $i < 12; $i++) {
		$z = $i+1;
		$month_val = sprintf("%02d", $z);
		if($dr_month == $z) {
			$sel_month .= qq~<option value="$month_val" selected="selected">$months[$i]</option>\n~;
		} else {
			$sel_month .= qq~<option value="$month_val">$months[$i]</option>\n~;
		}
	}
	$sel_month .= qq~</select>\n~;

	$sel_year = qq~
	<select name="dr_year">\n~;
	for($i = 90; $i <= 120; $i++) {
		if($i < 100) { $z = $i; $year_pre = qq~19~; } else { $z = $i-100; $year_pre = qq~20~; }
		$year_val = sprintf("%02d", $z);
		$year_opt = qq~$year_pre$year_val~;
		if($dr_year == $z) {
			$sel_year .= qq~<option value="$year_val" selected="selected">$year_opt</option>\n~;
		} else {
			$sel_year .= qq~<option value="$year_val">$year_opt</option>\n~;
		}
	}
	$sel_year .= qq~</select>\n~;

	$time_sel = ${$uid.$username}{'timeselect'};
	if($time_sel == 1 || $time_sel == 4 || $time_sel == 5) { $all_date = qq~$sel_month $sel_day $sel_year~; }
	else { $all_date = qq~$sel_day $sel_month $sel_year~; }

	$sel_hour = qq~
	<select name="dr_hour">\n~;
	for($i = 0; $i <= 23; $i++) {
		my $hour_val = sprintf("%02d", $i);
		if($dr_hour == $i) {
			$sel_hour .= qq~<option value="$hour_val" selected="selected">$hour_val</option>\n~;
		} else {
			$sel_hour .= qq~<option value="$hour_val">$hour_val</option>\n~;
		}
	}
	$sel_hour .= qq~</select>\n~;

	$sel_minute = qq~
	<select name="dr_minute">\n~;
	for($i = 0; $i <= 59; $i++) {
		$minute_val = sprintf("%02d", $i);
		if($dr_minute == $i) {
			$sel_minute .= qq~<option value="$minute_val" selected="selected">$minute_val</option>\n~;
		} else {
			$sel_minute .= qq~<option value="$minute_val">$minute_val</option>\n~;
		}
	}
	$sel_minute .= qq~</select>\n~;

	$sel_secund = qq~<input type="hidden" value="$dr_secund" name="dr_secund" />~;

	$all_time = qq~$sel_hour $sel_minute $sel_secund~;
	$showProfile .= qq~
 <tr class="windowbg">
    <td width="320" align="left"><b>$profile_txt{'233'}:</b></td>
    <td align="left" valign="middle">$all_date $maintxt{'107'} $all_time <small>(server <b>localtime</b>)</small></td>
 </tr> <tr class="windowbg">
    <td width="320" align="left"><b>$profile_txt{'234'}:</b></td>
    <td align="left" valign="middle"><textarea rows="4" cols="50" id="regreason" name="regreason">$regreason</textarea></td>
 </tr><tr class="windowbg">
    <td width="320" align="left"><b>$profile_amv_txt{'9'}: </b></td>
    <td align="left">$userlastlogin</td>
 </tr><tr class="windowbg">
    <td width="320" align="left"><b>$profile_amv_txt{'10'}: </b></td>
    <td align="left">$userlastpost</td>
 </tr><tr class="windowbg">
    <td width="320" align="left"><b>$profile_amv_txt{'11'}: </b><br /><br /></td>
    <td align="left">$userlastim<br /><br /></td>
 </tr>~;
 
	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$showProfile .= &ext_editprofile($user,"admin");
	}
	$showProfile .= qq~
 <tr class="catbg">
    <td height="50" valign="middle" align="center" colspan="2"><input type="submit" name="moda" value="$profile_txt{'88'}" class="button" /><br /><span class="small">$profile_txt{'sid_expires_1'} $sid_expires $profile_txt{'sid_expires_2'}</span></td>
 </tr>
</table>
</form>
	~;

	if (!$view) {
		$yymain .= $showProfile;
		&template;
	}
}

sub ModifyProfile2 {
	&SidCheck($action);

	my (%member, $key, $value, $a, @reservecfg, @reserve, $matchword, $matchcase, $matchuser, $matchname, $namecheck, $reserved, $reservecheck, $tempname);
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		$value =~ s~[\n\r]~~g;
		$member{$key} = $value;
	}
	my $user = $INFO{'username'};
	$member{'username'} = $user;
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	if ($sessions == 1 && $sessionvalid == 1 && ($iamadmin || $iamgmod) && $username eq $user) {
		&LoadLanguage('Sessions');
		if ($member{'sesquest'} ne "password" && $member{'sesanswer'} eq '') { &fatal_error('no_secret_answer'); }
		if ($member{'sesquest'} eq "password") { $member{'sesanswer'} = ''; }
	}

	# make sure this person has access to this profile
	if ($user eq "admin" && $username ne "admin") { &fatal_error("not_allowed_profile_change"); }
	if ($user ne $username && !$iamadmin && (!$iamgmod || !$allow_gmod_profile)) { &fatal_error("not_allowed_profile_change"); }
	if (!$iamadmin) {
		$member{'settings6'} = ${$uid.$user}{'postcount'};
		$member{'settings7'} = ${$uid.$user}{'position'};
		$member{'addgroup'}  = ${$uid.$user}{'addgroups'};
	}

	if ($member{'username'} =~ /\//) { &fatal_error("no_user_slash"); }
	if ($member{'username'} =~ /\\/) { &fatal_error("no_user_backslash"); }

	my $encryptpassw = ${$uid.$user}{'password'};
	my $encryptuname = &encode_password($member{'name'});

	if ($member{'passwrd1'} || $member{'passwrd2'}) {
		&fatal_error("password_mismatch","$member{'username'}") if ($member{'passwrd1'} ne $member{'passwrd2'});
		&fatal_error("no_password","$member{'username'}") if ($member{'passwrd1'} eq '');
		&fatal_error("invalid_character","$profile_txt{'36'} $profile_txt{'241'}") if ($member{'passwrd1'} =~ /[^\s\w!\@#\$\%\^&\*\(\)\+\|`~\-=\\:;'",\.\/\?\[\]\{\}]/);
		&fatal_error("password_is_userid") if ($member{'username'} eq $member{'passwrd1'});
		$member{'cpasswrd'} = $member{'passwrd1'};
		my $passchanged = 1;
		if (lc $member{'name'} eq lc &MemberIndex("check_exist", $member{'name'}) && ${$uid.$user}{'realname'} ne $member{'name'}) { &fatal_error('name_taken'); }
	}

	if ($name_cannot_be_userid && lc $member{'name'} eq lc $member{'username'}) { &fatal_error('name_is_userid'); }

	if ($member{'name'} eq '') {
		&fatal_error("no_name");
	}

	&LoadCensorList;

	$censored_word = &CheckCensor("$member{'name'}");
	$censored_name = &Censor("$member{'name'}");

	if ($censored_name ne $member{'name'}) {
		&fatal_error("name_censored","<b>$censored_word</b>");
	}

	&FromChars($member{'name'});
	$convertstr = $member{'name'};
	$convertcut = 30;
	&CountChars;
	$member{'name'} = $convertstr;
	&fatal_error("name_too_long") if ($cliped);

	&fatal_error("invalid_character","$profile_txt{'68'} $profile_txt{'241re'}") if ($member{'name'} =~ /[^ \w\x80-\xFF\[\]\(\)#\%\+,\-\|\.:=\?\@\^]/);
	&fatal_error("no_name") if ($member{'name'} eq '');
	if ($member{'bday1'} ne "" || $member{'bday2'} ne "" || $member{'bday3'} ne "") {
		&fatal_error("invalid_birthdate","($member{'bday1'}/$member{'bday2'}/$member{'bday3'})") if ($member{'bday1'} !~ /^[0-9]+$/ || $member{'bday2'} !~ /^[0-9]+$/ || $member{'bday3'} !~ /^[0-9]+$/ || length($member{'bday3'}) < 4);
		&fatal_error("invalid_birthdate","($member{'bday1'}/$member{'bday2'}/$member{'bday3'})") if ($member{'bday1'} < 1 || $member{'bday1'} > 12 || $member{'bday2'} < 1 || $member{'bday2'} > 31 || $member{'bday3'} < 1901 || $member{'bday3'} > $year - 5);
	}

	if ($member{'moda'} eq $profile_txt{'88'}) {
		$member{'bday1'} =~ s/[^0-9]//g;
		$member{'bday2'} =~ s/[^0-9]//g;
		$member{'bday3'} =~ s/[^0-9]//g;
		if ($member{'bday1'}) { $member{'bday'} = "$member{'bday1'}/$member{'bday2'}/$member{'bday3'}"; }
		else { $member{'bday'} = ''; }

		$tempname = $member{'name'};
		&ToHTML($member{'name'});

		# Check to see if name is already taken or reserved
		fopen(FILE, "$vardir/reservecfg.txt") || &fatal_error("cannot_open","$vardir/reservecfg.txt", 1);
		@reservecfg = <FILE>;
		fclose(FILE);
		for ($a = 0; $a < @reservecfg; $a++) { chomp $reservecfg[$a]; }
		$matchword = $reservecfg[0] eq 'checked';
		$matchcase = $reservecfg[1] eq 'checked';
		$matchuser = $reservecfg[2] eq 'checked';
		$matchname = $reservecfg[3] eq 'checked';
		$namecheck = $matchcase eq 'checked' ? $member{'name'} : lc $member{'name'};

		if($user ne "admin") {
			if ($encryptpassw eq $encryptuname) { &fatal_error("password_is_userid"); }
			fopen(FILE, "$vardir/reserve.txt") || &fatal_error("cannot_open","$vardir/reserve.txt", 1);
			@reserve = <FILE>;
			fclose(FILE);
			foreach $reserved (@reserve) {
				chomp $reserved;
				$reservecheck = $matchcase ? $reserved : lc $reserved;
				if ($matchname) {
					if ($matchword) {
						if ($namecheck eq $reservecheck) { &fatal_error("name_reserved","$namecheck"); }
					} else {
						if ($namecheck =~ $reservecheck) { &fatal_error("name_reserved","$namecheck"); }
					}
				}
			}
		}

		if ($extendedprofiles) {
			require "$sourcedir/ExtendedProfiles.pl";
			my $error = &ext_validate_submition($username,$user);
			if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
		}

		if (${$uid.$user}{'realname'} ne $member{'name'}) {
			$is_existing = lc &MemberIndex("check_exist", $member{'name'});
			if (lc $is_existing eq lc $member{'name'} && lc $is_existing ne lc $user) { &fatal_error('name_taken',"($member{'name'})"); }

			&ToChars($member{'name'});

			# rewrite attachments.txt with new username
			fopen(ATM, "+<$vardir/attachments.txt", 1) || &fatal_error("cannot_open","$vardir/attachments.txt");
			seek ATM, 0, 0;
			my @attachments = <ATM>;
			truncate ATM, 0;
			seek ATM, 0, 0;
			for (my $i = 0; $i < @attachments; $i++) {
				$attachments[$i] =~ s/^(\d+\|\d+\|.*?)\|(.*?)\|/ ($2 eq ${$uid.$user}{'realname'} ? "$1|$member{'name'}|" : "$1|$2|") /e;
			}
			print ATM @attachments;
			fclose(ATM);
			
			#Since we haven't encountered a fatal error, time to rewrite our memberlist a little.
			&ManageMemberinfo("update", $user, $member{'name'});
		}

		# let's restore the name now
		&ToHTML($tempname);
		$member{'name'} = $tempname;
		&ToChars($member{'name'});

		&ToHTML($member{'gender'});
		&FromChars($member{'location'});
		&ToHTML($member{'location'});
		&ToHTML($member{'bday'});
		&ToHTML($member{'sesquest'});

		# Free code is the best code, like this code
		# Time to print the changes to the username.vars file
		if ($member{'passwrd1'}) { ${$uid.$user}{'password'} = &encode_password($member{'passwrd1'}); }
		${$uid.$user}{'realname'} = $member{'name'};
		${$uid.$user}{'gender'} = $member{'gender'};
		${$uid.$user}{'location'} = $member{'location'};
		${$uid.$user}{'bday'} = $member{'bday'};
		${$uid.$user}{'sesquest'} = $member{'sesquest'};
		${$uid.$user}{'sesanswer'} = &scramble($member{'sesanswer'}, $user);
		${$uid.$username}{'session'} = &encode_password($user_ip);
		if ($extendedprofiles && $error eq "") { &ext_saveprofile($user); }
		&UserAccount($user, "update");

		if ($username eq $user) {
			my $pword = ${$uid.$user}{'password'};
			my $expiration = "Sunday, 17-Jan-2038 00:00:00 GMT";
			&UpdateCookie("write", $user, $pword, ${$uid.$user}{'session'}, "/", $expiration);
			&LoadUser($user);
		}
		&WriteLog;

		my $scriptAction = $view ? 'myprofileContacts' : 'profileContacts';
		$yySetLocation = qq~$scripturl?action=$scriptAction;username=$useraccount{$member{'username'}};sid=$INFO{'sid'}~;
		&redirectexit;

	} elsif ($member{'moda'} eq $profile_txt{'89'}) {
		&fatal_error("cannot_kill_admin") if ($member{'username'} eq 'admin');

		# For security, remove username from mod position
		&KillModerator($member{'username'});

		$noteuser = $iamadmin ? $member{'username'} : $user;

		unlink("$memberdir/$noteuser.dat");
		unlink("$memberdir/$noteuser.vars");
		unlink("$memberdir/$noteuser.ims");
		unlink("$memberdir/$noteuser.msg");
		unlink("$memberdir/$noteuser.log");
		unlink("$memberdir/$noteuser.rlog");
		unlink("$memberdir/$noteuser.outbox");
		unlink("$memberdir/$noteuser.imstore");
		unlink("$memberdir/$noteuser.imdraft");
		unlink("$facesdir/UserAvatars/$1") if ${$uid.$user}{'userpic'} && ${$uid.$user}{'userpic'} =~ /$facesurl\/UserAvatars\/(.+)/;

		&MemberIndex("remove", $noteuser);

		if (!$iamadmin) {
			&UpdateCookie("delete");
			$username = 'Guest';
			$iamguest = 1;
			$iamadmin = '';
			$iamgmod = '';
			$password = '';
			$yyim = '';
			$ENV{'HTTP_COOKIE'} = '';
			$yyuname = '';
		}
		$yySetLocation = $scripturl;
		&redirectexit;
	} else {
		&fatal_error('not_allowed');
	}
}

sub ModifyProfileContacts2 {
	&SidCheck($action);

	my (%member, $key, $value, $newpassemail, @entries, $tempname, $stealth);
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		$value =~ s~[\n\r]~~g;
		$member{$key} = $value;
	}
	my $user = $INFO{'username'};
	$member{'username'} = $user;
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	# make sure this person has access to this profile
	if ($user ne $username && !$iamadmin && (!$iamgmod || !$allow_gmod_profile)) { &fatal_error("not_allowed_profile_change"); }
	if (!$iamadmin) {
		$member{'settings6'} = ${$uid.$user}{'postcount'};
		$member{'settings7'} = ${$uid.$user}{'position'};
		$member{'addgroup'} = ${$uid.$user}{'addgroups'};
	}

	if ($emailnewpass && lc $member{'email'} ne lc ${$uid.$user}{'email'} && !$iamadmin) {
		srand();
		$member{'passwrd1'} = int(rand(100));
		$member{'passwrd1'} =~ tr/0123456789/ymifxupbck/;
		$_ = int(rand(77));
		$_ =~ tr/0123456789/q8dv7w4jm3/;
		$member{'passwrd1'} .= $_;
		$_ = int(rand(89));
		$_ =~ tr/0123456789/y6uivpkcxw/;
		$member{'passwrd1'} .= $_;
		$_ = int(rand(188));
		$_ =~ tr/0123456789/poiuytrewq/;
		$member{'passwrd1'} .= $_;
		$_ = int(rand(65));
		$_ =~ tr/0123456789/lkjhgfdaut/;
		$member{'passwrd1'} .= $_;
		${$uid.$user}{'password'} = &encode_password($member{'passwrd1'});
		$newpassemail = 1;
	}

	&fatal_error("no_email") if ($member{'email'} eq '');
	&fatal_error("invalid_character","$profile_txt{'69'} $profile_txt{'241e'}") if ($member{'email'} !~ /^[\w\-\.\+]+\@[\w\-\.\+]+\.\w{2,4}$/);
	&fatal_error("invalid_email") if (($member{'email'} =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/) || ($member{'email'} !~ /^.+@\[?(\w|[-.])+\.[a-zA-Z]{2,4}|[0-9]{1,4}\]?$/));

	if ($member{'moda'} eq $profile_txt{'88'}) {
		$member{'icq'} =~ s/[^0-9]//g;
		$member{'aim'} =~ s/ /\+/g;
		$member{'yim'} =~ s/ /\+/g;
		$member{'msn'} =~ s/ /\+/g;

		# store the name temorarily so we can restore any _'s later
		$tempname = $member{'name'};
		$member{'name'} =~ s/\_/ /g;

		&ToHTML($member{'email'});
		&ToHTML($member{'hideemail'});
		&ToHTML($member{'icq'});
		&ToHTML($member{'aim'});
		&ToHTML($member{'yim'});
		&ToHTML($member{'msn'});
		&ToHTML($member{'gtalk'});
		&ToHTML($member{'skype'});
		&ToHTML($member{'weburl'});
		&FromChars($member{'webtitle'});
		&ToHTML($member{'webtitle'});
		&ToHTML($member{'offlinestatus'});
		&ToHTML($member{'awaysubj'});
		&ToHTML($member{'awayreply'});

		if ($extendedprofiles) {
			require "$sourcedir/ExtendedProfiles.pl";
			my $error = &ext_validate_submition($username,$user);
			if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
		}

		# Check to see if email is already taken
		if (lc ${$uid.$user}{'email'} ne lc $member{'email'}) {
			$testemail = lc $member{'email'};
			$is_existing = &MemberIndex("check_exist", "$testemail");
			if (lc $is_existing eq $testemail) { &fatal_error("email_taken","($member{'email'})"); }

			# Since we haven't encountered a fatal error, time to rewrite our memberlist a little.
			&ManageMemberinfo("update", $user, '', $member{'email'});
		}
		## if enabled but not set, default offline status to 'offline'
		if ($enable_MCaway && $member{'offlinestatus'} eq '') { $member{'offlinestatus'} = 'offline'; }

		# let's restore the name now
		&ToHTML($tempname);
		$member{'name'} = $tempname;

		# if user is switching 'away' to 'off/on', clean out the away-sent list
		if ($FORM{'offlinestatus'} eq 'offline') { ${$uid.$user}{'awayreplysent'} = ''; }

		# Time to print the changes to the username.vars file
		${$uid.$user}{'email'} = $member{'email'};
		${$uid.$user}{'hidemail'} = $member{'hideemail'};
		${$uid.$user}{'icq'} = $member{'icq'};
		${$uid.$user}{'aim'} = $member{'aim'};
		${$uid.$user}{'yim'} = $member{'yim'};
		${$uid.$user}{'msn'} = $member{'msn'};
		${$uid.$user}{'gtalk'} = $member{'gtalk'};
		${$uid.$user}{'skype'} = $member{'skype'};
		${$uid.$user}{'webtitle'} = $member{'webtitle'};
		${$uid.$user}{'weburl'} = $member{'weburl'};
		${$uid.$user}{'offlinestatus'} = $member{'offlinestatus'};
		${$uid.$user}{'awaysubj'} = $member{'awaysubj'};
		${$uid.$user}{'awayreply'} = $member{'awayreply'};
		${$uid.$user}{'stealth'} = (${$uid.$user}{'position'} eq 'Administrator' || ${$uid.$user}{'position'} eq 'Global Moderator') ? $member{'stealth'} : "";
		if ($extendedprofiles && $error eq "") { &ext_saveprofile($user); }
		&UserAccount($user, "update");

		if ($emailnewpass && $newpassemail == 1) {
			# Write log
			fopen(LOG, "$vardir/log.txt");
			@entries = <LOG>;
			fclose(LOG);
			fopen(LOG, ">$vardir/log.txt", 1);
			$field = "$user";
			foreach $curentry (@entries) {
				$curentry =~ s/\n//g;
				($name, $value) = split(/\|/, $curentry);
				if ($name ne "$field") {
					print LOG "$curentry\n";
				}
			}
			fclose(LOG);

			if ($username eq $user) {
				&UpdateCookie("delete");
				$username = 'Guest';
				$iamguest = 1;
				$iamadmin = '';
				$iamgmod = '';
				$password = '';
				$yyim = '';
				$ENV{'HTTP_COOKIE'} = '';
				$yyuname = '';
			}
			&FormatUserName($member{'username'});
			require "$sourcedir/Mailer.pl";
			my $scriptAction = $view ? 'myprofile' : 'profile';
			&sendmail($member{'email'}, qq~$profile_txt{'700'} $mbname~, "$profile_txt{'733'} $member{'passwrd1'} $profile_txt{'734'} $member{'username'}.\n\n$profile_txt{'701'} $scripturl?action=$scriptAction;username=$useraccount{$member{'username'}}\n\n$profile_txt{'130'}");
			require "$sourcedir/LogInOut.pl";
			$sharedLogin_title = "$profile_txt{'34'}: $user";
			$sharedLogin_text = $profile_txt{'638'};
			$shared_login = &sharedLogin;
			$yymain .= $shared_login;
			$yytitle = $profile_txt{'245'};
			&template;
		}
	} else {
		&fatal_error('not_allowed');
	}

	my $scriptAction = $view ? 'myprofileOptions' : 'profileOptions';
	$yySetLocation = qq~$scripturl?action=$scriptAction;username=$useraccount{$member{'username'}};sid=$INFO{'sid'}~;
	&redirectexit;
}

sub ModifyProfileOptions2 {
	&SidCheck($action);

	&FromChars($FORM{'signature'});
	&ToHTML($FORM{'signature'});
	$FORM{'signature'} =~ s~\n~<br />~g;

	my (%member, $key, $value, $tempname);
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		$value =~ s~[\n\r]~~g;
		$member{$key} = $value;
	}
	my $user = $INFO{'username'};
	$member{'username'} = $user;
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	# make sure this person has access to this profile
	if ($user ne $username && !$iamadmin && (!$iamgmod || !$allow_gmod_profile)) { &fatal_error("not_allowed_profile_change"); }
	if (!$iamadmin) {
		$member{'settings6'} = ${$uid.$user}{'postcount'};
		$member{'settings7'} = ${$uid.$user}{'position'};
		$member{'addgroup'} = ${$uid.$user}{'addgroups'};
	}

	if ($member{'username'} =~ /\//) { &fatal_error("no_user_slash"); }
	if ($member{'username'} =~ /\\/) { &fatal_error("no_user_backslash"); }
	$INFO{'username'} = $member{'username'};

	&FromChars($member{'usertext'});
	$convertstr = $member{'usertext'};
	$convertcut = 51;
	&CountChars;
	$member{'usertext'} = $convertstr;

	if ($allowpics && $upload_useravatar && $upload_avatargroup) {
		$upload_useravatar = 0;
		foreach my $av_gr (split(/\, /, $upload_avatargroup)) {
			if ($av_gr eq ${$uid.$user}{'position'}) { $upload_useravatar = 1; last; }
			foreach (split(/\, /, ${$uid.$user}{'addgroups'})) {
				if ($av_gr eq $_) { $upload_useravatar = 1; last; }
			}
		}
	}

	my $file = $CGI_query->upload("file_avatar") if $CGI_query;
	if ($allowpics && $upload_useravatar && $file) {
		if ($file !~ /\.(gif|png|jpe?g)$/i) {
			&LoadLanguage('FA');
			&fatal_error('file_not_uploaded',"$file $fatxt{'20'} gif png jpeg jpg");
		}
		my $ext = $1;
		my $fixfile = ${$uid.$user}{'realname'};
		$fixfile =~ s/[^0-9A-Za-z\+\-\.:_]/_/g; # replace all inappropriate with the "_" character.
		$fixfile .= ".$ext";

		require "$sourcedir/SpamCheck.pl";
		my $spamdetected = &spamcheck("$fixfile");
		if (!$iamadmin && !$iamgmod && !$iammod){
			if ($spamdetected == 1) {
				${$uid.$username}{'spamcount'}++;
				${$uid.$username}{'spamtime'} = $date;
				&UserAccount($username,"update");
				$spam_hits_left_count = $post_speed_count - ${$uid.$username}{'spamcount'};
				&fatal_error("tsc_alert");
			}
		}

		my ($size,$buffer,$filesize,$file_buffer);
		while ($size = read($file, $buffer, 512)) { $filesize += $size; $file_buffer .= $buffer; }
		if ($avatar_limit && $filesize > (1024 * $avatar_limit)) {
			&LoadLanguage('FA');
			&fatal_error('file_not_uploaded',"$fatxt{'21'} $file (" . int($filesize / 1024) . " KB) $fatxt{'21b'} " . $avatar_limit);
		}
		if ($avatar_dirlimit) {
			my $dirsize = &dirsize("$facesdir/UserAvatars");
			if ($filesize > ((1024 * $avatar_dirlimit) - $dirsize)) {
				&LoadLanguage('FA');
				&fatal_error('file_not_uploaded',"$fatxt{'22'} $file (" . (int($filesize / 1024) - $avatar_dirlimit + int($dirsize / 1024)) . " KB) $fatxt{'22b'}");
			}
		}

		unlink("$facesdir/UserAvatars/$1") if ${$uid.$user}{'userpic'} =~ /$facesurl\/UserAvatars\/(.+)/;
		$fixfile = &check_existence("$facesdir/UserAvatars", $fixfile);

		# create a new file on the server using the formatted ( new instance ) filename
		if (fopen(NEWFILE, ">$facesdir/UserAvatars/$fixfile")) {
			binmode NEWFILE; # needed for operating systems (OS) Windows, ignored by Linux
			print NEWFILE $file_buffer; # write new file on HD
			fclose(NEWFILE);

		} else { # return the server's error message if the new file could not be created
			&fatal_error("file_not_open","$facesdir/UserAvatars");
		}

		# check if file has actually been uploaded, by checking the file has a size
		unless (-s "$facesdir/UserAvatars/$fixfile") {
			&fatal_error("file_not_uploaded",$fixfile);
		}

		my $illegal;
		if ($fixfile =~ /gif$/i) {
			my $header;
			fopen(ATTFILE, "$facesdir/UserAvatars/$fixfile");
			read(ATTFILE, $header, 10);
			my $giftest;
			($giftest, undef, undef, undef, undef, undef) = unpack("a3a3C4", $header);
			fclose(ATTFILE);
			if ($giftest ne "GIF") { $illegal = $giftest; }
		}
		fopen(ATTFILE, "$facesdir/UserAvatars/$fixfile");
		while ( read(ATTFILE, $buffer, 1024) ) {
			if ($buffer =~ /<(html|script|body)/ig) { $illegal = $1; last; }
		}
		fclose(ATTFILE);
		if($illegal) { # delete the file as it contains illegal code
			unlink("$facesdir/UserAvatars/$fixfile");
			&ToHTML($illegal);
			&fatal_error("file_not_uploaded","$fixfile <= illegal code ($illegal) inside image file!");
		}

		$member{'userpic'} = "$facesurl/UserAvatars/$fixfile";

	} elsif ($member{'userpicpersonalcheck'} && ($member{'userpicpersonal'} =~ /\.gif\Z/i || $member{'userpicpersonal'} =~ /\.jpe?g\Z/i || $member{'userpicpersonal'} =~ /\.png\Z/i)) {
		$member{'userpic'} = $member{'userpicpersonal'};
	}
	&fatal_error("invalid_character","$profile_txt{'592'}") if $member{'userpic'} !~ m^\A[0-9a-zA-Z_\.\#\%\-\:\+\?\$\&\~\.\,\@/]+\Z^;
	if ($member{'userpic'} eq "" || !$allowpics) { $member{'userpic'} = "blank.gif"; }
	unlink("$facesdir/UserAvatars/$1") if $member{'userpic'} ne ${$uid.$user}{'userpic'} && ${$uid.$user}{'userpic'} =~ /$facesurl\/UserAvatars\/(.+)/;

	if ($member{'moda'} eq $profile_txt{'88'}) {
		$convertstr = $member{'signature'};
		$convertcut = $MaxSigLen;
		&CountChars;
		$member{'signature'} = $convertstr;

		# store the name temorarily so we can restore any _'s later
		$tempname = $member{'name'};
		$member{'name'} =~ s/\_/ /g;
		if ($member{'usertemplate'} ne '' && !$templateset{$member{'usertemplate'}}) { &fatal_error('invalid_template'); }
		if ($member{'usertemplate'} eq '') { $member{'usertemplate'} = $template; }
		if ($member{'userlanguage'} ne '' && !-e "$langdir/$member{'userlanguage'}/Main.lng") { &fatal_error('invalid_language'); }
		if ($member{'userlanguage'} eq '') { $member{'userlanguage'} = $language; }

		# update notifications if users language is changed
		if (${$uid.$user}{'language'} ne "$member{'userlanguage'}") {
			require "$sourcedir/Notify.pl";
			&updateLanguage($user, $member{'userlanguage'});
		}

		if ($member{'usertimesign'} !~ /^-?$/ || $member{'usertimehour'} !~ /^\d+$/ || $member{'usertimemin'} !~ /^\d+$/) { &fatal_error("invalid_time_offset","$member{'usertimesign'}$member{'usertimehour'}.$member{'usertimemin'}"); }

		$member{'joinmemgroup'} = '';
		if (($addmemgroup_enabled == 2 || $addmemgroup_enabled == 3)) {
			foreach (keys %NoPost) {
				my $additional = (split /\|/, $NoPost{$_})[10];
				if ($additional == 1 && $FORM{'joinmemgroup'} eq $_) { $member{'joinmemgroup'} = $_; }
			}
			&ToHTML($member{'joinmemgroup'});
			$member{'addgroup'} = $member{'joinmemgroup'};
			if ($member{'addgroup'} eq '') { $member{'addgroup'} = qq~###blank###~; }
			if ($member{'addgroup'} ne ${$uid.$user}{'addgroups'}) {
				&ManageMemberinfo("update", $user, '', '', '', '', $member{'addgroup'});
			}
			if ($member{'addgroup'} =~ /###blank###/) { $member{'addgroup'} = ''; }
			${$uid.$user}{'addgroups'} = $member{'joinmemgroup'};
		}

		# let's restore the name now
		$member{'name'} = $tempname;

		&ToHTML($member{'name'});
		&ToHTML($member{'usertext'});
		#&ToHTML($member{'signature'}); Don't do this again! It's allready done!
		&ToHTML($member{'userpic'});
		&ToHTML($member{'onlinealert'});
		&ToHTML($member{'usertimeselect'});
		&ToHTML($member{'usertemplate'});
		&ToHTML($member{'userlanguage'});
		&ToHTML($member{'timeformat'});

		# Time to print the changes to the username.vars file
		${$uid.$user}{'usertext'} = $member{'usertext'};
		${$uid.$user}{'userpic'} = $member{'userpic'};
		${$uid.$user}{'signature'} = $member{'signature'};
		${$uid.$user}{'timeoffset'} = "$member{'usertimesign'}$member{'usertimehour'}.$member{'usertimemin'}";
		${$uid.$user}{'onlinealert'} = $member{'onlinealert'};
		${$uid.$user}{'notify_me'} = $member{'notify_N'} ? ((!${$uid.$user}{'notify_me'} || ${$uid.$user}{'notify_me'} == 1) ? 1 : 3) : ((${$uid.$user}{'notify_me'} == 2 || ${$uid.$user}{'notify_me'} == 3) ? 2 : 0);
		${$uid.$user}{'reversetopic'} = $member{'reversetopic'};
		${$uid.$user}{'dsttimeoffset'} = $member{'dsttimeoffset'} || 0;
		${$uid.$user}{'timeselect'} = $member{'usertimeselect'};
		${$uid.$user}{'template'} = $member{'usertemplate'};
		${$uid.$user}{'language'} = $member{'userlanguage'};
		${$uid.$user}{'timeformat'} = $member{'timeformat'};
		if ($extendedprofiles) {
			require "$sourcedir/ExtendedProfiles.pl";
			my $error = &ext_validate_submition($username,$user);
			if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
			&ext_saveprofile($user);
		}
		&UserAccount($user, "update");
	} else {
		&fatal_error("not_allowed");
	}
	
	my $scriptAction;
	if ($iamadmin || ($iamgmod && $allow_gmod_profile && $gmod_access2{'profileAdmin'} eq 'on')) {
		$scriptAction = qq~profileAdmin~;
	} else {
		$scriptAction = qq~viewprofile~;
	}
	if ($PM_level == 1 || ($PM_level == 2 && ($iamadmin || $iamgmod || $iammod)) || ($PM_level == 3 && ($iamadmin || $iamgmod))) {
		$scriptAction = qq~profileIM~;
	}
	if ($buddyListEnabled) {
		$scriptAction = qq~profileBuddy~;
	}
	if ($view) { $scriptAction = qq~my$scriptAction~; }
	$yySetLocation = qq~$scripturl?action=$scriptAction;username=$useraccount{$member{'username'}};sid=$INFO{'sid'}~;
	&redirectexit;
}

sub ModifyProfileBuddy2 {
	&SidCheck($action);

	my (%member, $key, $value, $tempname);
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		$value =~ s~[\n\r]~~g;
		$member{$key} = $value;
	}
	my $user = $INFO{'username'};
	$member{'username'} = $user;
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	# make sure this person has access to this profile
	if ($user ne $username && !$iamadmin && (!$iamgmod || !$allow_gmod_profile)) { &fatal_error("not_allowed_profile_change"); }
	if ($member{'username'} =~ /\//) { &fatal_error("no_user_slash"); }
	if ($member{'username'} =~ /\\/) { &fatal_error("no_user_backslash"); }

	if ($member{'moda'} eq $profile_txt{'88'}) {
		if ($member{'buddylist'}) {
			my @buddies = split(/\,/, $member{'buddylist'});
			chomp(@buddies);
			$member{'buddylist'} = '';
			foreach my $cloakedBuddy (@buddies) {
				$cloakedBuddy =~ s/^ //;
				$cloakedBuddy = &decloak($cloakedBuddy);
				&ToHTML($cloakedBuddy);
				$member{'buddylist'} = qq~$member{'buddylist'}\|$cloakedBuddy~;
			}
			$member{'buddylist'} =~ s/^\|//;
		}
		${$uid.$user}{'buddylist'} = $member{'buddylist'};
		&UserAccount($user, "update");
	} else {
		&fatal_error("not_allowed");
	}

	my $scriptAction;
	if ($iamadmin || ($iamgmod && $allow_gmod_profile && $gmod_access2{'profileAdmin'} eq 'on')) {
		$scriptAction = qq~profileAdmin~;
	} else {
		$scriptAction = qq~viewprofile~;
	}
	if ($PM_level == 1 || ($PM_level == 2 && ($iamadmin || $iamgmod || $iammod)) || ($PM_level == 3 && ($iamadmin || $iamgmod))) {
		$scriptAction = qq~profileIM~;
	}
	if ($view) { $scriptAction = qq~my$scriptAction~; }
	$yySetLocation = qq~$scripturl?action=$scriptAction;username=$useraccount{$member{'username'}};sid=$INFO{'sid'}~;
	&redirectexit;
}

sub ModifyProfileIM2 {
	&SidCheck($action);

	my (%member, $key, $value, $ignorelist);
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		unless ($key eq 'ignore') { $value =~ s~[\n\r]~~g; }
		$member{$key} = $value;
	}

	my $user = $INFO{'username'};
	$member{'username'} = $user;
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	# make sure this person has access to this profile
	if ($user ne $username && !$iamadmin && (!$iamgmod || !$allow_gmod_profile)) { &fatal_error("not_allowed_profile_change"); }

	if (!$member{'ignoreall'}) {
		my @ignoreList = split(/\,/, $member{'ignore'});
		chomp (@ignoreList);
		foreach my $cloakedIgnore (@ignoreList)	{
			$cloakedIgnore =~s/\A //;
			$cloakedIgnore =~s/ \Z//;
			$cloakedIgnore = &decloak($cloakedIgnore);
			&ToHTML ($cloakedIgnore);
			$ignorelist .= qq~\|$cloakedIgnore~;
		}
		$ignorelist =~ s~\A\|~~;
	} else {
		$ignorelist = '*';
	}

	&ToHTML($member{'userpopup'});
	&ToHTML($member{'popupims'});
	&ToHTML($member{'pmactprev'});
	&ToHTML($member{'pmmessprev'});
	&ToHTML($member{'pmviewMess'});

	# Time to print the changes to the username.vars file
	${$uid.$user}{'im_ignorelist'} = $ignorelist;
	${$uid.$user}{'notify_me'} = $member{'notify_PM'} ? ((!${$uid.$user}{'notify_me'} || ${$uid.$user}{'notify_me'} == 2) ? 2 : 3) : ((${$uid.$user}{'notify_me'} == 1 || ${$uid.$user}{'notify_me'} == 3) ? 1 : 0);
	${$uid.$user}{'im_popup'} = $member{'userpopup'};
	${$uid.$user}{'im_imspop'} = $member{'popupims'};
	${$uid.$user}{'pmactprev'} = $member{'pmactprev'};
	${$uid.$user}{'pmmessprev'} = $member{'pmmessprev'};
	${$uid.$user}{'pmviewMess'} = $member{'pmviewMess'};

	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		my $error = &ext_validate_submition($username,$user);
		if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
		&ext_saveprofile($user);
	}

	&UserAccount($user, "update");

	my $scriptAction = qq~viewprofile~;
	if ($iamadmin || ($iamgmod && $allow_gmod_profile && $gmod_access2{'profileAdmin'} eq 'on')) {
		$scriptAction = qq~profileAdmin~;
	}
	if ($view) { $scriptAction = qq~my$scriptAction~; }
	$yySetLocation = qq~$scripturl?action=$scriptAction;username=$useraccount{$member{'username'}};sid=$INFO{'sid'}~;
	&redirectexit;
}

sub ModifyProfileAdmin2 {
	&SidCheck($action);

	&is_admin_or_gmod;

	my (%member, $key, $value);
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		#$value =~ s~[\n\r]~~g;
		$member{$key} = $value;
	}
	my $user = $INFO{'username'};
	$member{'username'} = $user;
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	$member{'addgroup'} =~ s/\, /\,/g;

	# make sure this person has access to this profile
	if ($user ne $username && !$iamadmin && (!$iamgmod || !$allow_gmod_profile)) { &fatal_error("not_allowed_profile_change"); }

	if (!$iamadmin && ($member{'settings7'} eq "Administrator" || $member{'settings7'} eq "Global Moderator")) {
		$member{'settings7'} = ${$uid.$user}{'position'};
	}

	if ($member{'settings6'} eq '') { $member{'settings6'} = 0; }
	if ($member{'settings6'} !~ /\A[0-9]+\Z/) { &fatal_error('invalid_postcount'); }
	&fatal_error('cannot_regroup_admin') if ($member{'username'} eq 'admin' && $member{'settings7'} ne 'Administrator');

	$dr_month  = $member{'dr_month'};
	$dr_day    = $member{'dr_day'};
	$dr_year   = $member{'dr_year'};
	$dr_hour   = $member{'dr_hour'};
	$dr_minute = $member{'dr_minute'};
	$dr_secund = $member{'dr_secund'};

	if($dr_month == 4 || $dr_month == 6 || $dr_month == 9 || $dr_month == 11) {
		$max_days = 30;
	}
	elsif($dr_month == 2 && $dr_year % 4 == 0) {
		$max_days = 29;
	}
	elsif($dr_month == 2 && $dr_year % 4 != 0) {
		$max_days = 28;
	}
	else {
		$max_days = 31;
	}
	if($dr_day > $max_days) { $dr_day = $max_days; }

	$member{'dr'} = qq~$dr_month/$dr_day/$dr_year $maintxt{'107'} $dr_hour:$dr_minute:$dr_secund~;

	if ($member{'settings6'} != ${$uid.$user}{'postcount'} || $member{'settings7'} ne ${$uid.$user}{'position'}) {
		if ($member{'settings7'}) {
			$grp_after = qq~$member{'settings7'}~;
		} else {
			foreach $postamount (sort { $b <=> $a } keys %Post) {
				if ($member{'settings6'} >= $postamount) {
					($title, undef) = split(/\|/, $Post{$postamount}, 2);
					$grp_after = $title;
					last;
				}
			}
		}
		&ManageMemberinfo("update", $user, '', '', $grp_after, $member{'settings6'});
	}

	if ($member{'addgroup'} eq '') { $member{'addgroup'} = qq~###blank###~; }
	if ($member{'addgroup'} ne ${$uid.$user}{'addgroups'}) {
		&ManageMemberinfo("update", $user, '', '', '', '', $member{'addgroup'});
	}
	if ($member{'addgroup'} =~ /###blank###/) { $member{'addgroup'} = ''; }

	if ($member{'dr'} ne ${$uid.$user}{'regdate'}) {
		$newreg = &stringtotime($member{'dr'});
		$newreg = sprintf("%010d", $newreg);
		&ManageMemberlist("update", $user, $newreg);
		${$uid.$user}{'regtime'} = $newreg;
	}

	if ($member{'moda'} eq $profile_txt{'88'}) {
		if (!$iamadmin) { $member{'dr'} = ${$uid.$user}{'regdate'}; }
		&ToHTML($member{'regreason'});
		$member{'regreason'} =~ s~\n~<br />~ig;
		$member{'addgroup'} =~ s/\A\,//;
		${$uid.$user}{'regreason'} = $member{'regreason'};
		${$uid.$user}{'addgroups'} = $member{'addgroup'};
		${$uid.$user}{'postcount'} = $member{'settings6'};
		${$uid.$user}{'position'}  = $member{'settings7'};
		${$uid.$user}{'regdate'}   = $member{'dr'};
		${$uid.$user}{'stealth'}   = "" if ${$uid.$user}{'position'} ne 'Administrator' && ${$uid.$user}{'position'} ne 'Global Moderator';
		if ($extendedprofiles) {
			require "$sourcedir/ExtendedProfiles.pl";
			my $error = &ext_validate_submition($username,$user);
			if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
			&ext_saveprofile($user);
		}
		&UserAccount($user, "update");

		my $scriptAction = $view ? 'myviewprofile' : 'viewprofile';
		$yySetLocation = qq~$scripturl?action=$scriptAction;username=$useraccount{$user}~;
		&redirectexit;

	} else {
		&fatal_error("cannot_kill_admin");
	}
}

sub ViewProfile {
	# If someone registers with a '+' in their name It causes problems.
	# Gets turned into a <space> in the query string Change it back here.
	# Users who register with spaces get them replaced with _
	# So no problem there.
	$INFO{'username'} =~ tr/ /+/;

	if ($iamguest) { &fatal_error("members_only"); }
	if ($INFO{'username'} =~ /\//) { &fatal_error("no_user_slash"); }
	if ($INFO{'username'} =~ /\\/) { &fatal_error("no_user_backslash"); }
	if ($do_scramble_id) { &decloak($INFO{'username'}); }
	if (!-e ("$memberdir/$INFO{'username'}.vars")) { &fatal_error("no_profile_exists"); }

	my ($memberinfo, $modify, $email, $gender, $avstyle, $pic);
	my ($pic_row, $buddybutton, $row_addgrp, $row_gender, $row_age, $row_location, $row_icq, $row_aim, $row_yim, $row_msn, $row_gtalk, $row_skype, $row_email, $row_website, $row_signature, $showusertext);

	# Convert forum start date to string, if there is no date set,
	# Defaults to 1st Jan, 2005
	$forumstart = $forumstart ? &stringtotime($forumstart) : "1104537600";

	my $user = $INFO{'username'};
	if (!${$uid.$user}{'password'}) { &LoadUser($user); }

	if (${$uid.$user}{'weburl'} !~ m~\Ahttp://~ && ${$uid.$user}{'weburl'} !~ m~\Ahttps://~) { ${$uid.$user}{'weburl'} = "http://${$uid.$user}{'weburl'}"; }
	$memsettingsd[9] = ${$uid.$user}{'aim'};
	$memsettingsd[9] =~ tr/+/ /;
	$memsettingsd[10] = ${$uid.$user}{'yim'};
	$memsettingsd[10] =~ tr/+/ /;

	if (${$uid.$user}{'regtime'}) {
		$dr = &timeformat(${$uid.$user}{'regtime'});
	} else {
		$dr = $profile_txt{'470'};
	}

	&CalcAge($user, "calc");      # How old is he/she?
	&CalcAge($user, "isbday");    # is it the bday?
	if ($isbday) { $isbday = "<img src=\"$imagesdir/bdaycake.gif\" width=\"40\" />"; }
	## only show the 'modify' button if not using 'my center' or admin/gmod viewing
	if (!$view && ($iamadmin || ($iamgmod && $allow_gmod_profile && ${$uid.$user}{'position'} ne "Administrator"))) {
		$modify = qq~<a href="$scripturl?action=profileCheck;username=$useraccount{$user}">$img{'modify'}</a>~;
	}
	if ($user eq "admin" && $username ne "admin") { $modify = ""; }

	if ($allowpics) {
		if (${$uid.$user}{'userpic'} eq "blank.gif") {
			$pic = qq~<img src="$imagesdir/nn.gif" name="avatar_img_resize" alt="" border="0" style="display:none" />~;
		} elsif (${$uid.$user}{'userpic'} =~ /^https?:\/\//) {
			$pic = qq~<img src="${$uid.$user}{'userpic'}" name="avatar_img_resize" alt="" border="0" style="display:none" />~;
		} else {
			$pic = qq~<img src="$facesurl/${$uid.$user}{'userpic'}" name="avatar_img_resize" alt="" border="0" style="display:none" />~;
		}
		$pic_row = qq~
			<div style="float: left; width: 20%; text-align: center; padding: 5px 5px 5px 0px;">
			$pic
			</div>
		~;
	}

	&usersOnline;
	&userOnLineStatus($user);

	if ($buddyListEnabled && $user ne $username) {
		&loadMyBuddy;
		$isbuddy = qq~<br /><img src="$imagesdir/buddylist.gif" border="0" align="middle" alt="$display_txt{'isbuddy'}" /> $display_txt{'isbuddy'}~;
		$addbuddylink = qq~<br /><a href="$scripturl?action=addbuddy;name=$useraccount{$user}">$img{'addbuddy'}</a>~;
	}
	if ($mybuddie{$user}) { $buddybutton = $isbuddy; }
	else { $buddybutton = $addbuddylink; }

	# Hide empty profile fields from display
	if ($addmembergroup{$user}) {
		$showaddgr = $addmembergroup{$user};
		$showaddgr =~ s/<br \/>/\, /g;
		$showaddgr =~ s/\A, //;
		$showaddgr =~ s/, \Z//;
		$row_addgrp .= qq~<br /><span class="small">$showaddgr</span>~;
	}
	if (${$uid.$user}{'gender'}) {
		if (${$uid.$user}{'gender'} eq 'Male') { $gender = $profile_txt{'238'}; } 
		elsif (${$uid.$user}{'gender'} eq 'Female') { $gender = $profile_txt{'239'}; }
		$row_gender = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'231'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		$gender
		</div>
		~;
	}
	if ($age) {
		$row_age = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'420'}:</b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		$age &nbsp; $isbday
		</div>
		~;
	}
	if (${$uid.$user}{'location'}) {
		&ToChars(${$uid.$user}{'location'});
		$row_location = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'227'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		${$uid.$user}{'location'}
		</div>
		~;
	}
	if (${$uid.$user}{'icq'} && ${$uid.$user}{'icq'} !~ m/\D/) {
		$row_icq .= qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'513'}:</b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<a href="http://web.icq.com/${$uid.$user}{'icq'}" title="${$uid.$user}{'icq'}" target="_blank">
		<img src="http://web.icq.com/whitepages/online?icq=${$uid.$user}{'icq'}&#38;img=5" alt="${$uid.$user}{'icq'}" border="0" style="vertical-align: middle;" /> ${$uid.$user}{'icq'}</a>
		</div>
		~;
	}
	if (${$uid.$user}{'aim'}) {
		$row_aim = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'603'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<a href="aim:goim?screenname=${$uid.$user}{'aim'}&#38;message=Hi,+are+you+there?">
		<img src="$imagesdir/aim.gif" alt="${$uid.$user}{'aim'}" border="0" style="vertical-align: middle;" /> $memsettingsd[9]</a>
		</div>
		~;
	}
	if (${$uid.$user}{'yim'}) {
		$row_yim = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'604'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<img src="http://opi.yahoo.com/online?u=${$uid.$user}{'yim'}&#38;m=g&#38;t=0" border="0" alt="${$uid.$user}{'yim'}" style="vertical-align: middle;" />
		<a href="http://edit.yahoo.com/config/send_webmesg?.target=${$uid.$user}{'yim'}" target="_blank"> $memsettingsd[10]</a>
		</div>
		~;
	}
	if (${$uid.$user}{'msn'}) {
		$row_msn = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'823'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<img src="$imagesdir/msn3.gif" alt="" border="0" style="vertical-align: middle;" />
		<a href="#" onclick="window.open('$scripturl?action=setmsn;msnname=$user','','height=80,width=340,menubar=no,toolbar=no,scrollbars=no'); return false">$profile_txt{'823'} ${$uid.$user}{'realname'}</a>
		</div>
		~;
	}
	if (${$uid.$user}{'gtalk'}) {
		$row_gtalk = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'825'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<img src="$imagesdir/gtalk2.gif" alt="" border="0" style="vertical-align: middle;" />
		<a href="#" onclick="window.open('$scripturl?action=setgtalk;gtalkname=$user','','height=80,width=340,menubar=no,toolbar=no,scrollbars=no'); return false">$profile_txt{'825'} ${$uid.$user}{'realname'}</a>
		</div>
		~;
	}
	if (${$uid.$user}{'skype'}) {
		$row_skype = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px;  padding-bottom: 5px;">
		<b>$profile_txt{'827'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<img src="$imagesdir/skype.gif" alt="" border="0" style="vertical-align: middle;" />
		<a href="javascript:void(window.open('callto://${$uid.$user}{'skype'}','skype','height=80,width=340,menubar=no,toolbar=no,scrollbars=no'))">$profile_txt{'827'} ${$uid.$user}{'realname'}</a>
		</div>
		~;
	}
	if (${$uid.$user}{'hidemail'} ne 'checked' || $iamadmin || !$allow_hide_email || $view) {
		my $rowEmail = '';
		if ($view) {
			if (${$uid.$user}{'hidemail'} ne 'checked') {
				$rowEmail = $profile_txt{'showingemail'};
			} else {
				my ($admtitle, undef) = split(/\|/, $Group{'Administrator'}, 2);
				$rowEmail = qq~$profile_txt{'notshowingemail'} $admtitle$profile_txt{'notshowingemailend'}~;
			}
		} else {
			$rowEmail = &enc_eMail("$profile_txt{'889'} ${$uid.$user}{'realname'}",${$uid.$user}{'email'},'','');
		}

		$row_email = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'69'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		$rowEmail
		</div>
		~;
	}
	if (${$uid.$user}{'weburl'} && ${$uid.$user}{'webtitle'}) {
		&ToChars(${$uid.$user}{'webtitle'});
		$row_website = qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
		<b>$profile_txt{'96'}: </b>
		</div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
		<a href="${$uid.$user}{'weburl'}" target="_blank">${$uid.$user}{'webtitle'}</a>
		</div>
		~;
	}
	if (${$uid.$user}{'signature'}) {
		# do some ubbc on the signature to display in the view profile area
		$message     = ${$uid.$user}{'signature'};
		$displayname = ${$uid.$user}{'realname'};

		if ($enable_ubbc) {
			if (!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; }
			&DoUBBC(1);
		}

		&ToChars($message);

		# Censor the signature.
		&LoadCensorList;
		$message = &Censor($message);

		$row_signature = qq~
		<tr>
		<td class="catbg" align="left">
			<img src="$imagesdir/profile.gif" alt="" border="0" style="vertical-align: middle;" />&nbsp;
			<span class="text1"><b>$profile_txt{'85'}</b></span>
		</td>
		</tr>
		<tr>
		<td align="left" class="windowbg2">
			<div style="float: left; width: 100%; padding-top: 8px; padding-bottom: 8px; overflow: auto;">
			$message
			</div>
		</td>
		</tr>
		~;

		if ($img_greybox) {
			$yyinlinestyle .= qq~<link href="$yyhtml_root/greybox/gb_styles.css" rel="stylesheet" type="text/css" />\n~;
			$yyjavascript .= qq~
var GB_ROOT_DIR = "$yyhtml_root/greybox/";
// -->
</script>
<script type="text/javascript" src="$yyhtml_root/AJS.js"></script>
<script type="text/javascript" src="$yyhtml_root/AJS_fx.js"></script>
<script type="text/javascript" src="$yyhtml_root/greybox/gb_scripts.js"></script>
<script type="text/javascript">
<!--~;
		}
	}

	# End empty field checking

	# Just maths below...
	$post_count = ${$uid.$user}{'postcount'};
	if (!$post_count) { $post_count = 0 }

	$string_regdate = &stringtotime(${$uid.$user}{'regdate'});
	$string_curdate = $date;

	if ($string_regdate < $forumstart) { $string_regdate = $forumstart }
	if ($string_curdate < $forumstart) { $string_curdate = $forumstart }

	$member_for_days = int(($string_curdate - $string_regdate) / 86400);

	if ($member_for_days < 1) { $tmpmember_for_days = 1; }
	else { $tmpmember_for_days = $member_for_days; }
	$post_per_day = sprintf("%.2f", ($post_count / $tmpmember_for_days));

	# End statistics.
	if (${$uid.$user}{'usertext'}) {
		&ToChars(${$uid.$user}{'usertext'});

		# Censor the usertext and wrap it
		&LoadCensorList;
		$showusertext = &WrapChars(&Censor(${$uid.$user}{'usertext'}),20);
	}

	$showProfile .= qq~
<table border="0" cellpadding="8" cellspacing="1" class="bordercolor" align="center" width="100%">
	~;
	if (!$view) {
		$yynavigation = qq~&rsaquo; $profile_txt{'92'}~;
		$showProfile .= qq~
	<tr>
		<td class="titlebg" width="100%" align="left">
			<div class="text1" style="float: left; width: 100%;">~;
			if ($iamadmin || $iamgmod) {
				$showProfile .= qq~
			<img src="$imagesdir/profile.gif" alt="" border="0" style="vertical-align: middle;" />&nbsp; <b>$profile_txt{'35'}: $INFO{'username'}</b>
				~;
			} else {
				$showProfile .= qq~
			<img src="$imagesdir/profile.gif" alt="" border="0" style="vertical-align: middle;" />&nbsp; <b>$profile_txt{'68'}: ${$uid.$INFO{'username'}}{'realname'}</b>
				~;
			}
			$showProfile .= qq~
			</div>
		</td>
	</tr>
		~;
	}
	$showProfile .= qq~
	<tr>
		<td class="windowbg" valign="middle">
			$pic_row
			<div style="float: left; width: 60%; padding-top: 5px;  padding-bottom: 5px;">
			<span style="font-size: 18px;">${$uid.$user}{'realname'}</span><br />
			$col_title{$user}
			$row_addgrp<br />
			$online<br />
			<span class="small">$showusertext</span>
			<span class="small">$buddybutton</span>
			</div>
			<div style="float: right; width: 19%; text-align: right;">
			$modify
			</div>
		</td>
	</tr>
	<tr>
		<td class="windowbg2" align="left" valign="top">
			<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
			<b>$profile_txt{'21'}: </b>
			</div>
			<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
			<b>${$uid.$user}{'postcount'}<br />$post_per_day</b> $profile_txt{'893'}
			</div>
			<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
			<b>$profile_txt{'233'}: </b>
			</div>
			<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
			$dr<br /><b>$member_for_days</b> $profile_txt{'894'}
			</div>
		</td>
	</tr>
	~;

	if ($row_gender || $row_age || $row_location) {
		$showProfile .= qq~
	<tr>
		<td class="windowbg2" align="left" valign="top">
		$row_gender
		$row_age
		$row_location
		</td>
	</tr>
		~;
	}
	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$showProfile .= &ext_viewprofile($user);
	}
	$showProfile .= qq~
	<tr>
		<td class="catbg" align="left">
			<img src="$imagesdir/profile.gif" alt="" border="0" style="vertical-align: middle;" />&nbsp;
			<span class="text1"><b>$profile_txt{'819'}</b></span>
		</td>
	</tr>
	<tr>
		<td class="windowbg2" align="left">
	~;

	&CheckUserPM_Level($user);
	if (!$view && $user ne $username && ($PM_level == 1 || ($PM_level == 2 && $UserPM_Level{$user} > 1 && ($iamadmin || $iamgmod || $iammod)) || ($PM_level == 3 && $UserPM_Level{$user} == 3 && ($iamadmin || $iamgmod)))) {
		$showProfile .= qq~
			<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;">
			<b>$profile_txt{'144'}: </b>
			</div>
			<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">
			<a href="$scripturl?action=imsend;to=$useraccount{$user}">$profile_txt{'688'} ${$uid.$user}{'realname'}</a>
			</div>
		~;
	}

	$showProfile .= qq~
			$row_email
			$row_website
			$row_aim
			$row_msn
			$row_skype
			$row_yim
			$row_gtalk
			$row_icq
		</td>
	</tr>
	~;

	$userlastlogin = &timeformat(${$uid.$user}{'lastonline'});
	$userlastpost = &timeformat(${$uid.$user}{'lastpost'});
	$userlastim = &timeformat(${$uid.$user}{'lastim'});
	if ($userlastlogin eq "") { $userlastlogin = "$profile_txt{'470'}"; }
	if ($userlastpost  eq "") { $userlastpost  = "$profile_txt{'470'}"; }
	if ($userlastim    eq "") { $userlastim    = "$profile_txt{'470'}"; }
	if (${$uid.$user}{'postcount'} > 100000) { ${$uid.$user}{'postcount'} = "$profile_txt{'683'}"; }
	my ($lastonline, $lastpost, $lastPM);
	## MF-B's code fix for lpd
	if (${$uid.$user}{'postcount'} > 0) {
		$userlastpost = &usersrecentposts(1);
	}
	####
	if (!$view) {
		$lastonline = qq~$profile_amv_txt{'9'}~;
		$lastpost = qq~$profile_amv_txt{'10'}~;
		$lastPM = qq~$profile_amv_txt{'11'}~;

	} else {
		$lastonline = qq~$profile_amv_txt{'mylastonline'}~;
		$lastpost = qq~$profile_amv_txt{'mylastpost'}~;
		$lastPM = qq~$profile_amv_txt{'mylastpm'}~;
	}
	$showProfile .= qq~
	$row_signature
	<tr>
		<td class="catbg" align="left">
			<img src="$imagesdir/profile.gif" alt="" border="0" style="vertical-align: middle;" />&nbsp;
			<span class="text1"><b>$profile_txt{'459'}</b></span>
		</td>
	</tr>
	<tr>
 	<td class="windowbg2" align="left">
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;"><b>$lastonline: </b></div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">$userlastlogin</div>
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;"><b>$lastpost:</b></div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">$userlastpost</div>
	~;
	if ($PM_level == 1 || ($PM_level == 2 && ($iamadmin || $iamgmod || $iammod)) || ($PM_level == 3 && ($iamadmin || $iamgmod))) {
		$showProfile .= qq~
		<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;"><b>$lastPM: </b></div>
		<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">$userlastim</div>
		~;
	}
	$showProfile .= qq~
	</td>
	</tr>
	~;

	if (($iamadmin || $iamgmod && $gmod_access2{'ipban3'}) && !$view && $user ne $username && ${$uid.$user}{'position'} ne 'Administrator') {
		$is_banned = &check_banlist("${$uid.$user}{'email'}","","$user");
		if ($is_banned =~/E/) { $ban_email_link = qq~<a href="$adminurl?action=ipban3;ban_email=${$uid.$user}{'email'};username=$useraccount{$user};unban=1"><span class="small">[$profile_txt{'904'}]</span></a>~; }
		else { $ban_email_link = qq~<a href="$adminurl?action=ipban3;ban_email=${$uid.$user}{'email'};username=$useraccount{$user}"><span class="small">[$profile_txt{'907'}]</span></a>~; }

		if ($is_banned =~/U/) { $ban_user_link = qq~<a href="$adminurl?action=ipban3;ban_memname=$useraccount{$user};username=$useraccount{$user};unban=1"><span class="small">[$profile_txt{'903'}]</span></a>~; }
		else { $ban_user_link = qq~<a href="$adminurl?action=ipban3;ban_memname=$useraccount{$user};username=$useraccount{$user}"><span class="small">[$profile_txt{'906'}]</span></a>~; }

		# Shows the banning stuff for IP's
		if (${$uid.$user}{'lastips'}) {
			($ip_one, $ip_two, $ip_three) = split (/\|/, ${$uid.$user}{'lastips'});

			if (&check_banlist("","$ip_one","")) { $banlink_one = qq~<a href="$adminurl?action=ipban3;ban=$ip_one;username=$useraccount{$user};unban=1"><span class="small">[$profile_txt{'905'}]</span></a>~; }
			else { $banlink_one = qq~<a href="$adminurl?action=ipban3;ban=$ip_one;username=$useraccount{$user}"><span class="small">[$profile_txt{'908'}]</span></a>~; }

			if (&check_banlist("","$ip_two","")) { $banlink_two = qq~<a href="$adminurl?action=ipban3;ban=$ip_two;username=$useraccount{$user};unban=1"><span class="small">[$profile_txt{'905'}]</span></a>~; }
			else { $banlink_two = qq~<a href="$adminurl?action=ipban3;ban=$ip_two;username=$useraccount{$user}"><span class="small">[$profile_txt{'908'}]</span></a>~; }

			if (&check_banlist("","$ip_three","")) { $banlink_three = qq~<a href="$adminurl?action=ipban3;ban=$ip_three;username=$useraccount{$user};unban=1"><span class="small">[$profile_txt{'905'}]</span></a>~; }
			else { $banlink_three = qq~<a href="$adminurl?action=ipban3;ban=$ip_three;username=$useraccount{$user}"><span class="small">[$profile_txt{'908'}]</span></a>~; }

			if ($ip_one) { $ip_ban_options = qq~$ip_one<br />$banlink_one <br />~; }
			if ($ip_two) { $ip_ban_options .= qq~$ip_two<br />$banlink_two <br />~; }
			if ($ip_three) { $ip_ban_options .= qq~$ip_three<br />$banlink_three <br />~; }
		}

		$showProfile .= qq~
		<tr>
	 	<td class="windowbg2" align="left">
			<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;"><b>$profile_txt{'902'}:</b></div>
			<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">$user<br />$ban_user_link</div>
			<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;"><b>$profile_txt{'69'}:</b></div>
			<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">${$uid.$user}{'email'}<br />$ban_email_link</div>
			<div style="float: left; clear: left; width: 30%; padding-top: 5px; padding-bottom: 5px;"><b>$profile_txt{'909'}:</b></div>
			<div style="float: left; width: 70%; padding-top: 5px; padding-bottom: 5px;">$ip_ban_options</div>
		</td>
		</tr>
		~;
	}

	if (${$uid.$user}{'postcount'} > 0 && $maxrecentdisplay > 0 && !$view) {
		$showProfile .= qq~
	<tr>
	<td class="windowbg2" align="left">
		<form action="$scripturl?action=usersrecentposts;username=$useraccount{$user}" method="post">
		$profile_txt{'460'} <select name="viewscount" size="1">
		<option value="5" selected="selected">5</option>
		<option value="10">10</option>
		<option value="15">15</option>
		<option value="$maxrecentdisplay">$maxrecentdisplay</option>
		</select> $profile_txt{'461'} ${$uid.$user}{'realname'}.
		<input type="submit" value="$profile_txt{'462'}" class="button" />
		</form>
	</td>
	</tr>~;
	}
	$showProfile .= qq~
</table>~;

	$yytitle = "$profile_txt{'92'} ${$uid.$user}{'realname'}";
	if (!$view) {
		$yymain .= qq~$showProfile~;
		&template;
	}
}

sub usersrecentposts {
	if ($iamguest) { &fatal_error("members_only"); }
	if ($INFO{'username'} =~ /\//) { &fatal_error("no_user_slash"); }
	if ($INFO{'username'} =~ /\\/) { &fatal_error("no_user_backslash"); }
	if (!-e ("$memberdir/$INFO{'username'}.vars")) { &fatal_error("no_profile_exists"); }
	&spam_protection if $action =~ /^(?:my)?usersrecentposts$/;

	my $curuser = $INFO{'username'};
	&LoadUser($curuser);

	my $display = $FORM{'viewscount'} ? $FORM{'viewscount'} : $_[0];
	if (!$display) { $display = 5; }
	elsif ($display =~ /\D/) { &fatal_error("only_numbers_allowed"); }
	if ($display > $maxrecentdisplay) { $display = $maxrecentdisplay; }

	my (%data, $numfound, %threadfound, %boardtxt, %recentthreadfound, $recentfound, $save_recent, $catperms, $boardperms, $curcat, %catname, %catboards, $openmemgr, @membergroups, $tmpa, %openmemgr, $curboard, @threads, @boardinfo, $i, $c, @messages, $tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $mns, $counter, $board, $notify);

	&Recent_Load($curuser);
	my @recent = sort { ${$recent{$b}}[1] <=> ${$recent{$a}}[1] } grep { ${$recent{$_}}[1] > 0 } keys %recent;
	my $recentcount = keys %recent;
	my @data;
	$#data = $display - 1;
	@data = map(0, @data);

	if (!$mloaded) { require "$boardsdir/forum.master"; }
	foreach $catid (@categoryorder) {
		foreach (split(/\,/, $cat{$catid})) {
			$catid{$_} = $catid;
			$catname{$_} = (split(/\|/, $catinfo{$catid}))[0];
		}
	}

	recentcheck: foreach $thread (@recent) {
		&MessageTotals("load",$thread);
		if (${$thread}{'board'} eq '') {
			$save_recent = 1;
			delete $recent{$thread};
			$recentcount--;
			next recentcheck;
		}
		$curboard = ${$thread}{'board'};

		if (!$boardtxt{$curboard}) {
			($boardname{$curboard}, $boardperms, undef) = split(/\|/, $board{$curboard});

			if (!$iamadmin && &AccessCheck($curboard,'',$boardperms) ne "granted") { $recentcount--; next recentcheck; }

			fopen(FILE, "$boardsdir/$curboard.txt");
			@{$boardtxt{$curboard}} = <FILE>;
			fclose(FILE);

			if (!@{$boardtxt{$curboard}}) {
				$save_recent = 1;
				delete $recent{$thread};
				$recentcount--;
				next recentcheck;
			}
		} elsif ($numfound) {
			$recentfound += $recentthreadfound{$thread} if exists $recentthreadfound{$thread};
			last if $recentfound >= $display;
			next;
		}

		for ($i = 0; $i < @{$boardtxt{$curboard}}; $i++) {
			($tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate) = split(/\|/, ${$boardtxt{$curboard}}[$i]);

			if (($display == 1 && $thread == $tnum) || ($display > 1 && exists $recent{$tnum})) {
				if ($tstate =~ /h/ && !$iamadmin && !$iamgmod) {
					$recentcount--;
				} else {
					fopen(FILE, "$datadir/$tnum.txt");
					@messages = <FILE>;
					fclose(FILE);

					my $usercheck = 0;

					for ($c = $#messages; $c >= 0 ; $c--) {
						($msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns) = split(/\|/, $messages[$c]);

						if ($curuser eq $musername) {
							my @i = @data;
							push(@i, $mdate);
							@data = sort { $b <=> $a } @i;
							if (pop(@data) != $mdate) {
								chomp $mns;
								$data{$mdate} = [$curboard, $tnum, $c, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $tstate];
								if (!$usercheck) {
									$numfound++;
									$threadfound{$tnum} = 1;
								}
								if (exists $recent{$tnum}) {
									$recentthreadfound{$tnum}++;
									$recentfound++ if $thread == $tnum;
								}
								last recentcheck if $display == $recentfound;
								if (${$recent{$tnum}}[1] < $mdate) {
									$save_recent = 1;
									${$recent{$tnum}}[1] = $mdate;
								}
							}
							$usercheck = 1;
						}
					}
					if (!$usercheck) {
						$save_recent = 1;
						delete $recent{$tnum};
						$recentcount--;
					}
				}
			}
		}
	}

	if ($recentfound < $display && $numfound < $recentcount) {
		categorycheck: foreach $catid (@categoryorder) {
			(undef, $catperms) = split(/\|/, $catinfo{$catid});
			if (!&CatAccess($catperms)) { next categorycheck; }

			boardcheck: foreach $curboard (split(/\,/, $cat{$catid})) {
				if (!$boardtxt{$curboard}) {
					($boardname{$curboard}, $boardperms, undef) = split(/\|/, $board{$curboard});

					if (!$iamadmin && &AccessCheck($curboard,'',$boardperms) ne "granted") { next boardcheck; }

					fopen(FILE, "$boardsdir/$curboard.txt") || next boardcheck;
					@{$boardtxt{$curboard}} = <FILE>;
					fclose(FILE);
				}

				for ($i = 0; $i < @{$boardtxt{$curboard}}; $i++) {
					($tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate) = split(/\|/, ${$boardtxt{$curboard}}[$i]);

					if (exists($recent{$tnum}) && !exists $threadfound{$tnum}) {
						unless ($tstate =~ /h/ && !$iamadmin && !$iamgmod) {
							fopen(FILE, "$datadir/$tnum.txt");
							@messages = <FILE>;
							fclose(FILE);

							my $usercheck = 0;

							for ($c = $#messages; $c >= 0 ; $c--) {
								($msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns) = split(/\|/, $messages[$c]);

								if ($curuser eq $musername) {
									my @i = @data;
									push(@i, $mdate);
									@data = sort { $b <=> $a } @i;
									if (pop(@data) != $mdate) {
										chomp $mns;
										$data{$mdate} = [$curboard, $tnum, $c, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $tstate];
										if (${$recent{$tnum}}[1] < $mdate) {
											$save_recent = 1;
											${$recent{$tnum}}[1] = $mdate;
										}
									}
									$usercheck = 1;
								}
							}

							if (!$usercheck) {
								$save_recent = 1;
								delete $recent{$tnum};
							}
						}
					}
				}
			}
		}
	}

	undef %boardtxt;

	&Recent_Save($curuser) if $save_recent;

	if ($display == 1) {
		return if !$data[0];
		($board, $tnum, $c, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $tstate) = @{$data{$data[0]}};
		&ToChars($msub);
		($msub, undef) = &Split_Splice_Move($msub,0);
		return (&timeformat($mdate) . qq~<br />$profile_txt{'view'} &rsaquo; <a href="$scripturl?num=$tnum/$c#$c">$msub</a>~);
	}

	&LoadCensorList;

	for ($i = 0; $i < @data; $i++) {
		next if !$data[$i];

		($board, $tnum, $c, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $tstate) = @{$data{$data[$i]}};
		($msub, undef) = &Split_Splice_Move($msub,0);
		&wrap;
		$displayname = $mname;
		($message, undef) = &Split_Splice_Move($message,$tnum);
		if ($enable_ubbc) {
			$ns = $mns;
			if (!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; }
			&DoUBBC;
		}
		&wrap2;
		&ToChars($msub);
		&ToChars($message);
		$msub = &Censor($msub);
		$message = &Censor($message);
		&ToChars($catname{$board});
		&ToChars($boardname{$board});

		$mdate = &timeformat($mdate);

		$counter++;

		$showProfile .= qq~
<table border="0" width="100%" cellspacing="1" class="bordercolor" style="table-layout: fixed;">
	<tr>
		<td align="center" width="5%" class="titlebg">$counter</td>
		<td align="left" width="95%" class="titlebg">&nbsp;<a href="$scripturl?catselect=$catid{$board}"><u>$catname{$board}</u></a> / <a href="$scripturl?board=$board"><u>$boardname{$board}</u></a> / <a href="$scripturl?num=$tnum/$c#$c"><u>$msub</u></a><br />
		&nbsp;<span class="small">$profile_txt{'30'}: $mdate</span>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">
			<table border="0" width="100%" class="catbg">
				<tr>
					<td align="left">$maintxt{'109'} $tname | $maintxt{'197'} $mname</td>
					<td align="right">&nbsp;~;

		if ($tstate != 1) {
			if (${$uid.$username}{'thread_notifications'} =~ /\b$tnum\b/) {
				$notify = qq~$menusep<a href="$scripturl?action=notify3;num=$tnum/$c">$img{'del_notify'}</a>~;
			} else {
				$notify = qq~$menusep<a href="$scripturl?action=notify2;num=$tnum/$c">$img{'add_notify'}</a>~;
			}
			$showProfile .= qq~<a href="$scripturl?board=$board;action=post;num=$tnum/$c#$c;title=PostReply">$img{'reply'}</a>$menusep<a href="$scripturl?board=$board;action=post;num=$tnum;quote=$c;title=PostReply">$img{'recentquote'}</a>$notify &nbsp;~;
		}

		$showProfile .= qq~
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td align="left" height="80" colspan="2" class="windowbg2" valign="top"><div style="float: left; width: 99%; overflow: auto;">$message</div></td>
	</tr>
</table><br />~;
	}

	if (!$counter) { 
		$showProfile .= qq~<span class="text1"><b>$profile_txt{'755'}</b></span>~;
	} elsif (!$view) {
		$showProfile .= qq~<p align=left><a href="$scripturl?action=viewprofile;username=$useraccount{$curuser}"><b>$profile_txt{'92'} ${$uid.$curuser}{'realname'}</b></a></p>~;
	}

	if ($img_greybox) {
		$yyinlinestyle .= qq~<link href="$yyhtml_root/greybox/gb_styles.css" rel="stylesheet" type="text/css" />\n~;
		$yyjavascript .= qq~
var GB_ROOT_DIR = "$yyhtml_root/greybox/";
// -->
</script>
<script type="text/javascript" src="$yyhtml_root/AJS.js"></script>
<script type="text/javascript" src="$yyhtml_root/AJS_fx.js"></script>
<script type="text/javascript" src="$yyhtml_root/greybox/gb_scripts.js"></script>
<script type="text/javascript">
<!--~;
	}

	$yytitle = "$profile_txt{'458'} ${$uid.$curuser}{'realname'}";
	if (!$view) {
		$yynavigation = qq~&rsaquo; $maintxt{'213'}~;
		$yymain .= $showProfile;
		&template;
	}
}

sub DrawGroups {
	my ($availgroups) = @_[0];
	my ($userpos)     = @_[1];
	my @groups, $foundit, %found, $groupsel, $groupsel2, $name;
	%found = ();
	if ($availgroups eq "") { $availgroups = "xk8yj56ndkal"; }
	(@groups) = split(/\,/, $availgroups);
	$groupsel  = "\n";
	$groupsel2 = "";
	$count     = 0;
	foreach $curgroup (@groups) {
		$foundit = 0;
		chomp $curgroup;
		if ($foundit != 1 || $count == $#groups) {
			$k = 0;
			foreach my $key (@nopostorder) {
				chomp $key;
				($name, undef) = split(/\|/, $NoPost{$key}, 2);
				if ($key ne $userpos) {
					if ($curgroup eq $key) {
						$foundit = 1;
						$found{$key} = 1;
						$groupsel .= qq~<option value="$key" selected="selected">$name</option>\n~;
					}
					if ($found{$key} != 1 && $count == $#groups) { $groupsel2 .= qq~<option value="$key">$name</option>\n~; }
					$k++;
				}
			}
		}
		$count++;
	}
	$groupsel .= $groupsel2;
	$groupsel .= "</select>";
	return $groupsel;
}

# Check that profile-editing session is still valid
sub SidCheck {
	my $cur_sid = &decloak($INFO{'sid'});
	my $sid_check = substr($date, 5, 5);
	if ($sid_check <= 600 && $cur_sid >= 99400) { $sid_check += 100000; }

	$sid_expires = $cur_sid + 600 - $sid_check;

	&ProfileCheck($_[0]) if $sid_expires < 0 || $cur_sid > $sid_check;
}

1;