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

155 lines 4.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 $lockFileTypes = $this->mainConfig->getLockFileTypes();
37
38 if ($lockFileTypes === 'selected') {
39 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getLockedFileTypes());
40 $fileTypes = ($fileTypes !== '') ? "\.({$fileTypes})" : null;
41 } elseif ($lockFileTypes === 'not_selected') {
42 $fileTypes = $this->cleanUpFileTypes($this->mainConfig->getNotLockedFileTypes());
43 $fileTypes = ($fileTypes !== '') ? "^\.({$fileTypes})" : null;
44 }
45
46 return $fileTypes;
47 }
48
49 /**
50 * Creates the file content if permalinks are active.
51 *
52 * @param string $directory
53 * @param string $fileTypes
54 *
55 * @return string
56 */
57 private function getPermalinkFileContent($directory, $fileTypes)
58 {
59 $areaName = 'WP-Files';
60 // make .htaccess and .htpasswd
61 $content = "AuthType Basic"."\n";
62 $content .= "AuthName \"{$areaName}\""."\n";
63 $content .= "AuthUserFile {$directory}.htpasswd"."\n";
64 $content .= "require valid-user"."\n";
65
66 if ($fileTypes !== null) {
67 /** @noinspection */
68 $content = "<FilesMatch '{$fileTypes}'>\n{$content}</FilesMatch>\n";
69 }
70
71 return $content;
72 }
73
74 /**
75 * Creates the file content if no permalinks are active.
76 *
77 * @param string $fileTypes
78 * @param string $objectType
79 *
80 * @return string
81 */
82 private function getFileContent($fileTypes, $objectType)
83 {
84 if ($objectType === null) {
85 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
86 }
87
88 $homeRoot = parse_url($this->wordpress->getHomeUrl());
89 $homeRoot = (isset($homeRoot['path']) === true) ? '/'.trim($homeRoot['path'], '/\\').'/' : '/';
90
91 $content = "RewriteEngine On\n";
92 $content .= "RewriteBase {$homeRoot}\n";
93 $content .= "RewriteRule ^index\\.php$ - [L]\n";
94 $content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1 [QSA,L]\n";
95 $content .= "RewriteRule ^(.*)\\?(((?!uamfiletype).)*)$ ";
96 $content .= "{$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 [QSA,L]\n";
97 $content .= "RewriteRule ^(.*)\\?(.*)$ {$homeRoot}index.php?uamgetfile=$1&$2 [QSA,L]\n";
98
99 if ($fileTypes !== null) {
100 /** @noinspection */
101 $content = "<FilesMatch '{$fileTypes}'>\n{$content}</FilesMatch>\n";
102 }
103
104 $content = "<IfModule mod_rewrite.c>\n$content</IfModule>\n";
105
106 return $content;
107 }
108
109 /**
110 * Generates the htaccess file.
111 *
112 * @param string $directory
113 * @param string $objectType
114 *
115 * @return bool
116 */
117 public function create($directory, $objectType = null)
118 {
119 $directory = rtrim($directory, '/').'/';
120 $fileTypes = $this->getFileTypes();
121
122 if ($this->wordpressConfig->isPermalinksActive() === false) {
123 $content = $this->getPermalinkFileContent($directory, $fileTypes);
124 $this->createPasswordFile(true, $directory);
125 } else {
126 $content = $this->getFileContent($fileTypes, $objectType);
127 }
128
129 // save files
130 $fileWithPath = $directory.self::FILE_NAME;
131
132 try {
133 file_put_contents($fileWithPath, $content);
134 return true;
135 } catch (\Exception $exception) {
136 // Because file_put_contents can throw exceptions we use this try catch block
137 // to return the success result instead of an exception
138 }
139
140 return false;
141 }
142
143 /**
144 * Deletes the htaccess files.
145 *
146 * @param string $directory
147 *
148 * @return bool
149 */
150 public function delete($directory)
151 {
152 return $this->deleteFiles($directory);
153 }
154 }
155