PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
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 +73 -14 2.3.152.2.21 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,27 +71,38 @@
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
63 - private function getPermalinkFileContent(?string $objectType, ?bool $isSubSite): string
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
64 105 {
65 106 if ($objectType === null) {
66 107 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
67 108 }
@@ -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,14 +120,14 @@
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 133 return "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
@@ -91,13 +132,25 @@
91 132
92 133 return "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
93 134 }
94 135
95 - public function getFileNameWithPath(?string $directory = null): string
136 + /**
137 + * Returns the htaccess file name with path.
138 + * @param null|string $directory
139 + * @return string
140 + */
141 + public function getFileNameWithPath($directory = null): string
96 142 {
97 143 return $directory . self::FILE_NAME;
98 144 }
99 145
146 + /**
147 + * Generates the htaccess file.
148 + * @param string $directory
149 + * @param string|null $objectType
150 + * @param string|null $absolutePath
151 + * @return bool
152 + */
100 153 public function create(string $directory, ?string $objectType = null, ?string $absolutePath = null): bool
101 154 {
102 155 $directory = rtrim($directory, '/') . '/';
103 156
@@ -114,10 +167,11 @@
114 167 // save files
115 168 $fileWithPath = $this->getFileNameWithPath($directory);
116 169
117 170 try {
118 - return file_put_contents($fileWithPath, $content) !== false;
119 - } catch (Exception) {
171 + file_put_contents($fileWithPath, $content);
172 + return true;
173 + } catch (Exception $exception) {
120 174 // Because file_put_contents can throw exceptions we use this try catch block
121 175 // to return the success result instead of an exception
122 176 }
123 177
@@ -123,8 +177,13 @@
123 177
124 178 return false;
125 179 }
126 180
181 + /**
182 + * Deletes the htaccess files.
183 + * @param string $directory
184 + * @return bool
185 + */
127 186 public function delete(string $directory): bool
128 187 {
129 188 return $this->deleteFiles($directory);
130 189 }