TYPO3 API  SVNRelease
class.tslib_mediawizardcoreprovider.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Aishwara M.B. (aishu.moorthy@gmail.com)
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 /**
00028  * Contains an implementation of the mediaWizardProvider supporting some
00029  * well known providers.
00030  *
00031  * $Id: $
00032 
00033  * @author  Aishwara M.B.<aishu.moorthy@gmail.com>
00034  * @author  Steffen Kamper <info@sk-typo3.de>
00035  * @author  Ernesto Baschny <ernst@cron-it.de>
00036  */
00037 
00038 
00039 class tslib_mediaWizardCoreProvider implements tslib_mediaWizardProvider {
00040 
00041     /**
00042      * @var array List of providers we can handle in this class
00043      */
00044     protected $providers = array(
00045         'youtube',
00046         'dailymotion',
00047         'sevenload',
00048         'vimeo',
00049         'clipfish',
00050         'google',
00051         'metacafe',
00052         'myvideo',
00053         'liveleak',
00054         'veoh'
00055     );
00056 
00057     /**
00058      * Checks if we have a valid method for processing a given URL.
00059      *
00060      * This is done by analysing the hostname of the URL and checking if it contains
00061      * any of our provider tags defined in $this->providers.
00062      *
00063      * @param  $url
00064      * @return string
00065      */
00066     protected function getMethod($url) {
00067         $urlInfo = @parse_url($url);
00068         if ($urlInfo === FALSE) {
00069             return NULL;
00070         }
00071         $hostName = t3lib_div::trimExplode('.', $urlInfo['host'], TRUE);
00072         foreach ($this->providers as $provider) {
00073             $functionName = 'process_' . $provider;
00074             if (in_array($provider, $hostName) && is_callable(array($this, $functionName))) {
00075                 return $functionName;
00076             }
00077         }
00078         return NULL;
00079     }
00080 
00081     /***********************************************
00082      *
00083      * Implementation of tslib_mediaWizardProvider
00084      *
00085      ***********************************************/
00086 
00087     /**
00088      * @param  $url
00089      * @return bool
00090      * @see tslib_mediaWizardProvider::canHandle
00091      */
00092     public function canHandle($url) {
00093         return ($this->getMethod($url) !== NULL);
00094     }
00095 
00096     /**
00097      * @param  $url URL to rewrite
00098      * @return string The rewritten URL
00099      * @see tslib_mediaWizardProvider::rewriteUrl
00100      */
00101     public function rewriteUrl($url) {
00102         $method = $this->getMethod($url);
00103         return $this->$method($url);
00104     }
00105 
00106     /***********************************************
00107      *
00108      * Providers URL rewriting:
00109      *
00110      ***********************************************/
00111 
00112     /**
00113      * Parse youtube url
00114      *
00115      * @param string $url
00116      * @return string processed url
00117      */
00118     protected function process_youtube($url) {
00119         $videoId = '';
00120         if (strpos($url, '/user/') !== FALSE) {
00121                 // it's a channel
00122             $parts = explode('/', $url);
00123             $videoId = $parts[count($parts) - 1];
00124         } else if (preg_match('/v=([^(\&|$)]*)/', $url, $matches)) {
00125             $videoId = $matches[1];
00126         }
00127 
00128         if ($videoId) {
00129             $url = 'http://www.youtube.com/v/' . $videoId . '?fs=1';
00130         }
00131         return $url;
00132     }
00133 
00134     /**
00135      * Parse dailymotion url
00136      *
00137      * @param string $url
00138      * @return string processed url
00139      */
00140     protected function process_dailymotion($url) {
00141         $parts = explode('video/', $url);
00142         $videoId = $parts[1];
00143         if (strpos($videoId, '/') !== FALSE) {
00144             $videoId = substr($videoId, 0, strpos($videoId, '/'));
00145         }
00146         return 'http://www.dailymotion.com/swf/' . $videoId;
00147     }
00148 
00149     /**
00150      * Parse sevenload url
00151      *
00152      * @param string $url
00153      * @return string processed url and preview image
00154      */
00155     protected function process_sevenload($url) {
00156         $parts = explode('/', $url);
00157         $videoId = $parts[count($parts) - 1];
00158         if (strpos($videoId, '-') !== FALSE) {
00159             $videoId = substr($videoId, 0, strpos($videoId, '-'));
00160         }
00161         return 'http://de.sevenload.com/pl/' . $videoId . '/400x500/swf';
00162     }
00163 
00164     /**
00165      * Parse vimeo url
00166      *
00167      * Supports:
00168      * - http://vimeo.com/hd#<id>
00169      * - http://vimeo.com/<id>
00170      * - http://player.vimeo.com/video/<id>
00171      *
00172      * @param string $url
00173      * @return string processed url
00174      */
00175     protected function process_vimeo($url) {
00176         if (preg_match('/[\/#](\d+)$/', $url, $matches)) {
00177             $videoId = $matches[1];
00178             $url = 'http://vimeo.com/moogaloop.swf?&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&fullscreen=1&clip_id=' .
00179                 $videoId;
00180         }
00181         return $url;
00182     }
00183 
00184     /**
00185      * Parse clipfish url
00186      *
00187      * @param string $url
00188      * @return string processed url
00189      */
00190     protected function process_clipfish($url) {
00191         if (preg_match('/video([^(\&|$)]*)/', $url, $matches)) {
00192             $parts = explode('/', $matches[1]);
00193             $videoId = $parts[1];
00194             $url = 'http://www.clipfish.de/cfng/flash/clipfish_player_3.swf?as=0&r=1&noad=1&fs=1&vid=' . $videoId;
00195         }
00196         return $url;
00197     }
00198 
00199 
00200     /**
00201      * Parse google url
00202      *
00203      * @param string $url
00204      * @return string processed url
00205      */
00206     protected function process_google($url) {
00207         if (preg_match('/docid=([^(\&|$)]*)/', $url, $matches)) {
00208             $videoId = $matches[1];
00209             $url = 'http://video.google.com/googleplayer.swf?docid=' . $videoId;
00210         }
00211         return $url;
00212     }
00213 
00214     /**
00215      * Parse metacafe url
00216      *
00217      * @param string $url
00218      * @return string processed url
00219      */
00220 
00221     protected function process_metacafe($url) {
00222         if (preg_match('/watch([^(\&|$)]*)/', $url, $matches)) {
00223             $parts = explode('/', $matches[1]);
00224             $videoId = $parts[1];
00225             $url = 'http://www.metacafe.com/fplayer/' . $videoId . '/.swf';
00226         }
00227         return $url;
00228     }
00229 
00230     /**
00231      * Parse myvideo url
00232      *
00233      * @param string $url
00234      * @return string processed url
00235      */
00236 
00237     protected function process_myvideo($url) {
00238         preg_match('/watch([^(\&|$)]*)/', $url, $matches);
00239         $parts = explode('/', $matches[1]);
00240         $videoId = $parts[1];
00241 
00242         return 'http://www.myvideo.de/movie/' . $videoId . '/';
00243     }
00244 
00245 
00246     /**
00247      * Parse liveleak url
00248      *
00249      * @param string $url
00250      * @return string processed url
00251      */
00252     protected function process_liveleak($url) {
00253         preg_match('/i=([^(\&|$)]*)/', $url, $matches);
00254         $videoId = $matches[1];
00255 
00256         return 'http://www.liveleak.com/e/' . $videoId;
00257     }
00258 
00259     /**
00260      * Parse veoh url
00261      *
00262      * @param string $url
00263      * @return string processed url
00264      */
00265     protected function process_veoh($url) {
00266         preg_match('/watch\/([^(\&|$)]*)/', $url, $matches);
00267         $videoId = $matches[1];
00268 
00269         return 'http://www.veoh.com/static/swf/webplayer/WebPlayer.swf?version=AFrontend.5.5.2.1001&permalinkId=' . $videoId;
00270     }
00271 
00272 }
00273 
00274 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/class.tslib_mediawizardcoreprovider.php'])) {
00275     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/class.tslib_mediawizardcoreprovider.php']);
00276 }
00277 
00278 ?>