initial re-coding
authorpaulr <me@pjr.cc>
Wed, 8 Jun 2011 19:02:36 +0000 (05:02 +1000)
committerpaulr <me@pjr.cc>
Wed, 8 Jun 2011 19:02:36 +0000 (05:02 +1000)
etc/config.php
lib/cisco.plugin.php [new file with mode: 0644]
lib/db.php [new file with mode: 0644]
lib/lib.php
lib/screenos.plugin.php [new file with mode: 0644]
lib/unix.plugin.php [new file with mode: 0644]
lib/www.php [new file with mode: 0644]
lib/xmlrpc.php [new file with mode: 0644]
www/index.php

index e89f248..0620e15 100644 (file)
@@ -1,3 +1,7 @@
 <?php 
 
+$DB_URL="sqlite:$BASE_DIR/var/ds_store.db";
+
+global $DB_URL;
+
 ?>
\ No newline at end of file
diff --git a/lib/cisco.plugin.php b/lib/cisco.plugin.php
new file mode 100644 (file)
index 0000000..4b6490c
--- /dev/null
@@ -0,0 +1,8 @@
+<?php 
+
+$HOST_TYPE["cisco"]["name"] = "Cisco";
+$HOST_TYPE["cisco"]["configform"] = "somefuntion";
+$HOST_TYPE["cisco"]["postfunction"] = "somefuntion";
+
+
+?>
\ No newline at end of file
diff --git a/lib/db.php b/lib/db.php
new file mode 100644 (file)
index 0000000..f24917f
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+
+function db_getDB()
+{
+       global $DB_URL;
+       
+       $dbobject = false;
+    global $BASE_DIR, $DB_HANDLE;
+    if($DB_HANDLE != false) return $DB_HANDLE;
+       try {
+               $dbobject = new PDO("$DB_URL");
+       } catch(PDOException $exep) {
+               error_log("execpt on db open");
+               return false;
+       }
+       
+       $DB_HANDLE = $dbobject;
+
+       return $dbobject;
+}
+
+function db_createDB()
+{
+       // theres not much to do yet with only sqlite support
+}
+
+// all columns end up as text
+function db_createTable($tablename)
+{
+       $db = db_getDB();
+       
+       if(db_tableExists($tablename)) return true;
+       
+       $sql = "create table \"$tablename\" (\"".$tablename."_id\" INTEGER PRIMARY KEY AUTOINCREMENT";
+       for($i=1; $i < func_num_args(); $i++) {
+               $colname = func_get_arg($i);
+               $sql .= ", $colname text";
+       }
+       $sql .= ")";
+       
+       //echo "sql: $sql\n";
+       $db->query($sql);
+}
+
+function db_insertData($tablename)
+{
+
+       $db = db_getDB();
+       
+       $sql = "insert into \"$tablename\" values (NULL";
+       for($i=1; $i < func_num_args(); $i++) {
+               $sql .= ",'".func_get_arg($i)."'";
+       }
+       $sql .=")";
+       
+       $db->query($sql);
+}
+
+function db_selectData($tablename, $column="", $value="")
+{
+       $db = db_getDB();
+       
+       if($column != "") $extra = " where $column like '%$value%'";
+       else $extra = "";
+       $sql = "select * from \"$tablename\"$extra";
+       $res = $db->query($sql);
+       if(!$res) return false;
+       $data = $res->fetchAll();
+       
+       return $data;
+}
+
+function db_deleteData($tablename, $column, $value)
+{
+       $db = db_getDB();
+       
+       $sql = "delete from \"$tablename\" where $column like '%$value%'";
+       //echo "Sql is $sql\n";
+       return $db->query($sql);
+       
+}
+
+function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+{
+       $db = db_getDB();
+       
+       if($exact) $sql = "update \"$tablename\" set $column='$newdata' where $wherecol='$wheredata'";
+       else $sql = "update \"$tablename\" set $column='$newdata' where $wherecol like '$wheredata'";
+       return $db->query($sql);
+}
+
+function db_deleteTable($tablename)
+{
+       $db = db_getDB();
+       
+       $sql = "drop table $tablename";
+       
+       return $db->query($sql);
+}
+
+function db_tableExists($tablename)
+{
+       $db = db_getDB();
+       
+       $sql = "select count(*) from sqlite_master where type='table' and name='$tablename'";
+       $res = $db->query($sql);
+       
+       $value = $res->fetchColumn();
+       
+       if($value == 1) return true;
+       else return false;
+}
+
+function db_getTables()
+{
+       $db = db_getDB();
+       
+       $sql = "select name from sqlite_master where type='table' and name not like 'sqlite_%'";
+       $res = $db->query($sql);
+       
+       return $res->fetchAll();
+}
+?>
\ No newline at end of file
index 15c5adc..0fb5214 100644 (file)
@@ -1,3 +1,36 @@
 <?php
 
+$BASE_DIR = realpath(dirname(__FILE__)."/../");
+
+$DB_HANDLE = false;
+global $BASE_DIR, $HOST_TYPES, $DB_HANDLE;
+
+// require some files
+require_once("www.php");
+require_once("db.php");
+require_once("xmlrpc.php");
+
+// get the config
+if(file_exists("../etc/config.php")) require_once("../etc/config.php");
+
+// first and foremost, load the plugins
+$basedir = dirname(__FILE__);
+
+// we load this first
+if(is_dir("$basedir")) {
+       $dh = opendir("$basedir");
+       if($dh) {
+               while(($file = readdir($dh))!==false) {
+                       $mt = preg_match("/.*\.plugin\.php$/", $file);
+                       if($mt > 0) {
+                               require_once("$basedir/$file");
+                               //echo "required $basedir/$file\n";
+                       }
+               }
+       }
+} else {
+       echo "No plugins dir ($basedir/plugins), continuing without\n";
+}
+
+$db = db_getDB();
 ?>
\ No newline at end of file
diff --git a/lib/screenos.plugin.php b/lib/screenos.plugin.php
new file mode 100644 (file)
index 0000000..7739cfe
--- /dev/null
@@ -0,0 +1,27 @@
+<?php 
+$HOST_TYPE["screenos"]["name"] = "Netscreen Screen OS";
+$HOST_TYPE["screenos"]["configform"] = "nsos_formFunction";
+$HOST_TYPE["screenos"]["postfunction"] = "nsos_postFunction";
+
+function nsos_formFunction()
+{
+       ?>
+Name <input type="text" name="username"><br>
+Password <input type="password" name="password"><br>
+Method <select name="methodtype"><option value="ssh">SSH</option><option value="telnet">Telnet</option></select>
+       <?php
+}
+
+function nsos_postFunction()
+{
+       echo "i am post function, your awesome<br>";
+       echo "<pre>";
+       print_r($_REQUEST);
+       echo "</pre>";
+}
+
+function nsos_getConfig($istest)
+{
+       
+}
+?>
\ No newline at end of file
diff --git a/lib/unix.plugin.php b/lib/unix.plugin.php
new file mode 100644 (file)
index 0000000..26e18ba
--- /dev/null
@@ -0,0 +1,5 @@
+<?php 
+$HOST_TYPE["unix"]["name"] = "Unix (ssh)";
+$HOST_TYPE["unix"]["configform"] = "somefuntion";
+
+?>
\ No newline at end of file
diff --git a/lib/www.php b/lib/www.php
new file mode 100644 (file)
index 0000000..de4b337
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+
+// some global menu stuff
+$MENUS["home"]["url"] = "?action=home";
+$MENUS["home"]["name"] = "Home";
+$MENUS["hosts"]["url"] = "?action=hosts";
+$MENUS["hosts"]["name"] = "Hosts";
+$MENUS["add"]["url"] = "?action=addhost";
+$MENUS["add"]["name"] = "Add Host";
+$MENUS["config"]["url"] = "?action=config";
+$MENUS["config"]["name"] = "Configure";
+
+$FUNCTIONS["config"] = "www_configure";
+$FUNCTIONS["addhost"] = "www_addhost";
+$FUNCTIONS["addhostnext"] = "www_addHostStageTwo";
+$FUNCTIONS["addhostthree"] = "www_addHostStageThree";
+
+
+function www_header()
+{
+       ?>
+<html>
+<title>Network Config Manager</title>
+<body>
+       <?php
+}
+
+function www_pageLayout()
+{
+       ?>
+<table>
+<tr><td colspan="2"><h1>Network Config Manager</h1></td></tr>
+<tr valign="top"><td><?php www_menu() ?></td><td><?php www_body() ?></td></tr>
+</table>
+       
+       <?php 
+}
+
+function www_footer()
+{
+       ?>
+<br><font size="-1"><i>Copyright 2003-2011, Paul J Robinson</i></font><br>
+</body>
+</html>
+       <?php 
+}
+
+function www_menu()
+{
+       global $MENUS;
+       foreach($MENUS as $menuitems) {
+               $url = $menuitems["url"];
+               $name = $menuitems["name"];
+               echo "<a href=\"$url\">$name</a><br>";
+       }
+}
+
+function www_body()
+{
+       global $FUNCTIONS;
+       
+       $called = false;
+       if(isset($_REQUEST["action"])) {
+               $req = $_REQUEST["action"];
+               if(isset($FUNCTIONS["$req"])) {
+                       $func = $FUNCTIONS["$req"];
+                       if(function_exists($func)) {
+                               $func();
+                               return true;
+                       }
+               }
+       }
+       
+       $db = db_getDB();
+       
+       // here we do the normal body, whatever that
+       // we'll list the hosts, the size of the current config and
+       // the last time it was updated, plus the number of versions
+       
+}
+
+function www_hostTypesDropDown()
+{
+       global $HOST_TYPE;
+       
+       foreach($HOST_TYPE as $tloop => $types) {
+               $typename = $types["name"];
+               $typever = $tloop;
+               echo "<option value=\"$typever\">$typename</option>";
+       }
+}
+
+function www_addhost()
+{
+       ?>
+<form method="post" action="?action=addhostnext">
+<table>
+<tr><td>Name</td><td><input type="text" name="cname"></td></tr>
+<tr><td>Hostname/IP Address</td><td><input type="text" name="hname"></td></tr>
+<tr><td>Host Type</td><td><select name="hosttype"><?php www_hostTypesDropDown() ?></select></td></tr>
+</table>
+<input type="submit" value="Next" name="Next">
+</form>
+       <?php
+}
+
+function www_addHostStageTwo()
+{
+       global $HOST_TYPE;
+       if(isset($_REQUEST["hosttype"])) {
+               $htype = $_REQUEST["hosttype"];
+               $func = $HOST_TYPE["$htype"]["configform"];
+               if(function_exists($func)) {
+                       echo "<form method=\"post\" action=\"?action=addhostthree&hosttype=$htype\">";
+                       $func();
+                       echo "<input type=\"submit\" value=\"Add\" name=\"Add\">";
+               } else echo "would call $func for $htype but it doesnt exist\n";
+       }
+}
+
+function www_addHostStageThree()
+{
+       global $HOST_TYPE;
+       if(isset($_REQUEST["hosttype"])) {
+               $htype = $_REQUEST["htype"];
+               $func = $HOST_TYPE["$htype"]["postfunction"];
+               if(function_exists($func)) {
+                       $func();
+               } else echo "would call $func for $htype but it doesnt exist\n";
+       }
+       
+}
+?>
\ No newline at end of file
diff --git a/lib/xmlrpc.php b/lib/xmlrpc.php
new file mode 100644 (file)
index 0000000..5cbc582
--- /dev/null
@@ -0,0 +1,8 @@
+<?php 
+
+function rpc_handler()
+{
+       echo "rpc handler goes here\n"; 
+}
+
+?>
\ No newline at end of file
index e3417c9..a86ff2b 100644 (file)
@@ -1,3 +1,11 @@
 <?php 
 require_once("../lib/lib.php");
+
+if(isset($_REQUEST["xmlrpc"])) {
+       rpc_handler();
+       return;
+}
+www_header();
+www_pageLayout();
+www_footer();
 ?>
\ No newline at end of file