PluginProbe
User Access Manager / 2.1.9
User Access Manager v2.1.9
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.1.9, at src/File/FileProtection.php

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