0ec24318b0a15c0b66f1ccf27f1d00a1763cfa7f
[core.git] / inc / main / classes / client / http / class_HttpClient.php
1 <?php
2 /**
3  * A HTTP client class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class HttpClient extends BaseClient implements Client {
25         // Constants
26         const HTTP_EOL = "\r\n";
27         const HTTP_USER_AGENT = 'HttpClient-Core/1.0';
28
29         /**
30          * Protected constructor
31          *
32          * @return      void
33          */
34         protected function __construct () {
35                 // Set default user agent string (to allow other classes to override this)
36                 $this->setUserAgent(self::HTTP_USER_AGENT);
37
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40         }
41
42         /**
43          * Creates an instance of this Client class and prepares it for usage
44          *
45          * @param       $socketResource         Resource of a socket (optional)
46          * @return      $clientInstance         An instance of a Client class
47          */
48         public final static function createHttpClient ($socketResouce = FALSE) {
49                 // Get a new instance
50                 $clientInstance = new HttpClient();
51
52                 // Set socket resource
53                 $clientInstance->setSocketResource($socketResource);
54
55                 // Return the prepared instance
56                 return $clientInstance;
57         }
58
59         /**
60          * Checks wether proxy configuration is used
61          *
62          * @return      $isUsed         Wether proxy is used
63          */
64         protected function isProxyUsed () {
65                 // Do we have cache?
66                 if (!isset($GLOBALS[__METHOD__])) {
67                         // Determine it
68                         $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0));
69                 } // END - if
70
71                 // Return cache
72                 return $GLOBALS[__METHOD__];
73         }
74
75         /**
76          * Sets up a proxy tunnel for given hostname and through resource
77          *
78          * @param       $host           Host to connect to
79          * @param       $port           Port number to connect to
80          * @return      $response       Response array
81          */
82         protected function setupProxyTunnel ($host, $port) {
83                 // Initialize array
84                 $response = array('', '', '');
85
86                 // Do the connect
87                 $respArray = $this->doConnectRequest($host, $port);
88
89                 // Analyze first header line
90                 if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
91                         // Response code is not 200
92                         return $response;
93                 } // END - if
94
95                 // All fine!
96                 return $respArray;
97         }
98
99         /**
100          * Sends a raw HTTP request out to given IP/host and port number
101          *
102          * @param       $method                 Request method (GET, POST, HEAD, CONNECT, ...)
103          * @param       $host                   Host to connect to
104          * @param       $port                   Port number to connect to
105          * @return      $responseArray  Array with raw response
106          */
107         private function sendRawHttpRequest ($method, $host, $port, array $header = array()) {
108                 // Minimum raw HTTP/1.1 request
109                 $rawRequest  = $method . ' ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
110                 $rawRequest .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
111
112                 // Use login data to proxy? (username at least)
113                 if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
114                         // Add it as well
115                         $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password'));
116                         $rawRequest .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL;
117                 } // END - if
118
119                 // Add last new-line
120                 $rawRequest .= self::HTTP_EOL;
121                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HTTP-CLIENT[' . __METHOD__ . ':' . __LINE__ . ']: rawRequest=' . $rawRequest);
122
123                 // Write request
124                 fwrite($this->getSocketResource(), $rawRequest);
125
126                 // Got response?
127                 if (feof($this->getSocketResource())) {
128                         // No response received
129                         return $response;
130                 } // END - if
131
132                 // Read the first line
133                 $resp = trim(fgets($this->getSocketResource(), 10240));
134
135                 // "Explode" the string to an array
136                 $responseArray = explode(' ', $resp);
137
138                 // And return it
139                 return $responseArray;
140         }
141
142         /**
143          * A HTTP/1.1 CONNECT request
144          *
145          * @param       $host   Host to connect to
146          * @param       $port   Port number to connect to
147          * @return      $responseArray  An array with the read response
148          */
149         public function doConnectRequest ($host, $port) {
150                 // Prepare extra header(s)
151                 $headers = array(
152                         'Proxy-Connection' => 'Keep-Alive'
153                 );
154
155                 // Prepare raw request
156                 $responseArray = $this->sendRawHttpRequest('CONNECT', $host, $port, $headers);
157
158                 // Return response array
159                 return $responseArray;
160         }
161
162 }