diff --git a/index.php b/index.php index 626fda5..fab99db 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,4 @@ listen(); + // delete /_install_tmp using composer functionality + if (is_dir('_install_tmp') && file_exists('_install_tmp/composer/vendor/autoload.php')) { + if (array_key_exists('install-done', $_GET)) { + echo 1; + exit; + } + // include the extracted composer libraries + require_once '_install_tmp/composer/vendor/autoload.php'; + + $fs = new Composer\Util\Filesystem(); + $fs->removeDirectory('_install_tmp'); + } + + $server = new Flarum\Forum\Server(__DIR__); + + $server->listen(); + +} elseif ((ini_get('allow_url_fopen') && !ini_get('phar.readonly')) || file_exists('scripts/composer.phar')) { + if (array_key_exists('install-done', $_GET)) { + echo 0; + exit; + } + // prevent time out of page, drawback of running in browser + @set_time_limit(0); + // prevent extracting composer anew if already exists + if (!is_dir('_install_tmp/composer')) { + // attempt to download the latest composer file + if (!file_exists('scripts/composer.phar') && ini_get('allow_url_fopen')) { + file_put_contents('https://getcomposer.org/installer', 'scripts/composer.phar'); + } + // use Phar to extract the composer package + $composer = new Phar('scripts/composer.phar'); + // create a temporary directory for saving the package + mkdir('_install_tmp'); + // extract composer + $composer->extractTo('_install_tmp/composer'); + } + // if extraction succeeded, let's run the update command. + if (is_dir('_install_tmp/composer') && file_exists('_install_tmp/composer/vendor/autoload.php')) { + // force memory to at least 1GB (default for composer) otherwise composer will run out of memory + if (function_exists('ini_set')) { + ini_set('memory_limit', '1G'); + } + + // show an installation html + include "storage/views/composer-installation.html"; + // force the template to the user + ob_flush(); + // sets a home directory for storing information + putenv('COMPOSER_HOME=' . getcwd() . '/_install_tmp/home'); + // prevents any interaction composer might require + putenv('COMPOSER_NO_INTERACTION=true'); + + require_once '_install_tmp/composer/vendor/autoload.php'; + + // run the composer installation command + $application = new Composer\Console\Application(); + + // disable auto exit + $application->setAutoExit(false); + + // first set the github token to prevent installation errors + $input = new \Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'config', + 'github-oauth.github.com' => 'ec785da935d5535e151f7b3386190265f00e8fe2' + ]); + $application->run($input); + + // set the input for the composer install command + $input = new Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'install', + '--no-dev' => true, + '--prefer-dist' => true, + '--optimize-autoloader' => true, + '-q' => true + ]); + $application->run($input); + + // application installation is now done, redirect to Flarum installer + // we cannot send a header or redirect because we're already flushed the buffer + } +} else { + // todo provide proper error message + throw new Exception('This method of installation is currently unsupported.'); +} \ No newline at end of file