- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteContentFetcher.php
More file actions
126 lines (95 loc) · 2.79 KB
/
RemoteContentFetcher.php
File metadata and controls
126 lines (95 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Mpdf;
use Mpdf\Utils\Arrays;
use Psr\Log\LoggerInterface;
use Mpdf\Log\Context as LogContext;
class RemoteContentFetcher implements \Psr\Log\LoggerAwareInterface
{
/**
* @var \Mpdf\Mpdf
*/
private $mpdf;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
public function __construct(Mpdf $mpdf, LoggerInterface $logger)
{
$this->mpdf = $mpdf;
$this->logger = $logger;
}
public function getFileContentsByCurl($url)
{
$this->logger->debug(sprintf('Fetching (cURL) content of remote URL "%s"', $url), ['context' => LogContext::REMOTE_CONTENT]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1'); // mPDF 5.7.4
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->mpdf->curlTimeout);
if ($this->mpdf->curlFollowLocation) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
if ($this->mpdf->curlAllowUnsafeSslRequests) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
if (is_file($this->mpdf->curlCaCertificate)) {
curl_setopt($ch, CURLOPT_CAINFO, $this->mpdf->curlCaCertificate);
}
$data = curl_exec($ch);
if (curl_error($ch)) {
$this->logger->error(sprintf('cURL error: "%s"', curl_error($ch)), ['context' => LogContext::REMOTE_CONTENT]);
}
curl_close($ch);
return $data;
}
public function getFileContentsBySocket($url)
{
$this->logger->debug(sprintf('Fetching (socket) content of remote URL "%s"', $url), ['context' => LogContext::REMOTE_CONTENT]);
// mPDF 5.7.3
$timeout = 1;
$p = parse_url($url);
$file = Arrays::get($p, 'path', '');
$scheme = Arrays::get($p, 'scheme', '');
$port = Arrays::get($p, 'port', 80);
$prefix = '';
if ($scheme === 'https') {
$prefix = 'ssl://';
$port = Arrays::get($p, 'port', 443);
}
$query = Arrays::get($p, 'query', null);
if ($query) {
$file .= '?' . $query;
}
if (!($fh = @fsockopen($prefix . $p['host'], $port, $errno, $errstr, $timeout))) {
$this->logger->error(sprintf('Socket error "%s": "%s"', $errno, $errstr), ['context' => LogContext::REMOTE_CONTENT]);
return false;
}
$getstring = 'GET ' . $file . " HTTP/1.0 \r\n" .
'Host: ' . $p['host'] . " \r\n" .
"Connection: close\r\n\r\n";
fwrite($fh, $getstring);
// Get rid of HTTP header
$s = fgets($fh, 1024);
if (!$s) {
return false;
}
while (!feof($fh)) {
$s = fgets($fh, 1024);
if ($s === "\r\n") {
break;
}
}
$data = '';
while (!feof($fh)) {
$data .= fgets($fh, 1024);
}
fclose($fh);
return $data;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}