File cms/Client.class.php

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