OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxfile.php
Go to the documentation of this file.
1 <?php
2 
7 class oxFile extends oxBase
8 {
12  const NO_USER = 2;
13 
19  protected $_sCoreTable = 'oxfiles';
20 
26  protected $_sClassName = 'oxfile';
27 
33  protected $_sRelativeFilePath = null;
34 
40  protected $_blIsPaid = null;
41 
48  protected $_sDownloadLink = null;
49 
55  protected $_blHasValidDownloads = null;
56 
62  protected $_sManualUploadDir = "uploads";
63 
68  public function __construct()
69  {
71  $this->init();
72  }
73 
81  public function processFile( $sFileIndex )
82  {
83  $aFileInfo = $this->getConfig()->getUploadedFile( $sFileIndex );
84 
85  $this->_checkArticleFile( $aFileInfo );
86 
87  $sFileHash = $this->_getFileHash( $aFileInfo['tmp_name'] );
88  $this->oxfiles__oxstorehash = new oxField( $sFileHash, oxField::T_RAW );
89  $sUploadTo = $this->getStoreLocation();
90 
91  if ( !$this->_uploadFile( $aFileInfo['tmp_name'], $sUploadTo ) ) {
92  throw new oxException( 'EXCEPTION_COULDNOTWRITETOFILE' );
93  }
94 
95  }
96 
104  protected function _checkArticleFile( $aFileInfo )
105  {
106  //checking params
107  if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
108  throw new oxException( 'EXCEPTION_NOFILE' );
109  }
110 
111  // error uploading file ?
112  if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
113  throw new oxException( 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
114  }
115 
116  }
117 
123  protected function _getBaseDownloadDirPath()
124  {
125  $sConfigValue = oxRegistry::getConfig()->getConfigParam( 'sDownloadsDir' );
126 
127  //Unix full path is set
128  if ( $sConfigValue && $sConfigValue[0] == DIR_SEP) {
129  return $sConfigValue;
130  }
131 
132  //relative path is set
133  if ( $sConfigValue ) {
134  $sPath = getShopBasePath() . DIR_SEP . $sConfigValue;
135  return $sPath;
136  }
137 
138  //no path is set
139  $sPath = getShopBasePath() . "/out/downloads/";
140  return $sPath;
141  }
142 
150  public function getStoreLocation()
151  {
152  $sPath = $this->_getBaseDownloadDirPath();
153  $sPath .= DIR_SEP . $this->_getFileLocation();
154  return $sPath;
155  }
156 
162  protected function _getFileLocation()
163  {
164  $this->_sRelativeFilePath = '';
165  $sFileHash = $this->oxfiles__oxstorehash->value;
166  $sFileName = $this->oxfiles__oxfilename->value;
167 
168  //security check for demo shops
169  if ($this->getConfig()->isDemoShop()) {
170  $sFileName = basename($sFileName);
171  }
172 
173  if ($this->isUploaded()) {
174  $this->_sRelativeFilePath = $this->_getHashedFileDir($sFileHash);
175  $this->_sRelativeFilePath .= DIR_SEP . $sFileHash;
176  } else {
177  $this->_sRelativeFilePath = DIR_SEP . $this->_sManualUploadDir . DIR_SEP . $sFileName;
178  }
179 
181  }
182 
192  protected function _getHashedFileDir( $sFileHash )
193  {
194  $sDir = substr($sFileHash, 0, 2);
195  $sAbsDir = $this->_getBaseDownloadDirPath() . DIR_SEP . $sDir;
196 
197  if (!is_dir($sAbsDir)) {
198  mkdir($sAbsDir, 0755);
199  }
200  return $sDir;
201  }
202 
211  protected function _getFileHash( $sFileName )
212  {
213  return md5_file( $sFileName );
214  }
215 
225  protected function _uploadFile( $sSource, $sTarget )
226  {
227  $blDone = move_uploaded_file( $sSource, $sTarget );
228 
229  if ( $blDone ) {
230  $blDone = @chmod( $sTarget, 0644 );
231  }
232 
233  return $blDone;
234  }
235 
244  public function isUploaded()
245  {
246  $blHashed = false;
247  if ($this->oxfiles__oxstorehash->value) {
248  $blHashed = true;
249  }
250  return $blHashed;
251  }
252 
260  public function delete( $sOxId = null )
261  {
262  $sOxId = $sOxId ? $sOxId : $this->getId();
263 
264  $this->load($sOxId);
265  // if record cannot be delete, abort deletion
266  if ($blDeleted = parent::delete( $sOxId ) ) {
267  $this->_deleteFile( );
268  }
269 
270  return $blDeleted;
271  }
272 
279  protected function _deleteFile()
280  {
281  if (!$this->isUploaded()) {
282  return false;
283  }
284  $sHash = $this->oxfiles__oxstorehash->value;
285  $oDb = oxDb::getDb();
286  $iCount = $oDb->getOne(
287  'SELECT COUNT(*) FROM `oxfiles` WHERE `OXSTOREHASH` = ' . $oDb->quote( $sHash ), false, false );
288  if (!$iCount) {
289  $sPath = $this->getStoreLocation();
290  unlink($sPath);
291  }
292  }
293 
300  protected function _getFilenameForUrl()
301  {
302  return rawurlencode($this->oxfiles__oxfilename->value);
303  }
304 
308  public function download()
309  {
310  $oUtils = oxRegistry::getUtils();
311  $sFileName = $this->_getFilenameForUrl();
312  $sFileLocations = $this->getStoreLocation();
313 
314  if ( !$this->exist() ) {
315  throw new oxException( 'EXCEPTION_NOFILE' );
316  }
317 
318  $oUtils->setHeader("Pragma: public");
319  $oUtils->setHeader("Expires: 0");
320  $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
321  $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
322  $oUtils->setHeader("Content-Type: application/octet-stream");
323  if ( $iFileSize = $this->getSize() ) {
324  $oUtils->setHeader("Content-Length: " . $iFileSize);
325  }
326  readfile( $sFileLocations );
327  $oUtils->showMessageAndExit( null );
328  }
329 
335  public function exist()
336  {
337  return file_exists( $this->getStoreLocation() );
338  }
339 
345  public function hasValidDownloads()
346  {
347  if ( $this->_blHasValidDownloads == null ) {
348  $this->_blHasValidDownloads = false;
349  $sNow = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
350  $sFileId = $this->getId();
351 
352  $oDb = oxDb::getDb();
353 
354  $sSql = "SELECT
355  `oxorderfiles`.`oxid`
356  FROM `oxorderfiles`
357  LEFT JOIN `oxorderarticles` ON `oxorderarticles`.`oxid` = `oxorderfiles`.`oxorderarticleid`
358  LEFT JOIN `oxorder` ON `oxorder`.`oxid` = `oxorderfiles`.`oxorderid`
359  WHERE `oxorderfiles`.`oxfileid` = " . $oDb->quote($sFileId) . "
360  AND ( ! `oxorderfiles`.`oxmaxdownloadcount` OR `oxorderfiles`.`oxmaxdownloadcount` > `oxorderfiles`.`oxdownloadcount`)
361  AND ( `oxorderfiles`.`oxvaliduntil` = '0000-00-00 00:00:00' OR `oxorderfiles`.`oxvaliduntil` > '{$sNow}' )
362  AND `oxorder`.`oxstorno` = 0
363  AND `oxorderarticles`.`oxstorno` = 0";
364 
365  if ( $oDb->getOne( $sSql ) ) {
366  $this->_blHasValidDownloads = true;
367  }
368  }
370  }
371 
377  public function getMaxDownloadsCount()
378  {
379  $iMaxCount = $this->oxfiles__oxmaxdownloads->value;
380  //if value is -1, takes global options
381  if ( $iMaxCount < 0) {
382  $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCount" );
383  }
384  return $iMaxCount;
385  }
386 
393  {
394  $iMaxCount = $this->oxfiles__oxmaxunregdownloads->value;
395  //if value is -1, takes global options
396  if ( $iMaxCount < 0) {
397  $iMaxCount = $this->getConfig()->getConfigParam( "iMaxDownloadsCountUnregistered" );
398  }
399  return $iMaxCount;
400  }
401 
407  public function getLinkExpirationTime()
408  {
409  $iExpTime = $this->oxfiles__oxlinkexptime->value;
410  //if value is -1, takes global options
411  if ( $iExpTime < 0) {
412  $iExpTime = $this->getConfig()->getConfigParam( "iLinkExpirationTime" );
413  }
414  return $iExpTime;
415  }
416 
422  public function getDownloadExpirationTime()
423  {
424  $iExpTime = $this->oxfiles__oxdownloadexptime->value;
425  //if value is -1, takes global options
426  if ( $iExpTime < 0) {
427  $iExpTime = $this->getConfig()->getConfigParam( "iDownloadExpirationTime" );
428  }
429  return $iExpTime;
430  }
431 
437  public function getSize()
438  {
439  $iSize = 0;
440  if ( $this->exist() ) {
441  $iSize = filesize( $this->getStoreLocation() );
442  }
443  return $iSize;
444  }
445 
446 }