[BUGFIX] SQL error in install tool if no root page available
[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
118 $pattern = '%
119 ^(?:https?://)? # Optional URL scheme Either http or https
120 (?:www\.)? # Optional www subdomain
121 (?: # Group host alternatives:
122 youtu\.be/ # Either youtu.be/,
123 |youtube(?: # or youtube.com/
124 -nocookie # optional nocookie domain
125 )?\.com/(?:
126 [^/]+/.+/ # Either /something/other_params/ for channels,
127 |(?:v|e(?: # or v/ or e/,
128 mbed # optional mbed for embed/
129 )?)/
130 |.*[?&]v= # or ?v= or ?other_param&v=
131 )
132 ) # End host alternatives.
133 ([^"&?/ ]{11}) # 11 characters (Length of Youtube video ids).
134 (?:.+)?$ # Optional other ending URL parameters.
135 %xs';
136 if (preg_match($pattern, $url, $matches)) {
137 $videoId = $matches[1];
138 }
139
140 if ($videoId) {
141 $url = $this->getUrlSchema() . 'www.youtube.com/embed/' . $videoId . '?fs=1';
142 }
143 return $url;
144 }
145
146 /**
147 * Parse youtube short url
148 *
149 * @param string $url
150 * @return string processed url
151 */
152 protected function process_youtu($url) {
153 return $this->process_youtube($url);
154 }
155
156 /**
157 * Parse dailymotion url
158 *
159 * @param string $url
160 * @return string processed url
161 */
162 protected function process_dailymotion($url) {
163 $parts = explode('video/', $url);
164 $videoId = $parts[1];
165 if (strpos($videoId, '/') !== FALSE) {
166 $videoId = substr($videoId, 0, strpos($videoId, '/'));
167 }
168 return $this->getUrlSchema() . 'www.dailymotion.com/swf/' . $videoId;
169 }
170
171 /**
172 * Parse sevenload url
173 *
174 * @param string $url
175 * @return string processed url and preview image
176 */
177 protected function process_sevenload($url) {
178 $parts = explode('/', $url);
179 $videoId = $parts[count($parts) - 1];
180 if (strpos($videoId, '-') !== FALSE) {
181 $videoId = substr($videoId, 0, strpos($videoId, '-'));
182 }
183 return 'http://de.sevenload.com/pl/' . $videoId . '/400x500/swf';
184 }
185
186 /**
187 * Parse vimeo url
188 *
189 * Supports:
190 * - http://vimeo.com/hd#<id>
191 * - http://vimeo.com/<id>
192 * - http://player.vimeo.com/video/<id>
193 *
194 * @param string $url
195 * @return string processed url
196 */
197 protected function process_vimeo($url) {
198 if (preg_match('/[\\/#](\\d+)$/', $url, $matches)) {
199 $videoId = $matches[1];
200 $url = $this->getUrlSchema() . 'vimeo.com/moogaloop.swf?clip_id=' . $videoId . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&fullscreen=1';
201 }
202 return $url;
203 }
204
205 /**
206 * Parse clipfish url
207 *
208 * @param string $url
209 * @return string processed url
210 */
211 protected function process_clipfish($url) {
212 if (preg_match('/video([^(\\&|$)]*)/', $url, $matches)) {
213 $parts = explode('/', $matches[1]);
214 $videoId = $parts[1];
215 $url = 'http://www.clipfish.de/cfng/flash/clipfish_player_3.swf?as=0&r=1&noad=1&fs=1&vid=' . $videoId;
216 }
217 return $url;
218 }
219
220 /**
221 * Parse google url
222 *
223 * @param string $url
224 * @return string processed url
225 */
226 protected function process_google($url) {
227 if (preg_match('/docid=([^(\\&|$)]*)/', $url, $matches)) {
228 $videoId = $matches[1];
229 $url = $this->getUrlSchema() . 'video.google.com/googleplayer.swf?docid=' . $videoId;
230 }
231 return $url;
232 }
233
234 /**
235 * Parse metacafe url
236 *
237 * @param string $url
238 * @return string processed url
239 */
240 protected function process_metacafe($url) {
241 if (preg_match('/watch([^(\\&|$)]*)/', $url, $matches)) {
242 $parts = explode('/', $matches[1]);
243 $videoId = $parts[1];
244 $url = 'http://www.metacafe.com/fplayer/' . $videoId . '/.swf';
245 }
246 return $url;
247 }
248
249 /**
250 * Parse myvideo url
251 *
252 * @param string $url
253 * @return string processed url
254 */
255 protected function process_myvideo($url) {
256 preg_match('/watch([^(\\&|$)]*)/', $url, $matches);
257 $parts = explode('/', $matches[1]);
258 $videoId = $parts[1];
259 return $this->getUrlSchema() . 'www.myvideo.de/movie/' . $videoId . '/';
260 }
261
262 /**
263 * Parse liveleak url
264 *
265 * @param string $url
266 * @return string processed url
267 */
268 protected function process_liveleak($url) {
269 preg_match('/i=([^(\\&|$)]*)/', $url, $matches);
270 $videoId = $matches[1];
271 return 'http://www.liveleak.com/e/' . $videoId;
272 }
273
274 /**
275 * Parse veoh url
276 *
277 * @param string $url
278 * @return string processed url
279 */
280 protected function process_veoh($url) {
281 preg_match('/watch\\/([^(\\&|$)]*)/', $url, $matches);
282 $videoId = $matches[1];
283 return 'http://www.veoh.com/static/swf/webplayer/WebPlayer.swf?version=AFrontend.5.5.2.1001&permalinkId=' . $videoId;
284 }
285
286 /**
287 * Get the correct url schema
288 *
289 * @return string
290 */
291 protected function getUrlSchema() {
292 return \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
293 }
294
295 }
296
297
298 ?>