Friday, August 27, 2010

Open Source PHP deployment install script.

This script copies from current directory to the one below, for deployment.

This script copies from current directory to the one below, for deployment.





<?php

//exit;



$action = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];



echo "<strong>I am <u>{$_SERVER['SERVER_ADDR']}</u></strong><br>\n";

echo "<small>You are {$_SERVER['REMOTE_ADDR']}</small><br>\n<br>\n";



include("../modules/session.php");



if(md5('ljwn4flkn'.$_REQUEST['install_php_pass']) == '.. fill this in ..')

$_SESSION['loggedInForInstall'] = true;



if(!$_SESSION['loggedInForInstall']) {



echo '

<form method="POST" action="'.$action.'">

<input type="password" name="install_php_pass">

</form>

';

exit;

}



include("../modules/error.php");

$errorDebugMode = true;



include("../modules/database.php");

?>

<html>

<body>

<?php



if(!isset($_SESSION['completedSteps']) || $_REQUEST['reset_steps'])

$_SESSION['completedSteps'] = array();



if($_REQUEST['reset_step'])

$_SESSION['completedSteps'][intval($_REQUEST['reset_step'])] = FALSE;



if($_REQUEST['set_step'])

$_SESSION['completedSteps'][intval($_REQUEST['set_step'])] = TRUE;



$completedSteps =& $_SESSION['completedSteps'];



$step = $_POST['step'];



if(isset($_POST['repeat_step'])) {



$step = $_POST['repeat_step'];

$completedSteps[$step] = false;

}



if($step)

$step = intval($step);



function full_copy( $source, $target )

{

$ret = true;



if ( is_dir( $source ) )

{

if(!file_exists($target))

mkdir( $target );



$d = dir( $source );



while ( FALSE !== ( $entry = $d->read() ) )

{

if ( $entry == '.' || $entry == '..' || $entry == "install.php" )

{

continue;

}



$Entry = $source . '/' . $entry;

if ( is_dir( $Entry ) )

{

if(!full_copy( $Entry, $target . '/' . $entry ))

$ret = false;

continue;

}



$from_file = $Entry;

$to_file = $target . '/' . $entry;



$from_mtime = @filemtime($from_file);



$to_mtime = FALSE;



if(file_exists($to_file))

$to_mtime = @filemtime($to_file);



if(isset($_REQUEST['copyall']) ||

(FALSE === $to_mtime || intval($from_mtime) != intval($to_mtime))) {



$from_file = escapeshellarg ($from_file);

$to_file = escapeshellarg ($to_file);



if(isset($_REQUEST['fakecopy']))

echo "Out of date: $from_file $to_file\n";

else

system("cp -Rpv $from_file $to_file");

}

}



$d->close();

}else

{

if(!copy( $source, $target ))

$ret = false;

}



return $ret;

}



$folder = "..";



if($step == 1 && !$completedSteps[$step]) {



echo "Copying . to $folder, please wait...<br>";

echo '<br>';



ob_flush();



echo '<textarea rows="5" cols="85">';



$successful = full_copy(".", $folder);



echo '</textarea><br>';



if(!$successful)

echo '<strong style="background-color: #FFEEEE; color: red">

Copy operation failed. </strong>';

else {



echo '<strong style="background-color: #9999CC">

Copy operation sucessfull</strong>';



$completedSteps[$step] = true;

}

}



function outputStepButton($step)

{

if(!$GLOBALS['completedSteps'][$step])

echo '<button name="step" value="'.$step.'">Step '.$step.'</button>

<button name="set_step" value="'.$step.'">Skip</button>';

else

echo '<strong style="color: #9999CC">Step '.$step.' complete. </strong>

<button name="repeat_step" onclick="return confirm(\'Are you sure you would like to repeat step '.$step.'?\');" value="'.$step.'">Repeat Step '.$step.'</button>

<button name="reset_step" value="'.$step.'">Reset</button>';

}



?>

<form method="POST">

<div style="border: 1px solid black; width:100%">

Copy base code.

<input type="checkbox" name="copyall" <?php echo isset($_REQUEST['copyall']) ? 'checked="1"' :'' ?>"> skip mtime check

<br>

<input type="checkbox" name="fakecopy" <?php echo isset($_REQUEST['fakecopy']) ? 'checked="1"' :'' ?>"> do not copy -- output what would have been copied

<br>

<?php outputStepButton(1) ?>

</div>

</form>

</body>

</html>