Location: PHPKode > scripts > Dynamic Configuration > class_config.php
<?php
//////////////////////////////////////////////////////////////////////////////////////////
//
// Start a new session
//
//////////////////////////////////////////////////////////////////////////////////////////
   define("MAX_LOGIN_TRIES",3);
   session_start();
//////////////////////////////////////////////////////////////////////////////////////////
//
// Define everyting needed to connect to the database
//
//////////////////////////////////////////////////////////////////////////////////////////
   define("MYSQL_HOST","");
   define("MYSQL_USER", "");
   define("MYSQL_PASSWORD", "");
   define("MYSQL_DATABASE","");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site Configuration</title>
<style type="text/css">
   body {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: 12px;
      background-color: #110011;
      color: #FFFFFF;
   }
   a {
      color: #FFFF00;
      text-decoration: none;
   }
   a:link {
      color: #FFFF00;
      text-decoration: none;
   }
   a:visited {
      color: #FFFF00;
      text-decoration: none;
   }
   a:hover {
      color: #00FFFF;
      text-decoration: none;
   }
   .buttons {
      color: #000000;
      background-color: #999999;
   }
   .title1 {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: 16px;
      font-weight: bold;
   }
   .menubar {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: 10px;
      border-width: thin;
      border-style: solid;
      border-top-color: #AA00AA;
      border-left-color: #AA00AA;
      border-right-color:#660066;
      border-bottom-color: #660066;
      background-color: #330033;
      width: 98%;
      }
   .hover {
      color: #FFFF00;
      background-color: #660000;
      }
   .normal {
      color: #FFFFFF;
      background-color: #330033;
      }
   .summary_grid {
      width: 98%;
      border-style: none;
   }
   .summary_grid_title {
      color: #FFFFFF;
      font-size: 12px;
      font-weight: bold;
      font-style: italic;
   }
   .summary_grid_cell {
      border-bottom-style: solid;
      border-bottom-width: thin;
      border-bottom-color: #990099;
   }
   .information {
      color: #00FFFF;
      font-style: italic;
   }
</style>
<script type="text/javascript">
   //
   // Reload the page with the given parameters instead of the current ones
   //
   function reloadme() {
      document.location.replace("<?php echo $_SERVER['PHP_SELF']; ?>");
   }
   //
   // Reload the page with the given parameters instead of the current ones
   //
   function reloadmewith(params) {
      document.location.replace("<?php echo $_SERVER['PHP_SELF']; ?>?" + params);
   }
   //
   // Submit the form
   //
   function submitform() {
      document.userform.submit();
   }
   //
   // Change the value of the userform field "action" to the given value and submit
   // the form
   //
   function changeactionandsubmit(newaction) {
      document.userform.action.value = newaction;
      document.userform.submit();
   }
   //
   // Toggle a check box value
   //
   function togglecheckbox(boxName) {
      document.userform.elements[boxName].checked =
         !document.userform.elements[boxName].checked;
   }
   //
   // Sets the given userform's field to the given value
   //
   function setformvalue(name, value, caseSensitive) {
      var oList = document.userform.elements[name];
      var sTestValue = (caseSensitive ? value : value.toLowerCase());
      var sListValue;
      // Ok, it's not a SELECT so just try to set the.value property
      if(oList.type!="select-one") {
         oList.value = value;
         return;
      }
      for (var i = 0; i < oList.options.length; i++)
      {
         sListValue = (caseSensitive ? oList.options[i].text :
         // Change text to value if you wish to compare by value of listbox.
         oList.options[i].text.toLowerCase());
         if (sListValue == sTestValue) {
            oList.selectedIndex = i;
            break;
         }
      }
   }
   //
   // Confirm the request to reset the form before actually resetting it
   //
   function confirmreset() {
      if(confirm("Are you sure you want to reset the values on this form?\n" +
         "ALL CHANGES WILL BE LOST!"))
         document.userform.reset();
   }
   //
   // Sorts a given SELECT list
   //
   function sortlist($id) {
      var list = document.getElementById($id);
      if(!list) return;
      var tmp = new Array();
      var len = list.options.length;
      if(len<1) return;
      for(i=0; i<len; i++) {
         tmp[i] = new Array(list.options[i].text.toLowerCase(),
            new Object(list.options[i]));
      }
      tmp.sort();
      list.options.length = 0;
      for(i=0; i<len; i++) {
         list.appendChild(tmp[i][1]);
      }
      list.selectedIndex = 0;
   }
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>"  method="post"
   enctype="application/x-www-form-urlencoded" name="userform">
<span class="title1">Site Configuration</span><br /><br />
<?php
/*****************************************************************************************
**
** Load the base configuration file and attempt to open a link to the mySQL server
**
*****************************************************************************************/
include_once("class_mysql.php");
$myconfig = new mySQL_Config();
$myconfig->setConnection(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);

/*****************************************************************************************
**
** Process the $_REQUEST['action'] values
**
*****************************************************************************************/
if(isset($_SESSION['loggedin'])) {
   basic_menu();
   if(isset($_REQUEST['action'])) {
      switch(strtolower(trim($_REQUEST['action']))) {
         case "logout":
            //
            // Logging out, so destroy the session and reload this page with the
            // login prompt
            //
            session_unset();
            echo "<script>\nreloadme();</script>";
            break;
         case "edit":
            //
            // If a value is set then draw the view / edit page; otherwise, draw
            // the summary grid
            //
            if(isset($_REQUEST['value'])) {
               draw_edit_form($_REQUEST['value']);
            } else {
               draw_grid();
            }
            break;
         case "kill":
            //
            // If a value is set then draw the confirm kill page; otherwise, draw
            // the summary grid
            //
            if(isset($_REQUEST['value'])) {
               draw_confirm_kill($_REQUEST['value']);
            } else {
               draw_grid();
            }
            break;
         case "confirm_kill":
            //
            // The user confirmed the removal of the setting, so remove it
            //
            if(isset($_REQUEST['value'])) {
               $myconfig->config_remove($_REQUEST['value']);
            }
            draw_grid();
            break;
         case "save":
            //
            // Call the validation routine before saving the value
            //
            $oldname = isset($_REQUEST['name']) ?
               trim($_REQUEST['name']) : "";
            $name = isset($_REQUEST['cfg_setting']) ?
               trim($_REQUEST['cfg_setting']) : "";
            $value = isset($_REQUEST['cfg_value']) ?
               trim($_REQUEST['cfg_value']) : "";
            validate_settings($oldname, $name, $value);
            break;
         case "new":
            //
            // Request for a new setting; the edit function allows for handling
            // of both editing and new records, it just depends on what is sent
            //
            draw_edit_form();
            break;
         default:
            draw_grid();
            break;
      }
   } else {
      draw_grid();
   }
} else {
   if(isset($_REQUEST['action'])) {
      switch(strtolower(trim($_REQUEST['action']))) {
         case "login":
            echo "Processing login...<br>";
            if(strcmp(trim(strtolower($_REQUEST['uid'])),strtolower(MYSQL_USER))==0 &&
               strcmp(trim(),trim())==0) {
               $_SESSION['loggedin'] = true;
               echo "<script>\nreloadme();</script>";
            } else {
               login_screen();
            }
            break;
      }
   } else {
      login_screen();
   }
}

//////////////////////////////////////////////////////////////////////////////////////////
function login_screen() {
   if(isset($_SESSION['login_tries'])) {
      if($_SESSION['login_tries']>MAX_LOGIN_TRIES) {
         echo "<font color=\"#FF0000\">Maximum attempts to login has been reached"
            . "</font><br>";
         return;
      } else {
         $_SESSION['login_tries'] = $_SESSION['login_tries'] + 1;
         echo "<font color=\"#FF0000\">Invalid login attempt</font><br>";
      }
   } else {
      $_SESSION['login_tries'] = 1;
   }
?>
   Please log in using the account and password used to connect to mySQL:<br />
   <br />
    <input type="hidden" name="action" value="login" />
   <table>
      <tr>
         <td align="right">User name:</td>
            <td><input type="text" name="uid" value="" size="30" /></td>
        </tr>
      <tr>
         <td align="right">Password:</td>
            <td><input type="password" name="pwd" value="" size="30" /></td>
        </tr>
        <tr>
         <td>&nbsp;</td>
         <td>
               <input type="submit" value="Login" class="buttons" />
                &nbsp;&nbsp;
                <input type="reset" class="buttons" />
            </td>
        </tr>
    </table>
<?php
}

//////////////////////////////////////////////////////////////////////////////////////////
function basic_menu() {
   global $myconfig;

   echo "<table align=\"center\" cellpadding=\"2\" class=\"menubar\"><tr>\n";
   if($myconfig->mysql_config_allow_new) {
      draw_table_link($_SERVER['PHP_SELF'] . "?action=new","New", 1, 0, 45, "center");
   }
   draw_table_link($_SERVER['PHP_SELF'],"Refresh", 1, 0, 60, "center");
   echo "\t<td>&nbsp;</td>\n";
   draw_table_link($_SERVER['PHP_SELF'] . "?action=logout","Logout", 1, 0, 55,
      "center");
   echo "</tr></table>\n";
}

//////////////////////////////////////////////////////////////////////////////////////////
function edit_menu() {
   echo "<table align=\"center\" cellpadding=\"2\" class=\"menubar\"><tr>\n";
   draw_table_jslink("changeactionandsubmit('save');", "Save", 0, 45, "center");
   draw_table_jslink("confirmreset();", "Reset", 0, 50, "center");
   draw_table_link($_SERVER['PHP_SELF'],"Cancel", 1, 0, 60, "center");
   echo "\t<td>&nbsp;</td>\n";
   echo "</tr></table>\n";
}

//////////////////////////////////////////////////////////////////////////////////////////
function draw_grid() {
   global $myconfig;

   $data = $myconfig->config_get_names();
   echo "<br />\n"
      . "<table align=\"center\" cellpadding=\"2\" class=\"summary_grid\">\n"
      . "<tr>"
         . "<td width=\"30\">&nbsp;</td>"
         . "<td width=\"30\">&nbsp;</td>"
         . "<td class=\"summary_grid_title\">Configuration / Setting Names</td>"
      . "</tr>\n";
   for($x=0; $x<$data['count']; $x++) {
      echo "<tr><td align=\"center\" class=\"summary_grid_cell\">"
         . "<a href=\"" . $_SERVER['PHP_SELF'] . "?action=edit&value={$data[$x]}\">"
         . "View</a></td><td align=\"center\" class=\"summary_grid_cell\">"
         . "<a href=\"" . $_SERVER['PHP_SELF'] . "?action=kill&value={$data[$x]}\">"
         ."Kill</a></td><td class=\"summary_grid_cell\">{$data[$x]}</td></tr>\n";
   }
   echo "</table>\n";
}

//////////////////////////////////////////////////////////////////////////////////////////
function draw_edit_form($name="") {
   global $myconfig;

   if(strlen(trim($name))>0) {
      //
      // If there is a name given, look up its value
      //
      $local['cfg_setting'] = trim($name);
      $local['cfg_value'] = $myconfig->config_get(trim($name));
   } else {
      //
      // Set temporary values to blank
      //
      $local['cfg_setting'] = "";
      $local['cfg_value'] = "";
   }
   //
   // Draw the form
   //
   edit_menu();
   ?>
    <br />
    <input type="hidden" name="action" value="" />
    <input type="hidden" name="name" value="<?php echo $name; ?>" />
    <table>
      <tr>
            <td align="right">Setting:</td>
            <td><input type="text" name="cfg_setting" maxlength="128" size="79" value="<?php
            echo $myconfig->FixQuotes($local['cfg_setting'], true);
            ?>" /></td>
        </tr>
      <tr>
            <td align="right" valign="top">Value:</td>
            <td><textarea name="cfg_value" rows="10" cols="60"><?php
            echo $myconfig->FixQuotes($local['cfg_value'], true);
            ?></textarea></td>
        </tr>
    </table>
    <?php
}

//////////////////////////////////////////////////////////////////////////////////////////
function draw_confirm_kill($name) {
   //
   // Ask the user to confirm the removal of the setting
   //
   ?><br />
   Are you sure you want to remove "<span class="information"><?php
      echo $name; ?></span>"?<br />
   <br />
    <input type="hidden" name="action" value="" />
    <input type="hidden" name="value" value="<?php echo $name; ?>" />
    <input type="button" value="Yes" class="buttons"
      onclick="changeactionandsubmit('confirm_kill')" />
    &nbsp;&nbsp;
    <input type="button" value="No" class="buttons"
      onclick="document.location.replace('<?php echo $_SERVER['PHP_SELF'] ?>')" />
   <?php
}

//////////////////////////////////////////////////////////////////////////////////////////
function validate_settings($oldname, $name, $value) {
   global $myconfig;

   //
   // Make sure there is a name given
   //
   if(strlen(trim($name))<1) {
      echo "<br />You did not provide a setting name."
         . "<br /><br />"
         . "<a href=\"#\" onclick=\"history.go(-1);\">Back to form</a> or "
         . "<a href=\"" . $_SERVER['PHP_SELF'] . "\">continue</a>";
      return;
   }
   //
   // Check to see if the old name and new names match
   //
   if(strcmp(strtolower($oldname),strtolower($name))!=0) {
      //
      // They do not, so make sure the new name does not exist
      //
      if($myconfig->config_exists($name)) {
         //
         // The name already exists, so display an error message with two links (one
         // to return to the editing form, the other to continue back to the list)
         //
         echo "<br />I cannot rename this value from <span class=\"information\">"
            . "$oldname</span> to <span class=\"information\">$name</span> because "
            . "there is already a setting with the new name in the system."
            . "<br /><br />"
            . "<a href=\"#\" onclick=\"history.go(-1);\">Back to form</a> or "
            . "<a href=\"" . $_SERVER['PHP_SELF'] . "\">continue</a>";
         return;
      } else {
         //
         // Remove the setting with the old name
         //
         $myconfig->config_remove($oldname);
      }
   }
   //
   // Add or update the setting
   //
   $myconfig->config_set($name, $value);
   //
   // Draw the grid
   //
   draw_grid();
}

//////////////////////////////////////////////////////////////////////////////////////////
function draw_table_link($link, $text, $replacePage=0, $newRow=0, $width=0, $align="") {
  //
  // This function will draw a new TD in a table (already started) that
  // will act as a link and will change the cell's style class when the
  // user hovers / exits the cell.
  //
  // Optional -- replace the current page or act as a normal link
  // Optional -- write this as a row by itself
  // Optional -- width of the cell
  // Optional -- alignment of the contents
  //
  if ($newRow) echo "<tr>\n";
  echo "\t<td class=\"normal\" "
    . "onMouseOver=\"this.className='hover'\" "
    . "onMouseOut=\"this.className='normal'\" ";
  if($width>0) echo "width=\"$width\" ";
  if(strlen($align)>0) echo "align=\"$align\" ";
  if($replacePage) {
    echo "onClick=\"document.location.replace('$link');\">";
  } else {
    echo "onClick=\"document.location = '$link';\">";
  }
  echo $text . "</td>\n";
  if ($newRow) echo "</tr>\n";
}

//////////////////////////////////////////////////////////////////////////////////////////
function draw_table_jslink($jslink, $text, $newRow=0, $width=0, $align="") {
  //
  // This function will draw a new TD in a table (already started) that
  // will act as a link that calls a javascript function and will
  // change the cell's style class when the user hovers / exits the
  // cell.
  //
  // Optional -- replace the current page or act as a normal link
  // Optional -- width of the cell
  // Optional -- alignment of the contents
  //
  if ($newRow) echo "<tr>\n";
  echo "\t<td class=\"normal\" "
    . "onMouseOver=\"this.className='hover'\" "
    . "onMouseOut=\"this.className='normal'\" ";
  if($width>0) echo "width=\"$width\" ";
  if(strlen($align)>0) echo "align=\"$align\" ";
  echo "onClick=\"$jslink\">$text</td>\n";
  if ($newRow) echo "</tr>\n";
}

/*****************************************************************************************
**
** Close the SQL connection
**
*****************************************************************************************/
$myconfig->close();
?>
</form>
</body>
</html>
Return current item: Dynamic Configuration