1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_user_paths; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Interfaces\IProcessDirs; |
8
|
|
|
use kalanis\kw_files\Interfaces\IProcessNodes; |
9
|
|
|
use kalanis\kw_paths\ArrayPath; |
10
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
11
|
|
|
use kalanis\kw_paths\PathsException; |
12
|
|
|
use kalanis\kw_user_paths\Traits\TLang; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Actions |
17
|
|
|
* low-level work with user dirs |
18
|
|
|
*/ |
19
|
|
|
class Actions |
20
|
|
|
{ |
21
|
|
|
use TLang; |
22
|
|
|
|
23
|
|
|
protected UserDir $data; |
24
|
|
|
protected IProcessNodes $nodes; |
25
|
|
|
protected IProcessDirs $dirs; |
26
|
|
|
|
27
|
14 |
|
public function __construct(UserDir $data, IProcessNodes $nodes, IProcessDirs $dirs, ?Interfaces\IUPTranslations $lang = null) |
28
|
|
|
{ |
29
|
14 |
|
$this->data = $data; |
30
|
14 |
|
$this->nodes = $nodes; |
31
|
14 |
|
$this->dirs = $dirs; |
32
|
14 |
|
$this->setUpLang($lang); |
33
|
14 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create inner path tree |
37
|
|
|
* @throws PathsException |
38
|
|
|
* @throws FilesException |
39
|
|
|
* @return string with inner path |
40
|
|
|
*/ |
41
|
3 |
|
public function createTree(): string |
42
|
|
|
{ |
43
|
3 |
|
$path = $this->data->getFullPath(); |
44
|
3 |
|
if ($this->isNoPathSet($path)) { |
45
|
1 |
|
throw new PathsException($this->getUpLang()->upCannotDetermineUserDir()); |
46
|
|
|
} |
47
|
2 |
|
$userPath = $this->data->hasDataDir() ? $path->getArrayDirectory() : $path->getArray(); |
48
|
2 |
|
if (!$this->dirs->createDir($userPath)) { |
49
|
1 |
|
if (!$this->nodes->isDir($userPath)) { |
50
|
1 |
|
throw new PathsException($this->getUpLang()->upCannotCreateUserDir()); |
51
|
|
|
} |
52
|
|
|
} |
53
|
1 |
|
if ($this->data->hasDataDir()) { |
54
|
1 |
|
$this->dirs->createDir(array_merge($userPath, [IPaths::DIR_DATA])); |
55
|
1 |
|
$this->dirs->createDir(array_merge($userPath, [IPaths::DIR_CONF])); |
56
|
1 |
|
$this->dirs->createDir(array_merge($userPath, [IPaths::DIR_STYLE])); |
57
|
|
|
} |
58
|
1 |
|
return $path->getString(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Remove data in user's work dir |
63
|
|
|
* @throws PathsException |
64
|
|
|
* @throws FilesException |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
4 |
|
public function wipeWorkDir(): bool |
68
|
|
|
{ |
69
|
4 |
|
$path = $this->data->getFullPath(); |
70
|
4 |
|
if ($this->isNoPathSet($path)) { |
71
|
1 |
|
throw new PathsException($this->getUpLang()->upCannotDetermineUserDir()); |
72
|
|
|
} |
73
|
3 |
|
if ($this->isPathTooShort($path)) { # There surely will be an idiot who want to remove root data |
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
2 |
|
$whichPath = $this->data->hasDataDir() |
77
|
1 |
|
? array_merge($path->getArrayDirectory(), [IPaths::DIR_DATA]) |
78
|
2 |
|
: $path->getArray(); |
79
|
2 |
|
return $this->dirs->deleteDir($whichPath, true); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Remove everything in user's special sub dirs |
84
|
|
|
* @throws PathsException |
85
|
|
|
* @throws FilesException |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
5 |
|
public function wipeConfDirs(): bool |
89
|
|
|
{ |
90
|
5 |
|
$path = $this->data->getFullPath(); |
91
|
5 |
|
if ($this->isNoPathSet($path)) { |
92
|
1 |
|
throw new PathsException($this->getUpLang()->upCannotDetermineUserDir()); |
93
|
|
|
} |
94
|
4 |
|
if ($this->isPathTooShort($path)) { # There surely will be an idiot who want to remove root data |
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
3 |
|
if (!$this->data->hasDataDir()) { |
98
|
1 |
|
return false; |
99
|
|
|
} |
100
|
2 |
|
$userPath = $path->getArrayDirectory(); |
101
|
2 |
|
$r1 = $this->dirs->deleteDir(array_merge($userPath, [IPaths::DIR_CONF]), true); |
102
|
2 |
|
$r2 = $this->dirs->deleteDir(array_merge($userPath, [IPaths::DIR_STYLE]), true); |
103
|
2 |
|
return $r1 && $r2; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Remove everything in user's home dir and that home dir itself |
108
|
|
|
* @throws PathsException |
109
|
|
|
* @throws FilesException |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
4 |
|
public function wipeHomeDir(): bool |
113
|
|
|
{ |
114
|
4 |
|
$path = $this->data->getFullPath(); |
115
|
4 |
|
if ($this->isNoPathSet($path)) { |
116
|
1 |
|
throw new PathsException($this->getUpLang()->upCannotDetermineUserDir()); |
117
|
|
|
} |
118
|
3 |
|
if ($this->isPathTooShort($path)) { |
119
|
1 |
|
return false; # urcite se najde i blbec, co bude chtit wipe roota (jeste blbejsi napad, nez jsme doufali) - tudy se odinstalace fakt nedela! |
120
|
|
|
} |
121
|
2 |
|
$userPath = $this->data->hasDataDir() ? $path->getArrayDirectory() : $path->getArray(); |
122
|
2 |
|
$r1 = $this->dirs->deleteDir($userPath, true); |
123
|
2 |
|
$this->data->clear(); |
124
|
2 |
|
return $r1; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param ArrayPath $path |
129
|
|
|
* @throws PathsException |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
14 |
|
protected function isNoPathSet(ArrayPath $path): bool |
133
|
|
|
{ |
134
|
14 |
|
return empty($path->getString()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param ArrayPath $path |
139
|
|
|
* @throws PathsException |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
9 |
|
protected function isPathTooShort(ArrayPath $path): bool |
143
|
|
|
{ |
144
|
9 |
|
return (3 > strlen($path->getString())); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
//// how it works //// |
150
|
|
|
|
151
|
|
|
// create user, its dir, subdirs will be made auto |
152
|
|
|
$u = new UserDir(); |
153
|
|
|
$u->setUserName("nom"); |
154
|
|
|
$u->process(); |
155
|
|
|
$src = 'somewhere/on/volume'; |
156
|
|
|
$a = new Actions($u, new \kalanis\kw_files\Processing\Volume\ProcessNode($src), new \kalanis\kw_files\Processing\Volume\ProcessDir($src)); |
157
|
|
|
$a->createTree(); |
158
|
|
|
|
159
|
|
|
// when you do not need subdirs; sometimes necessary |
160
|
|
|
$u->wantDataDir(false); |
161
|
|
|
|
162
|
|
|
// user removal |
163
|
|
|
$u = new UserDir(); |
164
|
|
|
$u->setUserName("nom"); || $u->setUserPath("dat/dumb/dir"); |
165
|
|
|
$u->process(); |
166
|
|
|
$src = 'somewhere/on/volume'; |
167
|
|
|
$a = new Actions($u, new \kalanis\kw_files\Processing\Volume\ProcessNode($src), new \kalanis\kw_files\Processing\Volume\ProcessDir($src)); |
168
|
|
|
$a->wipeWorkDir(); || $a->wipeConfDirs(); || $a->wipeHomeDir(); // by the request and feel |
169
|
|
|
|
170
|
|
|
// create subdirs when they aren't |
171
|
|
|
$u = new UserDir(); |
172
|
|
|
$u->setUserName("nom"); || $u->setUserPath("dat/dumb/dir"); |
173
|
|
|
$u->wantDataDir(true); |
174
|
|
|
$u->process(); |
175
|
|
|
$a = new Actions($u, new \kalanis\kw_files\Processing\Volume\ProcessNode($src), new \kalanis\kw_files\Processing\Volume\ProcessDir($src)); |
176
|
|
|
$a->createTree(); |
177
|
|
|
|
178
|
|
|
// then it's good idea to write that updated form into password file - or the user will see strange things |
179
|
|
|
$where = $u->getUserPath(); |
180
|
|
|
|
181
|
|
|
// current user and its files |
182
|
|
|
$u = new UserDir(); |
183
|
|
|
$u->setUserName("nom"); || $u->setUserPath("dat/dumb/dir"); |
184
|
|
|
$u->process(); |
185
|
|
|
$where = $u->getUserPath(); |
186
|
|
|
|
187
|
|
|
// did that user have home dir and/or subdirs? |
188
|
|
|
$homedir = $u->hasHomeDir(); // beware, returns bool |
189
|
|
|
$subdirs = $u->hasDataDir(); // beware, returns bool |
190
|
|
|
*/ |
191
|
|
|
|
192
|
|
|
|