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

186 lines 4.6 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 * Cleans up the file types.
83 *
84 * @param string $fileTypes The file types which should be cleaned up.
85 *
86 * @return string
87 */
88 protected function cleanUpFileTypes($fileTypes)
89 {
90 $validFileTypes = [];
91 $fileTypes = explode(',', $fileTypes);
92 $mimeTypes = $this->wordpressConfig->getMimeTypes();
93
94 foreach ($fileTypes as $fileType) {
95 $cleanFileType = trim($fileType);
96
97 if (isset($mimeTypes[$cleanFileType]) === true) {
98 $validFileTypes[$cleanFileType] = $cleanFileType;
99 }
100 }
101
102 return implode('|', $validFileTypes);
103 }
104
105 /**
106 * Returns the password file name with the path.
107 *
108 * @param string $dir
109 *
110 * @return null|string
111 */
112 private function getDefaultPasswordFileWithPath($dir)
113 {
114 if ($dir === null) {
115 $wordpressUploadDir = $this->wordpress->getUploadDir();
116
117 if (empty($wordpressUploadDir['error']) === true) {
118 $dir = $wordpressUploadDir['basedir'].DIRECTORY_SEPARATOR;
119 }
120 }
121
122 return ($dir !== null) ? $dir.static::PASSWORD_FILE_NAME : null;
123 }
124
125 /**
126 * Creates a htpasswd file.
127 *
128 * @param boolean $createNew Force to create new file.
129 * @param string $dir The destination directory.
130 */
131 public function createPasswordFile($createNew = false, $dir = null)
132 {
133 $file = $this->getDefaultPasswordFileWithPath($dir);
134
135 if ($file !== null && (file_exists($file) === false || $createNew)) {
136 $currentUser = $this->wordpress->getCurrentUser();
137
138 $user = $currentUser->user_login;
139 $password = $currentUser->user_pass;
140
141 if ($this->mainConfig->getFilePassType() === 'random') {
142 try {
143 $randomPassword = $this->util->getRandomPassword();
144 $password = md5($randomPassword);
145 } catch (\Exception $exception) {
146 // Do nothing
147 }
148 }
149
150 // make .htpasswd
151 $content = "{$user}:{$password}\n";
152
153 // save file
154 $fileHandler = fopen($file, 'w');
155 fwrite($fileHandler, $content);
156 fclose($fileHandler);
157 }
158 }
159
160 /**
161 * Deletes the htaccess files.
162 *
163 * @param string $directory
164 *
165 * @return bool
166 */
167 public function deleteFiles($directory)
168 {
169 $success = true;
170 $directory = rtrim($directory, '/').'/';
171 $fileName = $directory.static::FILE_NAME;
172
173 if (file_exists($fileName) === true) {
174 $success = ($this->php->unlink($fileName) === true) && $success;
175 }
176
177 $passwordFile = $directory.static::PASSWORD_FILE_NAME;
178
179 if (file_exists($passwordFile) === true) {
180 $success = ($this->php->unlink($passwordFile) === true) && $success;
181 }
182
183 return $success;
184 }
185 }
186