PluginProbe
User Access Manager / 2.2.8
User Access Manager v2.2.8
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 / NginxFileProtection.php

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

119 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NginxFileProtection.php
4 *
5 * The NginxFileProtection 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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\File;
19
20 use Exception;
21 use UserAccessManager\Object\ObjectHandler;
22
23 /**
24 * Class NginxFileProtection
25 *
26 * @package UserAccessManager\FileHandler
27 */
28 class NginxFileProtection extends FileProtection implements FileProtectionInterface
29 {
30 const FILE_NAME = 'uam.conf';
31
32 /**
33 * Returns the location.
34 * @param string $directory
35 * @return string
36 */
37 protected function getLocation(string $directory): string
38 {
39 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
40 return "^{$directory}" . $this->getDirectoryMatch();
41 }
42
43 $directoryMatch = $this->getDirectoryMatch();
44 return $directoryMatch === null ? $directory : $directoryMatch;
45 }
46
47 /**
48 * Creates the file content if permalinks are active.
49 * @param string $absolutePath
50 * @param string $directory
51 * @param string|null $objectType
52 * @return string
53 */
54 private function getFileContent(string $absolutePath, string $directory, ?string $objectType): string
55 {
56 if ($objectType === null) {
57 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
58 }
59
60 $location = $this->getLocation(str_replace($absolutePath, '/', $directory));
61
62 $content = "location {$location} {\n";
63 $content .= "rewrite ^([^?]*)$ /index.php?uamfiletype={$objectType}&uamgetfile=$1 last;\n";
64 $content .= "rewrite ^(.*)\\?(((?!uamfiletype).)*)$ ";
65 $content .= "/index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 last;\n";
66 $content .= "rewrite ^(.*)\\?(.*)$ /index.php?uamgetfile=$1&$2 last;\n";
67 $content .= "}\n";
68
69 return $content;
70 }
71
72 /**
73 * Returns the nginx config file name with path.
74 * @param string null|$directory
75 * @return string
76 */
77 public function getFileNameWithPath($directory = null): string
78 {
79 return ABSPATH . self::FILE_NAME;
80 }
81
82 /**
83 * Generates the conf file.
84 * @param string $directory
85 * @param string|null $objectType
86 * @param string|null $absolutePath
87 * @return bool
88 */
89 public function create(string $directory, ?string $objectType = null, ?string $absolutePath = ABSPATH): bool
90 {
91 $directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
92 $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
93 $content = $this->getFileContent($absolutePath, $directory, $objectType);
94
95 // save files
96 $fileWithPath = $absolutePath . self::FILE_NAME;
97
98 try {
99 file_put_contents($fileWithPath, $content);
100 return true;
101 } catch (Exception $exception) {
102 // Because file_put_contents can throw exceptions we use this try catch block
103 // to return the success result instead of an exception
104 }
105
106 return false;
107 }
108
109 /**
110 * Deletes the conf file.
111 * @param string $directory
112 * @return bool
113 */
114 public function delete(string $directory): bool
115 {
116 return $this->deleteFiles($directory);
117 }
118 }
119