15c5d962e903f3da5c5f73573b622b7bfa57d87c
[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 * @author Aishwara M.B.<aishu.moorthy@gmail.com>
32 * @author Steffen Kamper <info@sk-typo3.de>
33 * @author Ernesto Baschny <ernst@cron-it.de>
34 */
35
36
37 class tslib_mediaWizardCoreProvider implements tslib_mediaWizardProvider {
38
39 /**
40 * @var array List of providers we can handle in this class
41 */
42 protected $providers = array(
43 'youtube',
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 $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 $url
87 * @return bool
88 * @see tslib_mediaWizardProvider::canHandle
89 */
90 public function canHandle($url) {
91 return ($this->getMethod($url) !== NULL);
92 }
93
94 /**
95 * @param $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=([^(\&|$)]*)/', $url, $matches)) {
123 $videoId = $matches[1];
124 }
125
126 if ($videoId) {
127 $url = 'http://www.youtube.com/v/' . $videoId . '?fs=1';
128 }
129 return $url;
130 }
131
132 /**
133 * Parse dailymotion url
134 *
135 * @param string $url
136 * @return string processed url
137 */
138 protected function process_dailymotion($url) {
139 $parts = explode('video/', $url);
140 $videoId = $parts[1];
141 if (strpos($videoId, '/') !== FALSE) {
142 $videoId = substr($videoId, 0, strpos($videoId, '/'));
143 }
144 return 'http://www.dailymotion.com/swf/' . $videoId;
145 }
146
147 /**
148 * Parse sevenload url
149 *
150 * @param string $url
151 * @return string processed url and preview image
152 */
153 protected function process_sevenload($url) {
154 $parts = explode('/', $url);
155 $videoId = $parts[count($parts) - 1];
156 if (strpos($videoId, '-') !== FALSE) {
157 $videoId = substr($videoId, 0, strpos($videoId, '-'));
158 }
159 return 'http://de.sevenload.com/pl/' . $videoId . '/400x500/swf';
160 }
161
162 /**
163 * Parse vimeo url
164 *
165 * Supports:
166 * - http://vimeo.com/hd#<id>
167 * - http://vimeo.com/<id>
168 * - http://player.vimeo.com/video/<id>
169 *
170 * @param string $url
171 * @return string processed url
172 */
173 protected function process_vimeo($url) {
174 if (preg_match('/[\/#](\d+)$/', $url, $matches)) {
175 $videoId = $matches[1];
176 $url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $videoId . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&fullscreen=1';
177 }
178 return $url;
179 }
180
181 /**
182 * Parse clipfish url
183 *
184 * @param string $url
185 * @return string processed url
186 */
187 protected function process_clipfish($url) {
188 if (preg_match('/video([^(\&|$)]*)/', $url, $matches)) {
189 $parts = explode('/', $matches[1]);
190 $videoId = $parts[1];
191 $url = 'http://www.clipfish.de/cfng/flash/clipfish_player_3.swf?as=0&r=1&noad=1&fs=1&vid=' . $videoId;
192 }
193 return $url;
194 }
195
196
197 /**
198 * Parse google url
199 *
200 * @param string $url
201 * @return string processed url
202 */
203 protected function process_google($url) {
204 if (preg_match('/docid=([^(\&|$)]*)/', $url, $matches)) {
205 $videoId = $matches[1];
206 $url = 'http://video.google.com/googleplayer.swf?docid=' . $videoId;
207 }
208 return $url;
209 }
210
211 /**
212 * Parse metacafe url
213 *
214 * @param string $url
215 * @return string processed url
216 */
217
218 protected function process_metacafe($url) {
219 if (preg_match('/watch([^(\&|$)]*)/', $url, $matches)) {
220 $parts = explode('/', $matches[1]);
221 $videoId = $parts[1];
222 $url = 'http://www.metacafe.com/fplayer/' . $videoId . '/.swf';
223 }
224 return $url;
225 }
226
227 /**
228 * Parse myvideo url
229 *
230 * @param string $url
231 * @return string processed url
232 */
233
234 protected function process_myvideo($url) {
235 preg_match('/watch([^(\&|$)]*)/', $url, $matches);
236 $parts = explode('/', $matches[1]);
237 $videoId = $parts[1];
238
239 return 'http://www.myvideo.de/movie/' . $videoId . '/';
240 }
241
242
243 /**
244 * Parse liveleak url
245 *
246 * @param string $url
247 * @return string processed url
248 */
249 protected function process_liveleak($url) {
250 preg_match('/i=([^(\&|$)]*)/', $url, $matches);
251 $videoId = $matches[1];
252
253 return 'http://www.liveleak.com/e/' . $videoId;
254 }
255
256 /**
257 * Parse veoh url
258 *
259 * @param string $url
260 * @return string processed url
261 */
262 protected function process_veoh($url) {
263 preg_match('/watch\/([^(\&|$)]*)/', $url, $matches);
264 $videoId = $matches[1];
265
266 return 'http://www.veoh.com/static/swf/webplayer/WebPlayer.swf?version=AFrontend.5.5.2.1001&permalinkId=' . $videoId;
267 }
268
269 }
270
271 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/class.tslib_mediawizardcoreprovider.php'])) {
272 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/class.tslib_mediawizardcoreprovider.php']);
273 }
274
275 ?>