l10n_update.http.inc 15 KB

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