<?php

/**
 * skype4web - PHP-Class
 * 
 * Copyright (C) 2007  Dennis Ploetner <dennis@ploetner.it>
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */

define ("SKYPE_BASE_URI""mystatus.skype.com");
define ("SKYPE_DNSCMD""dig TXT");              // or host -t if available
define ("SKYPE_CURL"0);                        // or 1 if u want to use CURL

class httpSkype {

    var 
$_id;
    var 
$_format;
    var 
$_image;
    var 
$_lang;
    var 
$_error;

    function 
httpSkype () {
        
$this->_format 'png';
        
$this->_error = array ();
    }

    function 
set ($arr) {
        
$slot array_keys (get_class_vars (get_class ($this)));
        if (
is_array ($arr)) {
            foreach (
$arr as $key => $value) {
                if (
in_array ('_' $key$slot)) {
                    
$this->$key ($value);
                }
            }
        }
    }

    function 
id ($val) {
        if (!empty (
$val)) {
            if (
eregi ('^[a-z0-9@,._-]+$'$val)) {
                
$this->_id str_replace ('.''%2E'$val);
                return 
TRUE;
            }
            
$this->error ("ID " $val " is not valid!");
            return 
FALSE;
        }
        
$this->error ("An empty ID is not valid!");
        return 
FALSE;
    }

    function 
format ($val) {
        if (!empty (
$val)) {
            if (
in_array ($valarray_keys ($this->getFormats ()))) {
                
$this->_format $val;
                if (
$this->_format != 'png') {
                    
$this->_image '';
                }
                return 
TRUE;
            }
            
$this->error ("Format " $val " is not valid!");
            return 
FALSE;
        }
        return 
TRUE;
    }

    function 
image ($val) {
        if (!empty (
$val)) {
            if (
in_array ($valarray_keys ($this->getImages ()))) {
                
$this->_image $val;
                
$this->_format 'png';
                return 
TRUE;
            }
            
$this->error ("Image " $val " is not valid!");
            return 
FALSE;
        }
        return 
TRUE;
    }

    function 
lang ($val) {
        if (!empty (
$val)) {
            if (
in_array ($valarray_keys ($this->getLangs ()))) {
                
$this->_lang $val;
                return 
TRUE;
            }
            
$this->error ("Language " $val " is not valid!");
            return 
FALSE;
        }
        return 
TRUE;
    }

    function 
error ($str) {
        
$this->_error[] = (!empty ($str) && is_string ($str) ? $str:'Unknown Error');
    }

    function 
getURL () {
        return (
            
"http://" .
            
SKYPE_BASE_URI .
            
"/" .
            
$this->getImage () .
            
$this->_id .
            
$this->getFormat () .
            
$this->getLang ()
        );
    }

    function 
getFormat () {
        return 
'.' $this->_format;
    }

    function 
getImage () {
        return (
$this->_format == 'png' && !empty ($this->_image) ? $this->_image '/':'');
    }

    function 
getLang () {
        if (
            !empty (
$this->_lang) &&
            
in_array ($this->_format, array ('txt''png')) &&
            !
in_array ($this->_image, array ('smallicon''mediumicon'))
        ) {
            return 
'.' $this->_lang;
        }
        return 
'';
    }

    function 
getFormats () {
        return (
            array (
                
'png' => 'Image',
                
'xml' => 'XML',
                
'txt' => 'Text',
                
'num' => 'Number',
            )
        );
    }

    function 
getImages () {
        return (
            array (
                
'balloon' => 'Ballon',
                
'bigclassic' => 'Big Classic',
                
'smallclassic' => 'Small Classic',
                
'smallicon' => 'Small Icon',
                
'mediumicon' => 'Medium Icon',
            )
        );
    }

    function 
getLangs () {
        return (
            array (
                
'en' => 'en',
                
'de' => 'de',
                
'fr' => 'fr',
                
'it' => 'it',
                
'pl' => 'pl',
                
'ja' => 'ja',
                
'pt' => 'pt',
                
'pt-br' => 'pt-br',
                
'se' => 'se',
                
'zh-cn' => 'zh',
                
'zh-cn' => 'cn',
                
'zh-cn' => 'zh-cn',
                
'zh-tw' => 'hk',
                
'zh-tw' => 'tw',
                
'zh-tw' => 'zh-tw',
            )
        );
    }

    function 
getHeader () {
        
$headers $this->getHeaders ();
        return 
'Content-type: ' $headers[$this->format];
    }

    function 
getHeaders () {
        return (
            array (
                
'txt' => 'text/plain',
                
'num' => 'text/plain',
                
'xml' => 'application/xhtml+xml',
                
'png' => 'image/png',
            )
        );
    }

    function 
result () {
        
$result '';
        if (!empty (
$this->_error)) {
            
$this->_format 'txt';
            
$result sprintf ('<pre>%s</pre>'implode ('<br/>'$this->_error));
        }
        else {
            if (
defined ("SKYPE_CURL") && SKYPE_CURL == 1) {
                
$ch curl_init ($this->getURL ()); 
                
curl_setopt ($chCURLOPT_RETURNTRANSFER1); 
                
$result curl_exec ($ch);
                
curl_close ($ch); 
            }
            else {
                
$result file_get_contents ($this->getURL ());
            }
        }
        return  
$result;
    }

    function 
repr () {
        
header ($this->getHeader ());
        echo 
$this->result ();
    }

}

class 
dnsSkype {
    
    var 
$_id;
    
    function 
id ($val) {
        if (!empty (
$val) && eregi ('^[a-z0-9@,.-]+$'$val)) {
            
$this->_id str_replace ('-''-2D'$val);
            
$this->_id =
                
str_replace (
                    array (
'.''_'',''@',),
                    array (
'-2E''-5F''-2C''-40',),
                    
$this->_id
                
);
        }
    }

    function 
result () {
        if (!empty (
$this->_id)) {
            
$output = array ();
            
exec (SKYPE_DNSCMD ' ' $this->_id '.' SKYPE_BASE_URI '.'$output);
            
$output implode (''$output);
            
preg_match_all (
                
"/\"\Wstatus=(\d+)\W+version=.+\W+str=(.+)\W+skypeid=(.+)\"/",
                
$output,
                
$regs,
                
PREG_SET_ORDER
            
);
            return 
$regs[0];
        }
    }

}

?>