Location: PHPKode > scripts > CK2ICQ > ck2icq/CK2ICQ.php
<?php
/**
* Copyright (c) 2003 Martin Minka (hide@address.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* available through the world wide web at, http://www.gnu.org/copyleft/lesser.html.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For documentation refer to: http://www.k2s.sk/k2PHP/
* For actual version refer to: http://www.phpclasses.org/browse.html/package/996.html
*
* @package k2PHP
*/

/**
* Constants defining the status of the ICQ user
*/
/**
* the user is offline
*/
define("ICQ_OFFLINE", 0);
/**
* the user is online
*/
define("ICQ_ONLINE", 1);
/**
* it was not possible to obtain the user status
*/
define("ICQ_UNKNOWN", 2);
/**
* an error occured in the process of obtaining the user status
*/
define("ICQ_ERROR", 3);

/**
* ICQ server communication class
*
* This class is implementing an communication routines with the ICQ server
*
* @package k2PHP
* @version 0.1
* @author Martin Minka <hide@address.com>
* @link http://www.k2s.sk
*/
class CK2ICQ {
    /**
	* if an error occured you can use this text to find more information about the problem
	* @var string
	* @access public
	*/  
    var $mError_str;    
    /**
	* the ICQ server http adress
	* @var string
	* @access private
	*/  
    var $mICQServer;
    /**
	* number of the default image to be displayed
	* @var byte
	* @access private
	*/  
	var $mDefault_image;
    /**
	* handle to an open connection to the ICQ server
	* @var handle
	* @access private
	*/  	
    var $mConnect;
    /**
	* array of actual states of all checked ICQ users
	* @var array
	* @access private
	*/      
    var $mStatus;
    /**
	* TRUE if the class was constructed in a mDebug mode
	* @var boolean
	* @access private
	*/          
    var $mDebug;

    /**
    * constructor for the class
    *
    * Create a new instance of the class with: $icq = new CK2ICQ();
    *
    * @param boolean $debug if you set TRUE, the class will output some mDebug information, allowing to find problems 
    * @param byte $default_img you can set the type of images used to represent the status of the ICQ user
    * @param string $server optionaly you can set the adress of the ICQ server
    * @access public
    */
    function CK2ICQ($debug = false, $default_img=1, $server='status.icq.com' )
    {
        $this->mDefault_image = $default_img;
        $this->mICQServer = $server;
        $this->mStatus = array();
        $this->mDebug = $debug;
        if ($this->mDebug)
        {
            echo "k2icq: server=$this->mICQServer<br>";
            echo "k2icq: default image=$this->mDefault_image<br>";            
        }
    }
    
    /**
    * open an connection handle to ICQ server
    *
    * $return boolean if successfull then TRUE
    * @access private
    */    
    function Connect2ICQ()
    {
        $this->CloseICQ();
            
        $this->mConnect = fsockopen($this->mICQServer, 80, $errno, $errstr, 90);
        if (!$this->mConnect)
        {
            unset($this->mConnect);
            $this->mError_str = "Unable to mConnect to ICQ server.";
            if ( $this->mDebug )
                echo "k2icq: ".$this->mError_str."<br>";
            
            return FALSE;
        }
        socket_set_blocking($this->mConnect, 1);        
        return TRUE;
    }
    
    /**
    * close the connection to ICQ server
    * @access private
    */    
    function CloseICQ()
    {
        if ( isset($this->mConnect) )
        {
            fclose($this->mConnect);
            unset($this->mConnect);
        }
    }

    /**
    * is checking the status of the ICQ user $id
    * 
    * @param long $id id of the checked ICQ user
    * @return byte return one of the ICQ_ constants
    * @see ICQ_OFFLINE, ICQ_ONLINE, ICQ_UNKNOWN, ICQ_ERROR
    * @access private
    */    
    function Check($id) 
    {
        $data = "";

        if ( $this->mDebug )
            echo "k2icq: query=http://$this->mICQServer/online.gif?icq=$id&img=1<br>";
        
        fputs($this->mConnect,
              "GET /online.gif?icq=" . $id . "&img=1 HTTP/1.1\r\n" .
              "Host: " . $this->mICQServer . "\r\n\r\n");
        while( !feof($this->mConnect) ){
            $data = fgets($this->mConnect, 2048);
            if( ereg("Location: +(.*)", $data, $parts) ){
                break;
            }
        }

        if ( $this->mDebug )
            echo "k2icq: $parts[1]<br>";
        
        $status = ICQ_UNKNOWN;
        if( isset($parts) )
        {
            if ( strstr($parts[1], "online1") != FALSE )
                $status = ICQ_ONLINE;            
            elseif ( strstr($parts[1], "online0") != FALSE )
                $status = ICQ_OFFLINE;
        }        
        
        return $status;
    }
    
    /**
    * will check the status of the ICQ user $id and set the result to cache
    * 
    * @param long $id optionaly you can set the adress of the ICQ server
    * @return byte return one of the ICQ_ constants
    * @see Check, ICQ_OFFLINE, ICQ_ONLINE, ICQ_UNKNOWN, ICQ_ERROR
    * @access private
    */    
    function CheckAndSet($id)
    {
        $ret = $this->Check($id);
        if ( $ret<ICQ_ERROR )
            $this->mStatus[$id]=$ret;
        else 
            unset($this->mStatus[$id]);
        
            return $ret;
    }
    
    /**
    * will check the state for a list of ICQ users
    * 
    * @param string $ids a list of ICQ id numbers separated by ','
    * @return boolean if successfull then TRUE
    * @access public
    */    
    function Prepare($ids)
    {
        $mError_str = "";
        
        $ida = explode(",", $ids);
        if ( !isset($ida) ) 
        {
            echo "empty";
            return TRUE;
        }

        if ( !$this->Connect2ICQ() ) 
            return FALSE;

                
        foreach ($ida as $id) 
        {
            $id = trim($id);
            $this->CheckAndSet($id);
        }
        
        $this->CloseICQ();                    
    }
    
    /**
    * will check the status of one ICQ user
    *
    * @param long $id id of the checked ICQ user
    * @return byte return one of the ICQ_ constants
    * @see ICQ_OFFLINE, ICQ_ONLINE, ICQ_UNKNOWN, ICQ_ERROR
    * @access public
    */    
    function GetStatus($id)
    {
        if ( isset($this->mStatus[$id]) )
        {
            // add check for time to refresh
            return $this->mStatus[$id];
        }
        
        if ( !$this->Connect2ICQ() ) 
            return ICQ_ERROR;
        
        $ret = $this->CheckAndSet($id);
       
        $this->CloseICQ();

        return $ret;
    }
    
    /**
    * will return the descriptive text for status of the ICQ user
    * 
    * @param long $id id of the checked ICQ user
    * @param array $msg if you dont set, the default texts will be user,
    * you can set an 4 item array with text for the states: offline, online, unknown, error
    * @return string
    * @access public
    */
    function GetText($id, $msgs=array())
    {
        if ( count($msgs)!=4 )
        {
            if ( $this->mDebug )
                echo "k2icq: parameter msgs in GetText has not 4 items, i will use defalt texts<br>";
            $msgs = array("offline", "online", "unknown", "error");
        }
        return $msgs[$this->GetStatus($id)];
    }

    /**
    * will return a link to image expressing the status of the ICQ user
    * 
    * @return string
    * @access public
    */   
    function GetDefaultIMGs()
    {
        
        return array("http://$this->mICQServer/$this->mDefault_image/online$ret.gif");
    }
    
    /**
    * will return a link to image expressing the status of the ICQ user
    * 
    * @param long $id id of the checked ICQ user
    * @param array $imgs if you dont set, the default images will be used, 
    * you can set a number of the default image style, 
    * or you can set an 4 item array with the image names for the states: offline, online, unknown, error
    * @return string
    * @access public
    */      
    function GetIMG($id, $imgs=array())
    {
        $ret = $this->GetStatus($id);
        
        if (!is_array($imgs))
        {
            return "http://$this->mICQServer/$imgs/online$ret.gif";
        } else
        {
            if ( count($msgs)!=4 )
            {
                if ( $this->mDebug )
                    echo "k2icq: parameter imgs in GetIMG has not 4 items, i will use defalt images<br>";
                return "http://$this->mICQServer/1/online$ret.gif";
            }
            
        }
        return $imgs[$ret];
    }
}
?>
Return current item: CK2ICQ