PluginProbe
User Access Manager / 2.2.4
User Access Manager v2.2.4
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 / ApacheFileProtection.php

ApacheFileProtection.php in User Access Manager 2.2.4, at src/File/ApacheFileProtection.php

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