<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

require_once 'init.php';

// Disable any caching by the browser
disable_browser_cache();

$error = 0;
$errorMsg = '';

$mode_options   = array ('register' => 'register', 'edit'  => 'edit', 'sendpassword' => 'sendpassword', 'confirm' => 'confirm', 'thankyou' => 'thankyou', 'confirmed'=>'confirmed', 'logged'=>'logged');
$agreed_options = array ('true'     => 'true'    , 'false' => 'false');

if (array_key_exists ($_REQUEST['mode'], $mode_options))
	$mode = $_REQUEST['mode'];
else
    $mode = 'register';

if (!empty ($_REQUEST['agreed'])) {
	if (!empty ($_REQUEST['agreed']) && array_key_exists ($_REQUEST['agreed'], $agreed_options))
    	$agreed = $_REQUEST['agreed'];
   	else
      	$agreed = 'false';
}

$tpl->assign('load_Javascript',  'true');

$exclude_user_id = 0;

switch ($mode) {
	case 'thankyou' :
   		$tpl->assign('thank_you', 1);
   	break;
   	case 'confirmed':
       	$tpl->assign('confirmed', 1);
   	break;
   	case 'confirm' :
   		$tpl->assign('confirm', 1);
   		if (empty ($_SESSION['phpld']['user']['id'])) {
      		$user_id     = (!empty ($_REQUEST['uid']) ? clean_string_paranoia($_REQUEST['uid']) : 0);
      		$confirm_key = (!empty ($_REQUEST['key']) ? clean_string_paranoia($_REQUEST['key']) : 0);

      		if (!empty ($user_id) && !empty ($confirm_key)) {
         		$data = $db->GetRow("SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($user_id)." AND `CONFIRM` = ".$db->qstr($confirm_key));
         		if (!empty ($data)){
            		$data['PASSWORD']     = $data['NEW_PASSWORD'];
            		$data['NEW_PASSWORD'] = '';
            		$data['CONFIRM']      = '';

            		$where = " `ID` = ".$db->qstr($user_id);

            		if ($db->AutoExecute($tables['user']['name'], $data, 'UPDATE', $where))
               			$tpl->assign('password_recovered', 1);
            		else
               			$tpl->assign('password_recovered', 0);
         		} else
            		die("!! ERROR !!\n<br />\n<br />\nSeems like an attack! Your access has been blocked.\n");
      		}
   		}
   	break;
		case 'logged':
       	$tpl->assign('user_logged', 1);
		$profileUpdate = (!empty ($_SESSION['phpld']['user']['profileUpdate']) ? $_SESSION['phpld']['user']['profileUpdate'] : 0);
		$tpl->assign('profileUpdate', $profileUpdate);
		$_SESSION['phpld']['user']['profileUpdate'] = 0;
   	break;
   	case 'sendpassword' :
   		
   		
   	
   		
   		//Generate unique imagehash for visual confirmation
   		if (VISUAL_CONFIRM == 1) {
      		require_once 'include/functions_imgverif.php';
      		$imagehash = fetch_captcha_hash();
      		$tpl->assign('imagehash', $imagehash);
      		unset ($imagehash);
   		}
   		$tpl->assign('send_password', 1);
   		
   		//for login
		    if (!SmartyValidate :: is_registered_form('login')) {
			    SmartyValidate :: register_form('login', true);
			    SmartyValidate :: register_validator('v_user', 'user', 'notEmpty', false, false, 'trim', 'login');
			    SmartyValidate :: register_validator('v_pass', 'pass', 'notEmpty', false, false, 'trim', 'login');
		    }
		    //end for login
   		
   		if (empty ($_POST['submit'])) {
   			//SmartyValidate :: disconnect();
			SmartyValidate :: connect($tpl, true);

			SmartyValidate :: register_form('profile', true);
			SmartyValidate :: register_criteria('isBannedEmail'    , 'validate_is_banned_email', 'profile');

			SmartyValidate :: register_validator('v_LOGIN', 'LOGIN', 'notEmpty', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_LOGIN_LENGTH', 'LOGIN:'.USER_LOGIN_MIN_LENGTH.':'.USER_LOGIN_MAX_LENGTH, 'isLength', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_EMAIL', 'EMAIL', 'isEmail', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_EMAIL_BANNED', 'EMAIL', 'isBannedEmail', false, false, 'trim', 'profile');

      		if (VISUAL_CONFIRM == 1) {
         		SmartyValidate :: register_criteria('isCaptchaValid', 'validate_captcha', 'profile');
         		SmartyValidate :: register_validator('v_CAPTCHA', 'CAPTCHA:IMAGEHASH', 'isCaptchaValid', false, false, 'trim', 'profile');
      		}   		
   		} else {
   			SmartyValidate :: connect($tpl, true);
      		$data = get_table_data('user');

      		//print_r($_SESSION);
      		
			//Take care of white-space chars
      		$data = filter_white_space($data);

      		if (SmartyValidate :: is_valid($_POST, 'profile')) {
      			$user = $db->GetRow("SELECT `ID`, `LOGIN`, `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `LOGIN` LIKE ".$db->qstr($data['LOGIN'])." AND `EMAIL` LIKE ".$db->qstr($data['EMAIL'])." AND `ACTIVE` = '1'");

      	 	if (is_array ($user) && !empty ($user)) {
            	$data['ID'] = $user['ID'];
             
	            $new_password         = create_password(8);
	            $data['CONFIRM']      = create_password(6);
	            $data['NEW_PASSWORD'] = encrypt_password($new_password);
	            unset ($data['LOGIN'], $data['EMAIL']);

            	$dbUpdate = $db->Replace($tables['user']['name'], $data, 'ID', true);

            	if ($dbUpdate == 1) {
                	$user['PASSWORD'] = $new_password;

               		$passwordRecoverUrl = (substr (SITE_URL, -1) == "/" ? SITE_URL : SITE_URL."/");
               		$passwordRecoverUrl .= "profile.php?mode=confirm&uid={$user['ID']}&key={$data['CONFIRM']}";

               		$loginURL = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL : SITE_URL.'/') . 'login.php';

               		
               		//Get email template
               		$emailTpl = get_email_template('NTF_USER_PASSWORD_TPL');

               		$tplSql   = "SELECT `TITLE`, `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr(NTF_USER_PASSWORD_TPL);
               		$emailTpl = $db->GetRow($tplSql);

               		//if email tpl is available
               		if (is_array ($emailTpl) && !empty ($emailTpl)) {
                  		$mail = get_emailer_admin();
                  		$mail->AddAddress($user['EMAIL'], $user['NAME']);

                  		//Add email subject
                  		$emailSubject = $emailTpl['SUBJECT'];
                  		$emailSubject = str_replace ('{PASSWORD_RECOVER_URL}', $passwordRecoverUrl, $emailSubject);
                  		$emailSubject = str_replace ('{USER_LOGIN_PAGE}', $loginURL, $emailSubject);
                  		$emailSubject = replace_email_vars($emailSubject, $user, 6);
                  		$emailSubject = replace_email_vars($emailSubject, $user, 5);
                  		$mail->Subject = trim ($emailSubject);

                  		//Add email body
                  		$emailBody = $emailTpl['BODY'];
                  		$emailBody = str_replace ('{PASSWORD_RECOVER_URL}', $passwordRecoverUrl, $emailBody);
                  		$emailBody = str_replace ('{USER_LOGIN_PAGE}', $loginURL, $emailBody);
                  		$emailBody = replace_email_vars($emailBody, $user, 6);
                  		$emailBody = replace_email_vars($emailBody, $user, 5);
                  		$mail->Body = trim ($emailBody);

                  		//Send email
                  		if (!$mail->Send()) {
                     		//An error occured while sending email
                     		$tpl->assign('email_sent', 'error');
                     		$tpl->assign('email_error_msg', $mail->ErrorInfo);
                  		} else {
	                    	//Email sent successfully
                     		$tpl->assign('email_sent', 'sent');
                  		}
                  		$tpl->assign('email_status', 1);

                  		//Clear all addresses (and attachments)
                  		$mail->ClearAddresses();
                  		$mail->ClearAttachments();

                  		//Free memory
                  		unset ($user, $data, $mail, $emailTpl, $emailSubject, $emailBody, $passwordRecoverUrl, $loginURL, $new_password);
               		}
            	}

            	//Free memory
            	unset ($data, $new_password);
         	} else {
            	$tpl->assign('wrong_user_details', 1);
            	unset ($data, $_POST, $_GET, $_REQUEST);
         	}

         	if (isset ($user)) {
            	unset ($user);
         	}
      	} else {
       		$tpl->assign($_POST);
         	unset ($data);
      	}
   	}

   	if (isset ($data)) {
    	$tpl->assign($data);
      	unset ($data);
   	}
	break;

   	case 'register' :
   		if ($agreed == "false")
      		http_custom_redirect('index.php');
   		elseif (empty ($agreed))
      		$tpl->assign('agreement_terms', 1);

   		elseif ($agreed == "true")
      		$tpl->assign('user_registration', 1);

   	case 'edit' :
      	$tpl->assign('user_edit', 1);
      	$exclude_user_id = (!empty ($_SESSION['phpld']['user']['id']) ? $_SESSION['phpld']['user']['id'] : 0);
   
    default :
   		//Generate unique imagehash for visual confirmation
   		if (VISUAL_CONFIRM == 1){
      		require_once 'include/functions_imgverif.php';
      		$imagehash = fetch_captcha_hash();
      		$tpl->assign('imagehash', $imagehash);
      		unset ($imagehash);
   		}
   		$tpl->assign('languages', select_lang('lang/'));

   		if (empty ($_POST['submit'])) {
      		//raluca
      		if (isset($_SESSION['SmartyValidate'])) {
         		unset ($_SESSION['SmartyValidate']);
      		}
      		//raluca
      		if ($mode == "edit") {
         		if (empty ($_POST['submit']) && !empty ($_SESSION['phpld']['user']['id']))
            		$user_info = $db->GetRow("SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($_SESSION['phpld']['user']['id']));

         		$tpl->assign($user_info);
      		}
      		
      		SmartyValidate :: connect($tpl, true);
      		SmartyValidate :: register_form('profile', true);
      		SmartyValidate :: register_criteria('isValueUnique', 'validate_unique', 'profile');
      		SmartyValidate :: register_criteria('isNotEqual', 'validate_not_equal', 'profile');
      		SmartyValidate :: register_criteria('isBannedEmail', 'validate_is_banned_email', 'profile');
      		SmartyValidate :: register_criteria('isDomainBanned'   , 'validate_isBannedDomain' , 'profile');
      		//author avatar related
      		//SmartyValidate :: register_criteria('isMimeType' , 'validate_isMimeType' , 'profile');
      		//end

      		if ($mode == "register") {
         		SmartyValidate :: register_validator('v_LOGIN', 'LOGIN', 'notEmpty', false, false, 'trim', 'profile');
         		SmartyValidate :: register_validator('v_LOGIN_LENGTH', 'LOGIN:'.USER_LOGIN_MIN_LENGTH.':'.USER_LOGIN_MAX_LENGTH, 'isLength', false, false, 'trim', 'profile');
         		SmartyValidate :: register_validator('v_LOGIN_U', 'LOGIN:user', 'isValueUnique', false, false, 'trim', 'profile');
      		} elseif ($mode == "edit") {
         		SmartyValidate :: register_validator('v_URL_BANNED'    , 'URL', 'isDomainBanned', false, false, 'trim', 'profile');
         		SmartyValidate :: register_validator('v_INFO_LENGTH', 'INFO:0:255', 'isLength' , true, false, 'trim', 'profile');
      		}
      		SmartyValidate :: register_validator('v_NAME', 'NAME', 'notEmpty', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_NAME_LENGTH', 'NAME:'.USER_NAME_MIN_LENGTH.':'.USER_NAME_MAX_LENGTH, 'isLength', false, false, 'trim', 'profile');

      		SmartyValidate :: register_validator('v_LANGUAGE', 'LANGUAGE', 'notEmpty', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_PASSWORD', 'PASSWORD', 'notEmpty', ($mode == "edit" ? true : false), false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_PASSWORD_LENGTH', 'PASSWORD:'.USER_PASSWORD_MIN_LENGTH.':'.USER_PASSWORD_MAX_LENGTH, 'isLength', ($mode == "edit" ? true : false), false, 'trim', 'profile');

      		SmartyValidate :: register_validator('v_PASSWORDC', 'PASSWORD:PASSWORDC', 'isEqual', true, false, 'trim', 'profile');

      		SmartyValidate :: register_validator('v_EMAIL', 'EMAIL', 'isEmail', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_EMAIL_U', 'EMAIL:user:'.$exclude_user_id, 'isValueUnique', false, false, 'trim', 'profile');
      		SmartyValidate :: register_validator('v_EMAIL_BANNED', 'EMAIL', 'isBannedEmail', false, false, 'trim', 'profile');

      		if (VISUAL_CONFIRM == 1) {
         		SmartyValidate :: register_criteria('isCaptchaValid', 'validate_captcha', 'profile');
         		SmartyValidate :: register_validator('v_CAPTCHA', 'CAPTCHA:IMAGEHASH', 'isCaptchaValid', false, false, 'trim', 'profile');
      		}
   		} else {
      		SmartyValidate :: connect($tpl, true);
     		$data = get_table_data('user');

      		//Take care of white-space chars
      		$data = filter_white_space($data);

      		if (VISUAL_CONFIRM == 1)
         		$data = array_merge ($data, array ('CAPTCHA' => $_POST['CAPTCHA'], 'IMAGEHASH' => $_POST['IMAGEHASH']));
         
      		if (SmartyValidate :: is_valid($_POST, 'profile')) {
      			
         		if ($mode == "edit" && !empty ($_SESSION['phpld']['user']['id']))
            		$id = $_SESSION['phpld']['user']['id'];
         		else
            		$id = $db->GenID($tables['user']['name'].'_SEQ');
        		if ($mode == "register"){
            		$data['REGISTRATION_DATE']     = $timenow;
            	$data['EMAIL_CONFIRMED'] = '0';
				}
         		if (isset ($data['CAPTCHA']))
            		unset($data['CAPTCHA']);
         		if (isset ($data['IMAGEHASH']))
            		unset ($data['IMAGEHASH']);

         		$unhashedPassword = (!empty ($data['PASSWORD']) ? $data['PASSWORD'] : '');

         		if (empty ($data['PASSWORD']) && $mode == "edit")
            		$data['PASSWORD'] = $db->GetOne("SELECT `PASSWORD` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($id));
         		else
            		$data['PASSWORD'] = encrypt_password($data['PASSWORD']);
         		if ($mode == "edit" && !empty ($_SESSION['phpld']['user']['level']))
            		$data['LEVEL'] = $_SESSION['phpld']['user']['level'];
         		else
            		$data['LEVEL']         = 0;
         		//author avatar related
         		if ($mode == "edit" && !empty ($_FILES['IMG']['name'])) {
            		$img = $_FILES['IMG']['name'];
            		$ext = substr($img, strrpos($img, '.'));
            		//make sure this directory is writable!
            		//the new width of the resized image, in pixels.
            		$img_thumb_width_small = 50; // thumb max width
            		$img_thumb_width = 200; // image max width
            		$imgname = $id;
            
            		/////////////////////////////////

            		// CREATE THE THUMBNAIL //
            
            		////////////////////////////////
            		$file_type = $_FILES['IMG']['type'];
            		$file_name = $_FILES['IMG']['name'];
            		$file_size = $_FILES['IMG']['size'];
            		$file_tmp = $_FILES['IMG']['tmp_name'];
            		$ThumbWidth = $img_thumb_width;
            		$ThumbWidthSmall = $img_thumb_width_small;
            
            		//keep image type
            		$getExt = explode ('.', $file_name);
            		$file_ext = $getExt[count($getExt)-1];
            
             		if($file_size){
                		if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                    		$new_img = imagecreatefromjpeg($file_tmp);
                    		$new_img2 = imagecreatefromjpeg($file_tmp);
                		}elseif($file_type == "image/x-png" || $file_type == "image/png"){
                    		$new_img = imagecreatefrompng($file_tmp);
                    		$new_img2 = imagecreatefrompng($file_tmp);
                		}elseif($file_type == "image/gif"){
                    		$new_img = imagecreatefromgif($file_tmp);
                    		$new_img2 = imagecreatefromgif($file_tmp);
                		}
                
                		//list the width and height and keep the height ratio.
                		list($width, $height) = getimagesize($file_tmp);
                		//calculate the image ratio
                		$imgratio=$width/$height;
                		if ($imgratio>1){
                    		$newwidth = $ThumbWidth;
                    		$newheight = $ThumbWidth/$imgratio;
                    		$newwidth2 = $ThumbWidthSmall;
                    		$newheight2 = $ThumbWidthSmall/$imgratio;
                		}else{
                    		$newheight = $ThumbWidth;
                    		$newwidth = $ThumbWidth*$imgratio;
                    		$newwidth2 = $ThumbWidthSmall;
                    		$newheight2 = $ThumbWidthSmall/$imgratio;
                		}
                
                		//function for resize image.
                		if (function_exists(imagecreatetruecolor)){
                    		$resized_img = imagecreatetruecolor($newwidth,$newheight);
                    		$resized_img2 = imagecreatetruecolor($newwidth2,$newheight2);
                		}else{
                    		die("Error: Please make sure you have GD library ver 2+");
                		}
                
                		//the resizing is going on here!
                		imagealphablending($resized_img, false);
                		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                		imagealphablending($resized_img2, false);
                		imagecopyresized($resized_img2, $new_img2, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
                		//finally, save the images
                		if (file_exists(INSTALL_PATH.'/images/author/'.$imgname.'.'.$file_ext)) {
                    		unlink(INSTALL_PATH.'/images/author/'.$imgname.'.'.$file_ext);
                    		unlink(INSTALL_PATH.'/images/author/thumb/'.$imgname.'.'.$file_ext);
                		}
                
                		if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                    		ImageJpeg($resized_img, INSTALL_PATH.'/images/author/'.$imgname.'.'.$file_ext);
                    		ImageJpeg($resized_img2, INSTALL_PATH.'/images/author/thumb/'.$imgname.'.'.$file_ext);
                		}elseif($file_type == "image/x-png" || $file_type == "image/png"){
                    		imagesavealpha($resized_img, true);
                    		imagesavealpha($resized_img2, true);
                    		ImagePng($resized_img, INSTALL_PATH.'/images/author/'.$imgname.'.'.$file_ext);
                    		ImagePng($resized_img2, INSTALL_PATH.'/images/author/thumb/'.$imgname.'.'.$file_ext);
                		}elseif($file_type == "image/gif"){
                    		ImageGIf($resized_img, INSTALL_PATH.'/images/author/'.$imgname.'.'.$file_ext);
                    		ImageGIf($resized_img2, INSTALL_PATH.'/images/author/thumb/'.$imgname.'.'.$file_ext);
                		}
                
                		ImageDestroy ($resized_img);
                		ImageDestroy ($new_img);
                		ImageDestroy ($resized_img2);
                		ImageDestroy ($new_img2);
             		}
            		//ok copy the finished file to the thumbnail directory
            		//move_uploaded_file ($file_tmp, INSTALL_PATH.'imgs/'.$imgname.'.'.$file_ext);
           			$data['AUTH_IMG'] = SITE_URL.'images/author/'.$imgname.'.'.$file_ext;
            		$data['AUTH_IMGTN'] = SITE_URL.'images/author/thumb/'.$imgname.'.'.$file_ext;
         		}
         
         		//end

       	  		$data['ID'] = $id;
         		$data['RANK']          = 0;
         		$data['ACTIVE']        = 1;
         		$data['SUBMIT_NOTIF']  = 0;
         		$data['PAYMENT_NOTIF'] = 0;

         		if($mode == "edit") {
         			$data['LOGIN'] = $db->GetOne("SELECT `LOGIN` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($id));
            		$mode  = "UPDATE";
            		$where = " `ID` = ".$db->qstr($id);
         		} else {
            		$mode  = "INSERT";
            		$where = false;
         		}
				//print_array($data);
         		if ($db->AutoExecute($tables['user']['name'], $data, $mode, $where)) {
            		//Send user settings confirmation email

            		//Get email template
            		$emailTpl = get_email_template('NTF_USER_DETAILS_TPL');

            		$tplSql   = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr(NTF_USER_DETAILS_TPL);
            		$emailTpl = $db->GetRow($tplSql);

            		//if password changed on edit and an email tpl is available
            		if (!empty ($unhashedPassword) && is_array ($emailTpl) && !empty ($emailTpl)) {
               			//Send unhashed password
               			$data['PASSWORD'] = $unhashedPassword;

               			$mail = get_emailer_admin();

               			//Add email subject
               			$emailSubject = $emailTpl['SUBJECT'];
               			$emailSubject = replace_email_vars($emailSubject, $data, 5);
               			$mail->Subject = trim ($emailSubject);

               			//Add owner email address
               			$mail->AddAddress($data['EMAIL'], $data['NAME']);

               			//Add email body
               			$emailBody = $emailTpl['BODY'];
               			$emailBody = replace_email_vars($emailBody, $data, 5);
               			$mail->Body = trim ($emailBody);

               			//Send email
               			if (!$mail->Send()){
                  			$error++;
                  			$tpl->assign('error', $error);
                  			$errorMsg = $mail->ErrorInfo;
                  			$tpl->assign('errorMsg', $errorMsg);
               			}

               			//Clear all addresses (and attachments) for next loop
               			$mail->ClearAddresses();
               			$mail->ClearAttachments();

               			//Free memory
               			unset ($unhashedPassword, $mail, $emailBody, $emailSubject);
            		}

            		
            		
            		$_SESSION['phpld']['user']['language'] = $data['LANGUAGE'];
            		$_SESSION['user_language'] = $data['LANGUAGE'];
               		
              		if ($mode == "edit" || !empty ($_SESSION['phpld']['user']['id'])){
               			
						$_SESSION['phpld']['user']['profileUpdate'] = 1;
			   			http_custom_redirect(DOC_ROOT.'/profile.php?mode=logged');
            		} else {
               			//echo $mode." - ".$error;
               			if ($error < 1) {
                   			//user has registered successfully, send confirmation mail (IF NEEDED)
                   			if(EMAIL_CONFIRMATION == 1) {
                       			$mail = get_emailer_admin();
    
                       			//Add email subject
                       			$emailSubject = "Email Confirmation";
                       			$mail->Subject = trim ($emailSubject);
        
                       			//Add owner email address
                       			$mail->AddAddress($data['EMAIL'], $data['NAME']);
        
                       			//Add email body
                       			$emailBody = "Please confirm email address here: ".SITE_URL."confirm_email.php?uid=".$data['ID'];
                       			$mail->Body = trim ($emailBody);
        
                       			//Send email
                       			if (!$mail->Send()) {
                          			$error++;
                          			$tpl->assign('error', $error);
                          			$errorMsg = $mail->ErrorInfo;
                          			$tpl->assign('errorMsg', $errorMsg);
                       			}                     
                       			//Clear all addresses (and attachments) for next loop
                       			$mail->ClearAddresses();
                       			$mail->ClearAttachments();
        
                       			//Free memory
                       			unset ($unhashedPassword, $mail, $emailBody, $emailSubject);
                       
                      			//end of confirmation email
                   			}
                  			http_custom_redirect(DOC_ROOT.'/profile.php?mode=thankyou');
               			}
            		}
            		//Free memory
            		unset ($data, $emailTpl);
         		}
         		//Remove unhashed password (if still available)
         		unset ($unhashedPassword);
      		}
   		}
   	break;
}

//Determine length of author's info field
$INFO_limit = (isset ($data['INFO']) && strlen (trim ($data['INFO'])) > 0 ? 255 - strlen (trim ($data['INFO'])) : '255');
$tpl->assign('InfoLimit', $INFO_limit);

$tpl->assign('yesno', array("1" => "Yes", "0" => "No"));

if (VISUAL_CONFIRM == 1)
   $tpl->assign('captcha_length', CAPTCHA_PHRASE_LENGTH);

if (!empty ($registration_errors))
   $tpl->assign('registration_errors', $registration_errors);

$path   = array();
$path[] = array ('ID' => '0', 'TITLE' => _L(DIRECTORY_TITLE)    , 'TITLE_URL' => DOC_ROOT, 'DESCRIPTION' => SITE_DESC);
$path[] = array ('ID' => '0', 'TITLE' => _L('User Management'), 'TITLE_URL' => ''      , 'DESCRIPTION' => '');

$tpl->assign('path'               , $path);
$tpl->assign('SITE_NAME'          , SITE_NAME);
$tpl->assign('ALLOW_AUTHOR_INFO'  , ALLOW_AUTHOR_INFO);
$tpl->assign('ALLOW_AVATARS'      , ALLOW_AVATARS);

$tpl->assign($data);
unset ($data);

//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

//Make output
echo $tpl->fetch('profile.tpl', $id);
?>