<commit_before><?php
namespace Concrete\Controller;

use Concrete\Core\Cache\Cache;
use Concrete\Core\Config\Renderer;
use Core;
use Concrete\Core\Localization\Localization as Localization;
use Controller;
use Database as DB;
use Config;
use Exception;
use Hautelook\Phpass\PasswordHash;
use Loader;
use StartingPointPackage;
use View;

defined('C5_EXECUTE') or die("Access Denied.");

if (!ini_get('safe_mode')) {
    @set_time_limit(1000);
}

class Install extends Controller
{

    protected $fp;
    protected $fpu;

    public $helpers = array('form', 'html');

    public function getViewObject()
    {
        $v = new View('/frontend/install');
        $v->setViewTheme('concrete');
        return $v;
    }

    public function view()
    {
        $locales = $this->getLocales();
        $this->set('locales', $locales);
        $this->testAndRunInstall();
    }

    protected function getLocales()
    {
        return Localization::getAvailableInterfaceLanguageDescriptions();
    }

    protected function testAndRunInstall()
    {
        if (file_exists(DIR_CONFIG_SITE . '/site_install_user.php')) {
            require(DIR_CONFIG_SITE . '/site_install.php');
            @include(DIR_CONFIG_SITE . '/site_install_user.php');
            if (defined('SITE_INSTALL_LOCALE') && Localization::activeLocale() !== SITE_INSTALL_LOCALE) {
                Localization::changeLocale(SITE_INSTALL_LOCALE);
            }
            $e = Loader::helper('validation/error');
            $e = $this->validateDatabase($e);
            if ($e->has()) {
                $this->set('error', $e);
            } else {
                if (defined('INSTALL_STARTING_POINT') && INSTALL_STARTING_POINT) {
                    $spl = StartingPointPackage::getClass(INSTALL_STARTING_POINT);
                } else {
                    $spl = StartingPointPackage::getClass('standard');
                }
                $this->set('installPackage', $spl->getPackageHandle());
                $this->set('installRoutines', $spl->getInstallRoutines());
                $this->set(
                    'successMessage',
                    t(
                        'Congratulations. concrete5 has been installed. You have been logged in as <b>%s</b> with the password you chose. If you wish to change this password, you may do so from the users area of the dashboard.',
                        USER_SUPER,
                        $uPassword));
            }
        }
    }

    protected function validateDatabase($e)
    {
        if (!extension_loaded('pdo')) {
            $e->add($this->getDBErrorMsg());
        } else {

            // attempt to connect to the database
            if (defined('DB_SERVER')) {
                $db = DB::connect(
                    array(
                        'host'     => DB_SERVER,
                        'user'     => DB_USERNAME,
                        'password' => <PASSWORD>,
                        'database' => DB_DATABASE
                    ));
                $DB_SERVER = DB_SERVER;
                $DB_DATABASE = DB_DATABASE;
            } else {
                $db = DB::connect(
                    array(
                        'host'     => $_POST['DB_SERVER'],
                        'user'     => $_POST['DB_USERNAME'],
                        'password' => $_POST['DB_PASSWORD'],
                        'database' => $_POST['DB_DATABASE']
                    ));

                $DB_SERVER = $_POST['DB_SERVER'];
                $DB_DATABASE = $_POST['DB_DATABASE'];
            }

            if ($DB_SERVER && $DB_DATABASE) {
                if (!$db) {
                    $e->add(t('Unable to connect to database.'));
                } else {
                    $num = $db->GetCol("show tables");
                    if (count($num) > 0) {
                        $e->add(
                            t(
                                'There are already %s tables in this database. concrete5 must be installed in an empty database.',
                                count($num)));
                    }
                }
            }
        }
        return $e;
    }

    public function getDBErrorMsg()
    {
        return t('The PDO extension is not loaded.');
    }

    public function setup()
    {

    }

    public function select_language()
    {

    }

    /**
     * Testing
     */
    public function on_start()
    {
        if (isset($_POST['locale']) && $_POST['locale']) {
            $loc = Localization::changeLocale($_POST['locale']);
            $this->set('locale', $_POST['locale']);
        }
        Cache::disableAll();
        $this->setRequiredItems();
        $this->setOptionalItems();

        if (\Core::isInstalled()) {
            throw new Exception(t('concrete5 is already installed.'));
        }
        if (!isset($_COOKIE['CONCRETE5_INSTALL_TEST'])) {
            setcookie('CONCRETE5_INSTALL_TEST', '1', 0, DIR_REL . '/');
        }
    }

    private function setRequiredItems()
    {
        $this->set('imageTest', function_exists('imagecreatetruecolor'));
        $this->set('mysqlTest', extension_loaded('PDO'));
        $this->set('jsonTest', extension_loaded('json'));
        $this->set('xmlTest', function_exists('xml_parse') && function_exists('simplexml_load_file'));
        $this->set('fileWriteTest', $this->testFileWritePermissions());
        $phpVmin = '5.3.3';
        if (version_compare(PHP_VERSION, $phpVmin, '>=')) {
            $phpVtest = true;
        } else {
            $phpVtest = false;
        }
        $this->set('phpVmin', $phpVmin);
        $this->set('phpVtest', $phpVtest);
    }

    private function testFileWritePermissions()
    {<commit_msg>Check for PDO MySQL extention not just PDO<commit_after><?php
namespace Concrete\Controller;

use Concrete\Core\Cache\Cache;
use Concrete\Core\Config\Renderer;
use Core;
use Concrete\Core\Localization\Localization as Localization;
use Controller;
use Database as DB;
use Config;
use Exception;
use Hautelook\Phpass\PasswordHash;
use Loader;
use StartingPointPackage;
use View;

defined('C5_EXECUTE') or die("Access Denied.");

if (!ini_get('safe_mode')) {
    @set_time_limit(1000);
}

class Install extends Controller
{

    protected $fp;
    protected $fpu;

    public $helpers = array('form', 'html');

    public function getViewObject()
    {
        $v = new View('/frontend/install');
        $v->setViewTheme('concrete');
        return $v;
    }

    public function view()
    {
        $locales = $this->getLocales();
        $this->set('locales', $locales);
        $this->testAndRunInstall();
    }

    protected function getLocales()
    {
        return Localization::getAvailableInterfaceLanguageDescriptions();
    }

    protected function testAndRunInstall()
    {
        if (file_exists(DIR_CONFIG_SITE . '/site_install_user.php')) {
            require(DIR_CONFIG_SITE . '/site_install.php');
            @include(DIR_CONFIG_SITE . '/site_install_user.php');
            if (defined('SITE_INSTALL_LOCALE') && Localization::activeLocale() !== SITE_INSTALL_LOCALE) {
                Localization::changeLocale(SITE_INSTALL_LOCALE);
            }
            $e = Loader::helper('validation/error');
            $e = $this->validateDatabase($e);
            if ($e->has()) {
                $this->set('error', $e);
            } else {
                if (defined('INSTALL_STARTING_POINT') && INSTALL_STARTING_POINT) {
                    $spl = StartingPointPackage::getClass(INSTALL_STARTING_POINT);
                } else {
                    $spl = StartingPointPackage::getClass('standard');
                }
                $this->set('installPackage', $spl->getPackageHandle());
                $this->set('installRoutines', $spl->getInstallRoutines());
                $this->set(
                    'successMessage',
                    t(
                        'Congratulations. concrete5 has been installed. You have been logged in as <b>%s</b> with the password you chose. If you wish to change this password, you may do so from the users area of the dashboard.',
                        USER_SUPER,
                        $uPassword));
            }
        }
    }

    protected function validateDatabase($e)
    {
        if (!extension_loaded('pdo')) {
            $e->add($this->getDBErrorMsg());
        } else {

            // attempt to connect to the database
            if (defined('DB_SERVER')) {
                $db = DB::connect(
                    array(
                        'host'     => DB_SERVER,
                        'user'     => DB_USERNAME,
                        'password' => <PASSWORD>,
                        'database' => DB_DATABASE
                    ));
                $DB_SERVER = DB_SERVER;
                $DB_DATABASE = DB_DATABASE;
            } else {
                $db = DB::connect(
                    array(
                        'host'     => $_POST['DB_SERVER'],
                        'user'     => $_POST['DB_USERNAME'],
                        'password' => $_POST['<PASSWORD>'],
                        'database' => $_POST['DB_DATABASE']
                    ));

                $DB_SERVER = $_POST['DB_SERVER'];
                $DB_DATABASE = $_POST['DB_DATABASE'];
            }

            if ($DB_SERVER && $DB_DATABASE) {
                if (!$db) {
                    $e->add(t('Unable to connect to database.'));
                } else {
                    $num = $db->GetCol("show tables");
                    if (count($num) > 0) {
                        $e->add(
                            t(
                                'There are already %s tables in this database. concrete5 must be installed in an empty database.',
                                count($num)));
                    }
                }
            }
        }
        return $e;
    }

    public function getDBErrorMsg()
    {
        return t('The PDO extension is not loaded.');
    }

    public function setup()
    {

    }

    public function select_language()
    {

    }

    /**
     * Testing
     */
    public function on_start()
    {
        if (isset($_POST['locale']) && $_POST['locale']) {
            $loc = Localization::changeLocale($_POST['locale']);
            $this->set('locale', $_POST['locale']);
        }
        Cache::disableAll();
        $this->setRequiredItems();
        $this->setOptionalItems();

        if (\Core::isInstalled()) {
            throw new Exception(t('concrete5 is already installed.'));
        }
        if (!isset($_COOKIE['CONCRETE5_INSTALL_TEST'])) {
            setcookie('CONCRETE5_INSTALL_TEST', '1', 0, DIR_REL . '/');
        }
    }

    private function setRequiredItems()
    {
        $this->set('imageTest', function_exists('imagecreatetruecolor'));
        $this->set('mysqlTest', extension_loaded('pdo_mysql'));
        $this->set('jsonTest', extension_loaded('json'));
        $this->set('xmlTest', function_exists('xml_parse') && function_exists('simplexml_load_file'));
        $this->set('fileWriteTest', $this->testFileWritePermissions());
        $phpVmin = '5.3.3';
        if (version_compare(PHP_VERSION, $phpVmin, '>=')) {
            $phpVtest = true;
        } else {
            $phpVtest = false;
        }
        $this->set('phpVmin', $phpVmin);
        $this->set('phpVtest', $phpVtest);
    }

    private function testFileWritePermissions()
    {
{'fraction_non_alphanumeric': 0.13428921140059452, 'fraction_numerical': 0.002797691904179052, 'mean_word_length': 1.705534531693472, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4722846', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3082, 'n_words': 621}