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

📄 status.php

📁 Linux嵌入式设计配套光盘,学习嵌入式设计可参考
💻 PHP
字号:
<?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("DB_HOST", '127.0.0.1');  /* 127.0.0.1 is better than localhost */define("DB_PORT", 8888);define("DB_USER", "");class Zone {  var $id;  var $name;  var $enabled;  var $alarm;  function Zone($id,$name,$enabled, $alarm) {    $this->id = $id;    $this->name = $name;    $this->enabled = $enabled;    $this->alarm = $alarm;  }}class ActionAndZoneId {  var $action;  var $id;}/*************************************************************** * readZoneStatus():  Read the status of the alarm daemon and  * return an array of Zone structures.   * * Input:   An already opened connection to the alarm daemon.  * Return:  NULL          - on error  *          array of Zone - if successful  * Output:  An array of error messages if NULL is returned.  * Effects: none  **************************************************************/function readZoneStatus($connection, &$error) {  $command = "SELECT id, name, enabled, alarm FROM Zone";  $result = pg_exec($connection, $command);  if (!$result) {     $error[] = "SQL command failed. Connected to \"" . DB_HOST . "\" on port " . DB_PORT . ".";      return NULL;   }  $status = array();  for ($row = 0; $row < pg_NumRows($result); $row++) {    $status[] = new Zone(      pg_result($result, $row, 0),  /* id */      pg_result($result, $row, 1),  /* name */      pg_result($result, $row, 2),  /* enabled */      pg_result($result, $row, 3)); /* alarm */  }  return $status;}function displayZoneStatus($statusArray) {  $tableHeader = <<< TABLE_HEADER        <h2>Zone Status</h2>        <div id=status_table>        <form action=status.php method=POST>         <table>          <tr>            <th>              Zone            </th>            <th style="width: 170px">              Name            </th>            <th>              Status            </th>            <th>            </th>          </tr>TABLE_HEADER;  $widget = $tableHeader;   foreach ($statusArray as $zone) {    if ($zone->enabled && $zone->alarm)       $status = 'Alarm';    else if ($zone->enabled)        $status = 'Safe';    else       $status = 'Inactive';         switch ($status) {    case 'Alarm':      $widget .= "<tr class=menu2-color> <td> $zone->id </td> <td> $zone->name </td> <td> $status </td> <td> <input type=submit name=Clear_$zone->id value=Clear> </td> </tr>\n";      break;    case 'Safe':       $widget .= "<tr> <td> $zone->id </td> <td> $zone->name </td> <td> $status </td> <td> <input type=submit name=Set_$zone->id value=Set> </td> </tr>\n";      break;    default:      $widget .= "<tr> <td> $zone->id </td> <td> $zone->name </td> <td> $status </td> <td> </td> </tr>\n";    }  }  $widget .= "</table></form></div>\n";   $jsInHead = <<< JAVASCRIPT<SCRIPT TYPE="text/javascript"><!-- hide from old browsers  var req_status = null;   var curr_id = 0;  // GetCurrentStatus sends a request for system status.  function GetCurrentStatus() {    // The ms= portion is to make the url unique so that IE doesn't retrieve    // a cached copy.    url = "wait_for_status.php?disp_id=" + curr_id + "&ms=" + new Date().getTime();    if (window.XMLHttpRequest) {      // branch for Firefox/Netscape XMLHttpRequest       req_status = new XMLHttpRequest();      if (req_status) {        req_status.abort();        req_status.onreadystatechange = GotStatus;        req_status.open("GET", url, true);        // Header necessary for lighttpd        req_status.setRequestHeader("Accept", "*/*");        req_status.send(null);      }    } else if (window.ActiveXObject) {      // branch for IE/Windows ActiveX version      try {      req_status = new ActiveXObject("Microsoft.XMLHTTP");      } catch (e) {}      if (req_status) {        req_status.abort();        req_status.onreadystatechange = GotStatus;        req_status.open("GET", url, true);        req_status.setRequestHeader("Accept", "*/*");        req_status.send();      }    }  }  // GotStatus is the read callback for the above XMLHttpRequest() call.  // We update the display.  function GotStatus() {    // only if req_status shows "loaded"    if (!req_status)      return;    if (req_status.readyState != 4)      return;     try {      // avoid FireFox bug      if (req_status.status != 200)        return;    } catch (e) {      return;    }    var zones  = req_status.responseXML.getElementsByTagName("zone");    table = generateHtmlTable(zones);     mdiv = document.getElementById('status_table');    mdiv.innerHTML = table;    var logcount = req_status.responseXML.getElementsByTagName("logcount");    curr_id = logcount[0].childNodes[0].nodeValue;    setTimeout("GetCurrentStatus()", 2000);  }  function generateHtmlTable(zones) {    table = "<form action=status.php method=POST>";    table = table + "<table><tr><th> Zone <\/th> <th style=\"width: 170px\"> Name <\/th><th> Status <\/th><th><\/th><\/tr>";    for (i = 0; i < zones.length; i++) {      zid = i+1;      zname = zones[i].getAttribute("name");      zenabled = zones[i].getAttribute("enabled");      zalarm = zones[i].childNodes[0].nodeValue;      if (zenabled == 1 && zalarm == 1)         table += "<tr class=menu2-color><td>" + zid + "<\/td><td>" + zname + "<\/td><td> Alarm <\/td><td><input type=submit name=Clear_" + zid + " value=Clear> <\/td><\/tr>";      else if (zenabled == 1)         table += "<tr><td>" + zid + "<\/td><td>" + zname + "<\/td><td> Safe <\/td><td><input type=submit name=Set_" + zid + " value=Set> <\/td><\/tr>";      else        table += "<tr><td>" + zid + "<\/td><td>" + zname + "<\/td><td> Inactive <\/td><td><\/td><\/tr>";    }    table = table + "<\/table><\/form>";     return table;  }// end of script hiding --></SCRIPT>JAVASCRIPT;  display_page("Status", "Zone Status", $widget, $jsInHead, "onload=\"GetCurrentStatus()\"");}function displayErrorMsg($error) {  $widget = "<h2>Error</h2>";  foreach ($error as $msg) {      $widget .= "$msg <br>\n";  }  display_page("Status", "Zone Status", $widget);}/* $value should be either 0 or 1 */function setAlarm($connection, $id, $value, &$error) {  if ($id >= 0 && $id <= 5) {    $command = "UPDATE Zone SET alarm=$value WHERE id=$id";    $result = pg_exec($connection, $command);    if (!$result) {      $error[] = "SQL Command failed";       return;    }    pg_freeresult($result);    return;  }}/*  * Returns an ActionAndZone object. */function getActionAndZoneId() {  $info = new ActionAndZoneId();   $info->action = "";  $info->id = -1;  $params = read_params();  foreach ($params as $key => $value) {    if (!strstr($key, "Set_") && !strstr($key, "Clear_"))       continue;    if (strstr($key, "Set_"))       $info->action = "Set";    else      $info->action = "Clear";    $tokens = explode("_", $key);    if (count($tokens) >= 2)      $info->id = $tokens[1];     if (!is_numeric($info->id)) {      $info->action = "";      $info->id = -1;    }    break;  }  return $info;}/* Main function */$error = array();if (!function_exists('pg_connect')) {     $error[] = "The pg_connect() function is not defined.";      $error[] = "This is probably because PHP on the web server was not configured with the --with-pgsql option.";      displayErrorMsg($error);      return; }/* Suppress Postgres error messages */error_reporting(error_reporting() & 0xFFFD);$connection = pg_connect(DB_HOST, DB_PORT, DB_USER);if (!$connection) {     $error[] = "Unable to connect to daemon.  Attempted to connect to \"" . DB_HOST . "\" on port " . DB_PORT . ".";      displayErrorMsg($error);      return;}$info = getActionAndZoneId();if ($info->action == "Set") {  setAlarm($connection, $info->id, 1, $error); } else if ($info->action == "Clear") {  setAlarm($connection, $info->id, 0, $error); } $statusArray = readZoneStatus($connection, $error);if (!$statusArray)   displayErrorMsg($error); else  displayZoneStatus($statusArray); ?>

⌨️ 快捷键说明

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