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

finnarne-guest at alioth.debian.org finnarne-guest at alioth.debian.org
Wed Apr 11 16:44:17 UTC 2007


  User: finnarne-guest
  Date: 07/04/11 16:44:17

  Modified:    .        index.php
  Log:
  Moved the reading of config-file into a separate function, to avoid
  conflicts in variable names, and to make it easier to duplicate things into writeconf :)
  
  Revision  Changes    Path
  1.7       +84 -62    slbackup-php/index.php
  
  Index: index.php
  ===================================================================
  RCS file: /cvsroot/slbackup/slbackup-php/index.php,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- index.php	11 Apr 2007 14:21:38 -0000	1.6
  +++ index.php	11 Apr 2007 16:44:17 -0000	1.7
  @@ -1,10 +1,78 @@
   <?php
   
  +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 () ; 
  +			} elseif ($readclient && $key == "</" . $newclient . ">") {
  +			    $config["client"][$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" ; 
   $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) {
  @@ -30,8 +98,6 @@
       }
   }
   
  -# fetch script to use whith passing the ssh-password
  -$ssh_askpass = sprintf ("%s/mypass.sh", dirname ($_SERVER["SCRIPT_FILENAME"])); 
   
   printf ("<html>\n") ; 
   printf ("<head>\n") ; 
  @@ -40,7 +106,10 @@
   printf ("<body>\n") ; 
   
   
  -if (empty ($passwd)) {
  +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) ; 
  @@ -53,64 +122,18 @@
       return ; 
   }
   
  -$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 ($client)) {
  -                        $client = trim ($key, "<>") ; 
  -                        $clientconf = array () ; 
  -                    } elseif ($readclient && $key == "</" . $client . ">") {
  -                        $config["client"][$client] = $clientconf ; 
  -                        unset ($client) ; 
  -                        unset ($clientconf) ; 
  -                    }
  -                    break ; 
  -            }
  -        }
  -        unset ($value) ; 
  -        unset ($key) ; 
  -    }
  -    fclose ($pipes[1]) ; 
  -    proc_close ($proc) ; 
  +switch ($submit) {
  +    case "addloc": 
  +	if (empty ($newconf["newloc"]))
  +	    break ; 
  +	if (in_array ($newconf["newloc"], 
  +		      $config["client"][$client]["location"]))
  +	    break ;
  +	printf ("Trying to add %s to the locations of %s<BR>\n", 
  +		$newconf["newloc"], $client) ; 
  +	$config["client"][$client]["location"][] = $newconf["newloc"] ; 
  +	break ;
  +}
   ?>
   <FORM method=post>
   <INPUT type=hidden name=Passwd value="<?php echo $passwd ?>">
  @@ -234,7 +257,6 @@
   </TABLE>
   </FORM>
   <?php
  -}
   
   # Show posted values at the bottom (except the password)
   printf ("<pre>") ; 
  
  
  



More information about the slbackup-commit mailing list