<?php
/**
 * curl wrapper
 *
 * wrapper for curl_* functions.
 *
 * @version:    0.1beta
 * @author:    alex 'alex_ez' yaroshevich <qfox@ya.ru>
 * @date:    20 oct 2008
 * @todo:    optimization, change parsing algorithms
 *
 */

class curl {

static function 
head$url$get null )
{
    
$useragents = array( 'iw2' => "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.8.1.17) Gecko/20080827 Iceweasel/2.0.0.17 (Debian-2.0.0.17-0etch1)", );
    
    
// create a new cURL resource
    
$ch curl_init( );
    
    
// set URL and other appropriate options
    
curl_setopt$chCURLOPT_URL,        $url );
    
curl_setopt$chCURLOPT_HEADER,    );
    
curl_setopt$chCURLOPT_RETURNTRANSFER);
    
curl_setopt$chCURLOPT_USERAGENT,    reset($useragents) );
    
curl_setopt$chCURLOPT_NOBODY,    );
    if( 
$get )
    
curl_setopt$chCURLOPT_HTTPGET,    );
    
    
// grab URL and pass it to the browser
    
$out curl_exec$ch );
    
$header_size curl_getinfo$chCURLINFO_HEADER_SIZE );
    
// close cURL resource, and free up system resources
    
curl_close$ch );
    
    
// parse response
    
$rawheaders substr($out0$header_size 4);
    
    
$_r = array( 'headers' => array( ) );
    
$rawheaders explode"\r\n"$rawheaders );
    
    
$_vcs explode' 'array_shift$rawheaders ) );
    
$_r['version'] = array_shift$_vcs );
    
$_r['code'] = array_shift$_vcs );
    
$_r['codestr'] = join' '$_vcs );
    
$_r['get'] = !!$get;
    
    if( 
$_r['code'] == 501 )
        return 
self::head$url);
    
    foreach( 
$rawheaders as $str )
    {
        
preg_match'@^([a-z0-9\-]+):\s@i'$str$m );
        if( 
strtolower($m[1]) == 'set-cookie' ) continue;
        
        
$v substr$strstrlen($m[1])+);
        
        
$_r['headers'][ $m[1] ] = $v;
    }
    
    return 
$_r;
}

static function 
request$url$options = array( ) )
{
    
$useragents = array( 'iw2' => "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.8.1.17) Gecko/20080827 Iceweasel/2.0.0.17 (Debian-2.0.0.17-0etch1)", );
    
    
// create a new cURL resource
    
$ch curl_init();
    
    
$_params = @$options['get'] ? self::pack$options['get'] ) : "";
    
// set URL and other appropriate options
    
curl_setopt($chCURLOPT_URL$url.'?'.$_params);
    
//curl_setopt($ch, CURLOPT_URL, $url);
    
curl_setopt($chCURLOPT_HEADER1);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_USERAGENTreset($useragents));
    global 
$cookiefile;
    
curl_setopt($chCURLOPT_COOKIEFILE$cookiefile );
    
curl_setopt($chCURLOPT_COOKIEJAR$cookiefile );
    
    
/*if( is_array( $options['cookies'] ) )
    foreach( $options['cookies'] as $k => $v )
        curl_setopt( $ch, CURLOPT_COOKIE, self::pack( array( $k => $v ) ) );*/
    
    // grab URL and pass it to the browser
    
$out curl_exec($ch);
    
    
// parse response
    
$header_size curl_getinfo$chCURLINFO_HEADER_SIZE );

    
$rawheaders substr($out0$header_size 4);
    
//echo "raw headers:\n".$rawheaders;
    
    
$_r = array(
        
'body' => substr$out$header_size ),
        
'headers' => array( ),
        
'cookies' => array( ),
        );
    
$rawheaders explode"\r\n"$rawheaders );
    list( 
$_r['version'], $_r['code'], $_r['codestr'] ) = explode' 'array_shift$rawheaders ) );
    foreach( 
$rawheaders as $str )
    {
        
preg_match'@^([a-z0-9\-]+):\s@i'$str$m );
        
        
$v substr$strstrlen($m[1])+);
        
        if( 
strtolower($m[1]) == 'set-cookie' ) {
            
$_set explode'; '$v );
            list( 
$k$v ) = explode'='array_shift$_set ) );
            
            
$_r['cookies'][ $k ][ 'value' ] = urldecode$v );
            foreach( 
$_set as $v ) {
                list( 
$_k$_v ) = explode'='$v );
                
$_r['cookies'][ $k ][ $_k ] = urldecode$v );
            }
        } else {
            
            
$_r['headers'][ $m[1] ] = $v;
        }
    }
    
    
// close cURL resource, and free up system resources
    
curl_close($ch);
    
    return 
$_r;
}

static function 
pack$params )
{
    
$_params '';
    foreach( 
$params as $k => $v )
        
$_params .= urlencode$k ).'='.urlencode$v ).'&';
    
$_params substr$_params0, -);
    return 
$_params;
}

static function 
unpack$_params )
{
    return array( );
}

}

if( !
is_callable'array_plain' ) )
{
function 
array_plain$a$_k null$_v null )
{
    
$_r = array( );
    
    foreach( 
$a as $k => $v )
        
$_r[ (is_null($_k)||!isset($v[$_v])) ? $k $v[$_k] ]
            = (
is_null($_v)||!isset($v[$_v])) ? $v $v[$_v];
    
    return 
$_r;
}
}

?>