Location: PHPKode > scripts > ICQ Status class > icq-status-class/icq_status_class.php5
<?php
/***************************************************************************
 *                         icq_status_class.php5
 *                        ----------------------
 *   begin                : Sunday, July 27, 2004
 *   copyright            : (C) 2004 Hemp "Jointy" Cluster
 *   email                : hide@address.com
 *
 *   $Id: icq_status_class.php5, v.0.1.0 hempcluster Exp $
 *
 *    LastChange: <!--DATE-->Samstag den 31.07.2004 -- 23:49:08<!--/DATE-->
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/**** Change Log ****/

// Date: 30 July 2004
// final release 0.1.0 comming out !!!



// Error Constanst for ICQ Class
// edit it if you want to change the values for your project
define("ICQ_CONNECT_FAILED" , 1000);
define("ICQ_GET_STATUS_FAILED" , 1001);
define("ICQ_INCORRECT_INPUT" , 1002);

final class ICQ
{
    static public $error = FALSE;
    static private $socket = FALSE;
    static private $url = "status.icq.com";


    /*
     * @param $uin  - ICQ UserIdentificationNumber
     * @param $img  - you can choose between 25 pictures for showing the status on your website
     *                here select you which number you want to have
     * @return a string [status that is avalible]:space:[image_url]
     *  example:  "offline http://status.icq.com/0/online0.gif"
     *
     * @access: public
     */

    static public function status( $uin , $img = 0 )
    {
        // Check is given uin a number.....
        if( !is_numeric($uin) )
        {
            return ICQ::msg_return( ICQ_INCORRECT_INPUT , "Your given uin is incorrect" , __LINE__ );
        }
        // Check given image number or is it out of range
        if( !is_numeric($img) || ($img > 50) )
        {
            return ICQ::msg_return( ICQ_INCORRECT_INPUT , "Your given image number is incorrect or out of range" , __LINE__ );
        }

        // Connect to the icq-server
        if( !ICQ::connect() ) return FALSE;

        $cmd = "HEAD /online.gif?icq=". $uin ."&img=". $img ." HTTP/1.0\r\nHost: web.icq.com\r\nConnection: close\r\n\r\n"; // "Connection: close\r\n"
        // Send the Command to the server
        if( !fwrite(ICQ::$socket , $cmd , strlen($cmd) ) )
        {
            return ICQ::msg_return(  ICQ_GET_STATUS_FAILED , "Couldn't send command to server" , __LINE__ );
        }

        $buf = "";
        do{
            // Get a string with max 1024 bytes from the server
            if( !$buf = fgets(ICQ::$socket , 1024) )
            {
                return ICQ::msg_return( ICQ_GET_STATUS_FAILED , "Couldn't read server response" , __LINE__ );
            }
        }while( !feof(ICQ::$socket) && !stristr($buf , "Location") );

        ICQ::close();

        // Explode the string by the space character
        $buf = explode(" ",trim($buf));

        if( eregi("online0",$buf[1]) ):
             $status = "offline";
        elseif( eregi("online1",$buf[1]) ):
             $status = "online";
        elseif( eregi("online2",$buf[1]) ):
             $status = "disable";
        else:
             $status = "unkown";
        endif;

        return $status." http://".ICQ::$url.$buf[1];

    }

    /*
     * Connect to the given ICQ Status Server
     * if an error was occured it call msg_return
     */
    static private function connect()
    {
        // Connect to the server
        if( !ICQ::$socket = fsockopen( ICQ::$url , 80 , $errno , $errstr , 10 ) )
        {
            return ICQ::msg_return( ICQ_CONNECT_FAILED , $errno ." -- Connect failed - ".$errstr , __LINE__ );
        }
        return TRUE;
    }

    static private function close()
    {
        if( is_resource(ICQ::$socket) )
        {
            @fclose(ICQ::$socket);
        }
        ICQ::$socket = FALSE;
        return;
    }

    /*
     * close the connecting if open an set the public error var
     * return FALSE
     */
    static private function msg_return( $err_code , $err_msg , $err_line )
    {
        ICQ::close();
        ICQ::$error = array("code" => $err_code,
        			   "msg" => $err_msg,
                            "line" => $err_line,
                            "file" => __FILE__ );

        return FALSE;
    }

}

?>
Return current item: ICQ Status class