From e2ac2802217a2c5deec5af69be1e4534ed6dd4b6 Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Mon, 11 Nov 2019 01:58:17 +0300 Subject: [PATCH] 191111 0158 --- public/assets/css/style.css | 92 ++++++++++++++++ src/App.php | 3 +- src/controllers/DefaultController.php | 3 +- src/controllers/ErrorsController.php | 5 +- src/controllers/GroupsController.php | 43 ++++++++ src/controllers/ServersController.php | 30 ++++++ src/core/Config.php | 3 + src/core/Controller.php | 43 ++++++++ src/core/View.php | 22 +++- src/models/GroupsModel.php | 8 ++ src/models/ServersModel.php | 106 ++++++++++++++++++- src/models/TallyLogsModel.php | 146 ++++++++++++++++++++++++++ src/services/GroupsService.php | 34 ++++++ src/services/PingService.php | 38 +++++++ src/templates/404.tpl.php | 5 + src/templates/default/index.tpl.php | 4 +- src/templates/groups/add.tpl.php | 7 ++ src/templates/groups/edit.tpl.php | 51 +++++++++ src/templates/groups/index.tpl.php | 24 +++++ src/templates/servers/add.tpl.php | 7 ++ src/templates/servers/index.tpl.php | 7 ++ src/templates/shared/footer.php | 7 ++ src/templates/shared/head.php | 18 +++- src/views/DefaultView.php | 4 +- src/views/GroupsView.php | 26 +++++ src/views/ServersView.php | 12 +++ 26 files changed, 732 insertions(+), 16 deletions(-) create mode 100644 src/controllers/GroupsController.php create mode 100644 src/controllers/ServersController.php create mode 100644 src/models/TallyLogsModel.php create mode 100644 src/services/GroupsService.php create mode 100644 src/services/PingService.php create mode 100644 src/templates/404.tpl.php create mode 100644 src/templates/groups/add.tpl.php create mode 100644 src/templates/groups/edit.tpl.php create mode 100644 src/templates/groups/index.tpl.php create mode 100644 src/templates/servers/add.tpl.php create mode 100644 src/templates/servers/index.tpl.php create mode 100644 src/templates/shared/footer.php create mode 100644 src/views/GroupsView.php create mode 100644 src/views/ServersView.php diff --git a/public/assets/css/style.css b/public/assets/css/style.css index f341183..ee295e7 100644 --- a/public/assets/css/style.css +++ b/public/assets/css/style.css @@ -1,6 +1,98 @@ +*, :after, :before { + -webkit-box-sizing: border-box; + box-sizing: border-box +} + +a { + color: #0063ff; + text-decoration: none; + border-bottom: .0625rem solid rgba(0, 99, 255, .3); +} + +a:hover, +a:visited:hover { + color: #ff475d; + border-bottom: .0625rem solid rgba(255, 71, 93, .24); + +} + +a:visited { + color: #cf00cf; + border-bottom: .0625rem solid rgba(207, 0, 207, .3); +} + body { font-family: Verdana, "Geneva CY", "DejaVu Sans", sans-serif; font-size: 1rem; font-weight: normal; + line-height: 1.25rem; } +.header { + display: grid; + grid-template-rows: 1fr 1fr; + grid-template-columns: 1fr 6fr; + grid-gap: 1vw; +} + +.header, .main, .footer, .navigate ul { + margin: 0 auto; + max-width: 1000px; +} + +.header__img { + grid-row: 1/3; + grid-column: 1; + margin-left: -1.75rem; + margin-right: -1.75rem; +} + +.header__title { + margin-bottom: 0; + line-height: 1; + margin-top: 3rem; +} + +.navigate { + background: black; + margin-top: 1.25rem; +} + +.navigate ul { + list-style: none unset none; + padding: .375rem 0; +} + +.navigate ul li { + display: inline-block; + width: 32%; + text-align: center; +} + +.navigate ul li a { + color: white; + text-decoration: none; +} + +.navigate ul li a:hover { + color: white; + border-bottom: .0625rem solid rgba(255, 255, 255, .6); +} + +.footer { + margin-top: 2rem; + padding-top: 1rem; + padding-bottom: 1rem; +} +.footer::before { + width: 100%; + display: block; + content: " "; + border-top: .25rem solid black; + position: absolute; + left: 0; +} + +table { + border-collapse: collapse; +} \ No newline at end of file diff --git a/src/App.php b/src/App.php index 8e58e30..9d50c2f 100644 --- a/src/App.php +++ b/src/App.php @@ -11,6 +11,7 @@ class App 'Controller\\' => 'controllers/', 'Core\\' => 'core/', 'Model\\' => 'models/', + 'Service\\' => 'services/', 'View\\' => 'views/', ]; private $basedir = __DIR__; @@ -26,7 +27,7 @@ class App /** * App constructor. * - * @param bool $start Start router. Default false. + * @param bool $start Start router. Default true. */ public function __construct(bool $start = true) { diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index e955b8c..a40d6c6 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -12,7 +12,6 @@ class DefaultController extends Controller */ public function index() { - (new View())->index(); - $Model = new \MyApp\Model\GroupsModel(); + $this->htmlResponse((new View())->index()); } } diff --git a/src/controllers/ErrorsController.php b/src/controllers/ErrorsController.php index 81310fc..0a2b436 100644 --- a/src/controllers/ErrorsController.php +++ b/src/controllers/ErrorsController.php @@ -3,13 +3,14 @@ namespace MyApp\Controller; use MyApp\Core\Controller; +use MyApp\Core\View; class ErrorsController extends Controller { public function get404() { - http_response_code(404); - echo 404; + $this->htmlResponse((new View())->setTitle('404 Page not found')->render('404'), + 404); } public function post404() diff --git a/src/controllers/GroupsController.php b/src/controllers/GroupsController.php new file mode 100644 index 0000000..8a6e19f --- /dev/null +++ b/src/controllers/GroupsController.php @@ -0,0 +1,43 @@ +htmlResponse((new View())->index((new GroupsService())->getTableData())); + } + + /** + * @RouteRegExp = "|^/groups/add$|" + */ + public function add() + { + + } + + /** + * @RouteRegExp = "|^/groups/edit/(?\d+)$|" + */ + public function edit($id) + { + $group_service = new GroupsService(); + + $model = $group_service->getGroup($id); + + if ($model) { + $this->htmlResponse((new View())->edit($group_service->getGroup($id), + $group_service->getTableData())); + } else { + $this->redirect('/groups'); + } + } +} diff --git a/src/controllers/ServersController.php b/src/controllers/ServersController.php new file mode 100644 index 0000000..9f9556b --- /dev/null +++ b/src/controllers/ServersController.php @@ -0,0 +1,30 @@ +\d+)$|" + */ + public function edit($id) + { + } +} diff --git a/src/core/Config.php b/src/core/Config.php index 204c48b..3448ed6 100644 --- a/src/core/Config.php +++ b/src/core/Config.php @@ -2,6 +2,9 @@ namespace MyApp\Core; +/** + * Class Config. + */ class Config { /** diff --git a/src/core/Controller.php b/src/core/Controller.php index eabeab2..bedcc49 100644 --- a/src/core/Controller.php +++ b/src/core/Controller.php @@ -2,6 +2,49 @@ namespace MyApp\Core; +/** + * Class Controller. + */ class Controller { + public function __construct() + { + } + + /** + * Send JSON response. + * + * @param mixed $data + * @param int $http_response_code HTTP code. Default 200 + */ + public function jsonResponse($data = null, $http_response_code = 200) + { + http_response_code($http_response_code); + header('Content-Type: application/json; charset=utf-8'); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + exit(0); + } + + /** + * Send html response. + * + * @param mixed $data + * @param int $http_response_code HTTP code. Default 200 + */ + public function htmlResponse($data = null, $http_response_code = 200) + { + http_response_code($http_response_code); + header('Content-Type: text/html; charset=utf-8'); + echo $data; + exit(0); + } + + /** + * @param string $path + * @param int $http_response_code + */ + public function redirect( $path = '/', $http_response_code = 302 ) { + header( "Location: $path", true, $http_response_code ); + exit; + } } diff --git a/src/core/View.php b/src/core/View.php index 5e8a24e..d3564da 100644 --- a/src/core/View.php +++ b/src/core/View.php @@ -8,23 +8,36 @@ class View * @var string */ public $title = ''; + /** + * @var string + */ + public $siteName = ''; - public function json($data) + public function __construct() { } - public function render($template) + /** + * @param string $template + * + * @return string + */ + public function render(string $template): string { /* @var \App $app */ global $app; $fileName = $app->config->getSrcDir().'/templates/'.$template.'.tpl.php'; + $out = ''; if (file_exists($fileName)) { ob_start(); require $fileName; - ob_end_flush(); + $out = ob_get_contents(); + ob_end_clean(); } + + return $out; } /** @@ -37,7 +50,8 @@ class View /* @var \App $app */ global $app; - $this->title = $title.' — '.$app->config->getSiteName(); + $this->siteName = $app->config->getSiteName(); + $this->title = $title; return $this; } diff --git a/src/models/GroupsModel.php b/src/models/GroupsModel.php index 1f29a97..c055482 100644 --- a/src/models/GroupsModel.php +++ b/src/models/GroupsModel.php @@ -74,4 +74,12 @@ class GroupsModel extends Model $this->parent = $parent; return $this; } + + /** + * @return int + */ + public function getParent(): ?int + { + return $this->parent; + } } \ No newline at end of file diff --git a/src/models/ServersModel.php b/src/models/ServersModel.php index a88b918..5062f58 100644 --- a/src/models/ServersModel.php +++ b/src/models/ServersModel.php @@ -1,13 +1,113 @@ id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + * + * @return ServersModel + */ + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + /** + * @return string + */ + public function getIp(): string + { + return long2ip($this->ip); + } + + /** + * @param string $ip + * + * @return ServersModel + */ + public function setIp(string $ip): self + { + $this->ip = ip2long($ip); + + return $this; + } + + /** + * @return int + */ + public function getGroup(): int + { + return $this->group; + } + + /** + * @param int $group + * + * @return ServersModel + */ + public function setGroup(int $group): self + { + $this->group = $group; + + return $this; + } +} diff --git a/src/models/TallyLogsModel.php b/src/models/TallyLogsModel.php new file mode 100644 index 0000000..dc74242 --- /dev/null +++ b/src/models/TallyLogsModel.php @@ -0,0 +1,146 @@ +id; + } + + /** + * @return int + */ + public function getTime(): int + { + return $this->time; + } + + /** + * @param DateTime $time + * + * @return TallyLogsModel + */ + public function setTime(DateTime $time): self + { + $this->time = $time->getTimestamp(); + + return $this; + } + + /** + * @return int + */ + public function getTransmitted(): int + { + return $this->transmitted; + } + + /** + * @param int $transmitted + * + * @return TallyLogsModel + */ + public function setTransmitted(int $transmitted): self + { + $this->transmitted = $transmitted; + + return $this; + } + + /** + * @return int + */ + public function getReceived(): int + { + return $this->received; + } + + /** + * @param int $received + * + * @return TallyLogsModel + */ + public function setReceived(int $received): self + { + $this->received = $received; + + return $this; + } + + /** + * @return int + */ + public function getLost(): int + { + return $this->lost; + } + + /** + * @param int $lost + * + * @return TallyLogsModel + */ + public function setLost(int $lost): self + { + $this->lost = $lost; + + return $this; + } +} diff --git a/src/services/GroupsService.php b/src/services/GroupsService.php new file mode 100644 index 0000000..fe12c0a --- /dev/null +++ b/src/services/GroupsService.php @@ -0,0 +1,34 @@ +getIp(); + exec("ping -c 10 $ip", $output, $status); + foreach ($output as $line) { + if (preg_match('/(?\d+)[\w\ ]*,\ (?\d+)[\w\ ]*, (?\d+)%/', + $line, $m)) { + $log = new TallyLogsModel(); + + $log->setTime(new DateTime()) + ->setLost($m['lost']) + ->setTransmitted($m['transmitted']) + ->setReceived($m['received']); + + Model::save([$log]); + + return $log; + } + } + } + + return null; + } +} diff --git a/src/templates/404.tpl.php b/src/templates/404.tpl.php new file mode 100644 index 0000000..6ff84cc --- /dev/null +++ b/src/templates/404.tpl.php @@ -0,0 +1,5 @@ + + -require __DIR__.'/../shared/head.php'; + +
+ +
+model; + +function getTree($table, $parent_id = null, $level = 1) +{ + $out = []; + foreach ($table as $item) { + /** @var \MyApp\Model\GroupsModel $item */ + if ( + $item->getParent() === $parent_id) { + $item->{"select-level"} = str_repeat("-", $level); + $out[] = $item; + $out = array_merge($out, + getTree($table, $item->getId(), $level + 1)); + } + } + + return $out; +} + +?> +
+ +
+ +
+ + +
+
+ + +
+ +
+ + table): ?> +
+ + + + + + + + table as $item):?> + + + + + + + + +
IdNameParentActions
getId() ?>getName() ?>getParent() ?>edit
+ + +
+ +
+ + + +
+ + + + diff --git a/src/templates/shared/head.php b/src/templates/shared/head.php index 3825df7..a0ec8d5 100644 --- a/src/templates/shared/head.php +++ b/src/templates/shared/head.php @@ -14,5 +14,21 @@ - <?= $this->title; ?> + <?= $this->title; ?> — <?= $this->siteName; ?> + +
+ +

siteName; ?>

+

title; ?>

+
+ +
+ + diff --git a/src/views/DefaultView.php b/src/views/DefaultView.php index 6929ac8..731c52c 100644 --- a/src/views/DefaultView.php +++ b/src/views/DefaultView.php @@ -6,8 +6,8 @@ use MyApp\Core\View; class DefaultView extends View { - public function index() + public function index(): string { - $this->setTitle('Index')->render('default/index'); + return $this->setTitle('Index')->render('default/index'); } } diff --git a/src/views/GroupsView.php b/src/views/GroupsView.php new file mode 100644 index 0000000..e20d7a0 --- /dev/null +++ b/src/views/GroupsView.php @@ -0,0 +1,26 @@ +{"table"} = $table; + + return $this->setTitle('Edit groups')->render('groups/index'); + } + + public function edit($model, ?array $table) + { + $this->{"model"} = $model; + $this->{"table"} = $table; + + return $this->setTitle('Edit groups')->render('groups/edit'); + } +} \ No newline at end of file diff --git a/src/views/ServersView.php b/src/views/ServersView.php new file mode 100644 index 0000000..ae7d554 --- /dev/null +++ b/src/views/ServersView.php @@ -0,0 +1,12 @@ +