<?php
class qqrobot {
	function qqrobot(){

	}

	function send($ExternalId, $Sender, $Message)
	{
		if($GLOBALS['_G']['cache']['plugin']['qqrobot']['mysql'])
		{
			$Message = '[font=宋体,9,'.rand(1,16777215).']'.$Message;
			DB::query('INSERT INTO '.DB::table('qqrobot_send')." (content) values('$ExternalId\r\n$Sender\r\n$Message')");
		}
		else
		{
			$Message = rawurlencode(mb_convert_encoding($Message,'utf-8', 'gb2312'));
			require_once libfile('function/filesock');
			$html = _dfsockopen($GLOBALS['_G']['cache']['plugin']['qqrobot']['roboturl']."ExternalId=$ExternalId&Sender=$Sender&Message=$Message");
		}
	}
}

function HttpSoap($server, $port, $url, $namespace, $action, $data) {
    $fp = @fsockopen($server, $port);
    if (!$fp) {
        return FALSE;
    } else {
        $soapData = ConstructData($namespace, $action, $data);
        $length = strlen($soapData);

        $out = "POST $url HTTP/1.1\r\n";
        $out .= "Host: $server\r\n";
        $out .= "Content-Type: text/xml; charset=utf-8\r\n";
        $out .= "Content-Length: $length\r\n";
        $out .= "SOAPAction: \"$namespace$action\"\r\n\r\n";
        $out .= $soapData;
        $out .= "\r\n\r\n";

        fputs($fp, $out);
        stream_set_timeout($fp, 2);

        $header = "";
        while($line = trim(fgets($fp))) {
            $header .= $line."\n";
        }
        $dataPos = strpos($header, "Content-Length: ") + 16;
        $dataEnd = strpos($header, "\n", $dataPos);
        $dataLength = substr($header, $dataPos, $dataEnd - $dataPos);
        $data = "";
        if($dataLength > 0) {
            $data = fread($fp, $dataLength);
        }
        fclose($fp);
        if(strlen($data) != $dataLength || $dataLength <= 0) {
            return FALSE;
        }
        return $data;
    }
}

function GetSOAPResult($str)
{
	return strip_tags($str);
}

function ConstructData($namespace, $action, $data) {
    $soapData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
    $soapData .= "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n";
    $soapData .= "  <soap:Body>\r\n";
    $soapData .= "    <$action xmlns=\"$namespace\">\r\n";
    foreach($data as $name => $value) {
        //$name = iconv("GBK","UTF-8",$name);
        //$value= iconv("GBK","UTF-8",$value);
        $soapData .= "      <$name>$value</$name>\r\n";
    }
    $soapData .= "    </$action>\r\n";
    $soapData .= "  </soap:Body>\r\n";
    $soapData .= "</soap:Envelope>";
    
    return $soapData;
}
?>