You are here

public function LingotekSession::download in Lingotek Translation 6

Same name and namespace in other branches
  1. 7.2 lingotek.session.inc \LingotekSession::download()
  2. 7.3 lingotek.session.inc \LingotekSession::download()
  3. 7.4 lingotek.session.inc \LingotekSession::download()

File

./lingotek.session.inc, line 133
Handles api calls, logging in and logging out of LingoTek

Class

LingotekSession

Code

public function download($api, $params) {
  $result = $this
    ->request($api, $params, NULL, FALSE);
  $tmpFile = tempnam(file_directory_temp(), "lingotek");
  $fp = fopen($tmpFile, 'w');
  fwrite($fp, $result->data);
  fclose($fp);
  $json = json_decode($result->data);
  if (isset($json->results)) {
    lingotek_error('Error downloading document', array(
      'params' => $params,
      'response' => $file_uri,
    ), 1);
    return "";
  }
  $text = "";
  $file = FALSE;

  //downloadDocument zips the file up
  if ($api == 'downloadDocument') {
    $zip = new ZipArchive();
    $zip
      ->open($tmpFile);
    $name = $zip
      ->getNameIndex(0);
    $file = $zip
      ->getStream($name);
  }
  else {
    $file = fopen($tmpFile, "r");
  }
  if ($file) {
    while (!feof($file)) {
      $text .= fread($file, 2);
    }
  }
  fclose($file);
  unlink($tmpFile);
  return $text;
}