[FEATURE] Add hook for TSFE fetch_the_id() post processing
[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 /**
29 * Contains an implementation of the mediaWizardProvider supporting some
30 * well known providers.
31 *
32 * @author Aishwara M.B.<aishu.moorthy@gmail.com>
33 * @author Steffen Kamper <info@sk-typo3.de>
34 * @author Ernesto Baschny <ernst@cron-it.de>
35 */
36 class tslib_mediaWizardCoreProvider implements tslib_mediaWizardProvider {
37
38 /**
39 * @var array List of providers we can handle in this class
40 */
41 protected $providers = array(
42 'youtube',
43 'youtu',
44 'dailymotion',
45 'sevenload',
46 'vimeo',
47 'clipfish',
48 'google',
49 'metacafe',
50 'myvideo',
51 'liveleak',
52 'veoh'
53 );
54
55 /**
56 * Checks if we have a valid method for processing a given URL.
57 *
58 * This is done by analysing the hostname of the URL and checking if it contains
59 * any of our provider tags defined in $this->providers.
60 *
61 * @param string $url
62 * @return string
63 */
64 protected function getMethod($url) {
65 $urlInfo = @parse_url($url);
66 if ($urlInfo === FALSE) {
67 return NULL;
68 }
69 $hostName = t3lib_div::trimExplode('.', $urlInfo['host'], TRUE);
70 foreach ($this->providers as $provider) {
71 $functionName = 'process_' . $provider;
72 if (in_array($provider, $hostName) && is_callable(array($this, $functionName))) {
73 return $functionName;
74 }
75 }
76 return NULL;
77 }
78
79 /***********************************************
80 *
81 * Implementation of tslib_mediaWizardProvider
82 *
83 ***********************************************/
84
85 /**
86 * @param string $url
87 * @return boolean
88 * @see tslib_mediaWizardProvider::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 tslib_mediaWizardProvider::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 /**
111 * Parse youtube url
112 *
113 * @param string $url
114 * @return string processed url
115 */
116 protected function process_youtube($url) {
117 $videoId = '';
118 if (strpos($url, '/user/') !== FALSE) {
119 // it's a channel
120 $parts = explode('/', $url);
121 $videoId = $parts[count($parts) - 1];
122 } elseif (preg_match('/(v=|v\/|.be\/)([^(\&|$)]*)/', $url, $matches)) {
123 $videoId = $matches[2];
124 }
125
126 if ($videoId) {
127 $url = 'http://www.youtube.com/v/' . $videoId . '?fs=1';
128 }
129 return $url;
130 }
131
132 /**
133 * Parse youtube short url
134 *
135 * @param string $url
136 * @return string processed url
137 */
138 protected function process_youtu($url) {
139 return $this->process_youtube($url);
140 }
141
142 /**
143 * Parse dailymotion url
144 *
145 * @param string $url
146 * @return string processed url
147 */
148 protected function process_dailymotion($url) {
149 $parts = explode('video/', $url);
150 $videoId = $parts[1];
151 if (strpos($videoId, '/') !== FALSE) {
152 $videoId = substr($videoId, 0, strpos($videoId, '/'));
153 }
154 return 'http://www.dailymotion.com/swf/' . $videoId;
155 }
156
157 /**
158 * Parse sevenload url
159 *
160 * @param string $url
161 * @return string processed url and preview image
162 */
163 protected function process_sevenload($url) {
164 $parts = explode('/', $url);
165 $videoId = $parts[count($parts) - 1];
166 if (strpos($videoId, '-') !== FALSE) {
167 $videoId = substr($videoId, 0, strpos($videoId, '-'));
168 }
169 return 'http://de.sevenload.com/pl/' . $videoId . '/400x500/swf';
170 }
171
172 /**
173 * Parse vimeo url
174 *
175 * Supports:
176 * - http://vimeo.com/hd#<id>
177 * - http://vimeo.com/<id>
178 * - http://player.vimeo.com/video/<id>
179 *
180 * @param string $url
181 * @return string processed url
182 */
183 protected function process_vimeo($url) {
184 if (preg_match('/[\/#](\d+)$/', $url, $matches)) {
185 $videoId = $matches[1];
186 $url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $videoId . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&fullscreen=1';
187 }
188 return $url;
189 }
190
191 /**
192 * Parse clipfish url
193 *
194 * @param string $url
195 * @return string processed url
196 */
197 protected function process_clipfish($url) {
198 if (preg_match('/video([^(\&|$)]*)/', $url, $matches)) {
199 $parts = explode('/', $matches[1]);
200 $videoId = $parts[1];
201 $url = 'http://www.clipfish.de/cfng/flash/clipfish_player_3.swf?as=0&r=1&noad=1&fs=1&vid=' . $videoId;
202 }
203 return $url;
204 }
205
206 /**
207 * Parse google url
208 *
209 * @param string $url
210 * @return string processed url
211 */
212 protected function process_google($url) {
213 if (preg_match('/docid=([^(\&|$)]*)/', $url, $matches)) {
214 $videoId = $matches[1];
215 $url = 'http://video.google.com/googleplayer.swf?docid=' . $videoId;
216 }
217 return $url;
218 }
219
220 /**
221 * Parse metacafe url
222 *
223 * @param string $url
224 * @return string processed url
225 */
226
227 protected function process_metacafe($url) {
228 if (preg_match('/watch([^(\&|$)]*)/', $url, $matches)) {
229 $parts = explode('/', $matches[1]);
230 $videoId = $parts[1];
231 $url = 'http://www.metacafe.com/fplayer/' . $videoId . '/.swf';
232 }
233 return $url;
234 }
235
236 /**
237 * Parse myvideo url
238 *
239 * @param string $url
240 * @return string processed url
241 */
242
243 protected function process_myvideo($url) {
244 preg_match('/watch([^(\&|$)]*)/', $url, $matches);
245 $parts = explode('/', $matches[1]);
246 $videoId = $parts[1];
247
248 return 'http://www.myvideo.de/movie/' . $videoId . '/';
249 }
250
251 /**
252 * Parse liveleak url
253 *
254 * @param string $url
255 * @return string processed url
256 */
257 protected function process_liveleak($url) {
258 preg_match('/i=([^(\&|$)]*)/', $url, $matches);
259 $videoId = $matches[1];
260
261 return 'http://www.liveleak.com/e/' . $videoId;
262 }
263
264 /**
265 * Parse veoh url
266 *
267 * @param string $url
268 * @return string processed url
269 */
270 protected function process_veoh($url) {
271 preg_match('/watch\/([^(\&|$)]*)/', $url, $matches);
272 $videoId = $matches[1];
273
274 return 'http://www.veoh.com/static/swf/webplayer/WebPlayer.swf?version=AFrontend.5.5.2.1001&permalinkId=' . $videoId;
275 }
276
277 }
278 ?>