Author Topic: PHP ASE parser  (Read 13113 times)

0 Members and 3 Guests are viewing this topic.

Offline Thomas

  • Major
  • *
  • Posts: 76
    • mnus.de
Re: PHP ASE parser
« Reply #20 on: May 08, 2007, 12:02:37 pm »
It worked for me yesterday (PHP 5.2.2 + Apache 2.0.59)
Edit: wtf, yesterday it worked, now i get errors but the request works

Code: [Select]
<?php
    
// Using Socketfunctions
    
$s socket_create(AF_INETSOCK_DGRAMSOL_UDP);
    
socket_clear_error($s);
    
socket_connect($s$host$port+123);
    if(
socket_last_error())
    {
    echo 'Error'.socket_last_error().': '.socket_strerror(socket_last_error());
    socket_clear_error($s);
    #return false;
    
}
    
socket_write($s,"s");
    if(
socket_last_error())
    {
    echo 'Error'.socket_last_error().': '.socket_strerror(socket_last_error());
    socket_clear_error($s);
    #return false;
    
}
    
$vsock socket_read($s4096); // read to "virtual socket"
    
$vpos 0// virtual socket "pointer"
    
socket_close($s);
    
$s $vsock;
    
vsocket_read($s,4);
?>

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: PHP ASE parser
« Reply #21 on: February 18, 2008, 05:40:36 pm »
(sry for bringing up an old topic)
I'm having issues trying to get this to work all the time.
I've tried the code in the first post and the code in Thomas's most recent post..
It seems to work sometimes, but some servers I try seems to freeze it up and the browser keeps loading and loading and loading...
There are like a list of like 10 servers that I'm trying to loop through to get server information about..

Offline Slazenger

  • Soldier
  • **
  • Posts: 100
Re: PHP ASE parser
« Reply #22 on: February 19, 2008, 10:32:09 am »
Where do I need to put the servers IP and PORT?

Offline rayanaga

  • Soldier
  • **
  • Posts: 143
  • ~Fur flying~
    • Kryonex
Re: PHP ASE parser
« Reply #23 on: June 05, 2008, 05:48:58 pm »
Add this right below the other code

It should ping your server and timeout if its not found. ;D
Code: [Select]
<?
//Settings.
$ipaddress = '127.0.0.1';
$port = '20000';

//Ping your server and then retrieve info
if(fsockopen ($ipaddress, $port, $errno, $errstr, 15))
print_r(ASE($ipaddress,$port));
else
echo 'The requested server is offline';
?>

Hope it helps!

« Last Edit: June 05, 2008, 05:50:35 pm by rayanaga »
[kY] Kryonex - Your local zombie fanatics.
http://www.kryonex.com/

Offline Frenchie

  • Camper
  • ***
  • Posts: 358
  • SoldatHQ
Re: PHP ASE parser
« Reply #24 on: June 22, 2008, 04:25:14 am »
Ok. I really needed a bulletproof ASE parser which wouldn't stuck in loops. I tried modifying FLAB's code and got somewhere but realised I wasn't get to the perfection I needed and my knowledge of this ASE protocol is lower than imagineable I decided to stick to some old code I had.

Because I see there are some in this topic who want a ASE parser who doesn't get stuck in loops I'll post my code. It has been used in my scripts which are run by cron jobs for quite a while.. so I know it is very reliable
(I found it on the internet somewhere, was modified by ChrisGBK to fully support the protocol and modified by Me to include the protection label (anti-hack))

I warn you it's extremely MESSY CODE!
some of you might die just looking at it

Code: [Select]
SNIP

Please ddon't bug too clean it up, I didn't post it here for those of you to bug me. I just wanted to help those who need another option as an ASE parser.

There is huge potential for this code to be tidied up. And if anyone does please upload for the rest to benefit.

Date Posted: June 22, 2008, 05:00:03 pm
EDIT-------------------------------
I decided to fix up the code / tidy it up. As I will be needing it in my next script. :)

I have tested both the ASE parsers on quite alot of servers and each one that FLAB's looped on, this one didn't (Not talking down FLAB's script, as I would like to have used it instead on using up my time re-writing this whole script)
This ASE Parser is more designed to be used in cron jobs / behind the scene scripts as it does not get caught on servers, has some error checking in it and Returns 'Error' if anything goes wrong. (This makes it easy to implement script checking into your script) And it works well

And one more thing  ;D
It's fully interchangeable / compatible with FLAB's script, I made it use the same variable names (and used his player for snippet) And it's compatible with that extra formatting snippet in FLAB's script.

Hope you enjoy... Had to modify / re-write the whole thing from 225 lines to just 65  ;D
Code: [Select]
<?php
//Original by: Christian Celler
//Player Details by: ChrisBGK
//Modified/Re-written by: Frenchie
function parse_ase (&$string) {
    
$size   intval (ord (substr ($string01)));
    if (
$size 0)
      
$value  substr ($string1$size-1);
    else
      
$value NULL;
    
$string substr ($string$size);
    return (
$value);
}

function 
ASE($host$port) {
$socket socket_create (AF_INETSOCK_DGRAMSOL_UDP);
    if (!(@
socket_connect ($socket$host$port+123))) 
return('Error'); //Could not connect
      
socket_set_option ($socketSOL_SOCKETSO_RCVTIMEO, array ('sec' => 0'usec' => 500000));
      
socket_send ($socket's'10);
  
  //Read all Data
      
while ($data = @socket_read ($socket16384PHP_BINARY_READ))
        
$result $output $data;

      
socket_close ($socket);

      if (empty (
$result))
   return('Error'); //No output
        
$result   substr ($result4);

        
parse_ase ($result);
        
$info['port'] = parse_ase ($result);
        
$info['servername'] = parse_ase ($result);
$info['ip'] = gethostbyname($host);
        
$info['mode'] = parse_ase ($result);
        
$info['map'] = parse_ase ($result);
        
$info['version'] = parse_ase ($result);
        
$info['passworded'] = parse_ase ($result);
        
$info['numplayers'] = parse_ase ($result);
        
$info['maxplayers'] = parse_ase ($result);        
      
//Loop through and retrieve Data 
        
while (ord (substr ($result01)) > 1)
        
$info[parse_ase($result)]= parse_ase ($result);

        
$result =substr($result1);

$info['players'] = Array();
         if (
strlen ($result) > 0) { //Check if there is still data to parse
   
for($i=0;$i<$info['numplayers'];$i++) {
       
  $info['players'][$i] = Array();
       
  $result =substr($result1);
       
  $info['players'][$i]['name'] = parse_ase($result);
        
$info['players'][$i]['team'] = parse_ase($result);
        
$info['players'][$i]['skin'] = parse_ase($result);
        
$info['players'][$i]['score'] = parse_ase($result);
        
$info['players'][$i]['ping'] = parse_ase($result);
        
$info['players'][$i]['time'] = parse_ase($result);
    
}    
     }
     return 
$info;
}
?>


Any feedback on this script appreciated
« Last Edit: June 22, 2008, 04:27:37 am by Frenchie »
Soldat Lobby Avec Players -New Release! - Updated AGAIN!


Offline chrisgbk

  • Inactive Staff
  • Veteran
  • *****
  • Posts: 1739
Re: PHP ASE parser
« Reply #25 on: July 17, 2008, 05:18:13 pm »
As a late followup to the discussion in here regarding reading data, and strange errors:

Thomas' code throws errors because he forgot to pass the socket to socket_last_error() resulting in the global socket error to be checked instead of the specific socket. Error checking should have been socket_last_error($s). He also forgot to clear the global socket error via socket_clear_error() without arguments before attempting to create the socket.

In order to guarantee the correct amount of data is read, use something like this:

Code: [Select]
while (($size = socket_recvfrom($socket, $data, ($last = $size) + 1024, MSG_PEEK, $host, $port)) && ($last != $size))
; // deliberately empty
socket_recvfrom($socket, $data, $size, 0, $host, $port);
« Last Edit: July 17, 2008, 05:21:04 pm by chrisgbk »