[BUGFIX] Use correct url schema in MediaWizardProvider
[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 textfile 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 \TYPO3\CMS\Frontend\MediaWizard\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 $hostName = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('.', $urlInfo['host'], TRUE);
71 foreach ($this->providers as $provider) {
72 $functionName = 'process_' . $provider;
73 if (in_array($provider, $hostName) && is_callable(array($this, $functionName))) {
74 return $functionName;
75 }
76 }
77 return NULL;
78 }
79
80 /***********************************************
81 *
82 * Implementation of MediaWizardProviderInterface
83 *
84 ***********************************************/
85 /**
86 * @param string $url
87 * @return boolean
88 * @see MediaWizardProviderInterface::canHandle
89 */
90 public function canHandle($url) {
91 return $this->getMethod($url) !== NULL;
92 }
93
94 /**
95 * @param string $url URL to rewrite
96 * @return string The rewritten URL
97 * @see MediaWizardProviderInterface::rewriteUrl
98 */
99 public function rewriteUrl($url) {
100 $method = $this->getMethod($url);
101 return $this->{$method}($url);
102 }
103
104 /***********************************************
105 *
106 * Providers URL rewriting:
107 *
108 ***********************************************/
109 /**
110 * Parse youtube url
111 *
112 * @param string $url
113 * @return string processed url
114 */
115 protected function process_youtube($url) {
116 $videoId = '';
117 if (strpos($url, '/user/') !== FALSE) {
118 // it's a channel
119 $parts = explode('/', $url);
120 $videoId = $parts[count($parts) - 1];
121 } elseif (preg_match('/(v=|v\\/|.be\\/)([^(\\&|$)]*)/', $url, $matches)) {
122 $videoId = $matches[2];
123 }
124 if ($videoId) {
125 $url = $this->getUrlSchema() . 'www.youtube.com/v/' . $videoId . '?fs=1';
126 }
127 return $url;
128 }
129
130 /**
131 * Parse youtube short url
132 *
133 * @param string $url
134 * @return string processed url
135 */
136 protected function process_youtu($url) {
137 return $this->process_youtube($url);
138 }
139
140 /**
141 * Parse dailymotion url
142 *
143 * @param string $url
144 * @return string processed url
145 */
146 protected function process_dailymotion($url) {
147 $parts = explode('video/', $url);
148 $videoId = $parts[1];
149 if (strpos($videoId, '/') !== FALSE) {
150 $videoId = substr($videoId, 0, strpos($videoId, '/'));
151 }
152 return $this->getUrlSchema() . 'www.dailymotion.com/swf/' . $videoId;
153 }
154
155 /**
156 * Parse sevenload url
157 *
158 * @param string $url
159 * @return string processed url and preview image
160 */
161 protected function process_sevenload($url) {
162 $parts = explode('/', $url);
163 $videoId = $parts[count($parts) - 1];
164 if (strpos($videoId, '-') !== FALSE) {
165 $videoId = substr($videoId, 0, strpos($videoId, '-'));
166 }
167 return 'http://de.sevenload.com/pl/' . $videoId . '/400x500/swf';
168 }
169
170 /**
171 * Parse vimeo url
172 *
173 * Supports:
174 * - http://vimeo.com/hd#<id>
175 * - http://vimeo.com/<id>
176 * - http://player.vimeo.com/video/<id>
177 *
178 * @param string $url
179 * @return string processed url
180 */
181 protected function process_vimeo($url) {
182 if (preg_match('/[\\/#](\\d+)$/', $url, $matches)) {
183 $videoId = $matches[1];
184 $url = $this->getUrlSchema() . 'vimeo.com/moogaloop.swf?clip_id=' . $videoId . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&fullscreen=1';
185 }
186 return $url;
187 }
188
189 /**
190 * Parse clipfish url
191 *
192 * @param string $url
193 * @return string processed url
194 */
195 protected function process_clipfish($url) {
196 if (preg_match('/video([^(\\&|$)]*)/', $url, $matches)) {
197 $parts = explode('/', $matches[1]);
198 $videoId = $parts[1];
199 $url = 'http://www.clipfish.de/cfng/flash/clipfish_player_3.swf?as=0&r=1&noad=1&fs=1&vid=' . $videoId;
200 }
201 return $url;
202 }
203
204 /**
205 * Parse google url
206 *
207 * @param string $url
208 * @return string processed url
209 */
210 protected function process_google($url) {
211 if (preg_match('/docid=([^(\\&|$)]*)/', $url, $matches)) {
212 $videoId = $matches[1];
213 $url = $this->getUrlSchema() . 'video.google.com/googleplayer.swf?docid=' . $videoId;
214 }
215 return $url;
216 }
217
218 /**
219 * Parse metacafe url
220 *
221 * @param string $url
222 * @return string processed url
223 */
224 protected function process_metacafe($url) {
225 if (preg_match('/watch([^(\\&|$)]*)/', $url, $matches)) {
226 $parts = explode('/', $matches[1]);
227 $videoId = $parts[1];
228 $url = 'http://www.metacafe.com/fplayer/' . $videoId . '/.swf';
229 }
230 return $url;
231 }
232
233 /**
234 * Parse myvideo url
235 *
236 * @param string $url
237 * @return string processed url
238 */
239 protected function process_myvideo($url) {
240 preg_match('/watch([^(\\&|$)]*)/', $url, $matches);
241 $parts = explode('/', $matches[1]);
242 $videoId = $parts[1];
243 return $this->getUrlSchema() . 'www.myvideo.de/movie/' . $videoId . '/';
244 }
245
246 /**
247 * Parse liveleak url
248 *
249 * @param string $url
250 * @return string processed url
251 */
252 protected function process_liveleak($url) {
253 preg_match('/i=([^(\\&|$)]*)/', $url, $matches);
254 $videoId = $matches[1];
255 return 'http://www.liveleak.com/e/' . $videoId;
256 }
257
258 /**
259 * Parse veoh url
260 *
261 * @param string $url
262 * @return string processed url
263 */
264 protected function process_veoh($url) {
265 preg_match('/watch\\/([^(\\&|$)]*)/', $url, $matches);
266 $videoId = $matches[1];
267 return 'http://www.veoh.com/static/swf/webplayer/WebPlayer.swf?version=AFrontend.5.5.2.1001&permalinkId=' . $videoId;
268 }
269
270 /**
271 * Get the correct url schema
272 *
273 * @return string
274 */
275 protected function getUrlSchema() {
276 return \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
277 }
278
279 }
280
281
282 ?>