PDA

View Full Version : Modify a chat script


crane550
05-12-2006, 11:06 AM
Hi, I have a couple questions about a JAVA/PHP script I copy/pasted and have been trying to modify to suit my needs and make my own. (It was free to do so with.) I am new to scripting but really trying to learn this stuff.

Here is what I am working with so far: www.charlescrane.net/chat/index.html

The way this script works is it updates a txt file on the server whenever you send a message and then the clients pull it every 5 seconds. Really simple. It is not the fastest, but it is simple and reliable.

There are a lot of things I want to do with this, but small steps. My biggest annoyance is how the most recent posts show up at the top. I want to change that. Here is the code for it:

function rec_chatcontent(cont1) {
if (cont1 != "") {
out1 = "";
/* Display Last Message First */
while (cont1.indexOf("\n") > -1) {
out1 = cont1.substr(0, cont1.indexOf("\n")) + "\n" + out1;
cont1 = cont1.substr(cont1.indexOf("\n") + 1);
}
out1 = unescape(out1);
if (chatwindow.value != out1) { display_msg(out1); }
intUpdate=window.setTimeout("read_cont()", waittime);
}
}

crane550
05-12-2006, 11:16 AM
Here is the rest of it for kicks:

w.php:
<?php
/* spam-keywords & block ip's */

$spam[] = "wojo.com";
$blockip[] = "72.60.167.89";

$msg = $_REQUEST["m"];
if ($msg != "") {
/* Spam Control Part 1 */
if (strlen($msg) < 2) { die(); }
if (strlen($msg) > 3) {
if (strtoupper($msg) == $msg) { die(); }
}
if (strlen($msg) > 150){ die(); }
if (strlen($msg) > 15) {
if (substr_count($msg, substr($msg, 6, 8)) > 1) { die(); }
}

foreach ($blockip as $a) {
if ($_SERVER["REMOTE_ADDR"] == $a) { die(); }
}

$mystring = strtoupper($msg);
foreach ($spam as $a) {
if (strpos($mystring, strtoupper($a)) === false) {
/* Everything Ok Here */
} else {
die();
}
}

/* Preparing the Content */
$fn = "chat.txt";
$maxlines = 20;

$handle = fopen ($fn, 'r');
$chattext = fread($handle, filesize($fn)); fclose($handle);

$arr1 = explode("\n", $chattext);

if (count($arr1) > $maxlines) {
/* Pruning */
$arr1 = array_reverse($arr1);
for ($i=0; $i<$maxlines; $i++) { $arr2[$i] = $arr1[$i]; }
$arr2 = array_reverse($arr2);
} else {
$arr2 = $arr1;
}

$chattext = implode("\n", $arr2);

/* Spam Control Part 2 */
if (substr_count($chattext, $msg) > 2) { die(); }

/* Write */
$out = $chattext . $msg . "\n";
$out = str_replace("\'", "'", $out);
$out = str_replace("\\\"", "\"", $out);

$handle = fopen ($fn, 'w'); fwrite ($handle, $out); fclose($handle);
}
?>

index.html:
<html>
<head>
<title>room1</title>
</head>
<body>
<textarea id="chatwindow" rows="10" cols="80" style="border:1px solid #aaaaaa; padding:4px;" readonly></textarea><br>
<input id="chatmsg" type="text" size="20" style="border:1px solid #aaaaaa;" onkeyup="keyup(event.keyCode);"> <input type="button" value="ok" onclick="submit_msg()" style="cursor:pointer;border:1px solid gray;">
</body>
</html>

<script type="text/javascript">
/* Writing Ajax Requests */
var http_request=false;var http_request2=false;var intUpdate;function ajax_request(url){http_request=false;if(window.XMLHttpRequest){http_request=new XMLHttpRequest();if(http_request.overrideMimeType){http_request.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try{http_request=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{http_request=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
if(!http_request){alert('Giving up :( Cannot create an XMLHTTP instance');return false;}
http_request.onreadystatechange=alertContents;http_request.open('GET',url,true);http_request.send(null);}
function alertContents(){if(http_request.readyState==4){if(http_request.status==200){rec_response(http_request.responseText);}else{}}}

/* Reading Ajax Requests */
function ajax_request2(url){http_request2=false;if(window.XMLHttpRequest){http_request2=new XMLHttpRequest();if(http_request2.overrideMimeType){http_request2.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try{http_request2=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{http_request2=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
if(!http_request2){alert('Giving up :( Cannot create an XMLHTTP instance');return false;}
http_request2.onreadystatechange=alertContents2;http_request2.open('GET',url,true);http_request2.send(null);}
function alertContents2(){if(http_request2.readyState==4){if(http_request2.status==200){rec_chatcontent(http_request2.responseText);}else{}}}

/* Chat Stuff */
waittime=2000;
intUpdate=window.setTimeout("read_cont()", waittime);
chatwindow.value = "connecting...";

function display_msg(msg1) {
/* Fill Textarea with the Content */
chatwindow.value = msg1;
}

function write_msg(msg1) {
ajax_request("w.php?m=" + escape(msg1));
}

function submit_msg() {
/* Send My Message */
write_msg(chatmsg.value);
chatmsg.value="";
}

function rec_response(str1) {
/* Response From w.php */
}

function rec_chatcontent(cont1) {
if (cont1 != "") {
out1 = "";
/* Display Last Message First */
while (cont1.indexOf("\n") > -1) {
out1 = cont1.substr(0, cont1.indexOf("\n")) + "\n" + out1;
cont1 = cont1.substr(cont1.indexOf("\n") + 1);
}
out1 = unescape(out1);
if (chatwindow.value != out1) { display_msg(out1); }
intUpdate=window.setTimeout("read_cont()", waittime);
}
}

function read_cont() {
/* Prevent Buffering by using ?x=timeinms */
zeit = new Date();
ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
ajax_request2("chat.txt?x=" + ms);
}
function keyup(arg1) { if (arg1 == 13) { submit_msg(); } }
</script>

crane550
05-12-2006, 12:14 PM
this is part of the spam control, but what does it do?

if (strlen($msg) > 15) {
if (substr_count($msg, substr($msg, 6, 8)) > 1) { die(); }

More specifically the second if statement, what is substr_count, and what does the data ($msg, 6, 8) mean? $msg is the message, clearly.

Nook Schreier
05-12-2006, 01:52 PM
Spam control: That's very wierd. It appears to me that it finds characters 6-14 in $msg, and if those characters appear more than once (in the same order) the script dies. I don't know if 8 characters at position 6 (which is what that says) means something...

Most recent to bottom: Oh boy, I forgot what a time I had trying to get that to work correctly :) My solution, though, requires a database. I'm using MySQL and I did this database query:
sql = "SELECT * from rzchat WHERE postedTime < #" & dateadd("m",-8,now()) & "# ORDER BY postedTime LIMIT 15;"
I still don't like the way it works. The gist is that it will return all information from the rzchat table (one row per line of chat) that was posted in the last 8 minutes, with a hard limit of 15 messages. I had to do this because if it returned too much, the IFRAME would gain a scrollbar, but with the most recent messages being last, it would be below the bottom of the frame.

I know that doesn't really apply to you, but the point is that the only way I was able to do it easily was with a database. It will take more work to use a text file. What you would do is instead of printing each item as it comes, load the lines into an array. Then find out how long it is ($numitems = count($arrayname);) and create another array, entering the values starting with the end.$filearray = (array, read from a file);
$items = count($filearray);
for ($i=0;$i<$items;$i++) { $newarray[$i] = $filearray[$items-$i]; }All that code does is reverses the array contents, effectively moving the most recent messages to the end. Of course, you may need some way to bottom-align the text once it becomes longer than the chat window itself.

crane550
05-12-2006, 04:53 PM
What about this?

$arr1 = explode("\n", $chattext);

if (count($arr1) > $maxlines) {
/* Pruning */
$arr1 = array_reverse($arr1);
for ($i=0; $i<$maxlines; $i++) { $arr2[$i] = $arr1[$i]; }
$arr2 = array_reverse($arr2);
} else {
$arr2 = $arr1;
}

What is explode?

Nook Schreier
05-12-2006, 11:29 PM
explode("\n", $chattext);

That means that it takes $chattext and separates it into chunks with \n separating each chunk. It puts these into an array, which the function returns. \n is a newline character in PHP strings.

An example:
$mystring = "what is explode?";
$myarray = explode($mystring," "); // separate it at the spaces
The result is that in the array $myarray, [0] = "what", [1] = "is", & [2] = "explode?"

crane550
05-13-2006, 08:14 PM
Hi,

Thanks for your help, ya'll, it really is appreciated. So I think Im starting to understand this more. I have made several mods and so far am happy with how it's looking. A few things I don't quite understand, tho. I quoted this already buy maybe someone could walk me through it?

function rec_chatcontent(cont1) {
if (cont1 != "") {
out1 = "";
/* Display Last Message First */
while (cont1.indexOf("\n") > -1) {
out1 = cont1.substr(0, cont1.indexOf("\n")) + "\n" + out1;
cont1 = cont1.substr(cont1.indexOf("\n") + 1);
}
out1 = unescape(out1);
if (chatwindow.value != out1) { display_msg(out1); }
intUpdate=window.setTimeout("read_cont()", waittime);
}
}

So if cont1 has data, then it resets it to "". What happens in the next 'while' is kinda confusing me. Also, what is the unescape function?