Typo3 eID mechanizmus
Zadanie
Popíšem využitia eID v mojom module rtg_files. Modul zobrazuje zoznam dokumentov alebo odkazov, po kliknutí na položku sa stiahne vybraný dokument alebo presmeruje na odkaz. Pomocou funkcie eID obchádzam kompletný "cirkus" s tvorbou Typo3 TSFE pre rendering frontendu, pretože je zbytočný, a taktiež sa tým rieši problém s cache. Potrebujem len poslať súbor cez HTTP protokol z Typo3 k užívateľovi.
Riešenie
Registrácia eID procesu/triedy.
ext_localconf.php
$TYPO3_CONF_VARS['FE']['eID_include']['tx_rtgfiles_download'] = '
EXT:rtg_files/lib/class.tx_rtgfiles_download.php
';
Tvorba odkazu na eID, pole $item obsahuje údaje o položke (uid - ID, title,- názov), $this->id je uid aktuálnej stránky.
class.tx_rtgfiles_pi1.php
$params = array(
'eID' => 'tx_rtgfiles_download',
$this->prefixId.'[uid]' => intval( $item['uid'] )
);
$link = '<a href="'.$this->pi_getPageLink( $this->id, '_self', $params ).'" title="'.$item['title'].'">';
$wrappedSubpart['###FILE_DOWNLOAD###'] = array( $link, '</a>' );
Spracovanie volania eID triedou tx_rtgfiles_download. Najprv sa vytvoria potrebné triedy (pripojenie k databáze), potom funkcia main podľa GET parameta uid vyhľadá v databáze príslušný záznam/súbor, inkrementuje sa počet jeho stiahnutí, pokiaľ je zadaná URL, vykoná sa presmerovanie ak je to súbor, tak jeho obsah sa odošle klientovi PHP funkciou readfile. Na konci súboru nesmieme zabudnúť manuálne vytvoriť triedu tx_rtgfiles_download, pretože pri volaní cez eID mechanizmus sa toto nedeje automaticky (objekt $SOBE).
class.tx_rtgfiles_download.php (skrátené)
if( !defined ('PATH_typo3conf') ) die ( 'Could not access this script directly!' );
require_once( t3lib_extMgm::extPath( 'lang', 'lang.php' ) );
require_once( t3lib_extMgm::extPath( 'cms', 'tslib/class.tslib_content.php' ) );
require_once( PATH_t3lib . 'class.t3lib_page.php' );
class tx_rtgfiles_download {
var $prefixId = 'tx_rtgfiles_pi1';
var $GPvars;
var $filesPath = 'uploads/tx_rtgfiles/';
var $file;
public function __construct() {
$feUserObj = tslib_eidtools::initFeUser(); // Initialize FE user object
tslib_eidtools::connectDB();
$this->GPvars = t3lib_div::_GP( $this->prefixId );
$this->GPvars['uid'] = intval( $this->GPvars['uid'] );
if( $this->GPvars['uid'] < 1 ) {
die( 'ERROR FILE DOWNLOAD!' );
}
}
public function main() {
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery( 'uid,file,url,clicks', 'tx_rtgfiles_files', 'uid = '.$this->GPvars['uid'].' AND deleted=0 AND hidden=0' );
if( $res ) {
if( $this->file = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
if( file_exists( $this->filesPath.$this->file['file'] ) ) {
// Update clicks
$values = array( 'clicks' => intval( $this->file['clicks'] ) + 1 );
$GLOBALS['TYPO3_DB']->exec_UPDATEquery( 'tx_rtgfiles_files', 'uid = '.$this->GPvars['uid'], $values );
// Redirect to external URL
if( $this->file['url'] != '' ) {
header( 'Location: '.$this->file['url'] );
exit;
}
// Set MIME type by file extension
$ext = strtolower( substr( ( $t = strrchr( $data['file'], '.' ) ) !== false ? $t : '' ,1 ) );
switch( $ext ) {
case 'pdf': $ctype = 'application/pdf'; break;
case 'exe': $ctype = 'application/octet-stream'; break;
case 'zip': $ctype = 'application/zip'; break;
...
}
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Type: '.$ctype );
// File attachment
header( "Content-Disposition: attachment; filename=".basename( $this->file['file'] ).";" );
header( "Content-Transfer-Encoding: binary" );
header( "Content-Length: ".filesize( $this->filesPath.$this->file['file'] ) );
readfile( $this->filesPath.$this->file['file'] );
exit;
}
}
}
die( 'ERROR FILE DOWNLOAD!!' );
}
}
// Make instance:
$SOBE = t3lib_div::makeInstance( 'tx_rtgfiles_download' );
$SOBE->main();
Generátor typoscriptu
Vytvára typoscript pre bežné použitie. Generátor je jednoduchý na obsluhu, stačí vyplniť zopár položiek formulára.