externalvideo: Added support for various media sources
[mahara:mahara.git] / htdocs / blocktype / externalvideo / media_sources / teachertube / mediasource.php
1 <?php
2
3 require_once(dirname(__FILE__) . '/../Media_base.php');
4
5 class Media_teachertube implements MediaBase {
6
7     private static $base_url = 'http://www.teachertube.com/';
8
9     private static $default_width  = 485;
10     private static $default_height = 300;
11
12     private static $embed_sources  = array(
13         array(
14             'match' => '#.*teachertube\.com/flvideo/([0-9]+)\.flv.*#',
15             'url'   => 'http://www.teachertube.com/skin-p/mediaplayer.swf?file=http://www.teachertube.com/flvideo/$1.flv',
16         ),
17         array(
18             'match' => '#.*teachertube\.com/viewVideo\.php\?video_id=(\d+).*#',
19             'url'   => 'http://www.teachertube.com/embed/player.swf?file=http://www.teachertube.com/embedFLV.php?pg=video_$1',
20         ),
21         array(
22             'match' => '#.*http://(www\.)?teachertube\.com/embed/player\.swf\?file=http://(www\.)?teachertube\.com/embedFLV.php\?pg=video_([0-9]+).*#',
23             'url'   => 'http://www.teachertube.com/embed/player.swf?file=http://www.teachertube.com/embedFLV.php?pg=video_$3',
24         ),
25         array(
26             'match' => '#.*http://(www\.)?teachertube.com/skin-p/mediaplayer\.swf\?file=http://www.teachertube.com/flvideo/([0-9]+)\.flv.*#',
27             'url'   => 'http://www.teachertube.com/skin-p/mediaplayer.swf?file=http://www.teachertube.com/flvideo/$2.flv',
28         ),
29     );
30
31     public function process_url($input, $width=0, $height=0) {
32         $width  = $width  ? (int)$width  : self::$default_width;
33         $height = $height ? (int)$height : self::$default_height;
34
35         foreach (self::$embed_sources as $source) {
36             if (preg_match($source['match'], $input)) {
37                 $output = preg_replace($source['match'], $source['url'], $input);
38                 $result = array(
39                     'videoid' => $output,
40                     'type'    => 'embed',
41                     'width'   => $width,
42                     'height'  => $height,
43                 );
44                 return $result;
45             }
46         }
47         return false;
48     }
49
50     public function validate_url($input) {
51         foreach (self::$embed_sources as $source) {
52             if (preg_match($source['match'], $input)) {
53                 return true;
54             }
55         }
56         return false;
57     }
58
59     public function get_base_url() {
60         return self::$base_url;
61     }
62 }