/kernel/setup/steps/ezstep_database_init.php

https://github.com/zerustech/ezpublish · PHP · 231 lines · 162 code · 34 blank · 35 comment · 32 complexity · 06f8b8bf706b028ffd067018aefc6066 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZStepDatabaseInit class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZStepDatabaseInit ezstep_database_init.php
  12. \brief The class eZStepDatabaseInit does
  13. */
  14. class eZStepDatabaseInit extends eZStepInstaller
  15. {
  16. /**
  17. * Constructor
  18. *
  19. * @param eZTemplate $tpl
  20. * @param eZHTTPTool $http
  21. * @param eZINI $ini
  22. * @param array $persistenceList
  23. */
  24. public function __construct( $tpl, $http, $ini, &$persistenceList )
  25. {
  26. parent::__construct( $tpl, $http, $ini, $persistenceList, 'database_init', 'Database init' );
  27. }
  28. function processPostData()
  29. {
  30. $databaseMap = eZSetupDatabaseMap();
  31. // Get database parameters from input form.
  32. if ( $this->Http->hasPostVariable( 'eZSetupDatabaseServer' ) )
  33. $this->PersistenceList['database_info']['server'] = $this->Http->postVariable( 'eZSetupDatabaseServer' );
  34. if ( $this->Http->hasPostVariable( 'eZSetupDatabasePort' ) )
  35. $this->PersistenceList['database_info']['port'] = $this->Http->postVariable( 'eZSetupDatabasePort' );
  36. if ( $this->Http->hasPostVariable( 'eZSetupDatabaseName' ) )
  37. $this->PersistenceList['database_info']['dbname'] = $this->Http->postVariable( 'eZSetupDatabaseName' );
  38. if ( $this->Http->hasPostVariable( 'eZSetupDatabaseUser' ) )
  39. $this->PersistenceList['database_info']['user'] = $this->Http->postVariable( 'eZSetupDatabaseUser' );
  40. if ( $this->Http->hasPostVariable( 'eZSetupDatabaseSocket' ) )
  41. $this->PersistenceList['database_info']['socket'] = $this->Http->postVariable( 'eZSetupDatabaseSocket' );
  42. if ( !isset( $this->PersistenceList['database_info']['socket'] ) )
  43. $this->PersistenceList['database_info']['socket'] = false;
  44. if ( !isset( $this->PersistenceList['database_info']['database'] ) )
  45. $this->PersistenceList['database_info']['database'] = false;
  46. $this->Error = 0;
  47. $dbStatus = false;
  48. // Get password
  49. if ( isset( $this->PersistenceList['database_info']['password'] ) )
  50. {
  51. $password = $this->PersistenceList['database_info']['password'];
  52. }
  53. if ( $this->Http->hasPostVariable( 'eZSetupDatabasePassword' ) )
  54. {
  55. $password = $this->Http->postVariable( 'eZSetupDatabasePassword' );
  56. $this->PersistenceList['database_info']['password'] = $password;
  57. }
  58. $databaseChoice = false;
  59. // Check database connection
  60. $databaseInfo = $this->PersistenceList['database_info'];
  61. $databaseInfo['info'] = $databaseMap[$databaseInfo['type']];
  62. if ( isset( $this->PersistenceList['regional_info'] ) )
  63. {
  64. $regionalInfo = $this->PersistenceList['regional_info'];
  65. }
  66. else
  67. {
  68. $regionalInfo = '';
  69. }
  70. $this->PersistenceList['database_info']['password'] = $password;
  71. $result = $this->checkDatabaseRequirements( false );
  72. $this->PersistenceList['database_info']['version'] = $result['db_version'];
  73. if ( isset( $result['db_required_version'] ) )
  74. $this->PersistenceList['database_info']['required_version'] = $result['db_required_version'];
  75. if ( !$result['status'] )
  76. {
  77. $this->Error = $result['error_code'];
  78. return false;
  79. }
  80. $db = $result['db_instance'];
  81. $this->PersistenceList['database_info']['use_unicode'] = $result['use_unicode'];
  82. $availDatabases = $db->availableDatabases();
  83. if ( $availDatabases === false ) // not possible to determine if username and password is correct here
  84. {
  85. return true;
  86. }
  87. else if ( count( $availDatabases ) > 0 ) // login succeeded, and at least one database available
  88. {
  89. $this->PersistenceList['database_info_available'] = $availDatabases;
  90. return true;
  91. }
  92. else if ( $availDatabases == null && $db->isConnected() === true )
  93. {
  94. $this->Error = eZStepInstaller::DB_ERROR_NO_DATABASES;
  95. return false;
  96. }
  97. $this->Error = eZStepInstaller::DB_ERROR_CONNECTION_FAILED;
  98. return false;
  99. }
  100. function init()
  101. {
  102. if ( $this->hasKickstartData() )
  103. {
  104. $data = $this->kickstartData();
  105. // Fill in database info in persistence list
  106. // This is needed for db requirement check
  107. $this->PersistenceList['database_info']['server'] = $data['Server'];
  108. $this->PersistenceList['database_info']['port'] = $data['Port'];
  109. $this->PersistenceList['database_info']['dbname'] = $data['Database'];
  110. $this->PersistenceList['database_info']['user'] = $data['User'];
  111. $this->PersistenceList['database_info']['password'] = $data['Password'];
  112. $this->PersistenceList['database_info']['socket'] = $data['Socket'];
  113. $this->PersistenceList['database_info']['database'] = $data['Database'];
  114. $result = $this->checkDatabaseRequirements( false );
  115. $this->PersistenceList['database_info']['version'] = $result['db_version'];
  116. if ( isset( $result['db_required_version'] ) )
  117. {
  118. $this->PersistenceList['database_info']['required_version'] = $result['db_required_version'];
  119. }
  120. if ( !$result['status'] )
  121. {
  122. $this->Error = $result['error_code'];
  123. return false;
  124. }
  125. $this->PersistenceList['database_info']['use_unicode'] = $result['use_unicode'];
  126. return $this->kickstartContinueNextStep();
  127. }
  128. // If using windows installer, set standard values, and continue
  129. /* if ( eZSetupTestInstaller() == 'windows' )
  130. {
  131. $this->PersistenceList['database_info']['server'] = 'localhost';
  132. $this->PersistenceList['database_info']['user'] = 'root';
  133. $this->PersistenceList['database_info']['password'] = '';
  134. return true;
  135. }*/
  136. $config = eZINI::instance( 'setup.ini' );
  137. if ( !isset( $this->PersistenceList['database_info']['server'] ) or
  138. !$this->PersistenceList['database_info']['server'] )
  139. $this->PersistenceList['database_info']['server'] = $config->variable( 'DatabaseSettings', 'DefaultServer' );
  140. if ( !isset( $this->PersistenceList['database_info']['port'] ) or
  141. !$this->PersistenceList['database_info']['port'] )
  142. $this->PersistenceList['database_info']['port'] = $config->variable( 'DatabaseSettings', 'DefaultPort' );
  143. if ( !isset( $this->PersistenceList['database_info']['dbname'] ) or
  144. !$this->PersistenceList['database_info']['dbname'] )
  145. $this->PersistenceList['database_info']['dbname'] = $config->variable( 'DatabaseSettings', 'DefaultName' );
  146. if ( !isset( $this->PersistenceList['database_info']['user'] ) or
  147. !$this->PersistenceList['database_info']['user'] )
  148. $this->PersistenceList['database_info']['user'] = $config->variable( 'DatabaseSettings', 'DefaultUser' );
  149. if ( !isset( $this->PersistenceList['database_info']['password'] ) or
  150. !$this->PersistenceList['database_info']['password'] )
  151. $this->PersistenceList['database_info']['password'] = $config->variable( 'DatabaseSettings', 'DefaultPassword' );
  152. if ( !isset( $this->PersistenceList['database_info']['socket'] ) )
  153. $this->PersistenceList['database_info']['socket'] = '';
  154. if ( $this->Http->postVariable( 'eZSetup_current_step' ) == 'SiteDetails' ) // Failed to connect to tables in database
  155. {
  156. $this->Error = eZStepInstaller::DB_ERROR_CONNECTION_FAILED;
  157. }
  158. return false; // Always show database initialization
  159. }
  160. function display()
  161. {
  162. $databaseMap = eZSetupDatabaseMap();
  163. $dbError = 0;
  164. $dbNotEmpty = 0;
  165. if ( $this->Error )
  166. {
  167. $dbError = $this->databaseErrorInfo( array( 'error_code' => $this->Error,
  168. 'database_info' => $this->PersistenceList['database_info'] ) );
  169. }
  170. $databaseInfo = $this->PersistenceList['database_info'];
  171. $databaseInfo['info'] = $databaseMap[$databaseInfo['type']];
  172. $databaseInfo['table']['is_empty'] = $this->DBEmpty;
  173. if ( isset( $this->PersistenceList['regional_info'] ) )
  174. {
  175. $regionalInfo = $this->PersistenceList['regional_info'];
  176. }
  177. else
  178. {
  179. $regionalInfo = '';
  180. }
  181. $this->Tpl->setVariable( 'db_error', $dbError );
  182. $this->Tpl->setVariable( 'database_info', $databaseInfo );
  183. $this->Tpl->setVariable( 'regional_info', $regionalInfo );
  184. $this->Tpl->setVariable( 'db_not_empty', $dbNotEmpty );
  185. $result = array();
  186. // Display template
  187. $result['content'] = $this->Tpl->fetch( 'design:setup/init/database_init.tpl' );
  188. $result['path'] = array( array( 'text' => ezpI18n::tr( 'design/standard/setup/init',
  189. 'Database initialization' ),
  190. 'url' => false ) );
  191. return $result;
  192. }
  193. public $Error = 0;
  194. public $DBEmpty = true;
  195. }
  196. ?>