OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxfile.php
Go to the documentation of this file.
1 <?php
2 
7 class oxFile extends oxBase
8 {
9 
13  const NO_USER = 2;
14 
20  protected $_sCoreTable = 'oxfiles';
21 
27  protected $_sClassName = 'oxfile';
28 
34  protected $_sRelativeFilePath = null;
35 
41  protected $_blIsPaid = null;
42 
49  protected $_sDownloadLink = null;
50 
56  protected $_blHasValidDownloads = null;
57 
63  protected $_sManualUploadDir = "uploads";
64 
69  public function __construct()
70  {
72  $this->init();
73  }
74 
80  public function processFile($sFileIndex)
81  {
82  $aFileInfo = $this->getConfig()->getUploadedFile($sFileIndex);
83 
84  $this->_checkArticleFile($aFileInfo);
85 
86  $sFileHash = $this->_getFileHash($aFileInfo['tmp_name']);
87  $this->oxfiles__oxstorehash = new oxField($sFileHash, oxField::T_RAW);
88  $sUploadTo = $this->getStoreLocation();
89 
90  if (!$this->_uploadFile($aFileInfo['tmp_name'], $sUploadTo)) {
91  throw new oxException('EXCEPTION_COULDNOTWRITETOFILE');
92  }
93 
94  }
95 
101  protected function _checkArticleFile($aFileInfo)
102  {
103  //checking params
104  if (!isset($aFileInfo['name']) || !isset($aFileInfo['tmp_name'])) {
105  throw new oxException('EXCEPTION_NOFILE');
106  }
107 
108  // error uploading file ?
109  if (isset($aFileInfo['error']) && $aFileInfo['error']) {
110  throw new oxException('EXCEPTION_FILEUPLOADERROR_' . ((int) $aFileInfo['error']));
111  }
112 
113  }
114 
120  protected function _getBaseDownloadDirPath()
121  {
122  $sConfigValue = oxRegistry::getConfig()->getConfigParam('sDownloadsDir');
123 
124  //Unix full path is set
125  if ($sConfigValue && $sConfigValue[0] == DIR_SEP) {
126  return $sConfigValue;
127  }
128 
129  //relative path is set
130  if ($sConfigValue) {
131  $sPath = getShopBasePath() . DIR_SEP . $sConfigValue;
132 
133  return $sPath;
134  }
135 
136  //no path is set
137  $sPath = getShopBasePath() . "/out/downloads/";
138 
139  return $sPath;
140  }
141 
149  public function getStoreLocation()
150  {
151  $sPath = $this->_getBaseDownloadDirPath();
152  $sPath .= DIR_SEP . $this->_getFileLocation();
153 
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 
201  return $sDir;
202  }
203 
212  protected function _getFileHash($sFileName)
213  {
214  return md5_file($sFileName);
215  }
216 
226  protected function _uploadFile($sSource, $sTarget)
227  {
228  $blDone = move_uploaded_file($sSource, $sTarget);
229 
230  if ($blDone) {
231  $blDone = @chmod($sTarget, 0644);
232  }
233 
234  return $blDone;
235  }
236 
245  public function isUploaded()
246  {
247  $blHashed = false;
248  if ($this->oxfiles__oxstorehash->value) {
249  $blHashed = true;
250  }
251 
252  return $blHashed;
253  }
254 
262  public function delete($sOxId = null)
263  {
264  $sOxId = $sOxId ? $sOxId : $this->getId();
265 
266  $this->load($sOxId);
267  // if record cannot be delete, abort deletion
268  if ($blDeleted = parent::delete($sOxId)) {
269  $this->_deleteFile();
270  }
271 
272  return $blDeleted;
273  }
274 
281  protected function _deleteFile()
282  {
283  if (!$this->isUploaded()) {
284  return false;
285  }
286  $sHash = $this->oxfiles__oxstorehash->value;
287  $oDb = oxDb::getDb();
288  $iCount = $oDb->getOne(
289  'SELECT COUNT(*) FROM `oxfiles` WHERE `OXSTOREHASH` = ' . $oDb->quote($sHash), false, false
290  );
291  if (!$iCount) {
292  $sPath = $this->getStoreLocation();
293  unlink($sPath);
294  }
295  }
296 
303  protected function _getFilenameForUrl()
304  {
305  return rawurlencode($this->oxfiles__oxfilename->value);
306  }
307 
311  public function download()
312  {
313  $oUtils = oxRegistry::getUtils();
314  $sFileName = $this->_getFilenameForUrl();
315  $sFileLocations = $this->getStoreLocation();
316 
317  if (!$this->exist()) {
318  throw new oxException('EXCEPTION_NOFILE');
319  }
320 
321  $oUtils->setHeader("Pragma: public");
322  $oUtils->setHeader("Expires: 0");
323  $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0, private");
324  $oUtils->setHeader('Content-Disposition: attachment;filename=' . $sFileName);
325  $oUtils->setHeader("Content-Type: application/octet-stream");
326  if ($iFileSize = $this->getSize()) {
327  $oUtils->setHeader("Content-Length: " . $iFileSize);
328  }
329  readfile($sFileLocations);
330  $oUtils->showMessageAndExit(null);
331  }
332 
338  public function exist()
339  {
340  return file_exists($this->getStoreLocation());
341  }
342 
348  public function hasValidDownloads()
349  {
350  if ($this->_blHasValidDownloads == null) {
351  $this->_blHasValidDownloads = false;
352  $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
353  $sFileId = $this->getId();
354 
355  $oDb = oxDb::getDb();
356 
357  $sSql = "SELECT
358  `oxorderfiles`.`oxid`
359  FROM `oxorderfiles`
360  LEFT JOIN `oxorderarticles` ON `oxorderarticles`.`oxid` = `oxorderfiles`.`oxorderarticleid`
361  LEFT JOIN `oxorder` ON `oxorder`.`oxid` = `oxorderfiles`.`oxorderid`
362  WHERE `oxorderfiles`.`oxfileid` = " . $oDb->quote($sFileId) . "
363  AND ( ! `oxorderfiles`.`oxmaxdownloadcount` OR `oxorderfiles`.`oxmaxdownloadcount` > `oxorderfiles`.`oxdownloadcount`)
364  AND ( `oxorderfiles`.`oxvaliduntil` = '0000-00-00 00:00:00' OR `oxorderfiles`.`oxvaliduntil` > '{$sNow}' )
365  AND `oxorder`.`oxstorno` = 0
366  AND `oxorderarticles`.`oxstorno` = 0";
367 
368  if ($oDb->getOne($sSql)) {
369  $this->_blHasValidDownloads = true;
370  }
371  }
372 
374  }
375 
381  public function getMaxDownloadsCount()
382  {
383  $iMaxCount = $this->oxfiles__oxmaxdownloads->value;
384  //if value is -1, takes global options
385  if ($iMaxCount < 0) {
386  $iMaxCount = $this->getConfig()->getConfigParam("iMaxDownloadsCount");
387  }
388 
389  return $iMaxCount;
390  }
391 
398  {
399  $iMaxCount = $this->oxfiles__oxmaxunregdownloads->value;
400  //if value is -1, takes global options
401  if ($iMaxCount < 0) {
402  $iMaxCount = $this->getConfig()->getConfigParam("iMaxDownloadsCountUnregistered");
403  }
404 
405  return $iMaxCount;
406  }
407 
413  public function getLinkExpirationTime()
414  {
415  $iExpTime = $this->oxfiles__oxlinkexptime->value;
416  //if value is -1, takes global options
417  if ($iExpTime < 0) {
418  $iExpTime = $this->getConfig()->getConfigParam("iLinkExpirationTime");
419  }
420 
421  return $iExpTime;
422  }
423 
429  public function getDownloadExpirationTime()
430  {
431  $iExpTime = $this->oxfiles__oxdownloadexptime->value;
432  //if value is -1, takes global options
433  if ($iExpTime < 0) {
434  $iExpTime = $this->getConfig()->getConfigParam("iDownloadExpirationTime");
435  }
436 
437  return $iExpTime;
438  }
439 
445  public function getSize()
446  {
447  $iSize = 0;
448  if ($this->exist()) {
449  $iSize = filesize($this->getStoreLocation());
450  }
451 
452  return $iSize;
453  }
454 }