Source for file GuiEditIp.php

Documentation is available at GuiEditIp.php

  1. <?php
  2. /**
  3.  * 
  4.  * GuiEditIp.php
  5.  *
  6.  * Long description for file: ip table
  7.  * Allow records to edited, deleted or inserted.
  8.  * Specific to the FreeNAC DB schema.
  9.  *
  10.  * @package     FreeNAC
  11.  * @author      Many: S.Boran, T.Dagonnier, P.Bizeau
  12.  * @copyright   2008 FreeNAC
  13.  * @license     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 3
  14.  * @version     SVN: $Id$
  15.  * @link        http://freenac.net
  16.  *
  17.  */
  18.  
  19.  
  20. class GuiEditIp extends WebCommon
  21. {
  22.   private $id$action;      // See also WebCommon and Common
  23.  
  24.  
  25.   function __construct($action$id=0$debug_level=1)
  26.   {
  27.     parent::__construct(false);     // See also WebCommon and Common
  28.     $this->logger->setDebugLevel($debug_level);
  29.     $this->debug("__construct id=$id, debug=$debug_level, action=$action"2);
  30.  
  31.     // 1. verify/clean 'id'
  32.     if !is_numeric($id) )     // must be a number
  33.        throw new InvalidWebInputException("invalid index: <$id> is not an integer");
  34.     //if ( $id===0 )              
  35.     //   throw new InvalidWebInputException(""GuiEditDevice__construct invalid index: zero");
  36.  
  37.     $this->id=$id;                   // remember the record number
  38.     $this->module='IP';              // identify module, in Webcommon
  39.     $this->table='ip';               // identify SQL table, in Webcommon
  40.     
  41.     // 2. verify/clean 'action'
  42.     // Now, have we a REQUEST action to carry out?
  43.     if !isset($action) ) {
  44.        throw new InvalidWebInputException("No action ");
  45.     }
  46.     $this->action=validate_webinput($action);
  47.  
  48.   }
  49.  
  50.  
  51.   public function handle_request()
  52.   {
  53.     $action=$this->action;
  54.     #global $_SESSION, $_REQUEST;
  55.     #$_REQUEST=array_map('validate_webinput',$_REQUEST);
  56.     $this->debug("handle_request() $action"2);
  57.  
  58.     if (isset($action)) {
  59.       if ($action==='Update'{
  60.         echo $this->print_title("Update {$this->module} Details");
  61.         echo $this->InsertOrUpdate();
  62.         echo $this->edit_record();          // Show update form
  63.         echo $this->print_footer();
  64.  
  65.       } else if ($action==='Edit') {
  66.         echo $this->print_title("Edit {$this->module} Details");
  67.         echo $this->edit_record();          // Show update form
  68.         echo $this->print_footer();
  69.        
  70.       } else if ($action==='Add') {
  71.  
  72.         if (isset($_REQUEST['address']) && isset($_REQUEST['comment']) ) {
  73.       // Add step2
  74.           echo $this->print_title("New {$this->module} record");  
  75.           echo $this->InsertOrUpdate(FALSE);  // Add mode
  76.  
  77.         } else {        // Add Step1
  78.           echo $this->print_title("Add new {$this->module}");
  79.           echo $this->edit_record(false);    // Show Add form: update_mode=false
  80.         }
  81.         echo $this->print_footer();
  82.        
  83.       } else if ($action==='Delete') {
  84.         echo $this->Delete($this->table$this->id);
  85.        
  86.       } else {
  87.         // do nothing, action does not concern us.
  88.       }
  89.     }
  90.   }
  91.  
  92.  
  93.  
  94.   /**
  95.    * Insert or Update a record
  96.    */
  97.   public function InsertOrUpdate($update_mode=TRUE)
  98.   {
  99.     $this->debug("InsertOrUpdate() update_mode=$update_mode", 2);
  100.     #var_dump($_REQUEST);
  101.     if ($_SESSION['nac_rights']<2)
  102.       throw new InsufficientRightsException('Update() ' .$_SESSION['nac_rights']);
  103.     $conn=$this->getConnection();     //  make sure we have a DB connection
  104.  
  105.     // Clean inputs from the web, (security). Use _REQUEST to
  106.     // allow both GET (automation) or POST (interactive GUIs)
  107.     $_REQUEST=array_map('validate_webinput',$_REQUEST);
  108.     if (!isset($_REQUEST['action_idx']) )
  109.       throw new InvalidWebInputException("InsertOrUpdate() action_idx not set");
  110.     #if ( !is_numeric($_REQUEST['action_idx']) || $_REQUEST['action_idx']==0) 
  111.     #   throw new InvalidWebInputException("invalid index: is not an integer");
  112.     #$mac=$this->sqlescape($name);
  113.  
  114.     if isset($_REQUEST['address']) )
  115.       throw new DatabaseInsertException("- No address value");
  116.     //if ( ! is_numeric($_REQUEST['address']) )
  117.     //  throw new DatabaseInsertException("- Address is not numeric");
  118.  
  119.     $q='';
  120.     if ($update_mode==TRUE{
  121.       $this->debug("Update() action_idx={$_REQUEST['action_idx']}", 3);
  122.       $this->id=$_REQUEST['action_idx'];
  123.       $q="UPDATE {$this->table} SET ";
  124.  
  125.     } else {
  126.       $q="INSERT INTO {$this->table} SET ";
  127.     }
  128.  
  129.     try {
  130.       $address=trim($_REQUEST['address']);
  131.         $q.=" address=INET_ATON('{$address}') ";
  132.         if (isset($_REQUEST['subnet']))  $q.=", subnet={$_REQUEST['subnet']} ";
  133.         if (isset($_REQUEST['system']))  $q.=", system={$_REQUEST['system']} ";
  134.         if (isset($_REQUEST['status']))  $q.=", status={$_REQUEST['status']} ";
  135.         if (isset($_REQUEST['comment'])) $q.=", comment='{$_REQUEST['comment']}"; //string
  136.         if (isset($_REQUEST['source']))  $q.=", source='{$_REQUEST['source']}";
  137.         if (isset($_REQUEST['dns_update'])) $q.=", dns_update={$_REQUEST['dns_update']} ";
  138.  
  139.       if ($update_mode==TRUE) {
  140.         $q.=" WHERE id={$this->id} LIMIT 1";     // only this record
  141.       }
  142.  
  143.       $this->debug("InsertOrUpdate() Query=" .$q3);
  144.       $res $conn->query($q);
  145.       if ($res === FALSE)
  146.         throw new DatabaseErrorException($conn->error);
  147.  
  148.       // Update Aliases in the systems table, if needed.
  149.       if (isset($_REQUEST['aliases']&& (strlen($_REQUEST['aliases'])>0&& ($_REQUEST['system'0) ) {
  150.         $q"UPDATE systems set dns_alias='{$_REQUEST['aliases']}' WHERE id={$_REQUEST['system']} LIMIT 1 ";
  151.  
  152.         $this->debug("InsertOrUpdate() Query=" .$q3);
  153.         $res $conn->query($q);
  154.         if ($res === FALSE)
  155.           throw new DatabaseErrorException($conn->error);
  156.       }
  157.  
  158.       if ($update_mode==TRUE) {
  159.         echo "<p class='UpdateMsgOK'>Update Successful</p>";
  160.         $this->loggui("{$this->module} id=" .$_REQUEST['id'] .", system id=" .$_REQUEST['system'] ." updated");
  161.  
  162.         // Show follow up instructions/links
  163.         echo "<br><p>Next: make more changes below, or go back to the <a href='{$this->calling_script}'>{$this->module} list</a></p>";
  164.  
  165.       } else {
  166.         echo "<p class='UpdateMsgOK'>Successful: new {$this->module} with address=$address added</p>";
  167.  
  168.         // after inserting, locate that record, and show the Update() screen.
  169.         $q = "SELECT address,id from ip where address=INET_ATON('$address') LIMIT 1";
  170.         #$q = "SELECT address,id from ip where address='" .ip2long($address) ."'";
  171.         $this->debug("InsertOrUpdate() $q", 3);
  172.         $res = $conn->query($q);
  173.         if ($res === FALSE)
  174.           throw new DatabaseErrorException($conn->error);
  175.         while (($row $res->fetch_assoc()) !== NULL{
  176.           $this->id=$row['id'];
  177.           //echo "<p class='UpdateMsgOK'>Index={$this->id}</p>";
  178.         }
  179.         $this->loggui("new {$this->module}, id={$this->id}, address=$address added");
  180.  
  181.         // Show follow up instructions/links
  182.         $ref1=$this->calling_script"?action=Edit&action_idx=$this->id";
  183.         echo "<br><p>Now review/update the <a href='{$ref1}'>{$this->module} details</a> or go back to the <a href='{$this->calling_script}'>{$this->module} list</a></p>";
  184.       }
  185.  
  186.     } catch (Exception $e) {
  187.       throw $e;
  188.     }
  189.   }
  190.  
  191.  
  192.  
  193.   /**
  194.    * Add or display a record, allow changes.
  195.    * Next Step is Either Update, Delete, or Restart Port
  196.    */
  197.   public function edit_record($update_mode=TRUE)
  198.   {
  199.     global $js1;
  200.     if ($_SESSION['nac_rights']<2)
  201.       throw new InsufficientRightsException($_SESSION['nac_rights']);
  202.     $conn=$this->getConnection();     //  make sure we have a DB connection
  203.  
  204.     $output ='<form name="formadd" action="' .$_SERVER['PHP_SELF'.'" method="POST">';
  205.     #$output ='<form action="'.$_SERVER['PHP_SELF'].'" method="GET">'; //debugging
  206.     $output.= "\n$js1\n <table id='GuiEditDeviceAdd'>";
  207.  
  208.     try {
  209.       if ($update_mode) {
  210.         $q=<<<TXT
  211. SELECT id,INET_NTOA(address) AS address,subnet,status,comment,system,source,dns_update from ip
  212.   WHERE id='{$this->id}'
  213.   LIMIT 1
  214. TXT;
  215.         $this->debug("edit_record() update_mode=query, $q", 3);
  216.         $res = $conn->query($q);
  217.         if ($res === FALSE)
  218.           throw new DatabaseErrorException($conn->error);
  219.  
  220.         // Title: Grab the list of field names
  221.         #$fields=$res->fetch_fields();
  222.         #while (($row = $res->fetch_assoc()) !== NULL) {
  223.         $row $res->fetch_assoc();
  224.  
  225.       } else { 
  226.         $this->debug("edit_record() update_mode=add, $q", 3);
  227.         // add mode, only show defaults 
  228.         $row['address']='';
  229.         $row['comment']='';   // TBD: Added on YY by XX
  230.         $row['subnet']=0;
  231.         $row['id']=0;
  232.         $row['source']='';
  233.         $row['system']=0;
  234.       }
  235.  
  236.       # Display the record:
  237.         #$this->debug(var_dump($row), 3);
  238.         $output.= '<tr><td width="87" title="Enter a valid IP address, in the format W.X.Y.Z ">IP Address:</td><td width="400">' ."\n";
  239.         $output.= '<input name="address" type="text" value="' .stripslashes($row['address']) .'" onBlur="checkLen(this,7)"/>' ."\n";
  240.         $output.= '</td><td>Index:' .$row['id'] .'</td>' ."</tr>\n";
  241.  
  242.         $output.= '<tr><td width="87" title="Status? (1=update DNS, 2=reserved - document only)">Status:</td><td width="400">' ."\n";
  243.         $output.=  $this->get_dstatusdropdown($row['status']'</td></tr>'."\n";
  244.  
  245.         // Subnet
  246.         $output.=  '<tr><td title="Which Subnet does this below to? Used for creating reverse DNS records.">Subnet:</td><td>'."\n";
  247.         $output.=  $this->get_subnetdropdown($row['subnet']'</td></tr>'."\n";
  248.  
  249.         // System
  250.         $output.=  '<tr><td title="What End-Device is this IP address linked to?">End-Device:</td><td>' ."\n";
  251.         $output.=  $this->get_systemdropdown($row['system']'</td></tr>'."\n";
  252.  
  253.         #$output.= '<tr><td width="87" title="Update dns?">Dns update:</td><td width="400">' ."\n";
  254.         #$output.= '<input name="dns_update" type="text" value="' .stripslashes($row['dns_update']) .'"/>' ."\n";
  255.  
  256.         $output.= '<tr><td width="87" title="Optional: Aliases (space or comma separated). Changing this value means that the alias field of the above end-device is updated.">Aliases:</td><td width="400">' ."\n";
  257.         $output.= '<input name="aliases" type="text" value="' .$this->get_systemaliases($row['system'].'"/>' ."\n";
  258.  
  259.         $output.= '<tr><td width="87" title="Optional Comment?">Comment:</td><td width="400">' ."\n";
  260.         $output.= '<input name="comment" type="text" value="' .stripslashes($row['comment'].'"/>' ."\n";
  261.  
  262.         //$output.= '<tr><td width="87" title="Optional: source of imported data">IP Source:</td><td width="400">' ."\n";
  263.         //$output.= '<input name="source" type="text" value="' .stripslashes($row['source']) .'"/>' ."\n";
  264.  
  265.         // Status
  266.         //$output.=  '<tr><td>Status:</td><td>' ."\n";
  267.         //$output.=  $this->get_statusdropdown($row['status']) . '</td></tr>'."\n";
  268.  
  269.         // Submit
  270.         $output.= '<tr><td>&nbsp;</td><td></td></tr>' ."\n";
  271.         $output.= '<tr><td>&nbsp;</td><td>' ."\n";
  272.         if ($update_mode{
  273.           $output.=<<<TXT
  274.           <input type="submit" name="action" class="bluebox" value="Update" />&nbsp;
  275.           <input type="submit" name="action" class="bluebox" value="Delete" 
  276.             onClick="javascript:return confirm('Really DELETE this record?')"
  277.             />
  278. TXT;
  279.         } else { 
  280.           // add mode, only show defaults 
  281.           $output.=<<<TXT
  282.         <input type="submit" class="bluebox" name="action" value="Add" onclick="return checkForm()"
  283.                 title="Click to add a new {$this->module} with the above details"/>
  284. TXT;
  285.     }
  286.  
  287.         $output.= '</td></tr> <tr><td>&nbsp;</td><td></td></tr>' ."\n";
  288.         $output.= '<tr><td>&nbsp;</td><td></td></tr>' ."\n";
  289.  
  290.       // close the table
  291.       $output.= '</table> ';
  292.  
  293.       #$output.= '<!input type="hidden" name="action" value="update" />'
  294.       $output.= ''
  295.         . '<input type="hidden" name="action_idx" value="' .$this->id .'" /></form>';
  296.         #. '<input type="hidden" name="id" value="' .$row['id'] .'" /></form>';
  297.  
  298.  
  299.     } catch (Exception $e) {
  300.       if (isset($conn))
  301.         $conn->close();
  302.       throw $e;
  303.     }
  304.  
  305.     return($output);
  306.   }                               // function
  307.  
  308.  
  309. function get_dstatusdropdown($s)
  310. {
  311.    $conn=$this->getConnection();     //  make sure we have a DB connection
  312.  
  313.    if ($_SESSION['nac_rights'== 1{   // read-only
  314.      $q="select value from dstatus where id='$s'";
  315.      $res = $conn->query($q);
  316.      if ($res === FALSE)
  317.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  318.      while (($row $res->fetch_assoc()) !== NULL{
  319.          $ret=$row['value'];
  320.      }
  321.    }
  322.    else if ($_SESSION['nac_rights'] > 1) {   // edit/admin
  323.      $ret='<select name="status">';
  324.  
  325.      $q='SELECT id, value FROM dstatus ORDER BY value ASC';
  326.      $res = $conn->query($q);
  327.      if ($res === FALSE)
  328.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  329.      while (($row $res->fetch_assoc()) !== NULL{
  330.        $ret.='<option value="' .$row['id'].'" '
  331.             .($s==$row['id'] ? 'selected="selected"' : '')
  332.             .'>' .$row['value'] .'</option>' ."\n";
  333.      }
  334.      $ret.="</select> \n";
  335.    }
  336.    return $ret;
  337. }
  338.  
  339.  
  340. function get_systemaliases($s)       // Lookup current aliases
  341. {
  342.    $ret='';
  343.    $conn=$this->getConnection();     //  make sure we have a DB connection
  344.    $q="select dns_alias from systems where id='$s'";
  345.      $this->debug("Query=" .$q3);
  346.      $res $conn->query($q);
  347.      if ($res === FALSE)
  348.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  349.  
  350.      while (($row $res->fetch_assoc()) !== NULL{
  351.          $ret=$row['dns_alias'];
  352.      }
  353.    return $ret;
  354. }
  355.  
  356. function get_systemdropdown($s)       // Show a list of Dend-Devices
  357. {
  358.    $conn=$this->getConnection();     //  make sure we have a DB connection
  359.  
  360.    $q="select id,name,mac,comment,r_ip,r_timestamp from systems ";
  361.    if ($_SESSION['nac_rights'== 1{   // read-only
  362.      $q.=" where id='$s';";
  363.      $res = $conn->query($q);
  364.      if ($res === FALSE)
  365.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  366.      while (($row $res->fetch_assoc()) !== NULL{
  367.          $ret=$row['name'];
  368.      }
  369.    }
  370.    else if ($_SESSION['nac_rights'] > 1) {   // edit/admin
  371.      $q.=" ORDER BY name";
  372.      $ret='<select name="system">';
  373.      $res = $conn->query($q);
  374.      if ($res === FALSE)
  375.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  376.      while (($row $res->fetch_assoc()) !== NULL{
  377.        $ret.='<option value="' .$row['id'].'" '
  378.             .($s==$row['id'] ? 'selected="selected"' : '')
  379.             .'>' .$row['name'] .': ' .$row['r_ip'] .' on ' .$row['r_timestamp'] .', mac=' .$row['mac'] .', comment=' .$row['comment'] .'</option>' ."\n";
  380.      }
  381.      $ret.="</select> \n";
  382.    }
  383.  
  384.    return $ret;
  385. }
  386.  
  387. function get_subnetdropdown($s)       //TBD: return mask too
  388. {
  389.    $conn=$this->getConnection();     //  make sure we have a DB connection
  390.  
  391.    if ($_SESSION['nac_rights'== 1{   // read-only
  392.      $q="select id,ip_address,ip_netmask from subnets where id='$s';";
  393.      $res = $conn->query($q);
  394.      if ($res === FALSE)
  395.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  396.      while (($row $res->fetch_assoc()) !== NULL{
  397.          $ret=$row['ip_address'];
  398.      }
  399.    }
  400.    else if ($_SESSION['nac_rights'] > 1) {   // edit/admin
  401.      //$q="select id,ip_address,ip_netmask from subnets ORDER BY ip_address ASC where id='$s';";
  402.      $q="select id,ip_address,ip_netmask from subnets ORDER BY ip_address";
  403.      $ret='<select name="subnet">';
  404.      $res = $conn->query($q);
  405.      if ($res === FALSE)
  406.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  407.      while (($row $res->fetch_assoc()) !== NULL{
  408.        $ret.='<option value="' .$row['id'].'" '
  409.             .($s==$row['id'] ? 'selected="selected"' : '')
  410.             .'>' .$row['ip_address'] .' /' .$row['ip_netmask'].'</option>' ."\n";
  411.      }
  412.      $ret.="</select> \n";
  413.    }
  414.  
  415.    return $ret;
  416. }
  417.  
  418.  
  419. } // class
  420.  
  421.  
  422. /////////// main() should never get here .. ///////////////////////////////////////
  423. if (isset($_POST['action']) && $_POST['action']=='Edit') {
  424.   $logger=Logger::getInstance();
  425.   $logger->debug("Edit__:action:"$_POST['action']1);
  426. }
  427.  
  428. if ( isset($_POST['submit']) ) {             // form submit, check fields
  429. ## Initialise (standard header for all modules)
  430.   dir(dirname(__FILE__)); set_include_path("./:../");
  431.   require_once('webfuncs.inc');
  432.   $logger=Logger::getInstance();
  433.   $logger->setDebugLevel(1);
  434.   $logger->debug("Edit__ main -submit");
  435.   #echo handle_submit();
  436.  
  437. } else {    
  438.   # Do nothing, we've been included.
  439. }
  440.  

Documentation generated on Mon, 17 Nov 2008 01:10:36 +0100 by phpDocumentor 1.4.0