oxfile.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxFile extends oxBase
00008 {
00012     const NO_USER = 2;
00013 
00019     protected $_sCoreTable = 'oxfiles';
00020 
00026     protected $_sClassName = 'oxfile';
00027 
00033     protected $_sRelativeFilePath = null;
00034 
00040     protected $_blIsPaid = null;
00041 
00048     protected $_sDownloadLink = null;
00049 
00055     protected $_blHasValidDownloads = null;
00056 
00062     protected $_sManualUploadDir = "uploads";
00063 
00069     public function __construct()
00070     {
00071         parent::__construct();
00072         $this->init();
00073     }
00074 
00082     public function processFile( $sFileIndex )
00083     {
00084         $aFileInfo = $this->getConfig()->getUploadedFile( $sFileIndex );
00085 
00086         $this->_checkArticleFile( $aFileInfo );
00087 
00088         $sFileHash = $this->_getFileHash( $aFileInfo['tmp_name'] );
00089         $this->oxfiles__oxstorehash = new oxField( $sFileHash, oxField::T_RAW );
00090         $sUploadTo = $this->getStoreLocation();
00091 
00092         if ( !$this->_uploadFile( $aFileInfo['tmp_name'], $sUploadTo ) ) {
00093             throw new oxException( 'EXCEPTION_COULDNOTWRITETOFILE' );
00094         }
00095 
00096     }
00097 
00105     protected function _checkArticleFile( $aFileInfo )
00106     {
00107         //checking params
00108         if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00109             throw new oxException( 'EXCEPTION_NOFILE' );
00110         }
00111 
00112         // error uploading file ?
00113         if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00114             throw new oxException( 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00115         }
00116 
00117     }
00118 
00124     protected function _getBaseDownloadDirPath()
00125     {
00126         $sConfigValue = oxRegistry::getConfig()->getConfigParam( 'sDownloadsDir' );
00127 
00128         //Unix full path is set
00129         if ( $sConfigValue && $sConfigValue[0] == DIR_SEP) {
00130             return $sConfigValue;
00131         }
00132 
00133         //relative path is set
00134         if ( $sConfigValue ) {
00135             $sPath = getShopBasePath() . DIR_SEP . $sConfigValue;
00136             return $sPath;
00137         }
00138 
00139         //no path is set
00140         $sPath = getShopBasePath() . "/out/downloads/";
00141         return $sPath;
00142     }
00143 
00151     public function getStoreLocation()
00152     {
00153         $sPath  = $this->_getBaseDownloadDirPath();
00154         $sPath .= DIR_SEP .  $this->_getFileLocation();
00155         return $sPath;
00156     }
00157 
00163     protected function _getFileLocation()
00164     {
00165         $this->_sRelativeFilePath = '';
00166         $sFileHash = $this->oxfiles__oxstorehash->value;
00167         $sFileName = $this->oxfiles__oxfilename->value;
00168 
00169         //security check for demo shops
00170         if ($this->getConfig()->isDemoShop()) {
00171             $sFileName = basename($sFileName);
00172         }
00173 
00174         if ($this->isUploaded()) {
00175             $this->_sRelativeFilePath  = $this->_getHashedFileDir($sFileHash);
00176             $this->_sRelativeFilePath .= DIR_SEP .  $sFileHash;
00177         } else {
00178             $this->_sRelativeFilePath = DIR_SEP . $this->_sManualUploadDir . DIR_SEP . $sFileName;
00179         }
00180 
00181         return $this->_sRelativeFilePath;
00182     }
00183 
00193     protected function _getHashedFileDir( $sFileHash )
00194     {
00195         $sDir = substr($sFileHash, 0, 2);
00196         $sAbsDir = $this->_getBaseDownloadDirPath() . DIR_SEP . $sDir;
00197 
00198         if (!is_dir($sAbsDir)) {
00199             mkdir($sAbsDir, 0755);
00200         }
00201         return $sDir;
00202     }
00203 
00212     protected function _getFileHash( $sFileName )
00213     {
00214         return md5_file( $sFileName );
00215     }
00216 
00226     protected function _uploadFile( $sSource, $sTarget )
00227     {
00228         $blDone = false;
00229         $blDone = move_uploaded_file( $sSource, $sTarget );
00230 
00231         if ( $blDone ) {
00232             $blDone = @chmod( $sTarget, 0644 );
00233         }
00234 
00235         return $blDone;
00236     }
00237 
00246     public function isUploaded()
00247     {
00248         $blHashed = false;
00249         if ($this->oxfiles__oxstorehash->value) {
00250             $blHashed = true;
00251         }
00252         return $blHashed;
00253     }
00254 
00262     public function delete( $sOXID = null )
00263     {
00264         $sOXID = $sOXID ? $sOXID : $this->getId();
00265 
00266         $this->load($sOXID);
00267         // if record cannot be delete, abort deletion
00268         if ($blDeleted = parent::delete( $sOXID ) ) {
00269             $this->_deleteFile( );
00270         }
00271 
00272         return $blDeleted;
00273     }
00274 
00281     protected function _deleteFile()
00282     {
00283         if (!$this->isUploaded()) {
00284             return false;
00285         }
00286         $sHash = $this->oxfiles__oxstorehash->value;
00287         $oDb = oxDb::getDb();
00288         $iCount = $oDb->getOne(
00289             'SELECT COUNT(*) FROM `oxfiles` WHERE `OXSTOREHASH` = ' . $oDb->quote( $sHash ), false, false );
00290         if (!$iCount) {
00291             $sPath  = $this->getStoreLocation();
00292             unlink($sPath);
00293         }
00294     }
00295 
00302     protected function _getFilenameForUrl()
00303     {
00304         return rawurlencode($this->oxfiles__oxfilename->value);
00305     }
00306 
00312     public function download()
00313     {
00314         $oUtils = oxRegistry::getUtils();
00315         $sFileName = $this->_getFilenameForUrl();
00316         $sFileLocations = $this->getStoreLocation();
00317 
00318         if (!file_exists($sFileLocations)) {
00319             throw new oxException( 'EXCEPTION_NOFILE' );
00320         }
00321 
00322         $iFilesize = filesize($sFileLocations);
00323 
00324         $oUtils->setHeader("Pragma: public");
00325         $oUtils->setHeader("Expires: 0");
00326         $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
00327         $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
00328         $oUtils->setHeader("Content-Type: application/octet-stream");
00329         if ($iFilesize) {
00330             $oUtils->setHeader("Content-Length: " . $iFilesize);
00331         }
00332         readfile( $sFileLocations );
00333         $oUtils->showMessageAndExit( null );
00334     }
00335 
00341     public function hasValidDownloads()
00342     {
00343         if ( $this->_blHasValidDownloads == null ) {
00344             $this->_blHasValidDownloads = false;
00345             $sNow   = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00346             $sFileId = $this->getId();
00347 
00348             $oDb = oxDb::getDb();
00349 
00350             $sSql = "SELECT
00351                         `oxorderfiles`.`oxid`
00352                      FROM `oxorderfiles`
00353                         LEFT JOIN `oxorderarticles` ON `oxorderarticles`.`oxid` = `oxorderfiles`.`oxorderarticleid`
00354                         LEFT JOIN `oxorder` ON `oxorder`.`oxid` = `oxorderfiles`.`oxorderid`
00355                      WHERE `oxorderfiles`.`oxfileid` = " . $oDb->quote($sFileId) . "
00356                         AND ( ! `oxorderfiles`.`oxmaxdownloadcount` OR `oxorderfiles`.`oxmaxdownloadcount` > `oxorderfiles`.`oxdownloadcount`)
00357                         AND ( `oxorderfiles`.`oxvaliduntil` = '0000-00-00 00:00:00' OR `oxorderfiles`.`oxvaliduntil` > '{$sNow}' )
00358                         AND `oxorder`.`oxstorno` = 0
00359                         AND `oxorderarticles`.`oxstorno` = 0";
00360 
00361             if ( $oDb->getOne( $sSql ) ) {
00362                 $this->_blHasValidDownloads = true;
00363             }
00364         }
00365         return $this->_blHasValidDownloads;
00366     }
00367 
00373     public function getMaxDownloadsCount()
00374     {
00375         $iMaxCount = $this->oxfiles__oxmaxdownloads->value;
00376         //if value is -1, takes global options
00377         if ( $iMaxCount < 0) {
00378             $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCount" );
00379         }
00380         return $iMaxCount;
00381     }
00382 
00388     public function getMaxUnregisteredDownloadsCount()
00389     {
00390         $iMaxCount = $this->oxfiles__oxmaxunregdownloads->value;
00391         //if value is -1, takes global options
00392         if ( $iMaxCount < 0) {
00393             $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCountUnregistered" );
00394         }
00395         return $iMaxCount;
00396     }
00397 
00403     public function getLinkExpirationTime()
00404     {
00405         $iExpTime = $this->oxfiles__oxlinkexptime->value;
00406         //if value is -1, takes global options
00407         if ( $iExpTime < 0) {
00408             $iExpTime = $this->getConfig()->getConfigParam( "iLinkExpirationTime" );
00409         }
00410         return $iExpTime;
00411     }
00412 
00418     public function getDownloadExpirationTime()
00419     {
00420         $iExpTime = $this->oxfiles__oxdownloadexptime->value;
00421         //if value is -1, takes global options
00422         if ( $iExpTime < 0) {
00423             $iExpTime = $this->getConfig()->getConfigParam( "iDownloadExpirationTime" );
00424         }
00425         return $iExpTime;
00426     }
00427 
00433     public function getSize()
00434     {
00435         $iSize = null;
00436         $sFilename = $this->getStoreLocation();
00437         if ( file_exists( $sFilename ) ) {
00438             $iSize = filesize( $sFilename );
00439         }
00440         return $iSize;
00441     }
00442 
00443 }