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