You are here

private static function URLDownloader::downloadUrl in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Downloads URL.

1 call to URLDownloader::downloadUrl()
URLDownloader::download in src/Flmngr/FileUploaderServer/lib/file/URLDownloader.php
Downlaods URL to directory.

File

src/Flmngr/FileUploaderServer/lib/file/URLDownloader.php, line 36

Class

URLDownloader
URL downloader.

Namespace

Drupal\n1ed\Flmngr\FileUploaderServer\lib\file

Code

private static function downloadUrl($url, $dir) {
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
  curl_setopt($curl, CURLOPT_FAILONERROR, FALSE);

  // For not redirecting response to stdout.
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

  // Allow redirects.
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
  $headers = [];
  curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($curl_, $header) use (&$headers) {
    $len = strlen($header);
    $header = explode(':', $header, 2);

    // Ignore invalid headers.
    if (count($header) < 2) {
      return $len;
    }
    $name = strtolower(trim($header[0]));
    if (!array_key_exists($name, $headers)) {
      $headers[$name] = [
        trim($header[1]),
      ];
    }
    else {
      $headers[$name][] = trim($header[1]);
    }
    return $len;
  });
  $result = new DownloadedURL();
  $fileName = "";
  $response = curl_exec($curl);
  if ($response !== FALSE) {
    $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($responseCode == '200') {
      if (array_key_exists("Content-Type", $headers)) {
        $result->contentType = $headers["Content-Type"];
      }
      if (array_key_exists("Content-Length", $headers)) {
        $result->contentLength = $headers["Content-Length"];
      }
      if (array_key_exists("Content-Disposition", $headers)) {
        $contentDisposition = $headers["Content-Disposition"];
        $index = strpos($contentDisposition, "filename=");
        if ($index !== FALSE) {
          $fileName = substr($contentDisposition, $index + 10);
        }
      }
      if (strlen(trim($fileName)) == 0) {
        $index = strrpos($url, "/");
        $fileName = substr($url, $index + 1);
        $index = strpos($fileName, "?");
        if ($index !== FALSE) {
          $fileName = substr($fileName, 0, $index);
        }
      }
      if (strlen(trim($fileName)) === 0) {
        $fileName = "url";
      }
      $fileName = Utils::fixFileName($fileName);
      $fileName = Utils::getFreeFileName($dir, $fileName, FALSE);
    }
    else {
      throw new MessageException(Message::createMessage(Message::DOWNLOAD_FAIL_CODE, $responseCode));
    }
  }
  else {
    throw new MessageException(Message::createMessage(Message::DOWNLOAD_FAIL_IO, curl_error($curl)));
  }
  $saveFilePath = $dir . DIRECTORY_SEPARATOR . $fileName;
  file_put_contents($saveFilePath, $response);
  curl_close($curl);
  $result->fileName = $fileName;
  return $result;
}