| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * FileProtection.php | |
| 4 | + * | |
| 5 | + * The FileProtection 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 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\File; |
| @@ -10,22 +23,69 @@ | ||
| 10 | 23 | use UserAccessManager\Util\Util; |
| 11 | 24 | use UserAccessManager\Wrapper\Php; |
| 12 | 25 | use UserAccessManager\Wrapper\Wordpress; |
| 13 | 26 | |
| 27 | +/** | |
| 28 | + * Class FileProtection | |
| 29 | + * | |
| 30 | + * @package UserAccessManager\FileHandler | |
| 31 | + */ | |
| 14 | 32 | abstract class FileProtection |
| 15 | 33 | { |
| 16 | - public const FILE_NAME = null; | |
| 17 | - public const PASSWORD_FILE_NAME = '.htpasswd'; | |
| 34 | + const FILE_NAME = null; | |
| 35 | + const PASSWORD_FILE_NAME = '.htpasswd'; | |
| 18 | 36 | |
| 37 | + /** | |
| 38 | + * @var Php | |
| 39 | + */ | |
| 40 | + protected $php; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * @var Wordpress | |
| 44 | + */ | |
| 45 | + protected $wordpress; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * @var WordpressConfig | |
| 49 | + */ | |
| 50 | + protected $wordpressConfig; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * @var MainConfig | |
| 54 | + */ | |
| 55 | + protected $mainConfig; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var Util | |
| 59 | + */ | |
| 60 | + protected $util; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * FileProtection constructor. | |
| 64 | + * @param Php $php | |
| 65 | + * @param Wordpress $wordpress | |
| 66 | + * @param WordpressConfig $wordpressConfig | |
| 67 | + * @param MainConfig $mainConfig | |
| 68 | + * @param Util $util | |
| 69 | + */ | |
| 19 | 70 | public function __construct( |
| 20 | - protected Php $php, | |
| 21 | - protected Wordpress $wordpress, | |
| 22 | - protected WordpressConfig $wordpressConfig, | |
| 23 | - protected MainConfig $mainConfig, | |
| 24 | - protected Util $util | |
| 71 | + Php $php, | |
| 72 | + Wordpress $wordpress, | |
| 73 | + WordpressConfig $wordpressConfig, | |
| 74 | + MainConfig $mainConfig, | |
| 75 | + Util $util | |
| 25 | 76 | ) { |
| 77 | + $this->php = $php; | |
| 78 | + $this->wordpress = $wordpress; | |
| 79 | + $this->wordpressConfig = $wordpressConfig; | |
| 80 | + $this->mainConfig = $mainConfig; | |
| 81 | + $this->util = $util; | |
| 26 | 82 | } |
| 27 | 83 | |
| 84 | + /** | |
| 85 | + * Returns the directory match. | |
| 86 | + * @return null|string | |
| 87 | + */ | |
| 28 | 88 | protected function getDirectoryMatch(): ?string |
| 29 | 89 | { |
| 30 | 90 | $directoryMatch = null; |
| 31 | 91 | $lockedDirectoryType = $this->mainConfig->getLockedDirectoryType(); |
| @@ -30,9 +90,9 @@ | ||
| 30 | 90 | $directoryMatch = null; |
| 31 | 91 | $lockedDirectoryType = $this->mainConfig->getLockedDirectoryType(); |
| 32 | 92 | |
| 33 | 93 | if ($lockedDirectoryType === 'wordpress') { |
| 34 | - $directoryMatch = '\d{4}' . DIRECTORY_SEPARATOR . '\d{2}'; | |
| 94 | + $directoryMatch = '[0-9]{4}' . DIRECTORY_SEPARATOR . '[0-9]{2}'; | |
| 35 | 95 | } elseif ($lockedDirectoryType === 'custom') { |
| 36 | 96 | $directoryMatch = $this->mainConfig->getCustomLockedDirectories(); |
| 37 | 97 | } |
| 38 | 98 | |
| @@ -38,8 +98,13 @@ | ||
| 38 | 98 | |
| 39 | 99 | return $directoryMatch; |
| 40 | 100 | } |
| 41 | 101 | |
| 102 | + /** | |
| 103 | + * Cleans up the file types. | |
| 104 | + * @param string $fileTypes The file types which should be cleaned up. | |
| 105 | + * @return string | |
| 106 | + */ | |
| 42 | 107 | protected function cleanUpFileTypes(string $fileTypes): string |
| 43 | 108 | { |
| 44 | 109 | $validFileTypes = []; |
| 45 | 110 | $fileTypes = explode(',', $fileTypes); |
| @@ -55,8 +120,13 @@ | ||
| 55 | 120 | |
| 56 | 121 | return implode('|', $validFileTypes); |
| 57 | 122 | } |
| 58 | 123 | |
| 124 | + /** | |
| 125 | + * Returns the password file name with the path. | |
| 126 | + * @param string|null $dir | |
| 127 | + * @return null|string | |
| 128 | + */ | |
| 59 | 129 | private function getDefaultPasswordFileWithPath(?string $dir): ?string |
| 60 | 130 | { |
| 61 | 131 | if ($dir === null) { |
| 62 | 132 | $wordpressUploadDir = $this->wordpress->getUploadDir(); |
| @@ -68,9 +138,14 @@ | ||
| 68 | 138 | |
| 69 | 139 | return ($dir !== null) ? $dir . static::PASSWORD_FILE_NAME : null; |
| 70 | 140 | } |
| 71 | 141 | |
| 72 | - public function createPasswordFile(bool $createNew = false, ?string $dir = null): void | |
| 142 | + /** | |
| 143 | + * Creates a htpasswd file. | |
| 144 | + * @param boolean $createNew Force to create new file. | |
| 145 | + * @param string $dir The destination directory. | |
| 146 | + */ | |
| 147 | + public function createPasswordFile($createNew = false, $dir = null) | |
| 73 | 148 | { |
| 74 | 149 | $file = $this->getDefaultPasswordFileWithPath($dir); |
| 75 | 150 | |
| 76 | 151 | if ($file !== null && (file_exists($file) === false || $createNew)) { |
| @@ -82,15 +157,15 @@ | ||
| 82 | 157 | if ($this->mainConfig->getFilePassType() === 'random') { |
| 83 | 158 | try { |
| 84 | 159 | $randomPassword = $this->util->getRandomPassword(); |
| 85 | 160 | $password = md5($randomPassword); |
| 86 | - } catch (Exception) { | |
| 161 | + } catch (Exception $exception) { | |
| 87 | 162 | // Do nothing |
| 88 | 163 | } |
| 89 | 164 | } |
| 90 | 165 | |
| 91 | 166 | // make .htpasswd |
| 92 | - $content = "$user:$password\n"; | |
| 167 | + $content = "{$user}:{$password}\n"; | |
| 93 | 168 | |
| 94 | 169 | // save file |
| 95 | 170 | $fileHandler = fopen($file, 'w'); |
| 96 | 171 | fwrite($fileHandler, $content); |
| @@ -97,8 +172,13 @@ | ||
| 97 | 172 | fclose($fileHandler); |
| 98 | 173 | } |
| 99 | 174 | } |
| 100 | 175 | |
| 176 | + /** | |
| 177 | + * Deletes the htaccess files. | |
| 178 | + * @param string $directory | |
| 179 | + * @return bool | |
| 180 | + */ | |
| 101 | 181 | public function deleteFiles(string $directory): bool |
| 102 | 182 | { |
| 103 | 183 | $success = true; |
| 104 | 184 | $directory = rtrim($directory, '/') . '/'; |
| @@ -104,9 +184,9 @@ | ||
| 104 | 184 | $directory = rtrim($directory, '/') . '/'; |
| 105 | 185 | $fileName = $directory . static::FILE_NAME; |
| 106 | 186 | |
| 107 | 187 | if (file_exists($fileName) === true) { |
| 108 | - $success = ($this->php->unlink($fileName) === true); | |
| 188 | + $success = ($this->php->unlink($fileName) === true) && $success; | |
| 109 | 189 | } |
| 110 | 190 | |
| 111 | 191 | $passwordFile = $directory . static::PASSWORD_FILE_NAME; |
| 112 | 192 | |