-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.video.php
390 lines (300 loc) · 11.4 KB
/
function.video.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
#
# Plugin to include videos uploaded to the most popular video sharing portals.
#
# Version: 1.0
# Author: Reto Hugi (http://hugi.to/blog/)
# License: GPL v3 (http://www.gnu.org/licenses/gpl.html)
#
# For more information see the help sections at the end of this file.
# Parts of the Plugin (namely the url parsers for some of the portals) are ported
# from a js function for TinyMCE available in LifeType (http://www.lifetype.org).
#
# Changelog:
# see below
function smarty_cms_function_Video($params, &$smarty) {
$vid = new Video();
if(isset($params['url'])) $vid->setVideoUrl($params['url']);
if(isset($params['width'])) $vid->setWidth((int)$params['width']);
if(isset($params['height'])) $vid->setHeight((int)$params['height']);
return $vid->getHtml();
}
class Video {
var $videoUrl; // permalink of the video on the service site
var $width; // width of the included video
var $height; // height of the included video
var $color1; // color setting for youtube (not implemented yet)
var $color2; // dito
var $availableServices;
function __construct() {
// Set some default values
$this->width = 480;
$this->height = 385;
$this->availableServices = array('youtube.com',
'youtu.be',
'video.google.com',
'5min.com',
'dailymotion.com',
'vimeo.com');
}
function getHtml() {
// validate the user parameters
if(!$this->validateParams()) {
return '<p>Invalid Parameters set for Video Plugin</p>';
}
$output = '';
$service = $this->getService($this->videoUrl);
// which service are we using?
switch ($service) {
case 'youtube.com':
case 'youtu.be':
$output = $this->getYoutube();
break;
case 'video.google.com':
$output = $this->getGoogleVideo();
break;
case '5min.com':
$output = $this->get5min();
break;
case 'dailymotion.com':
$output = $this->getDailymotion();
break;
case 'vimeo.com':
$output = $this->getVimeo();
break;
default:
$output = $service;
}
return $output;
}
function getService($url) {
foreach($this->availableServices as $value) {
$pos = strpos($url, $value);
//this is a lazy check to see if it's a "near" valid service url.
//It's not a validation check! Real validation should be done within
// the getters
if ($pos !== false) {
$service = $value;
break;
}
}
if ($service != '') {
return $service;
}
else {
return "<p>No valid service defined. Check help for available services.";
}
}
function getYoutube() {
$out = '';
$params = array("iframe","attributes");
$url = '';
$norel = 'rel=0'; //removes the "related video" display at the end of a video
$nocookies = true; // if set to true, the extended privacy settings
// (no cookies if video is not played) will take effect.
if ($nocookies === true) {
$domain = 'www.youtube-nocookie.com';
}
else {
$domain = 'www.youtube.com';
}
// check if this is a URL pointing to a youtube link
if ( preg_match("/http:\/\/.{2,3}\.youtube\.com\//i", $this->videoUrl)
|| preg_match("/http:\/\/youtu\.be\//i", $this->videoUrl) ) {
// try parsing long url
if( preg_match("/http:\/\/.{2,3}\.youtube\.com\/watch\?v=([0-9a-zA-z]*).*/i",$this->videoUrl,$matches) ) {
$videoId = $matches[1];
}
// try parsing short url
if( preg_match("/http:\/\/youtu\.be\/([0-9a-zA-z]*).*/i",$this->videoUrl,$matches) ) {
$videoId = $matches[1];
}
$url = 'http://'.$domain.'/embed/' . $videoId ."?". $norel;
$params = array("iframe" => array("width" => $this->width,
"height"=> $this->height,
"url" => $url),
"attributes" => array("frameborder" => 'frameborder="0"',
"fullscreen" => 'allowfullscreen')
);
$out = $this->getVideoHtml($params);
}
else {
$out = "<p>No valid Youtube link</p>";
}
return $out;
}
function getGoogleVideo() {
$out = '';
$params = array("object","param");
$url = '';
// check if it's a link to a video page or a link to the video player
if( substr($this->videoUrl, 0, 40 ) == "http://video.google.com/videoplay?docid=" ) {
if( preg_match("/http:\/\/video\.google\.com\/videoplay\?docid=([\-0-9a-zA-z_]*).*/i",$this->videoUrl,$matches) ) {
$videoId = $matches[1];
}
$url = "http://video.google.com/googleplayer.swf?docId=" . $videoId;
$params = array("object" => array("width" => $this->width,
"height"=> $this->height,
"data" => $url),
"param" => array("movie" => $url)
);
$out = $this->getVideoHtml($params);
}
else {
$out = "<p>No valid Google Video link</p>";
}
return $out;
}
function get5min() {
$out = '';
$params = array("object","param");
$url = '';
if( preg_match("/http:\/\/www\.5min\.com\//i", $this->videoUrl) ) {
if( preg_match("/.*[^-]-([0-9]*)$/i",$this->videoUrl,$matches) ) {
$videoId = $matches[1];
}
$url = "http://embed.5min.com/" . $videoId . "/";
$params = array("object" => array("width" => $this->width,
"height"=> $this->height,
"data" => $url),
"param" => array("movie" => $url)
);
$out = $this->getVideoHtml($params);
}
else {
$out = "<p>No valid 5min.com link</p>";
}
return $out;
}
function getDailymotion() {
$out = '';
$params = array("object","param");
$url = '';
if( preg_match("/http:\/\/www\.dailymotion\.com\//i", $this->videoUrl) ) {
if( preg_match("/video\/([0-9a-zA-z]*)_/i",$this->videoUrl,$matches) ) {
$videoId = $matches[1];
}
$url = "http://www.dailymotion.com/swf/" . $videoId . "&related=0";
$params = array("object" => array("width" => $this->width,
"height"=> $this->height,
"data" => $url),
"param" => array("movie" => $url)
);
$out = $this->getVideoHtml($params);
}
else {
$out = "<p>No valid dailymotion.com link</p>";
}
return $out;
}
function getVimeo() {
$out = '';
$params = array("iframe","attributes");
$url = '';
if( preg_match("/http:\/\/(www\.)?vimeo\.com\//i", $this->videoUrl) ) {
if( preg_match("/vimeo\.com\/([0-9]*)/i",$this->videoUrl,$matches) ) {
$videoId = $matches[1];
}
$url = 'http://player.vimeo.com/video/'
. $videoId
. '?title=0&byline=0&portrait=0&color=ffffff';
$params = array("iframe" => array("width" => $this->width,
"height"=> $this->height,
"url" => $url),
"attributes" => array("frameborder" => 'frameborder="0"',
"webkitfullscreen" => 'webkitAllowFullScreen',
"fullscreen" => 'allowFullScreen')
);
$out = $this->getVideoHtml($params);
}
else {
$out = "<p>No valid vimeo.com link</p>";
}
return $out;
}
function getVideoHtml(&$params) {
$html = null;
if ($params['object'] != null) {
$html = '<object type="application/x-shockwave-flash" ';
// add height and width
$html .= 'style="width: '.$params['object']['width'].'px; height: '.$params['object']['height'].'px;" ';
// add Data
$html .= 'data="'.$params['object']['data'].'" ';
$html .= '>';
// add Params
foreach($params['param'] as $name => $value) {
$html .= '<param name="'.$name.'" value="'.$value.'" />';
}
$html .= '</object>';
}
elseif ($params['iframe'] != null) {
$html = '<iframe src="'.$params['iframe']['url'].'" ';
// add height and width
$html .= 'width="'.$params['iframe']['width'].'" '
.'height="'.$params['iframe']['height'].'" ';
// add Params
foreach($params['attributes'] as $name => $value) {
$html .= $value.' ';
}
$html .= '></iframe>';
}
return $html;
}
function isValidUrl($url) {
if ( preg_match("/^(http:\/\/|https:\/\/)(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+[a-z]{2,6}/i", $url) )
return true;
else
return false;
}
function validateParams() {
if ($this->isValidUrl($this->videoUrl) == false ||
is_int($this->width) == false ||
is_int($this->height) == false) {
return false;
}
return true;
}
function setVideoUrl($var) {
$this->videoUrl = $var;
}
function setService($var) {
$this->service = $var;
}
function setWidth($var) {
$this->width = (int)$var;
}
function setHeight($var) {
$this->height = (int)$var;
}
}
function smarty_cms_help_function_Video() {
echo <<<EOF
<p>
Plugin to include videos uploaded to the most popular video sharing
portals.<br/>
Suported services: youtube.com, video.google.com, 5min.com,
dailymotion.com, vimeo.com.<br/>
Usage:<br/>
<code>{video url="<Url-of-Detail-Page"}</code>
</p>
<h2>Options</h2>
<ul>
<li><strong>height</strong>: sets the height of the video</li>
<li><strong>width</strong>: sets the width of the video</li>
</ul>
EOF;
}
function smarty_cms_about_function_Video() {
echo <<<EOF
<p>Author: <a href="http://hugi.to">Reto Hugi</a></p>
<p>Version: <strong>1.0</strong></p>
<p>
Change History:<br/>
<strong>Version 1.0</strong> - added youtu.be (Youtube short url) support<br/>
<strong>Version 0.3</strong> - Fixed fullscreen Feature for Vimeo and Youtube<br/>
<strong>Version 0.2</strong> - removed related videos display for youtube<br/>
<strong>Version 0.1</strong> - First release as a Plugin (Tag)<br/>
</p>
EOF;
}
?>