<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.


Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// Edit Profile
	// ========================================================================================

	$PageTitle = $MsgText[0][78];
	$BrowserTitle = $MsgText[0][78];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[0][78];
	
	
	if ($LoggedIn == 'y') {
	
		ReadFormArguments();
		Global $FormArguments;
		
		if (count($FormArguments) > 0) {
		
		ReadPermissions();
		Global $Permissions;
		
				
		
				
		$query = "select * from `".$Settings['DBPrefix']."core-Users` where Username='".$FormArguments['UsernameToEdit']."' limit 1";
	   	$result = mysql_query($query) or die (mysql_error());
		$row = mysql_fetch_array($result);
		
		if ($FormArguments['UsernameToEdit'] == $CookieUsername || ($Permissions[5] == 'y' && $row['Level'] <= $UserLevel)) {
		
		
			$FileData = $_FILES['FileData']['tmp_name'];
			$FileSize = $_FILES['FileData']['size'];
			$FileType = $_FILES['FileData']['type'];
			
			$ImageInfo = @getimagesize($FileData);
				
			$ImageWidth = $ImageInfo[0];
			$ImageHeight = $ImageInfo[1];
			$FileType = $ImageInfo[2];
		
			$TypeExtensions[1] = '.gif';
			$TypeExtensions[2] = '.jpg';
			$TypeExtensions[3] = '.png';
		
		
			$DestinationPath = './images/user_avatars/';
			
			if ($FileData != '') {
				// uploading custom avatar
				if (($FileType == 1 || $FileType == 2 || $FileType == 3) && $ImageWidth <= 100 && $ImageHeight <= 100) {
					move_uploaded_file($FileData, $DestinationPath.$row['RecordNumber'].'_avatar'.$TypeExtensions[$FileType]);
					
					$SaveChangesQuery .= ', ForumAvatar=\'\', CustomAvatar=\''.$row['RecordNumber'].'_avatar'.$TypeExtensions[$FileType].'\'';
				}
				else {
					$SaveChangesQuery .= ', ForumAvatar=\'\', CustomAvatar=\'\'';
				}
			}		
			else if ($FormArguments['ForumAvatar'] != '' && $FormArguments['ForumAvatar'] != '_') {
				// selecting existing community avatar
				
				if ($dir = @opendir('./images/avatars')) {
					while (($file = readdir($dir)) !== false) {
						if (!is_dir('./templates/'.$file) && $file != '.' && $file != '..' && $file != '_') {
							$ActiveAvatars[$file] = true;
		  				}
					}
					closedir($dir);
				}
				
				
				if ($ActiveAvatars[$FormArguments['ForumAvatar']] == true) {
					$SaveChangesQuery .= ', ForumAvatar=\''.$FormArguments['ForumAvatar'].'\'';
				}
				else {
					$SaveChangesQuery .= ', ForumAvatar=\'\'';
				}
					
			}
			else if ($FormArguments['ForumAvatar'] == '_') {
				// switching back to saved custom avatar
				$SaveChangesQuery .= ', ForumAvatar=\'\'';
			}
			else if ($FormArguments['ForumAvatar'] == '') {
				// "no avatar" selected -- set both to empty and delete any files
				$SaveChangesQuery .= ', ForumAvatar=\'\', CustomAvatar=\'\'';
				
				@unlink($DestinationPath.$row['RecordNumber'].'_avatar'.$TypeExtensions[1]);
				@unlink($DestinationPath.$row['RecordNumber'].'_avatar'.$TypeExtensions[2]);
				@unlink($DestinationPath.$row['RecordNumber'].'_avatar'.$TypeExtensions[3]);
			}
		
		
		
			function TestSignatureImage($SignatureImageURL) {
			
				Global $Settings;
				$query = "select * from `".$Settings['DBPrefix']."mb-Settings`";
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
					$mbSettings[$row['Name']] = $row['Value'];
				}
				
				if ($mbSettings['SignatureImageMaxWidth'] < 1) {
					$mbSettings['SignatureImageMaxWidth'] = 500;
				}
				if ($mbSettings['SignatureImageMaxHeight'] < 1) {
					$mbSettings['SignatureImageMaxHeight'] = 200;
				}
			
				if ($SigImageInfo = @getimagesize($SignatureImageURL)) {
					$ImageWidth = $SigImageInfo[0];
					$ImageHeight = $SigImageInfo[1];
			
					if ($ImageWidth <= $mbSettings['SignatureImageMaxWidth'] && $ImageHeight <= $mbSettings['SignatureImageMaxHeight']) {
						return $SignatureImageURL;			
					}
					else {
						Global $ImagesURL;
						return $ImagesURL.'/spacer.gif';
					}
			
				}
				else {
					return $SignatureImageURL;
				}
			}
			
			
			$FormArguments['ForumSignature'] = preg_replace("/(\[img\])(.*?)(\[\/img\])/ie", "'\[img\]'.TestSignatureImage('\\2').'\[\/img\]'", $FormArguments['ForumSignature']);
		
		
			$SaveChangesQuery .= ', ForumSignature=\''.$FormArguments['ForumSignature'].'\'';
		
		
		
		
		
		
			if ($FormArguments['DeleteAccount'] == 'delete') {
				// if the account is to be deleted
			
				$query = "delete from `".$Settings['DBPrefix']."core-Users` where Username='".$FormArguments['UsernameToEdit']."' limit 1";
	  		 	$result = mysql_query($query) or die (mysql_error());
				
				$query = "delete from `".$Settings['DBPrefix']."core-RecentActivity` where User='".$FormArguments['UsernameToEdit']."' limit 1";
	  		 	$result = mysql_query($query) or die (mysql_error());
				
				if ($FormArguments['UsernameToEdit'] == $CookieUsername) {
					header('Location: ' . $IndexURL);
				}
				else {
					$PageContents = $MsgText[0][93];
				}
			}
			else {
				// if not deleting the account
				
				$FinalPassword = $row['Password'];
				
				if ($FormArguments['Email'] != '') {
		
				if ($FormArguments['Password1'] != '') {
					if ($FormArguments['Password1'] == $FormArguments['Password2']) {
						
						$FinalPassword = '';
						// encrypt the password, correcting for the fact that php only encrypts the first eight characters
						$EncryptedUsername = crypt($FormArguments['Username'],substr($FormArguments['Username'],0,2));
						$EncryptedUsername = substr($EncryptedUsername,0,8);
						$LoopCount = ceil(strlen($FormArguments['Password1'])/8);
						for ($i = 1; $i <= $LoopCount; $i++) {
			 				$FinalPassword .= crypt(substr($FormArguments['Password1'],($i*8)-8,8),substr($FormArguments['Password1'],0,2));
						}
						$FinalPassword = $EncryptedUsername.$FinalPassword;
						
						$PasswordPart = ', Password=\''.$FinalPassword.'\'';
						
						$SaveChanges = 'y';
						
					}
					else {
						$PageContents .= '<br><br>'.$MsgText[0][94];
					} 
				}
				else {
					$SaveChanges = 'y';
				}
				
				
				
				if ($FormArguments['NewUserLevel'] >= 1 && $FormArguments['NewUserLevel'] <= $UserLevel && $Permissions[4] == 'y') {
					$NewUserLevelPart = ', Level=\''.intval($FormArguments['NewUserLevel']).'\'';
				}
								
			
			
				$RegFormOpts = explode('%%', $Settings['RegFormOpts']);
				
				$FormKeys = array('FirstName', 'LastName', 'OrganizationCompany', 'Address1', 'Address2', 'Address3', 'Address4', 'City', 'StateProvince', 'ZipPostal', 'Country', 'Phone', 'Fax', 'Cell', 'Pager', 'Website', 'ReferredBy', 'Custom1', 'Custom2', 'Custom3');
				
				for ($i = 3; $i < count($RegFormOpts); $i++) {
				
					if ($RegFormOpts[$i] == 'o' || $RegFormOpts[$i] == 'r') {
						$SaveChangesQuery .= ', '.$FormKeys[$i-3].'=\''.$FormArguments[$FormKeys[$i-3]].'\'';
					}
				
				}
			
			
				if ($FormArguments['UsernameToEdit'] == $FormArguments['Username'] || $Settings['AllowUsernameChanges'] != 'y') {
					// username hasn't changed, or isn't allowed to
				
					if ($SaveChanges == 'y') {									
						//$query = "update `".$Settings['DBPrefix']."core-Users` set DisplayName='".$FormArguments['DisplayName']."', Email='".$FormArguments['Email']."', DisplayEmail='".$FormArguments['DisplayEmail']."', RealName='".$FormArguments['RealName']."', Location='".$FormArguments['Location']."'".$PasswordPart." where Username='".$FormArguments['UsernameToEdit']."' LIMIT 1";
						//$query = "update `".$Settings['DBPrefix']."core-Users` set DisplayName='".$FormArguments['DisplayName']."', Email='".$FormArguments['Email']."', DisplayEmail='".$FormArguments['DisplayEmail']."', RealName='".$FormArguments['RealName']."', Location='".$FormArguments['Location']."', FirstName='".$FormArguments['FirstName']."', LastName='".$FormArguments['LastName']."', OrganizationCompany='".$FormArguments['OrganizationCompany']."', Address1='".$FormArguments['Address1']."', Address2='".$FormArguments['Address2']."', Address3='".$FormArguments['Address3']."', Address4='".$FormArguments['Address4']."', City='".$FormArguments['City']."', StateProvince='".$FormArguments['StateProvince']."', ZipPostal='".$FormArguments['ZipPostal']."', Country='".$FormArguments['Country']."', Phone='".$FormArguments['Phone']."', Fax='".$FormArguments['Fax']."', Cell='".$FormArguments['Cell']."', Pager='".$FormArguments['Pager']."', Website='".$FormArguments['Website']."', ReferredBy='".$FormArguments['ReferredBy']."', Custom1='".$FormArguments['Custom1']."', Custom2='".$FormArguments['Custom2']."', Custom3='".$FormArguments['Custom3']."'".$NewUserLevelPart.$PasswordPart." where Username='".$FormArguments['UsernameToEdit']."' LIMIT 1";
						$query = "update `".$Settings['DBPrefix']."core-Users` set DisplayName='".$FormArguments['DisplayName']."', Email='".$FormArguments['Email']."', DisplayEmail='".$FormArguments['DisplayEmail']."', RealName='".$FormArguments['RealName']."', Location='".$FormArguments['Location']."'".$SaveChangesQuery.$NewUserLevelPart.$PasswordPart.", Verified='y' where Username='".$FormArguments['UsernameToEdit']."' LIMIT 1";
						$result = mysql_query($query) or die (mysql_error());
					}
											
				}
				else {
											
					// username has changed, so check to make sure the new one doesn't already exist
					$query = "select Username from `".$Settings['DBPrefix']."core-Users` where Username='".$FormArguments['Username']."' LIMIT 1";
					$result = mysql_query($query) or die (mysql_error());
					$row = mysql_fetch_array($result);
					
					if ((mysql_num_rows($result) == 0 || strtolower($FormArguments['Username']) == strtolower($row['Username'])) && $FormArguments['Username'] != '') {
					
						if ($SaveChanges == 'y') {
							
							$EncryptedUsername = crypt($FormArguments['Username'],substr($FormArguments['Username'],0,2));
							$EncryptedUsername = substr($EncryptedUsername,0,8);
						
							$FinalPassword = $EncryptedUsername.substr($FinalPassword,8);
						
							$PasswordPart = ', Password=\''.$FinalPassword.'\'';
							
						
							//$query = "update `".$Settings['DBPrefix']."core-Users` set DisplayName='".$FormArguments['DisplayName']."', Email='".$FormArguments['Email']."', Username='".$FormArguments['Username']."', DisplayEmail='".$FormArguments['DisplayEmail']."', RealName='".$FormArguments['RealName']."', Location='".$FormArguments['Location']."'".$PasswordPart." where Username='".$FormArguments['UsernameToEdit']."' LIMIT 1";
							//$query = "update `".$Settings['DBPrefix']."core-Users` set DisplayName='".$FormArguments['DisplayName']."', Email='".$FormArguments['Email']."', DisplayEmail='".$FormArguments['DisplayEmail']."', RealName='".$FormArguments['RealName']."', Location='".$FormArguments['Location']."', FirstName='".$FormArguments['FirstName']."', LastName='".$FormArguments['LastName']."', OrganizationCompany='".$FormArguments['OrganizationCompany']."', Address1='".$FormArguments['Address1']."', Address2='".$FormArguments['Address2']."', Address3='".$FormArguments['Address3']."', Address4='".$FormArguments['Address4']."', City='".$FormArguments['City']."', StateProvince='".$FormArguments['StateProvince']."', ZipPostal='".$FormArguments['ZipPostal']."', Country='".$FormArguments['Country']."', Phone='".$FormArguments['Phone']."', Fax='".$FormArguments['Fax']."', Cell='".$FormArguments['Cell']."', Pager='".$FormArguments['Pager']."', Website='".$FormArguments['Website']."', ReferredBy='".$FormArguments['ReferredBy']."', Custom1='".$FormArguments['Custom1']."', Custom2='".$FormArguments['Custom2']."', Custom3='".$FormArguments['Custom3']."'".$NewUserLevelPart.$PasswordPart." where Username='".$FormArguments['UsernameToEdit']."' LIMIT 1";
							$query = "update `".$Settings['DBPrefix']."core-Users` set Username='".$FormArguments['Username']."', DisplayName='".$FormArguments['DisplayName']."', Email='".$FormArguments['Email']."', DisplayEmail='".$FormArguments['DisplayEmail']."', RealName='".$FormArguments['RealName']."', Location='".$FormArguments['Location']."'".$SaveChangesQuery.$NewUserLevelPart.$PasswordPart.", Verified='y' where Username='".$FormArguments['UsernameToEdit']."' LIMIT 1";
							$result = mysql_query($query) or die (mysql_error());
						}
																	
					}
					else {
						
						$PageContents .= '<br><br>'.$MsgText[0][95];
			
					}
					
				} // end if username has changed
			
			
				if ($PageContents == '') {
			
					$NewUsername = $CookieUsername;
				
					if ($FormArguments['Username'] != $FormArguments['UsernameToEdit'] || $row['DisplayName'] != $FormArguments['DisplayName']) {
						$query = "update `".$Settings['DBPrefix']."core-RecentActivity` set User='".$FormArguments['Username']."', Name='".$FormArguments['DisplayName']."', LastLocation='/profile-edit' where User='".$FormArguments['UsernameToEdit']."' LIMIT 1";
						$result = mysql_query($query) or die (mysql_error());
					}
					
					if ($FormArguments['UsernameToEdit'] == $CookieUsername) {
						if ($FormArguments['Username'] != $FormArguments['UsernameToEdit']) {
							$UpdateCookie = 'y';
							$NewUsername = $FormArguments['Username'];							
						}
						if ($Password1 != '' || $FormArguments['Password1'] != '') {
							$UpdateCookie = 'y';
						}
					}
					
					
					if ($UpdateCookie == 'y') {
					
						ob_start();
					
							$CookieData = $NewUsername . '==' . $FinalPassword;
							
							setcookie ($CookieName, $CookieData, 0, '/');
							
							header('Location: ' . $MainURL . '/profile/'.$FormArguments['Username'].'.html');
							
						ob_end_flush();
					
					}
					else {
						
						header('Location: ' . $MainURL . '/profile/'.$FormArguments['Username'].'.html');
						
					}
					
				} // end if page contents is empty (no errors)
				
				} // end if e-mail entered
				else {
				
					$PageContents = $MsgText[0][96];
				
				}
				
			}// end if not deleting the account
				
		} // end if authorized to edit the profile
		else {
		
			$PageContents = $MsgText[0][97];
		
		}
		
		
		
		}
		else {
			$PageContents = $MsgText[0][98];
		}
		
			
	}
	else {
	
		RedirectToLogin();
		
	}				

	

	// ========================================================================================




?>
