<?php









if (!defined('WEDGE'))
	die('Hacking attempt...');














function ModifyProfile($post_errors = array())
{
	global $txt, $context, $user_profile, $cur_profile;
	global $settings, $memberContext, $profile_vars, $post_errors;

	if (AJAX && isset($_GET['prv'], $_GET['pa']))
	{
		checkSession('get');


		$prvs = array_flip(explode(',', $_GET['prv']));
		if (isset($prvs[PRIVACY_DEFAULT]))
			$_GET['prv'] = PRIVACY_DEFAULT;
		elseif (isset($prvs[PRIVACY_MEMBERS]))
			$_GET['prv'] = PRIVACY_MEMBERS;
		elseif (isset($prvs[PRIVACY_AUTHOR]) || isset($prvs['null']))
			$_GET['prv'] = PRIVACY_AUTHOR;

		loadCustomFields(we::$id);
		$allowed = array('age', 'location', 'action', 'time', 'language', 'registered', 'login');

		call_hook('profile_privacy', array(&$allowed));
		foreach ($context['custom_fields'] as $field)
			$allowed[] = 'custom_' . $field;
		if (preg_match('~^[-\d][-\d,]*$~', $_GET['prv']) && in_array($_GET['pa'], $allowed))
			updateMyData(array('privacy' => array_merge(isset(we::$user['data']['privacy']) ? we::$user['data']['privacy'] : array(), array($_GET['pa'] => $_GET['prv']))));
		return_raw($_GET['prv']);
	}


	if (empty($post_errors))
		loadLanguage('Profile');
	loadTemplate('Profile');
	add_js_file('profile.js');


	if (isset($_REQUEST['guest']))
	{
		loadSource('Profile-View');
		wetem::load('showPosts');

		$context['profile_menu_name'] = 'dummy_menu';
		we::$user['is_owner'] = allowedTo('moderate_forum');
		showPosts(0);
		return;
	}

	loadSource('Subs-Menu');


	if (isset($_REQUEST['user']))
		$memberResult = loadMemberData($_REQUEST['user'], true, 'profile');

	elseif (!empty($_REQUEST['u']))
		$memberResult = loadMemberData((int) $_REQUEST['u'], false, 'profile');

	else
		$memberResult = loadMemberData(MID, false, 'profile');


	if (!is_array($memberResult))
		fatal_lang_error('not_a_user', false);


	list ($memID) = $memberResult;
	$context['id_member'] = $memID;
	$cur_profile = $user_profile[$memID];


	loadMemberContext($memID);
	$context['member'] = $memberContext[$memID];


	we::$user['is_owner'] = $memID == MID;





















	$temp = boardsAllowedTo('save_post_draft');

	$profile_areas = array(
		'info' => array(
			'title' => $txt['profileInfo'],
			'areas' => array(
				'summary' => array(
					'label' => $txt['summary'],
					'file' => 'Profile-View',
					'function' => 'summary',
					'permission' => array(
						'own' => 'profile_view_own',
						'any' => 'profile_view_any',
					),
				),
				'',
				'showdrafts' => array(
					'label' => $txt['showDrafts'],
					'file' => 'Profile-View',
					'function' => 'showDrafts',
					'enabled' => !empty($settings['masterSavePostDrafts']) && !empty($temp),
					'permission' => array(
						'own' => 'profile_view_own',
						'any' => array(),
					),
				),
				'thoughts' => array(
					'label' => $txt['showThoughts'],
					'file' => 'Thoughts',
					'function' => 'latestThoughts',
					'enabled' => true,
					'permission' => array(
						'own' => 'profile_view_own',
						'any' => 'profile_view_any',
					),
				),
				'showposts' => array(
					'label' => $txt['showPosts'],
					'file' => 'Profile-View',
					'function' => 'showPosts',
					'subsections' => array(
						'messages' => array($txt['showMessages']),
						'topics' => array($txt['showTopics']),
						'attach' => array($txt['showAttachments'], 'enabled' => !empty($settings['attachmentEnable'])),
					),
					'permission' => array(
						'own' => 'profile_view_own',
						'any' => 'profile_view_any',
					),
				),
				'statistics' => array(
					'label' => $txt['statPanel'],
					'file' => 'Profile-View',
					'function' => 'statPanel',
					'permission' => array(
						'own' => 'profile_view_own',
						'any' => 'profile_view_any',
					),
				),
			),
		),
		'edit_profile' => array(
			'title' => $txt['profileEdit'],
			'areas' => array(
				'forumprofile' => array(
					'label' => $txt['forumprofile'],
					'file' => 'Profile-Modify',
					'function' => 'forumProfile',
					'sc' => 'post',
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own', 'profile_title_own', 'profile_title_any'),
						'any' => array('profile_extra_any', 'profile_title_any'),
					),
				),
				'options' => array(
					'label' => $txt['options'],
					'file' => 'Profile-Modify',
					'function' => 'options',
					'sc' => 'post',
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
						'any' => array('profile_extra_any'),
					),
				),
				'',
				'skin' => array(
					'label' => $txt['change_skin'],
					'enabled' => !empty($settings['theme_allow']) || allowedTo('admin_forum'),
					'custom_url' => '<URL>?action=skin;u=' . $memID,
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
						'any' => array('profile_extra_any'),
					),
				),
				'',
				'notifications' => array(
					'label' => $txt['notifications_all'],
					'file' => 'Notifications',
					'function' => 'weNotif::profile',
					'sc' => 'post',
					'subsections' => array(
						'general' => array($txt['notifications_general']),
						'posts' => array($txt['notifications_posts']),
					),
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
					),
				),
				'notifsubs' => array(
					'label' => $txt['notif_subs'],
					'function' => 'weNotif::subs_profile',
					'file' => 'Notifications',
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
					),
				),

				'pmprefs' => array(
					'label' => $txt['pmprefs'],
					'file' => 'Profile-Modify',
					'function' => 'pmprefs',
					'enabled' => allowedTo(array('profile_extra_own', 'profile_extra_any')),
					'sc' => 'post',
					'permission' => array(
						'own' => array('pm_read'),
						'any' => array('profile_extra_any'),
					),
				),
				'ignoreboards' => array(
					'label' => $txt['ignoreboards'],
					'file' => 'Profile-Modify',
					'function' => 'ignoreboards',
					'enabled' => !empty($settings['ignorable_boards']),
					'sc' => 'post',
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
						'any' => array('profile_extra_any'),
					),
				),
				'contacts' => array(
					'label' => $txt['buddies'],
					'file' => 'Profile-Modify',
					'function' => 'editContacts',
					'enabled' => !empty($settings['enable_buddylist']),
					'sc' => 'post',
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
						'any' => array('profile_extra_any'),
					),
				),
				'lists' => array(
					'label' => $txt['editBuddyIgnoreLists'],
					'file' => 'Profile-Modify',
					'function' => 'editBuddyIgnoreLists',
					'enabled' => false,
					'sc' => 'post',
					'subsections' => array(
						'buddies' => array($txt['editBuddies']),
						'ignore' => array($txt['editIgnoreList']),
					),
					'permission' => array(
						'own' => array('profile_extra_any', 'profile_extra_own'),
						'any' => array(),
					),
				),
			),
		),
		'aeva' => array(
			'title' => $txt['media_gallery'],
			'areas' => array(
				'aeva' => array(
					'file' => 'media/Aeva-Gallery2',
					'function' => 'aeva_profileSummary',
					'label' => $txt['media_profile_sum'],
					'permission' => array(
						'own' => array('media_viewprofile'),
						'any' => array('media_viewprofile'),
					),
					'load_member' => true,
				),
				'aevaitems' => array(
					'file' => 'media/Aeva-Gallery2',
					'function' => 'aeva_profileItems',
					'label' => $txt['media_view_items'],
					'permission' => array(
						'own' => array('media_viewprofile'),
						'any' => array('media_viewprofile'),
					),
					'load_member' => true,
				),
				'aevacoms' => array(
					'file' => 'media/Aeva-Gallery2',
					'function' => 'aeva_profileComments',
					'label' => $txt['media_view_coms'],
					'permission' => array(
						'own' => array('media_viewprofile'),
						'any' => array('media_viewprofile'),
					),
					'load_member' => true,
				),
				'aevavotes' => array(
					'file' => 'media/Aeva-Gallery2',
					'function' => 'aeva_profileVotes',
					'label' => $txt['media_view_votes'],
					'permission' => array(
						'own' => array('media_viewprofile'),
						'any' => array('media_viewprofile'),
					),
					'load_member' => true,
				),
			),
		),
		'profile_manage' => array(
			'title' => $txt['manage_account'],
			'areas' => array(
				'sendpm' => array(
					'label' => $txt['profileSendIm'],
					'custom_url' => '<URL>?action=pm;sa=send;u=' . $memID,
					'permission' => array(
						'own' => array(),
						'any' => array('pm_send'),
					),
				),
				'account' => array(
					'label' => $txt['account'],
					'file' => 'Profile-Modify',
					'function' => 'account',
					'enabled' => we::$is_admin || ($cur_profile['id_group'] != 1 && !in_array(1, explode(',', $cur_profile['additional_groups']))),
					'sc' => 'post',
					'password' => true,
					'permission' => array(
						'own' => array('profile_identity_any', 'profile_identity_own', 'manage_membergroups'),
						'any' => array('profile_identity_any', 'manage_membergroups'),
					),
				),
				'subscriptions' => array(
					'label' => $txt['subscriptions'],
					'file' => 'Profile-Actions',
					'function' => 'subscriptions',
					'enabled' => !empty($settings['paid_enabled']),
					'permission' => array(
						'own' => array('profile_view_own'),
						'any' => array('moderate_forum'),
					),
				),
				'groupmembership' => array(
					'label' => $txt['groupmembership'],
					'file' => 'Profile-Modify',
					'function' => 'groupMembership',
					'enabled' => !empty($settings['show_group_membership']) && we::$user['is_owner'],
					'sc' => 'request',
					'permission' => array(
						'own' => array('profile_view_own'),
						'any' => array('manage_membergroups'),
					),
				),
				'',
				'permissions' => array(
					'label' => $txt['showPermissions'],
					'file' => 'Profile-View',
					'function' => 'showPermissions',
					'permission' => array(
						'own' => 'manage_permissions',
						'any' => 'manage_permissions',
					),
				),
				'tracking' => array(
					'label' => $txt['trackUser'],
					'file' => 'Profile-View',
					'function' => 'tracking',
					'subsections' => array(
						'activity' => array($txt['trackActivity'], 'manage_bans'),
						'ip' => array($txt['trackIP'], 'manage_bans'),
						'edits' => array($txt['trackEdits'], 'moderate_forum'),
						'reported' => array($txt['trackReported'], 'enabled' => !empty(we::$user['mod_cache']) && we::$user['mod_cache']['bq'] != '0=1'),
					),
					'permission' => array(
						'own' => 'moderate_forum',
						'any' => 'moderate_forum',
					),
				),
				'',
				'activateaccount' => array(
					'file' => 'Profile-Actions',
					'function' => 'activateAccount',
					'sc' => 'get',
					'permission' => array(
						'own' => array(),
						'any' => array('moderate_forum'),
					),
				),
				'infractions' => array(
					'label' => $txt['profile_infractions'],
					'file' => 'Profile-Actions',
					'function' => 'profileInfractions',
					'permission' => array(
						'own' => array('profile_view_own'),
						'any' => array('issue_warning'),
					),
				),
				'banuser' => array(
					'label' => $txt['profileBanUser'],
					'file' => 'Profile-Actions',
					'function' => 'profileBan',
					'enabled' => $cur_profile['id_group'] != 1 && !in_array(1, explode(',', $cur_profile['additional_groups'])),
					'permission' => array(
						'own' => array(),
						'any' => array('manage_bans'),
					),
				),
				'deleteaccount' => array(
					'label' => $txt['deleteAccount'],
					'file' => 'Profile-Actions',
					'function' => 'deleteAccount',
					'sc' => 'post',
					'password' => true,
					'permission' => array(
						'own' => array('profile_remove_any', 'profile_remove_own'),
						'any' => array('profile_remove_any'),
					),
				),
			),
		),
	);


	call_hook('profile_areas', array(&$profile_areas));

	if (empty($settings['media_enabled']))
		unset($profile_areas['aeva']);


	$context['password_areas'] = array();
	$current_area = isset($_REQUEST['area']) ? $_REQUEST['area'] : '';

	foreach ($profile_areas as $section_id => &$section)
	{

		foreach ($section['areas'] as $area_id => &$area)
		{
			if (is_numeric($area_id))
				continue;

			if (empty($area['permission'][we::$user['is_owner'] ? 'own' : 'any']))
				$area['enabled'] = false;

			else
				$area['permission'] = $area['permission'][we::$user['is_owner'] ? 'own' : 'any'];


			if (!empty($area['password']))
				$context['password_areas'][] = $area_id;
		}
	}
	unset($area, $section);


	if (isset($_GET['updated']))
		$context['profile_updated'] = $txt['profile_updated_own'];


	$menuOptions = array(
		'disable_url_session_check' => true,
		'current_area' => $current_area,
		'action' => 'profile' . (we::$user['is_owner'] ? '' : ';u=' . $memID),
	);


	$profile_include_data = createMenu($profile_areas, $menuOptions);


	if (!$profile_include_data)
		fatal_lang_error('no_access', false);


	$context['profile_menu_id'] = $context['max_menu_id'];
	$context['profile_menu_name'] = 'menu_data_' . $context['profile_menu_id'];


	$current_area = $profile_include_data['current_area'];
	$context['menu_item_selected'] = $current_area;


	$context['completed_save'] = false;
	$security_checks = array();
	$found_area = false;
	foreach ($profile_areas as $section_id => $section)
	{

		foreach ($section['areas'] as $area_id => $area)
		{

			if ($current_area == $area_id)
			{

				if ((isset($section['enabled']) && $section['enabled'] == false) || (isset($area['enabled']) && $area['enabled'] == false))
					fatal_lang_error('no_access', false);


				if (isset($area['sc'], $_REQUEST['save']))
				{
					$security_checks['session'] = $area['sc'];
					$context['completed_save'] = true;
				}


				if (!empty($area['validate']))
					$security_checks['validate'] = true;


				if (!empty($profile_include_data['permission']))
					$security_checks['permission'] = $profile_include_data['permission'];


				$found_area = true;
			}
		}
	}


	if (!$found_area)
		fatal_lang_error('no_access', false);


	unset($profile_areas);


	if (isset($security_checks['session']))
		checkSession($security_checks['session']);
	if (isset($security_checks['validate']))
		validateSession();
	if (isset($security_checks['permission']))
		isAllowedTo($security_checks['permission']);


	if (isset($profile_include_data['file']))
	{
		if (is_array($profile_include_data['file']))
			loadPluginSource($profile_include_data['file'][0], $profile_include_data['file'][1]);
		else
			loadSource($profile_include_data['file']);
	}


	if (!isset($profile_include_data['function']) || !is_callable($profile_include_data['function']))
	{
		destroyMenu();
		fatal_lang_error('no_access', false);
	}


	add_linktree(sprintf($txt['profile_of_username'], $context['member']['name']), '<URL>?action=profile' . ($memID != MID ? ';u=' . $memID : ''));

	if (!empty($profile_include_data['label']))
		add_linktree($profile_include_data['label'], '<URL>?action=profile' . ($memID != MID ? ';u=' . $memID : '') . ';area=' . $profile_include_data['current_area']);

	if (!empty($profile_include_data['current_subsection']) && $profile_include_data['subsections'][$profile_include_data['current_subsection']][0] != $profile_include_data['label'])
		add_linktree($profile_include_data['subsections'][$profile_include_data['current_subsection']][0], '<URL>?action=profile' . ($memID != MID ? ';u=' . $memID : '') . ';area=' . $profile_include_data['current_area'] . ';sa=' . $profile_include_data['current_subsection']);


	wetem::load($profile_include_data['function']);
	wetem::add(array('top', 'default'), 'profile_top');


	$check_password = we::$user['is_owner'] && in_array($profile_include_data['current_area'], $context['password_areas']);
	$context['require_password'] = $check_password;


	$post_errors = array();
	$profile_vars = array();


	if ($context['completed_save'])
	{

		if (!we::$user['is_owner'])
			validateSession();


		$_POST = htmltrim__recursive($_POST);
		$_POST = htmlspecialchars__recursive($_POST);

		if ($check_password)
		{

			if (trim($_POST['oldpasswrd']) == '')
				$post_errors[] = 'no_password';


			$_POST['oldpasswrd'] = un_htmlspecialchars($_POST['oldpasswrd']);


			$good_password = in_array(true, call_hook('verify_password', array($cur_profile['member_name'], $_POST['oldpasswrd'], false)), true);


			if (!$good_password && we::$user['passwd'] != sha1(strtolower($cur_profile['member_name']) . $_POST['oldpasswrd']))
				$post_errors[] = 'bad_password';


			if (in_array('bad_password', $post_errors))
				$context['password_auth_failed'] = true;
		}


		if (we::$user['is_owner'])
			$profile_vars['member_ip'] = we::$user['ip'];


		if ($current_area == 'activateaccount')
		{
			if (empty($post_errors))
				activateAccount($memID);
		}
		elseif ($current_area == 'deleteaccount')
		{
			if (empty($post_errors))
			{
				deleteAccount2($profile_vars, $post_errors, $memID);
				redirectexit();
			}
		}
		elseif ($current_area == 'groupmembership' && empty($post_errors))
		{
			$msg = groupMembership2($profile_vars, $memID);


			redirectexit('action=profile' . (we::$user['is_owner'] ? '' : ';u=' . $memID) . ';area=groupmembership' . (!empty($msg) ? ';msg=' . $msg : ''));
		}
		elseif (in_array($current_area, array('account', 'forumprofile', 'options', 'pmprefs')))
			saveProfileFields();
		else
		{
			$force_redirect = true;

			loadSource('Profile-Modify');
			saveProfileChanges($profile_vars, $memID);
		}


		if (!empty($post_errors))
		{

			loadLanguage('Errors');
			$context['post_error'] = $post_errors;
		}
		elseif (!empty($profile_vars))
		{

			if (isset($profile_vars['passwd']))
				call_hook('reset_pass', array($cur_profile['member_name'], $cur_profile['member_name'], $_POST['passwrd2']));

			updateMemberData($memID, $profile_vars);


			if ($settings['latestMember'] == $memID)
				updateStats('member');
			elseif (isset($profile_vars['real_name']))
				updateSettings(array('memberlist_updated' => time()));


			if (!empty($context['log_changes']) && !empty($settings['log_enabled_profile']))
			{
				$log_changes = array();
				foreach ($context['log_changes'] as $k => $v)
					$log_changes[] = array(
						'action' => $k,
						'id_log' => 2,
						'log_time' => time(),
						'id_member' => $memID,
						'ip' => get_ip_identifier(we::$user['ip']),
						'extra' => serialize(array_merge($v, array('applicator' => MID))),
					);

				wesql::insert('',
					'{db_prefix}log_actions',
					array(
						'action' => 'string', 'id_log' => 'int', 'log_time' => 'int', 'id_member' => 'int', 'ip' => 'int',
						'extra' => 'string-65534',
					),
					$log_changes
				);
			}


			if (!empty($context['profile_execute_on_save']))
				foreach ($context['profile_execute_on_save'] as $saveFunc)
					$saveFunc();


			$context['profile_updated'] = we::$user['is_owner'] ? $txt['profile_updated_own'] : sprintf($txt['profile_updated_else'], $cur_profile['member_name']);


			cache_put_data('member_data-profile-' . $memID, null, 0);
		}
	}


	if (!empty($post_errors))
	{

		foreach ($post_errors as $error_type)
			$context['modify_error'][$error_type] = true;
	}

	elseif (!empty($profile_vars) && we::$user['is_owner'])
		redirectexit('action=profile;area=' . $current_area . (isset($_GET['sa']) ? ';sa=' . $_GET['sa'] : '') . ';updated');
	elseif (!empty($force_redirect))
		redirectexit('action=profile' . (we::$user['is_owner'] ? '' : ';u=' . $memID) . ';area=' . $current_area . (isset($_GET['sa']) ? ';sa=' . $_GET['sa'] : ''));


	call_user_func_array($profile_include_data['function'], array($memID));


	if (!isset($context['page_title']))
		$context['page_title'] = $txt['profile'] . (isset($txt[$current_area]) ? ' - ' . $txt[$current_area] : '');
}


function loadCustomFields($memID, $area = 'summary')
{
	global $context, $txt, $user_profile;


	$where = 'active = 1';

	if ($area == 'register')
		$where .= ' AND show_reg != 0';
	elseif ($area != 'summary')
		$where .= ' AND show_profile = {string:area}';


	$privacy_to_check = $area == 'summary' ? 'can_see' : 'can_edit';


	$request = wesql::query('
		SELECT
			col_name, field_name, field_desc, field_type, field_length, field_options,
			default_value, bbc, enclose, placement, show_reg, can_see, can_edit
		FROM {db_prefix}custom_fields
		WHERE ' . $where . '
		ORDER BY position',
		array(
			'area' => $area === 'theme' ? 'options' : $area,
		)
	);
	$context['custom_fields'] = array();

	while ($row = wesql::fetch_assoc($request))
	{

		if (!we::$is_admin && $area != 'register')
		{
			$group_privacy = explode(',', $row[$privacy_to_check]);
			foreach ($group_privacy as $k => $v)
				$group_privacy[$k] = (int) $v;
			if (count(array_intersect($group_privacy, we::$user['groups'])) == 0)
				continue;


			if ($memID == MID && !in_array(-2, $group_privacy))
				continue;
		}


		$exists = $memID && isset($user_profile[$memID], $user_profile[$memID]['options'][$row['col_name']]);
		$value = $exists ? $user_profile[$memID]['options'][$row['col_name']] : '';


		if (isset($_POST['customfield'], $_POST['customfield'][$row['col_name']]))
		{
			$value = westr::htmlspecialchars($_POST['customfield'][$row['col_name']]);
			if (in_array($row['field_type'], array('select', 'radio')))
				$value = ($opts = explode(',', $row['field_options'])) && isset($opts[$value]) ? $opts[$value] : '';
		}


		$output_html = $value;
		if ($row['field_type'] == 'check')
		{
			$true = (!$exists && $row['default_value']) || $value;
			$input_html = '<input type="checkbox" name="customfield[' . $row['col_name'] . ']"' . ($true ? ' checked' : '') . '>';
			$output_html = $true ? $txt['yes'] : $txt['no'];
		}
		elseif ($row['field_type'] == 'select')
		{
			$input_html = '<select name="customfield[' . $row['col_name'] . ']"><option value="-1"></option>';
			$opts = explode(',', $row['field_options']);
			foreach ($opts as $k => $v)
			{
				$true = (!$exists && $row['default_value'] == $v) || $value == $v;
				$input_html .= '<option value="' . $k . '"' . ($true ? ' selected' : '') . '>' . $v . '</option>';
				if ($true)
					$output_html = $v;
			}

			$input_html .= '</select>';
		}
		elseif ($row['field_type'] == 'radio')
		{
			$input_html = '<fieldset>';
			foreach (explode(',', $row['field_options']) as $k => $v)
			{
				$true = (!$exists && $row['default_value'] == $v) || $value == $v;
				$input_html .= '<label><input type="radio" name="customfield[' . $row['col_name'] . ']" value="' . $k . '"' . ($true ? ' checked' : '') . '> ' . $v . '</label><br>';
				if ($true)
					$output_html = $v;
			}
			$input_html .= '</fieldset>';
		}
		elseif ($row['field_type'] == 'text')
		{
			$input_html = '<input name="customfield[' . $row['col_name'] . ']" ' . ($row['field_length'] != 0 ? 'maxlength="' . $row['field_length'] . '"' : '') . ' size="' . ($row['field_length'] == 0 || $row['field_length'] >= 50 ? 50 : ($row['field_length'] > 30 ? 30 : ($row['field_length'] > 10 ? 20 : 10))) . '" value="' . $value . '"' . ($area == 'register' && $row['show_reg'] > 1 ? ' required' : '') . '>';
		}
		else
		{
			@list ($rows, $cols) = @explode(',', $row['default_value']);
			$input_html = '<textarea name="customfield[' . $row['col_name'] . ']" ' . (!empty($rows) ? 'rows="' . $rows . '"' : '') . ' ' . (!empty($cols) ? 'cols="' . $cols . '"' : '') . '>' . $value . '</textarea>';
		}


		if ($row['bbc'])
			$output_html = parse_bbc($output_html, 'custom-field');

		elseif ($row['field_type'] == 'textarea')
			$output_html = strtr($output_html, array("\n" => '<br>'));


		if (!empty($row['enclose']) && !empty($output_html))
			$output_html = strtr($row['enclose'], array(
				'{SCRIPTURL}' => SCRIPT,
				'{IMAGES_URL}' => ASSETS,
				'{INPUT}' => $output_html,
			));

		$context['custom_fields'][] = array(
			'name' => $row['field_name'],
			'desc' => $row['field_desc'],
			'type' => $row['field_type'],
			'privacy' => 'custom_' . $row['field_name'],
			'input_html' => $input_html,
			'output_html' => $output_html,
			'placement' => $row['placement'],
			'colname' => $row['col_name'],
			'value' => $value,
		);
	}
	wesql::free_result($request);
}

function profile_can_see($area)
{
	static $lists = null;
	global $user_profile, $context;

	$profile = $user_profile[$context['id_member']];
	$data = isset($profile['data']['privacy'][$area]) ? $profile['data']['privacy'][$area] : '';
	if (empty($data) || we::$user['is_owner'])
		return true;
	if ($data == PRIVACY_AUTHOR && we::$id != $profile['id_member'])
		return false;
	if ($lists === null)
		$lists = explode(',', we::$user['privacy_list']);
	return count(array_intersect(explode(',', $data), $lists)) > 0;
}

function profile_privacy_icon($area, $text)
{
	static $lists = null;
	global $user_profile, $context;

	if (empty($area))
		return $text;

	$profile = $user_profile[$context['id_member']];
	$privacy = isset($profile['data']['privacy'][$area]) ? $profile['data']['privacy'][$area] : PRIVACY_DEFAULT;

	return get_privacy_widget($privacy, we::$user['is_owner'], $text, $area);
}
