⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setup_network.php

📁 Linux嵌入式设计配套光盘,学习嵌入式设计可参考
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php/********************************************************************  * Copyright (c) 2006, Graham P Phillips * All rights reserved. * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met: * *   * Redistributions of source code must retain the above copyright  *     notice, this list of conditions and the following disclaimer. *   * Redistributions in binary form must reproduce the above  *     copyright notice, this list of conditions and the following  *     disclaimer in the documentation and/or other materials provided  *     with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. ********************************************************************/include_once "php_params.php";include_once "layout.php";define('LINUXFROMSCRATCH_STYLE', 1);define('REDHAT_STYLE', 2);        /* Redhat or Mandrake */define('NETWORK_PERSISTENCE_STYLE', LINUXFROMSCRATCH_STYLE);define("DB_HOST", '127.0.0.1');  /* 127.0.0.1 is better than localhost */define("DB_CONFMGR_PORT", 8885);define("DB_NETWORK_PORT", 8884);define("DB_USER", "");define("MENU1", "Setup");define("MENU2", "Network"); define("HOST_NAME_MAX_INPUT", 255);class GlobalNetworkConfig {  var $hostname;  var $gateway;  var $primaryDNS;  var $secondaryDNS;  function GlobalNetworkConfig($hostname, $gateway, $primaryDNS, $secondaryDNS) {    $this->hostname = $hostname;    $this->gateway = $gateway;    $this->primaryDNS = $primaryDNS;     $this->secondaryDNS = $secondaryDNS;   }}class NetInterface {  var $name;  var $isEnabled;  var $isDhcp;  var $ipAddress;  var $netmask;  var $isEditable;  function NetInterface($name, $isEnabled, $isDhcp, $ipAddress, $netmask) {     $this->name = $name;    $this->isEnabled = $isEnabled;    $this->isDhcp = $isDhcp;    $this->ipAddress = $ipAddress;    $this->netmask = $netmask;    $this->isEditable = true;  }}/* Main Begin */$netConn = daemon_connect(DB_HOST, DB_NETWORK_PORT, DB_USER, $error);if (is_null($netConn)) {  display_error_msg(MENU1, MENU2, $error);  return;}$error = array();$confConn = daemon_connect(DB_HOST, DB_CONFMGR_PORT, DB_USER, $error);if (is_null($confConn)) {  display_error_msg(MENU1, MENU2, $error);  pg_close($netConn);  return;}$infs = read_daemon_interfaces($netConn, $confConn, $error); if (is_null($infs)) {  display_error_msg(MENU1, MENU2, $error);  pg_close($confConn);  pg_close($netConn);  return;}$globalConfig = read_daemon_global_network($confConn, $netConn, $error);if (is_null($globalConfig)) {  display_error_msg(MENU1, MENU2, $error);  pg_close($confConn);  pg_close($netConn);  return;}$newInfs = read_ui_interfaces($infs);$newGlobalConfig = read_ui_global_network(); if (!is_null($newInfs)) {  /* The user pressed "Update" for the network interfaces.   * Validate input from UI form.    */  foreach ($newInfs as $inf) {    $tmpInf = find_interface_by_name($infs, $inf->name);     if ($inf->isEnabled && $inf->isDhcp && !empty($inf->ipAddress) && strcasecmp($inf->ipAddress, $tmpInf->ipAddress)) {        $errMsgs = array("You cannot modify the IP Address when the interface is DHCP enabled.");         display_network_setup($globalConfig, $infs, array(), $errMsgs);         pg_close($confConn);        pg_close($netConn);        return;    }    if ($inf->isEnabled && $inf->isDhcp && !empty($inf->netmask) && strcasecmp($inf->netmask, $tmpInf->netmask)) {        $errMsgs = array("You cannot modify the Netmask when the interface is DHCP enabled.");         display_network_setup($globalConfig, $infs, array(), $errMsgs);         pg_close($confConn);        pg_close($netConn);        return;    }    if (!validate_ip_address($inf->ipAddress)) {      if (!$inf->isDhcp && ($inf->isEnabled || !empty($inf->ipAddress))) {        $errMsgs = array("The IP Address for the $inf->name interface was invalid.  Please use dotted-decimal notation for the IP Address.");        display_network_setup($globalConfig, $infs, array(), $errMsgs);         pg_close($confConn);        pg_close($netConn);        return;      }    }       if (!validate_netmask($inf->netmask)) {       if (!$inf->isDhcp && ($inf->isEnabled || !empty($inf->netmask))) {        $errMsgs = array("The Netmask for the $inf->name interface was invalid.  Examples of valid Netmasks are 255.255.255.254, 255.255.255.252 and 255.255.255.0");        display_network_setup($globalConfig, $infs, array(), $errMsgs);         return;      }    }  }  /* We've completed UI input validation */  /* The ipAddress and netmask may be empty if the interface is disabled */  $some_bootproto_changed = false;   foreach ($newInfs as $inf) {    if (has_bootproto_changed($inf, $infs))      $some_bootproto_changed = true;  }  if ($some_bootproto_changed) {    $modInfs = array();    foreach ($newInfs as $inf) {      if (has_bootproto_changed($inf, $infs)) {        $modInfs[] = $inf;         /* We're either changing from dhcp to static or          * visa versa.         */        if ($inf->isDhcp) {          $command = "UPDATE tbl2file SET script_parms=\"network modify DEVICE=$inf->name BOOTPROTO=dhcp STATE=$inf->isEnabled\", do_script=1 where name=\"$inf->name\"";        } else {          if (empty($globalConfig->gateway)) {            $globalConfig->gateway = read_ui_gateway();           }          if (empty($globalConfig->gateway)) {            $errMsgs = array("Please set the Default Gateway first, or set the interface using DHCP and enable it.");            display_network_setup($globalConfig, $infs, array(), $errMsgs);             return;          }	  if (!empty($inf->ipAddress) && !empty($inf->netmask))            $command = "UPDATE tbl2file SET script_parms=\"network modify DEVICE=$inf->name BOOTPROTO=static STATE=$inf->isEnabled IPADDR=$inf->ipAddress NETMASK=$inf->netmask GATEWAY=$globalConfig->gateway\", do_script=1 where name=\"$inf->name\"";	  else	    $command = NULL;	}    	if ($command) {          $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);          if (!$result) {             display_error_msg(MENU1, MENU2, $error);            return;	  }          pg_freeresult($result);	}      } else {	/* XXX Handle other interface changes.	 * If there are multiple interfaces and the user modified         * more than one interface, then only one interface will          * currently be modified.          */      }      /* We have all the modified interfaces in $modInfs.        * Add the remaining non-modified interfaces into $modInfs.       */      foreach ($infs as $inf) {        $tmpInf = find_interface_by_name($modInfs, $inf->name);         if (!$tmpInf)          $modInfs[] = $inf;      }    }    $gateway = read_daemon_gateway($confConn, $netConn, $error);    if (!empty($gateway)) {        $globalConfig->gateway = $gateway;     }    $tmpInfs = read_daemon_interfaces($netConn, $confConn, $error);     if (!empty($tmpInfs)) {       $modInfs = $tmpInfs;    }    display_network_setup($globalConfig, $modInfs);     pg_close($confConn);    pg_close($netConn);    return;  } else {    /* XXX refactor. Put calc_interfaces_diffs inside      * write_daemon_interfaces      */    $modInfs = calc_interface_diffs($newInfs, $infs);    if ($modInfs) {      write_daemon_interfaces($netConn, $confConn, $modInfs, $infs, $error);       /* Re-read interfaces to verify        * that the write worked.        */      $infs = read_daemon_interfaces($netConn, $confConn, $error);       if (is_null($infs)) {        display_error_msg(MENU1, MENU2, $error);        return;      }      $gateway = read_daemon_gateway($confConn, $netConn, $error);      if (!empty($gateway)) {        $globalConfig->gateway = $gateway;       }    }   } }if (!is_null($newGlobalConfig)) {  /* The user pressed "Update" for the global network config.   * Validate input from UI form.    */  if (!validate_ip_address($newGlobalConfig->gateway)) {     $errMsgs = array("The Gateway address was invalid.  Please use dotted-decimal notation.");    display_network_setup($globalConfig, $infs, $errMsgs);     return;  }  if (!validate_ip_address($newGlobalConfig->primaryDNS)) {     $errMsgs = array("The Primary DNS address was invalid.  Please use dotted-decimal notation.");    display_network_setup($globalConfig, $infs, $errMsgs);     return;  }  if (!validate_ip_address($newGlobalConfig->secondaryDNS)) {    $errMsgs = array("The Secondary DNS address was invalid.  Please use dotted-decimal notation.");    display_network_setup($globalConfig, $infs, $errMsgs);     return;  }  $newGlobalConfig->hostname = trim($newGlobalConfig->hostname);   if ($newGlobalConfig->hostname) {    /* The user has modified the hostname.  Validate it.     * Note that if we discovered a hostname greater than      * HOST_NAME_MAX_INPUT then we won't complain.     */     if (!validate_hostname($newGlobalConfig->hostname)) {      $errMsgs = array("The Hostname should start with a letter and be at most " . HOST_NAME_MAX_INPUT . " characters in length.");      display_network_setup($globalConfig, $infs, $errMsgs);       return;    }  }  $rc = write_daemon_global_network($confConn, $netConn, $newGlobalConfig, $errMsgs);  $newGlobalConfig = read_daemon_global_network($confConn, $netConn, $errMsgs);  if (!$rc) {      display_network_setup($newGlobalConfig, $infs, $errMsgs);       return;  }} else {  $newGlobalConfig = $globalConfig; }display_network_setup($newGlobalConfig, $infs); pg_close($confConn);pg_close($netConn);return;/* Main End */function display_network_setup($globalConfig, $interfaces, $globalMsgs=array(), $infMsgs=array()) {    /* cachedGateway should really be passed in as a parameter */  if (!empty($globalConfig->gateway)) {    $cachedGateway = $globalConfig->gateway;   } else {    $cachedGateway = read_ui_gateway();     $globalConfig->gateway = $cachedGateway;   }  $htmlRefresh = gen_html_refresh($cachedGateway);   $htmlInterfaces = gen_html_network_interfaces($interfaces,$cachedGateway,$infMsgs);   $htmlGlobal = gen_html_network_global($globalConfig,$globalMsgs);  $widget = "<h2> Setup Network </h2>\n" . $htmlRefresh . $htmlInterfaces .  "<br>" . $htmlGlobal;  display_page(MENU1, MENU2, $widget);}function gen_html_refresh($gateway) {  $hiddenGatewayHtml = gen_hidden_gateway_html($gateway);   $html = "<form action=setup_network.php method=get>" . $hiddenGatewayHtml . "<p><input type=submit value=Refresh></p></form>\n";  return $html;}function gen_html_network_interfaces($interfaces, $gateway, $errorMsgs=array()) {  $textSize = 15;  /* Re-order interfaces by name: "lo" first, alphabetical after that. */  $kinterfaces = array();   $lo_inf = NULL;   foreach ($interfaces as $inf) {    if ($inf->name == "lo")       $lo_inf = $inf;    else        $kinterfaces[$inf->name] = $inf;  }  ksort($kinterfaces);  if (!is_null($lo_inf))     array_unshift($kinterfaces, $lo_inf);  /* At this point $kinterfaces has the list of sorted interfaces */   $headerHtml = <<< INTERFACE_HEADER          <fieldset>            <legend>Network Devices</legend>            <form action=setup_network.php method=post>            <table>              <tr>                <th>                   Device                </th>                <th>                  State                </th>                <th>                  Type                </th>                <th>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -