| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\File\Protection; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\Config\MainConfig; |
| 9 |
use UserAccessManager\Config\WordpressConfig; |
| 10 |
use UserAccessManager\Util\Util; |
| 11 |
use UserAccessManager\Wrapper\Php; |
| 12 |
use UserAccessManager\Wrapper\Wordpress; |
| 13 |
|
| 14 |
abstract class FileProtection |
| 15 |
{ |
| 16 |
public const FILE_NAME = null; |
| 17 |
public const PASSWORD_FILE_NAME = '.htpasswd'; |
| 18 |
|
| 19 |
public function __construct( |
| 20 |
protected Php $php, |
| 21 |
protected Wordpress $wordpress, |
| 22 |
protected WordpressConfig $wordpressConfig, |
| 23 |
protected MainConfig $mainConfig, |
| 24 |
protected Util $util |
| 25 |
) { |
| 26 |
} |
| 27 |
|
| 28 |
protected function getDirectoryMatch(): ?string |
| 29 |
{ |
| 30 |
return match ($this->mainConfig->getLockedDirectoryType()) { |
| 31 |
'wordpress' => '\d{4}' . DIRECTORY_SEPARATOR . '\d{2}', |
| 32 |
'custom' => $this->mainConfig->getCustomLockedDirectories(), |
| 33 |
default => null |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
protected function cleanUpFileTypes(string $fileTypes): string |
| 38 |
{ |
| 39 |
$validFileTypes = []; |
| 40 |
$mimeTypes = $this->wordpressConfig->getMimeTypes(); |
| 41 |
|
| 42 |
foreach (explode(',', $fileTypes) as $fileType) { |
| 43 |
$cleanFileType = trim($fileType); |
| 44 |
|
| 45 |
if (isset($mimeTypes[$cleanFileType]) === true) { |
| 46 |
$validFileTypes[$cleanFileType] = $cleanFileType; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return implode('|', $validFileTypes); |
| 51 |
} |
| 52 |
|
| 53 |
private function getDefaultPasswordFileWithPath(?string $dir): ?string |
| 54 |
{ |
| 55 |
if ($dir === null) { |
| 56 |
$wordpressUploadDir = $this->wordpress->getUploadDir(); |
| 57 |
|
| 58 |
if (empty($wordpressUploadDir['error']) === true) { |
| 59 |
$dir = $wordpressUploadDir['basedir'] . DIRECTORY_SEPARATOR; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return ($dir !== null) ? $dir . static::PASSWORD_FILE_NAME : null; |
| 64 |
} |
| 65 |
|
| 66 |
public function createPasswordFile(bool $createNew = false, ?string $dir = null): void |
| 67 |
{ |
| 68 |
$file = $this->getDefaultPasswordFileWithPath($dir); |
| 69 |
|
| 70 |
if ($file === null || (file_exists($file) === true && $createNew === false)) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$currentUser = $this->wordpress->getCurrentUser(); |
| 75 |
$user = $currentUser->user_login; |
| 76 |
$password = $currentUser->user_pass; |
| 77 |
|
| 78 |
if ($this->mainConfig->getFilePassType() === 'random') { |
| 79 |
try { |
| 80 |
$password = md5($this->util->getRandomPassword()); |
| 81 |
} catch (Exception) { |
| 82 |
// Keep the current user's password hash when no random one can be generated. |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$fileHandler = fopen($file, 'w'); |
| 87 |
fwrite($fileHandler, "$user:$password\n"); |
| 88 |
fclose($fileHandler); |
| 89 |
} |
| 90 |
|
| 91 |
public function deleteFiles(string $directory): bool |
| 92 |
{ |
| 93 |
$success = true; |
| 94 |
$directory = rtrim($directory, '/') . '/'; |
| 95 |
|
| 96 |
foreach ([static::FILE_NAME, static::PASSWORD_FILE_NAME] as $fileName) { |
| 97 |
$file = $directory . $fileName; |
| 98 |
|
| 99 |
if (file_exists($file) === true) { |
| 100 |
$success = ($this->php->unlink($file) === true) && $success; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return $success; |
| 105 |
} |
| 106 |
} |
| 107 |
|