PluginProbe
User Access Manager / 2.0.6
User Access Manager v2.0.6
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 / NginxFileProtection.php

NginxFileProtection.php in User Access Manager 2.0.6, at src/UserAccessManager/FileHandler/NginxFileProtection.php

119 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\FileHandler;
16
17 use UserAccessManager\ObjectHandler\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 * Generates the conf file.
30 *
31 * @param string $dir
32 * @param string $objectType
33 * @param string $absPath
34 *
35 * @return bool
36 */
37 public function create($dir, $objectType = null, $absPath = ABSPATH)
38 {
39 $dir = rtrim($dir, '/').'/';
40 $absPath = rtrim($absPath, '/').'/';
41 $areaName = 'WP-Files';
42
43 if ($this->config->isPermalinksActive() === false) {
44 $fileTypes = null;
45
46 if ($this->config->getLockFileTypes() === 'selected') {
47 $fileTypes = $this->cleanUpFileTypes($this->config->getLockedFileTypes());
48 $fileTypes = "\\.({$fileTypes})";
49 }
50
51 $content = "location ".str_replace($absPath, '/', $dir)." {\n";
52
53 if ($fileTypes !== null) {
54 $content .= "location ~ {$fileTypes} {\n";
55 }
56
57 $content .= "auth_basic \"{$areaName}\";\n";
58 $content .= "auth_basic_user_file {$dir}.htpasswd;\n";
59 $content .= "}\n";
60
61 if ($fileTypes !== null) {
62 $content .= "}\n";
63 }
64
65 $this->createPasswordFile(true, $dir);
66 } else {
67 if ($objectType === null) {
68 $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
69 }
70
71 $content = "location ".str_replace($absPath, '/', $dir)." {\n";
72 $content .= "rewrite ^([^?]*)$ /index.php?uamfiletype={$objectType}&uamgetfile=$1 last;\n";
73 $content .= "rewrite ^(.*)\\?(((?!uamfiletype).)*)$ ";
74 $content .= "/index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 last;\n";
75 $content .= "rewrite ^(.*)\\?(.*)$ /index.php?uamgetfile=$1&$2 last;\n";
76 $content .= "}\n";
77 }
78
79 // save files
80 $fileWithPath = $absPath.self::FILE_NAME;
81
82 try {
83 file_put_contents($fileWithPath, $content);
84 return true;
85 } catch (\Exception $exception) {
86 // Because file_put_contents can throw exceptions we use this try catch block
87 // to return the success result instead of an exception
88 }
89
90 return false;
91 }
92
93 /**
94 * Deletes the conf file.
95 *
96 * @param string $dir
97 *
98 * @return bool
99 */
100 public function delete($dir)
101 {
102 $success = true;
103 $dir = rtrim($dir, '/').'/';
104 $fileName = $dir.self::FILE_NAME;
105
106 if (file_exists($fileName) === true) {
107 $success = ($this->php->unlink($fileName) === true) && $success;
108 }
109
110 $passwordFile = $dir.self::PASSWORD_FILE_NAME;
111
112 if (file_exists($passwordFile) === true) {
113 $success = ($this->php->unlink($passwordFile) === true) && $success;
114 }
115
116 return $success;
117 }
118 }
119