Client.class.php (5049B)
1 <?php 2 3 4 class Client 5 { 6 public $useCookies = false; 7 public $success; 8 9 protected $action; 10 protected $subaction; 11 12 public $cookies = array(); 13 protected $sessionName; 14 protected $sessionId; 15 16 protected $token; 17 18 protected $method; // GET oder POST 19 20 protected $responseHeader; 21 protected $parameterString; 22 protected $requestHeader; 23 24 25 public function call($method,$action,$subaction,$parameter=array(),$direct=false) 26 { 27 global $config; 28 $error = ''; 29 $status = ''; 30 31 $errno = 0; 32 $errstr = ''; 33 34 $host = $config['cms.host']; 35 $port = $config['cms.port']; 36 $path = $config['cms.path']; 37 38 // Vorbedingungen checken: 39 // Slash an Anfang und Ende? 40 if ( substr($path,-1 ) != '/' ) 41 $path = $path.'/'; 42 if ( substr($path,0,1 ) != '/' ) 43 $path = '/'.$path; 44 $path .= '/api/'; 45 46 // Methode: Fallback GET 47 if ( !$method ) 48 $method='GET'; 49 50 // Die Funktion fsockopen() erwartet eine Protokollangabe (bei TCP optional, bei SSL notwendig). 51 if ( $port == '443' || @$config['ssl'] ) 52 $prx_proto = 'ssl://'; // SSL 53 else 54 $prx_proto = 'tcp://'; // Default 55 56 $fp = fsockopen ($prx_proto.$host,$port, $errno, $errstr, 30); 57 58 if ( !$fp || !is_resource($fp) ) 59 { 60 echo "Connection refused: '".$prx_proto.$host.':'.$port." - $errstr ($errno)"; 61 } 62 else 63 { 64 $lb = "\r\n"; 65 $http_get = $path; 66 67 $parameter += array('action'=>$action,'subaction'=>$subaction); 68 if ( $method=='POST') 69 $parameter += array('token'=>$this->token); 70 71 $this->parameterString = ''; 72 73 foreach( $parameter as $name=>$value ) 74 { 75 if ( $this->parameterString ) 76 $this->parameterString .= '&'; 77 78 $this->parameterString .= urlencode($name).'='.urlencode($value); 79 } 80 81 if ( $method == 'GET') 82 $http_get .= '?'.$this->parameterString; 83 84 $this->requestHeader = array(); 85 86 $this->requestHeader[] = $method.' '.$http_get.' HTTP/1.0'; 87 $this->requestHeader[] = 'Host: '.$host; 88 $this->requestHeader[] = 'Accept: application/php-serialized'; 89 90 if ( $this->useCookies) 91 { 92 $cookies = array();; 93 foreach( $this->cookies as $cookieName=>$cookieValue) 94 $cookies[] = $cookieName.'='.$cookieValue; 95 $this->requestHeader[] = 'Cookie: '.implode('; ',$cookies); 96 97 } 98 99 //if ( ! empty($this->sessionName)) 100 // $this->requestHeader[] = 'Cookie: '.$this->sessionName.'='.$this->sessionId; 101 102 if ( $method == 'POST' ) 103 { 104 $this->requestHeader[] = 'Content-Type: application/x-www-form-urlencoded'; 105 $this->requestHeader[] = 'Content-Length: '.strlen($this->parameterString); 106 } 107 108 $http_request = implode($lb,$this->requestHeader).$lb.$lb; 109 110 if ( $method == 'POST' ) 111 { 112 $http_request .= $this->parameterString; 113 } 114 if (!is_resource($fp)) { 115 $error = 'Connection lost after connect: '.$prx_proto.$host.':'.$port; 116 return false; 117 } 118 fputs($fp, $http_request); // Die HTTP-Anfrage zum Server senden. 119 120 // Jetzt erfolgt das Auslesen der HTTP-Antwort. 121 $isHeader = true; 122 123 // RFC 1945 (Section 6.1) schreibt als Statuszeile folgendes Format vor 124 // "HTTP/" 1*DIGIT "." 1*DIGIT SP 3DIGIT SP 125 if (!is_resource($fp)) { 126 echo 'Connection lost during transfer: '.$host.':'.$port; 127 } 128 elseif (!feof($fp)) { 129 $line = fgets($fp,1028); 130 $status = substr($line,9,3); 131 132 } 133 else 134 { 135 echo 'Unexpected EOF while reading HTTP-Response'; 136 } 137 138 $body=''; 139 while (!feof($fp)) { 140 $line = fgets($fp,1028); 141 if ( $isHeader && trim($line)=='' ) // Leerzeile nach Header. 142 { 143 $isHeader = false; 144 } 145 elseif( $isHeader ) 146 { 147 list($headerName,$headerValue) = explode(': ',$line) + array(1=>''); 148 $this->responseHeader[$headerName] = trim($headerValue); 149 } 150 else 151 { 152 $body .= $line; 153 } 154 } 155 fclose($fp); // Verbindung brav schlie�en. 156 157 if ( @$status != '200' ) 158 { 159 throw new RuntimeException('Server-Status != 200: '."$line\n".$body); 160 } 161 162 foreach( $this->responseHeader as $headerName => $headerValue) 163 { 164 if ( $headerName == 'Set-Cookie' ) 165 { 166 $parts = explode(';',$headerValue); 167 $payload = $parts[0]; 168 list( $cookieName,$cookieValue) = explode('=',$payload); 169 { 170 $this->cookies[trim($cookieName)] = trim($cookieValue); 171 } 172 } 173 } 174 175 if ( $direct ) 176 return $body; 177 178 $result = unserialize($body); 179 if ( $result === false ) 180 { 181 throw new RuntimeException('The server response cannot be unserialized into a PHP array'); 182 } 183 else 184 { 185 $this->sessionName = $result['session']['name']; 186 $this->sessionId = $result['session']['id']; 187 $this->token = $result['session']['token']; 188 189 $this->success = @$result['success'] == 'true'; 190 $this->notices = $result['notices']; 191 192 return $result['output']; 193 } 194 195 } 196 } 197 198 public function __toString() 199 { 200 return print_r( get_object_vars($this),true); 201 } 202 }