<?

//=============================//
//                             //
//   pingStatus v0.1           //
//   pingStatus Class file     //
//   pingStatus.Class.php      //
//   by Stanga Razvan          //
//   razvan_stanga@yahoo.com   //
//   http://www.phprebel.org   //
//                             //
//       MADE IN ROMANIA       //
//                             //
//=============================//

class pingStatus {
    var 
$ip;
    var 
$results;
    var 
$status;
    var 
$ftp_conn;
    var 
$connection;
    var 
$config;
    
    
//------------
    // Start Class
    //------------
    
    
function pingStatus ($ip) {
        
$this->ip $ip;
        
$this->results = array ();
        
$this->status '';
        
$this->pingstatus 0;
        
$this->ftp_conn '';
        
$this->connection '';
        
        
$this->config = array ();
        
$this->config['cronjob'] = 300;
        
$this->config['imagename'] = "pingstatus.gif";
        
// post
        
$this->config['post'] = 1;
        
$this->config['posttourl'] = 'www.computergames.ro';
        
$this->config['posttopath'] = '/venom/onlinestatus/ping.php';
        
// image replace
        
$this->config['imagereplace'] = 1;
        
$this->config['imagepath'] = 'status';
        
// ftp upload
        
$this->config['ftp_active'] = 0;
        
$this->config['ftp_server'] = 'ftp.xxx.com';
        
$this->config['ftp_dir'] = 'images/ping'// no '/' needed
        
$this->config['ftp_port'] = 21;
        
$this->config['ftp_user'] = 'user';
        
$this->config['ftp_pass'] = 'pass';
        
    }
    
    
//-----------
    // Do ping IP
    //-----------

    
function pingIP () {

        if ( 
$this->ip ) {
             
exec("ping -c 3 ".$this->ip$this->results);
             
$this->status count ($this->results);
        
            if ( 
$this->status ) {
                
$this->pingProcess ();
                
$this->pingDoStuff ();
            }
        }
    }

    
//------------------------
    // Process results for log
    //------------------------

    
function pingProcess () {
         for(
$i=0$i $this->status$i++) {
               
preg_match ('/(.*) packets transmitted, (.*) received/is'$this->results$i ], $match);
               if ( 
$match[1] == AND $match[2] > ) {
                   
$this->pingstatus TRUE;
               }
         }
    }
    
    
//-----------
    // FTP Upload
    //-----------

    
function ftp_start () {
        
$this->ftp_conn ftp_connect ($this->config['ftp_server'], $this->config['ftp_port']);
        
$login_result ftp_login ($this->ftp_conn$this->config['ftp_user'], $this->config['ftp_pass']);
    }

    function 
ftp_upload ($filename$filesource) {
        
$upload ftp_put ($this->ftp_conn$filename$filesourceFTP_BINARY);
    }

    function 
ftp_dir () {
        
$upload ftp_chdir($this->ftp_conn$this->config['ftp_dir']);
    }

    function 
ftp_end () {
        
ftp_quit ($this->ftp_conn);
    }


    
//---------------
    // Make The Stats
    //---------------

    
function pingDoStuff () {

        if (
$this->pingstatus == TRUE) {
            
$img "online.gif";
        } else {
            
$img "offline.gif";
        }

        if (
$this->config['post']) {
            
$this->pingSendData ();
        }

        if (
$this->config['ftp_active']) {

            
$this->ftp_start ();
            
$this->ftp_dir();
            
$this->ftp_upload ($this->config['imagename'], "templates/".$img."");
            
$this->ftp_end ();
        }

        if (
$this->config['imagereplace']) {
            if ( 
file_exists ($this->config['imagepath']."/".$this->config['imagename']) ) {
                
unlink ($this->config['imagepath']."/".$this->config['imagename']);
            }
            
copy ("templates/".$img.""$this->config['imagepath']."/".$this->config['imagename']);
        }

    }


    
//--------
    // Send To
    //--------

    
function pingSendTo ($host$method$path$data$useragent=0) {

        if (empty(
$method)) {
              
$method 'GET';
        }

        
$method strtoupper ($method);
        
        
$this->connection  = @fsockopen ($host 80$errno$errstr30);
        if ( !
$this->connection ) {
            
//$this->pingLog ('Failed to connect to '.$host);
            //$this->pinglog ('Error : '. $errstr);
            //$this->pingLog ('Error NR : '. $errno);
        
}

        if (
$method == 'GET') {
              
$path .= '?' $data;
        }

        
$msg '';
        
$msg .= "".$method." ".$path." HTTP/1.0\r\n";
        
$msg .= "Host: ".$host."\r\n";
        
$msg .= "Content-type: application/x-www-form-urlencoded\r\n";
        
$msg .= "Content-length: " strlen($data) . "\r\n";

        if (
$useragent) {
            
$msg .= "User-Agent: MSIE\r\n";
            
$msg .= "Connection: close\r\n\r\n";
        }

        
$this->pingSend ($msg);
        
        if (
$method == 'POST') {
            
$this->pingSend ($data);
        }
        
        
fclose $this->connection );
    }

    function 
pingSendData () {

         
$host      $this->config['posttourl'];
         
$method    "GET";
         
$path      $this->config['posttopath'];
         
$data      "status=".$this->pingstatus."";
         
$useragent 1;

         
$buffer $this->pingSendTo ($host$method$path$data$useragent);
         
//$this->pingLog ("Data Sent : ".$buffer);

    
}
    
    function 
pingSend ($str) {
          
$str $str."\r\n";
        
$res = @fwrite($this->connection$strstrlen($str));
    }
    
}

?>