Source for file GuiEditEthernet.php

Documentation is available at GuiEditEthernet.php

  1. <?php
  2. /**
  3.  * 
  4.  * GuiEditEthernet.php
  5.  *
  6.  * Long description for file: ethernet 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 GuiEditEthernet extends WebCommon
  21. {
  22.   private $id$action;      // See also WebCommon and Common
  23.  
  24.   function __construct($action$id=0$debug_level=1)
  25.   {
  26.     parent::__construct(false);     // See also WebCommon and Common
  27.     $this->logger->setDebugLevel($debug_level);
  28.     $this->debug("__construct id=$id, debug=$debug_level, action=$action"2);
  29.  
  30.     // The Ethernet table has no 'id' column, unfortunately. But we 
  31.     // leave the relevant code here for this to be fixed later..
  32.     // 1. verify/clean 'id'
  33.     //if ( !is_numeric($id) )     // must be a number
  34.     //   throw new InvalidWebInputException("invalid index: <$id> is not an integer");
  35.     //if ( $id===0 )              
  36.     //   throw new InvalidWebInputException(""GuiEditDevice__construct invalid index: zero");
  37.  
  38.     $this->id=$id;                   // remember the record number
  39.     $this->module='Ethernet';              // identify module, in Webcommon
  40.     $this->table='ethernet';               // identify SQL table, in Webcommon
  41.  
  42.     // 2. verify/clean 'action'
  43.     // Now, have we a REQUEST action to carry out?
  44.     if !isset($action) ) {
  45.        throw new InvalidWebInputException("No action ");
  46.     }
  47.     $this->action=validate_webinput($action);
  48.  
  49.   }
  50.  
  51.  
  52.   public function handle_request()
  53.   {
  54.     $action=$this->action;
  55.     #global $_SESSION, $_REQUEST;
  56.     #$_REQUEST=array_map('validate_webinput',$_REQUEST);
  57.     $this->debug("handle_request() $action"2);
  58.  
  59.     if (isset($action)) {
  60.       if ($action==='Update'{
  61.         echo $this->print_title("Update {$this->module} Details");
  62.         #$logger->debug("action=$action, report1_index=" .$_SESSION['report1_index'], 1);
  63.         echo $this->InsertOrUpdate();
  64.         echo $this->edit_record();          // Show update form
  65.         echo $this->print_footer();
  66.  
  67.       } else if ($action==='Edit') {
  68.         echo $this->print_title("Edit {$this->module} Details");
  69.         echo $this->edit_record();          // Show update form
  70.         echo $this->print_footer();
  71.        
  72.       } else if ($action==='Add') {
  73.  
  74.         if (isset($_REQUEST['vendor']) && isset($_REQUEST['mac']) ) {
  75.           echo $this->print_title("New {$this->module} record");  // Add step2
  76.           echo $this->InsertOrUpdate(FALSE);  // Add mode
  77.         } else {        // Add Step1
  78.           echo $this->print_title("New {$this->module} record");
  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();
  85.        
  86.       } else {
  87.         // do nothing, action does not concern us.
  88.       }
  89.     }
  90.   }
  91.  
  92.  
  93.   /**
  94.    * Delete()  // Override the Webcommon functiion since we do not have
  95.    * an id column.
  96.    */
  97.   public function Delete()  
  98.   {
  99.     if ($_SESSION['nac_rights']<2)
  100.       throw new InsufficientRightsException($_SESSION['nac_rights']);
  101.     if ( $this->id===)              
  102.       throw new InvalidWebInputException("Delete() invalid index: zero");
  103.  
  104.     $ret $this->print_title("Delete {$this->module} record");
  105.     $conn=$this->getConnection();     //  make sure we have a DB connection
  106.     #var_dump($_REQUEST);
  107.     $id=$this->id;
  108.     $this->debug("Delete() index {$id}", 3);
  109.  
  110.     $q="DELETE FROM {$this->table} WHERE mac='{$id}' LIMIT 1";     // only this record
  111.       $this->debug($q3);
  112.       $res $conn->query($q);
  113.       if ($res === FALSE)
  114.         throw new DatabaseErrorException($q ." :: " .$conn->error);
  115.  
  116.     // Inform the user that is was OK
  117.     $txt=<<<TXT
  118. <p class='UpdateMsgOK'>Delete Successful</p>
  119.  <br><p>Go back to the <a href="{$this->calling_href}">{$this->module} list</a></p>
  120. </div>
  121. TXT;
  122.       $ret.= $txt;
  123.       $this->logit("Deleted {$this->module} with Mac Prefix {$id}");
  124.       $this->loggui("Deleted {$this->module} with Mac Prefix {$id}");
  125.     return $ret;
  126.   }
  127.  
  128.  
  129.   /**
  130.    * Update a record
  131.    */
  132.   public function InsertOrUpdate($update_mode=TRUE)
  133.   {
  134.     $this->debug("InsertOrUpdate() update_mode=$update_mode", 2);
  135.  
  136.     #var_dump($_REQUEST);
  137.     if ($_SESSION['nac_rights']<2)
  138.       throw new InsufficientRightsException('Update() ' .$_SESSION['nac_rights']);
  139.     $conn=$this->getConnection();     //  make sure we have a DB connection
  140.  
  141.     // Clean inputs from the web, (security). Use _REQUEST to
  142.     // allow both GET (automation) or POST (interactive GUIs)
  143.     $_REQUEST=array_map('validate_webinput',$_REQUEST);
  144.     if (!isset($_REQUEST['action_idx']) )
  145.       throw new InvalidWebInputException("InsertOrUpdate() action_idx not set");
  146.     #if ( !is_numeric($_REQUEST['action_idx']) || $_REQUEST['action_idx']==0)     // must be a number>0
  147.     #   throw new InvalidWebInputException("invalid index: is not an integer");
  148.     #$mac=$this->sqlescape($name);
  149.  
  150.     $q='';
  151.     if ($update_mode==TRUE{
  152.       $this->debug("Update() action_idx={$_REQUEST['action_idx']}", 3);
  153.       $this->id=$_REQUEST['action_idx'];
  154.       $q="UPDATE {$this->table} SET ";
  155.  
  156.     } else {
  157.       $q="INSERT INTO {$this->table} SET ";
  158.     }
  159.  
  160.     try {
  161.       $q.=($_REQUEST['mac']!='' ? 'mac=\'' .strtoupper($_REQUEST['mac']) .'\' ' : '');
  162.       if (isset($_REQUEST['vendor'])) $q.=", vendor='{$_REQUEST['vendor']}"; //string
  163.       if ($update_mode==TRUE) {
  164.         //$q.=" WHERE id={$this->id} LIMIT 1";     // only this record
  165.         $q.=" WHERE mac='{$this->id}' LIMIT 1";     // only this record
  166.       }
  167.  
  168.       $this->debug("InsertOrUpdate() Query=" .$q3);
  169.       $res $conn->query($q);
  170.       if ($res === FALSE)
  171.         throw new DatabaseErrorException($conn->error);
  172.  
  173.       echo "<p class='UpdateMsgOK'>Update Successful</p>";
  174.       if ($update_mode==TRUE{
  175.         $this->loggui("{$this->module" .$_REQUEST['vendor'] ."/" .$_REQUEST['mac'] ." updated");
  176.  
  177.       } else {
  178.         // after inserting, locate that record, and show the Update() screen.
  179.         #$res = $conn->query("SELECT mac,vendor from ethernet where mac='" .$mac ."'");
  180.         #if ($res === FALSE)
  181.         #  throw new DatabaseErrorException($conn->error);
  182.         #while (($row = $res->fetch_assoc()) !== NULL) {
  183.         #  //$this->id=$row['id'];
  184.         #}
  185.         $this->id=$_REQUEST['mac'];
  186.  
  187.         $this->loggui("{$this->module" .$_REQUEST['vendor'] ."/" .$_REQUEST['mac'] ." added");
  188.  
  189.       }
  190.  
  191.       // locate that record, and show the Update() screen.
  192.         $ref0=$this->calling_script;
  193.         $ref1=$ref0"?action=Edit&action_idx=$this->id";
  194.         echo "<br><p>Now review/update the <a href='{$ref1}'>{$this->module} details</a> or go back to the <a href='{$ref0}'>{$this->module} list</a></p>";
  195.  
  196.  
  197.     } catch (Exception $e) {
  198.       throw $e;
  199.     }
  200.   }
  201.  
  202.  
  203.   /**
  204.    * Display a record, allow changes.
  205.    * Next Step is Either Update, Delete, or Restart Port
  206.    */
  207.   public function edit_record($update_mode=TRUE)
  208.   {
  209.     global $js1;
  210.  
  211.     if ($_SESSION['nac_rights']<2)
  212.       throw new InsufficientRightsException($_SESSION['nac_rights']);
  213.     $conn=$this->getConnection();     //  make sure we have a DB connection
  214.  
  215.     $output ='<form name="formadd" action="' .$_SERVER['PHP_SELF'.'" method="POST">';
  216.     #$output ='<form action="'.$_SERVER['PHP_SELF'].'" method="GET">'; //debugging
  217.     $output.= "\n$js1\n <table id='GuiEditDeviceAdd'>";
  218.  
  219.     try {
  220.       if ($update_mode) {
  221.         $q=<<<TXT
  222. SELECT mac,vendor from ethernet
  223.   WHERE mac='{$this->id}'
  224.   LIMIT 1
  225. TXT;
  226.         $this->debug("edit_record() update_mode=query, $q", 3);
  227.         $res = $conn->query($q);
  228.         if ($res === FALSE)
  229.           throw new DatabaseErrorException($conn->error);
  230.  
  231.         // Title: Grab the list of field names
  232.         #$fields=$res->fetch_fields();
  233.         $row $res->fetch_assoc();
  234.  
  235.       } else {
  236.         $this->debug("edit_record() update_mode=add, $q", 3); // add mode, only show defaults
  237.         $row['mac']='F00001';    // a default not used?
  238.         $row['vendor']='';       // TBD: default?
  239.       }
  240.  
  241.       # Display the record:
  242.         #$this->debug(var_dump($row), 3);
  243.         $output.= '<tr><td width="87" title="What is name of the Vendor?">Vendor:</td><td width="400">' ."\n";
  244.         $output.= '<input name="vendor" type="text" value="' .stripslashes($row['vendor']) .'"/>' ."\n";
  245.         //$output.= '</td><td>Index:' .$row['id'] .'</td>' ."</tr>\n";
  246.         // MAC
  247.         $output.= '<tr><td title="Enter a valid 6 digit hex MAC prefix, in the format xxxxxx ">MAC prefix:</td><td>' ."\n";
  248.         $output.= '<input name="mac"  type="text" value="' .$row['mac'] .'" />' ."\n";
  249.         $output.= '</td></tr>'."\n";
  250.  
  251.         // Submit
  252.         $output.= '<tr><td>&nbsp;</td><td></td></tr>' ."\n";
  253.         $output.= '<tr><td>&nbsp;</td><td>' ."\n";
  254.         if ($update_mode) {
  255.           $output.=<<<TXT
  256.           <input type="submit" name="action" class="bluebox" value="Update" />&nbsp;
  257.           <input type="submit" name="action" class="bluebox" value="Delete"
  258.             onClick="javascript:return confirm('Really DELETE this record?')"
  259.             />
  260. TXT;
  261.         } else {
  262.           // add mode, only show defaults
  263.           $output.=<<<TXT
  264.         <input type="submit" class="bluebox" name="action" value="Add" onclick="return checkForm()"
  265.                 title="Click to add a new {$this->module} with the above details"/>
  266. TXT;
  267.         }
  268.  
  269.         $output.= '</td></tr> <tr><td>&nbsp;</td><td></td></tr>' ."\n";
  270.         $output.= '<tr><td>&nbsp;</td><td></td></tr>' ."\n";
  271.  
  272.       // close the table
  273.       $output.= '</table> ';
  274.  
  275.       #$output.= '<!input type="hidden" name="action" value="update" />'
  276.       $output.= ''
  277.         . '<input type="hidden" name="action_idx" value="' .$this->id .'" /></form>';
  278.         #. '<input type="hidden" name="id" value="' .$row['id'] .'" /></form>';
  279.  
  280.  
  281.     } catch (Exception $e) {
  282.       if (isset($conn))
  283.         $conn->close();
  284.       throw $e;
  285.     }
  286.  
  287.     return($output);
  288.   }                               // function
  289.  
  290.  
  291.  
  292. } // class
  293.  
  294.  
  295. /////////// main() should never get here .. ///////////////////////////////////////
  296. if (isset($_POST['action']) && $_POST['action']=='Edit') {
  297.   $logger=Logger::getInstance();
  298.   $logger->debug("Edit__:action:"$_POST['action']1);
  299. }
  300.  
  301. if ( isset($_POST['submit']) ) {             // form submit, check fields
  302. ## Initialise (standard header for all modules)
  303.   dir(dirname(__FILE__)); set_include_path("./:../");
  304.   require_once('webfuncs.inc');
  305.   $logger=Logger::getInstance();
  306.   $logger->setDebugLevel(1);
  307.   $logger->debug("Edit__ main -submit");
  308.   #echo handle_submit();
  309.  
  310. } else {    
  311.   # Do nothing, we've been included.
  312. }
  313.  

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