phpmytenconf.php: copy-config

replacement of running config
diff --git a/scripts/phpmynetconf.php b/scripts/phpmynetconf.php
index 47d7c8a..28ab008 100644
--- a/scripts/phpmynetconf.php
+++ b/scripts/phpmynetconf.php
@@ -238,6 +238,66 @@
  \param[in,out] $sock socket descriptor
  \return 0 on success
 */
+function handle_editconfig(&$sock)
+{
+	if (check_logged_keys() != 0) {
+		return 1;
+	}
+	$sessionkey = $_SESSION["keys"][$_REQUEST["key"]];
+	/* execute get-config */
+	$decoded = execute_operation($sock,
+		array("type" => MsgType::MSG_GETCONFIG,
+		"session" => $sessionkey,
+		"source" => "running"));
+
+	/* apply changes */
+	$oldtree = $decoded["data"];
+	var_dump($oldtree);
+	echo "<br><br><br>";
+	$newtree = simplexml_load_string("<rootnode>".str_replace('<?xml version="1.0" encoding="UTF-8"?>', "", $oldtree)."</rootnode>");
+	echo "<br><br><br>";
+	var_dump($newtree->{'comet-testers'}->{'comet-tester'}->statistics->enabled);
+	//return 0;
+	$newtree->{'comet-testers'}->{'comet-tester'}->statistics->enabled = "false";
+	var_dump($newtree->{'comet-testers'}->{'comet-tester'}->statistics->enabled);
+	$config = "";
+	foreach ($newtree as $ch) {
+		$config .= $ch->asXML();
+	}
+	/* copy-config to store new values */
+	$params = array("type" => MsgType::MSG_EDITCONFIG,
+			"session" => $sessionkey,
+			"target" => "running",
+			"config" => $config);
+	print_r($params);
+	$decoded = execute_operation($sock, $params);
+
+	echo "<h2>EDIT-CONFIG</h2>";
+	var_dump($decoded);
+	return 0;
+}
+
+function handle_copyconfig(&$sock)
+{
+	$sessionkey = $_SESSION["keys"][$_REQUEST["key"]];
+	if (isset($_REQUEST["config"]) && $_REQUEST["config"] != '') {
+		$config = $_REQUEST["config"];
+		var_dump($config);
+		$params = array("type" => MsgType::MSG_COPYCONFIG,
+			"session" => $sessionkey,
+			"target" => "running",
+			"config" => $config);
+		$decoded = execute_operation($sock, $params);
+		var_dump($decoded);
+	} else {
+		echo "No config was sent";
+	}
+}
+
+/**
+ \param[in,out] $sock socket descriptor
+ \return 0 on success
+*/
 function handle_disconnect(&$sock)
 {
 	if (check_logged_keys() != 0) {
@@ -263,6 +323,36 @@
 /* main part of script */
 session_start();
 
+/* create connection with socket */
+if (isset($_REQUEST["getconfig"]) || (isset($_REQUEST["command"]))) {
+	$errno = 0;
+	$errstr = "";
+	$sock = fsockopen('unix:///tmp/mod_netconf.sock', NULL, $errno, $errstr);
+	if ($errno != 0) {
+		echo "Could not connect to socket.";
+		echo "$errstr";
+		return 1;
+	}
+	stream_set_timeout($sock, 1, 100);
+}
+
+/* pseudo-AJAX response of get-config running */
+if (isset($_REQUEST["getconfig"])) {
+	if (check_logged_keys() != 0) {
+		return 1;
+	}
+	$sessionkey = $_SESSION["keys"][$_REQUEST["key"]];
+	$params = array("type" => MsgType::MSG_GETCONFIG,
+			"session" => $sessionkey,
+			"source" => "running");
+	$decoded = execute_operation($sock, $params);
+	echo $decoded["data"];
+	/* end script return only data */
+	exit(0);
+}
+
+
+/* print mainpage */
 if (!isset($_REQUEST["command"])) {
 	echo "<h2>Connect to new NETCONF server</h2>
 	<form action='?' method='POST'>
@@ -291,27 +381,42 @@
 <option value='startup'>Start-up</option>
 <option value='candidate'>Candidate</option></select>
 <input type='submit' value='Execute Get-config'></form>
-<a href='?command=disconnect&amp;key=$i'><button>disconnect</button></a><br>";
+<!--<form action='?' method='GET'>
+<input type='hidden' name='command' value='editconfig'>
+<input type='hidden' name='key' value='$i'>
+<label for='edit-element'>Element name:</label><input type='text' name='element'>
+<label for='edit-value'>Value:</label><input type='text' name='newval'>
+<input type='submit' value='Execute Edit-config'>
+</form>-->
+<form action='?' method='POST'>
+<input type='hidden' name='command' value='copyconfig'>
+<input type='hidden' name='key' value='$i'>
+<textarea name='config' id='configdata$i' cols=40 rows=10></textarea>
+<input type='submit' value='Rewrite running config'> (copy-config)
+</form>
+<a href='?command=disconnect&amp;key=$i'><button>disconnect</button></a><br>
+<script type='text/javascript'>
+xmlHttp = new XMLHttpRequest();
+xmlHttp.open( 'GET', '?key=$i&getconfig', false );
+xmlHttp.send( null );
+document.getElementById('configdata$i').value=xmlHttp.responseText;
+</script>";
 			$i++;
 		}
 	}
 	exit(0);
 }
 
+/* handle commands */
 if (isset($_REQUEST["command"])) {
-	$errno = 0;
-	$errstr = "";
-	$sock = fsockopen('unix:///tmp/mod_netconf.sock', NULL, $errno, $errstr);
-	if ($errno != 0) {
-		echo "Could not connect to socket.";
-		echo "$errstr";
-		return 1;
-	}
-	stream_set_timeout($sock, 1, 100);
 	echo "<a href='?'>Back</a>";
 
 	if ($_REQUEST["command"] === "connect") {
 		handle_connect($sock);
+	} else if ($_REQUEST["command"] === "copyconfig") {
+		handle_copyconfig($sock);
+	} else if ($_REQUEST["command"] === "editconfig") {
+		handle_editconfig($sock);
 	} else if ($_REQUEST["command"] === "get") {
 		handle_get($sock);
 	} else if ($_REQUEST["command"] === "getconfig") {