Skip to content

Instantly share code, notes, and snippets.

@thefinn93
Created July 22, 2012 20:21
Show Gist options
  • Save thefinn93/3160951 to your computer and use it in GitHub Desktop.
Save thefinn93/3160951 to your computer and use it in GitHub Desktop.
CJDNS Admin GUI

CJDNS Admin GUI

Allows access to the CJDNS Admin API via a GUI. Should be kept in a private, password protected location.

Dependencies

Install

  1. Place index.php in a (secure) location on your webserver. Do not allow the public to access it, as it will allow them to do nasty things (and do nasty things they will).
  2. Put gui.js in a subfolder called js (or update the bottom of index.php to reflect it's location).
  3. Correct all the script paths at the top and bottom of index.php (PHP at the top, Javascript at the bottom)
  4. Visit the page to confirm it works. Just loading up the tabs is a good sign, try using the ping or memory functions to confirm everything works.
xhr = {}
function cjdnsadmin(func, form, event) {
event.preventDefault();
output = document.getElementById(func + "-output");
output.style.display = "block";
output.innerHTML = "<img src=\"load.gif\" /> Loading...\n";
inputs = form.getElementsByTagName("input");
args = {};
for(i = 0; i < inputs.length; i++) {
if(inputs[i].value != "") {
if(functions[func][inputs[i].id]['type'] == "String") {
args[inputs[i].id] = inputs[i].value;
} else if(functions[func][inputs[i].id]['type'] == "Int") {
args[inputs[i].id] = parseInt(inputs[i].value);
}
}
}
params = "action=" + func;
if(inputs.length > 0) {
params += "&args=" + encodeURIComponent(JSON.stringify(args));
}
xhr[func] = new XMLHttpRequest();
xhr[func].onload = showresponse;
xhr[func].open("POST","gui.php", true);
xhr[func].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr[func].send(params)
}
function showresponse() {
for(func in xhr) {
if(func != undefined) {
if(xhr[func].readyState == 4) {
output = document.getElementById(func + "-output");
output.innerHTML = "<pre>" + xhr[func].responseText + "</pre>";
}
}
}
}
<?
require_once("secrets.inc.php");
require_once("cjdns.inc.php");
$cjdns = new cjdns;
$cjdns->connect();
if(isset($_REQUEST['action'])) {
if(isset($_REQUEST['args'])) {
# echo json_encode($cjdns->call($_REQUEST['action'], json_decode($_REQUEST['args'], TRUE)));
echo print_r($cjdns->call($_REQUEST['action'], json_decode($_REQUEST['args'], TRUE)));
} else {
echo json_encode($cjdns->call($_REQUEST['action']));
}
} else {
$tabsnav = "";
$tabscontent = "";
$active = TRUE;
foreach($cjdns->functionlist() as $function => $args) {
if($function != "isDct") {
if($active) {
$tabsnav .= " <li class=\"active\"><a href=\"#$function\" data-toggle=\"tab\">$function</a></li>\n";
$tabscontent .= "<div class=\"tab-pane active\" id=\"$function\">\n";
$active = FALSE;
} else {
$tabsnav .= " <li><a href=\"#$function\" data-toggle=\"tab\">$function</a></li>\n";
$tabscontent .= "<div class=\"tab-pane\" id=\"$function\">\n";
}
$tabscontent .= " <form class=\"form-horizontal\" action=\"index.php\" method=\"POST\" id=\"$function-form\" onsubmit=\"cjdnsadmin('$function',this,event)\">\n";
foreach($args as $arg=>$info) {
if($arg != "isDct") {
$required = "";
if($info['required'] == 1) {
$required = "<font color=\"#FF0000\">*</font>";
}
$tabscontent .= " <div class=\"control-group\">\n";
$tabscontent .= " <label class=\"control-label\" for=\"$arg\">".$arg.$required."</label>\n";
$tabscontent .= " <div class=\"controls\">\n";
$tabscontent .= " <input type=\"text\" class=\"input\" placeholder=\"".$info['type']."\" name=\"$arg\" id=\"$arg\"/>\n";
$tabscontent .= " </div>\n";
$tabscontent .= " </div>\n";
}
}
$tabscontent .= " <div class=\"form-actions\">\n";
$tabscontent .= " <button class=\"btn btn-success\">Execute</button>\n";
$tabscontent .= " <button class=\"btn\">Reset</button>\n";
$tabscontent .= " </div>\n";
$tabscontent .= " </form>\n";
$tabscontent .= " <div id=\"$function-output\" class=\"well\" style=\"display: none\"></div>\n";
$tabscontent .= "</div>\n";
}
} ?><html>
<head>
<title>CJDNS GUI</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
</head>
<body>
<br /><br />
<div class="content">
<div class="tabbable tabs-left span10">
<?
echo "<ul class=\"nav nav-tabs\" id=\"functions\">\n".$tabsnav."</ul>\n";
echo "<div class=\"tab-content\">\n".$tabscontent."</div>\n";
?>
</div>
</div>
<script src="bootstrap/js/jquery.js" type="text/javascript"></script>
<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="bootstrap/js/bootstrap-tabs.js" type="text/javascript"></script>
<script type="text/javascript">var functions = <? echo json_encode($cjdns->functionlist()); ?></script>
<script src="js/gui.js" type="text/javascript"></script>
</body>
</html><?
}
$cjdns->disconnect();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment