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