-
Notifications
You must be signed in to change notification settings - Fork 275
/
MakeDirStep.php
102 lines (85 loc) · 3.14 KB
/
MakeDirStep.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Application\Installation;
use Icinga\Web\Setup\Step;
class MakeDirStep extends Step
{
protected $paths;
protected $dirmode;
protected $errors;
/**
* @param array $paths
* @param string $dirmode
*/
public function __construct($paths, $dirmode)
{
$this->paths = $paths;
$this->dirmode = octdec($dirmode) | octdec('111'); // Make sure that the directories can be traversed
$this->errors = array();
}
public function apply()
{
$success = true;
foreach ($this->paths as $path) {
if (false === file_exists($path)) {
if (false === @mkdir($path)) {
$this->errors[$path] = error_get_last();
$success = false;
} else {
$this->errors[$path] = null;
$old = umask(0);
chmod($path, $this->dirmode);
umask($old);
}
}
}
return $success;
}
public function getSummary()
{
$pageHtml = '';
$pageTitle = t('Directory Creation');
$createMsg = t('The setup will create the following directories:');
$existsMsg = t('The setup does not need to create the following already existing directories:');
$toBeCreated = array_filter($this->paths, function ($p) { return false === file_exists($p); });
if (false === empty($toBeCreated)) {
$pageHtml .= '<p>' . $createMsg . '</p>';
$pageHtml .= '<ul>';
foreach ($toBeCreated as $path) {
$pageHtml .= '<li>' . $path . '</li>';
}
$pageHtml .= '</ul>';
}
$existing = array_diff($this->paths, $toBeCreated);
if (false === empty($existing)) {
$pageHtml .= '<p>' . $existsMsg . '</p>';
$pageHtml .= '<ul>';
foreach ($existing as $path) {
$pageHtml .= '<li>' . $path . '</li>';
}
$pageHtml .= '</ul>';
}
return '<h2>' . $pageTitle . '</h2>' . $pageHtml;
}
public function getReport()
{
$okMessage = t('Directory "%s" in "%s" has been successfully created.');
$existsMessage = t('Directory "%s" does already exist in "%s". Nothing to do.');
$failMessage = t('Unable to create directory "%s" in "%s". An error occured:');
$report = '';
foreach ($this->paths as $path) {
if (array_key_exists($path, $this->errors)) {
if (is_array($this->errors[$path])) {
$report .= '<p class="error">' . sprintf($failMessage, basename($path), dirname($path)) . '</p>'
. '<p>' . $this->errors[$path]['message'] . '</p>';
} else {
$report .= '<p>' . sprintf($okMessage, basename($path), dirname($path)) . '</p>';
}
} else {
$report .= '<p>' . sprintf($existsMessage, basename($path), dirname($path)) . '</p>';
}
}
return $report;
}
}