Skip to content
Permalink
Browse files

- Implement Node and NetInterface classes

- Add Parsedown dependency for rendering the node documentation
- Rename templates/locations/ to templates/location/
- Modify URIs for locations and nodes
  • Loading branch information...
decke committed Sep 12, 2017
1 parent 2adb342 commit f840ab6ca350a95203ab78077c62e994526730a1
3 LEGAL
@@ -28,6 +28,9 @@ js/popper.min.js:
js/jquery.slim.min.js:
"jQuery" used under MIT License

vendor/erusev:
"Parsedown" used under MIT License

vendor/slim:
"Slim Framework" used under MIT License

@@ -4,6 +4,7 @@
"homepage": "https://github.com/decke/nodeman",
"license": "BSD-2-Clause",
"require": {
"erusev/parsedown": "1.*",
"slim/slim": "^3.0",
"slim/flash": "^0.2.0",
"slim/php-view": "~2.0",

Some generated files are not rendered by default. Learn more.

@@ -160,28 +160,28 @@
});
/* Locations */
$app->get('/locations/', function ($request, $response) use ($session) {
$app->get('/locations', function ($request, $response) use ($session) {
$loc = new Location();
return $this->view->render($response, 'locations.html', array(
'locations' => $loc->getAllLocations(null, 0, 999999)
));
});
$app->get('/locations/add', function ($request, $response) use ($session) {
$app->get('/location/add', function ($request, $response) use ($session) {
if (!$session->isAuthenticated()) {
$this->flash->addMessage('error', 'Please login first');
return $response->withStatus(302)->withHeader('Location', '/');
}
return $this->view->render($response, 'locations/add.html', array(
return $this->view->render($response, 'location/add.html', array(
'css' => array('/css/leaflet.css'),
'js' => array('/js/leaflet.js', '/js/grazmap.js')
));
});
$app->post('/locations/add', function ($request, $response) use ($session) {
$app->post('/location/add', function ($request, $response) use ($session) {
if (!$session->isAuthenticated()) {
$this->flash->addMessage('error', 'Please login first');
@@ -232,11 +232,81 @@
'address' => $request->getParam('address')
);
return $this->view->render($response, 'locations/add.html', array(
return $this->view->render($response, 'location/add.html', array(
'data' => $data,
'css' => array('/css/leaflet.css'),
'js' => array('/js/leaflet.js', '/js/grazmap.js')
));
});
/* Nodes */
$app->get('/location/{locationid}/add', function ($request, $response, $args) use ($session) {
if (!$session->isAuthenticated()) {
$this->flash->addMessage('error', 'Please login first');
return $response->withStatus(302)->withHeader('Location', '/');
}
return $this->view->render($response, 'location/node/add.html', array(
'data' => array('locationid' => $args['locationid'])
));
});
$app->post('/location/{locationid}/add', function ($request, $response, $args) use ($session) {
if (!$session->isAuthenticated()) {
$this->flash->addMessage('error', 'Please login first');
return $response->withStatus(302)->withHeader('Location', '/');
}
$location = new Location($args['locationid']);
if ($location->owner != $session->getUser()->userid) {
$this->flash->addMessage('error', 'Permission denied');
return $response->withStatus(302)->withHeader('Location', '/');
}
if (!preg_match('/^[0-9A-Za-z]{3,50}$/', $request->getParam('name'))) {
$this->flash->addMessage('error', 'Node name is invalid. Length from 3-50. Allowed characters only 0-9, A-Z, a-z');
}
if (strlen($request->getParam('documentation')) > 16384) {
$this->flash->addMessage('error', 'Documentation is too long (max 16K)');
}
$location = new Location($args['locationid']);
if ($location->nodeExists($request->getParam('name'))) {
$this->flash->addMessage('error', 'Node name already exists');
}
/* HACK: Slim-Flash hasMessage('error') does not see messages for next request */
if (!isset($_SESSION['slimFlash']['error'])) {
$node = new node();
$node->name = $request->getParam('name');
$node->owner = $session->getUser()->userid;
$node->location = $args['locationid'],
$node->hardware = 0;
$node->documentation = $request->getParam('documentation');
if ($node->save()) {
$this->flash->addMessage('success', 'Node created');
return $response->withStatus(302)->withHeader('Location', '/location/'.$node->location.'/node/'.$node->nodeid.'/');
} else {
$this->flash->addMessage('error', 'Location creation failed');
}
}
$data = array(
'name' => $request->getParam('name'),
'documentation' => $request->getParam('documentation'),
'locationid' => $args['locationid']
);
return $this->view->render($response, 'location/node/add.html', array(
'data' => $data
));
});
$app->run();
@@ -0,0 +1,114 @@
<?php
namespace FunkFeuer\Nodeman;
/**
* Network Interface with assigned IP Address.
*
* @author Bernhard Froehlich <decke@bluelife.at>
* @copyright 2017 Bernhard Froehlich
* @license BSD License (2 Clause)
*
* @link https://github.com/decke/nodeman
*/
class NetInterface
{
private $_handle;
private $_data = array(
'interfaceid' => null,
'name' => null,
'node' => null,
'category' => null,
'type' => null,
'address' => null,
'status' => null,
'ping' => null,
'comment' => null
);
public function __construct($interfaceid = null)
{
$this->_handle = Config::getDbHandle();
if ($interfaceid !== null) {
$this->load($interfaceid);
}
}
public function __get($name)
{
if (array_key_exists($name, $this->_data)) {
return $this->_data[$name];
}
throw new \Exception('Undefined property '.$name.' in class '.__CLASS__);
}
public function __set($name, $value)
{
if (array_key_exists($name, $this->_data)) {
$this->_data[$name] = $value;
return true;
}
throw new \Exception('Undefined property '.$name.' in class '.__CLASS__);
}
public function load($id)
{
$stmt = $this->_handle->prepare('SELECT interfaceid, name, node, category, type, address,
status, ping, comment FROM interfaces WHERE interfaceid = ?');
if (!$stmt->execute(array($id))) {
return false;
}
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$this->_data = $row;
return true;
}
return false;
}
public function loadByName($name)
{
$stmt = $this->_handle->prepare('SELECT interfaceid, name, node, category, type, address,
status, ping, comment FROM interfaces WHERE name = ?');
if (!$stmt->execute(array($name))) {
return false;
}
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$this->_data = $row;
return true;
}
return false;
}
public function save()
{
if (!$this->locationid) {
$stmt = $this->_handle->prepare('INSERT INTO interfaces (name, node, category, type, address,
status, ping, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
if ($stmt->execute(array($this->name, $this->node, $this->category, $this->type,
$this->address, $this->status, $this->ping, $this->comment))) {
$this->interfaceid = $this->_handle->lastInsertId();
return true;
}
} else {
$stmt = $this->_handle->prepare('UPDATE interfaces SET name = ?, node = ?, category = ?,
type = ?, address = ?, status = ?, ping = ?, comment = ? WHERE interfaceid = ?');
return $stmt->execute(array($this->name, $this->node, $this->category, $this->type,
$this->address, $this->status, $this->ping, $this->comment, $this->interfaceid));
}
return false;
}
}
@@ -142,4 +142,30 @@ public function countAllLocations($owner = null)
return $stmt->fetch(\PDO::FETCH_BOTH)[0];
}
public function getAllNodes()
{
$data = array();
$stmt = $this->_handle->prepare('SELECT nodeid FROM nodes WHERE (location = ? OR ? IS NULL) ORDER BY nodeid');
if (!$stmt->execute(array($this->location, $this->location))) {
return $data;
}
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$data[] = new Node($row['nodeid']);
}
return $data;
}
public function nodeExists($name)
{
$stmt = $this->_handle->prepare('SELECT count(*) FROM nodes WHERE location = ? AND LOWER(name) = LOWER(?)');
if (!$stmt->execute(array($this->location, $name))) {
return false;
}
return $stmt->fetch(\PDO::FETCH_BOTH)[0] > 0;
}
}

0 comments on commit f840ab6

Please sign in to comment.
You can’t perform that action at this time.