[slbackup-commit] CVS update: slbackup-php/src index.php

finnarne-guest at alioth.debian.org finnarne-guest at alioth.debian.org
Thu Apr 12 13:06:08 UTC 2007


  User: finnarne-guest
  Date: 07/04/12 13:06:08

  Added:       src      index.php
  Log:
  Moved index.php to src/index.php
  Moved mypass.sh to script/mypass.sh
  
  Revision  Changes    Path
  1.1                  slbackup-php/src/index.php
  
  Index: index.php
  ===================================================================
  <?php
  
  function readcron ($passwd) {
  global $backuphost, $backupuser, $backupcron, $ssh_options, $ssh_askpass ;
  
      $cron = array() ; 
      $cmd = sprintf ("ssh %s %s@%s cat %s", 
     		     $ssh_options, $backupuser, 
  		     $backuphost, $backupcron) ; 
  
      $desc[0] = array ("pipe", "r") ; 
      $desc[1] = array ("pipe", "w") ; 
      $desc[2] = array ("file", "/tmp/error.log", "a") ; 
  
      $env = array ('SSH_ASKPASS' => $ssh_askpass, 
  		  'DISPLAY' => ':nowhere') ; 
      $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
      if (is_resource ($proc)) {
  	fprintf ($pipes[0], "%s\n", $passwd) ; 
  	fclose ($pipes[0]) ; 
  	while ($line = fgets ($pipes[1], 1024))
  	    $cron[] = trim ($line) ; 
  	fclose ($pipes[1]) ; 
  	proc_close ($proc) ; 
      }
      return $cron ; 
  }
  
  function writecron ($passwd, $cron) {
  global $backuphost, $backupuser, $backupcron, $ssh_options, $ssh_askpass ;
  
      $cmd = sprintf ("ssh %s %s@%s 'cat > %s'", 
     		     $ssh_options, $backupuser, 
  		     $backuphost, $backupcron) ; 
  
      $desc[0] = array ("pipe", "r") ; 
      $desc[1] = array ("pipe", "w") ; 
      $desc[2] = array ("file", "/tmp/error.log", "a") ; 
  
      $env = array ('SSH_ASKPASS' => $ssh_askpass, 
  		  'DISPLAY' => ':nowhere') ; 
      $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
      if (is_resource ($proc)) {
  	fprintf ($pipes[0], "%s\n", $passwd) ; 
  	fprintf ($pipes[0], "%s", $cron) ; 
  	fclose ($pipes[1]) ; 
  	fclose ($pipes[0]) ; 
  	proc_close ($proc) ; 
  	}
  }
  
  
  function writeconf ($passwd, $config) {
  global $backuphost, $backupuser, $backupconf, $ssh_options, $ssh_askpass ;
  
      $out = sprintf ("<client>\n") ;
      foreach ($config["clients"] as $key => $value) {
  	$out .= sprintf ("    <%s>\n", $key) ; 
  	$out .= sprintf ("        address %s\n", $value["address"]) ; 
  	$out .= sprintf ("        keep    %s\n", $value["keep"]) ; 
  	foreach ($value["location"] as $loc)
  	    $out .= sprintf ("        location %s\n", $loc) ; 
  	$out .= sprintf ("        type    %s\n", $value["type"]) ; 
  	$out .= sprintf ("        user    %s\n", $value["user"]) ; 
  	$out .= sprintf ("    </%s>\n", $key) ; 
      }
      $out .= sprintf ("<client>\n") ;
      $out .= sprintf ("server_address %s\n", $config["server_address"]) ; 
      $out .= sprintf ("server_destdir %s\n", $config["server_destdir"]) ; 
      $out .= sprintf ("server_type    %s\n", $config["server_type"]) ; 
      $out .= sprintf ("server_user    %s\n", $config["server_user"]) ; 
  
      $cmd = sprintf ("ssh %s %s@%s 'cat > %s'", 
     		     $ssh_options, $backupuser, 
  		     $backuphost, $backupconf) ; 
  
      $desc[0] = array ("pipe", "r") ; 
      $desc[1] = array ("pipe", "w") ; 
      $desc[2] = array ("file", "/tmp/error.log", "a") ; 
  
      $env = array ('SSH_ASKPASS' => $ssh_askpass, 
  		  'DISPLAY' => ':nowhere') ; 
      $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
      if (is_resource ($proc)) {
  	fprintf ($pipes[0], "%s\n", $passwd) ; 
  	fprintf ($pipes[0], "%s\n", $out) ; 
  	fclose ($pipes[1]) ; 
  	fclose ($pipes[0]) ; 
  	proc_close ($proc) ; 
  	}
  }
  
  function readconf ($passwd) {
  global $backuphost, $backupuser, $backupconf, $ssh_options, $ssh_askpass ;
  
      $cmd = sprintf ("ssh %s %s@%s cat %s", 
     		     $ssh_options, $backupuser, 
  		     $backuphost, $backupconf) ; 
  
      $desc[0] = array ("pipe", "r") ; 
      $desc[1] = array ("pipe", "w") ; 
      $desc[2] = array ("file", "/tmp/error.log", "a") ; 
  
      $env = array ('SSH_ASKPASS' => $ssh_askpass, 
  		  'DISPLAY' => ':nowhere') ; 
      $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
      if (is_resource ($proc)) {
  	fprintf ($pipes[0], "%s\n", $passwd) ; 
  	fclose ($pipes[0]) ; 
  	while (fscanf ($pipes[1], "\t%s\t%s", $key, $value)) {
  	    if (isset ($value)) {
  		switch (strtolower($key)) {
  		    case "address": 
  		    case "keep": 
  		    case "type": 
  		    case "user": 
  			$clientconf[$key] = $value ; 
  			break ; 
  		    case "location": 
  			$clientconf[$key][] = $value ; 
  			break ; 
  		    case "server_address": 
  		    case "server_destdir": 
  		    case "server_type": 
  		    case "server_user": 
  			$config[$key] = $value ; 
  			break ; 
  		}
  	    } else {
  		switch ($key) {
  		    case "<client>": 
  			$readclient = true ; 
  			break ; 
  		    case "</client>": 
  			$readclient = false ; 
  			break ; 
  		    default:
  			if ($readclient && empty ($newclient)) {
  			    $newclient = trim ($key, "<>") ; 
  			    $clientconf = array () ; 
  			    $clientconf["location"] = array () ; 
  			} elseif ($readclient && $key == "</" . $newclient . ">") {
  			    $config["clients"][$newclient] = $clientconf ; 
  			    unset ($newclient) ; 
  			    unset ($clientconf) ; 
  			}
  			break ; 
  		}
  	    }
  	    unset ($value) ; 
  	    unset ($key) ; 
  	}
  	fclose ($pipes[1]) ; 
  	proc_close ($proc) ; 
      }
      return $config ; 
  }
  
  # Define some variables to easier change configuration during development
  $backuphost="localhost" ; 
  $backupuser="root" ; 
  $backupconf="/etc/slbackup/slbackup.conf" ; 
  $backupcron="/etc/cron.d/slbackup" ; 
  $ssh_options="-o StrictHostKeyChecking=no" ; 
  # fetch script to use whith passing the ssh-password
  $ssh_askpass = sprintf ("%s/mypass.sh", dirname ($_SERVER["SCRIPT_FILENAME"]));
  
  
  # Fetch arguments passed as the script is executed
  foreach ($_POST as $key => $value) {
      switch ($key) {
          case "Passwd": 
              $passwd = $value ; 
              break ; 
          case "addclient":
          case "server":
          case "chooseclient":
          case "delclient":
          case "clientconfig":
          case "delloc":
          case "addloc":
          case "scheduler":
              $submit=$key ; 
              break ;
          case "client":
              $client=htmlspecialchars(trim($value)) ; 
              break ;
          default:
              $newconf[$key] = htmlspecialchars(trim($value)) ; 
              break ;
      }
  }
  
  
  printf ("<html>\n") ; 
  printf ("<head>\n") ; 
  printf ("<title>SLBackup Configuration</title>\n") ; 
  printf ("</head>\n") ; 
  printf ("<body>\n") ; 
  
  
  if (isset ($passwd))
      $config = readconf ($passwd) ;
  
  if (empty ($config)) {
      printf ("<FORM name=login method=POST>\n") ; 
      printf ("To read the configuration, you must log in as %s on %s.<BR>",
  	    $backupuser, $backuphost) ; 
      printf ("Please enter the password for the %s account on %s:<BR>",
  	    $backupuser, $backuphost) ; 
      printf ("<INPUT type=password name=Passwd><BR>\n") ; 
      printf ("<INPUT type=submit name=login value='Login'>\n") ; 
      printf ("<INPUT type=reset name=Reset value='Reset'>\n") ; 
      printf ("</FORM>") ; 
      return ; 
  }
  
  $scheduler = readcron ($passwd) ; 
  
  foreach ($scheduler as $line) {
      if ($line[0] == "#") 
          continue ; 
      if (strpos ($line, "slbackup-cron") === false)
          continue ; 
      $config["active"] = true ; 
      $array = explode (" ", $line) ; 
      sscanf ($array[0], "%d", $config["minutes"]) ; 
      sscanf ($array[1], "%d", $config["hours"]) ; 
  }
  switch ($submit) {
      case "scheduler": 
          $newsched = "" ; 
          foreach ($scheduler as $line) {
  	    if ($line[0] == "#") 
  		$newsched .= sprintf ("%s\n", $line) ; 
  	    elseif (strpos ($line, "slbackup-cron") === false)
  		$newsched .= sprintf ("%s\n", $line) ; 
  	    elseif (empty ($newconf["active"])) {
  		$newsched .= sprintf ("#%s\n", $line) ; 
                  $changed = true ; 
                  $config["active"] = false ; 
  	    } else {
  	        if ($config["minutes"] <> $newconf ["minutes"])
  	            $changed = true ; 
  	        if ($config["hours"] <> $newconf ["hours"])
  	            $changed = true ; 
  	        if ($changed) {
  	            $config["active"] = true ; 
  	            $config["minutes"] = $newconf ["minutes"] ; 
  		    $config["hours"] = $newconf ["hours"] ; 
  		    $newsched .= sprintf ("%02d %02d * * * root if [ -x /usr/share/slbackup/slbackup-cron -a -f /etc/slbackup/slbackup.conf ]; then /usr/share/slbackup/slbackup-cron ; fi\n", $newconf["minutes"], $newconf["hours"]) ; 
  		} else 
  		    $newsched .= sprintf ("%s\n", $line) ; 
              }
          }
          if (!$changed and empty($config["active"]) and 
               $newconf["active"] == "on") {
  	    $config["active"] = true ; 
  	    $config["minutes"] = $newconf ["minutes"] ; 
  	    $config["hours"] = $newconf ["hours"] ; 
  	    $newsched .= sprintf ("%02d %02d * * * root if [ -x /usr/share/slbackup/slbackup-cron -a -f /etc/slbackup/slbackup.conf ]; then /usr/share/slbackup/slbackup-cron ; fi\n", $newconf["minutes"], $newconf["hours"]) ; 
  	    $changed = true ; 
  	}
          if ($changed) {
  	    writecron ($passwd, $newsched);
  	}
          break ;
      case "server":
          foreach (array ("server_address", "server_destdir", "server_user", "server_type") as $key) {
              if ($newconf[$key] <> $config[$key]) {
                  $changed = true ; 
                  $config[$key] = $newconf[$key] ;
              }
          }
  	if ($changed)
  	    writeconf ($passwd, $config) ; 
  	break ; 
      case "delclient": 
  	if (empty ($client)) {
  	    printf ("No client specified :(<BR>\n") ; 
  	    break ; 
  	}
  	$pos = array_search($client, array_keys ($config["clients"])) ;
          if ($pos === false)
  	    printf ("Unable to find %s in the list of clients<BR>\n", 
  	 	    $client) ; 
  	else {
  	    array_splice ($config["clients"],
  	                  $pos,1) ; 
  	    writeconf ($passwd, $config) ; 
  	}
  	unset ($client) ; 
  	break ;
      case "addclient":
          $client="<new>" ; 
          $config["clients"][$client]["keep"]="185" ; 
          $config["clients"][$client]["user"]="root" ; 
          break ;
      case "clientconfig":
          if ($client == "&lt;new&gt;") {
  	    $client = $newconf["client_address"] ; 
  	    $config["clients"][$client]["location"] = array () ;
          }
          foreach (array ("address", "keep", "user", "type") as $key) {
              if ($newconf["client_".$key] <> $config["clients"][$client][$key]) {
                  $changed = true ; 
                  $config["clients"][$client][$key] = $newconf["client_" . $key] ;
              }
          }
  	if ($changed)
  	    writeconf ($passwd, $config) ; 
  	break ; 
      case "delloc": 
  	if (empty ($newconf["location"]))
  	    break ; 
  	$pos = array_search ($newconf["location"], 
  		         $config["clients"][$client]["location"]) ;
          if ($pos === false)
  	    printf ("Unable to find %s in the locations of %s<BR>\n", 
  	 	    $newconf["location"], $client) ; 
  	else
  	    array_splice ($config["clients"][$client]["location"],
  	                  $pos,1) ; 
  	writeconf ($passwd, $config) ; 
  	break ;
      case "addloc": 
  	if (empty ($newconf["newloc"]))
  	    break ; 
  	if (in_array ($newconf["newloc"], 
  		      $config["clients"][$client]["location"]))
  	    break ;
  	$config["clients"][$client]["location"][] = $newconf["newloc"] ; 
  	writeconf ($passwd, $config) ; 
  	break ;
  }
  
  ?>
  <FORM method=post>
  <INPUT type=hidden name=Passwd value="<?php echo $passwd ?>">
  <H2>Server scheduler</H2>
  Backup enabled 
  <INPUT type=checkbox name=active <?php if ($config["active"]) echo "checked" ?>>
  Run backup at
  <select name=minutes>
  <?php
      for ($i = 0 ; $i < 60 ; $i += 5)
          printf ("<OPTION value=%02d %s>%02d</option>", 
                  $i, ($i == $config["minutes"] ? "selected" : ""), $i) ; 
  ?>
  </select>
  <select name=hours>
  <?php
      for ($i = 0 ; $i < 24 ; $i++)
          printf ("<OPTION value=%02d %s>%02d</option>", 
                  $i, ($i == $config["hours"] ? "selected" : ""), $i) ; 
  ?>
  </select>
  <INPUT type=submit name=scheduler value="Update">
  <INPUT type=reset name=reset value="Reset">
  <H2>Server config</H2>
  <TABLE>
      <TR>
          <TD>Address</TD>
          <TD>
              <INPUT type=text name=server_address 
                     value="<?php echo $config[server_address] ?>">
          </TD>
          <TD>Destdir</TD>
          <TD>
              <INPUT type=text name=server_destdir 
                     value="<?php echo $config[server_destdir] ?>">
          </TD>
      </TR>
      <TR>
          <TD>Servertype</TD>
          <TD>
              <SELECT name=server_type>
  <?php
      foreach (array ('local', 'remote') as $value) 
          printf ("<OPTION value=%s %s>%s</option>", 
                  $value, 
                  ($config[server_type] == $value ? "selected": ""),
                  $value) ; 
  ?>
              </SELECT>
          </TD>
          <TD>User</TD>
          <TD>
              <INPUT type=text name=server_user 
                     value="<?php echo $config[server_user] ?>">
          </TD>
      </TR>
      <TR>
          <TD colspan=4 align=right>
              <INPUT type=submit name=server 
                     value="Update">
              <INPUT type=reset name=reset 
                     value="Reset">
          </TD>
      </TR>
  
  </TABLE>
  <H2>Client config</H2>
  Client: 
  <select name=client>
  <?php
  foreach ($config["clients"] as $key => $value) {
      if (empty($client))
          $client=$key ; 
      printf ("<option value='%s' %s>%s</option>\n", 
              $key, ($key == $client) ? "selected" : "" , $key) ; 
  }
  ?>
  </select>
  <INPUT type=submit name=chooseclient value="Choose">
  <INPUT type=submit name=addclient value="New">
  <INPUT type=submit name=delclient value="Remove">
  <TABLE>
      <TR>
          <TD>Address</TD>
          <TD>
              <INPUT type=text name=client_address 
                     value="<?php echo $config["clients"][$client]["address"] ?>">
          </TD>
          <TD>Backup Age</TD>
          <TD>
              <INPUT type=text name=client_keep
                     value="<?php echo $config["clients"][$client]["keep"] ?>">
          </TD>
      </TR>
      <TR>
          <TD>Clienttype</TD>
          <TD>
              <SELECT name=client_type>
  <?php
      foreach (array ('local', 'remote') as $value) 
          printf ("<OPTION value=%s %s>%s</option>", 
                  $value, 
                  ($config["clients"][$client]["type"] == $value ? "selected": ""),
                  $value) ; 
  ?>
              </SELECT>
          </TD>
          <TD>User</TD>
          <TD>
              <INPUT type=text name=client_user 
                     value="<?php echo $config["clients"][$client]["user"] ?>">
          </TD>
      </TR>
      <TR>
          <TD colspan=4 align=right>
              <INPUT type=submit name=clientconfig 
                     value="<?php echo ($client == "<new>"? "Add": "Update") ?>">
              <INPUT type=reset name=reset 
                     value="reset">
          </TD>
      </TR>
      <TR>
          <TD>Locations:</TD>
          <TD><INPUT type=text name=newloc></TD>
          <TD><INPUT Type=submit name=addloc value="Add"></TD>
      </TR>
      <TR>
          <TD/>
          <TD>
  <select name=location size=5>
  <?php
  foreach ($config["clients"][$client][location] as $key => $value) {
      printf ("<option value=%s>%s</option>\n", 
              $value, $value) ; 
  }
  ?>
  </select>
          </TD>
          <TD valign=top><INPUT type=submit name=delloc value="Delete"></TD>
      </TR>
  </TABLE>
  </FORM>
  
  
  



More information about the slbackup-commit mailing list