<?php

  session_register("my_result");
  session_register("user_err");

function show_result(){
  $lookupname = $_REQUEST['usrname'];
  $ipport_recvd=$_REQUEST['ip'];
  $ipport = split('[:]',$_REQUEST['ip']);
  $ip = $ipport[0];
  $port = $ipport[1]; 
  $objlookup=$_REQUEST['lookup'];
  $objname=$_REQUEST['objname'];
  $key = $_REQUEST['key'];
  $value = $_REQUEST['value'];
  $insert=$_REQUEST['insert'];
  $getrt=$_REQUEST['getrt'];
  $getro=$_REQUEST['getro'];
  if($_REQUEST['recursive']=='on'){
    $rec = "-r";
  }else {
    $rec = "";
  }

  if(empty($ip)){
    $_SESSION["user_err"]= "Error: host not selected.\n";
    header("Location: checker.php?key=$key&value=$value&usr=$lookupname&obj=$objname&ip=$ipport_recvd");
    return;
  }

  //chdir("/home/gg2265/p2p/scripts/");
  $USER="salman";
  $PATH="/home/$USER/html/peer/exec/0_2/";
  $CPATH="/home/$USER/html/peer/exec/0_2/checker";
  chdir($PATH);
  
  if($objlookup=='Lookup Object') {
    if(empty($objname)) {
      $_SESSION["user_err"]= "Error: Object name not entered.\n";
      header("Location: checker.php?key=$key&value=$value&usrname=$lookupname&obj=$objname&ip=$ipport_recvd");
      return;
    }
    exec("$CPATH $rec -o $objname -a $ip -p $port", $_SESSION["my_result"]);
  }else if($insert=='Publish') {
    if($key==''||$value==''){
      $_SESSION["user_err"]= "Error: Key and/or Value not entered.\n";
      header("Location: checker.php?key=$key&value=$value&usrname=$lookupname&obj=$objname&ip=$ipport_recvd");
      return;
    }
    exec("$CPATH $rec -i $key -v $value -a $ip -p $port",$_SESSION["my_result"]);
  }else if($getrt=='Get routing table') {
    exec("$CPATH  $rec -t -R -a $ip -p $port",$_SESSION["my_result"]);
  }else if($getro=='Get resource table') {
    exec("$CPATH  $rec -t -O -a $ip -p $port",$_SESSION["my_result"]);
  }else {
    if($lookupname==''){
      $_SESSION["user_err"]= "Error:  username not selected/entered.\n";
      header("Location: checker.php?key=$key&value=$value&usrname=$lookupname&obj=$objname&ip=$ipport_recvd");
      return;
    }
    exec("$CPATH $rec -u $lookupname -a $ip -p $port",$_SESSION["my_result"]);
  }
  header("Location: checker.php?key=$key&value=$value&usr=$lookupname&obj=$objname&ip=$ipport_recvd");
}  
show_result();
//echo $_SESSION["my_result"];
//foreach ($_SESSION["my_result"] as $str) {
//  echo $str."<br>";
//}

?>