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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
<?php
/**
* EmbedVideo
* EmbedVideo OEmbed Class
*
* @license MIT
* @package EmbedVideo
* @link https://www.mediawiki.org/wiki/Extension:EmbedVideo
*
**/
namespace EmbedVideo;
class OEmbed {
/**
* Data from oEmbed service.
*
* @var array
*/
private $data = [];
/**
* Main Constructor
*
* @access private
* @param array Data return from oEmbed service.
* @return void
*/
private function __construct( $data ) {
$this->data = $data;
}
/**
* Create a new object from an oEmbed URL.
*
* @access public
* @param string Full oEmbed URL to process.
* @return mixed New OEmbed object or false on initialization failure.
*/
static public function newFromRequest( $url ) {
$data = self::curlGet( $url );
if ( $data !== false ) {
// Error suppression is required as json_decode() tosses E_WARNING in contradiction to its documentation.
$data = @json_decode( $data, true );
}
if ( !$data || !is_array( $data ) ) {
return false;
}
return new self( $data );
}
/**
* Return the HTML from the data, typically an iframe.
*
* @access public
* @return mixed String HTML or false on error.
*/
public function getHtml() {
if ( isset( $this->data['html'] ) ) {
// Remove any extra HTML besides the iframe.
$iframeStart = strpos( $this->data['html'], '<iframe' );
$iframeEnd = strpos( $this->data['html'], '</iframe>' );
if ( $iframeStart !== false ) {
// Only strip if an iframe was found.
$this->data['html'] = substr( $this->data['html'], $iframeStart, $iframeEnd + 9 );
}
return $this->data['html'];
} else {
return false;
}
}
/**
* Return the title from the data.
*
* @access public
* @return mixed String or false on error.
*/
public function getTitle() {
if ( isset( $this->data['title'] ) ) {
return $this->data['title'];
} else {
return false;
}
}
/**
* Return the author name from the data.
*
* @access public
* @return mixed String or false on error.
*/
public function getAuthorName() {
if ( isset( $this->data['author_name'] ) ) {
return $this->data['author_name'];
} else {
return false;
}
}
/**
* Return the author URL from the data.
*
* @access public
* @return mixed String or false on error.
*/
public function getAuthorUrl() {
if ( isset( $this->data['author_url'] ) ) {
return $this->data['author_url'];
} else {
return false;
}
}
/**
* Return the provider name from the data.
*
* @access public
* @return mixed String or false on error.
*/
public function getProviderName() {
if ( isset( $this->data['provider_name'] ) ) {
return $this->data['provider_name'];
} else {
return false;
}
}
/**
* Return the provider URL from the data.
*
* @access public
* @return mixed String or false on error.
*/
public function getProviderUrl() {
if ( isset( $this->data['provider_url'] ) ) {
return $this->data['provider_url'];
} else {
return false;
}
}
/**
* Return the width from the data.
*
* @access public
* @return mixed Integer or false on error.
*/
public function getWidth() {
if ( isset( $this->data['width'] ) ) {
return intval( $this->data['width'] );
} else {
return false;
}
}
/**
* Return the height from the data.
*
* @access public
* @return mixed Integer or false on error.
*/
public function getHeight() {
if ( isset( $this->data['height'] ) ) {
return intval( $this->data['height'] );
} else {
return false;
}
}
/**
* Return the thumbnail width from the data.
*
* @access public
* @return mixed Integer or false on error.
*/
public function getThumbnailWidth() {
if ( isset( $this->data['thumbnail_width'] ) ) {
return intval( $this->data['thumbnail_width'] );
} else {
return false;
}
}
/**
* Return the thumbnail height from the data.
*
* @access public
* @return mixed Integer or false on error.
*/
public function getThumbnailHeight() {
if ( isset( $this->data['thumbnail_height'] ) ) {
return intval( $this->data['thumbnail_height'] );
} else {
return false;
}
}
/**
* Perform a Curl GET request.
*
* @access private
* @param string URL
* @return mixed
*/
static private function curlGet( $location ) {
global $wgServer;
$ch = curl_init();
$timeout = 10;
$useragent = "EmbedVideo/1.0/" . $wgServer;
$dateTime = gmdate( "D, d M Y H:i:s", time() ) . " GMT";
$headers = ['Date: ' . $dateTime];
$curlOptions = [
CURLOPT_TIMEOUT => $timeout,
CURLOPT_USERAGENT => $useragent,
CURLOPT_URL => $location,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_COOKIEFILE => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'curlget',
CURLOPT_COOKIEJAR => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'curlget',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers
];
curl_setopt_array( $ch, $curlOptions );
$page = curl_exec( $ch );
$response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( $responseCode == 503 || $responseCode == 404 || $responseCode == 501 || $responseCode == 401 ) {
return false;
}
return $page;
}
}
|