<?php

class DES
{
    var $key;
    var $iv; //偏移量
     
    function DES( $key, $iv=0 ) {
		//key长度8例如:1234abcd
        $this->key = $key;
        if( $iv == 0 ) {
            $this->iv = $key; //默认以$key 作为 iv
        } else {
            $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );
        }
    }
     
    function encrypt($str) {
    //加密，返回大写十六进制字符串
        $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
        $str = $this->pkcs5Pad ( $str, $size );
        return strtoupper( bin2hex( mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv ) ) );
    }
     
    function decrypt($str) {
    //解密
        $strBin = $this->hex2bin( strtolower( $str ) );
        $str = mcrypt_cbc( MCRYPT_DES, $this->key, $strBin, MCRYPT_DECRYPT, $this->iv );
        $str = $this->pkcs5Unpad( $str );
        return $str;
    }
     
    function hex2bin($hexData) {
        $binData = "";
        for($i = 0; $i < strlen ( $hexData ); $i += 2) {
            $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );
        }
        return $binData;
    }
 
    function pkcs5Pad($text, $blocksize) {
        $pad = $blocksize - (strlen ( $text ) % $blocksize);
        return $text . str_repeat ( chr ( $pad ), $pad );
    }
     
    function pkcs5Unpad($text) {
        $pad = ord ( $text {strlen ( $text ) - 1} );
        if ($pad > strlen ( $text ))
            return false;
        if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
            return false;
        return substr ( $text, 0, - 1 * $pad );
    }
}

class allcom
{
	var $userid;
	var $pass;
	var $deskey;
	var $type;

    function allcom( $userid, $pass ,$type="md5",$deskey="") {
		$this->userid = $userid;
		$this->pass = $pass;
		$this->type = $type;
		$this->deskey = $deskey;
	}

	function getAuth(){
		if($this->type == "basic"){
			return "basic " . base64_encode($this->userid.":".$this->pass);
		}
		else if($this->type=="md5"){
			return "md5 " . base64_encode($this->userid.":".md5($this->pass));
		}
		else if($this->type=="des"){
			$des = new DES($this->deskey);
			return "des " . base64_encode($this->userid.":".$des->encrypt($this->pass));
		}
	}

	function getHttp($url){
		$opts = array('http'=>array('method'=>"GET",'header'=>"Authorization: ".$this->getAuth() ));  
		$context = stream_context_create($opts);
		$result = @file_get_contents($url, false, $context); 
		return $result;
	}

	function sendFax($url,$postdata, $files){
		$data = ""; 
		$boundary =  md5(time());

		foreach($postdata as $key => $val) 
		{ 
			$data .= "--$boundary\r\n"; 
			$data .= "Content-Disposition: form-data; name=\"".$key."\"\r\n\r\n".$val."\r\n"; 
		} 
		$data .= "--$boundary\r\n"; 

		foreach($files as $key => $file) 
		{ 
			$fileContents = file_get_contents($file); 
			$filename = basename ($file);
			$data .= "Content-Disposition: form-data; name=\"$key\"; filename=\"$filename\"\r\n";
			$data .= "Content-Type: application/octet-stream\r\n";
			$data .= "Content-Transfer-Encoding: binary\r\n\r\n";
			$data .= $fileContents."\r\n";
			$data .= "--$boundary--\r\n";
		} 

		$opts = array('http' => array( 
			'method' => 'POST', 
			'header'=>"Authorization: ".$this->getAuth()."\r\n".
			'Content-Type: multipart/form-data; boundary='.$boundary, 
			'content' => $data 
		)); 

		$ctx = stream_context_create($opts); 
		$fp = @fopen($url, 'rb', false, $ctx);
		
   
		if (!$fp) {
			return "";
		}


		$response = @stream_get_contents($fp); 
		if ($response === false) { 
			return "";
		} 
		return $response; 
	} 
}
?>
