forked from navarr/ytaudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYTAudio.php
More file actions
617 lines (544 loc) · 15.1 KB
/
YTAudio.php
File metadata and controls
617 lines (544 loc) · 15.1 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
<?php
/**
* @author Navarr Barnier <me@navarr.me>
* @license MIT
*/
class YTAudio
{
const SIZE_INVISIBLE = 0;
const SIZE_TINY = 1;
const SIZE_SMALL = 2;
const SIZE_MEDIUM = 3;
const SIZE_LARGE = 4;
const THEME_LIGHT = "light";
const THEME_DARK = "dark";
const TYPE_VIDEO = 1;
const TYPE_PLAYLIST = 2;
protected $_id = null;
protected $_https = true;
protected $_type = self::TYPE_VIDEO;
protected $_size = self::SIZE_SMALL;
protected $_source = null;
protected $_hd = false;
protected $_autoplay = false;
protected $_jsapi = false;
protected $_progressbar = false;
protected $_timecode = false;
protected $_cookies = false;
protected $_theme = self::THEME_DARK;
protected $_loop = false;
/**
* Constructor
*
* @throws YTAudioException
* @param string $source
* @param array $settings
*/
public function __construct($source, $settings = null)
{
$this->source($source);
if($settings === null) $settings = array();
// Feature array, ie: array('https','hd','autoplay')
if(in_array('https',$settings)) $this->https();
if(in_array('hd',$settings)) $this->hd();
if(in_array('autoplay',$settings)) $this->autoplay();
if(in_array('jsapi',$settings)) $this->jsAPI();
if(in_array('progressbar',$settings)) $this->progressBar();
if(in_array('timecode',$settings)) $this->timeCode();
if(in_array('cookies',$settings)) $this->cookies();
if(in_array('loop',$settings)) $this->loop();
// Associative Feature Array, ie: array('https' => true, 'hd' => false)
if(isset($settings['https'])) $this->https($settings['https']);
if(isset($settings['size'])) $this->size($settings['size']);
if(isset($settings['hd'])) $this->hd($settings['hd']);
if(isset($settings['autoplay'])) $this->autoplay($settings['autoplay']);
if(isset($settings['jsapi'])) $this->jsAPI($settings['jsapi']);
if(isset($settings['progressbar'])) $this->progressBar($settings['progressbar']);
if(isset($settings['timecode'])) $this->timeCode($settings['timecode']);
if(isset($settings['cookies'])) $this->cookies($settings['cookies']);
if(isset($settings['theme'])) $this->theme($settings['theme']);
if(isset($settings['loop'])) $this->loop($settings['loop']);
}
/**
* Factory
* Allows easy creation and daisy-chaining of a YTAudio object.
*
* @see __construct
*
* @throws YTAudioException
* @param string $source
* @param array $settings
* @return YTAudio
*/
public static function create($source, $settings = null)
{
return new self($source,$settings);
}
/**
* Set Player Video/Playlist
* Does not validate whether or not YouTube video/playlist exists.
*
* @throws YTAudioException
* @param string $source Can be a URL or just the ID
* @return YTAudio
*/
public function source($source)
{
$oldSource = $this->_source;
$this->_source = $source;
$parsedURL = parse_url($source);
// 1 thing, auto-detect based on ID
if(count($parsedURL) === 1)
{
if(substr(strtoupper($parsedURL['path']),0,2) == "PL") return $this->playlist($source);
else return $this->video($source);
}
// Else, try to detect in turn.
try
{
return $this->video($source);
}
catch(YTAudioException $e) { } // do nothing. Might be playlist.
try
{
return $this->playlist($source);
}
catch(YTAudioException $e)
{
$this->_source = $oldSource;
throw new YTAudioException("Could not detect source");
}
}
/**
* Set Player Video
* Does not validate whether or not YouTube video exists.
*
* @throws YTAudioException
* @param string $video Can be a URL or just the ID
* @return YTAudio
*/
public function video($video)
{
$oldSource = $this->_source;
$this->_source = $video;
$parsedURL = parse_url($video);
// 1 thing, assume video.
if(count($parsedURL) === 1)
{
$this->_type = self::TYPE_VIDEO;
$this->_id = $parsedURL['path'];
return $this;
}
// Youtu.be - Video
if(strtolower($parsedURL['host']) == "youtu.be")
{
$this->_type = self::TYPE_VIDEO;
$this->_id = substr($parsedURL['path'],1);
return $this;
}
// Assume its a YouTube URL
// check for /watch
if(strtolower($parsedURL['path']) == "/watch")
{
$parsedQuery = explode("&",$parsedURL['query']);
// Find v=
foreach($parsedQuery as $v)
{
if(substr(strtolower($v),0,2) == "v=")
{
$this->_type = self::TYPE_VIDEO;
$this->_id = substr($v,2);
return $this;
}
}
}
// check for /v/
if(substr(strtolower($parsedURL['path']),0,3) == "/v/")
{
$this->_type = self::TYPE_VIDEO;
$this->_id = substr($parsedURL['path'],0,3);
return $this;
}
$this->_source = $oldSource;
throw new YTAudioException("Could not identify video");
}
/**
* Set Player Playlist
* Does not validate whether or not YouTube playlist exists.
*
* @throws YTAudioException
* @param string $playlist Can be a URL or just the ID
* @return YTAudio
*/
public function playlist($playlist)
{
$oldSource = $this->_source;
$this->_source = $playlist;
$parsedURL = parse_url($playlist);
// 1 thing, assume playlist.
if(count($parsedURL) === 1)
{
$this->_type = self::TYPE_PLAYLIST;
$this->_id = $parsedURL['path'];
return $this->cookies()->theme(self::THEME_LIGHT);
}
// both playlist types use list=
$parsedQuery = explode("&",$parsedURL['query']);
// Find list=
foreach($parsedQuery as $v)
{
if(substr(strtolower($v),0,5) == "list=")
{
$this->_type = self::TYPE_PLAYLIST;
$this->_id = substr($v,5);
return $this->cookies()->theme(self::THEME_LIGHT);
}
}
// If we don't find list=, then its not a playlist.
$this->_source = $oldSource;
throw new YTAudioException("Could not identify playlist");
}
/**
* Get Source
* Returns the source exactly as you fed it to us.
* The source is only changed if setting it was successful.
*
* @return string
*/
public function getSource() { return $this->_source; }
/**
* Get Player Type
*
* @return enum/bool
*/
public function getType() { return $this->_type; }
public function isVideo() { return ($this->getType() == self::TYPE_VIDEO); }
public function isPlaylist() { return ($this->getType() == self::TYPE_PLAYLIST); }
/**
* Get Player ID
*
* @return string
*/
public function getID() { return $this->_id; }
/**
* Set Player Size
*
* @throws YTAudioException
* @param enum $size [SIZE_INVISIBLE | SIZE_TINY | SIZE_SMALL | SIZE_MEDIUM | SIZE_LARGE]
* @return YTAudio
*/
public function size($size)
{
if($size != self::SIZE_INVISIBLE && $size != self::SIZE_TINY && $size != self::SIZE_SMALL && $size != self::SIZE_MEDIUM && $size != self::SIZE_LARGE) throw new YTAudioException("Invalid Size");
$this->_size = $size;
// Progress Bar & Time Code can not be used with Tiny/Invisible
// Any other size (Small, Medium, Large) MUST have a Progress Bar
if($size == self::SIZE_TINY || $size == self::SIZE_INVISIBLE) $this->progressBar(false)->timeCode(false);
else $this->progressBar();
return $this;
}
/**
* Get Player Size
*
* @return enum/bool
*/
public function getSize() { return $this->_size; }
public function isTiny() { return ($this->getSize() == self::SIZE_TINY); }
public function isSmall() { return ($this->getSize() == self::SIZE_SMALL); }
public function isMedium() { return ($this->getSize() == self::SIZE_MEDIUM); }
public function isLarge() { return ($this->getSize() == self::SIZE_LARGE); }
/**
* Set Player Invisible
* Convenience function, since Invisibility is a SIZE
*
* @return YTAudio
*/
public function invisible() { $this->size(self::SIZE_INVISIBLE); }
/**
* Get Player Invisibility Setting
*
* @return bool
*/
public function getInvisible() { return ($this->getSize() == self::SIZE_INVISIBLE); }
public function isInvisible() { return $this->getInvisible(); }
/**
* Set Player Theme
*
* @throws YTAudioException
* @param enum $theme [THEME_LIGHT | THEME_DARK]
* @return YTAudio
*/
public function theme($theme)
{
if($this->isPlaylist() && $theme == self::THEME_DARK) throw new YTAudioException("Playlists can not use the Dark Theme. YouTube limitation.");
if($theme != self::THEME_LIGHT && $theme != self::THEME_DARK) throw new YTAudioException("Invalid Theme");
$this->_theme = $theme;
return $this;
}
/**
* Get Player Theme
*
* @return bool
*/
public function getTheme() { return $this->_theme; }
/**
* Set HD
* Choose whether or not to force the player into HD
*
* @param bool $useHD
* @return YTAudio
*/
public function hd($useHD = true)
{
if($useHD) $this->_hd = true;
else $this->_hd = false;
return $this;
}
/**
* Get HD Setting
*
* @return bool
*/
public function getHD() { return $this->_hd; }
public function isHD() { return $this->getHD(); }
/**
* Set Autoplay
* Choose whether or not to automatically play the video when it loads
* Please don't use this. You'll make me sad.
*
* @param bool $autoplay
* @return YTAudio
*/
public function autoplay($autoplay = true)
{
if($autoplay) $this->_autoplay = true;
else $this->_autoplay = false;
return $this;
}
/**
* Get Autoplay Setting
*
* @return bool
*/
public function getAutoplay() { return $this->_autoplay; }
public function willAutoplay() { return $this->getAutoplay(); }
/**
* Set JSApi
* Choose whether or not to allow access via the YouTube JavaScript API
*
* @param bool $useJSAPI
* @return YTAudio
*/
public function jsAPI($useJSAPI = true)
{
if($useJSAPI) $this->_jsapi = true;
else $this->_jsapi = false;
return $this;
}
/**
* Get JavaScript API Setting
*
* @return bool
*/
public function getJSAPI() { return $this->_jsapi; }
public function canUseJSAPI() { return $this->getJSAPI(); }
public function canUseJavaScriptAPI() { return $this->getJSAPI(); }
/**
* Set Loop
* Choose whether or not to loop once the video/playlist is over
*
* @param bool $loop
* @return YTAudio
*/
public function loop($loop = true)
{
if($loop) $this->_loop = true;
else $this->loop = false;
return $this;
}
/**
* Get Loop Setting
*
* @return bool
*/
public function getLoop() { return $this->_loop; }
public function willLoop() { return $this->getLoop(); }
/**
* Set Progress Bar
* Choose whether or not to display the progress bar
*
* @param bool $useProgressBar
* @return YTAudio
*/
public function progressBar($useProgressBar = true)
{
if($useProgressBar) $this->_progressbar = true;
else $this->_progressbar = false;
// If they set this true after saying they want tiny, they actually want small.
if($useProgressBar && $this->getSize() == self::SIZE_TINY) $this->size(self::SIZE_SMALL);
// If they set this true after saying they want invisible, they're being silly and I refuse to handle it.
return $this;
}
/**
* Get Progress Bar Setting
*
* @return bool
*/
public function getProgressBar() { return $this->_progressbar; }
public function hasProgressBar() { return $this->getProgressBar(); }
/**
* Set Time Code
* Choose whether or not to display the time code. Requires Progress Bar
*
* @param bool $useTimeCode
* @return YTAudio
*/
public function timeCode($useTimeCode = true)
{
if($useTimeCode)
{
// Requires Progress Bar. Sorry.
$this->progressBar();
$this->_timecode = true;
}
else $this->_timecode = false;
// If they set this true after saying they want tiny, they actually want small.
if($useProgressBar && $this->getSize() == self::SIZE_TINY) $this->size(self::SIZE_SMALL);
// If they set this true after saying they want invisible, they're being silly and I refuse to handle it.
return $this;
}
/**
* Get Time Code Setting
*
* @return bool
*/
public function getTimeCode() { return $this->_timecode; }
public function hasTimeCode() { return $this->getTimeCode(); }
/**
* Set Cookies
* Choose whether or not to allow YouTube to collect cookies.
*
* @param bool $useCookies
* @return YTAudio
*/
public function cookies($useCookies = true)
{
if(!$useCookies && $this->getType() == self::TYPE_PLAYLIST) throw new YTAudioException("Can not disable cookies with playlists. YouTube limitation.");
if($useCookies) $this->_cookies = true;
else $this->_cookies = false;
return $this;
}
/**
* Get Cookie Setting
*
* @return bool
*/
public function getCookies() { return $this->_cookies; }
public function willUseCookies() { return $this->getCookies(); }
/**
* Set HTTPS
* Choose whether to use HTTPs or HTTP
*
* @param bool $useHTTPS
* @return YTAudio
*/
public function https($useHTTPS = true)
{
if($useHTTPS) $this->_https = true;
else $this->_https = false;
return $this;
}
/**
* Get HTTPS Setting
*
* @return bool
*/
public function getHTTPS() { return $this->_https; }
public function isHTTPS() { return $this->_https; }
public function isHTTP() { return !$this->_https; }
/**
* Get Height (px)
*
* @return int
*/
public function _getHeight()
{
if($this->getSize() == self::SIZE_INVISIBLE) return 1;
return 25;
}
/**
* Get Width (px)
*
* @return int
*/
public function _getWidth()
{
if($this->getSize() == self::SIZE_INVISIBLE) return 1;
if($this->getSize() == self::SIZE_TINY) return 30;
$modifier = 0;
if($this->getTimeCode()) $modifier = 75;
if($this->getSize() == self::SIZE_SMALL) return 150 + $modifier;
if($this->getSize() == self::SIZE_MEDIUM) return 187 + $modifier;
if($this->getSize() == self::SIZE_LARGE) return 224 + $modifier;
}
/**
* Get Embed URL
*
* @return string
*/
public function _getEmbedURL($encode = TRUE)
{
$url = "";
// PROTOCOL
if($this->getHTTPS()) $url .= "https://";
else $url .= "http://";
// DOMAIN
if($this->getCookies()) $url .= "www.youtube.com";
else $url .= "www.youtube-nocookie.com";
// PATH
if($this->isVideo()) $url .= "/v/";
else $url .= "/p/";
// ID
if($this->isVideo()) $url .= $this->getID();
else $url .= substr($this->getID(),2); // Playlists start with PL but YouTube doesn't want that
// Build Query String
$query = array();
$query['version'] = 2;
if($this->getAutoplay()) $query['autoplay'] = 1;
if($this->getLoop()) $query['loop'] = 1;
if($this->getJSAPI()) $query['enablejsapi'] = 1;
if($this->isHD()) $query['hd'] = 1;
$query['theme'] = $this->getTheme();
$seperator = $encode ? '&' : '&';
$url .= '?' . http_build_query($query,$seperator);
return $url;
}
/**
* Render valid XHTML
*
* @param bool $return Return the HTML instead of echoing it.
* @return string
*/
public function render($return = false)
{
// Build the string
$html = '<object type="application/x-shockwave-flash"';
$html .= ' width="' . $this->_getWidth() . '"';
$html .= ' height="' . $this->_getHeight() . '"';
$html .= ' data="' . $this->_getEmbedURL() . '"';
if($this->isInvisible()) $html .= ' style="visibility:hidden;display:inline;"';
$html .= '>';
$html .= '<param name="movie" value="' . $this->_getEmbedURL() . '" />';
$html .= '<param name="wmode" value="transparent" />';
$html .= '</object>';
if($return) return $html;
echo $html;
}
}
class YTAudioException extends Exception
{
public function __construct($message,$code = 0)
{
parent::__construct($message,$code);
}
}