externalvideo: Added support for various media sources
[mahara:mahara.git] / htdocs / blocktype / externalvideo / media_sources / slideshare / mediasource.php
1 <?php
2
3 require_once(dirname(__FILE__) . '/../Media_base.php');
4
5 class Media_slideshare implements MediaBase {
6
7     private static $base_url = 'http://slideshare.net/';
8
9     private static $default_width  = 425;
10     private static $default_height = 355;
11
12     private static $scrape_sources = array(
13         array(
14             'match' => '@.*https?://(www\.)?slideshare\.net/([^/]+)/([^/?&#]+).*@',
15             'url'   => 'http://www.slideshare.net/$2/$3',
16         ),
17     );
18
19     private static $iframe_sources = array(
20         array(
21             'match' => '#.*https?://(www\.)?slideshare\.net/slideshow/embed_code/([0-9]+).*#',
22             'url'   => 'http://www.slideshare.net/slideshow/embed_code/$2',
23         ),
24     );
25
26     public function process_url($input, $width=0, $height=0) {
27         if (empty($input)) {
28             return false;
29         }
30
31         $width  = $width  ? (int)$width  : self::$default_width;
32         $height = $height ? (int)$height : self::$default_height;
33
34         foreach (self::$iframe_sources as $source) {
35             if (preg_match($source['match'], $input)) {
36                 $output = preg_replace($source['match'], $source['url'], $input);
37                 $result = array(
38                     'videoid' => $output,
39                     'type'    => 'iframe',
40                     'width'   => $width,
41                     'height'  => $height,
42                 );
43                 return $result;
44             }
45         }
46
47         foreach (self::$scrape_sources as $source) {
48             if (preg_match($source['match'], $input)) {
49                 $output = preg_replace($source['match'], $source['url'], $input);
50                 return $this->process_url(self::scrape_url($output), $width, $height);
51             }
52         }
53         return false;
54     }
55
56     public function validate_url($input) {
57         foreach (self::$scrape_sources as $source) {
58             if (preg_match($source['match'], $input)) {
59                 return true;
60             }
61         }
62
63         foreach (self::$iframe_sources as $source) {
64             if (preg_match($source['match'], $input)) {
65                 return true;
66             }
67         }
68         return false;
69     }
70
71     public function get_base_url() {
72         return self::$base_url;
73     }
74
75     private static function scrape_url($url) {
76         $config = array(
77             CURLOPT_URL => $url,
78         );
79
80         static $scrape_regex = '#.*?https?://(www\.)?slideshare\.net/share/(tweet|facebook|linkedin)/([0-9]+)/.*#';
81
82         $data = mahara_http_request($config);
83         if (preg_match($scrape_regex, $data->data, $matches)) {
84             $slideid = $matches[3];
85             return 'http://www.slideshare.net/slideshow/embed_code/' . $slideid;
86         }
87         return false;
88     }
89 }