All pastes #387 Raw Edit

xmlrpc.php

public php v1 · immutable
#387 ·published 2004-08-01 07:57 UTC
rendered paste body
<?phpfunction Introspect($userdata) {    $str =  '<?xml version="1.0" ?><introspection version="1.0">';    $str .= '<methodList><methodDescription name="system.listMethods"><purpose>enumerates</purpose>';    $str .= '<signatures><signature><returns><value type="array" desc="an array of strings">';    $str .= '<value type="string">name of a method</value></returns></signature></signatures>';    $str .= "</methodDescription></methodList></introspection>\n";    return $str;}class XMLRPC_Client {    var $protocol;    var $server;    var $port;    var $base_url;    var $last_headers;    var $secret;    var $sfd;    var $map;    var $debug;    # Constructor, parse our URL    function XMLRPC_Client($url = 'http://xmlrpc.geeksanon.ca/', $map = 0, $debug = 0) {        $this->setURL($url);        $this->sfd = FALSE;        $this->secret = '';        $this->map = $map;        $this->debug = $debug;    }    function setSecret($secret) {        $this->secret = $secret;    }    function setURL($url) {        $this->protocol = $this->getProto($url);        $this->server = $this->getServer($url);        $this->port = $this->getPort($url);        $this->base_url = $this->getURL($url);    }    # Parse our URL into components    function getProto($url) {        preg_match(',^([^:]+)://,', $url, $matches);        return $matches[1];    }    function getServer($url) {        preg_match(',^[^:]+://([^/:]+),', $url, $matches);        return $matches[1];    }    function getPort($url) {        preg_match(',^[^:]+://[^/:]+:([0-9]+)/,', $url, $matches);        if(empty($matches[1]))          return 80;        else          return $matches[1];    }    function getURL($url) {        preg_match(',^[^:]+://[^/:]+(:[0-9]+)?/(.*),', $url, $matches);        return '/'.$matches[2];    }    # Send a request via HTTP/1.1    function sendRequestHTTP11($path, $method, $req) {        if(!$this->sfd)          $this->sfd = fsockopen($this->server, $this->port, $errno, $errstr);        elseif(feof($this->sfd))          $this->sfd = fsockopen($this->server, $this->port, $errno, $errstr);        $post =          "POST ".$this->base_url.$path." HTTP/1.1\r\n".          "User-Agent: GA-XMLRPC 1.4 ($method; ".strlen($req)." bytes; ".$_SERVER['REMOTE_ADDR'].")\r\n".          "Host: ".$this->server."\r\n".          "Referer: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\r\n".          #       "Accept-Encoding: gzip\r\n".          #       "Connection: keepalive\r\n".          "Content-Type: text/xml\r\n".          "Content-Length: ".strlen($req)."\r\n\r\n$req";        fputs($this->sfd, $post, strlen($post));        if($this->debug)          print($post);        $tot = '';        $this->last_headers = array();        $rs = 32768;        while(feof($this->sfd)===FALSE) {            $data = fgets($this->sfd, $rs);            if($data == "\r\n")              break;            else {                if(preg_match("/^([^ :]+):\s+(.*)\r\n/", $data, $matches))                  $this->last_headers[strtolower($matches[1])] = $matches[2];                if(preg_match(",^HTTP/(.*) ([0-9]{3}) (.*)\r\n,", $data, $matches)) {                    $this->last_headers['HTTP']['version'] = $matches[1];                    $this->last_headers['HTTP']['response'] = $matches[2];                    $this->last_headers['HTTP']['message'] = $matches[3];                }            }        }        if(!isset($this->last_headers['content-length']))          while($data = fread($this->sfd, $rs)) {              $tot .= $data;          }        else        {            $size = intval($this->last_headers['content-length']);            while($size > 0 && feof($this->sfd) === FALSE) {                $data = fread($this->sfd, ($size < $rs ? $size : $rs));                $tot .= $data;                $size = (($size - strlen($data)) < 0 ? 0 : $size - strlen($data));            }        }        if($this->debug) {            print_r($this->last_headers);            print($tot);            print(strlen($tot));        }        /*      if(isset($this->last_headers['content-encoding']) && preg_match('/crypt/',$this->last_headers['content-encoding'])) {         $iv = substr(md5($this->secret), 0, mcrypt_get_iv_size('tripledes',MCRYPT_MODE_CFB));         $tot = mcrypt_decrypt('tripledes', $this->secret, base64_decode($tot), "cfb", $iv);         }         if(isset($this->last_headers['content-encoding']) && preg_match('/gzip/',$this->last_headers['content-encoding'])) {         $str = substr($tot, 10);         $tot = gzinflate($str);         }*/        if(isset($this->last_headers['connection']) && $this->last_headers['connection'] == 'close') {            fclose($this->sfd);            $this->sfd = FALSE;        }        fclose($this->sfd);        $this->sfd = FALSE;        return $tot;    }    # Send a request    function m($method, $params = '') {        return $this->method($method, $params);    }    function method($method, $params = '') {        $req = xmlrpc_encode_request($method, $params, array('verbosity'=>'no_white_space'));        if($this->map) {            $a = split('\.',$method);            $path = $a[0].".php";        } else {            $path = '';        }        switch($this->protocol) {         default:         case 'http':            $data = $this->sendRequestHTTP11($path, $method, $req);            break;        }        $decode = xmlrpc_decode($data);        return $decode;    }    function __call($method, $params, &$return_value) {        if($this->debug) {            print "Using __call to call $method with ";            print_r($params);        }        $return_value = $this->m($method, $params);        return TRUE;    }}class XMLRPC_Server {    var $server;    var $secret;    var $methods;    function XMLRPC_Server($Methods = '') {        $this->server = xmlrpc_server_create();        //        xmlrpc_server_register_introspection_callback($this->server, "Introspect");        if(is_array($Methods))          $this->registerMethods($Methods);    }    function setSecret($secret) {        $this->secret = $secret;    }    function registerMethods($arr) {        foreach($arr as $key => $value) {            $this->methods[$key] = $value;            xmlrpc_server_register_method($this->server, $key, $value);        }    }    function handleRequest($xml) {        $resp = xmlrpc_server_call_method($this->server, $xml, '', array('verbosity'=>'no_white_space'));        header("Content-Type: text/xml");        /*      if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && preg_match('/gzip/',$_SERVER['HTTP_ACCEPT_ENCODING']) && strlen($resp) > 256)         {         $oldsize = strlen($resp);         $resp = gzencode($resp, 9);         if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && preg_match('/crypt/',$_SERVER['HTTP_ACCEPT_ENCODING']) && !empty($this->secret)) {         $iv = substr(md5($this->secret), 0, mcrypt_get_iv_size('tripledes',MCRYPT_MODE_CFB));         $resp = base64_encode(mcrypt_encrypt('tripledes', $this->secret, $resp, "cfb", $iv));         header("Content-Encoding: gzip, crypt");         } else {         header("Content-Encoding: gzip");         }         header("X-Compression: ".sprintf("%0.1f%%", ($oldsize / strlen($resp))));         }         else         {         if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && preg_match('/crypt/',$_SERVER['HTTP_ACCEPT_ENCODING']) && !empty($this->secret)) {         $iv = substr(md5($this->secret), 0, mcrypt_get_iv_size('tripledes',MCRYPT_MODE_CFB));         $resp = base64_encode(mcrypt_encrypt('tripledes', $this->secret, $resp, "cfb", $iv));         header("Content-Encoding: crypt");         }         }         */     header("Content-Length: ".strlen($resp));        print $resp;    }    function handle() {        global $HTTP_RAW_POST_DATA;        $this->handleRequest($HTTP_RAW_POST_DATA);    }}//overload('XMLRPC_Client');# We were directly called, so handle ALL methods we know ofif($_SERVER['SCRIPT_FILENAME'] == __FILE__){    $xml = new XMLRPC_Server;    $fp = fopen("methods.lst","r");    if($fp) {        while(!feof($fp)) {            $line = fgets($fp, 32768);            $arr = split("\t", $line);            print $arr[0];        }        fclose($fp);    }}?>