l10n_update.http.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * @file
  4. * Http API for l10n updates.
  5. */
  6. /**
  7. * Check if remote file exists and when it was last updated.
  8. *
  9. * @param $url
  10. * URL of remote file.
  11. * @param $headers
  12. * HTTP request headers.
  13. * @return object
  14. * Result object containing the HTTP request headers, response code, headers,
  15. * data, redirect status and updated timestamp.
  16. * @see l10n_update_http_request()
  17. */
  18. function l10n_update_http_check($url, $headers = array()) {
  19. $result = l10n_update_http_request($url, array('headers' => $headers, 'method' => 'HEAD'));
  20. if (!isset($result->error)) {
  21. if ($result && $result->code == 200) {
  22. $result->updated = isset($result->headers['last-modified']) ? strtotime($result->headers['last-modified']) : 0;
  23. }
  24. return $result;
  25. }
  26. else {
  27. switch ($result->code) {
  28. case 404:
  29. // File not found occurs when a translation file is not yet available
  30. // at the translation server. But also if a custom module or custom
  31. // theme does not define the location of a translation file. By default
  32. // the file is checked at the translation server, but it will not be
  33. // found there.
  34. watchdog('l10n_update', 'File not found: @uri.', array('@uri' => $url));
  35. return TRUE;
  36. case 0:
  37. watchdog('l10n_update', 'Error occurred when trying to check @remote: @errormessage.', array('@errormessage' => $result->error, '@remote' => $url), WATCHDOG_ERROR);
  38. break;
  39. default:
  40. watchdog('l10n_update', 'HTTP error @errorcode occurred when trying to check @remote.', array('@errorcode' => $result->code, '@remote' => $url), WATCHDOG_ERROR);
  41. break;
  42. }
  43. }
  44. return $result;
  45. }
  46. /**
  47. * Perform an HTTP request.
  48. *
  49. * We cannot use drupal_http_request() at install, see http://drupal.org/node/527484
  50. *
  51. * This is a flexible and powerful HTTP client implementation. Correctly
  52. * handles GET, POST, PUT or any other HTTP requests. Handles redirects.
  53. *
  54. * @param $url
  55. * A string containing a fully qualified URI.
  56. * @param array $options
  57. * (optional) An array that can have one or more of the following elements:
  58. * - headers: An array containing request headers to send as name/value pairs.
  59. * - method: A string containing the request method. Defaults to 'GET'.
  60. * - data: A string containing the request body, formatted as
  61. * 'param=value&param=value&...'. Defaults to NULL.
  62. * - max_redirects: An integer representing how many times a redirect
  63. * may be followed. Defaults to 3.
  64. * - timeout: A float representing the maximum number of seconds the function
  65. * call may take. The default is 30 seconds. If a timeout occurs, the error
  66. * code is set to the HTTP_REQUEST_TIMEOUT constant.
  67. * - context: A context resource created with stream_context_create().
  68. *
  69. * @return object
  70. * An object that can have one or more of the following components:
  71. * - request: A string containing the request body that was sent.
  72. * - code: An integer containing the response status code, or the error code
  73. * if an error occurred.
  74. * - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
  75. * - status_message: The status message from the response, if a response was
  76. * received.
  77. * - redirect_code: If redirected, an integer containing the initial response
  78. * status code.
  79. * - redirect_url: If redirected, a string containing the URL of the redirect
  80. * target.
  81. * - error: If an error occurred, the error message. Otherwise not set.
  82. * - headers: An array containing the response headers as name/value pairs.
  83. * HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
  84. * easy access the array keys are returned in lower case.
  85. * - data: A string containing the response body that was received.
  86. */
  87. function l10n_update_http_request($url, array $options = array()) {
  88. $result = new stdClass();
  89. // Parse the URL and make sure we can handle the schema.
  90. $uri = @parse_url($url);
  91. if ($uri == FALSE) {
  92. $result->error = 'unable to parse URL';
  93. $result->code = -1001;
  94. return $result;
  95. }
  96. if (!isset($uri['scheme'])) {
  97. $result->error = 'missing schema';
  98. $result->code = -1002;
  99. return $result;
  100. }
  101. timer_start(__FUNCTION__);
  102. // Merge the default options.
  103. $options += array(
  104. 'headers' => array(),
  105. 'method' => 'GET',
  106. 'data' => NULL,
  107. 'max_redirects' => 3,
  108. 'timeout' => 30.0,
  109. 'context' => NULL,
  110. );
  111. // Merge the default headers.
  112. $options['headers'] += array(
  113. 'User-Agent' => 'Drupal (+http://drupal.org/)',
  114. );
  115. // stream_socket_client() requires timeout to be a float.
  116. $options['timeout'] = (float) $options['timeout'];
  117. // Use a proxy if one is defined and the host is not on the excluded list.
  118. $proxy_server = variable_get('proxy_server', '');
  119. if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
  120. // Set the scheme so we open a socket to the proxy server.
  121. $uri['scheme'] = 'proxy';
  122. // Set the path to be the full URL.
  123. $uri['path'] = $url;
  124. // Since the URL is passed as the path, we won't use the parsed query.
  125. unset($uri['query']);
  126. // Add in username and password to Proxy-Authorization header if needed.
  127. if ($proxy_username = variable_get('proxy_username', '')) {
  128. $proxy_password = variable_get('proxy_password', '');
  129. $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
  130. }
  131. // Some proxies reject requests with any User-Agent headers, while others
  132. // require a specific one.
  133. $proxy_user_agent = variable_get('proxy_user_agent', '');
  134. // The default value matches neither condition.
  135. if ($proxy_user_agent === NULL) {
  136. unset($options['headers']['User-Agent']);
  137. }
  138. elseif ($proxy_user_agent) {
  139. $options['headers']['User-Agent'] = $proxy_user_agent;
  140. }
  141. }
  142. switch ($uri['scheme']) {
  143. case 'proxy':
  144. // Make the socket connection to a proxy server.
  145. $socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080);
  146. // The Host header still needs to match the real request.
  147. $options['headers']['Host'] = $uri['host'];
  148. $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
  149. break;
  150. case 'http':
  151. case 'feed':
  152. $port = isset($uri['port']) ? $uri['port'] : 80;
  153. $socket = 'tcp://' . $uri['host'] . ':' . $port;
  154. // RFC 2616: "non-standard ports MUST, default ports MAY be included".
  155. // We don't add the standard port to prevent from breaking rewrite rules
  156. // checking the host that do not take into account the port number.
  157. $options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : '');
  158. break;
  159. case 'https':
  160. // Note: Only works when PHP is compiled with OpenSSL support.
  161. $port = isset($uri['port']) ? $uri['port'] : 443;
  162. $socket = 'ssl://' . $uri['host'] . ':' . $port;
  163. $options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');
  164. break;
  165. default:
  166. $result->error = 'invalid schema ' . $uri['scheme'];
  167. $result->code = -1003;
  168. return $result;
  169. }
  170. if (empty($options['context'])) {
  171. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
  172. }
  173. else {
  174. // Create a stream with context. Allows verification of a SSL certificate.
  175. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
  176. }
  177. // Make sure the socket opened properly.
  178. if (!$fp) {
  179. // When a network error occurs, we use a negative number so it does not
  180. // clash with the HTTP status codes.
  181. $result->code = -$errno;
  182. $result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket));
  183. // Mark that this request failed. This will trigger a check of the web
  184. // server's ability to make outgoing HTTP requests the next time that
  185. // requirements checking is performed.
  186. // See system_requirements().
  187. // variable_set('drupal_http_request_fails', TRUE);
  188. return $result;
  189. }
  190. // Construct the path to act on.
  191. $path = isset($uri['path']) ? $uri['path'] : '/';
  192. if (isset($uri['query'])) {
  193. $path .= '?' . $uri['query'];
  194. }
  195. // Only add Content-Length if we actually have any content or if it is a POST
  196. // or PUT request. Some non-standard servers get confused by Content-Length in
  197. // at least HEAD/GET requests, and Squid always requires Content-Length in
  198. // POST/PUT requests.
  199. $content_length = strlen($options['data']);
  200. if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
  201. $options['headers']['Content-Length'] = $content_length;
  202. }
  203. // If the server URL has a user then attempt to use basic authentication.
  204. if (isset($uri['user'])) {
  205. $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
  206. }
  207. // If the database prefix is being used by SimpleTest to run the tests in a copied
  208. // database then set the user-agent header to the database prefix so that any
  209. // calls to other Drupal pages will run the SimpleTest prefixed database. The
  210. // user-agent is used to ensure that multiple testing sessions running at the
  211. // same time won't interfere with each other as they would if the database
  212. // prefix were stored statically in a file or database variable.
  213. $test_info = &$GLOBALS['drupal_test_info'];
  214. if (!empty($test_info['test_run_id'])) {
  215. $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
  216. }
  217. $request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
  218. foreach ($options['headers'] as $name => $value) {
  219. $request .= $name . ': ' . trim($value) . "\r\n";
  220. }
  221. $request .= "\r\n" . $options['data'];
  222. $result->request = $request;
  223. // Calculate how much time is left of the original timeout value.
  224. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  225. if ($timeout > 0) {
  226. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  227. fwrite($fp, $request);
  228. }
  229. // Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
  230. // and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
  231. // instead must invoke stream_get_meta_data() each iteration.
  232. $info = stream_get_meta_data($fp);
  233. $alive = !$info['eof'] && !$info['timed_out'];
  234. $response = '';
  235. while ($alive) {
  236. // Calculate how much time is left of the original timeout value.
  237. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  238. if ($timeout <= 0) {
  239. $info['timed_out'] = TRUE;
  240. break;
  241. }
  242. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  243. $chunk = fread($fp, 1024);
  244. $response .= $chunk;
  245. $info = stream_get_meta_data($fp);
  246. $alive = !$info['eof'] && !$info['timed_out'] && $chunk;
  247. }
  248. fclose($fp);
  249. if ($info['timed_out']) {
  250. $result->code = HTTP_REQUEST_TIMEOUT;
  251. $result->error = 'request timed out';
  252. return $result;
  253. }
  254. // Parse response headers from the response body.
  255. // Be tolerant of malformed HTTP responses that separate header and body with
  256. // \n\n or \r\r instead of \r\n\r\n.
  257. list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
  258. $response = preg_split("/\r\n|\n|\r/", $response);
  259. // Parse the response status line.
  260. list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
  261. $result->protocol = $protocol;
  262. $result->status_message = $status_message;
  263. $result->headers = array();
  264. // Parse the response headers.
  265. while ($line = trim(array_shift($response))) {
  266. list($name, $value) = explode(':', $line, 2);
  267. $name = strtolower($name);
  268. if (isset($result->headers[$name]) && $name == 'set-cookie') {
  269. // RFC 2109: the Set-Cookie response header comprises the token Set-
  270. // Cookie:, followed by a comma-separated list of one or more cookies.
  271. $result->headers[$name] .= ',' . trim($value);
  272. }
  273. else {
  274. $result->headers[$name] = trim($value);
  275. }
  276. }
  277. $responses = array(
  278. 100 => 'Continue',
  279. 101 => 'Switching Protocols',
  280. 200 => 'OK',
  281. 201 => 'Created',
  282. 202 => 'Accepted',
  283. 203 => 'Non-Authoritative Information',
  284. 204 => 'No Content',
  285. 205 => 'Reset Content',
  286. 206 => 'Partial Content',
  287. 300 => 'Multiple Choices',
  288. 301 => 'Moved Permanently',
  289. 302 => 'Found',
  290. 303 => 'See Other',
  291. 304 => 'Not Modified',
  292. 305 => 'Use Proxy',
  293. 307 => 'Temporary Redirect',
  294. 400 => 'Bad Request',
  295. 401 => 'Unauthorized',
  296. 402 => 'Payment Required',
  297. 403 => 'Forbidden',
  298. 404 => 'Not Found',
  299. 405 => 'Method Not Allowed',
  300. 406 => 'Not Acceptable',
  301. 407 => 'Proxy Authentication Required',
  302. 408 => 'Request Time-out',
  303. 409 => 'Conflict',
  304. 410 => 'Gone',
  305. 411 => 'Length Required',
  306. 412 => 'Precondition Failed',
  307. 413 => 'Request Entity Too Large',
  308. 414 => 'Request-URI Too Large',
  309. 415 => 'Unsupported Media Type',
  310. 416 => 'Requested range not satisfiable',
  311. 417 => 'Expectation Failed',
  312. 500 => 'Internal Server Error',
  313. 501 => 'Not Implemented',
  314. 502 => 'Bad Gateway',
  315. 503 => 'Service Unavailable',
  316. 504 => 'Gateway Time-out',
  317. 505 => 'HTTP Version not supported',
  318. );
  319. // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  320. // base code in their class.
  321. if (!isset($responses[$code])) {
  322. $code = floor($code / 100) * 100;
  323. }
  324. $result->code = $code;
  325. switch ($code) {
  326. case 200: // OK
  327. case 304: // Not modified
  328. break;
  329. case 301: // Moved permanently
  330. case 302: // Moved temporarily
  331. case 307: // Moved temporarily
  332. $location = $result->headers['location'];
  333. $options['timeout'] -= timer_read(__FUNCTION__) / 1000;
  334. if ($options['timeout'] <= 0) {
  335. $result->code = HTTP_REQUEST_TIMEOUT;
  336. $result->error = 'request timed out';
  337. }
  338. elseif ($options['max_redirects']) {
  339. // Redirect to the new location.
  340. $options['max_redirects']--;
  341. $result = l10n_update_http_request($location, $options);
  342. $result->redirect_code = $code;
  343. }
  344. if (!isset($result->redirect_url)) {
  345. $result->redirect_url = $location;
  346. }
  347. break;
  348. default:
  349. $result->error = $status_message;
  350. }
  351. return $result;
  352. }