ApacheFileProtection.php
5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* ApacheFileProtection.php
*
* The ApacheFileProtection class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\File;
use Exception;
use UserAccessManager\Object\ObjectHandler;
/**
* Class ApacheFileProtection
*
* @package UserAccessManager\FileHandler
*/
class ApacheFileProtection extends FileProtection implements FileProtectionInterface
{
const FILE_NAME = '.htaccess';
/**
* Returns the file types.
* @return null|string
*/
private function getFileTypes(): ?string
{
$fileTypes = null;
$lockedFileTypes = $this->mainConfig->getLockedFileType();
if ($lockedFileTypes === 'selected') {
$fileTypes = $this->cleanUpFileTypes($this->mainConfig->getLockedFiles());
$fileTypes = ($fileTypes !== '') ? "\.({$fileTypes})" : null;
} elseif ($lockedFileTypes === 'not_selected') {
$fileTypes = $this->cleanUpFileTypes($this->mainConfig->getNotLockedFiles());
$fileTypes = ($fileTypes !== '') ? "^\.({$fileTypes})" : null;
}
return $fileTypes;
}
/**
* Returns the directory match.
* @return null|string
*/
protected function getDirectoryMatch(): ?string
{
if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
return '^.*' . DIRECTORY_SEPARATOR . parent::getDirectoryMatch() . '.*$';
}
return parent::getDirectoryMatch();
}
/**
* @param string $content
* @return string
*/
private function applyFilters(string $content): string
{
$fileTypes = $this->getFileTypes();
if ($fileTypes !== null) {
/** @noinspection */
$content = "<FilesMatch '{$fileTypes}'>\n{$content}</FilesMatch>\n";
}
return $content;
}
/**
* Creates the file content if no permalinks are active.
* @param string $directory
* @return string
*/
private function getFileContent(string $directory): string
{
$areaName = 'WP-Files';
// make .htaccess and .htpasswd
$content = "AuthType Basic" . "\n";
$content .= "AuthName \"{$areaName}\"" . "\n";
$content .= "AuthUserFile {$directory}.htpasswd" . "\n";
$content .= "require valid-user" . "\n";
return $this->applyFilters($content);
}
/**
* Creates the file content if permalinks are active.
* @param string|null $objectType
* @param bool|null $isSubSite
* @return string
*/
private function getPermalinkFileContent(?string $objectType, ?bool $isSubSite = false): string
{
if ($objectType === null) {
$objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
}
$homeRoot = parse_url($this->wordpress->getSiteUrl());
$homeRoot = (isset($homeRoot['path']) === true) ? '/' . trim($homeRoot['path'], '/\\') . '/' : '/';
$content = "RewriteEngine On\n";
$content .= "RewriteBase {$homeRoot}\n";
$content .= "RewriteRule ^index\\.php$ - [L]\n";
if ($isSubSite === false) {
$content .= "RewriteCond %{REQUEST_URI} !.*\/sites\/[0-9]+\/.*\n";
}
$directoryMatch = $this->getDirectoryMatch();
if ($directoryMatch !== null) {
$content .= "RewriteCond %{REQUEST_URI} {$directoryMatch}\n";
}
$content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1 [QSA,L]\n";
$content .= "RewriteRule ^(.*)\\?(((?!uamfiletype).)*)$ ";
$content .= "{$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 [QSA,L]\n";
$content .= "RewriteRule ^(.*)\\?(.*)$ {$homeRoot}index.php?uamgetfile=$1&$2 [QSA,L]\n";
$content = $this->applyFilters($content);
$content = "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
return $content;
}
/**
* Returns the htaccess file name with path.
* @param null|string $directory
* @return string
*/
public function getFileNameWithPath($directory = null): string
{
return $directory . self::FILE_NAME;
}
/**
* Generates the htaccess file.
* @param string $directory
* @param string|null $objectType
* @param string|null $absolutePath
* @return bool
*/
public function create(string $directory, ?string $objectType = null, ?string $absolutePath = null): bool
{
$directory = rtrim($directory, '/') . '/';
if ($this->wordpress->gotModRewrite() === false) {
$content = $this->getFileContent($directory);
$this->createPasswordFile(true, $directory);
} else {
$content = $this->getPermalinkFileContent(
$objectType,
preg_match('/.*\/sites\/[0-9]+\/$/', $directory) !== 0
);
}
// save files
$fileWithPath = $this->getFileNameWithPath($directory);
try {
file_put_contents($fileWithPath, $content);
return true;
} catch (Exception $exception) {
// Because file_put_contents can throw exceptions we use this try catch block
// to return the success result instead of an exception
}
return false;
}
/**
* Deletes the htaccess files.
* @param string $directory
* @return bool
*/
public function delete(string $directory): bool
{
return $this->deleteFiles($directory);
}
}