PluginProbe
User Access Manager / 2.1.10
User Access Manager v2.1.10
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.1.10, at src/File/NginxFileProtection.php

125 lines 3.4 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 namespace UserAccessManager\File;
16
17 use UserAccessManager\Object\ObjectHandler;
18
19 /**
20 * Class NginxFileProtection
21 *
22 * @package UserAccessManager\FileHandler
23 */
24 class NginxFileProtection extends FileProtection implements FileProtectionInterface
25 {
26 const FILE_NAME = 'uam.conf';
27
28 /**
29 * Returns the location.
30 *
31 * @param string $directory
32 *
33 * @return string
34 */
35 protected function getLocation($directory)
36 {
37 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
38 return "^{$directory}".$this->getDirectoryMatch();
39 }
40
41 $directoryMatch = $this->getDirectoryMatch();
42 return $directoryMatch === null ? $directory : $directoryMatch;
43 }
44
45 /**
46 * Creates the file content if permalinks are active.
47 *
48 * @param string $absolutePath
49 * @param string $directory
50 * @param string $objectType
51 *
52 * @return string
53 */
54 private function getFileContent($absolutePath, $directory, $objectType)
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 *
75 * @param string null|$directory
76 *
77 * @return string
78 */
79 public function getFileNameWithPath($directory = null)
80 {
81 return ABSPATH.self::FILE_NAME;
82 }
83
84 /**
85 * Generates the conf file.
86 *
87 * @param string $directory
88 * @param string $objectType
89 * @param string $absolutePath
90 *
91 * @return bool
92 */
93 public function create($directory, $objectType = null, $absolutePath = ABSPATH)
94 {
95 $directory = rtrim($directory, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
96 $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
97 $content = $this->getFileContent($absolutePath, $directory, $objectType);
98
99 // save files
100 $fileWithPath = $absolutePath.self::FILE_NAME;
101
102 try {
103 file_put_contents($fileWithPath, $content);
104 return true;
105 } catch (\Exception $exception) {
106 // Because file_put_contents can throw exceptions we use this try catch block
107 // to return the success result instead of an exception
108 }
109
110 return false;
111 }
112
113 /**
114 * Deletes the conf file.
115 *
116 * @param string $directory
117 *
118 * @return bool
119 */
120 public function delete($directory)
121 {
122 return $this->deleteFiles($directory);
123 }
124 }
125