<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// Copyright (C) 2003, 2005 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// 

// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

class User {
	// CLASS FUNCTIONS
	// User
	// checkLogin
	// loginUser
	// setLocale
	// authUser
	// getStatus
	// getUser
	// emailVerification
	// getLanguages
	// getProfile
	// getTemplates
	// getTemplate
	// deleteLanguages
	// getProfiles
	// updateUser
	// insertLanguages
	// createPassword
	// insertProfile
	// updateProfile
	// deleteProfile
	// updateDefaultProfile
	// getDegreeProfiles
	// updateDegreeProfiles
	
	
	
	// User --------------------------------------------------------------------
	// the constuctor
	// tom calthrop - 19th April 2004
	//
	function User($db, $date) {
		$this->db = $db;
		$this->date = $date;
	}//EO User
	
	
	
	// checkLogin --------------------------------------------------------------------
	// checks if the person can log in
	// tom calthrop - 19th April 2004
	//
	function checkLogin($email, $password){
		if(empty($email))
			$GLOBALS['am_error_log'][] = array(2, null, null);
		else{
			$query = "
				SELECT user_id FROM " . $this->db->prefix . "_user
				WHERE user_email='" . $email . "'"
			;
			
			$result = $this->db->Execute($query);

			if(empty($result->fields['user_id']))
				$GLOBALS['am_error_log'][] = array(24, null, null);
		}

		// Check whether the password is in the database and correct
		if(empty($password))
			$GLOBALS['am_error_log'][] = array(25, null, null);
		else{
			if(empty($GLOBALS['am_error_log']))
			{
				$query = "
					SELECT user_id, status_id 
					FROM " . $this->db->prefix . "_user
					WHERE
					user_email='" . $email . "' AND
					user_password='" . MD5($password) . "'"
				;
				
				$result = $this->db->Execute($query);

				if(empty($result->fields['user_id'])) {
					$GLOBALS['am_error_log'][] = array(19, null, null);
				}

				if($result->fields['status_id'] != $this->status_ids['active']) {
					$GLOBALS['am_error_log'][] = array(26, null, null);
				}
			}
		}

	}//EO checkLogin
	

	
	// loginUser --------------------------------------------------------------------
	// logs in the user
	// tom calthrop - 19th April 2004
	//
	function loginUser($email, $password, $node_id) {

		$query = "SELECT
			u.user_id, u.role_id, u.user_email, u.user_firstname, u.user_surname, 
			u.user_homepage, t.template_title, u.language_id, 
			MD5(UNIX_TIMESTAMP() + user_id + RAND(UNIX_TIMESTAMP())) AS session_id
			FROM " . $this->db->prefix . "_user u, " . $this->db->prefix . "_template t
			WHERE u.template_id = t.template_id AND
			user_email='" . $email . "' AND
			user_password='" . MD5($password) . "'"
		;
		
		$result = $this->db->Execute($query);

		if (isset($result->fields['user_id'])){
			// Starts a session
			if (empty($_SESSION)) {
			    session_start();
			}

			$this->user_id = $result->fields['user_id'];

			// Sets the sessionvariables
			$_SESSION['s_user_id'] = 		$result->fields['user_id'];
			$_SESSION['s_email'] = 			$result->fields['user_email'];
			$_SESSION['s_firstname'] = 		$result->fields['user_firstname'];
			$_SESSION['s_surname'] = 		$result->fields['user_surname'];
			$_SESSION['s_role_id'] = 		$result->fields['role_id'];
			$_SESSION['s_md5'] = 			md5($result->fields['session_id']);
			$_SESSION['s_language_id'] = 	$result->fields['language_id'];
			$_SESSION['s_homepage'] = 		$result->fields['user_homepage'];
			$_SESSION['s_user_template'] = 	$result->fields['template_title'];
			$_SESSION['s_breadcrumb'] = 	array();
			$_SESSION['s_language_ids'] = 	$this->getLanguages();
			
			$timestamp = $this->db->DBTimeStamp(mktime());

			$query =
				"INSERT INTO " . $this->db->prefix . "_user_session
				(
				session_md5,
				session_firstused,
				session_lastused,
				session_ip,
				user_id
				) values (
				'" . $_SESSION['s_md5'] . "',
				" . $timestamp . ",
				" . $timestamp . ",
				'" . $_SERVER['REMOTE_ADDR'] . "',
				'" . $this->user_id . "'
				)"
			;
			
			$this->db->Execute($query);

			if (isset($result)) {
				//get friends and aquintances
				$this->sn->user_id = $result->fields['user_id'];
				$_SESSION['s_social_network'] = $this->sn->createNetwork();
				$this->sn->updateSnStatistics();
				
				//set total logins statistic
				$this->statistic->section_item_id = $this->user_id;
				$this->statistic->statistic_type_id = $this->total_login_stat_id;
				$total_logins = $this->statistic->getStatistic();
				if (empty($total_logins)) {
				    $total_logins = 0;
				}
				$total_logins++;
				$this->statistic->setStatistic($total_logins);
				
				
				//set locale
				if (isset($result->fields['language_id'])) {
				    $this->setLocale();
				}

				return header("Location: " . $this->aroundme_dir . $result->fields['user_homepage']."?".SID);
				exit;
			}
			else {
				$GLOBALS['am_error_log'][] = array(1, 1, $this->db->ErrorMsg());
			}
		}
		else {
			$GLOBALS['am_error_log'][] = array(19, null, null);
		}
	}//EO loginUser


	
	// setLocale --------------------------------------------------------------------
	// setup local language for date setting etc
	// tom calthrop - 19th April 2004
	//
	function setLocale($language_id = null) {

		if ($language_id == "2") { //Albanian
		    setlocale(LC_ALL,"alb");
		}
		elseif ($language_id == "3") { //Serbian
		    setlocale(LC_ALL,"sr");
		}
		else { //We default to English
		    setlocale(LC_ALL,"eng");
		}
	}


	function logoffUser () {
		if (!isset($_SESSION)) {
		    session_start();
		}

		$_SESSION = array();
		session_destroy();
	}//EO setLocale
	


	// authUser --------------------------------------------------------------------
	// check that the user is authorised for entry into system
	// tom calthrop - 19th April 2004
	//
	function authUser () {
		session_start();

		// Check for a session-variable containing the session-id, if none go to login page
		if(empty($_SESSION['s_md5'])) {
			header("Location: index.php?" . SID);
			exit;
		}

		// Authenticate user
		$query = "
			SELECT session_md5
			FROM " . $this->db->prefix . "_user_session
			WHERE user_id = " . $_SESSION['s_user_id'] . "
			order by session_id desc limit 1";

		$result = $this->db->Execute($query);

		// Checks if the session-id in the database and in the session-variables agree
		if($result->fields['session_md5'] != $_SESSION['s_md5']) {
			$this->logoffUser();
			header("Location: index.php?" . SID);
			exit;
		}


		// Check if user is locked
		$query = "
			SELECT status_id FROM " . $this->db->prefix . "_user 
			WHERE 
			user_id = " . $_SESSION['s_user_id']
		;

		$result = $this->db->Execute($query);

		// If user is not active...
		if($result->fields['status_id'] != $this->status_ids['active']) {
			header("Location: index.php?" . SID);
			exit;
		}

		
		// Setting the user to online
		$timestamp = $this->db->DBTimeStamp(mktime());
		
		$query = "
			UPDATE " . $this->db->prefix . "_user_session SET
			session_lastused=" . $timestamp . "
			WHERE
			session_md5='". $_SESSION['s_md5'] . "'"
		;

		$result = $this->db->Execute($query);

		$query = "
			UPDATE " . $this->db->prefix . "_user
			SET active_time=" . $timestamp . "
			WHERE
			user_id=". $_SESSION['s_user_id']
		;
		
		$result = $this->db->Execute($query);
		
		// Logging out people who have not been active for 12 hours - they wil be directed
		// to the login-page as their session-id will not match the database session-id
		$logout_timestamp = $timestamp-(720*60);

		$query = "
			SELECT user_id
			FROM " . $this->db->prefix . "_user
			WHERE " . $logout_timestamp . " > active_time"
		;

		$result = $this->db->Execute($query);  // Logout if not active for 720 minutes

		// Going through the list and changing the sessions-ids,
		// which forces "really" inactive users to log on again....
		if (isset($result->fields)) {
			while (!$result->EOF) {
				$user_id = $result->fields['user_id'];
	
				$query = "
					UPDATE " . $this->db->prefix . "_user_session
					SET session_md5 is null
					WHERE
					user_id=" . $user_id
				;
				$this->db->Execute($query);
				$result->MoveNext();
		  	}
		}

	}//EO authUser


	
	// getStatus --------------------------------------------------------------------
	// gets the current status of the user
	// tom calthrop - 19th April 2004
	//
	function getStatus($active_time = null){

		$status = 4; //offline

		$time[1] = 0; //online
		$time[2] = 15; //idle
		$time[3] = 20; // sleeping
		$time[4] = 30; //offline

		foreach($time as $key => $t):
			$timedif = $this->db->UserTimeStamp(mktime()-(60*$t), "YmdHis");

			if($active_time <= $timedif) {
				$status = $key;
			}

		endforeach;

		return $status;
	}//EO getStatus
	
	

	// getStatus --------------------------------------------------------------------
	// change the user's status... NOT YET FULLY WORKING!
	// tom calthrop - 19th April 2004
	//
	function setStatus($status){
		$time[2] = 15;
		$time[4] = 20;
		$time[6] = 30;
		
		$query = "
			UPDATE " . $this->db->prefix . "_user SET 
			active_time=UNIX_TIMESTAMP()-60*" . $time[$status] . " 
			WHERE 
			user_id=". $_SESSION["s_user_id"]
		;
		
		$this->db->Execute($query);
	}//EO getStatus

	


	// getUser --------------------------------------------------------------------
	// retrieves single DB row for user
	// tom calthrop - 19th April 2004
	//
	function getUser($user_id = null) {
		if (empty($user_id) && isset($this->user_id)) {
			$user_id = $this->user_id;
		}
		
		$query = "
			SELECT u.user_id, u.user_hometown, u.role_id, u.user_create_datetime, 
			u.language_id, u.template_id, u.user_firstname, 
			u.user_surname, u.user_email, u.user_dob, u.user_gender, u.user_number, 
			u.active_time, u.status_id, u.user_file_allocation, u.user_homepage 
			FROM " . $this->db->prefix . "_user u 
			WHERE "
		;
		
		if (isset($user_id)) {
		    $query .= "u.user_id=" . $user_id . " and ";
		}
		
		if (isset($this->user_email)) {
		    $query .= "u.user_email='" . $this->user_email . "' and ";
		}
		
		$query .= "1=1";
		
		$result = $this->db->Execute($query);

		if (isset($result->fields['user_id'])) {
			$temp = $result->fields;
			$temp['display_user_create_datetime'] = $this->date->formatDate($temp['user_create_datetime']);
			
			$this->user_id = $temp['user_id'];
			
			$result->Close();
			return $temp;
		}
		else {
			header("Location: sn_network.php?" . SID);
			exit;
		}
	}//EO getUser
	
	
	
	// emailVerification --------------------------------------------------------------------
	// sends user an email with the password
	// tom calthrop - 19th April 2004
	//
	function emailVerification($password, $email, $firstname, $language_id, $txt_template){
		//pick up the language file

		$body_file_txt = readTxtFile($this->aroundme_dir . "language/" . $language_id . "/" . $this->language_dir . "/texts/" . $txt_template);

		$body_file_txt = str_replace("\\\"", "\"", $body_file_txt);
		$body_file_txt = str_replace("USERNAME", $firstname, $body_file_txt);
		$body_file_txt = str_replace("EMAIL", $email, $body_file_txt);
		$body_file_txt = str_replace("PASSWORD", $password, $body_file_txt);
		
		//create URL
		$url = "http://" . $_SERVER['HTTP_HOST'];
		
		if (dirname ($_SERVER['PHP_SELF']) != "/") {
		    $url .= dirname ($_SERVER['PHP_SELF']);
			
			//if the aroundme directory is not barnraiser/aroundme..." then we need
			// to create a url for the upper directory, hence we trim off the last 
			//part of the url
			//aroundme is not barn... if the node_register_user form is completed and not the
			// standard registration form
			if (empty($this->aroundme_dir)) {
			    $last_dir_pos = strrpos($url, "/");
				$url = substr($url, 0, $last_dir_pos);
			}
			
			$url .= "/";
		}
		
		$url .= "setup.php?em=" . $email . "&language_id=" . $language_id;
		
		if (isset($user->invitee_id)) {
		    $url = $url . "invitee=" . $this->user_id;
		}

		$body_file_txt = str_replace("URL", $url, $body_file_txt);

		//send email to barnraiser
		$this->mail->Body = $body_file_txt;
		$this->mail->AddAddress($email, $firstname);

		if(!$this->mail->Send()) {
			$error_info = "function=emailVerification class=User.class.php -- Mailer Error: -- " . $this->mail->ErrorInfo;
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO emailVerification
	
	
	
	// getLanguages --------------------------------------------------------------------
	// get the languages that the user can read
	// tom calthrop - 18th Nov 2004
	//
	function getLanguages() {
		$query = "
			SELECT language_id FROM " . $this->db->prefix . "_user_language 
			WHERE
			user_id=" . $this->user_id
		;
		
		$result = $this->db->Execute($query);

		if (isset($result->fields)) {
			$temp = array();

			while (!$result->EOF) {
				array_push($temp, $result->fields['language_id']);
				$result->MoveNext();
			}

			$result->Close();
			return $temp;
		}
		else {
			$error_info = "function=getLanguages class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getLanguages


	
	// getProfile --------------------------------------------------------------------
	// gets the user profile
	// tom calthrop - 19th December 2004
	//
	function getProfile($profile_id = null) {
		if (empty($profile_id) && isset($this->profile_id)) {
		    $profile_id = $this->profile_id;
		}

		$query = "
			SELECT p.profile_id, p.profile_name, p.profile_summary, p.profile_nic, 
			p.profile_msn_account, p.profile_yahoo_account, p.profile_icq_account, 
			p.file_id, p.profile_skype_account, p.profile_hometown, p.user_id, 
			p.profile_create_datetime, p.profile_mobile_phone, p.profile_default, 
			u.active_time, p.profile_gender, p.profile_allow_comments, p.profile_allow_msg 
			FROM " . $this->db->prefix . "_user_profile p, " . $this->db->prefix . "_user u 
			WHERE 
			p.user_id=u.user_id AND "
		;
		
		if (isset($profile_id)) {
		    $query .= "p.profile_id=" . $profile_id . " AND ";
		}
		
		if (empty($profile_id)) {
		    $query .= "
				p.profile_default=1 AND 
				p.user_id=" . $this->user_id . " AND "
			;
		}
		
		$query .= "1=1";
		//echo $query;
		$result = $this->db->Execute($query);

		if (isset($result->fields)) {

			$temp = $result->fields;
			
			$temp['display_profile_create_datetime'] = $this->date->formatDate($temp['profile_create_datetime'], 1);

			//if the user is me we set to "online", else we find the status
			if (isset($_SESSION['s_user_id']) && $_SESSION['s_user_id'] == $temp['user_id']) {
			    $temp['online_status'] = 1;
			}
			else {
				$temp['online_status'] = $this->getStatus($temp['user_id']);
			}
			
			$result->Close();
			return $temp;
		}
		else {
			$error_info = "function=getProfile class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
		
	}//EO getProfile
	


	// getTemplates --------------------------------------------------------------------
	// gets the available templates
	// tom calthrop - 17th April 2004
	//
	function getTemplates() {
		$query = "
			SELECT t.template_id, t.template_title 
			FROM " . $this->db->prefix . "_template t
			order by t.template_title"
		;
	
		$result = $this->db->Execute($query);
	
		if (isset($result->fields)) {
		
			$temp = array();
	
			while (!$result->EOF) {
				array_push($temp, $result->fields);
				$result->MoveNext();
			}
	
			$result->Close();
			return $temp;
		}
		else {
			$error_info = "function=getTemplates class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getTemplates
	
	
	
	// getTemplate --------------------------------------------------------------------
	// gets the available templates
	// tom calthrop - 17th April 2004
	//
	function getTemplate($template_id) {
		$query = "
			SELECT t.template_title
			FROM " . $this->db->prefix . "_template t
			WHERE
			t.template_id =" . $template_id
		;
	
		$result = $this->db->Execute($query);
	
		if (isset($result->fields)) {
			$temp = $result->fields;
			
			$result->Close();
			return $temp;
		}
		else {
			$error_info = "function=getTemplate class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getTemplate
	
	
	
	// deleteLanguages --------------------------------------------------------------------
	// deletes languages that a user can read
	// tom calthrop - 18th Nov 2004
	//
	function deleteLanguages() {

		$query = "
			DELETE FROM " . $this->db->prefix . "_user_language 
			WHERE
			user_id=" . $this->user_id
		;

		$result = $this->db->Execute($query);

		if (isset($result)) {
			return true;
		}
		else {
			$GLOBALS['am_error_log'][] = array(1, 1, $query . "-" . $this->db->ErrorMsg());
		}
	}//EO deleteLanguages
	


	// getProfiles --------------------------------------------------------------------
	// gets the available profile
	// tom calthrop - 17th April 2004
	//
	function getProfiles() {
		$query = "
			SELECT p.profile_id, p.profile_name, p.profile_nic, p.profile_default, 
			p.file_id, p.profile_create_datetime, p.profile_hometown, p.user_id, 
			p.profile_allow_msg, u.active_time   
			FROM " . $this->db->prefix . "_user_profile p, " . $this->db->prefix . "_user u 
			WHERE 
			p.user_id=u.user_id AND "
		;
		
		if (isset($this->user_ids)) {
			$user_ids = "";
			foreach($this->user_ids as $key => $i):
				$user_ids .= $i;
				if (count($this->user_ids) > $key+1) {
				    $user_ids .=",";
				}
			endforeach;
			$query .="p.user_id in (" . $user_ids . ") AND 
			p.profile_default=1 AND ";
		}
		else {
			$query .= "p.user_id=" . $this->user_id . " AND ";
		}
		
		$query .= "1=1 ";
		
		if (isset($this->orderby)) {
		    $query .= "order by " . $this->orderby;
		}
		else {
			$query .= "order by p.profile_name";
		}
		//echo $query;
		
		$result = $this->db->Execute($query);
	
		if (isset($result->fields)) {
		
			$temp = array();
	
			while (!$result->EOF) {
				$temp1 = $result->fields;
				
				if (isset($this->file)) {
					
					if (isset($temp1['file_id'])) {
						$this->file->file_id = $temp1['file_id'];
						$temp1['image'] = $this->file->getFileRecord();
					}
					else {
						$temp1['image']['file_title'] = "";
						$temp1['image']['file'] = $this->file->noimage;
					}
				}
				
				$temp1['profile_create_datetime'] = $this->date->formatDate($temp1['profile_create_datetime'], 1);

				//if the user is me we set to "online", else we find the status
				if (isset($_SESSION['s_user_id']) && $_SESSION['s_user_id'] == $temp1['user_id']) {
				    $temp1['online_status'] = 1;
				}
				else {
					$temp1['online_status'] = $this->getStatus($temp1['user_id']);
				}
				
				array_push($temp, $temp1);
				$result->MoveNext();
			}
	
			$result->Close();
			return $temp;
		}
		else {
			$error_info = "function=getProfiles class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getProfiles
	
	
	
	// updateUser --------------------------------------------------------------------
	// update non administration columns in user
	// tom calthrop - 19th December 2004
	//
	function updateUser($data) {
		if(empty($data['user_hometown'])) {
		$GLOBALS['am_error_log'][] = array(10, null, null);
		}
		
		if (empty($data['language_ids'])) {
			$GLOBALS['am_error_log'][] = array(28, null, null);
		}
		
		if(empty($data['user_firstname'])) {
			$GLOBALS['am_error_log'][] = array(5, null, null);
		}
		
		// Check the password
		if(!empty($data['user_password1']) || !empty($data['user_password2'])) {
			
			if(empty($data['user_password1']) != empty($data['user_password2'])) {
				$GLOBALS['am_error_log'][] = array(15, null, null);
			}
			
			if(strlen($data['user_password1']) < 5) {
				$GLOBALS['am_error_log'][] = array(16, null, null);
			}
		}
		
		if (empty($GLOBALS['am_error_log'])) {
			
			$data['user_dob'] = $this->db->DBDate($data['user_dob']);
			
			$query = "
				UPDATE " . $this->db->prefix . "_user SET 
				user_firstname = '" . $data['user_firstname'] . "',
				user_surname = '" . $data['user_surname'] . "',
				language_id = " . $data['language_id'] . ",
				template_id = " . $data['template_id'] . ",
				user_hometown = '" . $data['user_hometown'] . "',
				user_dob = " . $data['user_dob'] . ",
				user_homepage='" . $data['user_homepage'] . "',
				user_gender = '" . $data['user_gender'] . "' "
			;
			
			if (!empty($data['user_password1'])) {
				$query .= ", user_password = '" . md5($data['user_password1']) . "'";
			}
			
			$query .= "
				WHERE
				user_id=" . $this->user_id
			;
			
			$_SESSION['s_language_id'] = $data['language_id'];
			
			$result = $this->db->Execute($query);
				
			if (empty($result)) {
				$GLOBALS['am_error_log'][] = array(1, 1, $query . "-" . $this->db->ErrorMsg());
			}
		}
	} //EO updateUser;
	
	
	
	// insertLanguages --------------------------------------------------------------------
	// inserts languages that a user can read
	// tom calthrop - 18th Nov 2004
	//
	function insertLanguages($in_arr) {
		$rownums = count ($in_arr);
		
		for ($i=0; $i < $rownums; $i++) {
			$query = "
				INSERT INTO " . $this->db->prefix . "_user_language 
				(user_id, language_id) VALUES (
				" . $this->user_id . ", 
				" . $in_arr[$i] . ")"
			;
			
			$result = $this->db->Execute($query);

			if (empty($result)) {
				$error_info = "function=insertLanguages class=UserAdmin.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
				$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
			}
		}
		
	}//EO insertLanguages
	
	
	
	// createPassword --------------------------------------------------------------------
	// creates a password
	// tom calthrop - 19th April 2004
	//
	function createPassword(){

		// creates a password
		$totallength = 8;
		$salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
		srand((double)microtime()*1000000);
		$password = "";
		for ($i = 0; $i < $totallength; $i++) {
			$password .= substr ($salt, rand() % strlen($salt), 1);
		}

		return $password;
	}//EO createPassword
	
	
	
	// insertProfile --------------------------------------------------------------------
	// inserts a profile
	// tom calthrop - 27th December 2004
	//
	function insertProfile($data) {
		if (empty($data['profile_nic'])) {
			$GLOBALS['am_error_log'][] = array(7, null, null);
		}
		
		if (empty($data['profile_hometown'])) {
			$GLOBALS['am_error_log'][] = array(10, null, null);
		}
		
		if (empty($GLOBALS['am_error_log'])) {
			$datetime = date("Y-m-d H:i:s");
			$datetime = $this->db->DBDate($datetime);
	
			if (empty($data['profile_default'])) {
			    $data['profile_default'] = "null";
			}
			
			if (empty($data['file_id'])) {
			    $data['file_id'] = "null";
			}
			
			if (isset($data['profile_allow_msg'])) {
			    $data['profile_allow_msg'] = 1;
			}
			else {
			    $data['profile_allow_msg'] = "null";
			}
				
			if (empty($data['profile_gender']) || (isset($data['profile_gender']) && $data['profile_gender']<=0)) {
			    $data['profile_gender'] = "null";
			}
			
			if (empty($data['profile_allow_comments'])) {
			    $data['profile_allow_comments'] = "null";
			}
			if (empty($data['profile_allow_msg'])) {
			    $data['profile_allow_msg'] = "null";
			}
			
			$query = "
				INSERT INTO " . $this->db->prefix . "_user_profile (
				user_id,
				profile_default,
				profile_name,
				profile_summary,
				profile_nic,
				profile_create_datetime,
				profile_msn_account,
				profile_yahoo_account,
				profile_icq_account,
				profile_mobile_phone,
				profile_skype_account,
				profile_hometown,
				profile_gender,
				profile_allow_comments, 
				profile_allow_msg, 
				file_id
				) values (
				" . $this->user_id . ",
				" . $data['profile_default'] . ",
				'" . $data['profile_name'] . "',
				'" . $data['profile_summary'] . "',
				'" . $data['profile_nic'] . "',
				" . $datetime . ",
				'" . $data['profile_msn_account'] . "',
				'" . $data['profile_yahoo_account'] . "',
				'" . $data['profile_icq_account'] . "',
				'" . $data['profile_mobile_phone'] . "',
				'" . $data['profile_skype_account'] . "',
				'" . $data['profile_hometown'] . "',
				'" . $data['profile_gender'] . "',
				'" . $data['profile_allow_comments'] . "',
				'" . $data['profile_allow_msg'] . "',
				" . $data['file_id'] . ")"
			;
			
			$result = $this->db->Execute($query);
	
			if (isset($result->fields)) {
				$query = "
					SELECT profile_id  
					FROM " . $this->db->prefix . "_user_profile 
					where
					user_id=" . $this->user_id . " and 
					profile_create_datetime=" . $datetime 
				;
		
				$result = $this->db->SelectLimit($query, 1);
				
				if (isset($result->fields['profile_id'])) {
					$this->profile_id = $result->fields['profile_id'];
				}
				else {
					$error_info = "function=insertProfile class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
					$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
				}
			}
			else {
				$error_info = "function=insertProfile class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
				$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
			}
		}
	}//EO insertProfile
	
	
	
	// updateProfile --------------------------------------------------------------------
	// upates a profile
	// tom calthrop - 27th December 2004
	//
	function updateProfile($data) {
		if (empty($data['profile_nic'])) {
			$GLOBALS['am_error_log'][] = array(7, null, null);
		}
		
		if (empty($data['profile_hometown'])) {
			$GLOBALS['am_error_log'][] = array(10, null, null);
		}
		
		if (empty($GLOBALS['am_error_log'])) {
			$datetime = date("Y-m-d H:i:s");
			$datetime = $this->db->DBDate($datetime);
			
			if (empty($data['file_id'])) {
			    $data['file_id'] = "null";
			}
				
			if (empty($data['profile_gender']) || (isset($data['profile_gender']) && $data['profile_gender']<=0)) {
			    $data['profile_gender'] = "null";
			}
			
			if (empty($data['profile_allow_comments'])) {
			    $data['profile_allow_comments'] = "null";
			}
			
			if (empty($data['profile_allow_msg'])) {
			    $data['profile_allow_msg'] = "null";
			}
	
			$query = "
				UPDATE " . $this->db->prefix . "_user_profile SET 
				profile_name='" . $data['profile_name'] . "',
				profile_summary='" . $data['profile_summary'] . "',
				profile_nic='" . $data['profile_nic'] . "',
				profile_msn_account='" . $data['profile_msn_account'] . "',
				profile_yahoo_account='" . $data['profile_yahoo_account'] . "',
				profile_icq_account='" . $data['profile_icq_account'] . "',
				profile_mobile_phone='" . $data['profile_mobile_phone'] . "',
				profile_skype_account='" . $data['profile_skype_account'] . "',
				profile_hometown='" . $data['profile_hometown'] . "', 
				profile_gender=" . $data['profile_gender'] . ", 
				profile_allow_comments=" . $data['profile_allow_comments'] . ", 
				profile_allow_msg=" . $data['profile_allow_msg'] . ", 
				file_id=" . $data['file_id'] . " 
				WHERE 
				profile_id=" . $this->profile_id
			;
			
			$result = $this->db->Execute($query);
	
			if (empty($result)) {
				$error_info = "function=updateProfile class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
				$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
			}
		}
	}//EO updateProfile
	
	
	
	// deleteProfile --------------------------------------------------------------------
	// deletes a profile
	// tom calthrop - 27th December 2004
	//
	function deleteProfile($profile_id) {
		
		$query = "
			DELETE FROM " . $this->db->prefix . "_user_profile 
			WHERE 
			profile_id=" . $profile_id
		;
		
		$result = $this->db->Execute($query);
	
		if (empty($result)) {
			$error_info = "function=deleteProfile class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO deleteProfile
	
	
	
	// updateDefaultProfile --------------------------------------------------------------------
	// updates the DB with the default profile ID
	// tom calthrop - 22nd December 2004
	//
	function updateDefaultProfile($default_profile_id) {

		$query = "
			UPDATE " . $this->db->prefix . "_user_profile SET 
			profile_default=null 
			WHERE 
			user_id=" . $this->user_id
		;
		
		$result = $this->db->Execute($query);
		
		$query = "
			UPDATE " . $this->db->prefix . "_user_profile SET 
			profile_default=1 
			WHERE 
			profile_id=" . $default_profile_id
		;
		
		$result = $this->db->Execute($query);
	}//EO updateDefaultProfile
	


	// getDegreeProfiles --------------------------------------------------------------------
	// gets the available profile
	// tom calthrop - 17th April 2004
	//
	function getDegreeProfiles($degree_id=null) {
		$query = "
			SELECT profile_id, degree_id 
			FROM " . $this->db->prefix . "_sn 
			WHERE 
			user_id=" . $this->user_id . " AND "
		;
		
		if (isset($degree_id)) {
		    $query .= "degree_id=" . $degree_id . " AND ";
		}
		
		$query .= "1=1";
		
		$result = $this->db->Execute($query);
	
		if (isset($result->fields)) {
		
			$temp = array();
	
			while (!$result->EOF) {
				$temp1 = $result->fields;
				array_push($temp, $temp1);
				$result->MoveNext();
			}
	
			$result->Close();
			return $temp;
		}
		else {
			$error_info = "function=getDegreeProfiles class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getDegreeProfiles
	
	
	
	// updateDegreeProfiles --------------------------------------------------------------------
	// update profile degree
	// tom calthrop - 8th January 2005
	//
	function updateDegreeProfiles($data) {
		
		if (!empty($data)) {
			foreach($data as $key => $i):
			    $query = "
					UPDATE " . $this->db->prefix . "_sn SET 
					profile_id=" . $i . " 
					WHERE
					degree_id=" . $key . " and 
					user_id=" . $this->user_id
				;
				
				$result = $this->db->Execute($query);
			
				if (empty($result)) {
					$error_info = "function=updateDegreeProfiles class=User.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
					$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
				}
			endforeach;
	}
		
		
	} //EO updateDegreeProfiles
}
