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