2016-09-21 12:10:37 +01:00
< ? php
namespace BtwInstaller ;
class BlueTwilightInstaller
{
private $baseDirectory ;
2017-09-05 14:59:18 +01:00
private $composerSignature ;
2016-09-21 12:10:37 +01:00
public function __construct ()
{
$this -> baseDirectory = dirname ( __DIR__ );
2017-09-05 14:59:18 +01:00
chdir ( $this -> baseDirectory );
2017-09-06 10:33:22 +01:00
putenv ( 'HOME=' . $this -> baseDirectory );
2016-09-21 12:10:37 +01:00
// Display errors so installer never gets a WSOD!
ini_set ( 'display_errors' , true );
}
public function run ()
{
2017-09-05 14:59:18 +01:00
if ( strtoupper ( $_SERVER [ 'REQUEST_METHOD' ]) == 'POST' )
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
// Handle post
$this -> runInstall ();
exit ();
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
?>
2017-09-06 10:33:22 +01:00
< html >
< head >
< title > Blue Twilight Setup </ title >
</ head >
< body >
< h1 > Blue Twilight Setup </ h1 >
< p > We need to download a few things - namely < a href = " http://getcomposer.org " target = " _blank " > Composer </ a > and related packages - before we can kick off the Blue Twilight installer .</ p >
< p > We can do this for you - simply click the button below .</ p >
2017-09-16 12:54:43 +01:00
< p style = " font-weight: bold; color: #ff0000; " > This can take a few minutes so please be patient , and only click the button once !</ p >
2017-09-06 10:33:22 +01:00
< form method = " post " >
2017-09-16 12:54:43 +01:00
< button type = " submit " > Install Composer and dependencies for me </ button >
2017-09-06 10:33:22 +01:00
</ form >
< hr />
< h2 > Got Composer ? </ h2 >
< p > If you already have Composer installed , however , you may want to use that instead . Just run the below commands on your server , changing the path to Composer as appropriate :</ p >
< p >< em > Please note : & quot ; composer . phar & quot ; may actually be & quot ; composer & quot ; on your system .</ em ></ p >
< pre > cd < ? php echo $this -> baseDirectory ; ?> <br/>/path/to/composer.phar install</pre>
</ body >
</ html >
2017-09-05 14:59:18 +01:00
< ? php
}
2016-09-21 12:10:37 +01:00
2017-09-05 14:59:18 +01:00
private function runInstall ()
{
?>
< h1 > Installing Blue Twilight Setup Files </ h1 >
< ul >
< ? php
$steps = [
2017-09-06 10:33:22 +01:00
[ 'Checking PHP modules' , 'checkPhpModules' ],
2017-09-05 14:59:18 +01:00
[ 'Fetching Composer signature' , 'fetchComposerSignature' ],
[ 'Installing Composer' , 'installComposer' ],
2017-09-06 10:33:22 +01:00
[ 'Installing dependencies using Composer' , 'runComposer' ],
[ 'Generating application key' , 'generateAppKey' ]
2017-09-05 14:59:18 +01:00
];
2017-09-06 10:33:22 +01:00
$successful = true ;
2017-09-05 14:59:18 +01:00
foreach ( $steps as $step )
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
echo sprintf ( " <li>%s...</li>%s " , $step [ 0 ], PHP_EOL );
$result = call_user_func ([ $this , $step [ 1 ]]);
if ( ! $result )
{
2017-09-06 10:33:22 +01:00
$successful = false ;
2017-09-05 14:59:18 +01:00
break ;
}
2016-09-21 12:10:37 +01:00
}
2017-09-06 10:33:22 +01:00
if ( $successful )
{
header ( 'Location: install/check' );
exit ();
}
2017-09-05 14:59:18 +01:00
?>
</ ul >
< ? php
}
2017-09-06 10:33:22 +01:00
private function checkPhpModules ()
{
$requiredModules = [
'simplexml' ,
'curl' ,
'mbstring' ,
'dom'
];
$invalidModules = [];
foreach ( $requiredModules as $module )
{
if ( ! extension_loaded ( $module ))
{
$invalidModules [] = $module ;
}
}
if ( count ( $invalidModules ) > 0 )
{
$this -> echoError ( sprintf ( 'The following PHP modules are missing and need to be installed to continue: %s' , join ( ', ' , $invalidModules )));
return false ;
}
return true ;
}
2017-09-05 14:59:18 +01:00
private function echoError ( $message )
{
echo sprintf ( " <span style= \" color: #ff0000; \" >%s.</span>%s " , $message , PHP_EOL );
}
private function echoOK ( $message = '' )
{
echo " <span style= \" color: #008800; \" >OK " ;
if ( strlen ( $message ) > 0 )
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
echo sprintf ( '... %s' , $message );
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
echo '</span>' . PHP_EOL ;
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
private function fetchComposerSignature ()
2016-09-21 12:10:37 +01:00
{
2017-09-30 08:14:21 +01:00
if ( ! boolval ( ini_get ( 'allow_url_fopen' )))
{
$this -> echoError ( 'allow_url_fopen is disabled so we cannot use Composer' );
echo '<br/>' ;
$this -> echoError ( 'You will need to install the vendor libraries manually - <a href="https://github.com/pandy06269/blue-twilight/wiki/Install-Vendor-libraries-manually" target="_blank">see this page for more details</a>' );
return false ;
}
2017-09-05 14:59:18 +01:00
$signatureUrl = 'https://composer.github.io/installer.sig' ;
$this -> composerSignature = trim ( file_get_contents ( $signatureUrl ));
if ( strlen ( $this -> composerSignature ) == 0 )
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
$this -> echoError ( sprintf ( " Failed downloading the Composer signature from %s " , $signatureUrl ));
return false ;
2016-09-21 12:10:37 +01:00
}
else
{
2017-09-05 14:59:18 +01:00
$this -> echoOK ( $this -> composerSignature );
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
return true ;
2016-09-21 12:10:37 +01:00
}
2017-09-06 10:33:22 +01:00
private function generateAppKey ()
{
if ( ! file_exists ( '.env' ) && file_exists ( '.env.example' ))
{
copy ( '.env.example' , '.env' );
}
ob_start ();
system ( 'touch .env' , $rc );
$result = ob_get_clean ();
echo nl2br ( $result );
ob_start ();
system ( 'php artisan key:generate' , $rc );
$result = ob_get_clean ();
echo nl2br ( $result );
if ( $rc != 0 )
{
$this -> echoError ( 'Failed to generate application key' );
return false ;
}
$this -> echoOK ();
return true ;
}
2017-09-05 14:59:18 +01:00
private function installComposer ()
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
$rc = - 1 ;
2017-09-06 10:33:22 +01:00
ob_start ();
2017-09-05 14:59:18 +01:00
system ( 'php -r "copy(\'https://getcomposer.org/installer\', \'composer-setup.php\');"' , $rc );
2017-09-06 10:33:22 +01:00
$result = ob_get_clean ();
echo nl2br ( $result );
2017-09-05 14:59:18 +01:00
if ( $rc != 0 )
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
$this -> echoError ( 'Failed to fetch Composer' );
return false ;
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
ob_start ();
system ( sprintf ( 'php -r "if (hash_file(\'SHA384\', \'composer-setup.php\') === \'%s\') { echo \'Installer verified\'; } else { echo \'Installer corrupt\'; unlink(\'composer-setup.php\'); } echo PHP_EOL;"' , $this -> composerSignature ), $rc );
$result = ob_get_clean ();
echo nl2br ( $result );
if ( $rc != 0 )
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
$this -> echoError ( 'Composer verification failed' );
return false ;
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
ob_start ();
system ( 'php composer-setup.php' , $rc );
$result = ob_get_clean ();
echo nl2br ( $result );
if ( $rc != 0 )
{
$this -> echoError ( 'Failed to install Composer' );
return false ;
}
2016-09-21 12:10:37 +01:00
2017-09-05 14:59:18 +01:00
ob_start ();
system ( 'php -r "unlink(\'composer-setup.php\');"' , $rc );
$result = ob_get_clean ();
echo nl2br ( $result );
if ( $rc != 0 )
{
$this -> echoError ( 'Failed to remove Composer setup file' );
return false ;
}
2016-09-21 12:10:37 +01:00
2017-09-05 14:59:18 +01:00
$this -> echoOK ();
return true ;
2016-09-21 12:10:37 +01:00
}
2017-09-05 14:59:18 +01:00
private function runComposer ()
2016-09-21 12:10:37 +01:00
{
2017-09-05 14:59:18 +01:00
ob_start ();
system ( 'php composer.phar install' , $rc );
$result = ob_get_clean ();
echo nl2br ( $result );
if ( $rc != 0 )
{
$this -> echoError ( 'Installing Composer packages failed' );
return false ;
}
$this -> echoOK ();
return true ;
2016-09-21 12:10:37 +01:00
}
}
$installer = new BlueTwilightInstaller ();
$installer -> run ();