]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/YouTube.php
Add missing alt argument
[SourceForge/phpwiki.git] / lib / plugin / YouTube.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2007 Reini Urban
5  Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
6 */
7
8 /**
9  * Embed YouTube videos
10  *
11  * Browse: Daily pick, Most Recent, Most Viewed, Top Rated, Most Discussed, Top Favorites, Most Linked,
12  *         Recently Featured, Most Responded, Watch on Mobile
13  * Time:   Today, This Week, This Month, All Time
14  * Category: All, Autos & Vehicles, Comedy, Entertainment, Film & Animation, 
15  *         Gadgets & Games, Howto & DIY, Music, News & Politics, People & Blogs, Pets & Animals, 
16  *         Sports, Travel & Places
17  * Language: All, English, Spanish, Japanese, German, Chinese, French
18  * @author: Reini Urban
19  */
20
21 class WikiPlugin_YouTube
22 extends WikiPlugin
23 {
24     function getName () {
25         return _("YouTube");
26     }
27     
28     function getDescription () {
29         return _("Embed YouTube videos");
30     }
31     
32     function getVersion() {
33         return preg_replace("/[Revision: $]/", '',"\$Revision$");
34     }
35     
36     function getDefaultArguments() {
37         return array('v' => "",
38                      'browse' => '',      // see above
39                      'time'   => '',      // only if browse
40                      'category' => '',    // only if browse
41                      'language' => '',    // only if browse
42                      'index'    => 0,     // only if browse
43                      'style' => 'inline', // or link. link links to youtube.
44                      'size'  => 'medium', // or large, medium or small
45                      'autoplay' => 0,
46                      'width' => "425",
47                      'height' => "350");
48     }
49    
50     function run($dbi, $argstr, &$request, $basepage) {
51         $args = $this->getArgs($argstr, $request);
52         extract($args);
53         if (empty($args['v'])) {
54             if (empty($args['browse']))
55                 return $this->error(fmt("Required argument %s missing", "v"));
56             $this->_browse = array("Most Recent"   => "mr",
57                                    "Most Viewed"   => "mp",
58                                    "Top Rated"     => "tr",
59                                    "Most Discussed"=> "md",
60                                    "Top Favorites" => "mf",
61                                    "Most Linked"   => "mrd",
62                                    "Recently Featured"=> "rf",
63                                    "Most Responded"   => "ms",
64                                    "Watch on Mobile"  => "mv");
65             $this->browse   = array_keys($this->_browse);
66             array_unshift($this->browse, "Daily Pick");
67             $this->_time    = array("Today" => "t",
68                                     "This Week" => "w",
69                                     "This Month" => "m",
70                                     "All Time" => "a");
71             $this->_category = array("All"              => "0",
72                                      "Autos & Vehicles" => "2",
73                                      "Comedy"           => "23",
74                                      "Entertainment"    => "24",
75                                      "Film & Animation" => "1",
76                                      "Gadgets & Games"  => "20",
77                                      "Howto & DIY"      => "26",
78                                      "Music"            => "10",
79                                      "News & Politics"  => "25",
80                                      "People & Blogs"   => "22",
81                                      "Pets & Animals"   => "15",
82                                      "Sports"           => "17",
83                                      "Travel & Places"  => "19");
84             $this->_language = array("All"     => "",
85                                      "English" => "EN",
86                                      "Spanish" => "ES",
87                                      "Japanese"=> "JA",
88                                      "German"  => "DE",
89                                      "Chinese" => "CN",
90                                      "French"  => "FR");
91             if (!in_array($browse,$this->browse))
92                 return $this->error(fmt("Invalid argument %s", "browse"));
93             if ($time and !in_array($time,array_keys($this->_time)))
94                 return $this->error(fmt("Invalid argument %s", "time"));
95             if ($category and !in_array($category,$this->category)) 
96                 return $this->error(fmt("Invalid argument %s", "category"));
97             if ($language and !in_array($language,$this->language)) 
98                 return $this->error(fmt("Invalid argument %s", "language"));
99             if ($browse == "Daily Pick")
100                 $v = $this->Daily_pick();
101             else {
102                 $s = $this->_browse[$browse];
103                 $t = $time ? $this->_time[$time] : 't';
104                 $c = $category ? $this->_category[$category] : '0';
105                 $l = $language ? $this->_language[$language] : '';
106                 $url = "http://www.youtube.com/browse?s=$s&t=$t&c=$c&l=$l";
107                 $m = array('','');
108                 if ($xml = url_get_contents($url)) {
109                     if ($index) {
110                         if (preg_match_all('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
111                             $v = $m[1][$index];
112                     }
113                     else {
114                         if (preg_match('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
115                             $v = $m[1];
116                     }
117                 }
118             }
119         }
120         // sanify check
121         if (strlen($v) < 10 or strlen($v) > 12)
122             return $this->error(fmt("Invalid argument %s", "v"));
123         if (strcspn($v,"-_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
124             return $this->error(fmt("Invalid argument %s", "v"));
125         $url = "http://www.youtube.com/v/" . $v;
126         if ($autoplay)
127             $url .= "?autoplay=1";
128         if ($size != 'medium') {
129             if ($size == 'large') { $width = 640; $height = 526; }
130             elseif ($size == 'small') { $width = 240; $height = 200; }
131         }
132         unset($args['size']);
133         unset($args['style']);
134         $args['src'] = $v;
135         unset($args['v']);
136         if ($style == 'link') {
137             if ($size == 'medium') { $width = 130; $height = 97; }
138             elseif ($size == 'large') { $width = 320; $height = 240; }
139             elseif ($size == 'small') { $width = 90; $height = 60; }
140             // img: http://img.youtube.com/vi/KKTDRqQtPO8/2.jpg or 0.jpg
141             $link = HTML::a(array('href' => $url),
142                             HTML::img(array('src' => "http://img.youtube.com/vi/".
143                                             $v."/".(($size == 'large')?"0":"2").".jpg",
144                                             'width' => $width,
145                                             'height' => $height,
146                                             'alt' => "YouTube video $v")));
147             return $link;
148         }
149         $object = HTML::object(array('class' => 'inlineobject',
150                                      'width' => $width,
151                                      'height' => $height,
152                                      ));
153         $attrs = array('src' => $url,
154                        'type' => 'application/x-shockwave-flash', 
155                        'wmode' => 'transparent',
156                        'width' => $width,
157                        'height' => $height);
158         if (isBrowserSafari()) {
159             return HTML::embed($attrs);
160         }
161         $object->pushContent(HTML::param(array('name' => 'movie', 'value' => $url)));
162         $object->pushContent(HTML::param(array('name' => 'wmode', 'value' => 'transparent')));
163         $object->pushContent(HTML::embed($attrs));
164         return $object;
165
166         //<object width="$w" height="$h"><param name="movie" value="http://www.youtube.com/v/$v"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$v" type="application/x-shockwave-flash" wmode="transparent" width="$w" height="$h"></embed></object>
167     }
168
169     function Daily_pick() {
170         if ($xml = url_get_contents("http://www.youtube.com/categories")) {
171             if (preg_match('/<div class="heading"><b>Pick of The Day<\/b><\/div>.*?<a href="\/watch\?v=(\w+)">/s', $xml, $m))
172                 return $m[1];
173         }
174         return '';
175     }
176 };
177
178 // For emacs users
179 // Local Variables:
180 // mode: php
181 // tab-width: 8
182 // c-basic-offset: 4
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil
185 // End:
186 ?>