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