RemoteDocument.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
15 
16 /**
17  * RemoteDocument represents a remote file retrieved via cURL.
18  *
19  * @author ingo herwig <ingo@wemove.com>
20  */
22 
23  private $curlOptions = [];
24 
25  /**
26  * Constructor
27  * @param $curlOptions
28  * @param $isDownload
29  */
30  public function __construct($curlOptions, $isDownload) {
31  if (!function_exists('curl_init')) {
32  throw new IOException("cURL is required to use RemoteDocument.");
33  }
34  if (!isset($curlOptions[CURLOPT_URL])) {
35  throw new IOException("No url set in cURL options.");
36  }
37  $this->curlOptions = $curlOptions;
38 
39  // get filename from url
40  $filename = basename(parse_url($curlOptions[CURLOPT_URL], PHP_URL_PATH));
41  $mimeType = FileUtil::getMimeType($filename);
42  parent::__construct($mimeType, $isDownload, $filename);
43  }
44 
45  /**
46  * @see ResponseDocument::getContent()
47  */
48  public function getContent() {
49  return null;
50  }
51 
52  /**
53  * @see ResponseDocument::output()
54  */
55  public function output() {
56  $ch = curl_init($this->curlOptions[CURLOPT_URL]);
57  foreach ($this->curlOptions as $key => $value) {
58  curl_setopt($ch, $key, $value);
59  }
60  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
61  curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this, 'write']);
62  curl_exec($ch);
63  $error = curl_error($ch);
64  curl_close($ch);
65  if ($error) {
66  throw new IOException("Error retrieving remote document: "+$error);
67  }
68  }
69 
70  private function write($ch, $data) {
71  echo $data;
72  return strlen($data);
73  }
74 }
75 ?>
AbstractFormat is used as base class for specialized documents.
__construct($curlOptions, $isDownload)
Constructor.
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
RemoteDocument represents a remote file retrieved via cURL.
IOException signals an exception in i/o operations.
Definition: IOException.php:18
static getMimeType($file)
Get the mime type of the given file.
Definition: FileUtil.php:66