]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - install/checkDBSettings.php
Release 6.4.0
[Github/sugarcrm.git] / install / checkDBSettings.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41
42 function checkDBSettings($silent=false) {
43
44     installLog("Begin DB Check Process *************");
45     global $mod_strings;
46     $errors = array();
47     copyInputsIntoSession();
48
49     $db = getInstallDbInstance();
50
51     installLog("testing with {$db->dbType}:{$db->variant}");
52
53
54         if( trim($_SESSION['setup_db_database_name']) == '' ){
55             $errors['ERR_DB_NAME'] = $mod_strings['ERR_DB_NAME'];
56             installLog("ERROR::  {$errors['ERR_DB_NAME']}");
57         }
58
59
60         if (!$db->isDatabaseNameValid($_SESSION['setup_db_database_name'])) {
61             $errIdx = 'ERR_DB_' . strtoupper($_SESSION['setup_db_type']) . '_DB_NAME_INVALID';
62             $errors[$errIdx] = $mod_strings[$errIdx];
63             installLog("ERROR::  {$errors[$errIdx]}");
64         }
65
66         if($_SESSION['setup_db_type'] != 'oci8') {
67             // Oracle doesn't need host name, others do
68             if( trim($_SESSION['setup_db_host_name']) == '' ){
69                 $errors['ERR_DB_HOSTNAME'] = $mod_strings['ERR_DB_HOSTNAME'];
70                 installLog("ERROR::  {$errors['ERR_DB_HOSTNAME']}");
71             }
72         }
73
74         //check to see that password and retype are same, if needed
75         if(!empty($_SESSION['dbUSRData']) && ($_SESSION['dbUSRData']=='create' || $_SESSION['dbUSRData']=='provide'))
76         {
77             if( $_SESSION['setup_db_sugarsales_password'] != $_SESSION['setup_db_sugarsales_password_retype'] ){
78                 $errors['ERR_DBCONF_PASSWORD_MISMATCH'] = $mod_strings['ERR_DBCONF_PASSWORD_MISMATCH'];
79                 installLog("ERROR::  {$errors['ERR_DBCONF_PASSWORD_MISMATCH']}");
80             }
81         }
82
83         // bail if the basic info isn't valid
84         if( count($errors) > 0 ){
85                 installLog("Basic form info is INVALID, exit Process.");
86             return printErrors($errors);
87         } else {
88             installLog("Basic form info is valid, continuing Process.");
89         }
90
91         $dbconfig = array(
92                 "db_host_name" => $_SESSION['setup_db_host_name'],
93                 "db_host_instance" => $_SESSION['setup_db_host_instance'],
94         );
95
96         if(!empty($_SESSION['setup_db_port_num'])) {
97             $dbconfig["db_port"] = $_SESSION['setup_db_port_num'];
98         } else {
99             $_SESSION['setup_db_port_num'] = '';
100         }
101
102         // Needed for database implementation that do not allow connections to the server directly
103         // and that typically require the manual setup of a database instances such as DB2
104         if(empty($_SESSION['setup_db_create_database'])) {
105             $dbconfig["db_name"] = $_SESSION['setup_db_database_name'];
106         }
107
108         // Bug 29855 - Check to see if given db name is valid
109         if (preg_match("![\"'*/\\?:<>-]+!i", $_SESSION['setup_db_database_name']) ) {
110             $errors['ERR_DB_MSSQL_DB_NAME'] = $mod_strings['ERR_DB_MSSQL_DB_NAME_INVALID'];
111             installLog("ERROR::  {$errors['ERR_DB_MSSQL_DB_NAME']}");
112         }
113
114         // test the account that will talk to the db if we're not creating it
115         if( $_SESSION['setup_db_sugarsales_user'] != '' && !$_SESSION['setup_db_create_sugarsales_user'] ){
116             $dbconfig["db_user_name"] = $_SESSION['setup_db_sugarsales_user'];
117             $dbconfig["db_password"] = $_SESSION['setup_db_sugarsales_password'];
118             installLog("Testing user account...");
119
120             // try connecting to the DB
121             if(!$db->connect($dbconfig, false)) {
122                 $error = $db->lastError();
123                 $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE'];
124                 installLog("ERROR::  {$errors['ERR_DB_LOGIN_FAILURE']}");
125             } else {
126                 installLog("Connection made using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
127                 $db->disconnect();
128             }
129         }
130
131         // privileged account tests
132         if( empty($_SESSION['setup_db_admin_user_name']) ){
133             $errors['ERR_DB_PRIV_USER'] = $mod_strings['ERR_DB_PRIV_USER'];
134             installLog("ERROR:: {$errors['ERR_DB_PRIV_USER']}");
135         } else {
136             installLog("Testing priviliged account...");
137             $dbconfig["db_user_name"] = $_SESSION['setup_db_admin_user_name'];
138             $dbconfig["db_password"] = $_SESSION['setup_db_admin_password'];
139             if(!$db->connect($dbconfig, false)) {
140                 $error = $db->lastError();
141                 $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE'];
142                 installLog("ERROR::  {$errors['ERR_DB_LOGIN_FAILURE']}");
143             } else {
144                 installLog("Connection made using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
145                 $db_selected = $db->dbExists($_SESSION['setup_db_database_name']);
146                 if($silent==false && $db_selected && $_SESSION['setup_db_create_database'] && empty($_SESSION['setup_db_drop_tables'])) {
147                     // DB exists but user didn't agree to overwrite it
148                         $errStr = $mod_strings['ERR_DB_EXISTS_PROCEED'];
149                         $errors['ERR_DB_EXISTS_PROCEED'] = $errStr;
150                         installLog("ERROR:: {$errors['ERR_DB_EXISTS_PROCEED']}");
151                 } elseif($silent==false && !$db_selected && !$_SESSION['setup_db_create_database'] ) {
152                     // DB does not exist but user did not allow to create it
153                         $errors['ERR_DB_EXISTS_NOT'] = $mod_strings['ERR_DB_EXISTS_NOT'];
154                         installLog("ERROR:: {$errors['ERR_DB_EXISTS_NOT']}");
155                 } else {
156                     if($db_selected) {
157                         installLog("DB Selected, will reuse {$_SESSION['setup_db_database_name']}");
158                         if($db->tableExists('config') && !$_SESSION['setup_db_drop_tables']) {
159                            include('sugar_version.php');
160                            $versions = $db->getOne("SELECT COUNT(*) FROM config WHERE category='info' AND name='sugar_version' AND VALUE LIKE '$sugar_db_version'");
161                            if($versions != 1 && $silent==false) {
162                                $errors['ERR_DB_EXISTS_WITH_CONFIG'] = $mod_strings['ERR_DB_EXISTS_WITH_CONFIG'];
163                                installLog("ERROR:: {$errors['ERR_DB_EXISTS_WITH_CONFIG']}");
164                            }
165                         }
166                     } else {
167                         installLog("DB not selected, will create {$_SESSION['setup_db_database_name']}");
168                     }
169                     if($_SESSION['setup_db_create_sugarsales_user'] && $_SESSION['setup_db_sugarsales_user'] != '' && $db_selected) {
170                         if($db->userExists($_SESSION['setup_db_sugarsales_user'])) {
171                             $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
172                             installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
173                         }
174                     }
175                 }
176
177                 // DB SPECIFIC
178                 $check = $db->canInstall();
179                 if($check !== true) {
180                     $error = array_shift($check);
181                     array_unshift($check, $mod_strings[$error]);
182                     $errors[$error] = call_user_func_array('sprintf', $check);
183                     installLog("ERROR:: {$errors[$error]}");
184                 } else {
185                     installLog("Passed DB install check");
186                 }
187
188                 $db->disconnect();
189             }
190         }
191
192         if($silent){
193             return $errors;
194         }else{
195             printErrors($errors);
196         }
197         installLog("End DB Check Process *************");
198 }
199
200 function printErrors($errors ){
201
202 global $mod_strings;
203     if(count($errors) == 0){
204         echo 'dbCheckPassed';
205         installLog("SUCCESS:: no errors detected!");
206     }else if((count($errors) == 1 && (isset($errors["ERR_DB_EXISTS_PROCEED"])||isset($errors["ERR_DB_EXISTS_WITH_CONFIG"])))  ||
207     (count($errors) == 2 && isset($errors["ERR_DB_EXISTS_PROCEED"]) && isset($errors["ERR_DB_EXISTS_WITH_CONFIG"])) ){
208         ///throw alert asking to overwwrite db
209         echo 'preexeest';
210         installLog("WARNING:: no errors detected, but DB tables will be dropped!, issuing warning to user");
211     }else{
212         installLog("FATAL:: errors have been detected!  User will not be allowed to continue.  Errors are as follow:");
213          //print out errors
214         $validationErr  = "<p><b>{$mod_strings['ERR_DBCONF_VALIDATION']}</b></p>";
215         $validationErr .= '<ul>';
216
217         foreach($errors as $key =>$erMsg){
218             if($key != "ERR_DB_EXISTS_PROCEED" && $key != "ERR_DB_EXISTS_WITH_CONFIG"){
219                 if($_SESSION['dbUSRData'] == 'same' && $key == 'ERR_DB_ADMIN'){
220                     installLog(".. {$erMsg}");
221                     break;
222                 }
223                 $validationErr .= '<li class="error">' . $erMsg . '</li>';
224                 installLog(".. {$erMsg}");
225             }
226         }
227         $validationErr .= '</ul>';
228         $validationErr .= '</div>';
229
230          echo $validationErr;
231     }
232
233 }
234
235
236 function copyInputsIntoSession(){
237             if(isset($_REQUEST['setup_db_type'])){$_SESSION['setup_db_type']                        = $_REQUEST['setup_db_type'];}
238             if(isset($_REQUEST['setup_db_admin_user_name'])){$_SESSION['setup_db_admin_user_name']  = $_REQUEST['setup_db_admin_user_name'];}
239             if(isset($_REQUEST['setup_db_admin_password'])){$_SESSION['setup_db_admin_password']    = $_REQUEST['setup_db_admin_password'];}
240             if(isset($_REQUEST['setup_db_database_name'])){$_SESSION['setup_db_database_name']      = $_REQUEST['setup_db_database_name'];}
241             if(isset($_REQUEST['setup_db_host_name'])){$_SESSION['setup_db_host_name']              = $_REQUEST['setup_db_host_name'];}
242
243             if(isset($_SESSION['setup_db_type']) && (!isset($_SESSION['setup_db_manager']) || isset($_REQUEST['setup_db_type']))) {
244                 $_SESSION['setup_db_manager'] = DBManagerFactory::getManagerByType($_SESSION['setup_db_type']);
245             }
246
247             if(isset($_REQUEST['setup_db_host_instance'])){
248                 $_SESSION['setup_db_host_instance'] = $_REQUEST['setup_db_host_instance'];
249             }
250
251             if(isset($_REQUEST['setup_db_port_num'])){
252                 $_SESSION['setup_db_port_num'] = $_REQUEST['setup_db_port_num'];
253             }
254
255             // on a silent install, copy values from $_SESSION into $_REQUEST
256             if (isset($_REQUEST['goto']) && $_REQUEST['goto'] == 'SilentInstall') {
257                 if (isset($_SESSION['dbUSRData']) && !empty($_SESSION['dbUSRData']))
258                     $_REQUEST['dbUSRData'] = $_SESSION['dbUSRData'];
259                 else $_REQUEST['dbUSRData'] = 'same';
260
261                 if (isset($_SESSION['setup_db_sugarsales_user']) && !empty($_SESSION['setup_db_sugarsales_user']))
262                     $_REQUEST['setup_db_sugarsales_user'] = $_SESSION['setup_db_sugarsales_user'];
263                 else $_REQUEST['dbUSRData'] = 'same';
264
265                 $_REQUEST['setup_db_sugarsales_password'] = $_SESSION['setup_db_sugarsales_password'];
266                 $_REQUEST['setup_db_sugarsales_password_retype'] = $_SESSION['setup_db_sugarsales_password'];
267             }
268
269             //make sure we are creating or using provided user for app db connections
270             $_SESSION['setup_db_create_sugarsales_user']  = true;//get_boolean_from_request('setup_db_create_sugarsales_user');
271             $db = getInstallDbInstance();
272             if( !$db->supports("create_user") ){
273              //if the DB doesn't support creating users, make the admin user/password same as connecting user/password
274               $_SESSION['setup_db_sugarsales_user']             = $_SESSION['setup_db_admin_user_name'];
275               $_SESSION['setup_db_sugarsales_password']         = $_SESSION['setup_db_admin_password'];
276               $_SESSION['setup_db_sugarsales_password_retype']  = $_SESSION['setup_db_sugarsales_password'];
277               $_SESSION['setup_db_create_sugarsales_user']      = false;
278               $_SESSION['setup_db_create_database']             = false;
279
280             } else {
281                 $_SESSION['setup_db_create_database']             = true;
282                 //retrieve the value from dropdown in order to know what settings the user
283                 //wants to use for the sugar db user.
284
285                 //use provided db admin by default
286                 $_SESSION['dbUSRData'] = 'same';
287
288                 if(isset($_REQUEST['dbUSRData'])  && !empty($_REQUEST['dbUSRData'])){
289                     $_SESSION['dbUSRData'] = $_REQUEST['dbUSRData'];
290                 }
291
292
293                   if($_SESSION['dbUSRData'] == 'auto'){
294                     //create user automatically
295                       $_SESSION['setup_db_create_sugarsales_user']          = true;
296                       $_SESSION['setup_db_sugarsales_user']                 = "sugar".create_db_user_creds(5);
297                       $_SESSION['setup_db_sugarsales_password']             = create_db_user_creds(10);
298                       $_SESSION['setup_db_sugarsales_password_retype']      = $_SESSION['setup_db_sugarsales_password'];
299                   }elseif($_SESSION['dbUSRData'] == 'provide'){
300                     //use provided user info
301                       $_SESSION['setup_db_create_sugarsales_user']          = false;
302                       $_SESSION['setup_db_sugarsales_user']                 = $_REQUEST['setup_db_sugarsales_user'];
303                       $_SESSION['setup_db_sugarsales_password']             = $_REQUEST['setup_db_sugarsales_password'];
304                       $_SESSION['setup_db_sugarsales_password_retype']      = $_REQUEST['setup_db_sugarsales_password_retype'];
305                   }elseif($_SESSION['dbUSRData'] == 'create'){
306                     // create user with provided info
307                       $_SESSION['setup_db_create_sugarsales_user']        = true;
308                       $_SESSION['setup_db_sugarsales_user']               = $_REQUEST['setup_db_sugarsales_user'];
309                       $_SESSION['setup_db_sugarsales_password']           = $_REQUEST['setup_db_sugarsales_password'];
310                       $_SESSION['setup_db_sugarsales_password_retype']    = $_REQUEST['setup_db_sugarsales_password_retype'];
311                   }else{
312                    //Use the same login as provided admin user
313                       $_SESSION['setup_db_create_sugarsales_user']      = false;
314                       $_SESSION['setup_db_sugarsales_user']             = $_SESSION['setup_db_admin_user_name'];
315                       $_SESSION['setup_db_sugarsales_password']         = $_SESSION['setup_db_admin_password'];
316                       $_SESSION['setup_db_sugarsales_retype']           = $_SESSION['setup_db_admin_password'];
317                   }
318             }
319
320             if(!isset($_SESSION['demoData']) || empty($_SESSION['demoData'])){
321                 $_SESSION['demoData'] = 'no';
322             }
323             if(isset($_REQUEST['demoData'])){$_SESSION['demoData'] = $_REQUEST['demoData'] ;}
324
325             if($db->supports('create_db')) {
326                 if(!empty($_SESSION['setup_db_create_database'])) {
327                 // if we're dropping DB, no need to drop tables
328                         $_SESSION['setup_db_drop_tables']  = false;
329                 }
330             } else {
331                 // we can't create DB, so can't drop it
332                 $_SESSION['setup_db_create_database'] = false;
333             }
334
335             if (isset($_REQUEST['goto']) && $_REQUEST['goto'] == 'SilentInstall' && isset($_SESSION['setup_db_drop_tables'])) {
336                 //set up for Oracle Silent Installer
337                 $_REQUEST['setup_db_drop_tables'] = $_SESSION['setup_db_drop_tables'] ;
338             }
339 }
340
341 ////    END PAGEOUTPUT
342 ///////////////////////////////////////////////////////////////////////////////
343 ?>