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