| 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 |
|