Пример #1
0
function installSlicer($version, $installDir, $filename, $quiet, $disableTls, $cafile)
{
    $installPath = (is_dir($installDir) ? rtrim($installDir, '/') . '/' : '') . $filename;
    $installDir = realpath($installDir) ? realpath($installDir) : getcwd();
    $file = $installDir . DIRECTORY_SEPARATOR . $filename;
    if (is_readable($file)) {
        @unlink($file);
    }
    if (FALSE === $disableTls && empty($cafile) && !HttpClient::getSystemCaRootBundlePath()) {
        $errorHandler = new ErrorHandler();
        set_error_handler([$errorHandler, 'handleError']);
        $home = getenv('COMPOSER_HOME');
        if (!$home) {
            if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
                if (!getenv('APPDATA')) {
                    throw new RuntimeException('The APPDATA or SLICER_HOME environment variable must be set for composer to install correctly');
                }
                $home = strtr(getenv('APPDATA'), '\\', '/') . '/Slicer';
            } else {
                if (!getenv('HOME')) {
                    throw new RuntimeException('The HOME or SLICER_HOME environment variable must be set for composer to install correctly');
                }
                $home = rtrim(getenv('HOME'), '/') . '/.slicer';
            }
        }
        $target = $home . '/cacert.pem';
        if (!is_dir($home)) {
            @mkdir($home, 0777, TRUE);
        }
        $write = file_put_contents($target, HttpClient::getPackagedCaFile(), LOCK_EX);
        @chmod($target, 0644);
        restore_error_handler();
        if (!$write) {
            throw new RuntimeException('Unable to write bundled cacert.pem to: ' . $target);
        }
        $cafile = $target;
    }
    $httpClient = new HttpClient($disableTls, $cafile);
    $retries = 3;
    while ($retries--) {
        if (!$quiet) {
            out("Downloading...", 'info');
        }
        $uriScheme = FALSE === $disableTls ? 'https' : 'http';
        $urlPath = (FALSE !== $version ? "/download/{$version}" : '') . '/slicer.phar';
        $url = "{$uriScheme}://getslicer.com{$urlPath}";
        $errorHandler = new ErrorHandler();
        set_error_handler([$errorHandler, 'handleError']);
        $fh = fopen($file, 'w');
        if (!$fh) {
            out('Could not create file ' . $file . ': ' . $errorHandler->message, 'error');
        }
        if (!fwrite($fh, $httpClient->get($url))) {
            out('Download failed: ' . $errorHandler->message, 'error');
        }
        fclose($fh);
        restore_error_handler();
        if ($errorHandler->message) {
            continue;
        }
        try {
            // create a temp file ending in .phar since the Phar class only accepts that
            if ('.phar' !== substr($file, -5)) {
                copy($file, $file . '.tmp.phar');
                $pharFile = $file . '.tmp.phar';
            } else {
                $pharFile = $file;
            }
            if (!ini_get('phar.readonly')) {
                // test the phar validity
                $phar = new Phar($pharFile);
                // free the variable to unlock the file
                unset($phar);
            }
            // clean up temp file if needed
            if ($file !== $pharFile) {
                unlink($pharFile);
            }
            break;
        } catch (Exception $e) {
            if (!$e instanceof UnexpectedValueException && !$e instanceof PharException) {
                throw $e;
            }
            // clean up temp file if needed
            if ($file !== $pharFile) {
                unlink($pharFile);
            }
            unlink($file);
            if ($retries) {
                if (!$quiet) {
                    out('The download is corrupt, retrying...', 'error');
                }
            } else {
                out('The download is corrupt (' . $e->getMessage() . '), aborting.', 'error');
                exit(1);
            }
        }
    }
    if ($errorHandler->message) {
        out('The download failed repeatedly, aborting.', 'error');
        exit(1);
    }
    chmod($file, 0755);
    if (!$quiet) {
        out(PHP_EOL . "Slicer successfully installed to: " . $file, 'success', FALSE);
        out(PHP_EOL . "Use it: php {$installPath}", 'info');
    }
}
Пример #2
0
/**
 * installs composer to the current working directory
 */
function installComposer($version, $installDir, $filename, $quiet, $disableTls, $cafile, $channel)
{
    $installPath = (is_dir($installDir) ? rtrim($installDir, '/') . '/' : '') . $filename;
    $installDir = realpath($installDir) ? realpath($installDir) : getcwd();
    $file = $installDir . DIRECTORY_SEPARATOR . $filename;
    if (is_readable($file)) {
        @unlink($file);
    }
    $home = getHomeDir();
    file_put_contents($home . '/keys.dev.pub', <<<DEVPUBKEY
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAnBDHjZS6e0ZMoK3xTD7f
FNCzlXjX/Aie2dit8QXA03pSrOTbaMnxON3hUL47Lz3g1SC6YJEMVHr0zYq4elWi
i3ecFEgzLcj+pZM5X6qWu2Ozz4vWx3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iu+KoC2r3HDBrpDeBVdOWxDzT5R4iI0KoLzFh2pKqwbY+obNPS2bj+2dgJ
rV3V5Jjry42QOCBN3c88wU1PKftOLj2ECpewY6vnE478IipiEu7EAdK8Zwj2LmTr
RKQUSa9k7ggBkYZWAeO/2Ag0ey3g2bg7eqk+sHEq5ynIXd5lhv6tC5PBdHlWipDK
tl2IxiEnejnOmAzGVivE1YGduYBjN+mjxDVy8KGBrjnz1JPgAvgdwJ2dYw4Rsc/e
TzCFWGk/HM6a4f0IzBWbJ5ot0PIi4amk07IotBXDWwqDiQTwyuGCym5EqWQ2BD95
RGv89BPD+2DLnJysngsvVaUCAwEAAQ==
-----END PUBLIC KEY-----
TAGSPUBKEY
);
    if (false === $disableTls && empty($cafile) && !HttpClient::getSystemCaRootBundlePath()) {
        $errorHandler = new ErrorHandler();
        set_error_handler(array($errorHandler, 'handleError'));
        $target = $home . '/cacert.pem';
        $write = file_put_contents($target, HttpClient::getPackagedCaFile(), LOCK_EX);
        @chmod($target, 0644);
        restore_error_handler();
        if (!$write) {
            throw new RuntimeException('Unable to write bundled cacert.pem to: ' . $target);
        }
        $cafile = $target;
    }
    $httpClient = new HttpClient($disableTls, $cafile);
    $uriScheme = false === $disableTls ? 'https' : 'http';
    if (!$version) {
        $versions = json_decode($httpClient->get($uriScheme . '://getcomposer.org/versions'), true);
        foreach ($versions[$channel] as $candidate) {
            if ($candidate['min-php'] <= PHP_VERSION_ID) {
                $version = $candidate['version'];
                $downloadUrl = $candidate['path'];
                break;
            }
        }
        if (!$version) {
            throw new RuntimeException('There is no version of Composer available for your PHP version (' . PHP_VERSION . ')');
        }
    } else {
        $downloadUrl = "/download/{$version}/composer.phar";
    }
    $retries = 3;
    while ($retries--) {
        if (!$quiet) {
            out("Downloading {$version}...", 'info');
        }
        $url = "{$uriScheme}://getcomposer.org{$downloadUrl}";
        $errorHandler = new ErrorHandler();
        set_error_handler(array($errorHandler, 'handleError'));
        // download signature file
        if (false === $disableTls) {
            $signature = $httpClient->get($url . '.sig');
            if (!$signature) {
                out('Download failed: ' . $errorHandler->message, 'error');
            } else {
                $signature = json_decode($signature, true);
                $signature = base64_decode($signature['sha384']);
            }
        }
        $fh = fopen($file, 'w');
        if (!$fh) {
            out('Could not create file ' . $file . ': ' . $errorHandler->message, 'error');
        }
        if (!fwrite($fh, $httpClient->get($url))) {
            out('Download failed: ' . $errorHandler->message, 'error');
        }
        fclose($fh);
        restore_error_handler();
        if ($errorHandler->message) {
            continue;
        }
        try {
            // create a temp file ending in .phar since the Phar class only accepts that
            if ('.phar' !== substr($file, -5)) {
                copy($file, $file . '.tmp.phar');
                $pharFile = $file . '.tmp.phar';
            } else {
                $pharFile = $file;
            }
            // verify signature
            if (false === $disableTls) {
                $pubkeyid = openssl_pkey_get_public('file://' . $home . '/' . (preg_match('{^[0-9a-f]{40}$}', $version) ? 'keys.dev.pub' : 'keys.tags.pub'));
                $algo = defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'SHA384';
                if (!in_array('SHA384', openssl_get_md_methods())) {
                    out('SHA384 is not supported by your openssl extension, could not verify the phar file integrity', 'error');
                    exit(1);
                }
                $verified = 1 === openssl_verify(file_get_contents($file), $signature, $pubkeyid, $algo);
                openssl_free_key($pubkeyid);
                if (!$verified) {
                    out('Signature mismatch, could not verify the phar file integrity', 'error');
                    exit(1);
                }
            }
            // test the phar validity
            if (!ini_get('phar.readonly')) {
                $phar = new Phar($pharFile);
                // free the variable to unlock the file
                unset($phar);
            }
            // clean up temp file if needed
            if ($file !== $pharFile) {
                unlink($pharFile);
            }
            break;
        } catch (Exception $e) {
            if (!$e instanceof UnexpectedValueException && !$e instanceof PharException) {
                throw $e;
            }
            // clean up temp file if needed
            if ($file !== $pharFile) {
                unlink($pharFile);
            }
            unlink($file);
            if ($retries) {
                if (!$quiet) {
                    out('The download is corrupt, retrying...', 'error');
                }
            } else {
                out('The download is corrupt (' . $e->getMessage() . '), aborting.', 'error');
                exit(1);
            }
        }
    }
    if ($errorHandler->message) {
        out('The download failed repeatedly, aborting.', 'error');
        exit(1);
    }
    chmod($file, 0755);
    if (!$quiet) {
        out(PHP_EOL . "Composer successfully installed to: " . $file, 'success', false);
        out(PHP_EOL . "Use it: php {$installPath}", 'info');
    }
}