-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
UrlOembed.php
220 lines (191 loc) · 5.62 KB
/
UrlOembed.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
namespace humhub\models;
use Yii;
use humhub\models\Setting;
/**
* This is the model class for table "url_oembed".
*
* @property string $url
* @property string $preview
*/
class UrlOembed extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'url_oembed';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['url', 'preview'], 'required'],
[['preview'], 'string'],
[['url'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'url' => 'Url',
'preview' => 'Preview',
];
}
/**
* Returns OEmbed Code for a given URL
*
* If no oembed code is found, null is returned
*
* @param type $url
*/
public static function GetOEmbed($url)
{
$url = trim($url);
// Check if the given URL has OEmbed Support
if (UrlOembed::HasOEmbedSupport($url)) {
// Lookup Cached OEmebed Item from Datbase
$urlOembed = UrlOembed::findOne(['url'=>$url]);
if ($urlOembed !== null) {
return $urlOembed->preview;
} else {
return self::loadUrl($url);
}
}
return null;
}
/**
* Prebuilds oembeds for all urls in a given text
*
* @param type $text
*/
public static function preload($text)
{
preg_replace_callback('/http(.*?)(\s|$)/i', function($match) {
$url = $match[0];
// Already looked up?
if (UrlOembed::findOne(['url'=>$url]) !== null) {
return;
}
UrlOembed::loadUrl($url);
}, $text);
}
/**
* Loads OEmbed Data from a given URL and writes them to the database
*
* @param type $url
* @return string
*/
public static function loadUrl($url)
{
$urlOembed = new UrlOembed();
$urlOembed->url = $url;
$html = "";
if ($urlOembed->getProviderUrl() != "") {
// Build OEmbed Preview
$jsonOut = UrlOembed::fetchUrl($urlOembed->getProviderUrl());
if ($jsonOut != "") {
try {
$data = \yii\helpers\Json::decode($jsonOut);
if (isset($data['type']) && ($data['type'] === "video" || $data['type'] === 'rich' || $data['type'] === 'photo')) {
$html = "<div class='oembed_snippet' data-url='" . \yii\helpers\Html::encode($url) . "'>" . $data['html'] . "</div>";
}
} catch (\yii\base\InvalidParamException $ex) {
Yii::warning($ex->getMessage());
}
}
}
if ($html != "") {
$urlOembed->preview = $html;
$urlOembed->save();
}
return $html;
}
/**
* Returns the OEmbed API Url if exists
*/
public function getProviderUrl()
{
foreach (UrlOembed::getProviders() as $providerBaseUrl => $providerAPI) {
if (strpos($this->url, $providerBaseUrl) !== false) {
return str_replace("%url%", urlencode($this->url), $providerAPI);
}
}
return "";
}
/**
* Checks if a given URL Supports OEmbed
*
* @param type $url
* @return boolean
*/
public static function HasOEmbedSupport($url)
{
foreach (UrlOembed::getProviders() as $providerBaseUrl => $providerAPI) {
if (strpos($url, $providerBaseUrl) !== false) {
return true;
}
}
return false;
}
/**
* Fetches a given URL and returns content
*
* @param type $url
* @return type
*/
public static function fetchUrl($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
// Not available when open_basedir is set.
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if (Setting::Get('enabled', 'proxy')) {
curl_setopt($curl, CURLOPT_PROXY, Setting::Get('server', 'proxy'));
curl_setopt($curl, CURLOPT_PROXYPORT, Setting::Get('port', 'proxy'));
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
if (defined('CURLOPT_PROXYUSERNAME')) {
curl_setopt($curl, CURLOPT_PROXYUSERNAME, Setting::Get('user', 'proxy'));
}
if (defined('CURLOPT_PROXYPASSWORD')) {
curl_setopt($curl, CURLOPT_PROXYPASSWORD, Setting::Get('pass', 'proxy'));
}
if (defined('CURLOPT_NOPROXY')) {
curl_setopt($curl, CURLOPT_NOPROXY, Setting::Get('noproxy', 'proxy'));
}
}
$return = curl_exec($curl);
curl_close($curl);
return $return;
}
/**
* Returns all available OEmbed providers
*
* @return array
*/
public static function getProviders()
{
$providers = Setting::GetText('oembedProviders');
if ($providers != "") {
return \yii\helpers\Json::decode($providers);
}
return array();
}
/**
* Saves an array of available OEmbed providers
*
* @param array $providers
*/
public static function setProviders($providers)
{
Setting::SetText('oembedProviders', \yii\helpers\Json::encode($providers));
}
}