Commit 404a5114 authored by StilgarBF's avatar StilgarBF Committed by Clemens John

add community-file api

Allows the admin provide a preconfigured community-json and
updates nodecount and lastchange-field if needed.
parent 9f719570
<?php
/**
* delivers a json containing the community-file
*
* this reads the provided community-json, sets the current "nodecount"
* and updates the "lastchange" field if needed.
*/
try {
require_once("../runtime.php");
require_once(ROOT_DIR.'/lib/core/db.class.php');
require_once(ROOT_DIR.'/lib/core/ConfigLine.class.php');
$configSavedCommunityJson = ConfigLine::configByName('community_json');
if(empty($configSavedCommunityJson) || empty(json_decode($configSavedCommunityJson)))
{
// no json in config - exit
return;
}
try
{
$query = "SELECT COUNT(*) AS count FROM routers";
$stmt = DB::getInstance()->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo $e->getMessage();
echo $e->getTraceAsString();
}
$community = json_decode($configSavedCommunityJson);
$nodeCount = (int)$result[0]['count'];
$community->state->nodes = $nodeCount;
$lastNodeCount = ConfigLine::configByName('last_node_count');
$now = date('c');
if($nodeCount != $lastNodeCount)
{
// nodecount has changed
// update "lastchange-field"
$community->state->lastchange = $now;
Config::writeConfigLine('last_node_count', $nodeCount);
Config::writeConfigLine('last_node_count_time', $now);
}
else
{
$lastTime = ConfigLine::configByName('last_node_count_time');
if(!$lastTime)
{
// there has never been a setting stored - store and use current datettime
Config::writeConfigLine('last_node_count_time', $now);
$lastTime = $now;
}
// write to json
$community->state->lastchange = $lastTime;
}
try
{
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60*15)));
header('Content-type: application/json');
echo json_encode($community, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
catch (Exception $e)
{
echo json_encode('Unable to create communityfile: ', $e->getMessage(), "\n");
}
}
catch (Exception $e)
{
echo json_encode('Caught exception: ', $e->getMessage(), "\n");
}
......@@ -102,6 +102,7 @@ if(Permission::checkPermission(PERM_ROOT)) {
$smarty->assign('community_essid', ConfigLine::configByName('community_essid'));
$smarty->assign('community_homepage', ConfigLine::configByName('community_homepage'));
$smarty->assign('community_homepage_name', ConfigLine::configByName('community_homepage_name'));
$smarty->assign('community_json', ConfigLine::configByName('community_json'));
$smarty->assign('message', Message::getMessage());
$smarty->display("header.tpl.html");
......@@ -121,6 +122,19 @@ if(Permission::checkPermission(PERM_ROOT)) {
Config::writeConfigLine('community_homepage_name', $_POST['community_homepage_name']);
}
if(!empty($_POST['community_json']))
{
if(!empty(json_decode($_POST['community_json'])))
{
// community-json posted and valid json
Config::writeConfigLine('community_json', $_POST['community_json']);
}
else
{
$message[] = array('Community-JSON konnte nicht gespeichert werden. Kein valides JSON.', 2);
}
}
if(empty($_POST['enable_network_policy']))
$_POST['enable_network_policy'] = 'false';
Config::writeConfigLine('enable_network_policy', $_POST['enable_network_policy']);
......
......@@ -43,7 +43,20 @@
<p>Ist die Netzwerkpolicy eingeschaltet, dann wird neuen Benutzern bei der Registration ein Link auf eine Netzwerkpolicy gezeigt. Damit ein Benutzer sich erfolgreich registrieren kann, muss er diese bestätigen.</p>
<p><b>Netzwerkpolicy einschalten:</b> <input name="enable_network_policy" type="checkbox" value="true" {if $enable_network_policy=='true'}checked{/if}>
<p><b>Link zur Netzwerkpolicy:</b><br><input name="network_policy_url" type="text" size="70" value="{$network_policy_url}"></p>
<br>
<h2>Community-File</h2>
<p>
Das Community-JSON kann in der Freifunk-API verwendet werden. Netmon wird die aktuelle Node-Zahl automatisch aktualisieren.<br />
Füge hier das fertig vorbereitete JSON ein. Die Knoten für state - nodes und state - lastchange müssen bereits vorhanden sein.
</p>
<p>
<b>Community-JSON:</b><br>
<textarea name="community_json" style="font-size:0.8em; width: 530px; height: 100px">{if !empty($community_json)}{$community_json}{/if}</textarea>
</p>
<br />
<p><input type="submit" value="Absenden"></p>
</form>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment