PluginProbe
User Access Manager / 2.3.15
User Access Manager v2.3.15
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.3.15, at src/File/ApacheFileProtection.php

132 lines 4.3 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;
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 $fileTypes = null;
17 $lockedFileTypes = $this->mainConfig->getLockedFileType();
18
19 if ($lockedFileTypes === 'selected') {
20 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getLockedFiles());
21 $fileTypes = ($fileTypes !== '') ? "\.($fileTypes)" : null;
22 } elseif ($lockedFileTypes === 'not_selected') {
23 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getNotLockedFiles());
24 $fileTypes = ($fileTypes !== '') ? "^\.($fileTypes)" : null;
25 }
26
27 return $fileTypes;
28 }
29
30 protected function getDirectoryMatch(): ?string
31 {
32 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
33 return '^.*' . DIRECTORY_SEPARATOR . parent::getDirectoryMatch() . '.*$';
34 }
35
36 return parent::getDirectoryMatch();
37 }
38
39 private function applyFilters(string $content): string
40 {
41 $fileTypes = $this->getFileTypes();
42
43 if ($fileTypes !== null) {
44 /** @noinspection */
45 $content = "<FilesMatch '$fileTypes'>\n$content</FilesMatch>\n";
46 }
47
48 return $content;
49 }
50
51 private function getFileContent(string $directory): string
52 {
53 $areaName = 'WP-Files';
54 // 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";
59
60 return $this->applyFilters($content);
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 $homeRoot = parse_url($this->wordpress->getSiteUrl());
70 $homeRoot = (isset($homeRoot['path']) === true) ? '/' . trim($homeRoot['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 // save files
115 $fileWithPath = $this->getFileNameWithPath($directory);
116
117 try {
118 return file_put_contents($fileWithPath, $content) !== false;
119 } catch (Exception) {
120 // Because file_put_contents can throw exceptions we use this try catch block
121 // to return the success result instead of an exception
122 }
123
124 return false;
125 }
126
127 public function delete(string $directory): bool
128 {
129 return $this->deleteFiles($directory);
130 }
131 }
132