Location: PHPKode > scripts > phm Instant Messenger Core > phm-instant-messenger-core/msg.cls.php
<?php
/*
* phm Instant Messenger pre-Alfa 0.1
* 
* Developed by Martin Barker 
* This Header must not be removed this file is open source under GLP and LGPL
* No other code will be provided
*/
class im{
	var $con;
	
	function connect($host, $user, $pass, $db){
		$this->con = mysql_connect($host, $user, $pass);
		mysql_select_db($db, $this->con);
	}
	
	function login($user, $pass){// Allows Users to login
		$pass = md5($pass);
		$sql = "SELECT * FROM `users` WHERE `username` = '$user' AND `password` = '$pass'";
		$query = mysql_query($sql,$this->con);
		$return = mysql_fetch_array($query);
		if($return['user'] == $user){// found users with correct pass
			$_SESSION['id'] = $return['id'];
			$_SESSION['user'] = $return['username'];
			return $return['id'];
		}else{// user not found with username and password
			return false;
		}
	}
	
	function changePass($user,$pass){ // to be used after forgotPass to reset pass using username and new password
			$sql = "SELECT * `users` WHERE `username` = '$user'"; // get user information
			$query = mysql_query($sql,$this->con);
			$return = mysql_fetch_array($query);
			$pass = md5($pass);
			$sql = "UPDATE `users` SET `password` = '$pass' WHERE `username` = '$user'";// set the new password
			$query = mysql_query($sql, $this->con);
	}
	
	function forgotPass($user, $answer = ''){// forgot password recover
		if($answer != ''){// on the first stage
			$sql = "SELECT * `users` WHERE `username` = '$user'"; // get user information
			$query = mysql_query($sql,$this->con);
			$return = mysql_fetch_array($query);
			if($return['user'] == $user){// if user exsits
				return $return['quest'];
			}else{// if user dose not exsit
				return false;
			}
		}else{// second stage
			$sql = "SELECT * `users` WHERE `username` = '$user'"; // get user information
			$query = mysql_query($sql,$this->con);
			$return = mysql_fetch_array($query);
			if($return['answer'] == $answer){// check if they ented the correct answer
				return true;// correct allow them to change password
			}else{
				return false;// incorrect anser
			}
		}
	}
	
	function addFreind($fid,$user){
		$sql = "INSERT INTO `freinds` VALUES (NULL, '$fid', '$user', 0)"; // adds the freind to be used after the findFreind
		$query = mysql_query($sql, $this->con);
		if($query){
			return true; // freind added
		}else{
			return false; // System error
		}
	}
	
	function createAcc($fname, $lname, $user, $pass, $quest, $answer){// allows users to create accounts
		$sql = "INSERT INTO `users` VALUES ( NULL, '$fname', '$lname', '$user', '".md5($pass)."', '$quest', '$answer')";
		$query = mysql_query($sql, $this->sql);
		if($query){
			return true; // created account
		}else{
			return false; // system error
		}
	}
	
	function findFreind($user = '', $email = ''){// only retunrs users ID for username or email 
		if($user != ''){// searching by username
			$sql = "SELECT * FROM `users` WHERE `username` = '$username'"//find user
			$query = mysql_query($sql,$this->con);
			$return = mysql_fetch_array($query);
			return $return['id']
		}
		if($email != ''){// sarching by email
			$sql = "SELECT * FROM `users` WHERE `email` = '$email'"
			$query = mysql_query($sql,$this->con);
			$return = mysql_fetch_array($query);
			return $return['id']
		}
		if($email == '' && $user == ''){// no username or email address spesifyed
			return false;
		}
	}
	
	function getUserNick($user){ // retrevies a users nickname and displayed
		$sql = "SELECT * FROM `users` WHERE `username` = '$user'";
		$query = mysql_query($sql, $this->con);
		$row = mysql_fetch_array($query);
		return $row['nick'];
	}
	
	function sendMsg($user, $to, $msg){// sends a message
		$sql = "INSERT INTO `msg` VALUES (NULL, '$user', '$msg', '$to')";// add msg to db
		if($query){
			return true; // added the msg
		}else{
			return false; // system error
		}
	}
	
	function awatingAccept($user){// lists freind requests for the loged in user
		$sql = "SELECT * FROM `freinds` WHERE `invited`='$user' AND `allowed` = 0"
		$query = mysql_query($sql, $this->con)
		$out = '';
		while($return = mysql_fetch_array($query)){
			$out .= $return.',';			
		}
		return substr_replace($out,"",-1); // returns list of freinds comma seperated
	}
	
	function allowAccept($user, $friend){// allows a freind
		$sql = "UPDATE `freinds` SET `allowed`='1' WHERE `invited`='$user' AND `invite`='$friend'";
		mysql_query($sql, $this->con);
		return true; // returns true
	}
	
	function reciveMsgs($user){ // get all held messages from the system
		$output = '';
		$sql = "SELECT * FROM `msg` WHERE `to` = '$user' ORDER BY `id` ASC";
		$query = mysql_query($sql, $this->con);
		while($results = mysql_fetch_array($query)){
			$output .= $results['from'].':'.$this->smiles($results['msg']).';';// add msg to output
			$sql1 = "DELETE FROM `msg` WHERE `id`='".$results['id']."'";// delete msg from system
			$query1 = mysql_query($sql,$this->con);
		}
		return $this->smiles($output);
	}
	
	function bbcode($text){// allows the use of bbcode in the messages requires a webbrowser to display messages
		$Text = str_replace("<", "&lt;", $Text);
		$Text = str_replace(">", "&gt;", $Text);
		$Text = nl2br($Text);
		$URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
		$MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";
		$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="$1" target="_blank">$1</a>', $Text);
		$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);
		$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);
		$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
		$Text = preg_replace("(\[b\](.+?)\[\/b])is",'<span class="bold">$1</span>',$Text);
		$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span class="italics">$1</span>',$Text);
		$Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<span class="underline">$1</span>',$Text);
		$Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text);
		$Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
		$Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text);
		$Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1px\">$2</span>",$Text);
		$Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);
		$Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);
		$Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '<ul class="listlowerroman">$1</ul>' ,$Text);
		$Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);
		$Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);
		$Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);
		$Text = str_replace("[*]", "<li>", $Text);
		$Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
		$CodeLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
							<tr>
								<td class="quotecodeheader"> Code:</td>
							</tr>
							<tr>
								<td class="codebody">$1</td>
							</tr>
					   </table>';
		$Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text);
		$QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
							<tr>
								<td class="quotecodeheader"> Quote:</td>
							</tr>
							<tr>
								<td class="quotebody">$1</td>
							</tr>
					   </table>';
		$Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text);
		$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $Text);
		$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text);
		
		return $Text;// outputs html code
	}
	
	function smiles($msg){// use str_replace to replace text with images if you wish to allow bbcode run $this->bbcode($msg); this is HTML only
		// using smiles use BBCode to show [img]Src Address[/img]
		// leave till the end
		$msg = bbcode($msg);
		return $msg
	}
	
	function addAvatar($user, $imagLoc){
		$sql = "UPDATE `image` SET `image`='$imgLoc' WHERE `username`='$user'";
		$query = mysql_query($sql,$this->con);
		return true;
	}
	
	function avatar($user){// get Users Image
		$sql = "SELECT `image` FROM `Users` WHERE `username`='$user'";
		$query = mysql_query($sql,$this->con);
		$row = mysql_fetch_array($query);
		return $row['image'];
	}
	
	function freinds($user){ // get a users freinds from Database
		$sql = "SELECT * FROM `freinds` WHERE (`invite` = '$user' OR `invited` = '$user') AND `allowed` = 1";
		$query = mysql_query($sql, $this->con);
		$out = '';
		while($return = mysql_fetch_array($query)){
			if($return['invite'] == $user){
				$out .= $return['invited'].',';
			}else{
				$out .= $return['invite'].',';
			}
		}
		return substr_replace($out,"",-1); // returns a comma seperated list of freinds
	}

}
?>
Return current item: phm Instant Messenger Core