| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
namespace Liuch\DmarcSrg; |
| 44 |
|
| 45 |
use Liuch\DmarcSrg\Users\User; |
| 46 |
use Liuch\DmarcSrg\Domains\Domain; |
| 47 |
use Liuch\DmarcSrg\Domains\DomainList; |
| 48 |
use Liuch\DmarcSrg\Report\SummaryReport; |
| 49 |
use Liuch\DmarcSrg\Report\OverallReport; |
| 50 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 51 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 52 |
|
| 53 |
require realpath(__DIR__ . '/..') . '/init.php'; |
| 54 |
|
| 55 |
if (Core::isJson() && isset($_GET['mode'])) { |
| 56 |
try { |
| 57 |
$core = Core::instance(); |
| 58 |
$core->auth()->isAllowed(User::LEVEL_USER); |
| 59 |
$user = $core->getCurrentUser(); |
| 60 |
|
| 61 |
$mode = $_GET['mode']; |
| 62 |
if ($mode === 'options') { |
| 63 |
Core::sendJson( |
| 64 |
[ |
| 65 |
'domains' => (new DomainList($user))->names() |
| 66 |
] |
| 67 |
); |
| 68 |
return; |
| 69 |
} elseif ($mode === 'report') { |
| 70 |
if (empty($_GET['domain'])) { |
| 71 |
throw new SoftException('Parameter "domain" is not specified'); |
| 72 |
} |
| 73 |
if (empty($_GET['period'])) { |
| 74 |
throw new SoftException('Parameter "period" is not specified'); |
| 75 |
} |
| 76 |
|
| 77 |
$format = $_GET['format'] ?? ''; |
| 78 |
if (!in_array($format, [ 'raw', 'text', 'csv' ])) { |
| 79 |
throw new SoftException('The `format` parameter can only be `raw`, `text` or `csv`'); |
| 80 |
} |
| 81 |
|
| 82 |
$ovr = null; |
| 83 |
$rep = new SummaryReport($_GET['period']); |
| 84 |
if (true) { |
| 85 |
$ovr = new OverallReport(); |
| 86 |
$rep->bindSection($ovr); |
| 87 |
} |
| 88 |
$reports = []; |
| 89 |
foreach (explode(',', $_GET['domain']) as $d) { |
| 90 |
$dom = new Domain($d); |
| 91 |
$dom->isAssigned($user, true); |
| 92 |
$rep->setDomain($dom); |
| 93 |
$r = [ 'domain' => $d ]; |
| 94 |
switch ($format) { |
| 95 |
case 'raw': |
| 96 |
$r['data'] = $rep->toArray(); |
| 97 |
break; |
| 98 |
case 'text': |
| 99 |
$r['text'] = $rep->text(); |
| 100 |
break; |
| 101 |
case 'csv': |
| 102 |
$r['csv'] = $rep->csv(); |
| 103 |
break; |
| 104 |
} |
| 105 |
$reports[] = $r; |
| 106 |
} |
| 107 |
$res['reports'] = $reports; |
| 108 |
if ($ovr) { |
| 109 |
$r = []; |
| 110 |
switch ($format) { |
| 111 |
case 'raw': |
| 112 |
$r['data'] = $ovr->toArray(); |
| 113 |
break; |
| 114 |
case 'text': |
| 115 |
$r['text'] = $ovr->text(); |
| 116 |
break; |
| 117 |
case 'csv': |
| 118 |
$r['csv'] = $ovr->csv(); |
| 119 |
break; |
| 120 |
} |
| 121 |
$res['overall'] = $r; |
| 122 |
} |
| 123 |
|
| 124 |
Core::sendJson($res); |
| 125 |
return; |
| 126 |
} else { |
| 127 |
throw new SoftException('The `mode` parameter can only be `options` or `report`'); |
| 128 |
} |
| 129 |
} catch (RuntimeException $e) { |
| 130 |
Core::sendJson(ErrorHandler::exceptionResult($e)); |
| 131 |
return; |
| 132 |
} |
| 133 |
} elseif (Core::requestMethod() == 'GET') { |
| 134 |
Core::instance()->sendHtml(); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
Core::sendBad(); |