| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Users\User; |
| 26 |
use Liuch\DmarcSrg\Report\ReportList; |
| 27 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 28 |
|
| 29 |
require realpath(__DIR__ . '/..') . '/init.php'; |
| 30 |
|
| 31 |
if (Core::requestMethod() == 'GET') { |
| 32 |
if (Core::isJson() && isset($_GET['list'])) { |
| 33 |
try { |
| 34 |
$core = Core::instance(); |
| 35 |
$core->auth()->isAllowed(User::LEVEL_USER); |
| 36 |
|
| 37 |
$lst = explode(',', $_GET['list']); |
| 38 |
$res = []; |
| 39 |
$first_page = false; |
| 40 |
if (array_search('reports', $lst) !== false) { |
| 41 |
$pos = isset($_GET['position']) ? intval($_GET['position']) : 0; |
| 42 |
$dir = isset($_GET['direction']) ? $_GET['direction'] : 'ascent'; |
| 43 |
$order = isset($_GET['order']) ? $_GET['order'] : 'begin_time'; |
| 44 |
$filter = Common::getFilter(); |
| 45 |
$list = new ReportList($core->getCurrentUser()); |
| 46 |
if ($filter) { |
| 47 |
$list->setFilter($filter); |
| 48 |
} |
| 49 |
$n_dir = $dir === 'ascent' ? |
| 50 |
ReportList::ORDER_ASCENT : ReportList::ORDER_DESCENT; |
| 51 |
$n_order = $order === 'begin_time' ? |
| 52 |
ReportList::ORDER_BEGIN_TIME : ReportList::ORDER_NONE; |
| 53 |
$list->setOrder($n_order, $n_dir); |
| 54 |
$res = $list->getList($pos); |
| 55 |
$first_page = ($pos === 0); |
| 56 |
} |
| 57 |
if (array_search('count', $lst) !== false) { |
| 58 |
if ($first_page && !$res['more'] && intval($_GET['position'] ?? 0) === 0) { |
| 59 |
$tc = 0; |
| 60 |
$uc = 0; |
| 61 |
foreach ($res['reports'] as $r) { |
| 62 |
if (!$r['seen']) { |
| 63 |
++$uc; |
| 64 |
} |
| 65 |
++$tc; |
| 66 |
} |
| 67 |
} else { |
| 68 |
$list = new ReportList($core->getCurrentUser()); |
| 69 |
$filter = Common::getFilter(); |
| 70 |
if ($filter) { |
| 71 |
$list->setFilter($filter); |
| 72 |
} |
| 73 |
$tc = $list->count(); |
| 74 |
if (!$filter) { |
| 75 |
$filter = []; |
| 76 |
} |
| 77 |
$status = $filter['status'] ?? ''; |
| 78 |
if ($status === 'unread') { |
| 79 |
$uc = $tc; |
| 80 |
} elseif ($status === 'read') { |
| 81 |
$uc = 0; |
| 82 |
} else { |
| 83 |
$filter['status'] = 'unread'; |
| 84 |
$uc = $list->setFilter($filter)->count(); |
| 85 |
} |
| 86 |
} |
| 87 |
$res['count'] = [ 'total' => $tc, 'unread' => $uc ]; |
| 88 |
} |
| 89 |
if (array_search('filters', $lst) !== false) { |
| 90 |
$res['filters'] = (new ReportList($core->getCurrentUser()))->getFilterList(); |
| 91 |
} |
| 92 |
Core::sendJson($res); |
| 93 |
} catch (RuntimeException $e) { |
| 94 |
Core::sendJson(ErrorHandler::exceptionResult($e)); |
| 95 |
} |
| 96 |
return; |
| 97 |
} |
| 98 |
Core::instance()->sendHtml(); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
Core::sendBad(); |