You are here

function l10n_update_download_file in Localization update 6

Same name and namespace in other branches
  1. 7 l10n_update.inc \l10n_update_download_file()

Get remote file and download it to a temporary path.

Parameters

$download_url: URL of remote file.

$destination: URL of local destination file. By default the download will be stored in a temporary file.

2 calls to l10n_update_download_file()
l10n_update_download_import in ./l10n_update.inc
Download and import remote translation file.
l10n_update_source_download in ./l10n_update.check.inc
Download source file from remote server.

File

./l10n_update.inc, line 98

Code

function l10n_update_download_file($download_url, $destination = NULL) {
  $t = get_t();
  $variables['%download_link'] = $download_url;

  // Create temporary file or use specified file destination.
  // Temporary files get a 'translation-' file name prefix.
  $file = $destination ? $destination : tempnam(file_directory_temp(), 'translation-');
  if ($file) {
    $variables['%tmpfile'] = $file;

    // We download and store the file (in one if statement! Isnt't that neat ;) ).
    // @todo remove the timeout once we use the batch API to download the files.
    if (($contents = drupal_http_request($download_url, array(
      'timeout' => 90,
    ))) && $contents->code == 200 && ($file_result = file_put_contents($file, $contents->data))) {
      watchdog('l10n_update', 'Successfully downloaded %download_link to %tmpfile', $variables);
      return $file;
    }
    else {
      if (isset($contents->error)) {
        watchdog('l10n_update', 'An error occured during the download operation: %error.', array(
          '%error' => $contents->error,
        ), WATCHDOG_ERROR);
      }
      elseif (isset($contents->code) && $contents->code != 200) {
        watchdog('l10n_update', 'An error occured during the download operation: HTTP status code %code.', array(
          '%code' => $contents->code,
        ), WATCHDOG_ERROR);
      }
      if (isset($file_result)) {

        // file_put_contents() was called but returned FALSE.
        watchdog('l10n_update', 'Unable to save %download_link file to %tmpfile.', $variables, WATCHDOG_ERROR);
      }
    }
  }
  else {
    $variables['%tmpdir'] = file_directory_temp();
    watchdog('l10n_update', 'Error creating temporary file for download in %tmpdir. Remote file is %download_link.', $variables, WATCHDOG_ERROR);
  }
}