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 = oxConfig::getInstance()->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         $blDeleted = false;
00265 
00266         $sOXID = $sOXID ? $sOXID : $this->getId();
00267 
00268         $this->load($sOXID);
00269         // if record cannot be delete, abort deletion
00270         if ( parent::delete( $sOXID ) ) {
00271             $blDeleted = $this->_deleteFile( );
00272         }
00273 
00274         return $blDeleted;
00275     }
00276 
00283     protected function _deleteFile()
00284     {
00285         if (!$this->isUploaded()) {
00286             return false;
00287         }
00288         $sHash = $this->oxfiles__oxstorehash->value;
00289         $oDb = oxDb::getDb();
00290         $iCount = $oDb->getOne(
00291             'SELECT COUNT(*) FROM `oxfiles` WHERE `OXSTOREHASH` = ' . $oDb->quote( $sHash ), false, false );
00292         if (!$iCount) {
00293             $sPath  = $this->getStoreLocation();
00294             unlink($sPath);
00295         }
00296     }
00297 
00304     protected function _getFilenameForUrl()
00305     {
00306         return rawurlencode($this->oxfiles__oxfilename->value);
00307     }
00308 
00314     public function download()
00315     {
00316         $oUtils = oxUtils::getInstance();
00317         $sFileName = $this->_getFilenameForUrl();
00318         $sFileLocations = $this->getStoreLocation();
00319 
00320         if (!file_exists($sFileLocations)) {
00321             throw new oxException( 'EXCEPTION_NOFILE' );
00322         }
00323 
00324         $iFilesize = filesize($sFileLocations);
00325 
00326         //$sMime = oxUtilsFile::getInstance()->getMimeType( $sFileLocations );
00327 
00328         $oUtils->setHeader("Pragma: public");
00329         $oUtils->setHeader("Expires: 0");
00330         $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
00331         $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
00332         $oUtils->setHeader("Content-Type: application/octet-stream");
00333         if ($iFilesize) {
00334             $oUtils->setHeader("Content-Length: " . $iFilesize);
00335         }
00336         readfile( $sFileLocations );
00337         $oUtils->showMessageAndExit( null );
00338     }
00339 
00345     public function hasValidDownloads()
00346     {
00347         if ( $this->_blHasValidDownloads == null ) {
00348             $this->_blHasValidDownloads = false;
00349             $sNow   = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00350             $sFileId = $this->getId();
00351 
00352             $oDb = oxDb::getDb();
00353 
00354             $sSql = "SELECT
00355                         `oxorderfiles`.`oxid`
00356                      FROM `oxorderfiles`
00357                         LEFT JOIN `oxorderarticles` ON `oxorderarticles`.`oxid` = `oxorderfiles`.`oxorderarticleid`
00358                         LEFT JOIN `oxorder` ON `oxorder`.`oxid` = `oxorderfiles`.`oxorderid`
00359                      WHERE `oxorderfiles`.`oxfileid` = " . $oDb->quote($sFileId) . "
00360                         AND ( ! `oxorderfiles`.`oxmaxdownloadcount` OR `oxorderfiles`.`oxmaxdownloadcount` > `oxorderfiles`.`oxdownloadcount`)
00361                         AND ( `oxorderfiles`.`oxvaliduntil` = '0000-00-00 00:00:00' OR `oxorderfiles`.`oxvaliduntil` > '{$sNow}' )
00362                         AND `oxorder`.`oxstorno` = 0
00363                         AND `oxorderarticles`.`oxstorno` = 0";
00364 
00365             if ( $oDb->getOne( $sSql ) ) {
00366                 $this->_blHasValidDownloads = true;
00367             }
00368         }
00369         return $this->_blHasValidDownloads;
00370     }
00371 
00377     public function getMaxDownloadsCount()
00378     {
00379         $iMaxCount = $this->oxfiles__oxmaxdownloads->value;
00380         //if value is -1, takes global options
00381         if ( $iMaxCount < 0) {
00382             $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCount" );
00383         }
00384         return $iMaxCount;
00385     }
00386 
00392     public function getMaxUnregisteredDownloadsCount()
00393     {
00394         $iMaxCount = $this->oxfiles__oxmaxunregdownloads->value;
00395         //if value is -1, takes global options
00396         if ( $iMaxCount < 0) {
00397             $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCountUnregistered" );
00398         }
00399         return $iMaxCount;
00400     }
00401 
00407     public function getLinkExpirationTime()
00408     {
00409         $iExpTime = $this->oxfiles__oxlinkexptime->value;
00410         //if value is -1, takes global options
00411         if ( $iExpTime < 0) {
00412             $iExpTime = $this->getConfig()->getConfigParam( "iLinkExpirationTime" );
00413         }
00414         return $iExpTime;
00415     }
00416 
00422     public function getDownloadExpirationTime()
00423     {
00424         $iExpTime = $this->oxfiles__oxdownloadexptime->value;
00425         //if value is -1, takes global options
00426         if ( $iExpTime < 0) {
00427             $iExpTime = $this->getConfig()->getConfigParam( "iDownloadExpirationTime" );
00428         }
00429         return $iExpTime;
00430     }
00431 
00437     public function getSize()
00438     {
00439         $iSize = null;
00440         $sFilename = $this->getStoreLocation();
00441         if ( file_exists( $sFilename ) ) {
00442             $iSize = filesize( $sFilename );
00443         }
00444         return $iSize;
00445     }
00446 
00447 }