oxfile.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxFile extends oxBase
00008 {
00009 
00013     const NO_USER = 2;
00014 
00020     protected $_sCoreTable = 'oxfiles';
00021 
00027     protected $_sClassName = 'oxfile';
00028 
00034     protected $_sRelativeFilePath = null;
00035 
00041     protected $_blIsPaid = null;
00042 
00049     protected $_sDownloadLink = null;
00050 
00056     protected $_blHasValidDownloads = null;
00057 
00063     protected $_sManualUploadDir = "uploads";
00064 
00069     public function __construct()
00070     {
00071         parent::__construct();
00072         $this->init();
00073     }
00074 
00080     public function processFile($sFileIndex)
00081     {
00082         $aFileInfo = $this->getConfig()->getUploadedFile($sFileIndex);
00083 
00084         $this->_checkArticleFile($aFileInfo);
00085 
00086         $sFileHash = $this->_getFileHash($aFileInfo['tmp_name']);
00087         $this->oxfiles__oxstorehash = new oxField($sFileHash, oxField::T_RAW);
00088         $sUploadTo = $this->getStoreLocation();
00089 
00090         if (!$this->_uploadFile($aFileInfo['tmp_name'], $sUploadTo)) {
00091             throw new oxException('EXCEPTION_COULDNOTWRITETOFILE');
00092         }
00093 
00094     }
00095 
00101     protected function _checkArticleFile($aFileInfo)
00102     {
00103         //checking params
00104         if (!isset($aFileInfo['name']) || !isset($aFileInfo['tmp_name'])) {
00105             throw new oxException('EXCEPTION_NOFILE');
00106         }
00107 
00108         // error uploading file ?
00109         if (isset($aFileInfo['error']) && $aFileInfo['error']) {
00110             throw new oxException('EXCEPTION_FILEUPLOADERROR_' . ((int) $aFileInfo['error']));
00111         }
00112 
00113     }
00114 
00120     protected function _getBaseDownloadDirPath()
00121     {
00122         $sConfigValue = oxRegistry::getConfig()->getConfigParam('sDownloadsDir');
00123 
00124         //Unix full path is set
00125         if ($sConfigValue && $sConfigValue[0] == DIR_SEP) {
00126             return $sConfigValue;
00127         }
00128 
00129         //relative path is set
00130         if ($sConfigValue) {
00131             $sPath = getShopBasePath() . DIR_SEP . $sConfigValue;
00132 
00133             return $sPath;
00134         }
00135 
00136         //no path is set
00137         $sPath = getShopBasePath() . "/out/downloads/";
00138 
00139         return $sPath;
00140     }
00141 
00149     public function getStoreLocation()
00150     {
00151         $sPath = $this->_getBaseDownloadDirPath();
00152         $sPath .= DIR_SEP . $this->_getFileLocation();
00153 
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 
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 = 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 
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         );
00291         if (!$iCount) {
00292             $sPath = $this->getStoreLocation();
00293             unlink($sPath);
00294         }
00295     }
00296 
00303     protected function _getFilenameForUrl()
00304     {
00305         return rawurlencode($this->oxfiles__oxfilename->value);
00306     }
00307 
00311     public function download()
00312     {
00313         $oUtils = oxRegistry::getUtils();
00314         $sFileName = $this->_getFilenameForUrl();
00315         $sFileLocations = $this->getStoreLocation();
00316 
00317         if (!$this->exist()) {
00318             throw new oxException('EXCEPTION_NOFILE');
00319         }
00320 
00321         $oUtils->setHeader("Pragma: public");
00322         $oUtils->setHeader("Expires: 0");
00323         $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
00324         $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
00325         $oUtils->setHeader("Content-Type: application/octet-stream");
00326         if ($iFileSize = $this->getSize()) {
00327             $oUtils->setHeader("Content-Length: " . $iFileSize);
00328         }
00329         readfile($sFileLocations);
00330         $oUtils->showMessageAndExit(null);
00331     }
00332 
00338     public function exist()
00339     {
00340         return file_exists($this->getStoreLocation());
00341     }
00342 
00348     public function hasValidDownloads()
00349     {
00350         if ($this->_blHasValidDownloads == null) {
00351             $this->_blHasValidDownloads = false;
00352             $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
00353             $sFileId = $this->getId();
00354 
00355             $oDb = oxDb::getDb();
00356 
00357             $sSql = "SELECT
00358                         `oxorderfiles`.`oxid`
00359                      FROM `oxorderfiles`
00360                         LEFT JOIN `oxorderarticles` ON `oxorderarticles`.`oxid` = `oxorderfiles`.`oxorderarticleid`
00361                         LEFT JOIN `oxorder` ON `oxorder`.`oxid` = `oxorderfiles`.`oxorderid`
00362                      WHERE `oxorderfiles`.`oxfileid` = " . $oDb->quote($sFileId) . "
00363                         AND ( ! `oxorderfiles`.`oxmaxdownloadcount` OR `oxorderfiles`.`oxmaxdownloadcount` > `oxorderfiles`.`oxdownloadcount`)
00364                         AND ( `oxorderfiles`.`oxvaliduntil` = '0000-00-00 00:00:00' OR `oxorderfiles`.`oxvaliduntil` > '{$sNow}' )
00365                         AND `oxorder`.`oxstorno` = 0
00366                         AND `oxorderarticles`.`oxstorno` = 0";
00367 
00368             if ($oDb->getOne($sSql)) {
00369                 $this->_blHasValidDownloads = true;
00370             }
00371         }
00372 
00373         return $this->_blHasValidDownloads;
00374     }
00375 
00381     public function getMaxDownloadsCount()
00382     {
00383         $iMaxCount = $this->oxfiles__oxmaxdownloads->value;
00384         //if value is -1, takes global options
00385         if ($iMaxCount < 0) {
00386             $iMaxCount = $this->getConfig()->getConfigParam("iMaxDownloadsCount");
00387         }
00388 
00389         return $iMaxCount;
00390     }
00391 
00397     public function getMaxUnregisteredDownloadsCount()
00398     {
00399         $iMaxCount = $this->oxfiles__oxmaxunregdownloads->value;
00400         //if value is -1, takes global options
00401         if ($iMaxCount < 0) {
00402             $iMaxCount = $this->getConfig()->getConfigParam("iMaxDownloadsCountUnregistered");
00403         }
00404 
00405         return $iMaxCount;
00406     }
00407 
00413     public function getLinkExpirationTime()
00414     {
00415         $iExpTime = $this->oxfiles__oxlinkexptime->value;
00416         //if value is -1, takes global options
00417         if ($iExpTime < 0) {
00418             $iExpTime = $this->getConfig()->getConfigParam("iLinkExpirationTime");
00419         }
00420 
00421         return $iExpTime;
00422     }
00423 
00429     public function getDownloadExpirationTime()
00430     {
00431         $iExpTime = $this->oxfiles__oxdownloadexptime->value;
00432         //if value is -1, takes global options
00433         if ($iExpTime < 0) {
00434             $iExpTime = $this->getConfig()->getConfigParam("iDownloadExpirationTime");
00435         }
00436 
00437         return $iExpTime;
00438     }
00439 
00445     public function getSize()
00446     {
00447         $iSize = 0;
00448         if ($this->exist()) {
00449             $iSize = filesize($this->getStoreLocation());
00450         }
00451 
00452         return $iSize;
00453     }
00454 }