1
|
|
|
<?php |
2
|
|
|
namespace BretRZaun\StatusPage\Check; |
3
|
|
|
|
4
|
|
|
use BretRZaun\StatusPage\Result; |
5
|
|
|
|
6
|
|
|
class FileCheck extends AbstractCheck |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $filename; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var boolean |
16
|
|
|
*/ |
17
|
|
|
protected $writable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var integer |
21
|
|
|
*/ |
22
|
|
|
protected $maxage; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $unwantedRegex; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* FileCheck constructor. |
31
|
|
|
* @param $label |
32
|
|
|
* @param $filename |
33
|
|
|
*/ |
34
|
|
|
public function __construct($label, $filename) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($label); |
37
|
|
|
$this->filename = $filename; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $filename |
42
|
|
|
* @return FileCheck |
43
|
|
|
*/ |
44
|
|
|
public function setFilename($filename) |
45
|
|
|
{ |
46
|
|
|
$this->filename = $filename; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return FileCheck |
52
|
|
|
*/ |
53
|
|
|
public function setWritable() |
54
|
|
|
{ |
55
|
|
|
$this->writable = true; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $maxage |
61
|
|
|
* @return FileCheck |
62
|
|
|
*/ |
63
|
|
|
public function setMaxage($maxage) |
64
|
|
|
{ |
65
|
|
|
$this->maxage = $maxage; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $unwantedRegex |
71
|
|
|
* @return FileCheck |
72
|
|
|
*/ |
73
|
|
|
public function setUnwantedRegex($unwantedRegex) |
74
|
|
|
{ |
75
|
|
|
$this->unwantedRegex = $unwantedRegex; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Result |
82
|
|
|
*/ |
83
|
|
|
public function check(): Result |
84
|
|
|
{ |
85
|
|
|
$result = new Result($this->label); |
86
|
|
|
|
87
|
|
View Code Duplication |
if (!file_exists($this->filename)) { |
|
|
|
|
88
|
|
|
$result->setSuccess(false); |
89
|
|
|
$result->setError($this->filename." does not exist!"); |
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (null !== $this->maxage) { |
94
|
|
|
$mtime = filemtime($this->filename); |
95
|
|
|
if ($mtime === false) { |
96
|
|
|
$result->setSuccess(false); |
97
|
|
|
$result->setError("mtime() returns error"); |
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
$age = (time()-$mtime); |
101
|
|
|
$age = round($age/60); // sec-to-min |
102
|
|
View Code Duplication |
if ($age > (int)$this->maxage) { |
|
|
|
|
103
|
|
|
$result->setSuccess(false); |
104
|
|
|
$result->setError($this->filename." is to old!"); |
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (null !== $this->writable && !is_writable($this->filename)) { |
110
|
|
|
$result->setSuccess(false); |
111
|
|
|
$result->setError($this->filename." is not writable!"); |
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (null !== $this->unwantedRegex) { |
116
|
|
|
$fp = fopen($this->filename, 'r'); |
117
|
|
|
if ($fp === false) { |
118
|
|
|
$result->setSuccess(false); |
119
|
|
|
$result->setError("fopen() returns error"); |
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
$linenr = 0; |
123
|
|
|
while($line = fgets($fp)) { |
124
|
|
|
$linenr++; |
125
|
|
|
if (preg_match('~'.$this->unwantedRegex.'~i', $line)) { |
126
|
|
|
$result->setSuccess(false); |
127
|
|
|
$result->setError("Found '".$this->unwantedRegex."' in '".$line."' [".$this->filename.":".$linenr."]"); |
128
|
|
|
fclose($fp); |
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
fclose($fp); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.