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
00108 if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00109 throw new oxException( 'EXCEPTION_NOFILE' );
00110 }
00111
00112
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
00129 if ( $sConfigValue && $sConfigValue[0] == DIR_SEP) {
00130 return $sConfigValue;
00131 }
00132
00133
00134 if ( $sConfigValue ) {
00135 $sPath = getShopBasePath() . DIR_SEP . $sConfigValue;
00136 return $sPath;
00137 }
00138
00139
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
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
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 = oxUtils::getInstance();
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
00325
00326 $oUtils->setHeader("Pragma: public");
00327 $oUtils->setHeader("Expires: 0");
00328 $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
00329 $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
00330 $oUtils->setHeader("Content-Type: application/octet-stream");
00331 if ($iFilesize) {
00332 $oUtils->setHeader("Content-Length: " . $iFilesize);
00333 }
00334 readfile( $sFileLocations );
00335 $oUtils->showMessageAndExit( null );
00336 }
00337
00343 public function hasValidDownloads()
00344 {
00345 if ( $this->_blHasValidDownloads == null ) {
00346 $this->_blHasValidDownloads = false;
00347 $sNow = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00348 $sFileId = $this->getId();
00349
00350 $oDb = oxDb::getDb();
00351
00352 $sSql = "SELECT
00353 `oxorderfiles`.`oxid`
00354 FROM `oxorderfiles`
00355 LEFT JOIN `oxorderarticles` ON `oxorderarticles`.`oxid` = `oxorderfiles`.`oxorderarticleid`
00356 LEFT JOIN `oxorder` ON `oxorder`.`oxid` = `oxorderfiles`.`oxorderid`
00357 WHERE `oxorderfiles`.`oxfileid` = " . $oDb->quote($sFileId) . "
00358 AND ( ! `oxorderfiles`.`oxmaxdownloadcount` OR `oxorderfiles`.`oxmaxdownloadcount` > `oxorderfiles`.`oxdownloadcount`)
00359 AND ( `oxorderfiles`.`oxvaliduntil` = '0000-00-00 00:00:00' OR `oxorderfiles`.`oxvaliduntil` > '{$sNow}' )
00360 AND `oxorder`.`oxstorno` = 0
00361 AND `oxorderarticles`.`oxstorno` = 0";
00362
00363 if ( $oDb->getOne( $sSql ) ) {
00364 $this->_blHasValidDownloads = true;
00365 }
00366 }
00367 return $this->_blHasValidDownloads;
00368 }
00369
00375 public function getMaxDownloadsCount()
00376 {
00377 $iMaxCount = $this->oxfiles__oxmaxdownloads->value;
00378
00379 if ( $iMaxCount < 0) {
00380 $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCount" );
00381 }
00382 return $iMaxCount;
00383 }
00384
00390 public function getMaxUnregisteredDownloadsCount()
00391 {
00392 $iMaxCount = $this->oxfiles__oxmaxunregdownloads->value;
00393
00394 if ( $iMaxCount < 0) {
00395 $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCountUnregistered" );
00396 }
00397 return $iMaxCount;
00398 }
00399
00405 public function getLinkExpirationTime()
00406 {
00407 $iExpTime = $this->oxfiles__oxlinkexptime->value;
00408
00409 if ( $iExpTime < 0) {
00410 $iExpTime = $this->getConfig()->getConfigParam( "iLinkExpirationTime" );
00411 }
00412 return $iExpTime;
00413 }
00414
00420 public function getDownloadExpirationTime()
00421 {
00422 $iExpTime = $this->oxfiles__oxdownloadexptime->value;
00423
00424 if ( $iExpTime < 0) {
00425 $iExpTime = $this->getConfig()->getConfigParam( "iDownloadExpirationTime" );
00426 }
00427 return $iExpTime;
00428 }
00429
00435 public function getSize()
00436 {
00437 $iSize = null;
00438 $sFilename = $this->getStoreLocation();
00439 if ( file_exists( $sFilename ) ) {
00440 $iSize = filesize( $sFilename );
00441 }
00442 return $iSize;
00443 }
00444
00445 }