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