Location: PHPKode > projects > DebWeb GUI > debwgui/services.php
<?php require_once("includes/acl.inc"); ?>
<p class="pgtitle">System: Services</p>
<?php
// checking if a command is defined and not empty, (need more securing)
if (isset($_POST['command']) && !empty($_POST['command'])) {
  // we execute the command and printing the message from shell
  exec("$SUDO $DIR_INITD/".$_POST['servicefile']." ".$_POST['command']."",$result,$errorlevel);
print "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">".
      "<tr><td bgcolor=\"#68a468\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"images/check.gif\" alt=\"info\" width=\"28\" height=\"32\"></td>".
      "<td bgcolor=\"#d9e8d9\" style=\"padding-left: 8px; padding-top: 6px\">".
      "<span class=\"infomsg\">Result of action:</span>".
      "<ul>".
      "<li class=\"successmsg\">".implode("<br>",$result)."</li>".
      "<li class=\"successmsg\">$errorlevel</li>".
      "</ul>".
      "</td></tr></table></p>";

  // we want to sleep for x seconds to make sure PHP reports correct info after a command is runned, sleep timings are individual.
  sleep ($_POST['sleep']);
}

// checking if a change boot command is defined and not empty (needs more security)
if (isset($_POST['bootchange']) && !empty($_POST['bootchange'])) {
  // we check wich action is taken and execute the boot change command and print 1 line of result
  if ($_POST['bootchange'] == "addboot") {
    exec("$CMD_UPDATERCD ".$_POST['servicefile']." defaults",$result);
    print"<p>$result[0]</p>";
  } elseif($_POST['bootchange'] == "removeboot") {
    exec("$CMD_UPDATERCD -f ".$_POST['servicefile']." remove",$result);
    print"<p>$result[0]</p>";
  }
} 

?>
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="25%" class="listhdrr">Service</td>
      <td width="15%" class="listhdrr">Status</td>
      <td width="15%" colspan="2" class="listhdrr">Starts at boot</td>
      <td width="4%" class="list"></td>
      <td width="6%" class="list"></td>
    </tr>
<?php
// includes services array
require_once('includes/services.inc');
sort($services);
exec("$CMD_PS -e",$psarray);
foreach ($services as $eachservice) {
  //  we split and make a list of the services array so it's easier to process 
  list($name, $initfile, $process, $binaryfile, $pidfile, $startcmd, $stopcmd, $restartcmd, $delay) = explode(":", $eachservice);
  // we check if a program is "installed" by looking for it's init.d file in the directory specified in config.php
  if (file_exists("$DIR_INITD/$initfile") && file_exists($binaryfile)) {
    // grep for a process in the array
    $procrunning = preg_grep('/'.$process.'/',$psarray);
    
    // see if the result is empty or not (means process is running or not) and set variables according to result
    if (empty($procrunning)) {
      $status['current'] = "is not running";
      $status['choice']     = "$startcmd";
      $status['image']   = "<a href=\"javascript: submit$name()\" onclick=\"return confirm('Are you sure want to START $name?')\"><img src=\"images/y.gif\" width=\"17\" height=\"17\" alt=\"Start Service\" border=\"0\"></a>";
    } else {
      $status['current'] = "is running";
      $status['choice']     = "$stopcmd";
      $status['image']   = "<a href=\"javascript: submit$name()\" onclick=\"return confirm('Are you sure want to STOP $name?')\"><img src=\"images/x.gif\" alt=\"Stop Service\" width=\"17\" height=\"17\" border=\"0\"></a>";
    }
    
    // boot
    exec("$CMD_LS /etc/rc2.d/S*".$initfile." 2>&1", $result, $errorlevel);
    if ($errorlevel == "0") {
      $bootstatus = "yes";
      $bootchanges = "removeboot";
    } else {
      $bootstatus = "no";
      $bootchanges = "addboot";
    }
    
    // printing the html and javascript that can start/stop/restart services etc.. (with newlines so the code looks a little cleaner in the browser)
    print"<tr>\n".
         "  <td class=\"listlr\">$name</td>\n".
         "  <td class=\"listr\">".$status['current']."</td>\n".
         "  <td class=\"listr\">$bootstatus</td>\n".
         "  <td align=\"center\" class=\"listr\">[<a href=\"javascript: submitbootchange$name()\" onclick=\"return confirm('Are you sure you want to change boot config of $name?')\">change</a>]\n".
         "  <form name=\"formbootchange$name\" method=\"post\" action=\"\">\n".
         "  <input type=\"hidden\" name=\"servicefile\" value=\"$initfile\">\n".
         "  <input type=\"hidden\" name=\"bootchange\" value=\"$bootchanges\">\n".
         "  </form>\n".
         "<script type=\"text/javascript\">\n".
         "  function submitbootchange$name() { \n    document.formbootchange$name.submit();\n  }\n".
         "</script>\n".
         "</td>\n".
         "  <td valign=\"middle\" nowrap class=\"list\">\n".
         "  <form name=\"form$name\" method=\"post\" action=\"\">\n".
         "  <input type=\"hidden\" name=\"servicefile\" value=\"$initfile\">\n".
         "  <input type=\"hidden\" name=\"command\" value=\"".$status['choice']."\">\n".
         "  <input type=\"hidden\" name=\"sleep\" value=\"$delay\">\n".
         "  ".$status['image']."\n".
         "  </form>\n".
         "<script type=\"text/javascript\">\n".
         "  function submit$name() { \n    document.form$name.submit();\n  }\n".
         "</script>\n".
         "</td>".
         "  <td valign=\"middle\" nowrap>".
         "  <form name=\"formrestart$name\" method=\"post\" action=\"\">\n".
         "  <input type=\"hidden\" name=\"servicefile\" value=\"$initfile\">\n".
         "  <input type=\"hidden\" name=\"command\" value=\"$restartcmd\">\n".
         "  <input type=\"hidden\" name=\"sleep\" value=\"$delay\">\n".
         "  <a href=\"javascript: submitrestart$name()\" onclick=\"return confirm('Are you sure want to RESTART $name?')\"><img src=\"images/r.gif\" alt=\"Restart Service\" width=\"17\" height=\"17\" border=\"0\"></a>".
         "  </form>\n".
         "<script type=\"text/javascript\">\n".
         "  function submitrestart$name() { \n    document.formrestart$name.submit();\n  }\n".
         "</script>\n".
         "  </td>\n".
         "</tr>\n";
  }
}

?>
Return current item: DebWeb GUI