PluginProbe
User Access Manager / 2.0.3
User Access Manager v2.0.3
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / UserAccessManager / FileHandler / FileHandler.php

FileHandler.php in User Access Manager 2.0.3, at src/UserAccessManager/FileHandler/FileHandler.php

198 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return null
88 */
89 public function getFile($file, $isImage)
90 {
91 //Deliver content
92 if (file_exists($file) === true) {
93 $fileName = basename($file);
94
95 /*
96 * This only for compatibility
97 * mime_content_type has been deprecated as the PECL extension file info
98 * provides the same functionality (and more) in a much cleaner way.
99 */
100 $explodedFileName = explode('.', $fileName);
101 $lastElement = array_pop($explodedFileName);
102 $fileExt = strtolower($lastElement);
103
104 $mimeTypes = $this->config->getMimeTypes();
105
106 if ($this->php->functionExists('finfo_open') === true) {
107 $fileInfo = finfo_open(FILEINFO_MIME);
108 $fileMimeType = finfo_file($fileInfo, $file);
109 finfo_close($fileInfo);
110 } elseif ($this->php->functionExists('mime_content_type')) {
111 $fileMimeType = mime_content_type($file);
112 } elseif (isset($mimeTypes[$fileExt]) === true) {
113 $fileMimeType = $mimeTypes[$fileExt];
114 } else {
115 $fileMimeType = 'application/octet-stream';
116 }
117
118 header('Content-Description: File Transfer');
119 header('Content-Type: '.$fileMimeType);
120
121 if ($isImage === false) {
122 $baseName = str_replace(' ', '_', basename($file));
123 header('Content-Disposition: attachment; filename="'.$baseName.'"');
124 }
125
126 header('Content-Transfer-Encoding: binary');
127 header('Content-Length: '.filesize($file));
128 $this->clearBuffer();
129
130 if ($this->config->getDownloadType() === 'fopen'
131 && $isImage === false
132 ) {
133 $handler = fopen($file, 'r');
134
135 while (feof($handler) === false) {
136 if ($this->php->iniGet('safe_mode') !== '') {
137 $this->php->setTimeLimit(30);
138 }
139
140 echo $this->php->fread($handler, 1024);
141 }
142 } else {
143 readfile($file);
144 }
145 } else {
146 $this->wordpress->wpDie(TXT_UAM_FILE_NOT_FOUND_ERROR);
147 }
148
149 return null;
150 }
151
152 /**
153 * Creates a protection file.
154 *
155 * @param string $dir The destination directory.
156 * @param string $objectType The object type.
157 *
158 * @return false
159 */
160 public function createFileProtection($dir = null, $objectType = null)
161 {
162 $dir = ($dir === null) ? $this->config->getUploadDirectory() : $dir;
163
164 if ($dir !== null) {
165 if ($this->wordpress->isNginx() === true) {
166 return $this->fileProtectionFactory->createNginxFileProtection()->create($dir, $objectType);
167 } else {
168 return $this->fileProtectionFactory->createApacheFileProtection()->create($dir, $objectType);
169 }
170 }
171
172 return false;
173 }
174
175
176 /**
177 * Deletes the protection files.
178 *
179 * @param string $dir The destination directory.
180 *
181 * @return false
182 */
183 public function deleteFileProtection($dir = null)
184 {
185 $dir = ($dir === null) ? $this->config->getUploadDirectory() : $dir;
186
187 if ($dir !== null) {
188 if ($this->wordpress->isNginx() === true) {
189 return $this->fileProtectionFactory->createNginxFileProtection()->delete($dir);
190 } else {
191 return $this->fileProtectionFactory->createApacheFileProtection()->delete($dir);
192 }
193 }
194
195 return false;
196 }
197 }
198