l10n_update.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. <?php
  2. /**
  3. * @file
  4. * Reusable API for l10n remote updates.
  5. */
  6. include_once DRUPAL_ROOT . '/includes/locale.inc';
  7. module_load_include('locale.inc', 'l10n_update');
  8. /**
  9. * Default update server, filename and URL.
  10. */
  11. define('L10N_UPDATE_DEFAULT_SERVER', 'localize.drupal.org');
  12. define('L10N_UPDATE_DEFAULT_SERVER_URL', 'http://localize.drupal.org/l10n_server.xml');
  13. define('L10N_UPDATE_DEFAULT_UPDATE_URL', 'http://ftp.drupal.org/files/translations/%core/%project/%project-%release.%language.po');
  14. // Translation filename, will be used just for local imports
  15. define('L10N_UPDATE_DEFAULT_FILENAME', '%project-%release.%language.po');
  16. // Translation status: String imported from po
  17. define('L10N_UPDATE_STRING_DEFAULT', 0);
  18. // Translation status: Custom string, overridden original import
  19. define('L10N_UPDATE_STRING_CUSTOM', 1);
  20. /**
  21. * Retrieve data for default server.
  22. *
  23. * @return array
  24. * Server parameters:
  25. * name : Localization server name
  26. * server_url : Localization server URL where language list can be retrieved.
  27. * update_url : URL containing po file pattern.
  28. */
  29. function l10n_update_default_server() {
  30. return array(
  31. 'name' => variable_get('l10n_update_default_server', L10N_UPDATE_DEFAULT_SERVER),
  32. 'server_url' => variable_get('l10n_update_default_server_url', L10N_UPDATE_DEFAULT_SERVER_URL),
  33. 'update_url' => variable_get('l10n_update_default_update_url', L10N_UPDATE_DEFAULT_UPDATE_URL),
  34. );
  35. }
  36. /**
  37. * Download and import remote translation file.
  38. *
  39. * @param $download_url
  40. * Download URL.
  41. * @param $locale
  42. * Language code.
  43. * @param $mode
  44. * Download mode. How to treat exising and modified translations.
  45. *
  46. * @return boolean
  47. * TRUE on success.
  48. */
  49. function l10n_update_download_import($download_url, $locale, $mode = LOCALE_IMPORT_OVERWRITE) {
  50. if ($file = l10n_update_download_file($download_url)) {
  51. $result = l10n_update_import_file($file, $locale, $mode);
  52. return $result;
  53. }
  54. }
  55. /**
  56. * Import local file into the database.
  57. *
  58. * @param $file
  59. * File object of localy stored file
  60. * or path to localy stored file.
  61. * @param $locale
  62. * Language code.
  63. * @param $mode
  64. * Download mode. How to treat exising and modified translations.
  65. *
  66. * @return boolean
  67. * Result array on success. FALSE on failure
  68. */
  69. function l10n_update_import_file($file, $locale, $mode = LOCALE_IMPORT_OVERWRITE) {
  70. // If the file is a uri, create a $file object
  71. if (is_string($file)) {
  72. $uri = $file;
  73. $file = new stdClass();
  74. $file->uri = $uri;
  75. $file->filename = $uri;
  76. }
  77. return _l10n_update_locale_import_po($file, $locale, $mode, 'default');
  78. }
  79. /**
  80. * Get remote file and download it to a temporary path.
  81. *
  82. * @param $download_url
  83. * URL of remote file.
  84. * @param $destination
  85. * URL of local destination file. By default the download will be stored
  86. * in a temporary file.
  87. */
  88. function l10n_update_download_file($download_url, $destination = NULL) {
  89. $t = get_t();
  90. $variables['%download_link'] = $download_url;
  91. // Create temporary file or use specified file destination.
  92. // Temporary files get a 'translation-' file name prefix.
  93. $file = $destination ? $destination : drupal_tempnam(file_directory_temp(), 'translation-');
  94. if ($file) {
  95. $variables['%tmpfile'] = $file;
  96. // We download and store the file (in one if statement! Isnt't that neat ;) ).
  97. // @todo remove the timeout once we use the batch API to download the files.
  98. if (($contents = drupal_http_request($download_url, array('timeout' => 90))) && $contents->code == 200 && $file_result = file_put_contents($file, $contents->data)) {
  99. watchdog('l10n_update', 'Successfully downloaded %download_link to %tmpfile', $variables);
  100. return $file;
  101. }
  102. else {
  103. if (isset($contents->error)) {
  104. watchdog('l10n_update', 'An error occured during the download operation: %error.', array('%error' => $contents->error), WATCHDOG_ERROR);
  105. }
  106. elseif (isset($contents->code) && $contents->code != 200) {
  107. watchdog('l10n_update', 'An error occured during the download operation: HTTP status code %code.', array('%code' => $contents->code), WATCHDOG_ERROR);
  108. }
  109. if (isset($file_result)) {
  110. // file_put_contents() was called but returned FALSE.
  111. watchdog('l10n_update', 'Unable to save %download_link file to %tmpfile.', $variables, WATCHDOG_ERROR);
  112. }
  113. }
  114. }
  115. else {
  116. $variables['%tmpdir'] = file_directory_temp();
  117. watchdog('l10n_update', 'Error creating temporary file for download in %tmpdir. Remote file is %download_link.', $variables, WATCHDOG_ERROR);
  118. }
  119. }
  120. /**
  121. * Get names for the language list from locale system.
  122. *
  123. * @param $string_list
  124. * Comma separated list of language codes.
  125. * Language codes must exist in languages from _locale_get_predefined_list().
  126. * @return array
  127. * Array of language names keyed by language code.
  128. */
  129. function l10n_update_get_language_names($string_list) {
  130. $t = get_t();
  131. $language_codes = array_map('trim', explode(',', $string_list));
  132. $languages = _locale_get_predefined_list();
  133. $result = array();
  134. foreach ($language_codes as $lang) {
  135. if (array_key_exists($lang, $languages)) {
  136. // Try to use verbose locale name
  137. $name = $lang;
  138. $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' ' . $t('(@language)', array('@language' => $languages[$name][1])) : '');
  139. $result[$lang] = $name;
  140. }
  141. }
  142. return $result;
  143. }
  144. /**
  145. * Build project data as an object.
  146. *
  147. * @param $name
  148. * Project name.
  149. * @param $version
  150. * Project version.
  151. * @param $server
  152. * Localisation server name.
  153. * @param $path
  154. * Localisation server URL.
  155. * @return object
  156. * Project object containing the supplied data.
  157. */
  158. function _l10n_update_build_project($name, $version = NULL, $server = L10N_UPDATE_DEFAULT_SERVER, $path = L10N_UPDATE_DEFAULT_SERVER_URL) {
  159. $project = new stdClass();
  160. $project->name = $name;
  161. $project->version = $version;
  162. $project->l10n_server = $server;
  163. $project->l10n_path = $path;
  164. return $project;
  165. }
  166. /**
  167. * Update the file history table.
  168. *
  169. * @param $file
  170. * Object representing the file just imported or downloaded.
  171. * @return integer
  172. * FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED.
  173. * @see drupal_write_record()
  174. */
  175. function l10n_update_file_history($file) {
  176. // Update or write new record
  177. if (db_query("SELECT project FROM {l10n_update_file} WHERE project = :project AND language = :language", array(':project' => $file->project, ':language' => $file->language))->fetchField()) {
  178. $update = array('project', 'language');
  179. }
  180. else {
  181. $update = array();
  182. }
  183. return drupal_write_record('l10n_update_file', $file, $update);
  184. }
  185. /**
  186. * Delete the history of downloaded translations.
  187. *
  188. * @param string $langcode
  189. * Language code of the file history to be deleted.
  190. */
  191. function l10n_update_delete_file_history($langcode) {
  192. db_delete('l10n_update_file')
  193. ->condition('language', $langcode)
  194. ->execute();
  195. }
  196. /**
  197. * Flag the file history as up to date.
  198. *
  199. * Compare history data in the {l10n_update_file} table with translations
  200. * available at translations server(s). Update the 'last_checked' timestamp of
  201. * the files which are up to date.
  202. *
  203. * @param $available
  204. * Available translations as retreived from remote server.
  205. */
  206. function l10n_update_flag_history($available) {
  207. if ($history = l10n_update_get_history()) {
  208. foreach($history as $name => $project) {
  209. foreach ($project as $langcode => $current) {
  210. if (isset($available[$name][$langcode])) {
  211. $update = $available[$name][$langcode];
  212. // When the available update is equal to the current translation the current
  213. // is marked checked in the {l10n_update_file} table.
  214. if (_l10n_update_source_compare($current, $update) == 0 && $current->version == $update->version) {
  215. db_update('l10n_update_file')
  216. ->fields(array(
  217. 'last_checked' => REQUEST_TIME,
  218. ))
  219. ->condition('project', $current->project)
  220. ->condition('language', $current->language)
  221. ->execute();
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. /**
  229. * Check if remote file exists and when it was last updated.
  230. *
  231. * @param $url
  232. * URL of remote file.
  233. * @param $headers
  234. * HTTP request headers.
  235. * @return object
  236. * Result object containing the HTTP request headers, response code, headers,
  237. * data, redirect status and updated timestamp.
  238. * @see l10n_update_http_request()
  239. */
  240. function l10n_update_http_check($url, $headers = array()) {
  241. $result = l10n_update_http_request($url, array('headers' => $headers, 'method' => 'HEAD'));
  242. if (!isset($result->error)) {
  243. if ($result && $result->code == 200) {
  244. $result->updated = isset($result->headers['last-modified']) ? strtotime($result->headers['last-modified']) : 0;
  245. }
  246. return $result;
  247. }
  248. else {
  249. switch ($result->code) {
  250. case 404:
  251. // File not found occurs when a translation file is not yet available
  252. // at the translation server. But also if a custom module or custom
  253. // theme does not define the location of a translation file. By default
  254. // the file is checked at the translation server, but it will not be
  255. // found there.
  256. watchdog('l10n_update', 'File not found: @uri.', array('@uri' => $url));
  257. return TRUE;
  258. case 0:
  259. watchdog('l10n_update', 'Error occurred when trying to check @remote: @errormessage.', array('@errormessage' => $result->error, '@remote' => $url), WATCHDOG_ERROR);
  260. break;
  261. default:
  262. watchdog('l10n_update', 'HTTP error @errorcode occurred when trying to check @remote.', array('@errorcode' => $result->code, '@remote' => $url), WATCHDOG_ERROR);
  263. break;
  264. }
  265. }
  266. return $result;
  267. }
  268. /**
  269. * Perform an HTTP request.
  270. *
  271. * We cannot use drupal_http_request() at install, see http://drupal.org/node/527484
  272. *
  273. * This is a flexible and powerful HTTP client implementation. Correctly
  274. * handles GET, POST, PUT or any other HTTP requests. Handles redirects.
  275. *
  276. * @param $url
  277. * A string containing a fully qualified URI.
  278. * @param array $options
  279. * (optional) An array that can have one or more of the following elements:
  280. * - headers: An array containing request headers to send as name/value pairs.
  281. * - method: A string containing the request method. Defaults to 'GET'.
  282. * - data: A string containing the request body, formatted as
  283. * 'param=value&param=value&...'. Defaults to NULL.
  284. * - max_redirects: An integer representing how many times a redirect
  285. * may be followed. Defaults to 3.
  286. * - timeout: A float representing the maximum number of seconds the function
  287. * call may take. The default is 30 seconds. If a timeout occurs, the error
  288. * code is set to the HTTP_REQUEST_TIMEOUT constant.
  289. * - context: A context resource created with stream_context_create().
  290. *
  291. * @return object
  292. * An object that can have one or more of the following components:
  293. * - request: A string containing the request body that was sent.
  294. * - code: An integer containing the response status code, or the error code
  295. * if an error occurred.
  296. * - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
  297. * - status_message: The status message from the response, if a response was
  298. * received.
  299. * - redirect_code: If redirected, an integer containing the initial response
  300. * status code.
  301. * - redirect_url: If redirected, a string containing the URL of the redirect
  302. * target.
  303. * - error: If an error occurred, the error message. Otherwise not set.
  304. * - headers: An array containing the response headers as name/value pairs.
  305. * HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
  306. * easy access the array keys are returned in lower case.
  307. * - data: A string containing the response body that was received.
  308. */
  309. function l10n_update_http_request($url, array $options = array()) {
  310. $result = new stdClass();
  311. // Parse the URL and make sure we can handle the schema.
  312. $uri = @parse_url($url);
  313. if ($uri == FALSE) {
  314. $result->error = 'unable to parse URL';
  315. $result->code = -1001;
  316. return $result;
  317. }
  318. if (!isset($uri['scheme'])) {
  319. $result->error = 'missing schema';
  320. $result->code = -1002;
  321. return $result;
  322. }
  323. timer_start(__FUNCTION__);
  324. // Merge the default options.
  325. $options += array(
  326. 'headers' => array(),
  327. 'method' => 'GET',
  328. 'data' => NULL,
  329. 'max_redirects' => 3,
  330. 'timeout' => 30.0,
  331. 'context' => NULL,
  332. );
  333. // Merge the default headers.
  334. $options['headers'] += array(
  335. 'User-Agent' => 'Drupal (+http://drupal.org/)',
  336. );
  337. // stream_socket_client() requires timeout to be a float.
  338. $options['timeout'] = (float) $options['timeout'];
  339. // Use a proxy if one is defined and the host is not on the excluded list.
  340. $proxy_server = variable_get('proxy_server', '');
  341. if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
  342. // Set the scheme so we open a socket to the proxy server.
  343. $uri['scheme'] = 'proxy';
  344. // Set the path to be the full URL.
  345. $uri['path'] = $url;
  346. // Since the URL is passed as the path, we won't use the parsed query.
  347. unset($uri['query']);
  348. // Add in username and password to Proxy-Authorization header if needed.
  349. if ($proxy_username = variable_get('proxy_username', '')) {
  350. $proxy_password = variable_get('proxy_password', '');
  351. $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
  352. }
  353. // Some proxies reject requests with any User-Agent headers, while others
  354. // require a specific one.
  355. $proxy_user_agent = variable_get('proxy_user_agent', '');
  356. // The default value matches neither condition.
  357. if ($proxy_user_agent === NULL) {
  358. unset($options['headers']['User-Agent']);
  359. }
  360. elseif ($proxy_user_agent) {
  361. $options['headers']['User-Agent'] = $proxy_user_agent;
  362. }
  363. }
  364. switch ($uri['scheme']) {
  365. case 'proxy':
  366. // Make the socket connection to a proxy server.
  367. $socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080);
  368. // The Host header still needs to match the real request.
  369. $options['headers']['Host'] = $uri['host'];
  370. $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
  371. break;
  372. case 'http':
  373. case 'feed':
  374. $port = isset($uri['port']) ? $uri['port'] : 80;
  375. $socket = 'tcp://' . $uri['host'] . ':' . $port;
  376. // RFC 2616: "non-standard ports MUST, default ports MAY be included".
  377. // We don't add the standard port to prevent from breaking rewrite rules
  378. // checking the host that do not take into account the port number.
  379. $options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : '');
  380. break;
  381. case 'https':
  382. // Note: Only works when PHP is compiled with OpenSSL support.
  383. $port = isset($uri['port']) ? $uri['port'] : 443;
  384. $socket = 'ssl://' . $uri['host'] . ':' . $port;
  385. $options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');
  386. break;
  387. default:
  388. $result->error = 'invalid schema ' . $uri['scheme'];
  389. $result->code = -1003;
  390. return $result;
  391. }
  392. if (empty($options['context'])) {
  393. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
  394. }
  395. else {
  396. // Create a stream with context. Allows verification of a SSL certificate.
  397. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
  398. }
  399. // Make sure the socket opened properly.
  400. if (!$fp) {
  401. // When a network error occurs, we use a negative number so it does not
  402. // clash with the HTTP status codes.
  403. $result->code = -$errno;
  404. $result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket));
  405. // Mark that this request failed. This will trigger a check of the web
  406. // server's ability to make outgoing HTTP requests the next time that
  407. // requirements checking is performed.
  408. // See system_requirements().
  409. // variable_set('drupal_http_request_fails', TRUE);
  410. return $result;
  411. }
  412. // Construct the path to act on.
  413. $path = isset($uri['path']) ? $uri['path'] : '/';
  414. if (isset($uri['query'])) {
  415. $path .= '?' . $uri['query'];
  416. }
  417. // Only add Content-Length if we actually have any content or if it is a POST
  418. // or PUT request. Some non-standard servers get confused by Content-Length in
  419. // at least HEAD/GET requests, and Squid always requires Content-Length in
  420. // POST/PUT requests.
  421. $content_length = strlen($options['data']);
  422. if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
  423. $options['headers']['Content-Length'] = $content_length;
  424. }
  425. // If the server URL has a user then attempt to use basic authentication.
  426. if (isset($uri['user'])) {
  427. $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
  428. }
  429. // If the database prefix is being used by SimpleTest to run the tests in a copied
  430. // database then set the user-agent header to the database prefix so that any
  431. // calls to other Drupal pages will run the SimpleTest prefixed database. The
  432. // user-agent is used to ensure that multiple testing sessions running at the
  433. // same time won't interfere with each other as they would if the database
  434. // prefix were stored statically in a file or database variable.
  435. $test_info = &$GLOBALS['drupal_test_info'];
  436. if (!empty($test_info['test_run_id'])) {
  437. $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
  438. }
  439. $request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
  440. foreach ($options['headers'] as $name => $value) {
  441. $request .= $name . ': ' . trim($value) . "\r\n";
  442. }
  443. $request .= "\r\n" . $options['data'];
  444. $result->request = $request;
  445. // Calculate how much time is left of the original timeout value.
  446. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  447. if ($timeout > 0) {
  448. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  449. fwrite($fp, $request);
  450. }
  451. // Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
  452. // and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
  453. // instead must invoke stream_get_meta_data() each iteration.
  454. $info = stream_get_meta_data($fp);
  455. $alive = !$info['eof'] && !$info['timed_out'];
  456. $response = '';
  457. while ($alive) {
  458. // Calculate how much time is left of the original timeout value.
  459. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  460. if ($timeout <= 0) {
  461. $info['timed_out'] = TRUE;
  462. break;
  463. }
  464. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  465. $chunk = fread($fp, 1024);
  466. $response .= $chunk;
  467. $info = stream_get_meta_data($fp);
  468. $alive = !$info['eof'] && !$info['timed_out'] && $chunk;
  469. }
  470. fclose($fp);
  471. if ($info['timed_out']) {
  472. $result->code = HTTP_REQUEST_TIMEOUT;
  473. $result->error = 'request timed out';
  474. return $result;
  475. }
  476. // Parse response headers from the response body.
  477. // Be tolerant of malformed HTTP responses that separate header and body with
  478. // \n\n or \r\r instead of \r\n\r\n.
  479. list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
  480. $response = preg_split("/\r\n|\n|\r/", $response);
  481. // Parse the response status line.
  482. list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
  483. $result->protocol = $protocol;
  484. $result->status_message = $status_message;
  485. $result->headers = array();
  486. // Parse the response headers.
  487. while ($line = trim(array_shift($response))) {
  488. list($name, $value) = explode(':', $line, 2);
  489. $name = strtolower($name);
  490. if (isset($result->headers[$name]) && $name == 'set-cookie') {
  491. // RFC 2109: the Set-Cookie response header comprises the token Set-
  492. // Cookie:, followed by a comma-separated list of one or more cookies.
  493. $result->headers[$name] .= ',' . trim($value);
  494. }
  495. else {
  496. $result->headers[$name] = trim($value);
  497. }
  498. }
  499. $responses = array(
  500. 100 => 'Continue',
  501. 101 => 'Switching Protocols',
  502. 200 => 'OK',
  503. 201 => 'Created',
  504. 202 => 'Accepted',
  505. 203 => 'Non-Authoritative Information',
  506. 204 => 'No Content',
  507. 205 => 'Reset Content',
  508. 206 => 'Partial Content',
  509. 300 => 'Multiple Choices',
  510. 301 => 'Moved Permanently',
  511. 302 => 'Found',
  512. 303 => 'See Other',
  513. 304 => 'Not Modified',
  514. 305 => 'Use Proxy',
  515. 307 => 'Temporary Redirect',
  516. 400 => 'Bad Request',
  517. 401 => 'Unauthorized',
  518. 402 => 'Payment Required',
  519. 403 => 'Forbidden',
  520. 404 => 'Not Found',
  521. 405 => 'Method Not Allowed',
  522. 406 => 'Not Acceptable',
  523. 407 => 'Proxy Authentication Required',
  524. 408 => 'Request Time-out',
  525. 409 => 'Conflict',
  526. 410 => 'Gone',
  527. 411 => 'Length Required',
  528. 412 => 'Precondition Failed',
  529. 413 => 'Request Entity Too Large',
  530. 414 => 'Request-URI Too Large',
  531. 415 => 'Unsupported Media Type',
  532. 416 => 'Requested range not satisfiable',
  533. 417 => 'Expectation Failed',
  534. 500 => 'Internal Server Error',
  535. 501 => 'Not Implemented',
  536. 502 => 'Bad Gateway',
  537. 503 => 'Service Unavailable',
  538. 504 => 'Gateway Time-out',
  539. 505 => 'HTTP Version not supported',
  540. );
  541. // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  542. // base code in their class.
  543. if (!isset($responses[$code])) {
  544. $code = floor($code / 100) * 100;
  545. }
  546. $result->code = $code;
  547. switch ($code) {
  548. case 200: // OK
  549. case 304: // Not modified
  550. break;
  551. case 301: // Moved permanently
  552. case 302: // Moved temporarily
  553. case 307: // Moved temporarily
  554. $location = $result->headers['location'];
  555. $options['timeout'] -= timer_read(__FUNCTION__) / 1000;
  556. if ($options['timeout'] <= 0) {
  557. $result->code = HTTP_REQUEST_TIMEOUT;
  558. $result->error = 'request timed out';
  559. }
  560. elseif ($options['max_redirects']) {
  561. // Redirect to the new location.
  562. $options['max_redirects']--;
  563. $result = l10n_update_http_request($location, $options);
  564. $result->redirect_code = $code;
  565. }
  566. if (!isset($result->redirect_url)) {
  567. $result->redirect_url = $location;
  568. }
  569. break;
  570. default:
  571. $result->error = $status_message;
  572. }
  573. return $result;
  574. }
  575. /**
  576. * Build abstract translation source, to be mapped to a file or a download.
  577. *
  578. * @param $project
  579. * Project object containing data to be inserted in the template.
  580. * @param $template
  581. * String containing place holders. Available place holders:
  582. * - '%project': Project name.
  583. * - '%release': Poject version.
  584. * - '%core': Project core version.
  585. * - '%language': Language code.
  586. * - '%filename': Project file name.
  587. * @return string
  588. * String with replaced place holders.
  589. */
  590. function l10n_update_build_string($project, $template) {
  591. $variables = array(
  592. '%project' => $project->name,
  593. '%release' => $project->version,
  594. '%core' => $project->core,
  595. '%language' => isset($project->language) ? $project->language : '%language',
  596. '%filename' => isset($project->filename) ? $project->filename : '%filename',
  597. );
  598. return strtr($template, $variables);
  599. }