| 1 |
<?php |
| 2 |
/** |
| 3 |
* FileHandler.php |
| 4 |
* |
| 5 |
* The FileHandler class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
namespace UserAccessManager\FileHandler; |
| 16 |
|
| 17 |
use UserAccessManager\Config\Config; |
| 18 |
use UserAccessManager\Wrapper\Php; |
| 19 |
use UserAccessManager\Wrapper\Wordpress; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class FileHandler |
| 23 |
* |
| 24 |
* @package UserAccessManager\FileHandler |
| 25 |
*/ |
| 26 |
class FileHandler |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @var Php |
| 30 |
*/ |
| 31 |
private $php; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Wordpress |
| 35 |
*/ |
| 36 |
private $wordpress; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var Config |
| 40 |
*/ |
| 41 |
private $config; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var FileProtectionFactory |
| 45 |
*/ |
| 46 |
private $fileProtectionFactory; |
| 47 |
|
| 48 |
/** |
| 49 |
* FileHandler constructor. |
| 50 |
* |
| 51 |
* @param Php $php |
| 52 |
* @param Wordpress $wordpress |
| 53 |
* @param Config $config |
| 54 |
* @param FileProtectionFactory $fileProtectionFactory |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
Php $php, |
| 58 |
Wordpress $wordpress, |
| 59 |
Config $config, |
| 60 |
FileProtectionFactory $fileProtectionFactory |
| 61 |
) { |
| 62 |
$this->php = $php; |
| 63 |
$this->wordpress = $wordpress; |
| 64 |
$this->config = $config; |
| 65 |
$this->fileProtectionFactory = $fileProtectionFactory; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Clears the buffer. |
| 70 |
*/ |
| 71 |
private function clearBuffer() |
| 72 |
{ |
| 73 |
//TODO find better solution (prevent '\n' / '0A') |
| 74 |
if (is_numeric(ob_get_length()) === true) { |
| 75 |
ob_clean(); |
| 76 |
} |
| 77 |
|
| 78 |
flush(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Delivers the content of the requested file. |
| 83 |
* |
| 84 |
* @param string $file |
| 85 |
* @param bool $isImage |
| 86 |
*/ |
| 87 |
public function getFile($file, $isImage) |
| 88 |
{ |
| 89 |
//Deliver content |
| 90 |
if (file_exists($file) === true) { |
| 91 |
$fileName = basename($file); |
| 92 |
|
| 93 |
/* |
| 94 |
* This only for compatibility |
| 95 |
* mime_content_type has been deprecated as the PECL extension file info |
| 96 |
* provides the same functionality (and more) in a much cleaner way. |
| 97 |
*/ |
| 98 |
$explodedFileName = explode('.', $fileName); |
| 99 |
$lastElement = array_pop($explodedFileName); |
| 100 |
$fileExt = strtolower($lastElement); |
| 101 |
|
| 102 |
$mimeTypes = $this->config->getMimeTypes(); |
| 103 |
|
| 104 |
if ($this->php->functionExists('finfo_open') === true) { |
| 105 |
$fileInfo = finfo_open(FILEINFO_MIME); |
| 106 |
$fileMimeType = finfo_file($fileInfo, $file); |
| 107 |
finfo_close($fileInfo); |
| 108 |
} elseif ($this->php->functionExists('mime_content_type')) { |
| 109 |
$fileMimeType = mime_content_type($file); |
| 110 |
} elseif (isset($mimeTypes[$fileExt]) === true) { |
| 111 |
$fileMimeType = $mimeTypes[$fileExt]; |
| 112 |
} else { |
| 113 |
$fileMimeType = 'application/octet-stream'; |
| 114 |
} |
| 115 |
|
| 116 |
header('Content-Description: File Transfer'); |
| 117 |
header('Content-Type: '.$fileMimeType); |
| 118 |
|
| 119 |
if ($isImage === false) { |
| 120 |
$baseName = str_replace(' ', '_', basename($file)); |
| 121 |
header('Content-Disposition: attachment; filename="'.$baseName.'"'); |
| 122 |
} |
| 123 |
|
| 124 |
header('Content-Transfer-Encoding: binary'); |
| 125 |
header('Content-Length: '.filesize($file)); |
| 126 |
$this->clearBuffer(); |
| 127 |
|
| 128 |
if ($this->config->getDownloadType() === 'fopen' |
| 129 |
&& $isImage === false |
| 130 |
) { |
| 131 |
$handler = fopen($file, 'r'); |
| 132 |
|
| 133 |
while (feof($handler) === false) { |
| 134 |
if ($this->php->iniGet('safe_mode') !== '') { |
| 135 |
$this->php->setTimeLimit(30); |
| 136 |
} |
| 137 |
|
| 138 |
echo $this->php->fread($handler, 1024); |
| 139 |
} |
| 140 |
} else { |
| 141 |
readfile($file); |
| 142 |
} |
| 143 |
|
| 144 |
$this->php->callExit(); |
| 145 |
} else { |
| 146 |
$this->wordpress->wpDie( |
| 147 |
TXT_UAM_FILE_NOT_FOUND_ERROR_MESSAGE, |
| 148 |
TXT_UAM_FILE_NOT_FOUND_ERROR_TITLE, |
| 149 |
['response' => 404] |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Creates a protection file. |
| 156 |
* |
| 157 |
* @param string $dir The destination directory. |
| 158 |
* @param string $objectType The object type. |
| 159 |
* |
| 160 |
* @return false |
| 161 |
*/ |
| 162 |
public function createFileProtection($dir = null, $objectType = null) |
| 163 |
{ |
| 164 |
$dir = ($dir === null) ? $this->config->getUploadDirectory() : $dir; |
| 165 |
|
| 166 |
if ($dir !== null) { |
| 167 |
if ($this->wordpress->isNginx() === true) { |
| 168 |
return $this->fileProtectionFactory->createNginxFileProtection()->create($dir, $objectType); |
| 169 |
} else { |
| 170 |
return $this->fileProtectionFactory->createApacheFileProtection()->create($dir, $objectType); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Deletes the protection files. |
| 180 |
* |
| 181 |
* @param string $dir The destination directory. |
| 182 |
* |
| 183 |
* @return false |
| 184 |
*/ |
| 185 |
public function deleteFileProtection($dir = null) |
| 186 |
{ |
| 187 |
$dir = ($dir === null) ? $this->config->getUploadDirectory() : $dir; |
| 188 |
|
| 189 |
if ($dir !== null) { |
| 190 |
if ($this->wordpress->isNginx() === true) { |
| 191 |
return $this->fileProtectionFactory->createNginxFileProtection()->delete($dir); |
| 192 |
} else { |
| 193 |
return $this->fileProtectionFactory->createApacheFileProtection()->delete($dir); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
|