<?php
/**
* This plugin depends on XThreads.
* XThreads official release thread:
*
*		http://mybbhacks.zingaburga.com/showthread.php?tid=288
*		Coded by: Yumi/ZiNgA BuRgA
*		@ http://mybbhacks.zingaburga.com
*/

if(!defined('IN_MYBB')) die();
function xt_ucp_main_info(){
	return array(
		'name'			=> 'UCP Main Page',
		'description'	=> '<strong style="color: red"><em><a href="http://mybbhacks.zingaburga.com/showthread.php?tid=288">XThreads</a> is required for this plugin.</em></strong><br />Load Custom Profile Fields, some Edit Options and some Edit Profile inputs into User CP main page.',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.1',
		'compatibility' => '*',
		'guid'        	=> ''
	);
}

function xt_ucp_main_activate(){}
function xt_ucp_main_deactivate(){}

$plugins->add_hook('usercp_start','xt_ucp_main_usercp_start');
function xt_ucp_main_usercp_start(){
	if($GLOBALS['mybb']->settings['xt_proffields_ucp'] != 0){
		global $xtpfc;
		if(!$xtpfc) $xtpfc = $GLOBALS['cache']->read('xt_proffields');
		if($xtpfc){
			global $mybb,$xtpfc,$errors,$lang,$templates,$user,$xtpf_minp,$xtpf_data,$birth_day,$userwebsite,$showsig,$showavatar,$bdprivacy,$allownotices,$receivepms,$pmnotice,$pmnotify,$tzselect,$dstcorrection;
			$lang->load('global');
			if($mybb->input['action'] == 'do_update_xt_proffields' && $mybb->request_method == 'post'){
				verify_post_check($mybb->input['my_post_key']);
				require_once MYBB_ROOT.'inc/datahandlers/user.php';
				$userhandler = new UserDataHandler('update');
				$userxtpf = array('uid'=>$mybb->user['uid']);
				foreach($xtpfc as $pfields => &$pfval){
					if(xthreads_user_in_groups($pfval['xt_proffields_editable']) && !$pfval['xt_proffields_cinp'] && !$mybb->input['profile_fields']['fid'.$pfval['fid']]){
						$mybb->input['profile_fields']['fid'.$pfval['fid']] = $mybb->user['fid'.$pfval['fid']];
					}
				}
				$userxtpf['profile_fields'] = $mybb->input['profile_fields'];
				$userxtpf['birthday'] = array('day'=>$mybb->input['bday1'],'month'=>$mybb->input['bday2'],'year'=>$mybb->input['bday3']);
				$userxtpf['website'] = $mybb->input['website'];
				$userxtpf['timezone'] = $mybb->input['timezoneoffset'];
				$userxtpf['birthdayprivacy'] = $mybb->input['birthdayprivacy'];
				if($mybb->settings['usertppoptions']) $userxtpf['options']['tpp'] = (int)$mybb->input['tpp'];
				if($mybb->settings['userpppoptions']) $userxtpf['options']['ppp'] = (int)$mybb->input['ppp'];
				$userxtpf['options']['showsigs'] = (int)$mybb->input['showsigs'];
				$userxtpf['options']['showavatars'] = (int)$mybb->input['showavatars'];
				$userxtpf['options']['allownotices'] = (int)$mybb->input['allownotices'];
				$userxtpf['options']['receivepms'] = (int)$mybb->input['receivepms'];
				$userxtpf['options']['pmnotice'] = (int)$mybb->input['pmnotice'];
				$userxtpf['options']['pmnotify'] = (int)$mybb->input['pmnotify'];
				$userxtpf['options']['dstcorrection'] = (int)$mybb->input['dstcorrection'];
				$userhandler->set_data($userxtpf);
				if(!$userhandler->validate_user()){
					$errors = $userhandler->get_friendly_errors();
					$errors = inline_error($errors);
					$mybb->input['action'] = '';
				}else{
					$userhandler->update_user();
					if(!$lang->xt_proffields_updated) $lang->load('xt_proffields');
					redirect('usercp.php',$lang->xt_proffields_updated);
				}
			}
			if($errors){
				$user = $mybb->input;
				$bday = array();
				$bday[0] = $mybb->input['bday1'];
				$bday[1] = $mybb->input['bday2'];
				$bday[2] = intval($mybb->input['bday3']);
			}else{
				$user = $mybb->user;
				$bday = explode('-',$user['birthday']);
			}
			$xtpf_minp = array();
			foreach($xtpfc as $uf => &$xtpf){
				if($xtpf['editable'] == 1){
					$code = '';
					if($xtpf['postnum'] && $xtpf['postnum'] > $user['postnum']) continue;
					if(xthreads_user_in_groups($xtpf['xt_proffields_editable'])){
						$code = xt_proffields_inp($xtpf,$user,$errors,$vars);
						$xtpf_minp['fid'.$xtpf['fid']] = xt_proffields_cinp($xtpf,$vars);
					}
				}
			}
			$bdaydaysel = $bdaymonthsel = $bdayprivacysel = '';
			for($i = 1; $i <= 31; ++$i){
				if($bday[0] == $i) $daysel[$i] = ' selected="selected"';
				$bdaydaysel .= '<option value="'.$i.'"'.$daysel[$i].'>'.$i.'</option>'."\n";
			}
			for($i = 1; $i <= 12; ++$i){
				$lang_month = 'month_'.$i;
				if($bday[1] == $i) $monthsel[$i] = ' selected="selected"';
				$bdaymonthsel .= '<option value="'.$i.'"'.$monthsel[$i].'>'.$lang->$lang_month.'</option>'."\n";
			}
			$bd_day = '<select name="bday1"><option value="">&nbsp;</option>'.$bdaydaysel.'</select>';
			$bd_month = '<select name="bday2"><option value="">&nbsp;</option>'.$bdaymonthsel.'</select>';
			$bd_year = '<input type="text" class="textbox" size="4" maxlength="4" name="bday3" value="'.$bday['2'].'" />';
			$birth_day = $bd_day.' '.$bd_month.' '.$bd_year;
			if($user['birthdayprivacy'] == 'all' || !$user['birthdayprivacy']){
				$allselected = ' selected="selected"';
			}else if($user['birthdayprivacy'] == 'none'){
				$noneselected = ' selected="selected"';
			}else if($user['birthdayprivacy'] == 'age'){
				$ageselected = ' selected="selected"';
			}
			$bdayprivacysel .= '<option value="all"'.$allselected.'>'.$lang->birthdayprivacyall.'</option>'."\n";
			$bdayprivacysel .= '<option value="none"'.$noneselected.'>'.$lang->birthdayprivacynone.'</option>'."\n";
			$bdayprivacysel .= '<option value="age"'.$ageselected.'>'.$lang->birthdayprivacyage.'</option>';
			$bdprivacy = '<select name="birthdayprivacy">'.$bdayprivacysel.'</select>';
			if($user['website'] == '' || $user['website'] == 'http://'){
				$user['website'] = 'http://';
			}else{
				$user['website'] = htmlspecialchars_uni($user['website']);
			}
			$userwebsite = '<input type="text" class="textbox" name="website" size="25" maxlength="75" value="'.$user['website'].'" />';
			if($mybb->settings['usertppoptions']){
				global $threadperpage;
				$explodedtpp = explode(',',$mybb->settings['usertppoptions']);
				$tppoptions = '';
				if(is_array($explodedtpp)){
					foreach($explodedtpp as $key => $val){
						$val = trim($val);
						$selected = '';
						if($user['tpp'] == $val) $selected = ' selected="selected"';
						$tppoptions .= '<option value="'.$val.'"'.$selected.'>'.$lang->sprintf($lang->tpp_option,$val).'</option>'."\n";
					}
				}
				$threadperpage = '<select name="tpp"><option value="">'.$lang->use_default.'</option>'.$tppoptions.'</select>';
				//eval('$tppselect = "'.$templates->get('usercp_options_tppselect').'";');
			}
			if($mybb->settings['userpppoptions']){
				global $postperpage;
				$explodedppp = explode(',',$mybb->settings['userpppoptions']);
				$pppoptions = '';
				if(is_array($explodedppp)){
					foreach($explodedppp as $key => $val){
						$val = trim($val);
						$selected = '';
						if($user['ppp'] == $val) $selected = ' selected="selected"';
						$pppoptions .= '<option value="'.$val.'"'.$selected.'>'.$lang->sprintf($lang->ppp_option, $val).'</option>'."\n";
					}
				}
				$postperpage = '<select name="ppp"><option value="">'.$lang->use_default.'</option>'.$pppoptions.'</select>';
				//eval('$pppselect = "'.$templates->get('usercp_options_pppselect').'";');
			}
			if($user['showsigs'] == 1) $showsigscheck = ' checked="checked"';
			if($user['showavatars'] == 1) $showavatarscheck = ' checked="checked"';
			$showsig = '<table border="0" style="width:100%"><tr><td style="width:1px"><input type="checkbox" class="checkbox" name="showsigs" id="showsigs" value="1"'.$showsigscheck.' /></td><td><label for="showsigs">'.$lang->show_sigs.'</label></td></tr></table>';
			$showavatar = '<table border="0" style="width:100%"><tr><td style="width:1px"><input type="checkbox" class="checkbox" name="showavatars" id="showavatars" value="1"'.$showavatarscheck.' /></td><td><label for="showavatars">'.$lang->show_avatars.'</label></td></tr></table>';
			$dstcc = array();
			$dstcc[$user['dstcorrection']] = ' selected="selected"';
			$dstcorrection = '<select name="dstcorrection">
				<option value="2"'.$dstcc[2].'>'.$lang->dst_correction_auto.'</option>
				<option value="1"'.$dstcc[1].'>'.$lang->dst_correction_enabled.'</option>
				<option value="0"'.$dstcc[0].'>'.$lang->dst_correction_disabled.'</option>
			</select>';
			if($user['allownotices'] == 1){
				$allownoticescheck = ' checked="checked"';
			}else{
				$allownoticescheck = '';
			}
			$allownotices = '<table border="0" style="width:100%"><tr><td style="width:1px"><input type="checkbox" class="checkbox" name="allownotices" id="allownotices" value="1"'.$allownoticescheck.' /></td><td><label for="allownotices">'.$lang->allow_notices.'</label></td></tr></table>';
			if($user['receivepms'] == 1){
				$receivepmscheck = ' checked="checked"';
			}else{
				$receivepmscheck = '';
			}
			$receivepms = '<table border="0" style="width:100%"><tr><td style="width:1px"><input type="checkbox" class="checkbox" name="receivepms" id="receivepms" value="1"'.$receivepmscheck.' /></td><td><label for="receivepms">'.$lang->receive_pms.'</label></td></tr></table>';
			if($user['pmnotice'] == 1 || $user['pmnotice'] == 2){
				$pmnoticecheck = ' checked="checked"';
			}else{
				$pmnoticecheck = '';
			}
			$pmnotice = '<table border="0" style="width:100%"><tr><td style="width:1px"><input type="checkbox" class="checkbox" name="pmnotice" id="pmnotice" value="1"'.$pmnoticecheck.' /></td><td><label for="pmnotice">'.$lang->pm_notice.'</label></td></tr></table>';
			if($user['pmnotify'] == 1){
				$pmnotifycheck = ' checked="checked"';
			}else{
				$pmnotifycheck = '';
			}
			$pmnotify = '<table border="0" style="width:100%"><tr><td style="width:1px"><input type="checkbox" class="checkbox" name="pmnotify" id="pmnotify" value="1"'.$pmnotifycheck.' /></td><td><label for="pmnotify">'.$lang->pm_notify.'</label></td></tr></table>';
			$tzselect = build_timezone_select('timezoneoffset',$mybb->user['timezone'],true);
		}
	}
}
?>