PluginProbe
User Access Manager / 2.2.22
User Access Manager v2.2.22
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 / File / FileProtection.php

FileProtection.php in User Access Manager 2.2.22, at src/File/FileProtection.php

200 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * FileProtection.php
4 *
5 * The FileProtection 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
16 declare(strict_types=1);
17
18 namespace UserAccessManager\File;
19
20 use Exception;
21 use UserAccessManager\Config\MainConfig;
22 use UserAccessManager\Config\WordpressConfig;
23 use UserAccessManager\Util\Util;
24 use UserAccessManager\Wrapper\Php;
25 use UserAccessManager\Wrapper\Wordpress;
26
27 /**
28 * Class FileProtection
29 *
30 * @package UserAccessManager\FileHandler
31 */
32 abstract class FileProtection
33 {
34 const FILE_NAME = null;
35 const PASSWORD_FILE_NAME = '.htpasswd';
36
37 /**
38 * @var Php
39 */
40 protected $php;
41
42 /**
43 * @var Wordpress
44 */
45 protected $wordpress;
46
47 /**
48 * @var WordpressConfig
49 */
50 protected $wordpressConfig;
51
52 /**
53 * @var MainConfig
54 */
55 protected $mainConfig;
56
57 /**
58 * @var Util
59 */
60 protected $util;
61
62 /**
63 * FileProtection constructor.
64 * @param Php $php
65 * @param Wordpress $wordpress
66 * @param WordpressConfig $wordpressConfig
67 * @param MainConfig $mainConfig
68 * @param Util $util
69 */
70 public function __construct(
71 Php $php,
72 Wordpress $wordpress,
73 WordpressConfig $wordpressConfig,
74 MainConfig $mainConfig,
75 Util $util
76 ) {
77 $this->php = $php;
78 $this->wordpress = $wordpress;
79 $this->wordpressConfig = $wordpressConfig;
80 $this->mainConfig = $mainConfig;
81 $this->util = $util;
82 }
83
84 /**
85 * Returns the directory match.
86 * @return null|string
87 */
88 protected function getDirectoryMatch(): ?string
89 {
90 $directoryMatch = null;
91 $lockedDirectoryType = $this->mainConfig->getLockedDirectoryType();
92
93 if ($lockedDirectoryType === 'wordpress') {
94 $directoryMatch = '[0-9]{4}' . DIRECTORY_SEPARATOR . '[0-9]{2}';
95 } elseif ($lockedDirectoryType === 'custom') {
96 $directoryMatch = $this->mainConfig->getCustomLockedDirectories();
97 }
98
99 return $directoryMatch;
100 }
101
102 /**
103 * Cleans up the file types.
104 * @param string $fileTypes The file types which should be cleaned up.
105 * @return string
106 */
107 protected function cleanUpFileTypes(string $fileTypes): string
108 {
109 $validFileTypes = [];
110 $fileTypes = explode(',', $fileTypes);
111 $mimeTypes = $this->wordpressConfig->getMimeTypes();
112
113 foreach ($fileTypes as $fileType) {
114 $cleanFileType = trim($fileType);
115
116 if (isset($mimeTypes[$cleanFileType]) === true) {
117 $validFileTypes[$cleanFileType] = $cleanFileType;
118 }
119 }
120
121 return implode('|', $validFileTypes);
122 }
123
124 /**
125 * Returns the password file name with the path.
126 * @param string|null $dir
127 * @return null|string
128 */
129 private function getDefaultPasswordFileWithPath(?string $dir): ?string
130 {
131 if ($dir === null) {
132 $wordpressUploadDir = $this->wordpress->getUploadDir();
133
134 if (empty($wordpressUploadDir['error']) === true) {
135 $dir = $wordpressUploadDir['basedir'] . DIRECTORY_SEPARATOR;
136 }
137 }
138
139 return ($dir !== null) ? $dir . static::PASSWORD_FILE_NAME : null;
140 }
141
142 /**
143 * Creates a htpasswd file.
144 * @param boolean $createNew Force to create new file.
145 * @param string $dir The destination directory.
146 */
147 public function createPasswordFile($createNew = false, $dir = null)
148 {
149 $file = $this->getDefaultPasswordFileWithPath($dir);
150
151 if ($file !== null && (file_exists($file) === false || $createNew)) {
152 $currentUser = $this->wordpress->getCurrentUser();
153
154 $user = $currentUser->user_login;
155 $password = $currentUser->user_pass;
156
157 if ($this->mainConfig->getFilePassType() === 'random') {
158 try {
159 $randomPassword = $this->util->getRandomPassword();
160 $password = md5($randomPassword);
161 } catch (Exception $exception) {
162 // Do nothing
163 }
164 }
165
166 // make .htpasswd
167 $content = "{$user}:{$password}\n";
168
169 // save file
170 $fileHandler = fopen($file, 'w');
171 fwrite($fileHandler, $content);
172 fclose($fileHandler);
173 }
174 }
175
176 /**
177 * Deletes the htaccess files.
178 * @param string $directory
179 * @return bool
180 */
181 public function deleteFiles(string $directory): bool
182 {
183 $success = true;
184 $directory = rtrim($directory, '/') . '/';
185 $fileName = $directory . static::FILE_NAME;
186
187 if (file_exists($fileName) === true) {
188 $success = ($this->php->unlink($fileName) === true) && $success;
189 }
190
191 $passwordFile = $directory . static::PASSWORD_FILE_NAME;
192
193 if (file_exists($passwordFile) === true) {
194 $success = ($this->php->unlink($passwordFile) === true) && $success;
195 }
196
197 return $success;
198 }
199 }
200