note 41350 deleted from function.fsockopen by aidan
| From: | aidan@php.net | Date: | Sun, 11 Jul 2004 05:50:09 +0000 |
| Subject: | note 41350 deleted from function.fsockopen by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73040@lists.php.net to get a copy of this message | ||
Note Submitter: Andri Erebara
----
//I needed a who list quickly from the MUD I am an Imm in...
//So I made this script, which logins as a player at the mud,
// gets the who list and shows it in a webpage.
//I could not get it to display color yet.
//This is the name of the guy who will get the ip/host reading the page.
$thisguy = "loren";
$ip = $_SERVER['REMOTE_ADDR'];
$hostname = $_SERVER['REMOTE_HOST'];
if (strlen($hostname)!=0) {
$hosting = $hostname;
} else {
$hosting = $ip;
};
//If the connection can't be estabilished, we don't want our script hang around, do we?
//That's why we put a 10 second timeout on the fsockopen() trying to create the handle.
if($fp = fsockopen ("localhost", 9999, $errno, $errstr, 10)) //hostname, port, errorno,
error, timeout
{
stream_set_blocking ($fp, TRUE);
//Output with delay :)
fwrite($fp, "Phpscript\n"); //player name
fwrite($fp, "password\n"); //password
usleep(500000);
fwrite($fp, "\ny\n\n"); //if we drop link by accident
usleep(500000);
fwrite($fp, "who\n"); //ask for who list
fwrite($fp, "tell $thisguy $hosting got my list.\n"); //tells me who got the list
sleep(1);
fwrite($fp, "quit\n");
//Read enough into a string
$data .= fread($fp, 8192);
//Convenience
$out = $data;
fclose($fp);
} else {
echo "Connection can't be estabilished...<br>";
echo "Error: $errno / Reason: $errstr";
}
//If we fclose() at this point, we get an error since there might be no handle to fclose(),
so we NULL the handle...
//Thanks to the php community for this idea!
$fp = NULL;
//LOG ATTEMPT'S IP IN A FILE
if ($fp = fopen("phplog.log", 'a')) {
fwrite($fp, "$hosting\n");
fclose($fp);
} else {
echo "Log cant be created";
}
//Very unreliable method. Exploding by prompt.
$virgin = explode("100ToniMontana100", $tagged); //100ToniMontana100 is an unique string
in the MUD.
//We loop from 0 to max count which should be 3, normally.
//I did this part in case an area reset happens and you get more than 2 prompts. PRECAUTION.
for ($i=0;$i<=count($virgin);$i++) {
//if in any case we don't hit our guy on with the hardcoding method
//there's always the strstr which adds some logics to that.
if (strstr($virgin[$i], "| Dungeons & Dragons |")) {
$ourguy = $virgin[$i];
}
}
//We make a list of each line into an array.
$linez = explode("\n", $ourguy);
//With this loop we'll find our name and delete.
//Same as above loop, just modified a little.
//In case you decide to make the dude an imm, added support for WIZI and/or INCOG imms and players.
//It will not show them aswell.
for ($i=0;$i<=count($linez);$i++) {
if (strstr($linez[$i], "Phpscript") ||
strstr($linez[$i], "W-9") ||
strstr($linez[$i], "I-9") ||
strstr($linez[$i], "W-100") ||
strstr($linez[$i], "I-100")) {
//Here we go, found it, clear it.
unset($linez[$i]);
}
}
//We glue it together again, now html compatible.
$final = implode("<br>", $linez);
//The ereg() replaces any whitespace with a   HTML compatible whitespace.
//Fixedsys is for the list to show cool and formatted.
$outputted = "<font face=Fixedsys color=silver>" . ereg_replace("
"," ",$final) . "</font>";
//In case any char is in our range.
$out2 = ereg_replace("PK","--",$outputted);
echo $out2;