<commit_before><?php
/**
 * This file is part of the teamneusta/hosts project.
 * Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
 *
 */

/**
 * originally authored by <NAME><<EMAIL>>
 */
namespace

{
    use Herrera\Version\Comparator;
    use Herrera\Version\Dumper;
    use Herrera\Version\Parser;

    $n = PHP_EOL;

    set_error_handler(
        function ($code, $message, $file, $line) use ($n) {
            if ($code & error_reporting()) {
                echo "$n{$n}Error: $message$n$n";
                exit(1);
            }
        }
    );

    echo "Host Installer$n";
    echo "=============$n$n";

    echo "Environment Check$n";
    echo "-----------------$n$n";

    echo "\"-\" indicates success.$n";
    echo "\"*\" indicates error.$n$n";

    // check version
    check(
        'You have a supported version of PHP (>= 7.0.0).',
        'You need PHP 7.0.0 or greater.',
        function () {
            return version_compare(PHP_VERSION, '7.0.0', '>=');
        }
    );

    // check phar extension
    check(
        'You have the "phar" extension installed.',
        'You need to have the "phar" extension installed.',
        function () {
            return extension_loaded('phar');
        }
    );

    // check phar extension version
    check(
        'You have a supported version of the "phar" extension.',
        'You need a newer version of the "phar" extension (>=2.0).',
        function () {
            $phar = new ReflectionExtension('phar');

            return version_compare($phar->getVersion(), '2.0', '>=');
        }
    );

    // check openssl extension
    check(
        'You have the "openssl" extension installed.',
        'Notice: The "openssl" extension will be needed to sign with private keys.',
        function () {
            return extension_loaded('openssl');
        },
        false
    );

    // check phar readonly setting
    check(
        'The "phar.readonly" setting is off.',
        'Notice: The "phar.readonly" setting needs to be off to create Phars.',
        function () {
            return (false == ini_get('phar.readonly'));
        },
        false
    );

    // check detect unicode setting
    check(
        'The "detect_unicode" setting is off.',
        'The "detect_unicode" setting needs to be off.',
        function () {
            return (false == ini_get('detect_unicode'));
        }
    );

    // check suhosin setting
    if (extension_loaded('suhosin')) {
        check(
            'The "phar" stream wrapper is allowed by suhosin.',
            'The "phar" stream wrapper is blocked by suhosin.',
            function () {
                $white = ini_get('suhosin.executor.include.whitelist');
                $black = ini_get('suhosin.executor.include.blacklist');

                if ((false === stripos($white, 'phar'))
                    || (false !== stripos($black, 'phar'))) {
                    return false;
                }

                return true;
            }
        );
    }

    // check allow url open setting
    check(
        'The "allow_url_fopen" setting is on.',
        'The "allow_url_fopen" setting needs to be on.',
        function () {
            return (true == ini_get('allow_url_fopen'));
        }
    );

    echo "{$n}Everything seems good!$n$n";

    // Retrieve manifest
    echo " - Downloading manifest...$n";

    $manifest = file_get_contents('https://tomtone.github.io/host/manifest.json');

    echo " - Reading manifest...$n";

    $manifest = json_decode($manifest);
    $current = null;

    foreach ($manifest as $item) {
        $item->version = Parser::toVersion($item->version);

        if ($current
            && (Comparator::isGreaterThan($item->version, $current->version))) {
            $current = $item;
        }
    }

    if (!$item) {
        echo " x No application download was found.$n";
    }

    echo " - Downloading Host v", Dumper::toString($item->version), "...$n";

    file_put_contents($item->name, file_get_contents($item->url));

    echo " - Checking file checksum...$n";

    if ($item->sha1 !== sha1_file($item->name)) {
        unlink($item->name);

        echo " x The download was corrupted.$n";
    }

    echo " - Checking if valid Phar...$n";

    try {
        new Phar($item->name);
    } catch (Exception $e) {
        echo " x The Phar is not valid.\n\n";

        throw $e;
    }

    echo " - Making Host executable...$n";

    @chmod($item->name, 0755);

    echo "{$n}Host installed!$n";

    /**
     * Checks a condition, outputs a message, and exits if failed.
     *
     * @param string   $success   The success message.
     * @param string   $failure   The failure message.
     * @param callable $condition The condition to check.
     * @param boolean  $exit      Exit on failure?
     */
    function check($success, $failure, $condition, $exit = true)
    {
        global $n;

        if ($condition()) {
            echo ' - ', $success, $n;
        } else {
            echo ' * ', $failure, $n;

            if ($exit) {
                exit(1);
            }
        }
    }
}

namespace Herrera\Version\Exception

{
    use Exception;<commit_msg>Code-Style fixes<commit_after><?php
/**
 * This file is part of the teamneusta/hosts project.
 * Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
 *
 */

/**
 * originally authored by <NAME><<EMAIL>>
 */
namespace

{
    use Herrera\Version\Comparator;
    use Herrera\Version\Dumper;
    use Herrera\Version\Parser;

    $n = PHP_EOL;

    set_error_handler(
        function ($code, $message) use ($n) {
            if ($code & error_reporting()) {
                echo "$n{$n}Error: $message$n$n";
                exit(1);
            }
        }
    );

    echo "Host Installer$n";
    echo "=============$n$n";

    echo "Environment Check$n";
    echo "-----------------$n$n";

    echo "\"-\" indicates success.$n";
    echo "\"*\" indicates error.$n$n";

    // check version
    check(
        'You have a supported version of PHP (>= 7.0.0).',
        'You need PHP 7.0.0 or greater.',
        function () {
            return version_compare(PHP_VERSION, '7.0.0', '>=');
        }
    );

    // check phar extension
    check(
        'You have the "phar" extension installed.',
        'You need to have the "phar" extension installed.',
        function () {
            return extension_loaded('phar');
        }
    );

    // check phar extension version
    check(
        'You have a supported version of the "phar" extension.',
        'You need a newer version of the "phar" extension (>=2.0).',
        function () {
            $phar = new ReflectionExtension('phar');

            return version_compare($phar->getVersion(), '2.0', '>=');
        }
    );

    // check openssl extension
    check(
        'You have the "openssl" extension installed.',
        'Notice: The "openssl" extension will be needed to sign with private keys.',
        function () {
            return extension_loaded('openssl');
        },
        false
    );

    // check phar readonly setting
    check(
        'The "phar.readonly" setting is off.',
        'Notice: The "phar.readonly" setting needs to be off to create Phars.',
        function () {
            return (ini_get_bool('phar.readonly') === false);
        },
        false
    );

    // check detect unicode setting
    check(
        'The "detect_unicode" setting is off.',
        'The "detect_unicode" setting needs to be off.',
        function () {
            return (ini_get_bool('detect_unicode') === false);
        }
    );

    // check suhosin setting
    if (extension_loaded('suhosin')) {
        check(
            'The "phar" stream wrapper is allowed by suhosin.',
            'The "phar" stream wrapper is blocked by suhosin.',
            function () {
                $white = ini_get('suhosin.executor.include.whitelist');
                $black = ini_get('suhosin.executor.include.blacklist');

                if ((false === stripos($white, 'phar'))
                    || (false !== stripos($black, 'phar'))) {
                    return false;
                }

                return true;
            }
        );
    }

    // check allow url open setting
    check(
        'The "allow_url_fopen" setting is on.',
        'The "allow_url_fopen" setting needs to be on.',
        function () {
            return ini_get_bool('allow_url_fopen');
        }
    );

    echo "{$n}Everything seems good!$n$n";

    // Retrieve manifest
    echo " - Downloading manifest...$n";

    $manifest = file_get_contents('https://tomtone.github.io/host/manifest.json');

    echo " - Reading manifest...$n";

    $manifest = json_decode($manifest);
    $current = null;

    foreach ($manifest as $item) {
        $item->version = Parser::toVersion($item->version);

        if ($current
            && (Comparator::isGreaterThan($item->version, $current->version))) {
            $current = $item;
        }
    }

    if (!$item) {
        echo " x No application download was found.$n";
    }

    echo " - Downloading Host v", Dumper::toString($item->version), "...$n";

    file_put_contents($item->name, file_get_contents($item->url));

    echo " - Checking file checksum...$n";

    if ($item->sha1 !== sha1_file($item->name)) {
        unlink($item->name);

        echo " x The download was corrupted.$n";
    }

    echo " - Checking if valid Phar...$n";

    try {
        new Phar($item->name);
    } catch (Exception $e) {
        echo " x The Phar is not valid.\n\n";

        throw $e;
    }

    echo " - Making Host executable...$n";

    if (@chmod($item->name, 0755) === false) {
        throw new \RuntimeException('Permissions on '.$item->name.' could not be changed.');
    }

    echo "{$n}Host installed!$n";

    /**
     * Checks a condition, outputs a message, and exits if failed.
     *
     * @param string   $success   The success message.
     * @param string   $failure   The failure message.
     * @param callable $condition The condition to check.
     * @param boolean  $exit      Exit on failure?
     */
    function check($success, $failure, $condition, $exit = true)
    {
        global $n;

        if ($condition()) {
            echo ' - ', $success, $n;
        } else {
            echo ' * ', $failure, $n;

            if ($exit) {
                exit(1);
            }
        }
    }

    /**
     * @param string $varname
     *
     * @return bool
     */
    function ini_get_bool($varname) {
        return \filter_var(\ini_get($varname), FILTER_VALIDATE_BOOLEAN);
    }
}

namespace Herrera\Version\Exception

{
    use Exception;
{'fraction_non_alphanumeric': 0.1444805194805195, 'fraction_numerical': 0.004509379509379509, 'mean_word_length': 1.8937891440501045, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 44, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1491131', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3170, 'n_words': 898}