| 1 |
<?php |
| 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 |
namespace Liuch\DmarcSrg\Sources; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Core; |
| 35 |
use Liuch\DmarcSrg\ReportFile\ReportFile; |
| 36 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 37 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class DirectorySource extends Source |
| 43 |
{ |
| 44 |
private $path = null; |
| 45 |
private $list = null; |
| 46 |
private $index = 0; |
| 47 |
private $params = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function setParams(array $params): void |
| 61 |
{ |
| 62 |
$this->params = []; |
| 63 |
$this->params['when_done'] = SourceAction::fromSetting( |
| 64 |
$params['when_done'] ?? [], |
| 65 |
SourceAction::FLAG_BASENAME, |
| 66 |
'delete' |
| 67 |
); |
| 68 |
$this->params['when_failed'] = SourceAction::fromSetting( |
| 69 |
$params['when_failed'] ?? [], |
| 70 |
SourceAction::FLAG_BASENAME, |
| 71 |
'move_to:failed' |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function current(): object |
| 81 |
{ |
| 82 |
return ReportFile::fromFile($this->list[$this->index]); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function key(): int |
| 91 |
{ |
| 92 |
return $this->index; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function next(): void |
| 101 |
{ |
| 102 |
++$this->index; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function rewind(): void |
| 111 |
{ |
| 112 |
if (is_null($this->list)) { |
| 113 |
$this->path = $this->data->toArray()['location']; |
| 114 |
if (!is_dir($this->path)) { |
| 115 |
throw new SoftException("The {$this->path} directory does not exist!"); |
| 116 |
} |
| 117 |
try { |
| 118 |
$fs = new \FilesystemIterator($this->path); |
| 119 |
} catch (\Exception $e) { |
| 120 |
throw new RuntimeException("Error accessing directory {$this->path}", -1, $e); |
| 121 |
} |
| 122 |
$this->list = []; |
| 123 |
foreach ($fs as $entry) { |
| 124 |
if ($entry->isFile()) { |
| 125 |
$this->list[] = $entry->getPathname(); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
if (is_null($this->params)) { |
| 130 |
$this->setParams([]); |
| 131 |
} |
| 132 |
$this->index = 0; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function valid(): bool |
| 141 |
{ |
| 142 |
return isset($this->list[$this->index]); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function accepted(): void |
| 151 |
{ |
| 152 |
$this->processReportFileActions($this->params['when_done']); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function rejected(): void |
| 161 |
{ |
| 162 |
$this->processReportFileActions($this->params['when_failed']); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function type(): int |
| 171 |
{ |
| 172 |
return Source::SOURCE_DIRECTORY; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private function logError(string $message): void |
| 181 |
{ |
| 182 |
Core::instance()->logger()->error($message); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private function processReportFileActions(array &$actions): void |
| 193 |
{ |
| 194 |
foreach ($actions as $sa) { |
| 195 |
switch ($sa->type) { |
| 196 |
case SourceAction::ACTION_DELETE: |
| 197 |
$this->deleteReportFile(); |
| 198 |
break; |
| 199 |
case SourceAction::ACTION_MOVE: |
| 200 |
$this->moveReportFile($sa->param); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private function deleteReportFile(): void |
| 212 |
{ |
| 213 |
try { |
| 214 |
unlink($this->list[$this->index]); |
| 215 |
} catch (\ErrorException $e) { |
| 216 |
$error_message = "Error deleting file from directory {$this->path}"; |
| 217 |
$this->logError($error_message); |
| 218 |
throw new RuntimeException($error_message, -1, $e); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
private function moveReportFile(string $dir_name): void |
| 230 |
{ |
| 231 |
$fdir = $this->path . $dir_name; |
| 232 |
if (!is_dir($fdir)) { |
| 233 |
try { |
| 234 |
mkdir($fdir); |
| 235 |
} catch (\ErrorException $e) { |
| 236 |
$e = new RuntimeException("Error creating directory {$fdir}/", -1, $e); |
| 237 |
$this->logError(strval($e)); |
| 238 |
throw $e; |
| 239 |
} |
| 240 |
try { |
| 241 |
chmod($fdir, 0700); |
| 242 |
} catch (\ErrorException $e) { |
| 243 |
$this->logError(strval($e)); |
| 244 |
} |
| 245 |
} |
| 246 |
$file = $this->list[$this->index]; |
| 247 |
try { |
| 248 |
rename($file, $fdir . '/' . basename($file)); |
| 249 |
} catch (\ErrorException $e) { |
| 250 |
$e = new RuntimeException("Error moving file to directory {$fdir}/", -1, $e); |
| 251 |
$this->logError(strval($e)); |
| 252 |
throw $e; |
| 253 |
} |
| 254 |
} |
| 255 |
} |