l10n_update.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 ($result && $result->code == '200') {
  243. $result->updated = isset($result->headers['last-modified']) ? strtotime($result->headers['last-modified']) : 0;
  244. }
  245. return $result;
  246. }
  247. /**
  248. * Perform an HTTP request.
  249. *
  250. * We cannot use drupal_http_request() at install, see http://drupal.org/node/527484
  251. *
  252. * This is a flexible and powerful HTTP client implementation. Correctly
  253. * handles GET, POST, PUT or any other HTTP requests. Handles redirects.
  254. *
  255. * @param $url
  256. * A string containing a fully qualified URI.
  257. * @param array $options
  258. * (optional) An array that can have one or more of the following elements:
  259. * - headers: An array containing request headers to send as name/value pairs.
  260. * - method: A string containing the request method. Defaults to 'GET'.
  261. * - data: A string containing the request body, formatted as
  262. * 'param=value&param=value&...'. Defaults to NULL.
  263. * - max_redirects: An integer representing how many times a redirect
  264. * may be followed. Defaults to 3.
  265. * - timeout: A float representing the maximum number of seconds the function
  266. * call may take. The default is 30 seconds. If a timeout occurs, the error
  267. * code is set to the HTTP_REQUEST_TIMEOUT constant.
  268. * - context: A context resource created with stream_context_create().
  269. *
  270. * @return object
  271. * An object that can have one or more of the following components:
  272. * - request: A string containing the request body that was sent.
  273. * - code: An integer containing the response status code, or the error code
  274. * if an error occurred.
  275. * - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
  276. * - status_message: The status message from the response, if a response was
  277. * received.
  278. * - redirect_code: If redirected, an integer containing the initial response
  279. * status code.
  280. * - redirect_url: If redirected, a string containing the redirection location.
  281. * - error: If an error occurred, the error message. Otherwise not set.
  282. * - headers: An array containing the response headers as name/value pairs.
  283. * HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
  284. * easy access the array keys are returned in lower case.
  285. * - data: A string containing the response body that was received.
  286. */
  287. function l10n_update_http_request($url, array $options = array()) {
  288. $result = new stdClass();
  289. // Parse the URL and make sure we can handle the schema.
  290. $uri = @parse_url($url);
  291. if ($uri == FALSE) {
  292. $result->error = 'unable to parse URL';
  293. $result->code = -1001;
  294. return $result;
  295. }
  296. if (!isset($uri['scheme'])) {
  297. $result->error = 'missing schema';
  298. $result->code = -1002;
  299. return $result;
  300. }
  301. timer_start(__FUNCTION__);
  302. // Merge the default options.
  303. $options += array(
  304. 'headers' => array(),
  305. 'method' => 'GET',
  306. 'data' => NULL,
  307. 'max_redirects' => 3,
  308. 'timeout' => 30.0,
  309. 'context' => NULL,
  310. );
  311. // stream_socket_client() requires timeout to be a float.
  312. $options['timeout'] = (float) $options['timeout'];
  313. switch ($uri['scheme']) {
  314. case 'http':
  315. case 'feed':
  316. $port = isset($uri['port']) ? $uri['port'] : 80;
  317. $socket = 'tcp://' . $uri['host'] . ':' . $port;
  318. // RFC 2616: "non-standard ports MUST, default ports MAY be included".
  319. // We don't add the standard port to prevent from breaking rewrite rules
  320. // checking the host that do not take into account the port number.
  321. $options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : '');
  322. break;
  323. case 'https':
  324. // Note: Only works when PHP is compiled with OpenSSL support.
  325. $port = isset($uri['port']) ? $uri['port'] : 443;
  326. $socket = 'ssl://' . $uri['host'] . ':' . $port;
  327. $options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');
  328. break;
  329. default:
  330. $result->error = 'invalid schema ' . $uri['scheme'];
  331. $result->code = -1003;
  332. return $result;
  333. }
  334. if (empty($options['context'])) {
  335. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
  336. }
  337. else {
  338. // Create a stream with context. Allows verification of a SSL certificate.
  339. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
  340. }
  341. // Make sure the socket opened properly.
  342. if (!$fp) {
  343. // When a network error occurs, we use a negative number so it does not
  344. // clash with the HTTP status codes.
  345. $result->code = -$errno;
  346. $result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket));
  347. // Mark that this request failed. This will trigger a check of the web
  348. // server's ability to make outgoing HTTP requests the next time that
  349. // requirements checking is performed.
  350. // See system_requirements()
  351. // variable_set('drupal_http_request_fails', TRUE);
  352. return $result;
  353. }
  354. // Construct the path to act on.
  355. $path = isset($uri['path']) ? $uri['path'] : '/';
  356. if (isset($uri['query'])) {
  357. $path .= '?' . $uri['query'];
  358. }
  359. // Merge the default headers.
  360. $options['headers'] += array(
  361. 'User-Agent' => 'Drupal (+http://drupal.org/)',
  362. );
  363. // Only add Content-Length if we actually have any content or if it is a POST
  364. // or PUT request. Some non-standard servers get confused by Content-Length in
  365. // at least HEAD/GET requests, and Squid always requires Content-Length in
  366. // POST/PUT requests.
  367. $content_length = strlen($options['data']);
  368. if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
  369. $options['headers']['Content-Length'] = $content_length;
  370. }
  371. // If the server URL has a user then attempt to use basic authentication.
  372. if (isset($uri['user'])) {
  373. $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
  374. }
  375. // If the database prefix is being used by SimpleTest to run the tests in a copied
  376. // database then set the user-agent header to the database prefix so that any
  377. // calls to other Drupal pages will run the SimpleTest prefixed database. The
  378. // user-agent is used to ensure that multiple testing sessions running at the
  379. // same time won't interfere with each other as they would if the database
  380. // prefix were stored statically in a file or database variable.
  381. $test_info = &$GLOBALS['drupal_test_info'];
  382. if (!empty($test_info['test_run_id'])) {
  383. $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
  384. }
  385. $request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
  386. foreach ($options['headers'] as $name => $value) {
  387. $request .= $name . ': ' . trim($value) . "\r\n";
  388. }
  389. $request .= "\r\n" . $options['data'];
  390. $result->request = $request;
  391. // Calculate how much time is left of the original timeout value.
  392. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  393. if ($timeout > 0) {
  394. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  395. fwrite($fp, $request);
  396. }
  397. // Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
  398. // and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
  399. // instead must invoke stream_get_meta_data() each iteration.
  400. $info = stream_get_meta_data($fp);
  401. $alive = !$info['eof'] && !$info['timed_out'];
  402. $response = '';
  403. while ($alive) {
  404. // Calculate how much time is left of the original timeout value.
  405. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  406. if ($timeout <= 0) {
  407. $info['timed_out'] = TRUE;
  408. break;
  409. }
  410. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  411. $chunk = fread($fp, 1024);
  412. $response .= $chunk;
  413. $info = stream_get_meta_data($fp);
  414. $alive = !$info['eof'] && !$info['timed_out'] && $chunk;
  415. }
  416. fclose($fp);
  417. if ($info['timed_out']) {
  418. $result->code = HTTP_REQUEST_TIMEOUT;
  419. $result->error = 'request timed out';
  420. return $result;
  421. }
  422. // Parse response headers from the response body.
  423. list($response, $result->data) = explode("\r\n\r\n", $response, 2);
  424. $response = preg_split("/\r\n|\n|\r/", $response);
  425. // Parse the response status line.
  426. list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
  427. $result->protocol = $protocol;
  428. $result->status_message = $status_message;
  429. $result->headers = array();
  430. // Parse the response headers.
  431. while ($line = trim(array_shift($response))) {
  432. list($name, $value) = explode(':', $line, 2);
  433. $name = strtolower($name);
  434. if (isset($result->headers[$name]) && $name == 'set-cookie') {
  435. // RFC 2109: the Set-Cookie response header comprises the token Set-
  436. // Cookie:, followed by a comma-separated list of one or more cookies.
  437. $result->headers[$name] .= ',' . trim($value);
  438. }
  439. else {
  440. $result->headers[$name] = trim($value);
  441. }
  442. }
  443. $responses = array(
  444. 100 => 'Continue',
  445. 101 => 'Switching Protocols',
  446. 200 => 'OK',
  447. 201 => 'Created',
  448. 202 => 'Accepted',
  449. 203 => 'Non-Authoritative Information',
  450. 204 => 'No Content',
  451. 205 => 'Reset Content',
  452. 206 => 'Partial Content',
  453. 300 => 'Multiple Choices',
  454. 301 => 'Moved Permanently',
  455. 302 => 'Found',
  456. 303 => 'See Other',
  457. 304 => 'Not Modified',
  458. 305 => 'Use Proxy',
  459. 307 => 'Temporary Redirect',
  460. 400 => 'Bad Request',
  461. 401 => 'Unauthorized',
  462. 402 => 'Payment Required',
  463. 403 => 'Forbidden',
  464. 404 => 'Not Found',
  465. 405 => 'Method Not Allowed',
  466. 406 => 'Not Acceptable',
  467. 407 => 'Proxy Authentication Required',
  468. 408 => 'Request Time-out',
  469. 409 => 'Conflict',
  470. 410 => 'Gone',
  471. 411 => 'Length Required',
  472. 412 => 'Precondition Failed',
  473. 413 => 'Request Entity Too Large',
  474. 414 => 'Request-URI Too Large',
  475. 415 => 'Unsupported Media Type',
  476. 416 => 'Requested range not satisfiable',
  477. 417 => 'Expectation Failed',
  478. 500 => 'Internal Server Error',
  479. 501 => 'Not Implemented',
  480. 502 => 'Bad Gateway',
  481. 503 => 'Service Unavailable',
  482. 504 => 'Gateway Time-out',
  483. 505 => 'HTTP Version not supported',
  484. );
  485. // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  486. // base code in their class.
  487. if (!isset($responses[$code])) {
  488. $code = floor($code / 100) * 100;
  489. }
  490. $result->code = $code;
  491. switch ($code) {
  492. case 200: // OK
  493. case 304: // Not modified
  494. break;
  495. case 301: // Moved permanently
  496. case 302: // Moved temporarily
  497. case 307: // Moved temporarily
  498. $location = $result->headers['location'];
  499. $options['timeout'] -= timer_read(__FUNCTION__) / 1000;
  500. if ($options['timeout'] <= 0) {
  501. $result->code = HTTP_REQUEST_TIMEOUT;
  502. $result->error = 'request timed out';
  503. }
  504. elseif ($options['max_redirects']) {
  505. // Redirect to the new location.
  506. $options['max_redirects']--;
  507. $result = l10n_update_http_request($location, $options);
  508. $result->redirect_code = $code;
  509. }
  510. $result->redirect_url = $location;
  511. break;
  512. default:
  513. $result->error = $status_message;
  514. }
  515. return $result;
  516. }
  517. /**
  518. * Build abstract translation source, to be mapped to a file or a download.
  519. *
  520. * @param $project
  521. * Project object containing data to be inserted in the template.
  522. * @param $template
  523. * String containing place holders. Available place holders:
  524. * - '%project': Project name.
  525. * - '%release': Poject version.
  526. * - '%core': Project core version.
  527. * - '%language': Language code.
  528. * - '%filename': Project file name.
  529. * @return string
  530. * String with replaced place holders.
  531. */
  532. function l10n_update_build_string($project, $template) {
  533. $variables = array(
  534. '%project' => $project->name,
  535. '%release' => $project->version,
  536. '%core' => $project->core,
  537. '%language' => isset($project->language) ? $project->language : '%language',
  538. '%filename' => isset($project->filename) ? $project->filename : '%filename',
  539. );
  540. return strtr($template, $variables);
  541. }