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
00107 if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00108 throw new oxException( 'EXCEPTION_NOFILE' );
00109 }
00110
00111
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
00128 if ( $sConfigValue && $sConfigValue[0] == DIR_SEP) {
00129 return $sConfigValue;
00130 }
00131
00132
00133 if ( $sConfigValue ) {
00134 $sPath = getShopBasePath() . DIR_SEP . $sConfigValue;
00135 return $sPath;
00136 }
00137
00138
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
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 = move_uploaded_file( $sSource, $sTarget );
00228
00229 if ( $blDone ) {
00230 $blDone = @chmod( $sTarget, 0644 );
00231 }
00232
00233 return $blDone;
00234 }
00235
00244 public function isUploaded()
00245 {
00246 $blHashed = false;
00247 if ($this->oxfiles__oxstorehash->value) {
00248 $blHashed = true;
00249 }
00250 return $blHashed;
00251 }
00252
00260 public function delete( $sOxId = null )
00261 {
00262 $sOxId = $sOxId ? $sOxId : $this->getId();
00263
00264 $this->load($sOxId);
00265
00266 if ($blDeleted = parent::delete( $sOxId ) ) {
00267 $this->_deleteFile( );
00268 }
00269
00270 return $blDeleted;
00271 }
00272
00279 protected function _deleteFile()
00280 {
00281 if (!$this->isUploaded()) {
00282 return false;
00283 }
00284 $sHash = $this->oxfiles__oxstorehash->value;
00285 $oDb = oxDb::getDb();
00286 $iCount = $oDb->getOne(
00287 'SELECT COUNT(*) FROM `oxfiles` WHERE `OXSTOREHASH` = ' . $oDb->quote( $sHash ), false, false );
00288 if (!$iCount) {
00289 $sPath = $this->getStoreLocation();
00290 unlink($sPath);
00291 }
00292 }
00293
00300 protected function _getFilenameForUrl()
00301 {
00302 return rawurlencode($this->oxfiles__oxfilename->value);
00303 }
00304
00308 public function download()
00309 {
00310 $oUtils = oxRegistry::getUtils();
00311 $sFileName = $this->_getFilenameForUrl();
00312 $sFileLocations = $this->getStoreLocation();
00313
00314 if ( !$this->exist() ) {
00315 throw new oxException( 'EXCEPTION_NOFILE' );
00316 }
00317
00318 $oUtils->setHeader("Pragma: public");
00319 $oUtils->setHeader("Expires: 0");
00320 $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
00321 $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
00322 $oUtils->setHeader("Content-Type: application/octet-stream");
00323 if ( $iFileSize = $this->getSize() ) {
00324 $oUtils->setHeader("Content-Length: " . $iFileSize);
00325 }
00326 readfile( $sFileLocations );
00327 $oUtils->showMessageAndExit( null );
00328 }
00329
00335 public function exist()
00336 {
00337 return file_exists( $this->getStoreLocation() );
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', oxRegistry::get("oxUtilsDate")->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
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
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
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
00426 if ( $iExpTime < 0) {
00427 $iExpTime = $this->getConfig()->getConfigParam( "iDownloadExpirationTime" );
00428 }
00429 return $iExpTime;
00430 }
00431
00437 public function getSize()
00438 {
00439 $iSize = 0;
00440 if ( $this->exist() ) {
00441 $iSize = filesize( $this->getStoreLocation() );
00442 }
00443 return $iSize;
00444 }
00445
00446 }