<?php
/** 
* @package	uForum 
* @file		eprofile.php
* @version	$Id$
* @copyright	2007-2010 (c) PioDer <pioder@wp.pl>
* @link    		http://www.pioder.pl/
* @license	see LICENSE.txt
**/
define('IN_uF', true);
//include files
require('./config.php');
require('./includes/constants.php');
require('./includes/db.php');
require('./includes/errors.php');
//connect to database
DataBase::db_connect();
require('./includes/sessions.php');
require('./includes/classes/class_user.php');
require('./common.php');
require('./includes/misc_functions.php');
require('./includes/classes/class_posting.php');
require('./includes/classes/class_forum.php');
require('./includes/classes/class_topic.php');
require('./includes/classes/secure.php');
$default_skin = ViewSkinName();
$default_lang = DefaultLang();
require('./lngs/'.$default_lang.'/main.php');
$start = TimeGeneration();
SessDelInvalid();	
SessRegister();
SessDeleteOld();
if ($_SESSION['uid']==0)
{
	$stop = TimeGeneration();
	message_forum($lng['youarenotlogd'],'login.php?mode=login');
}
foreach ($_POST as $name => $value)
{
	if ($forum_config['use_censorlist'])
	{
		$_POST[$name] = Secure::UseCensorlist($value);
	}
}	
$msg='';
$errors = true;
$message = '';
if (isset($_POST['email']))
{
	if ( ereg ("^.+@.+\..+$", $_POST['email']))
	{
		//if user changing password... 
		if ($_POST['password']!='')
		{
			if (md5($_POST['password'])==User::UserInformation($_SESSION['uid'],'pass'))
			{
				if ($_POST['newpassword']==$_POST['confirmpassword'])
				{
					User::UpdatePassword($_SESSION['uid'], md5(strip_tags($_POST['newpassword'])));
					$errors = false;
				}
				else
				{
					$message=$lng['incorrect_password2'];
					$msg = './skins/'.$default_skin.'/post_error_body.tpl';
				}
			}
			else
			{
				$message=$lng['incorrect_password'];
				$msg = './skins/'.$default_skin.'/post_error_body.tpl';  
			}
		}
		else
		{
			if ($_POST['default_skin']=='-1')
			{
				$message .= $lng['invalid_skin'].'<br />';
			}
			
			if ($_POST['default_lang']=='-1')
			{
				$message .= $lng['invalid_lang'].'<br />';
			}
			
			if ($_POST['limit_tpid']=='-1')
			{
				$message .= $lng['no_limit_tpid'].'<br />';
			}
			
			if ($_POST['limit_ftid'] =='-1')
			{
				$message .= $lng['no_limit_ftid'].'<br />';
			}
			
			if ($_POST['limit_users']=='-1')
			{
				$message .= $lng['no_limit_users'].'<br />';
			}
			
			if (strlen(trim($_POST['sig']))>$forum_config['sig_len'])
			{
				$message .= $lng['signature_too_long'].'<br />';
			}
			
			if ($message != '')
			{
				$msg = './skins/'.$default_skin.'/post_error_body.tpl';
			}
			else
			{
				$errors = false;
			}
									
		}
		if (!$errors)
		{
			$_POST['ggnumber']=intval($_POST['ggnumber']);
			$_POST['interests']=strip_tags($_POST['interests']);
			$_POST['sig']=Secure::TagsReplace($_POST['sig']);
			$allow_shoutbox = (isset($_POST['allow_shoutbox'])) ? '1' : 0;
			if (isset($_FILES['avatar_file']['tmp_name']))
			{
				$extension = substr($_FILES['avatar_file']['name'],(strlen($_FILES['avatar_file']['name'])-3));
				if (($extension == 'jpg') or ($extension == 'gif'))
				{
					if (file_exists(AV_CATALOG.'av-'.$_SESSION['uid'].'.jpg'))
					{
						unlink(AV_CATALOG.'av-'.$_SESSION['uid'].'.jpg');
					}
					if (file_exists(AV_CATALOG.'av-'.$_SESSION['uid'].'.gif'))
					{
						unlink(AV_CATALOG.'av-'.$_SESSION['uid'].'.gif');
					}
					move_uploaded_file($_FILES['avatar_file']['tmp_name'], AV_CATALOG.'av-'.$_SESSION['uid'].'.'.$extension);
					$imagesize = @getimagesize(AV_CATALOG.'av-'.$_SESSION['uid'].'.'.$extension);
					if (($imagesize[0]<$forum_config['max_av_x']) && ($imagesize[1]<$forum_config['max_av_y']))
					{
						$_POST['avatar'] = AV_CATALOG.'av-'.$_SESSION['uid'].'.'.$extension;
					}
					else
					{
						$_POST['avatar'] = 'images/av-to-big.jpg';
					}
				}
				else
				{
					$_POST['avatar'] = htmlspecialchars($_POST['avatar']);
					if (!preg_match('#^(http)|(ftp):\/\/#i', $_POST['avatar']) && $_POST['avatar'] != 'images/av-to-big.jpg')
					{
						$_POST['avatar'] = 'http://' . $_POST['avatar'];
					}

					if (!preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|gif))$)#is", $_POST['avatar']) && $_POST['avatar'] != 'images/av-to-big.jpg')
					{
						$_POST['avatar'] = '';	
					}
					else
					{
						$imagesize = getimagesize($_POST['avatar']);
						if (($imagesize[0]<$forum_config['max_av_x']) && ($imagesize[1]<$forum_config['max_av_y']))
						{
							$_POST['avatar'] = $_POST['avatar'];
						}
						else
						{
							$_POST['avatar'] = 'images/av-to-big.jpg';
						}
					}
				}
			}
			else
			{
				$_POST['avatar'] = htmlspecialchars($_POST['avatar']);
				if (!preg_match('#^(http)|(ftp):\/\/#i', $_POST['avatar']) && $_POST['avatar'] != 'images/av-to-big.jpg')
				{
					$_POST['avatar'] = 'http://' . $_POST['avatar'];
				}

				if (!preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|gif))$)#is", $_POST['avatar']) && $_POST['avatar'] != 'images/av-to-big.jpg')
				{
					$_POST['avatar'] = '';	
				}
				else
				{
					$imagesize = getimagesize($_POST['avatar']);
					if (($imagesize[0]<$forum_config['max_av_x']) && ($imagesize[1]<$forum_config['max_av_y']))
					{
						$_POST['avatar'] = $_POST['avatar'];
					}
					else
					{
						$_POST['avatar'] = 'images/av-to-big.jpg';
					}
				}
			}	
			User::UpdateProfile($_SESSION['uid'],$_POST['ggnumber'],strip_tags($_POST['email']),$_POST['interests'], $_POST['sig'],$_POST['avatar'],$_POST['allow_qr'],$_POST['allow_email'],$_POST['allow_gg'],$_POST['default_skin'],$_POST['default_lang'], $_POST['limit_tpid'],$_POST['limit_ftid'], $_POST['limit_users'], $allow_shoutbox);
			message_forum($lng['profile_modernized'],'eprofile.php');
		}
	}
	else
	{
		$message .= $lng['invalid_email'];
		$msg = './skins/'.$default_skin.'/post_error_body.tpl';  
	}
}
//add skin variables
$skin = array(
//labels profile
'lnick'=>$lng['user_name'],
'lpass'=>$lng['lpassw'],
'lnewpass'=>$lng['new_password'],
'lcpass'=>$lng['confirm_password'],
'lemail'=>'E-mail',
'lgg'=>$lng['gg_number'],
'lallow_gg'=>$lng['allow_gg'],
'lallow_email'=>$lng['allow_email'],
'lallow_qr'=>$lng['allow_qr'],
'linterests'=>$lng['luinterests'],
'lsig'=>$lng['sig'],
'lavaddr'=>$lng['picture_adress'],
'lovpr'=>$lng['general_settings'],
'L.select_value'=>$lng['select_value'],
'L.limit_users'=>$lng['limit_users'],
'OPTIONS.limit_users'=>ListPages($userdata['limit_users']),
'L.posts_in_topic'=>$lng['limit_posts'],
'OPTIONS.limit_tpid'=>ListPages($userdata['limit_tpid']),
'L.topics_in_forum'=>$lng['limit_topics'],
'OPTIONS.limit_ftid'=>ListPages($userdata['limit_ftid']),
'lupr'=>$lng['profile_settings'],
'lspr'=>$lng['signature_settings'],
'ldefault_lang'=>$lng['default_lang'],
'default_lang'=>ListDir('lngs', $default_lang),
'l2default_lang'=>$lng['select_lang'],
'ldefault_skin'=>$lng['default_skin'],
'default_skin'=>ListDir('skins', $default_skin),
'l2default_skin'=>$lng['select_skin'],
'lapr'=>$lng['avatar_settings'],
'lsubmit'=>$lng['save'],
'lreset'=>$lng['reset'],
'nick'=>$userdata['nick'],

//options profile
'sig'=>$userdata['sig'],
'allow'=>$lng['allow'],
'allow_shoutbox'=>($userdata['view_shoutbox']==1) ? 'checked="checked"' : '',
'avatar'=>$userdata['avatar'],
'interests'=>$userdata['interests'],
'email'=>$userdata['email'],
'gg'=>$userdata['gg'],

//options values
'option_no_gg'=>($userdata['allow_gg']==0) ? 'checked="checked"' : '',
'option_no_email'=>($userdata['allow_email']==0) ? 'checked="checked"' : '',
'option_no_qr'=>($userdata['allow_qr']==0) ? 'checked="checked"' : '',
'option_yes_gg'=>($userdata['allow_gg']==1) ? 'checked="checked"' : '',
'option_yes_email'=>($userdata['allow_email']==1) ? 'checked="checked"' : '',
'option_yes_qr'=>($userdata['allow_qr']==1) ? 'checked="checked"' : '',
'no'=>$lng['no'],
'lallow_shoutbox'=>$lng['allow_shoutbox'],
'lavfile'=>$lng['avatar_file'],
'yes'=>$lng['yes']
);
$skin = array_push_assoc($skin,GenerateHeader($lng['leprofile'], '<a href="eprofile.php" class="navigator">'.$lng['leprofile']));
if ($msg=='')
{
	$msg = './skins/'.$default_skin.'/blank.tpl';
}
//do it!
require('./skins/'.$default_skin.'/overall_header.tpl');
require('./skins/'.$default_skin.'/eprofile_body.tpl');
$stop = TimeGeneration();
$skin['queries'] =  ShowQueries($start, $stop);
require('./skins/'.$default_skin.'/overall_footer.tpl');
?>