Location: PHPKode > projects > AnoBBS 3DES encrypted forum and communication repository > anobbs-3des-encrypted-forum-and-communication-repository/progs/cFunctions.php
<?PHP
####################################################################################################
## The AnoChat/BBS functions. # v. 1.0 mlk #  Coded by Knut Møgster 2008 | http://www.redesign.no ##
####################################################################################################

## Loop through skin dir and return dir names##
function returnSkins($skin_dir) {
	global $root_dir;
	$retskins = array();
	if($dir = opendir($root_dir."/".$skin_dir)) {
			while (($file = readdir($dir)) !== false) {
			if(is_dir($root_dir."/".$skin_dir."/".$file) && $file != "." && $file != "..") {
				array_push($retskins, $file);
			}
		}
	}
	closedir($dir);
	return $retskins;
}

## Returns skin array as buttons ##
function returnSkinbuttons($skin_array, $dcode) {
	global $root_dir,$skin_dir;
	$retskinstring = "";
	for($i=0;$i<sizeof($skin_array);$i++) {
		$chkskinsts = $skin_dir."/".$skin_array[$i]."/skinthumb.jpg";
		if(file_exists($chkskinsts)) {
			$retskinstring .= "<a href=\"javascript:void(0);\" onClick=\"retChatz('".$dcode."', '".$skin_array[$i]."');resetMain();\"><img src=\"".$skin_dir."/".$skin_array[$i]."/skinthumb.jpg\" width=\"150\" height=\"137\" border=\"0\" style=\"border:#AAAAAA 1px solid;\" alt=\"".strtoupper($skin_array[$i])."\"></a>&nbsp;&nbsp;";
		}
	}
	return $retskinstring;
}

## Loop through chat dir and delete files older than sessruntime ##
function cleanChats($sessruntime) {
	global $chatfiledir;
	if($dir = opendir($chatfiledir)) {
			while (($file = readdir($dir)) !== false) {
			if (substr($file,-4) == ".cht") {
				if(filemtime($chatfiledir."/".$file) < $sessruntime) {
					unlink($chatfiledir."/".$file);
				}
			}
		}
	}
	closedir($dir);
}

## Query and create chat, and return loginlink ##
function qUerycode($dcode, $gstate) {
	global $chatfiledir,$chat_url,$sessruntime,$skin_dir,$root_dir;
	if(file_exists($chatfiledir."/".$dcode.".cht")) {
		$inskins = returnSkins($skin_dir);
		$skinbutstr = returnSkinbuttons($inskins, $dcode);
		return array("Yes the chat exists. Please start a chat window by clicking on the skin you prefer:<br><br>".$skinbutstr, "3");
		} else {
		if($gstate == "crcht") {
			cleanChats($sessruntime);
			createChat($dcode);
			$inskins = returnSkins($skin_dir);
			$skinbutstr = returnSkinbuttons($inskins, $dcode);
			return array($chdels."The chat is created. Please start a chat window by clicking on the skin you prefer:<br><br>".$skinbutstr, "2");
			} else {
			return array("No chat found. Do you want to create one?&nbsp;&nbsp;&nbsp;<a href=\"#\" class=\"printstatez\" onClick=\"qUery_code();return false\">&nbsp;&nbsp;&nbsp;&nbsp;Yes&nbsp;&nbsp;&nbsp;&nbsp;</a>&nbsp;&nbsp;<a href=\"".$chat_url."\" class=\"printstatez\">&nbsp;&nbsp;&nbsp;&nbsp;No&nbsp;&nbsp;&nbsp;&nbsp;</a>", "1");
		}
	}
}

## Create chatfile ##
function createChat($dcode) {
	global $chatfiledir;
	if(!file_exists($chatfiledir."/".$dcode.".cht")) {
		touch($chatfiledir."/".$dcode.".cht");
		$filestring = "### Chat file $dcode start ###\n|u||/u|\n|t||/t|\n|i|".date("Y-m-d G:i", time())."|/i|\n|Conversation below this|\n";
		$fp = fopen($chatfiledir."/".$dcode.".cht", "w");
		$fw = fwrite($fp, $filestring);
		fclose($fp);
	}
}

## Append line to chat file ##
function authPost($dcrypt, $dcode) {
	global $chatfiledir;
	$chkintime = time();
	$chkintime = date("G:i:s",$chkintime);
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$fp = fopen($chatfiledir."/".$dcode.".cht", "a");
		$fw = fputs($fp, $dcrypt."-".$chkintime."\n");
		fclose($fp);
	}
}

## Fetch and return chat content, renew user timestamp ##
function authChat($dcode, $chatuser) {
	global $chatfiledir;
	$chkintime = time();
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$rdc = $checkfiled;
		$handle = fopen($checkfiled, "r");
		if ($handle) {
    	while(!feof($handle)) {
       	 $buffer .= fgets($handle, 4096);
   		 }
    	fclose($handle);
		}
		$retset = retChfields($buffer);
		$ulist = trim($retset[2]);
		$ulistarr = explode("^", $ulist);
		if(sizeof($ulistarr) > 0) {
			for($i=0;$i<sizeof($ulistarr);$i++) {
				if($ulistarr[$i] != "") {
					$rgdarr = explode(":", trim($ulistarr[$i]));
					if(trim($rgdarr[0]) != $chatuser) {
						$ulsitring .= $ulistarr[$i]."^";
					}
				}
			}
			$ulsitring .= $chatuser.":".$chkintime;
			} else {
			$ulsitring = $chatuser.":".$chkintime;
		}
		updateList($dcode, $ulsitring, "|u|", "|/u|", $buffer);
		$redux_str_list = rawurlencode(str_replace("\n", "^", popChLines($buffer)));
		$retset[3] = $redux_str_list;
		$retset[2] = rawurlencode($ulsitring);
		return $retset;
		} else {
		return array("Wrong chat ID, sorry.");
	}
}

## Fetch and return chat content on ping ##
function authChatping($dcode, $chatuser) {
	global $chatfiledir;
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$rdc = $checkfiled;
		$handle = fopen($checkfiled, "r");
		if ($handle) {
    	while(!feof($handle)) {
       	 $buffer .= fgets($handle, 4096);
   		 }
    	fclose($handle);
		}
		$retset = retChfields($buffer);
		$redux_str_list = rawurlencode(str_replace("\n", "^", popChLines($buffer)));
		$retset[3] = $redux_str_list;
		return $retset;
		} else {
		return array("Wrong chat ID, sorry.");
	}
}

## Log out ##
function logOut($xuser, $dcode) {
	global $chatfiledir;
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$rdc = $checkfiled;
		$handle = fopen($checkfiled, "r");
		if ($handle) {
    	while(!feof($handle)) {
       	 $buffer .= fgets($handle, 4096);
   		 }
    	fclose($handle);
		}
		$retset = retChfields($buffer);
		$ulist = trim($retset[2]);
		$ulistarr = explode("^", $ulist);
		if(sizeof($ulistarr) == 1) {
			unlink($checkfiled);
			} else {
			if(sizeof($ulistarr) > 0) {
				for($i=0;$i<sizeof($ulistarr);$i++) {
					if($ulistarr[$i] != "") {
						$rgdarr = explode(":", trim($ulistarr[$i]));
						if(trim($rgdarr[0]) != $xuser) {
							$ulsitring .= $ulistarr[$i]."^";
						}
					}
				}
				$ulsitring = substr($ulsitring, 0, -1);
			}
			updateList($dcode, $ulsitring, "|u|", "|/u|", $buffer);
		}
	}
}

## Return array of chat content ##
function retChfields($fcontent) {
	$barr2 = explode("|Conversation below this|", $fcontent);
	$barr1 = explode("|/i|", $barr2[0]);
	$barr3 = explode("|i|", $barr1[0]);
	$timest = trim($barr3[1]);
	$barr4 = explode("|/t|", $barr3[0]);
	$barr5 = explode("|t|", $barr4[0]);
	$chtopic = trim($barr5[1]);
	$barr6 = explode("|/u|", $barr5[0]);
	$barr7 = explode("|u|", $barr6[0]);
	$userlist = trim($barr7[1]);
	return array($timest,$chtopic,$userlist,$retconvlayercont = rawurlencode(str_replace("\n", "^", trim($barr2[1]))));
}

## Clip chat lines. This will become deprecated ##
function popChLines($fcontent) {
	global $maxlines;
	$barrx = explode("|Conversation below this|", $fcontent);
	$barry = explode("\n", $barrx[1]);
	$nrstr = "";
	$yrrab = array_reverse($barry);
	if(sizeof($yrrab) > $maxlines) {
		$rmax = sizeof($yrrab);
		$tmaxl = $maxlines;
		for($i=$tmaxl;$i>=0;$i=$i-1)	{
			$nrstr .= trim($yrrab[$i])."\n";
		}
		} else {
		$nrstr = $barrx[1];
	}
	return $nrstr;
}

## Return $maxlines chatlines ##
function saveChLines($fcontent) {
	global $maxlines;
	$barrx = explode("|Conversation below this|", $fcontent);
	$barry = explode("\n", $barrx[1]);
	$nrstr = "";
	$yrrab = array_reverse($barry);
	if(sizeof($yrrab) > $maxlines) {
		$rmax = sizeof($yrrab);
		$tmaxl = $maxlines;
		for($i=$tmaxl;$i>=0;$i=$i-1)	{
			if(trim($yrrab[$i]) != "") {
				$nrstr .= trim($yrrab[$i])."\n";
			}
		}
		} else {
		$nrstr = $barrx[1];
	}
	return $nrstr;
}

## Update userlist. To be deprecated ##
function updateUsers($ulist, $dcode, $filearr) {
	global $chatfiledir;
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$filestring = "### Chat file $dcode start ###\n|u|".$ulist."|/u|\n|t|".$filearr[1]."|/t|\n|i|".$filearr[0]."|/i|\n|Conversation below this|\n".str_replace("^", "\n", rawurldecode($filearr[3]))."\n";
		$fp = fopen($chatfiledir."/".$dcode.".cht", "w");
		$fw = fwrite($fp, $filestring);
		fclose($fp);
	}
}

## Set topic for chat ##
function topicSet($chat_topic, $dcode) {
	global $chatfiledir;
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$handle = fopen($checkfiled, "r");
		if ($handle) {
    	while(!feof($handle)) {
       	 $buffer .= fgets($handle, 4096);
   		 }
    	fclose($handle);
		}
	}
	updateList($dcode, $chat_topic, "|t|", "|/t|", $buffer);
}

## Main function for updating chatfile fields ##
function updateList($dcode, $dstring, $field1, $field2, $dlist) {
	global $chatfiledir;
	$redux_str_list = saveChLines($dlist);
	$barrtexts = explode("|Conversation below this|", $dlist);
	$barr1 = explode($field2, $barrtexts[0]);
	$barr2 = explode($field1, trim($barr1[0]));
	$filestring = $barr2[0].$field1.$dstring.$field2.$barr1[1]."|Conversation below this|".$redux_str_list;
	$checkfiled = $chatfiledir."/".$dcode.".cht";
	if(file_exists($checkfiled)) {
		$fp = fopen($chatfiledir."/".$dcode.".cht", "w");
		$fw = fwrite($fp, $filestring);
		fclose($fp);
	}
}

## For future use ##

## Clean up id ##
function cleanMe($text) {
	$text = ereg_replace("[^[:digit:]]", "", $text);
	return(trim($text));
}
?>
Return current item: AnoBBS 3DES encrypted forum and communication repository