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

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

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