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