<?php
/**********************************************************************
*    Author:    Terry Valladon (terry@terryvalladon.com)
*    Web...:    http://www.terryvalladon.com
*    Name..:    UDBDebug Class
*    Desc..:    This class is used to send debug messages as a datagram packet to a UDP port
*
*    Notes.:    <IP ADDRESS>    = The IP address you want the packet sent to
*        <PORT>        = The port you want the packet sent to
*        <LEVEL>        = LOW, MEDIUM, HIGH
*        <VERBOSE>    = TRUE, FALSE - Display information like LEVEL and PORT
*
*    Usage.:    require_once("UDPDEBUG_CLASS.php");            
*        $UDPDEBUG = new UDPDEBUG(<IP ADDRESS>, <PORT>);
*        $UDPDEBUG->SEND($MESSAGE, <LEVEL>, <VERBOSE>);
*
*/

    
class UDPDEBUG {
        public 
$IP_ADDRESS;
        public 
$PORT;
        public 
$UDP_OBJECT;
        public 
$ERR_NO;
        public 
$ERR_STR;
        public 
$ESC_CHR;
        public 
$LEVEL = array('LOW'=>0,'MEDIUM'=>1,'HIGH'=>2);
        public 
$LEVEL_WORDS = array(0=>'LOW',1=>'MEDIUM',2=>'HIGH');
        public 
$LEVEL_COLORS = array(0=>'GREEN',1=>'YELLOW'2=>'RED');
        public 
$ANSI_BG = array('RED'=>'[41m','GREEN'=>'[42m','YELLOW'=>'[43m','CYAN'=>'[46m');
        public 
$ANSI_FG = array('RED'=>'[1;31m','GREEN'=>'[1;32m','YELLOW'=>'[1;33m','CYAN'=>'[1;36m');
        public 
$ANSI_SPECIAL = array('NORMAL'=>'[0m');

        function 
UDPDEBUG($IP_ADDRESS$PORT){
            
$this->ESC_CHR chr(27);
            
$this->IP_ADDRESS $IP_ADDRESS;
            
$this->PORT $PORT;
        }

        function 
SEND($STRING$LEVEL 1$VERBOSE FALSE)
        {
            
$LEVEL $this->LEVEL[$LEVEL];
            if(
$VERBOSE == TRUE){
                
$OUTPUT sprintf("[%s]:%s:%s%s\n",$this->ESC_CHR.$this->ANSI_FG['CYAN'].$this->PORT,$this->ESC_CHR.$this->ANSI_FG[$this->LEVEL_COLORS[$LEVEL]].str_pad($this->LEVEL_WORDS[$LEVEL], 9),$this->ESC_CHR.$this->ANSI_SPECIAL['NORMAL'], $STRING);
            }else{
                
$OUTPUT sprintf("%s\n",$this->ESC_CHR.$this->ANSI_FG[$this->LEVEL_COLORS[$LEVEL]].$STRING.$this->ESC_CHR.$this->ANSI_SPECIAL['NORMAL']);
            }
            
$this->UPD_OBJECT fsockopen("udp://" $this->IP_ADDRESS$this->PORT$this->ERR_NO$this->ERR_STR);
            if (!
$this->UPD_OBJECT) {
                echo 
"ERROR IN UDP SYSTEM: $this->ERR_NO - $this->ERR_STR<br />\n";
                return 
0;
            }
            
fwrite($this->UPD_OBJECT$OUTPUT);
            
fclose($this->UPD_OBJECT);
        }
    }
?>