- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / setup / steps / ezstep_database_init.php
1 <?php
2 //
3 // Definition of eZStepDatabaseInit class
4 //
5 // Created on: <12-Aug-2003 11:45:59 kk>
6 //
7 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
8 // SOFTWARE NAME: eZ Publish
9 // SOFTWARE RELEASE: 4.1.x
10 // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
11 // SOFTWARE LICENSE: GNU General Public License v2.0
12 // NOTICE: >
13 //   This program is free software; you can redistribute it and/or
14 //   modify it under the terms of version 2.0  of the GNU General
15 //   Public License as published by the Free Software Foundation.
16 //
17 //   This program is distributed in the hope that it will be useful,
18 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //   GNU General Public License for more details.
21 //
22 //   You should have received a copy of version 2.0 of the GNU General
23 //   Public License along with this program; if not, write to the Free
24 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 //   MA 02110-1301, USA.
26 //
27 //
28 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
29 //
30
31 /*! \file
32 */
33
34 /*!
35   \class eZStepDatabaseInit ezstep_database_init.php
36   \brief The class eZStepDatabaseInit does
37
38 */
39
40 class eZStepDatabaseInit extends eZStepInstaller
41 {
42     /*!
43      Constructor
44     */
45     function eZStepDatabaseInit( $tpl, $http, $ini, &$persistenceList )
46     {
47         $this->eZStepInstaller( $tpl, $http, $ini, $persistenceList,
48                                 'database_init', 'Database init' );
49     }
50
51     function processPostData()
52     {
53         $databaseMap = eZSetupDatabaseMap();
54
55         // Get database parameters from input form.
56         if ( $this->Http->hasPostVariable( 'eZSetupDatabaseServer' ) )
57             $this->PersistenceList['database_info']['server'] = $this->Http->postVariable( 'eZSetupDatabaseServer' );
58         if ( $this->Http->hasPostVariable( 'eZSetupDatabasePort' ) )
59             $this->PersistenceList['database_info']['port'] = $this->Http->postVariable( 'eZSetupDatabasePort' );
60         if ( $this->Http->hasPostVariable( 'eZSetupDatabaseName' ) )
61             $this->PersistenceList['database_info']['dbname'] = $this->Http->postVariable( 'eZSetupDatabaseName' );
62         if ( $this->Http->hasPostVariable( 'eZSetupDatabaseUser' ) )
63             $this->PersistenceList['database_info']['user'] = $this->Http->postVariable( 'eZSetupDatabaseUser' );
64         if ( $this->Http->hasPostVariable( 'eZSetupDatabaseSocket' ) )
65             $this->PersistenceList['database_info']['socket'] = $this->Http->postVariable( 'eZSetupDatabaseSocket' );
66         if ( !isset( $this->PersistenceList['database_info']['socket'] ) )
67             $this->PersistenceList['database_info']['socket'] = false;
68         if ( !isset( $this->PersistenceList['database_info']['database'] ) )
69             $this->PersistenceList['database_info']['database'] = false;
70
71
72         $this->Error = 0;
73         $dbStatus = false;
74
75         // Get password
76         if ( isset( $this->PersistenceList['database_info']['password'] ) )
77         {
78             $password = $this->PersistenceList['database_info']['password'];
79         }
80
81         if ( $this->Http->hasPostVariable( 'eZSetupDatabasePassword' ) )
82         {
83             $password = $this->Http->postVariable( 'eZSetupDatabasePassword' );
84             $this->PersistenceList['database_info']['password'] = $password;
85         }
86
87         $databaseChoice = false;
88
89         // Check database connection
90         $databaseInfo = $this->PersistenceList['database_info'];
91         $databaseInfo['info'] = $databaseMap[$databaseInfo['type']];
92         if ( isset( $this->PersistenceList['regional_info'] ) )
93         {
94             $regionalInfo = $this->PersistenceList['regional_info'];
95         }
96         else
97         {
98             $regionalInfo = '';
99         }
100
101         $this->PersistenceList['database_info']['password'] = $password;
102
103         $result = $this->checkDatabaseRequirements( false );
104
105         $this->PersistenceList['database_info']['version'] = $result['db_version'];
106         if ( isset( $result['db_required_version'] ) )
107             $this->PersistenceList['database_info']['required_version'] = $result['db_required_version'];
108         if ( !$result['status'] )
109         {
110             $this->Error = $result['error_code'];
111             return false;
112         }
113
114         $db = $result['db_instance'];
115         $this->PersistenceList['database_info']['use_unicode'] = $result['use_unicode'];
116         $availDatabases = $db->availableDatabases();
117
118         if ( $availDatabases === false ) // not possible to determine if username and password is correct here
119         {
120             return true;
121         }
122         else if ( count( $availDatabases ) > 0 ) // login succeded, and at least one database available
123         {
124             $this->PersistenceList['database_info_available'] = $availDatabases;
125             return true;
126         }
127         else if ( $availDatabases == null && $db->isConnected() === true )
128         {
129             $this->Error = eZStepInstaller::DB_ERROR_NO_DATABASES;
130             return false;
131         }
132
133         $this->Error = eZStepInstaller::DB_ERROR_CONNECTION_FAILED;
134
135         return false;
136     }
137
138     function init()
139     {
140         if ( $this->hasKickstartData() )
141         {
142             $data = $this->kickstartData();
143
144             // Fill in database info in persistence list
145             // This is needed for db requirement check
146             $this->PersistenceList['database_info']['server'] = $data['Server'];
147             $this->PersistenceList['database_info']['port'] = $data['Port'];
148             $this->PersistenceList['database_info']['dbname'] = $data['Database'];
149             $this->PersistenceList['database_info']['user'] = $data['User'];
150             $this->PersistenceList['database_info']['password'] = $data['Password'];
151             $this->PersistenceList['database_info']['socket'] = $data['Socket'];
152             $this->PersistenceList['database_info']['database'] = $data['Database'];
153
154             $result = $this->checkDatabaseRequirements( false );
155
156             $this->PersistenceList['database_info']['version'] = $result['db_version'];
157             if ( isset( $result['db_required_version'] ) )
158             {
159                 $this->PersistenceList['database_info']['required_version'] = $result['db_required_version'];
160             }
161             if ( !$result['status'] )
162             {
163                 $this->Error = $result['error_code'];
164                 return false;
165             }
166
167             $this->PersistenceList['database_info']['use_unicode'] = $result['use_unicode'];
168
169             return $this->kickstartContinueNextStep();
170         }
171
172         // If using windows installer, set standard values, and continue
173 /*        if ( eZSetupTestInstaller() == 'windows' )
174         {
175             $this->PersistenceList['database_info']['server'] = 'localhost';
176             $this->PersistenceList['database_info']['user'] = 'root';
177             $this->PersistenceList['database_info']['password'] = '';
178             return true;
179         }*/
180
181         $config = eZINI::instance( 'setup.ini' );
182         if ( !isset( $this->PersistenceList['database_info']['server'] ) or
183              !$this->PersistenceList['database_info']['server'] )
184             $this->PersistenceList['database_info']['server'] = $config->variable( 'DatabaseSettings', 'DefaultServer' );
185         if ( !isset( $this->PersistenceList['database_info']['port'] ) or
186              !$this->PersistenceList['database_info']['port'] )
187             $this->PersistenceList['database_info']['port'] = $config->variable( 'DatabaseSettings', 'DefaultPort' );
188         if ( !isset( $this->PersistenceList['database_info']['dbname'] ) or
189              !$this->PersistenceList['database_info']['dbname'] )
190             $this->PersistenceList['database_info']['dbname'] = $config->variable( 'DatabaseSettings', 'DefaultName' );
191         if ( !isset( $this->PersistenceList['database_info']['user'] ) or
192              !$this->PersistenceList['database_info']['user'] )
193             $this->PersistenceList['database_info']['user'] = $config->variable( 'DatabaseSettings', 'DefaultUser' );
194         if ( !isset( $this->PersistenceList['database_info']['password'] ) or
195              !$this->PersistenceList['database_info']['password'] )
196             $this->PersistenceList['database_info']['password'] = $config->variable( 'DatabaseSettings', 'DefaultPassword' );
197         if ( !isset( $this->PersistenceList['database_info']['socket'] ) )
198             $this->PersistenceList['database_info']['socket'] = '';
199
200         if ( $this->Http->postVariable( 'eZSetup_current_step' ) == 'SiteDetails' ) // Failed to connect to tables in database
201         {
202             $this->Error = eZStepInstaller::DB_ERROR_CONNECTION_FAILED;
203         }
204
205         return false; // Always show database initialization
206     }
207
208     function display()
209     {
210         $databaseMap = eZSetupDatabaseMap();
211
212         $dbError = 0;
213         $dbNotEmpty = 0;
214         if ( $this->Error )
215         {
216             $dbError = $this->databaseErrorInfo( array( 'error_code' => $this->Error,
217                                                         'database_info' => $this->PersistenceList['database_info'] ) );
218         }
219
220         $databaseInfo = $this->PersistenceList['database_info'];
221         $databaseInfo['info'] = $databaseMap[$databaseInfo['type']];
222         $databaseInfo['table']['is_empty'] = $this->DBEmpty;
223         if ( isset( $this->PersistenceList['regional_info'] ) )
224         {
225             $regionalInfo = $this->PersistenceList['regional_info'];
226         }
227         else
228         {
229             $regionalInfo = '';
230         }
231
232         $this->Tpl->setVariable( 'db_error', $dbError );
233         $this->Tpl->setVariable( 'database_info', $databaseInfo );
234         $this->Tpl->setVariable( 'regional_info', $regionalInfo );
235         $this->Tpl->setVariable( 'db_not_empty', $dbNotEmpty );
236
237         $result = array();
238         // Display template
239         $result['content'] = $this->Tpl->fetch( 'design:setup/init/database_init.tpl' );
240         $result['path'] = array( array( 'text' => ezpI18n::translate( 'design/standard/setup/init',
241                                                           'Database initalization' ),
242                                         'url' => false ) );
243         return $result;
244     }
245
246     public $Error = 0;
247     public $DBEmpty = true;
248 }
249
250 ?>