PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.3
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
← All changes | src/File/ApacheFileProtection.php +75 -14 2.3.142.2.3 View file →
@@ -1,5 +1,18 @@
1 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\File;
@@ -6,12 +19,21 @@
6 19
7 20 use Exception;
8 21 use UserAccessManager\Object\ObjectHandler;
9 22
23 +/**
24 + * Class ApacheFileProtection
25 + *
26 + * @package UserAccessManager\FileHandler
27 + */
10 28 class ApacheFileProtection extends FileProtection implements FileProtectionInterface
11 29 {
12 - public const FILE_NAME = '.htaccess';
30 + const FILE_NAME = '.htaccess';
13 31
32 + /**
33 + * Returns the file types.
34 + * @return null|string
35 + */
14 36 private function getFileTypes(): ?string
15 37 {
16 38 $fileTypes = null;
17 39 $lockedFileTypes = $this->mainConfig->getLockedFileType();
@@ -17,17 +39,21 @@
17 39 $lockedFileTypes = $this->mainConfig->getLockedFileType();
18 40
19 41 if ($lockedFileTypes === 'selected') {
20 42 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getLockedFiles());
21 - $fileTypes = ($fileTypes !== '') ? "\.($fileTypes)" : null;
43 + $fileTypes = ($fileTypes !== '') ? "\.({$fileTypes})" : null;
22 44 } elseif ($lockedFileTypes === 'not_selected') {
23 45 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getNotLockedFiles());
24 - $fileTypes = ($fileTypes !== '') ? "^\.($fileTypes)" : null;
46 + $fileTypes = ($fileTypes !== '') ? "^\.({$fileTypes})" : null;
25 47 }
26 48
27 49 return $fileTypes;
28 50 }
29 51
52 + /**
53 + * Returns the directory match.
54 + * @return null|string
55 + */
30 56 protected function getDirectoryMatch(): ?string
31 57 {
32 58 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
33 59 return '^.*' . DIRECTORY_SEPARATOR . parent::getDirectoryMatch() . '.*$';
@@ -35,8 +61,12 @@
35 61
36 62 return parent::getDirectoryMatch();
37 63 }
38 64
65 + /**
66 + * @param string $content
67 + * @return string
68 + */
39 69 private function applyFilters(string $content): string
40 70 {
41 71 $fileTypes = $this->getFileTypes();
42 72
@@ -41,26 +71,37 @@
41 71 $fileTypes = $this->getFileTypes();
42 72
43 73 if ($fileTypes !== null) {
44 74 /** @noinspection */
45 - $content = "<FilesMatch '$fileTypes'>\n$content</FilesMatch>\n";
75 + $content = "<FilesMatch '{$fileTypes}'>\n{$content}</FilesMatch>\n";
46 76 }
47 77
48 78 return $content;
49 79 }
50 80
81 + /**
82 + * Creates the file content if no permalinks are active.
83 + * @param string $directory
84 + * @return string
85 + */
51 86 private function getFileContent(string $directory): string
52 87 {
53 88 $areaName = 'WP-Files';
54 89 // make .htaccess and .htpasswd
55 90 $content = "AuthType Basic" . "\n";
56 - $content .= "AuthName \"$areaName\"" . "\n";
57 - $content .= "AuthUserFile $directory.htpasswd" . "\n";
91 + $content .= "AuthName \"{$areaName}\"" . "\n";
92 + $content .= "AuthUserFile {$directory}.htpasswd" . "\n";
58 93 $content .= "require valid-user" . "\n";
59 94
60 95 return $this->applyFilters($content);
61 96 }
62 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 + */
63 104 private function getPermalinkFileContent(?string $objectType, ?bool $isSubSite = false): string
64 105 {
65 106 if ($objectType === null) {
66 107 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
@@ -69,9 +110,9 @@
69 110 $homeRoot = parse_url($this->wordpress->getSiteUrl());
70 111 $homeRoot = (isset($homeRoot['path']) === true) ? '/' . trim($homeRoot['path'], '/\\') . '/' : '/';
71 112
72 113 $content = "RewriteEngine On\n";
73 - $content .= "RewriteBase $homeRoot\n";
114 + $content .= "RewriteBase {$homeRoot}\n";
74 115 $content .= "RewriteRule ^index\\.php$ - [L]\n";
75 116
76 117 if ($isSubSite === false) {
77 118 $content .= "RewriteCond %{REQUEST_URI} !.*\/sites\/[0-9]+\/.*\n";
@@ -79,25 +120,39 @@
79 120
80 121 $directoryMatch = $this->getDirectoryMatch();
81 122
82 123 if ($directoryMatch !== null) {
83 - $content .= "RewriteCond %{REQUEST_URI} $directoryMatch\n";
124 + $content .= "RewriteCond %{REQUEST_URI} {$directoryMatch}\n";
84 125 }
85 126
86 - $content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype=$objectType&uamgetfile=$1 [QSA,L]\n";
127 + $content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1 [QSA,L]\n";
87 128 $content .= "RewriteRule ^(.*)\\?(((?!uamfiletype).)*)$ ";
88 - $content .= "{$homeRoot}index.php?uamfiletype=$objectType&uamgetfile=$1&$2 [QSA,L]\n";
129 + $content .= "{$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 [QSA,L]\n";
89 130 $content .= "RewriteRule ^(.*)\\?(.*)$ {$homeRoot}index.php?uamgetfile=$1&$2 [QSA,L]\n";
90 131 $content = $this->applyFilters($content);
91 132
92 - return "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
133 + $content = "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
134 +
135 + return $content;
93 136 }
94 137
95 - public function getFileNameWithPath(?string $directory = null): string
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
96 144 {
97 145 return $directory . self::FILE_NAME;
98 146 }
99 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 + */
100 155 public function create(string $directory, ?string $objectType = null, ?string $absolutePath = null): bool
101 156 {
102 157 $directory = rtrim($directory, '/') . '/';
103 158
@@ -114,10 +169,11 @@
114 169 // save files
115 170 $fileWithPath = $this->getFileNameWithPath($directory);
116 171
117 172 try {
118 - return file_put_contents($fileWithPath, $content) !== false;
119 - } catch (Exception) {
173 + file_put_contents($fileWithPath, $content);
174 + return true;
175 + } catch (Exception $exception) {
120 176 // Because file_put_contents can throw exceptions we use this try catch block
121 177 // to return the success result instead of an exception
122 178 }
123 179
@@ -123,8 +179,13 @@
123 179
124 180 return false;
125 181 }
126 182
183 + /**
184 + * Deletes the htaccess files.
185 + * @param string $directory
186 + * @return bool
187 + */
127 188 public function delete(string $directory): bool
128 189 {
129 190 return $this->deleteFiles($directory);
130 191 }