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