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 / NginxFileProtection.php

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

64 lines 2.1 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 NginxFileProtection extends FileProtection implements FileProtectionInterface
11 {
12 public const FILE_NAME = 'uam.conf';
13
14 protected function getLocation(string $directory): string
15 {
16 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
17 return "^$directory" . $this->getDirectoryMatch();
18 }
19
20 return $this->getDirectoryMatch() ?? $directory;
21 }
22
23 private function getFileContent(string $absolutePath, string $directory, ?string $objectType): string
24 {
25 if ($objectType === null) {
26 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
27 }
28
29 $location = $this->getLocation(str_replace($absolutePath, '/', $directory));
30
31 return "location ~ \"$location\" {\n"
32 . "rewrite ^([^?]*)$ /index.php?uamfiletype=$objectType&uamgetfile=$1 last;\n"
33 . "rewrite ^(.*)\\?(((?!uamfiletype).)*)$ "
34 . "/index.php?uamfiletype=$objectType&uamgetfile=$1&$2 last;\n"
35 . "rewrite ^(.*)\\?(.*)$ /index.php?uamgetfile=$1&$2 last;\n"
36 . "}\n";
37 }
38
39 public function getFileNameWithPath(?string $directory = null): string
40 {
41 return ABSPATH . self::FILE_NAME;
42 }
43
44 public function create(string $directory, ?string $objectType = null, ?string $absolutePath = ABSPATH): bool
45 {
46 $directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
47 $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
48 $content = $this->getFileContent($absolutePath, $directory, $objectType);
49
50 try {
51 return file_put_contents($absolutePath . self::FILE_NAME, $content) !== false;
52 } catch (Exception) {
53 // file_put_contents may throw, but callers expect a boolean success result.
54 }
55
56 return false;
57 }
58
59 public function delete(string $directory): bool
60 {
61 return $this->deleteFiles($directory);
62 }
63 }
64