PluginProbe
User Access Manager / 2.0.3
User Access Manager v2.0.3
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 / UserAccessManager / FileHandler / ApacheFileProtection.php

ApacheFileProtection.php in User Access Manager 2.0.3, at src/UserAccessManager/FileHandler/ApacheFileProtection.php

123 lines 3.8 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\FileHandler;
16
17 use UserAccessManager\ObjectHandler\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 * Generates the htaccess file.
30 *
31 * @param string $dir
32 * @param string $objectType
33 *
34 * @return bool
35 */
36 public function create($dir, $objectType = null)
37 {
38 $dir = rtrim($dir, '/').'/';
39 $content = '';
40 $areaName = 'WP-Files';
41
42 if ($this->config->isPermalinksActive() === false) {
43 $fileTypes = null;
44 $lockFileTypes = $this->config->getLockFileTypes();
45
46 if ($lockFileTypes === 'selected') {
47 $fileTypes = $this->cleanUpFileTypes($this->config->getLockedFileTypes());
48 $fileTypes = "\.({$fileTypes})";
49 } elseif ($lockFileTypes === 'not_selected') {
50 $fileTypes = $this->cleanUpFileTypes($this->config->getLockedFileTypes());
51 $fileTypes = "^\.({$fileTypes})";
52 }
53
54 // make .htaccess and .htpasswd
55 $content .= "AuthType Basic"."\n";
56 $content .= "AuthName \"{$areaName}\""."\n";
57 $content .= "AuthUserFile {$dir}.htpasswd"."\n";
58 $content .= "require valid-user"."\n";
59
60 if ($fileTypes !== null) {
61 /** @noinspection */
62 $content = "<FilesMatch '{$fileTypes}'>\n{$content}</FilesMatch>\n";
63 }
64
65 $this->createPasswordFile(true, $dir);
66 } else {
67 if ($objectType === null) {
68 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
69 }
70
71 $homeRoot = parse_url($this->wordpress->getHomeUrl());
72 $homeRoot = (isset($homeRoot['path']) === true) ? trim($homeRoot['path'], '/\\').'/' : '/';
73
74 $content = "<IfModule mod_rewrite.c>\n";
75 $content .= "RewriteEngine On\n";
76 $content .= "RewriteBase {$homeRoot}\n";
77 $content .= "RewriteRule ^index\\.php$ - [L]\n";
78 $content .= "RewriteRule ^([^?]*)$ {$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1 [QSA,L]\n";
79 $content .= "RewriteRule ^(.*)\\?(((?!uamfiletype).)*)$ ";
80 $content .= "{$homeRoot}index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 [QSA,L]\n";
81 $content .= "RewriteRule ^(.*)\\?(.*)$ {$homeRoot}index.php?uamgetfile=$1&$2 [QSA,L]\n";
82 $content .= "</IfModule>\n";
83 }
84
85 // save files
86 $fileWithPath = $dir.self::FILE_NAME;
87
88 try {
89 file_put_contents($fileWithPath, $content);
90 return true;
91 } catch (\Exception $exception) {
92 }
93
94 return false;
95 }
96
97 /**
98 * Deletes the htaccess files.
99 *
100 * @param string $dir
101 *
102 * @return bool
103 */
104 public function delete($dir)
105 {
106 $success = true;
107 $dir = rtrim($dir, '/').'/';
108 $fileName = $dir.self::FILE_NAME;
109
110 if (file_exists($fileName) === true) {
111 $success = ($this->php->unlink($fileName) === true) && $success;
112 }
113
114 $passwordFile = $dir.self::PASSWORD_FILE_NAME;
115
116 if (file_exists($passwordFile) === true) {
117 $success = ($this->php->unlink($passwordFile) === true) && $success;
118 }
119
120 return $success;
121 }
122 }
123