[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/phpbb/install/module/obtain_data/task/ -> obtain_database_data.php (source)

   1  <?php
   2  /**
   3   *
   4   * This file is part of the phpBB Forum Software package.
   5   *
   6   * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7   * @license GNU General Public License, version 2 (GPL-2.0)
   8   *
   9   * For full copyright and license information, please see
  10   * the docs/CREDITS.txt file.
  11   *
  12   */
  13  
  14  namespace phpbb\install\module\obtain_data\task;
  15  
  16  use phpbb\install\exception\user_interaction_required_exception;
  17  
  18  /**
  19   * This class requests and validates database information from the user
  20   */
  21  class obtain_database_data extends \phpbb\install\task_base implements \phpbb\install\task_interface
  22  {
  23      /**
  24       * @var \phpbb\install\helper\database
  25       */
  26      protected $database_helper;
  27  
  28      /**
  29       * @var \phpbb\install\helper\config
  30       */
  31      protected $install_config;
  32  
  33      /**
  34       * @var \phpbb\install\helper\iohandler\iohandler_interface
  35       */
  36      protected $io_handler;
  37  
  38      /**
  39       * Constructor
  40       *
  41       * @param \phpbb\install\helper\database                        $database_helper    Installer's database helper
  42       * @param \phpbb\install\helper\config                            $install_config        Installer's config helper
  43       * @param \phpbb\install\helper\iohandler\iohandler_interface    $iohandler            Installer's input-output handler
  44       */
  45  	public function __construct(\phpbb\install\helper\database $database_helper,
  46                                  \phpbb\install\helper\config $install_config,
  47                                  \phpbb\install\helper\iohandler\iohandler_interface $iohandler)
  48      {
  49          $this->database_helper    = $database_helper;
  50          $this->install_config    = $install_config;
  51          $this->io_handler        = $iohandler;
  52  
  53          parent::__construct(true);
  54      }
  55  
  56      /**
  57       * {@inheritdoc}
  58       */
  59  	public function run()
  60      {
  61          // Check if data is sent
  62          if ($this->io_handler->get_input('submit_database', false))
  63          {
  64              $this->process_form();
  65          }
  66          else
  67          {
  68              $this->request_form_data();
  69          }
  70      }
  71  
  72      /**
  73       * Process form data
  74       */
  75  	protected function process_form()
  76      {
  77          // Collect database data
  78          $dbms            = $this->io_handler->get_input('dbms', '');
  79          $dbhost            = $this->io_handler->get_input('dbhost', '', true);
  80          $dbport            = $this->io_handler->get_input('dbport', '');
  81          $dbuser            = $this->io_handler->get_input('dbuser', '', true);
  82          $dbpasswd        = $this->io_handler->get_raw_input('dbpasswd', '', true);
  83          $dbname            = $this->io_handler->get_input('dbname', '', true);
  84          $table_prefix    = $this->io_handler->get_input('table_prefix', '', true);
  85  
  86          // Check database data
  87          $user_data_vaild = $this->check_database_data($dbms, $dbhost, $dbport, $dbuser, $dbpasswd, $dbname, $table_prefix);
  88  
  89          // Save database data if it is correct
  90          if ($user_data_vaild)
  91          {
  92              $this->install_config->set('dbms', $dbms);
  93              $this->install_config->set('dbhost', $dbhost);
  94              $this->install_config->set('dbport', $dbport);
  95              $this->install_config->set('dbuser', $dbuser);
  96              $this->install_config->set('dbpasswd', $dbpasswd);
  97              $this->install_config->set('dbname', $dbname);
  98              $this->install_config->set('table_prefix', $table_prefix);
  99          }
 100          else
 101          {
 102              $this->request_form_data(true);
 103          }
 104      }
 105  
 106      /**
 107       * Request data from the user
 108       *
 109       * @param bool $use_request_data Whether to use submited data
 110       *
 111       * @throws \phpbb\install\exception\user_interaction_required_exception When the user is required to provide data
 112       */
 113  	protected function request_form_data($use_request_data = false)
 114      {
 115          if ($use_request_data)
 116          {
 117              $dbms            = $this->io_handler->get_input('dbms', '');
 118              $dbhost            = $this->io_handler->get_input('dbhost', '', true);
 119              $dbport            = $this->io_handler->get_input('dbport', '');
 120              $dbuser            = $this->io_handler->get_input('dbuser', '');
 121              $dbname            = $this->io_handler->get_input('dbname', '');
 122              $table_prefix    = $this->io_handler->get_input('table_prefix', 'phpbb_');
 123          }
 124          else
 125          {
 126              $dbms            = '';
 127              $dbhost            = '';
 128              $dbport            = '';
 129              $dbuser            = '';
 130              $dbname            = '';
 131              $table_prefix    = 'phpbb_';
 132          }
 133  
 134          $dbms_select = array();
 135          foreach ($this->database_helper->get_available_dbms() as $dbms_key => $dbms_array)
 136          {
 137              $dbms_select[] = array(
 138                  'value'        => $dbms_key,
 139                  'label'        => 'DB_OPTION_' . strtoupper($dbms_key),
 140                  'selected'    => ($dbms_key === $dbms),
 141              );
 142          }
 143  
 144          $database_form = array(
 145              'dbms' => array(
 146                  'label'        => 'DBMS',
 147                  'type'        => 'select',
 148                  'options'    => $dbms_select,
 149              ),
 150              'dbhost' => array(
 151                  'label'            => 'DB_HOST',
 152                  'description'    => 'DB_HOST_EXPLAIN',
 153                  'type'            => 'text',
 154                  'default'        => $dbhost,
 155              ),
 156              'dbport' => array(
 157                  'label'            => 'DB_PORT',
 158                  'description'    => 'DB_PORT_EXPLAIN',
 159                  'type'            => 'text',
 160                  'default'        => $dbport,
 161              ),
 162              'dbuser' => array(
 163                  'label'        => 'DB_USERNAME',
 164                  'type'        => 'text',
 165                  'default'    => $dbuser,
 166              ),
 167              'dbpasswd' => array(
 168                  'label'        => 'DB_PASSWORD',
 169                  'type'    => 'password',
 170              ),
 171              'dbname' => array(
 172                  'label'        => 'DB_NAME',
 173                  'type'        => 'text',
 174                  'default'    => $dbname,
 175              ),
 176              'table_prefix' => array(
 177                  'label'            => 'TABLE_PREFIX',
 178                  'description'    => 'TABLE_PREFIX_EXPLAIN',
 179                  'type'            => 'text',
 180                  'default'        => $table_prefix,
 181              ),
 182              'submit_database' => array(
 183                  'label'    => 'SUBMIT',
 184                  'type'    => 'submit',
 185              ),
 186          );
 187  
 188          $this->io_handler->add_user_form_group('DB_CONFIG', $database_form);
 189  
 190          // Require user interaction
 191          throw new user_interaction_required_exception();
 192      }
 193  
 194      /**
 195       * Check database data
 196       *
 197       * @param string    $dbms            Selected database type
 198       * @param string    $dbhost            Database host address
 199       * @param int        $dbport            Database port number
 200       * @param string    $dbuser            Database username
 201       * @param string    $dbpass            Database password
 202       * @param string    $dbname            Database name
 203       * @param string    $table_prefix    Database table prefix
 204       *
 205       * @return bool    True if database data is correct, false otherwise
 206       */
 207  	protected function check_database_data($dbms, $dbhost, $dbport, $dbuser, $dbpass, $dbname, $table_prefix)
 208      {
 209          $available_dbms = $this->database_helper->get_available_dbms();
 210          $data_valid = true;
 211  
 212          // Check if PHP has the database extensions for the specified DBMS
 213          if (!isset($available_dbms[$dbms]))
 214          {
 215              $this->io_handler->add_error_message('INST_ERR_NO_DB');
 216              $data_valid = false;
 217          }
 218  
 219          // Validate table prefix
 220          $prefix_valid = $this->database_helper->validate_table_prefix($dbms, $table_prefix);
 221          if (is_array($prefix_valid))
 222          {
 223              foreach ($prefix_valid as $error)
 224              {
 225                  $this->io_handler->add_error_message(
 226                      $error['title'],
 227                      (isset($error['description'])) ? $error['description'] : false
 228                  );
 229              }
 230  
 231              $data_valid = false;
 232          }
 233  
 234          // Try to connect to database if all provided data is valid
 235          if ($data_valid)
 236          {
 237              $connect_test = $this->database_helper->check_database_connection($dbms, $dbhost, $dbport, $dbuser, $dbpass, $dbname, $table_prefix);
 238              if (is_array($connect_test))
 239              {
 240                  foreach ($connect_test as $error)
 241                  {
 242                      $this->io_handler->add_error_message(
 243                          $error['title'],
 244                          (isset($error['description'])) ? $error['description'] : false
 245                      );
 246                  }
 247  
 248                  $data_valid = false;
 249              }
 250          }
 251  
 252          return $data_valid;
 253      }
 254  
 255      /**
 256       * {@inheritdoc}
 257       */
 258  	static public function get_step_count()
 259      {
 260          return 0;
 261      }
 262  
 263      /**
 264       * {@inheritdoc}
 265       */
 266  	public function get_task_lang_name()
 267      {
 268          return '';
 269      }
 270  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1