/**
  * Denotes a check message that is a failure
  *
  * @param string $key The check being performed
  * @param string $title The failure message title.
  * @param string $message A detailed failure message.
  * @return boolean
  */
 protected function fail($key, $title = '', $message = '')
 {
     if (empty($title)) {
         $title = $this->install->lexicon('failed');
     }
     $msg = '<span class="notok">' . $title . '</span></p>';
     if (!empty($message)) {
         $msg .= '<p><strong>' . $message . '</strong></p>';
     }
     if (!isset($this->results[$key])) {
         $this->results[$key] = array();
     }
     if (!isset($this->results[$key]['msg'])) {
         $this->results[$key]['msg'] = '';
     }
     $this->results[$key]['msg'] .= $msg;
     $this->results[$key]['class'] = 'testFailed';
     if (!isset($this->results['fail'][$key])) {
         $this->results['fail'][$key] = array();
     }
     if (!isset($this->results['fail'][$key]['msg'])) {
         $this->results['fail'][$key]['msg'] = '';
     }
     $this->results['fail'][$key]['msg'] .= $msg;
     $this->results['fail'][$key]['class'] = 'testFailed';
     $this->results['fail'][$key]['title'] = $title;
     $this->results['fail'][$key]['message'] = $message;
     $this->success = false;
     return true;
 }
 /**
  * Writes the config file.
  *
  * @return boolean Returns true if successful; false otherwise.
  */
 public function writeConfig()
 {
     $written = false;
     $configTpl = MODX_CORE_PATH . 'docs/config.inc.tpl';
     $configFile = MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
     /**
      * Sanitize MySQL Password before writing to config, escaping '
      * I'm sure there's a better way to do this, but this works for now.
      * Otherwise, we risk fatal PHP errors if the entered Password
      * contains any single quotes as they would escape the string.
      * See GitHub issue 12502 for more information. https://github.com/modxcms/revolution/issues/12502
      */
     $this->install->settings->settings['database_password'] = addslashes($this->install->settings->settings['database_password']);
     $settings = $this->install->settings->fetch();
     $settings['last_install_time'] = time();
     $settings['site_id'] = uniqid('modx', true);
     /* make UUID if not set */
     if (empty($settings['uuid'])) {
         $settings['uuid'] = $this->install->generateUUID();
     }
     if (file_exists($configTpl)) {
         if ($tplHandle = @fopen($configTpl, 'rb')) {
             $content = @fread($tplHandle, filesize($configTpl));
             @fclose($tplHandle);
             if ($content) {
                 $replace = array();
                 while (list($key, $value) = each($settings)) {
                     if (is_scalar($value)) {
                         $replace['{' . $key . '}'] = "{$value}";
                     } elseif (is_array($value)) {
                         $replace['{' . $key . '}'] = var_export($value, true);
                     }
                 }
                 $content = str_replace(array_keys($replace), array_values($replace), $content);
                 if ($configHandle = @fopen($configFile, 'wb')) {
                     $written = @fwrite($configHandle, $content);
                     @fclose($configHandle);
                 }
             }
         }
     }
     $perms = $this->install->settings->get('new_file_permissions', sprintf("%04o", 0666 & 0666 - umask()));
     if (is_string($perms)) {
         $perms = octdec($perms);
     }
     $chmodSuccess = @chmod($configFile, $perms);
     if ($written) {
         $this->addResult(modInstallRunner::RESULT_SUCCESS, '<p class="ok">' . $this->install->lexicon('config_file_written') . '</p>');
     } else {
         $this->addResult(modInstallRunner::RESULT_FAILURE, '<p class="notok">' . $this->install->lexicon('config_file_err_w') . '</p>');
     }
     if ($chmodSuccess) {
         $this->addResult(modInstallRunner::RESULT_SUCCESS, '<p class="ok">' . $this->install->lexicon('config_file_perms_set') . '</p>');
     } else {
         $this->addResult(modInstallRunner::RESULT_WARNING, '<p>' . $this->install->lexicon('config_file_perms_notset') . '</p>');
     }
     return $written;
 }
 /**
  * Writes the config file.
  *
  * @return boolean Returns true if successful; false otherwise.
  */
 public function writeConfig()
 {
     $written = false;
     $configTpl = MODX_CORE_PATH . 'docs/config.inc.tpl';
     $configFile = MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
     $settings = $this->install->settings->fetch();
     $settings['last_install_time'] = time();
     $settings['site_id'] = uniqid('modx', true);
     /* make UUID if not set */
     if (empty($settings['uuid'])) {
         $settings['uuid'] = $this->install->generateUUID();
     }
     if (file_exists($configTpl)) {
         if ($tplHandle = @fopen($configTpl, 'rb')) {
             $content = @fread($tplHandle, filesize($configTpl));
             @fclose($tplHandle);
             if ($content) {
                 $replace = array();
                 while (list($key, $value) = each($settings)) {
                     if (is_scalar($value)) {
                         $replace['{' . $key . '}'] = "{$value}";
                     } elseif (is_array($value)) {
                         $replace['{' . $key . '}'] = var_export($value, true);
                     }
                 }
                 $content = str_replace(array_keys($replace), array_values($replace), $content);
                 if ($configHandle = @fopen($configFile, 'wb')) {
                     $written = @fwrite($configHandle, $content);
                     @fclose($configHandle);
                 }
             }
         }
     }
     $perms = $this->install->settings->get('new_file_permissions', sprintf("%04o", 0666 & 0666 - umask()));
     if (is_string($perms)) {
         $perms = octdec($perms);
     }
     $chmodSuccess = @chmod($configFile, $perms);
     if ($written) {
         $this->addResult(modInstallRunner::RESULT_SUCCESS, '<p class="ok">' . $this->install->lexicon('config_file_written') . '</p>');
     } else {
         $this->addResult(modInstallRunner::RESULT_FAILURE, '<p class="notok">' . $this->install->lexicon('config_file_err_w') . '</p>');
     }
     if ($chmodSuccess) {
         $this->addResult(modInstallRunner::RESULT_SUCCESS, '<p class="ok">' . $this->install->lexicon('config_file_perms_set') . '</p>');
     } else {
         $this->addResult(modInstallRunner::RESULT_WARNING, '<p>' . $this->install->lexicon('config_file_perms_notset') . '</p>');
     }
     return $written;
 }
 /**
  * Check database settings
  * @return void
  */
 public function checkDatabase()
 {
     $mode = $this->settings->get('installmode');
     /* get an instance of xPDO using the install settings */
     $xpdo = $this->install->getConnection($mode);
     if (!is_object($xpdo) || !$xpdo instanceof xPDO) {
         $this->end($this->install->lexicon('xpdo_err_ins'));
     }
     /* try to get a connection to the actual database */
     $dbExists = $xpdo->connect();
     if (!$dbExists) {
         if ($mode == modInstall::MODE_NEW && $xpdo->getManager()) {
             /* otherwise try to create the database */
             $dbExists = $xpdo->manager->createSourceContainer(array('dbname' => $this->settings->get('dbase'), 'host' => $this->settings->get('database_server')), $this->settings->get('database_user'), $this->settings->get('database_password'), array('charset' => $this->settings->get('database_connection_charset'), 'collation' => $this->settings->get('database_collation')));
             if (!$dbExists) {
                 $this->end($this->install->lexicon('db_err_create_database'));
             } else {
                 $xpdo = $this->install->getConnection($mode);
                 if (!is_object($xpdo) || !$xpdo instanceof xPDO) {
                     $this->end($this->install->lexicon('xpdo_err_ins'));
                 }
             }
         } elseif ($mode == modInstall::MODE_NEW) {
             $this->end($this->install->lexicon('db_err_connect_server'));
         }
     }
     if (!$xpdo->connect()) {
         $this->end($this->install->lexicon('db_err_connect'));
     }
     /* test table prefix */
     if ($mode == modInstall::MODE_NEW || $mode == modInstall::MODE_UPGRADE_REVO_ADVANCED) {
         $count = null;
         $database = $this->settings->get('dbase');
         $prefix = $this->settings->get('table_prefix');
         $stmt = $xpdo->query($this->install->driver->testTablePrefix($database, $prefix));
         if ($stmt) {
             $row = $stmt->fetch(PDO::FETCH_ASSOC);
             if ($row) {
                 $count = (int) $row['ct'];
             }
             $stmt->closeCursor();
         }
         if ($mode == modInstall::MODE_NEW && $count !== null) {
             $this->end($this->install->lexicon('test_table_prefix_inuse'));
         } elseif ($mode == modInstall::MODE_UPGRADE_REVO_ADVANCED && $count === null) {
             $this->end($this->install->lexicon('test_table_prefix_nf'));
         }
     }
 }