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