-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
Code.php
142 lines (118 loc) · 3.73 KB
/
Code.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types = 1);
namespace Embed\Detectors;
use Embed\EmbedCode;
use function Embed\html;
class Code extends Detector
{
public function detect(): ?EmbedCode
{
return $this->detectFromEmbed()
?: $this->detectFromOpenGraph()
?: $this->detectFromTwitter()
?: $this->detectFromContentType();
}
private function detectFromEmbed(): ?EmbedCode
{
$oembed = $this->extractor->getOEmbed();
$html = $oembed->html('html');
if (!$html) {
return null;
}
return new EmbedCode(
$html,
$oembed->int('width'),
$oembed->int('height')
);
}
private function detectFromOpenGraph(): ?EmbedCode
{
$metas = $this->extractor->getMetas();
$url = $metas->url('og:video:secure_url', 'og:video:url', 'og:video');
if (!$url) {
return null;
}
if (!($type = pathinfo($url->getPath(), PATHINFO_EXTENSION))) {
$type = $metas->str('og:video_type');
}
$width = $metas->int('twitter:player:width');
$height = $metas->int('twitter:player:height');
switch ($type) {
case 'swf':
case 'application/x-shockwave-flash':
return null; //Ignore flash
case 'mp4':
case 'ogg':
case 'ogv':
case 'webm':
case 'application/mp4':
case 'video/mp4':
case 'video/ogg':
case 'video/ogv':
case 'video/webm':
$code = html('video', [
'src' => $url,
'width' => $width,
'height' => $height,
]);
break;
default:
$code = html('iframe', [
'src' => $url,
'frameborder' => 0,
'width' => $width,
'height' => $height,
'allowTransparency' => 'true',
]);
}
return new EmbedCode($code, $width, $height);
}
private function detectFromTwitter(): ?EmbedCode
{
$metas = $this->extractor->getMetas();
$url = $metas->url('twitter:player');
if (!$url) {
return null;
}
$width = $metas->int('twitter:player:width');
$height = $metas->int('twitter:player:height');
$code = html('iframe', [
'src' => $url,
'frameborder' => 0,
'width' => $width,
'height' => $height,
'allowTransparency' => 'true',
]);
return new EmbedCode($code, $width, $height);
}
private function detectFromContentType()
{
if (!$this->extractor->getResponse()->hasHeader('content-type')) {
return null;
}
$contentType = $this->extractor->getResponse()->getHeader('content-type')[0];
$isBinary = !preg_match('/(text|html|json)/', strtolower($contentType));
if (!$isBinary) {
return null;
}
$url = $this->extractor->getRequest()->getUri();
if (strpos($contentType, 'video/') === 0 || $contentType === 'application/mp4') {
$code = html('video', [
'src' => $url,
'controls' => true,
]);
} elseif (strpos($contentType, 'audio/') === 0) {
$code = html('audio', [
'src' => $url,
'controls' => true,
]);
} elseif (strpos($contentType, 'image/') === 0) {
$code = html('img', [
'src' => $url,
]);
} else {
return null;
}
return new EmbedCode($code);
}
}