PluginProbe
User Access Manager / 2.3.9
User Access Manager v2.3.9
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / File / FileProtection.php

FileProtection.php in User Access Manager 2.3.9, at src/File/FileProtection.php

120 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\File;
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 $directoryMatch = null;
31 $lockedDirectoryType = $this->mainConfig->getLockedDirectoryType();
32
33 if ($lockedDirectoryType === 'wordpress') {
34 $directoryMatch = '[0-9]{4}' . DIRECTORY_SEPARATOR . '[0-9]{2}';
35 } elseif ($lockedDirectoryType === 'custom') {
36 $directoryMatch = $this->mainConfig->getCustomLockedDirectories();
37 }
38
39 return $directoryMatch;
40 }
41
42 protected function cleanUpFileTypes(string $fileTypes): string
43 {
44 $validFileTypes = [];
45 $fileTypes = explode(',', $fileTypes);
46 $mimeTypes = $this->wordpressConfig->getMimeTypes();
47
48 foreach ($fileTypes as $fileType) {
49 $cleanFileType = trim($fileType);
50
51 if (isset($mimeTypes[$cleanFileType]) === true) {
52 $validFileTypes[$cleanFileType] = $cleanFileType;
53 }
54 }
55
56 return implode('|', $validFileTypes);
57 }
58
59 private function getDefaultPasswordFileWithPath(?string $dir): ?string
60 {
61 if ($dir === null) {
62 $wordpressUploadDir = $this->wordpress->getUploadDir();
63
64 if (empty($wordpressUploadDir['error']) === true) {
65 $dir = $wordpressUploadDir['basedir'] . DIRECTORY_SEPARATOR;
66 }
67 }
68
69 return ($dir !== null) ? $dir . static::PASSWORD_FILE_NAME : null;
70 }
71
72 public function createPasswordFile(bool $createNew = false, string $dir = null): void
73 {
74 $file = $this->getDefaultPasswordFileWithPath($dir);
75
76 if ($file !== null && (file_exists($file) === false || $createNew)) {
77 $currentUser = $this->wordpress->getCurrentUser();
78
79 $user = $currentUser->user_login;
80 $password = $currentUser->user_pass;
81
82 if ($this->mainConfig->getFilePassType() === 'random') {
83 try {
84 $randomPassword = $this->util->getRandomPassword();
85 $password = md5($randomPassword);
86 } catch (Exception) {
87 // Do nothing
88 }
89 }
90
91 // make .htpasswd
92 $content = "$user:$password\n";
93
94 // save file
95 $fileHandler = fopen($file, 'w');
96 fwrite($fileHandler, $content);
97 fclose($fileHandler);
98 }
99 }
100
101 public function deleteFiles(string $directory): bool
102 {
103 $success = true;
104 $directory = rtrim($directory, '/') . '/';
105 $fileName = $directory . static::FILE_NAME;
106
107 if (file_exists($fileName) === true) {
108 $success = ($this->php->unlink($fileName) === true);
109 }
110
111 $passwordFile = $directory . static::PASSWORD_FILE_NAME;
112
113 if (file_exists($passwordFile) === true) {
114 $success = ($this->php->unlink($passwordFile) === true) && $success;
115 }
116
117 return $success;
118 }
119 }
120