Author Topic: PHP Who for Websites  (Read 1277 times)

Offline hells

  • Acquaintance
  • *
  • Posts: 11
    • View Profile
PHP Who for Websites
« on: March 10, 2015, 03:56:59 PM »
Below is a simple page i use to log in to my mud and print out a list of who is currently connected.  You will probably have to customize the login process a bit and modify the username / password.  This should work for any mud that has telnet access.

This assumes you are passing a referrer as a parameter. If you want to grab the referrer directly you could use $_SERVER['referrer']

Code: [Select]
<html>
<head>
<?php

/** Represents one character.  Style holds any amount of HTML that does
 * not take up any space--e.g. span tags, bold, italics, etc.
 */
class Cell
{
    var 
$style;
    var 
$data;

    function 
__construct()
    {
        
$this->data "&nbsp;";
    }

    function 
set_style($s)
    {
        
$this->style $s;
    }

    function 
set_data($d)
    {
        if (
$d == " ")
            
$this->data "&nbsp;";
        else
            
$this->data $d;
    }

    function 
print_cell()
    {
        echo 
$this->style;
        echo 
$this->data;
    }
}


/** Represents one row or line of characters. */
class Row
{
    var 
$cells = array();

    function 
get_cell($c)
    {
        if (!
array_key_exists($c$this->cells))
            
$this->cells[$c] = new Cell();
        return 
$this->cells[$c];
    }

    function 
set_style($col$style)
    {
        
$this->get_cell($col)->set_style($style);
    }

    function 
set_data($col$data)
    {
        
$this->get_cell($col)->set_data($data);
    }

    function 
print_row()
    {
        
$last 0;
        foreach (
$this->cells as $key => &$cell)
        {
            for (
$i $last 1$i $key$i++)
                echo 
"&nbsp;";
            
$cell->print_cell();
            
$last $key;
        }
        echo 
"<br>";
    }
}


/** Represents a text block made up of a number of lines. */
class TextBlock
{
    var 
$rows = array();

    function 
get_row($r)
    {
        if (!
array_key_exists($r$this->rows))
            
$this->rows[$r] = new Row();
        return 
$this->rows[$r];
    }

    function 
set_style($row$col$style)
    {
        
$this->get_row($row)->set_style($col$style);
    }

    function 
set_data($row$col$data)
    {
        
$this->get_row($row)->set_data($col$data);
    }

    function 
clear()
    {
        
$this->rows = array();
    }

    function 
print_block()
    {
        
$last 0;
        foreach (
$this->rows as $key => &$row)
        {
            for (
$i $last 1$i $key$i++)
                echo 
"<br>";
            
$row->print_row();
            
$last $key;
        }
        echo 
"<br>";
    }
}


/** Translates ANSI codes into a TextBlock. */
class AnsiTranslator
{
    private 
$ansi_colours = array( '0' => array (
                                       
'30' => 'black',
                                       
'31' => 'maroon',
                                       
'32' => 'green',
                                       
'33' => 'olive',
                                       
'34' => 'navy',
                                       
'35' => 'purple',
                                       
'36' => 'teal',
                                       
'37' => 'gray', ),
                                   
'1' => array (
                                       
'30' => 'silver',
                                       
'31' => 'red',
                                       
'32' => 'lime',
                                       
'33' => 'yellow',
                                       
'34' => 'blue',
                                       
'35' => 'fuchsia',
                                       
'36' => 'aqua',
                                       
'37' => 'white' ) );

    private 
$lord_colours = array( '1' => 'navy',
                                   
'2' => 'green',
                                   
'3' => 'teal',
                                   
'4' => 'maroon',
                                   
'5' => 'purple',
                                   
'6' => 'olive',
                                   
'7' => 'gray',
                                   
'8' => 'gray',
                                   
'9' => 'blue',
                                   
'0' => 'lime',
                                   
'!' => 'aqua',
                                   
'@' => 'red',
                                   
'#' => 'fuchsia',
                                   
'$' => 'yellow',
                                   
'%' => 'white' );

    var 
$block;
    var 
$filename;

    
/** Interpret special Legend of the Red Dragon colour codes?
     * These are of the style "`<code>" where <code> is 1-0 and shift-1 to
     * shift-5 (!-%).  If this is false the characters will be printed
     * literally.
     */
    
var $interpret_lord_chars;

    function 
__construct($_filename$_interpret_lord_chars)
    {
        
$this->filename $_filename;
        
$this->interpret_lord_chars $_interpret_lord_chars;
    }

    function 
translate($val)
    {
        
$lines $val;//file($this->filename);
        
$this->block = new TextBlock();
$x 0;
        foreach (
$lines as $line) {
        
$x++;
            
$this->translate_line($x$line);
}
        return 
$this->block;
    }

    private function 
translate_line($line_num$line)
    {
        
$ansi_mode false;
        
$lord_colour_mode false;
        
$col 1;
        for (
$x 0$x strlen($line); $x++)
        {
            
$letter substr($line$x1);
            if (
$lord_colour_mode)
            {
                
// Next letter is a LORD colour indicator.
                
$style '</span><span style=color:' .
                         
$this->lord_colours[$letter] . '>';
                
$this->block->set_style($line_num+1$col$style);
                
$lord_colour_mode false;
            }
            else if (!
$ansi_mode)
            {
                if (
$letter == chr(0x1b))
                {
                    
$ansi_mode true;
                    
$ansi_code '';
                }
                else if (
$this->interpret_lord_chars && $letter == '`')
                {
                    
$lord_colour_mode true;
                }
                else
                {
                    if (
$letter == chr(0xc4))
                        
$data '&ndash;';
                    else
                        
$data htmlentities($letter);
                    
$this->block->set_data($line_num 1$col$data);
                    
$col++;
                }
            }
            else
            {
                
// We are in the middle of an ANSI code.
                
if ($letter == 'm')
                {
                    if (
$ansi_code == '0')
                        
$this->block->set_style($line_num+1$col,
                                          
'</span><span style=color:white>');
                    else
                    {
                        
$attrs explode(';'$ansi_code);
                        
$highlight 0;
                        
$colour 30;
                        
$background 0;
                        
$blinking false;

                        foreach (
$attrs as $a)
                        {
                            if (
$a == || $a == 1)
                                
$highlight $a;
                            elseif (
$a == || $a == 6)
                                
$blinking true;
                            elseif (
$a >= 30 && $a <= 37)
                                
$colour $a;
                            elseif (
$a >= 40 && $a <= 47)
                                
$background $a;
                        }

try {
                        $style '</span><span style=color:' .
                                 
$this->ansi_colours[$highlight][$colour];
                    
} catch (Exception $e) {
                    
$style '</span><span style=color: black';
                    
}
   
                        if (
$background)
                            
$style .= ';background:' .
                                      
$this->ansi_colours[0][$background 10];
                        if (
$blinking)
                            
$style .= ';text-decoration:blink';
                        
$style .= '>';
                        
$this->block->set_style($line_num+1$col$style);
                    }
                    
$ansi_mode false;
                }
                else if (
$letter == 'J')
                {
                    if (
$ansi_code == '2')
                        
$this->block->clear();
                    
$ansi_mode false;
                }
                else if (
$letter != '[')
                    
$ansi_code .= $letter;
            }
        }
    }
}

?>

</head>

<body bgcolor="black">
<tt>
<?php

$socket 
fsockopen('localhost''6666'$timeout=1);
$notdone 1;
stream_set_timeout($socket,0.7);

sleep(0.1);
fputs($socket"AREGULARJOE\r\n");

sleep(0.1);
fputs($socket"APASSWORD\r\n");
sleep(0.1);
while (!
feof($socket)) {
$line fgets($socket);
if (strpos($line"assword")) {
break;
}
}

$start round(microtime(true) * 1000);
$timeout 1;
$results = array();
while((!
feof($socket) || (round(microtime(true) * 1000) - $start) < $timeout)) {
$line fgets($socket);
if (strpos($line"=*")) {
continue;
    } elseif (
trim($line) == "") {
        continue;
    } elseif (
strpos($line"you wish to")) {
        
fputs($socket"y\r\n");
} elseif (strpos($line"blue jeans") || strpos($line"llowed login")) {
        
$results = array();
        
sleep(0.2);
        
fgets($socket);
        
fputs($socket"who\r\n");
        
//break;
    
} elseif (strpos($line"The Savage Soul")) {
        continue;
} elseif (strpos($line"interactive copy")) {
continue;
} elseif (strpos($line"ebserver")) {
continue;
} elseif (strpos($line"our reality")) {
break;
} else {
$results[] = $line;
}
}
fputs($socket"tell hells Referrer:" $_REQUEST['referrer'] . "\r\n");
sleep(0.1);
$line fgets($socket);
$line fgets($socket);
sleep(0.1);
fputs($socket"quit\r\n");
$line fgets($socket);
fclose($socket);

$translator = new AnsiTranslator($results0);


$block $translator->translate($results);
echo 
'<span>';
$block->print_block();
echo 
'</span>'
?>

</tt>
</body>
</html>
« Last Edit: March 10, 2015, 03:59:11 PM by hells »