PluginProbe
User Access Manager / 2.3.18
User Access Manager v2.3.18
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 / Protection / ApacheFileProtection.php

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

128 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\File\Protection;
6
7 use Exception;
8 use UserAccessManager\Object\ObjectHandler;
9
10 class ApacheFileProtection extends FileProtection implements FileProtectionInterface
11 {
12 public const FILE_NAME = '.htaccess';
13
14 private function getFileTypes(): ?string
15 {
16 $lockedFileTypes = $this->mainConfig->getLockedFileType();
17
18 if ($lockedFileTypes === 'selected') {
19 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getLockedFiles());
20
21 return ($fileTypes !== '') ? "\.($fileTypes)" : null;
22 }
23
24 if ($lockedFileTypes === 'not_selected') {
25 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getNotLockedFiles());
26
27 return ($fileTypes !== '') ? "^\.($fileTypes)" : null;
28 }
29
30 return null;
31 }
32
33 protected function getDirectoryMatch(): ?string
34 {
35 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
36 return '^.*' . DIRECTORY_SEPARATOR . parent::getDirectoryMatch() . '.*$';
37 }
38
39 return parent::getDirectoryMatch();
40 }
41
42 private function applyFilters(string $content): string
43 {
44 $fileTypes = $this->getFileTypes();
45
46 if ($fileTypes === null) {
47 return $content;
48 }
49
50 return "<FilesMatch '$fileTypes'>\n$content</FilesMatch>\n";
51 }
52
53 private function getFileContent(string $directory): string
54 {
55 return $this->applyFilters(
56 "AuthType Basic\n"
57 . "AuthName \"WP-Files\"\n"
58 . "AuthUserFile {$directory}.htpasswd\n"
59 . "require valid-user\n"
60 );
61 }
62
63 private function getPermalinkFileContent(?string $objectType, ?bool $isSubSite): string
64 {
65 if ($objectType === null) {
66 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
67 }
68
69 $siteUrlParts = parse_url($this->wordpress->getSiteUrl());
70 $homeRoot = (isset($siteUrlParts['path']) === true) ? '/' . trim($siteUrlParts['path'], '/\\') . '/' : '/';
71
72 $content = "RewriteEngine On\n";
73 $content .= "RewriteBase $homeRoot\n";
74 $content .= "RewriteRule ^index\\.php$ - [L]\n";
75
76 if ($isSubSite === false) {
77 $content .= "RewriteCond %{REQUEST_URI} !.*\/sites\/[0-9]+\/.*\n";
78 }
79
80 $directoryMatch = $this->getDirectoryMatch();
81
82 if ($directoryMatch !== null) {
83 $content .= "RewriteCond %{REQUEST_URI} $directoryMatch\n";
84 }
85
86 $content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype=$objectType&uamgetfile=$1 [QSA,L]\n";
87 $content .= "RewriteRule ^(.*)\\?(((?!uamfiletype).)*)$ ";
88 $content .= "{$homeRoot}index.php?uamfiletype=$objectType&uamgetfile=$1&$2 [QSA,L]\n";
89 $content .= "RewriteRule ^(.*)\\?(.*)$ {$homeRoot}index.php?uamgetfile=$1&$2 [QSA,L]\n";
90 $content = $this->applyFilters($content);
91
92 return "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
93 }
94
95 public function getFileNameWithPath(?string $directory = null): string
96 {
97 return $directory . self::FILE_NAME;
98 }
99
100 public function create(string $directory, ?string $objectType = null, ?string $absolutePath = null): bool
101 {
102 $directory = rtrim($directory, '/') . '/';
103
104 if ($this->wordpress->gotModRewrite() === false) {
105 $content = $this->getFileContent($directory);
106 $this->createPasswordFile(true, $directory);
107 } else {
108 $content = $this->getPermalinkFileContent(
109 $objectType,
110 preg_match('/.*\/sites\/[0-9]+\/$/', $directory) !== 0
111 );
112 }
113
114 try {
115 return file_put_contents($this->getFileNameWithPath($directory), $content) !== false;
116 } catch (Exception) {
117 // file_put_contents may throw, but callers expect a boolean success result.
118 }
119
120 return false;
121 }
122
123 public function delete(string $directory): bool
124 {
125 return $this->deleteFiles($directory);
126 }
127 }
128