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

201 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ApacheFileProtection.php
4 *
5 * The ApacheFileProtection 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 ApacheFileProtection
21 *
22 * @package UserAccessManager\FileHandler
23 */
24 class ApacheFileProtection extends FileProtection implements FileProtectionInterface
25 {
26 const FILE_NAME = '.htaccess';
27
28 /**
29 * Returns the file types.
30 *
31 * @return null|string
32 */
33 private function getFileTypes()
34 {
35 $fileTypes = null;
36 $lockedFileTypes = $this->mainConfig->getLockedFileType();
37
38 if ($lockedFileTypes === 'selected') {
39 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getLockedFiles());
40 $fileTypes = ($fileTypes !== '') ? "\.({$fileTypes})" : null;
41 } elseif ($lockedFileTypes === 'not_selected') {
42 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getNotLockedFiles());
43 $fileTypes = ($fileTypes !== '') ? "^\.({$fileTypes})" : null;
44 }
45
46 return $fileTypes;
47 }
48
49 /**
50 * Returns the directory match.
51 *
52 * @return null|string
53 */
54 protected function getDirectoryMatch()
55 {
56 if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
57 return '^.*'.DIRECTORY_SEPARATOR.parent::getDirectoryMatch().'.*$';
58 }
59
60 return parent::getDirectoryMatch();
61 }
62
63 /**
64 * @param string $content
65 *
66 * @return string
67 */
68 private function applyFilters($content)
69 {
70 $fileTypes = $this->getFileTypes();
71
72 if ($fileTypes !== null) {
73 /** @noinspection */
74 $content = "<FilesMatch '{$fileTypes}'>\n{$content}</FilesMatch>\n";
75 }
76
77 return $content;
78 }
79
80 /**
81 * Creates the file content if no permalinks are active.
82 *
83 * @param string $directory
84 *
85 * @return string
86 */
87 private function getFileContent($directory)
88 {
89 $areaName = 'WP-Files';
90 // make .htaccess and .htpasswd
91 $content = "AuthType Basic"."\n";
92 $content .= "AuthName \"{$areaName}\""."\n";
93 $content .= "AuthUserFile {$directory}.htpasswd"."\n";
94 $content .= "require valid-user"."\n";
95
96 return $this->applyFilters($content);
97 }
98
99 /**
100 * Creates the file content if permalinks are active.
101 *
102 * @param string $objectType
103 * @param bool $isSubSite
104 *
105 * @return string
106 */
107 private function getPermalinkFileContent($objectType, $isSubSite = false)
108 {
109 if ($objectType === null) {
110 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
111 }
112
113 $homeRoot = parse_url($this->wordpress->getSiteUrl());
114 $homeRoot = (isset($homeRoot['path']) === true) ? '/'.trim($homeRoot['path'], '/\\').'/' : '/';
115
116 $content = "RewriteEngine On\n";
117 $content .= "RewriteBase {$homeRoot}\n";
118 $content .= "RewriteRule ^index\\.php$ - [L]\n";
119
120 if ($isSubSite === false) {
121 $content .= "RewriteCond %{REQUEST_URI} !.*\/sites\/[0-9]+\/.*\n";
122 }
123
124 $directoryMatch = $this->getDirectoryMatch();
125
126 if ($directoryMatch !== null) {
127 $content .= "RewriteCond %{REQUEST_URI} {$directoryMatch}\n";
128 }
129
130 $content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1 [QSA,L]\n";
131 $content .= "RewriteRule ^(.*)\\?(((?!uamfiletype).)*)$ ";
132 $content .= "{$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 [QSA,L]\n";
133 $content .= "RewriteRule ^(.*)\\?(.*)$ {$homeRoot}index.php?uamgetfile=$1&$2 [QSA,L]\n";
134 $content = $this->applyFilters($content);
135
136 $content = "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
137
138 return $content;
139 }
140
141 /**
142 * Returns the htaccess file name with path.
143 *
144 * @param null|string $directory
145 *
146 * @return string
147 */
148 public function getFileNameWithPath($directory = null)
149 {
150 return $directory.self::FILE_NAME;
151 }
152
153 /**
154 * Generates the htaccess file.
155 *
156 * @param string $directory
157 * @param string $objectType
158 *
159 * @return bool
160 */
161 public function create($directory, $objectType = null)
162 {
163 $directory = rtrim($directory, '/').'/';
164
165 if ($this->wordpress->gotModRewrite() === false) {
166 $content = $this->getFileContent($directory);
167 $this->createPasswordFile(true, $directory);
168 } else {
169 $content = $this->getPermalinkFileContent(
170 $objectType,
171 preg_match('/.*\/sites\/[0-9]+\/$/', $directory) !== 0
172 );
173 }
174
175 // save files
176 $fileWithPath = $this->getFileNameWithPath($directory);
177
178 try {
179 file_put_contents($fileWithPath, $content);
180 return true;
181 } catch (\Exception $exception) {
182 // Because file_put_contents can throw exceptions we use this try catch block
183 // to return the success result instead of an exception
184 }
185
186 return false;
187 }
188
189 /**
190 * Deletes the htaccess files.
191 *
192 * @param string $directory
193 *
194 * @return bool
195 */
196 public function delete($directory)
197 {
198 return $this->deleteFiles($directory);
199 }
200 }
201