Skip to content

Instantly share code, notes, and snippets.

@ahsankhatri
Created October 2, 2016 19:35
Show Gist options
  • Select an option

  • Save ahsankhatri/556064662d368d63ad51483cecc7eb27 to your computer and use it in GitHub Desktop.

Select an option

Save ahsankhatri/556064662d368d63ad51483cecc7eb27 to your computer and use it in GitHub Desktop.

Revisions

  1. ahsankhatri created this gist Oct 2, 2016.
    60 changes: 60 additions & 0 deletions webComposer.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?php
    // This is just a sample code, do not use it on production as this is insecure
    // For security, you may use .htaccess IP Access or HTTP Basic Aauthentication

    require 'vendor/autoload.php';

    $allowedCommands = [
    'update',
    'install',
    'dump-autoload',
    'dump-autoload -o',
    ];

    showOptions($allowedCommands);

    if ( !isset($_GET['cmd']) ) {
    exit('<br />');
    }

    $cmdRaw = base64_decode($_GET['cmd']);

    if ( !in_array($cmdRaw, $allowedCommands) ) {
    exit;
    }

    $cmdRawArray = explode(' ', $cmdRaw);
    $inputArray = ['command' => array_shift($cmdRawArray) ] + $cmdRawArray;

    ini_set('memory_limit', '1G');
    set_time_limit(300); // 5 minutes execution

    use Composer\Console\Application;
    use Symfony\Component\Console\Input\ArrayInput;
    use Symfony\Component\Console\Output\BufferedOutput as Output;
    use Symfony\Component\Console\Output\OutputInterface;

    $isDebug = isset($_GET['debug']) ? true : false;

    // set COMPOSER_HOME environment
    putenv('COMPOSER_HOME=' . __DIR__ . '/vendor/bin/composer');

    $output = new Output(
    $isDebug ? OutputInterface::VERBOSITY_DEBUG : OutputInterface::VERBOSITY_NORMAL
    );

    $input = new ArrayInput( $inputArray );
    $application = new Application();
    $application->setAutoExit(false);
    $application->run($input, $output);

    echo '<pre>' . $output->fetch() . '</pre>';

    function showOptions($allowedCommands) {
    $buttons = [];
    foreach ($allowedCommands as $cmd) {
    $buttons[] = '<button type="button" onclick="window.location=\'' . $_SERVER['SCRIPT_NAME'] . '?cmd=' . base64_encode($cmd) . '\'">composer ' . $cmd . '</button>';
    }

    echo implode('&nbsp;', $buttons) . '<hr>';
    }