generated from smedev/Template-for-SMEServer-Contribs-Package
Add in software files and templates
This commit is contained in:
255
root/opt/dmarc-srg/classes/Sources/DirectorySource.php
Normal file
255
root/opt/dmarc-srg/classes/Sources/DirectorySource.php
Normal file
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
|
||||
* Copyright (C) 2020 Aleksey Andreev (liuch)
|
||||
*
|
||||
* Available at:
|
||||
* https://github.com/liuch/dmarc-srg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* =========================
|
||||
*
|
||||
* This file contains the class DirectorySource
|
||||
*
|
||||
* @category API
|
||||
* @package DmarcSrg
|
||||
* @author Aleksey Andreev (liuch)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
||||
*/
|
||||
|
||||
namespace Liuch\DmarcSrg\Sources;
|
||||
|
||||
use Liuch\DmarcSrg\Core;
|
||||
use Liuch\DmarcSrg\ReportFile\ReportFile;
|
||||
use Liuch\DmarcSrg\Exception\SoftException;
|
||||
use Liuch\DmarcSrg\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* This class is designed to process report files from local server directories.
|
||||
*/
|
||||
class DirectorySource extends Source
|
||||
{
|
||||
private $path = null;
|
||||
private $list = null;
|
||||
private $index = 0;
|
||||
private $params = null;
|
||||
|
||||
/**
|
||||
* Sets parameters that difine the behavior of the source
|
||||
*
|
||||
* @param $params Key-value array
|
||||
* 'when_done' => one or more rules to be executed after successful report processing
|
||||
* (array|string)
|
||||
* 'when_failed' => one or more rules to be executed after report processing fails
|
||||
* (array|string)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setParams(array $params): void
|
||||
{
|
||||
$this->params = [];
|
||||
$this->params['when_done'] = SourceAction::fromSetting(
|
||||
$params['when_done'] ?? [],
|
||||
SourceAction::FLAG_BASENAME,
|
||||
'delete'
|
||||
);
|
||||
$this->params['when_failed'] = SourceAction::fromSetting(
|
||||
$params['when_failed'] ?? [],
|
||||
SourceAction::FLAG_BASENAME,
|
||||
'move_to:failed'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of the ReportFile class for the current file.
|
||||
*
|
||||
* @return ReportFile
|
||||
*/
|
||||
public function current(): object
|
||||
{
|
||||
return ReportFile::fromFile($this->list[$this->index]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the currect file.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function key(): int
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves forward to the next file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next(): void
|
||||
{
|
||||
++$this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewinds the position to the first file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind(): void
|
||||
{
|
||||
if (is_null($this->list)) {
|
||||
$this->path = $this->data->toArray()['location'];
|
||||
if (!is_dir($this->path)) {
|
||||
throw new SoftException("The {$this->path} directory does not exist!");
|
||||
}
|
||||
try {
|
||||
$fs = new \FilesystemIterator($this->path);
|
||||
} catch (\Exception $e) {
|
||||
throw new RuntimeException("Error accessing directory {$this->path}", -1, $e);
|
||||
}
|
||||
$this->list = [];
|
||||
foreach ($fs as $entry) {
|
||||
if ($entry->isFile()) {
|
||||
$this->list[] = $entry->getPathname();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_null($this->params)) {
|
||||
$this->setParams([]);
|
||||
}
|
||||
$this->index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current postion is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid(): bool
|
||||
{
|
||||
return isset($this->list[$this->index]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the accepted report file according to the settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function accepted(): void
|
||||
{
|
||||
$this->processReportFileActions($this->params['when_done']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the rejected report file according to the settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rejected(): void
|
||||
{
|
||||
$this->processReportFileActions($this->params['when_failed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns type of the source.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function type(): int
|
||||
{
|
||||
return Source::SOURCE_DIRECTORY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs an error message
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
private function logError(string $message): void
|
||||
{
|
||||
Core::instance()->logger()->error($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the current report file according to settings
|
||||
*
|
||||
* @param array $actions List of actions to apply to the file
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function processReportFileActions(array &$actions): void
|
||||
{
|
||||
foreach ($actions as $sa) {
|
||||
switch ($sa->type) {
|
||||
case SourceAction::ACTION_DELETE:
|
||||
$this->deleteReportFile();
|
||||
break;
|
||||
case SourceAction::ACTION_MOVE:
|
||||
$this->moveReportFile($sa->param);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the current report file
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function deleteReportFile(): void
|
||||
{
|
||||
try {
|
||||
unlink($this->list[$this->index]);
|
||||
} catch (\ErrorException $e) {
|
||||
$error_message = "Error deleting file from directory {$this->path}";
|
||||
$this->logError($error_message);
|
||||
throw new RuntimeException($error_message, -1, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the current report file
|
||||
*
|
||||
* @param string $dir_name Directory name where to move the report file to
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function moveReportFile(string $dir_name): void
|
||||
{
|
||||
$fdir = $this->path . $dir_name;
|
||||
if (!is_dir($fdir)) {
|
||||
try {
|
||||
mkdir($fdir);
|
||||
} catch (\ErrorException $e) {
|
||||
$e = new RuntimeException("Error creating directory {$fdir}/", -1, $e);
|
||||
$this->logError(strval($e));
|
||||
throw $e;
|
||||
}
|
||||
try {
|
||||
chmod($fdir, 0700);
|
||||
} catch (\ErrorException $e) {
|
||||
$this->logError(strval($e));
|
||||
}
|
||||
}
|
||||
$file = $this->list[$this->index];
|
||||
try {
|
||||
rename($file, $fdir . '/' . basename($file));
|
||||
} catch (\ErrorException $e) {
|
||||
$e = new RuntimeException("Error moving file to directory {$fdir}/", -1, $e);
|
||||
$this->logError(strval($e));
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
238
root/opt/dmarc-srg/classes/Sources/MailboxSource.php
Normal file
238
root/opt/dmarc-srg/classes/Sources/MailboxSource.php
Normal file
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
|
||||
* Copyright (C) 2020 Aleksey Andreev (liuch)
|
||||
*
|
||||
* Available at:
|
||||
* https://github.com/liuch/dmarc-srg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* =========================
|
||||
*
|
||||
* This file contains the class MailboxSource
|
||||
*
|
||||
* @category API
|
||||
* @package DmarcSrg
|
||||
* @author Aleksey Andreev (liuch)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
||||
*/
|
||||
|
||||
namespace Liuch\DmarcSrg\Sources;
|
||||
|
||||
use Liuch\DmarcSrg\ReportFile\ReportFile;
|
||||
use Liuch\DmarcSrg\Exception\SoftException;
|
||||
use Liuch\DmarcSrg\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* This class is designed to process report files from an mail box.
|
||||
*/
|
||||
class MailboxSource extends Source
|
||||
{
|
||||
private $list = null;
|
||||
private $index = 0;
|
||||
private $msg = null;
|
||||
private $params = null;
|
||||
|
||||
/**
|
||||
* Sets parameters that difine the behavior of the source
|
||||
*
|
||||
* @param $params Key-value array
|
||||
* 'when_done' => one or more rules to be executed after successful report processing
|
||||
* (array|string)
|
||||
* 'when_failed' => one or more rules to be executed after report processing fails
|
||||
* (array|string)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setParams(array $params): void
|
||||
{
|
||||
$this->params = [];
|
||||
$this->params['when_done'] = SourceAction::fromSetting(
|
||||
$params['when_done'] ?? [],
|
||||
0,
|
||||
'mark_seen'
|
||||
);
|
||||
$this->params['when_failed'] = SourceAction::fromSetting(
|
||||
$params['when_failed'] ?? [],
|
||||
0,
|
||||
'move_to:failed'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of the ReportFile class for the current email message.
|
||||
*
|
||||
* @return ReportFile
|
||||
*/
|
||||
public function current(): object
|
||||
{
|
||||
$this->msg = $this->data->message($this->list[$this->index]);
|
||||
try {
|
||||
$this->msg->validate();
|
||||
} catch (SoftException $e) {
|
||||
throw new SoftException('Incorrect message: ' . $e->getMessage(), $e->getCode());
|
||||
} catch (RuntimeException $e) {
|
||||
throw new RuntimeException('Incorrect message', -1, $e);
|
||||
}
|
||||
$att = $this->msg->attachment();
|
||||
return ReportFile::fromStream($att->datastream(), $att->filename(), $att->mimeType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the currect email message.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function key(): int
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves forward to the next email message
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next(): void
|
||||
{
|
||||
$this->msg = null;
|
||||
++$this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of unread messages and rewinds the position to the first email message.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind(): void
|
||||
{
|
||||
$this->msg = null;
|
||||
$this->list = $this->data->sort(SORTDATE, 'UNSEEN', false);
|
||||
$this->index = 0;
|
||||
if (is_null($this->params)) {
|
||||
$this->setParams([]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current postion is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid(): bool
|
||||
{
|
||||
return isset($this->list[$this->index]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the accepted email messages according to the settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function accepted(): void
|
||||
{
|
||||
if ($this->msg) {
|
||||
$this->processMessageActions($this->params['when_done']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the rejected email messages according to the settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rejected(): void
|
||||
{
|
||||
$this->processMessageActions($this->params['when_failed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns type of the source.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function type(): int
|
||||
{
|
||||
return Source::SOURCE_MAILBOX;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current email message.
|
||||
*
|
||||
* @return MailMessage|null
|
||||
*/
|
||||
public function mailMessage()
|
||||
{
|
||||
return $this->msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the current report message according to settings
|
||||
*
|
||||
* @param array $actions List of actions to apply to the message
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function processMessageActions(array &$actions): void
|
||||
{
|
||||
foreach ($actions as $sa) {
|
||||
switch ($sa->type) {
|
||||
case SourceAction::ACTION_SEEN:
|
||||
$this->markMessageSeen();
|
||||
break;
|
||||
case SourceAction::ACTION_MOVE:
|
||||
$this->moveMessage($sa->param);
|
||||
break;
|
||||
case SourceAction::ACTION_DELETE:
|
||||
$this->deleteMessage();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the current report message as seen
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function markMessageSeen(): void
|
||||
{
|
||||
$this->msg->setSeen();
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the current report message
|
||||
*
|
||||
* @param string $mbox_name Child mailbox name where to move the current message to.
|
||||
* If the target mailbox does not exists, it will be created.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function moveMessage(string $mbox_name): void
|
||||
{
|
||||
$this->data->ensureMailbox($mbox_name);
|
||||
$this->data->moveMessage($this->list[$this->index], $mbox_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the current report message
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function deleteMessage(): void
|
||||
{
|
||||
$this->data->deleteMessage($this->list[$this->index]);
|
||||
}
|
||||
}
|
115
root/opt/dmarc-srg/classes/Sources/Source.php
Normal file
115
root/opt/dmarc-srg/classes/Sources/Source.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
|
||||
* Copyright (C) 2020 Aleksey Andreev (liuch)
|
||||
*
|
||||
* Available at:
|
||||
* https://github.com/liuch/dmarc-srg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* =========================
|
||||
*
|
||||
* This file contains the class Source
|
||||
*
|
||||
* @category API
|
||||
* @package DmarcSrg
|
||||
* @author Aleksey Andreev (liuch)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
||||
*/
|
||||
|
||||
namespace Liuch\DmarcSrg\Sources;
|
||||
|
||||
/**
|
||||
* It's an abstract class for easy access to reports of a report source
|
||||
*/
|
||||
abstract class Source implements \Iterator
|
||||
{
|
||||
public const SOURCE_UPLOADED_FILE = 1;
|
||||
public const SOURCE_MAILBOX = 2;
|
||||
public const SOURCE_DIRECTORY = 3;
|
||||
|
||||
protected $data = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed Data to reach report files
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets parameters that difine the behavior of the source
|
||||
*
|
||||
* @param $params Key-value array
|
||||
* 'when_done' => one or more rules to be executed after successful report processing
|
||||
* (array|string)
|
||||
* 'when_failed' => one or more rules to be executed after report processing fails
|
||||
* (array|string)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setParams(array $params): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator interface methods
|
||||
*/
|
||||
abstract public function current(): object;
|
||||
abstract public function key(): int;
|
||||
abstract public function next(): void;
|
||||
abstract public function rewind(): void;
|
||||
abstract public function valid(): bool;
|
||||
|
||||
/**
|
||||
* Called when the current report has been successfully processed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function accepted(): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the current report has been rejected.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rejected(): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns type of source, i.e. one of Source::SOURCE_* values
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function type(): int;
|
||||
|
||||
/**
|
||||
* Returns the source itself that was passed to the constructor
|
||||
*
|
||||
* @return class
|
||||
*/
|
||||
public function container()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
156
root/opt/dmarc-srg/classes/Sources/SourceAction.php
Normal file
156
root/opt/dmarc-srg/classes/Sources/SourceAction.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
|
||||
* Copyright (C) 2020 Aleksey Andreev (liuch)
|
||||
*
|
||||
* Available at:
|
||||
* https://github.com/liuch/dmarc-srg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* =========================
|
||||
*
|
||||
* This file contains the class SourceAction
|
||||
*
|
||||
* @category API
|
||||
* @package DmarcSrg
|
||||
* @author Aleksey Andreev (liuch)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
||||
*/
|
||||
|
||||
namespace Liuch\DmarcSrg\Sources;
|
||||
|
||||
use Liuch\DmarcSrg\Exception\LogicException;
|
||||
|
||||
/**
|
||||
* It's a class for describing one action of source
|
||||
*/
|
||||
class SourceAction
|
||||
{
|
||||
public const ACTION_SEEN = 1;
|
||||
public const ACTION_MOVE = 2;
|
||||
public const ACTION_DELETE = 3;
|
||||
public const FLAG_BASENAME = 1;
|
||||
|
||||
private $valid = false;
|
||||
private $type = 0;
|
||||
private $param = null;
|
||||
|
||||
/**
|
||||
* The constructor
|
||||
*
|
||||
* @param string $action Action name with parameter separated by colon
|
||||
* Examples: 'move_to:failed', 'delete'
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __construct(string $action)
|
||||
{
|
||||
if (($delim_offset = mb_strpos($action, ':')) === false) {
|
||||
$name = $action;
|
||||
$param = null;
|
||||
} else {
|
||||
$name = mb_substr($action, 0, $delim_offset);
|
||||
$param = mb_substr($action, $delim_offset + 1);
|
||||
}
|
||||
switch ($name) {
|
||||
case 'mark_seen':
|
||||
$this->type = self::ACTION_SEEN;
|
||||
if (!empty($param)) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case 'move_to':
|
||||
$this->type = self::ACTION_MOVE;
|
||||
if (empty($param)) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case 'delete':
|
||||
$this->type = self::ACTION_DELETE;
|
||||
if (!empty($param)) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
$this->param = $param;
|
||||
$this->valid = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The getter
|
||||
*
|
||||
* @param string $name Property name. Must be one of the following: 'type', 'param'
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
if (in_array($name, [ 'type', 'param' ])) {
|
||||
return $this->$name;
|
||||
}
|
||||
throw new LogicException('Undefined property: ' . $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a setting, flags, and returns an array of SourceAction instances
|
||||
*
|
||||
* @param string|array $setting Setting from the conf.php
|
||||
* @param int $flags Flags of extra checking the result
|
||||
* @param string $default Action to add if the result array is empty
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function fromSetting($setting, int $flags, string $default): array
|
||||
{
|
||||
if (gettype($setting) !== 'array') {
|
||||
$setting = [ $setting ];
|
||||
}
|
||||
$tmap = [];
|
||||
$list = [];
|
||||
foreach ($setting as $it) {
|
||||
if (gettype($it) === 'string') {
|
||||
$sa = new self($it);
|
||||
if ($sa->valid && !isset($tmap[$sa->type])) {
|
||||
if (($flags & self::FLAG_BASENAME) && !self::checkBasename($sa)) {
|
||||
continue;
|
||||
}
|
||||
$list[] = $sa;
|
||||
$tmap[$sa->type] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count($list) === 0) {
|
||||
$sa = new self($default);
|
||||
if ($sa->valid) {
|
||||
$list[] = $sa;
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the param is just a directory name without a path
|
||||
*
|
||||
* @param self $sa
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function checkBasename($sa): bool
|
||||
{
|
||||
return ($sa->type !== self::ACTION_MOVE || basename($sa->param) === $sa->param);
|
||||
}
|
||||
}
|
113
root/opt/dmarc-srg/classes/Sources/UploadedFilesSource.php
Normal file
113
root/opt/dmarc-srg/classes/Sources/UploadedFilesSource.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
|
||||
* Copyright (C) 2020 Aleksey Andreev (liuch)
|
||||
*
|
||||
* Available at:
|
||||
* https://github.com/liuch/dmarc-srg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* =========================
|
||||
*
|
||||
* This file contains the class UploadedFilesSource
|
||||
*
|
||||
* @category API
|
||||
* @package DmarcSrg
|
||||
* @author Aleksey Andreev (liuch)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
||||
*/
|
||||
|
||||
namespace Liuch\DmarcSrg\Sources;
|
||||
|
||||
use Liuch\DmarcSrg\ReportFile\ReportFile;
|
||||
use Liuch\DmarcSrg\Exception\SoftException;
|
||||
|
||||
/**
|
||||
* This class is designed to process report files from uploaded files.
|
||||
*/
|
||||
class UploadedFilesSource extends Source
|
||||
{
|
||||
private $index = 0;
|
||||
|
||||
/**
|
||||
* Returns an instance of the ReportFile class for the current file.
|
||||
*
|
||||
* @return ReportFile
|
||||
*/
|
||||
public function current(): object
|
||||
{
|
||||
if ($this->data['error'][$this->index] !== UPLOAD_ERR_OK) {
|
||||
throw new SoftException('Failed to upload the report file');
|
||||
}
|
||||
|
||||
$realfname = $this->data['name'][$this->index];
|
||||
$tempfname = $this->data['tmp_name'][$this->index];
|
||||
if (!is_uploaded_file($tempfname)) {
|
||||
throw new SoftException('Possible file upload attack');
|
||||
}
|
||||
|
||||
return ReportFile::fromFile($tempfname, $realfname, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the currect file.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function key(): int
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves forward to the next file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next(): void
|
||||
{
|
||||
++$this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewinds the position to the first file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind(): void
|
||||
{
|
||||
$this->index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current postion is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid(): bool
|
||||
{
|
||||
return isset($this->data['name'][$this->index]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns type of the source.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function type(): int
|
||||
{
|
||||
return Source::SOURCE_UPLOADED_FILE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user