forked from oscarotero/Embed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbed.php
176 lines (146 loc) · 4.68 KB
/
Embed.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Embed;
use Embed\Adapters\Adapter;
use Embed\Http\CurlDispatcher;
use Embed\Http\DispatcherInterface;
use Embed\Http\Url;
abstract class Embed
{
/**
* @var array
*/
public static $default_config = [
'custom_adapters_namespace' => null,
'min_image_width' => 1,
'min_image_height' => 1,
'choose_bigger_image' => false,
'images_blacklist' => [],
'url_blacklist' => [
'?&ns_campaign=*',
'?&ns_source=*',
'?&utm_campaign=*',
'?&utm_medium=*',
'?&utm_source=*',
],
'follow_canonical' => true,
'html' => [
'max_images' => -1,
'external_images' => false
],
'oembed' => [
'parameters' => [],
'embedly_key' => null,
'iframely_key' => null,
],
'google' => [
'key' => null,
],
'soundcloud' => [
'key' => null,
],
'facebook' => [
'key' => null,
'events_fields' => 'id,cover,description,end_time,name,owner,place,start_time,timezone',
'videos_fields' => 'id,description,embed_html',
]
];
/**
* Gets the info from an url.
*
* @param Url|string $url
* @param array|null $config
* @param DispatcherInterface|null $dispatcher
*
* @return Adapter
*/
public static function create($url, array $config = null, DispatcherInterface $dispatcher = null)
{
if (!($url instanceof Url)) {
$url = Url::create($url);
}
if ($config === null) {
$config = self::$default_config;
} else {
$config += self::$default_config;
}
if ($dispatcher === null) {
$dispatcher = new CurlDispatcher();
}
$info = self::process($url, $config, $dispatcher);
if ($info->getConfig('follow_canonical') === false) {
return $info;
}
// Repeat the process if:
// - The canonical url is different
// - No embed code has found
$from = preg_replace('|^(\w+://)|', '', rtrim((string)$info->getResponse()->getUrl(), '/'));
$to = preg_replace('|^(\w+://)|', '', rtrim($info->url, '/'));
if ($from !== $to && empty($info->code)) {
//accept new result if valid
try {
return self::process(Url::create($info->url), $config, $dispatcher);
} catch (\Exception $e) {
return $info;
}
}
return $info;
}
/**
* Process the url.
*
* @param Url $url
* @param array $config
* @param DispatcherInterface $dispatcher
*
* @throws Exceptions\InvalidUrlException If the urls is not valid
*
* @return Adapter
*/
private static function process(Url $url, array $config, DispatcherInterface $dispatcher)
{
$response = $dispatcher->dispatch($url);
//If is a file use File Adapter
$adapter = self::getClass('File', $config);
if ($adapter::check($response)) {
return new $adapter($response, $config, $dispatcher);
}
//Search the adapter using the domain
$adapter = self::getClass($response->getUrl()->getClassNameForDomain(), $config);
if (class_exists($adapter) && $adapter::check($response)) {
return new $adapter($response, $config, $dispatcher);
}
//Use the default webpage adapter
$adapter = self::getClass('Webpage', $config);
if ($adapter::check($response)) {
return new $adapter($response, $config, $dispatcher);
}
if ($response->getError() === null) {
$exception = new Exceptions\InvalidUrlException(sprintf("Invalid url '%s' (Status code %s)", (string) $url, $response->getStatusCode()));
} else {
$exception = new Exceptions\InvalidUrlException($response->getError());
}
$exception->setResponse($response);
throw $exception;
}
/**
* Returns a class name using the custom_adapters_namespace
*
* @param string $name
* @param array $config
*
* @return string
*/
private static function getClass($name, array $config)
{
if (!empty($config['custom_adapters_namespace'])) {
$namespaces = (array) $config['custom_adapters_namespace'];
foreach ($namespaces as $namespace) {
$class = $namespace.$name;
if (class_exists($class)) {
return $class;
}
}
}
return 'Embed\\Adapters\\'.$name;
}
}