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