[BUGFIX] Media element does not recognize short YouTube links
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_mediawizardcoreprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Aishwara M.B. (aishu.moorthy@gmail.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * 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 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains an implementation of the mediaWizardProvider supporting some
29 * well known providers.
30 *
31 * $Id: $
32
33 * @author Aishwara M.B.<aishu.moorthy@gmail.com>
34 * @author Steffen Kamper <info@sk-typo3.de>
35 * @author Ernesto Baschny <ernst@cron-it.de>
36 */
37
38
39 class tslib_mediaWizardCoreProvider implements tslib_mediaWizardProvider {
40
41 /**
42 * @var array List of providers we can handle in this class
43 */
44 protected $providers = array(
45 'youtube',
46 'youtu',
47 'dailymotion',
48 'sevenload',
49 'vimeo',
50 'clipfish',
51 'google',
52 'metacafe',
53 'myvideo',
54 'liveleak',
55 'veoh'
56 );
57
58 /**
59 * Checks if we have a valid method for processing a given URL.
60 *
61 * This is done by analysing the hostname of the URL and checking if it contains
62 * any of our provider tags defined in $this->providers.
63 *
64 * @param $url
65 * @return string
66 */
67 protected function getMethod($url) {
68 $urlInfo = @parse_url($url);
69 if ($urlInfo === FALSE) {
70 return NULL;
71 }
72 $hostName = t3lib_div::trimExplode('.', $urlInfo['host'], TRUE);
73 foreach ($this->providers as $provider) {
74 $functionName = 'process_' . $provider;
75 if (in_array($provider, $hostName) && is_callable(array($this, $functionName))) {
76 return $functionName;
77 }
78 }
79 return NULL;
80 }
81
82 /***********************************************
83 *
84 * Implementation of tslib_mediaWizardProvider
85 *
86 ***********************************************/
87
88 /**
89 * @param $url
90 * @return bool
91 * @see tslib_mediaWizardProvider::canHandle
92 */
93 public function canHandle($url) {
94 return ($this->getMethod($url) !== NULL);
95 }
96
97 /**
98 * @param $url URL to rewrite
99 * @return string The rewritten URL
100 * @see tslib_mediaWizardProvider::rewriteUrl
101 */
102 public function rewriteUrl($url) {
103 $method = $this->getMethod($url);
104 return $this->$method($url);
105 }
106
107 /***********************************************
108 *
109 * Providers URL rewriting:
110 *
111 ***********************************************/
112
113 /**
114 * Parse youtube url
115 *
116 * @param string $url
117 * @return string processed url
118 */
119 protected function process_youtube($url) {
120 $videoId = '';
121 if (strpos($url, '/user/') !== FALSE) {
122 // it's a channel
123 $parts = explode('/', $url);
124 $videoId = $parts[count($parts) - 1];
125 } elseif (preg_match('/(v=|v\/|.be\/)([^(\&|$)]*)/', $url, $matches)) {
126 $videoId = $matches[2];
127 }
128
129 if ($videoId) {
130 $url = 'http://www.youtube.com/v/' . $videoId . '?fs=1';
131 }
132 return $url;
133 }
134
135 /**
136 * Parse youtube short url
137 *
138 * @param string $url
139 * @return string processed url
140 */
141 protected function process_youtu($url) {
142 return $this->process_youtube($url);
143 }
144
145 /**
146 * Parse dailymotion url
147 *
148 * @param string $url
149 * @return string processed url
150 */
151 protected function process_dailymotion($url) {
152 $parts = explode('video/', $url);
153 $videoId = $parts[1];
154 if (strpos($videoId, '/') !== FALSE) {
155 $videoId = substr($videoId, 0, strpos($videoId, '/'));
156 }
157 return 'http://www.dailymotion.com/swf/' . $videoId;
158 }
159
160 /**
161 * Parse sevenload url
162 *
163 * @param string $url
164 * @return string processed url and preview image
165 */
166 protected function process_sevenload($url) {
167 $parts = explode('/', $url);
168 $videoId = $parts[count($parts) - 1];
169 if (strpos($videoId, '-') !== FALSE) {
170 $videoId = substr($videoId, 0, strpos($videoId, '-'));
171 }
172 return 'http://de.sevenload.com/pl/' . $videoId . '/400x500/swf';
173 }
174
175 /**
176 * Parse vimeo url
177 *
178 * Supports:
179 * - http://vimeo.com/hd#<id>
180 * - http://vimeo.com/<id>
181 * - http://player.vimeo.com/video/<id>
182 *
183 * @param string $url
184 * @return string processed url
185 */
186 protected function process_vimeo($url) {
187 if (preg_match('/[\/#](\d+)$/', $url, $matches)) {
188 $videoId = $matches[1];
189 $url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $videoId . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&fullscreen=1';
190 }
191 return $url;
192 }
193
194 /**
195 * Parse clipfish url
196 *
197 * @param string $url
198 * @return string processed url
199 */
200 protected function process_clipfish($url) {
201 if (preg_match('/video([^(\&|$)]*)/', $url, $matches)) {
202 $parts = explode('/', $matches[1]);
203 $videoId = $parts[1];
204 $url = 'http://www.clipfish.de/cfng/flash/clipfish_player_3.swf?as=0&r=1&noad=1&fs=1&vid=' . $videoId;
205 }
206 return $url;
207 }
208
209
210 /**
211 * Parse google url
212 *
213 * @param string $url
214 * @return string processed url
215 */
216 protected function process_google($url) {
217 if (preg_match('/docid=([^(\&|$)]*)/', $url, $matches)) {
218 $videoId = $matches[1];
219 $url = 'http://video.google.com/googleplayer.swf?docid=' . $videoId;
220 }
221 return $url;
222 }
223
224 /**
225 * Parse metacafe url
226 *
227 * @param string $url
228 * @return string processed url
229 */
230
231 protected function process_metacafe($url) {
232 if (preg_match('/watch([^(\&|$)]*)/', $url, $matches)) {
233 $parts = explode('/', $matches[1]);
234 $videoId = $parts[1];
235 $url = 'http://www.metacafe.com/fplayer/' . $videoId . '/.swf';
236 }
237 return $url;
238 }
239
240 /**
241 * Parse myvideo url
242 *
243 * @param string $url
244 * @return string processed url
245 */
246
247 protected function process_myvideo($url) {
248 preg_match('/watch([^(\&|$)]*)/', $url, $matches);
249 $parts = explode('/', $matches[1]);
250 $videoId = $parts[1];
251
252 return 'http://www.myvideo.de/movie/' . $videoId . '/';
253 }
254
255
256 /**
257 * Parse liveleak url
258 *
259 * @param string $url
260 * @return string processed url
261 */
262 protected function process_liveleak($url) {
263 preg_match('/i=([^(\&|$)]*)/', $url, $matches);
264 $videoId = $matches[1];
265
266 return 'http://www.liveleak.com/e/' . $videoId;
267 }
268
269 /**
270 * Parse veoh url
271 *
272 * @param string $url
273 * @return string processed url
274 */
275 protected function process_veoh($url) {
276 preg_match('/watch\/([^(\&|$)]*)/', $url, $matches);
277 $videoId = $matches[1];
278
279 return 'http://www.veoh.com/static/swf/webplayer/WebPlayer.swf?version=AFrontend.5.5.2.1001&permalinkId=' . $videoId;
280 }
281
282 }
283
284 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/class.tslib_mediawizardcoreprovider.php'])) {
285 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/class.tslib_mediawizardcoreprovider.php']);
286 }
287
288 ?>