forked from mantisbt/mantisbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.php
1372 lines (1240 loc) · 39.9 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Mantis Database installation process
*
* @package MantisBT
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - [email protected]
* @copyright Copyright 2002 MantisBT Team - [email protected]
* @link http://www.mantisbt.org
*/
error_reporting( E_ALL );
@set_time_limit( 0 );
# Load the MantisDB core in maintenance mode. This mode will assume that
# config/config_inc.php hasn't been specified. Thus the database will not be opened
# and plugins will not be loaded.
define( 'MANTIS_MAINTENANCE_MODE', true );
require_once( dirname( dirname( __FILE__ ) ) . '/core.php' );
require_api( 'install_helper_functions_api.php' );
require_api( 'crypto_api.php' );
$g_error_send_page_header = false; # bypass page headers in error handler
$g_failed = false;
$g_database_upgrade = false;
/**
* Print Test result
*
* @param integer $p_result Result - BAD|GOOD.
* @param boolean $p_hard_fail Fail installation or soft warning.
* @param string $p_message Message to display to user.
* @return void
*/
function print_test_result( $p_result, $p_hard_fail = true, $p_message = '' ) {
global $g_failed;
echo '<td ';
if( BAD == $p_result ) {
if( $p_hard_fail ) {
$g_failed = true;
echo 'bgcolor="red">BAD';
} else {
echo 'bgcolor="pink">POSSIBLE PROBLEM';
}
if( '' != $p_message ) {
echo '<br />' . $p_message;
}
}
if( GOOD == $p_result ) {
echo 'bgcolor="green">GOOD';
}
echo '</td>';
}
/**
* Print Test result
*
* @param string $p_test_description Test Description.
* @param integer $p_result Result - BAD|GOOD.
* @param boolean $p_hard_fail Fail installation or soft warning.
* @param string $p_message Message to display to user.
* @return void
*/
function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_message = '' ) {
echo '<tr><td bgcolor="#ffffff">' . $p_test_description . '</td>';
print_test_result( $p_result, $p_hard_fail, $p_message );
echo '</tr>' . "\n";
}
# install_state
# 0 = no checks done
# 1 = server ok, get database information
# 2 = check the database information
# 3 = install the database
# 4 = get additional config file information
# 5 = write the config file
# 6 = post install checks
# 7 = done, link to login or db updater
$t_install_state = gpc_get_int( 'install', 0 );
html_begin();
html_head_begin();
html_css_link( 'admin.css' );
html_content_type();
html_title( 'Administration - Installation' );
html_javascript_link( 'jquery-1.11.3.min.js' );
html_javascript_link( 'install.js' );
html_head_end();
?>
<body>
<table width="100%" cellspacing="0" cellpadding="0">
<tr class="top-bar">
<td class="links">
[ <a href="index.php">Back to Administration</a> ]
</td>
<td class="title">
<?php
switch( $t_install_state ) {
case 7:
echo 'Installation Complete';
break;
case 6:
echo 'Post Installation Checks';
break;
case 5:
echo 'Install Configuration File';
break;
case 4:
echo 'Additional Configuration Information';
break;
case 3:
echo 'Install Database';
break;
case 2:
echo 'Check and Install Database';
break;
case 1:
echo 'Database Parameters';
break;
case 0:
default:
$t_install_state = 0;
echo 'Pre-Installation Check';
break;
}
?>
</td>
</tr>
</table>
<br /><br />
<?php
# installation checks table header is valid both for pre-install and
# database installation steps
if( 0 == $t_install_state || 2 == $t_install_state ) {
?>
<table width="100%" cellpadding="10" cellspacing="1">
<tr>
<td bgcolor="#e8e8e8" colspan="2">
<span class="title">Checking Installation</span>
</td>
</tr>
<?php
}
$t_config_filename = $g_config_path . 'config_inc.php';
$t_config_exists = file_exists( $t_config_filename );
# Initialize Oracle-specific values for prefix and suffix, and set
# values for other db's as per config defaults
$t_prefix_defaults = array(
'oci8' => array(
'db_table_prefix' => 'm',
'db_table_plugin_prefix' => 'plg',
'db_table_suffix' => '',
) ,
);
foreach( $t_prefix_defaults['oci8'] as $t_key => $t_value ) {
$t_prefix_defaults['other'][$t_key] = config_get( $t_key, '' );
}
if( $t_config_exists && $t_install_state <= 1 ) {
# config already exists - probably an upgrade
$f_dsn = config_get( 'dsn', '' );
$f_hostname = config_get( 'hostname', '' );
$f_db_type = config_get( 'db_type', '' );
$f_database_name = config_get( 'database_name', '' );
$f_db_schema = config_get( 'db_schema', '' );
$f_db_username = config_get( 'db_username', '' );
$f_db_password = config_get( 'db_password', '' );
$f_timezone = config_get( 'default_timezone', '' );
# Set default prefix/suffix form variables ($f_db_table_XXX)
$t_prefix_type = 'other';
foreach( $t_prefix_defaults[$t_prefix_type] as $t_key => $t_value ) {
${'f_' . $t_key} = $t_value;
}
} else {
# read control variables with defaults
$f_dsn = gpc_get( 'dsn', config_get( 'dsn', '' ) );
$f_hostname = gpc_get( 'hostname', config_get( 'hostname', 'localhost' ) );
$f_db_type = gpc_get( 'db_type', config_get( 'db_type', '' ) );
$f_database_name = gpc_get( 'database_name', config_get( 'database_name', 'bugtracker' ) );
$f_db_schema = gpc_get( 'db_schema', config_get( 'db_schema', '' ) );
$f_db_username = gpc_get( 'db_username', config_get( 'db_username', '' ) );
$f_db_password = gpc_get( 'db_password', config_get( 'db_password', '' ) );
if( CONFIGURED_PASSWORD == $f_db_password ) {
$f_db_password = config_get( 'db_password' );
}
$f_timezone = gpc_get( 'timezone', config_get( 'default_timezone' ) );
# Set default prefix/suffix form variables ($f_db_table_XXX)
$t_prefix_type = $f_db_type == 'oci8' ? $f_db_type : 'other';
foreach( $t_prefix_defaults[$t_prefix_type] as $t_key => $t_value ) {
${'f_' . $t_key} = gpc_get( $t_key, $t_value );
}
}
$f_admin_username = gpc_get( 'admin_username', '' );
$f_admin_password = gpc_get( 'admin_password', '' );
if( CONFIGURED_PASSWORD == $f_admin_password ) {
$f_admin_password = '';
}
$f_log_queries = gpc_get_bool( 'log_queries', false );
$f_db_exists = gpc_get_bool( 'db_exists', false );
if( $t_config_exists ) {
if( 0 == $t_install_state ) {
print_test( 'Config File Exists - Upgrade', true );
print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' );
# @TODO: dsn config seems to be undefined, remove ?
$t_db_conn_exists = ( $f_dsn !== '' || ( $f_database_name !== '' && $f_db_username !== '' && $f_hostname !== '' ) );
# Oracle supports binding in two ways:
# - hostname, username/password and database name
# - tns name (insert into hostname field) and username/password, database name is still empty
if( $f_db_type == 'oci8' ) {
$t_db_conn_exists = $t_db_conn_exists || ( $f_database_name == '' && $f_db_username !== '' && $f_hostname !== '' );
}
print_test( 'Checking Database connection settings exist',
$t_db_conn_exists,
true,
'database connection settings do not exist?' );
print_test( 'Checking PHP support for database type',
db_check_database_support( $f_db_type ), true,
'database is not supported by PHP. Check that it has been compiled into your server.' );
if( $f_db_type == 'mssql' ) {
print_test( 'Checking PHP support for Microsoft SQL Server driver',
version_compare( phpversion(), '5.3' ) < 0, true,
'mssql driver is no longer supported in PHP >= 5.3, please use mssqlnative instead' );
}
}
$g_db = ADONewConnection( $f_db_type );
$t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
if( $g_db->IsConnected() ) {
$g_db_connected = true;
}
$t_cur_version = config_get( 'database_version', -1 );
if( $t_cur_version > 1 ) {
$g_database_upgrade = true;
$f_db_exists = true;
} else {
if( 0 == $t_install_state ) {
print_test( 'Config File Exists but Database does not', false, false, 'Bad config_inc.php?' );
}
}
}
if( $f_db_type == 'db2' ) {
# If schema name is supplied, then separate it from database name.
if( strpos( $f_database_name, '/' ) != false ) {
$f_db2AS400 = $f_database_name;
list( $f_database_name, $f_db_schema ) = explode( '/', $f_db2AS400, 2 );
}
}
if( 0 == $t_install_state ) {
?>
<!-- Check PHP Version -->
<?php print_test( ' Checking PHP version (your version is ' . phpversion() . ')', check_php_version( phpversion() ), true, 'Upgrade to a more recent version of PHP' );?>
<!-- Check Safe Mode -->
<?php
print_test( 'Checking if safe mode is enabled for install script',
!ini_get( 'SAFE_MODE' ),
true,
'Disable safe_mode in php.ini before proceeding' ) ?>
<?php
# Check for custom config files in obsolete locations
$t_config_files = array(
'config_inc.php' => 'move',
'custom_constants_inc.php' => 'move',
'custom_strings_inc.php' => 'move',
'custom_functions_inc.php' => 'move',
'custom_relationships_inc.php' => 'move',
'mc_config_defaults_inc.php' => 'delete',
'mc_config_inc.php' => 'contents',
);
foreach( $t_config_files as $t_file => $t_action ) {
$t_dir = dirname( dirname( __FILE__ ) ) . '/';
if( substr( $t_file, 0, 3 ) == 'mc_' ) {
$t_dir .= 'api/soap/';
}
switch( $t_action ) {
case 'move':
$t_message = "Move $t_file to config/$t_file.";
break;
case 'delete':
$t_message = 'Delete this file.';
break;
case 'contents':
$t_message = 'Move contents to config_inc.php file.';
break;
}
print_test(
"Checking there is no '$t_file' file in 1.2.x location.",
!file_exists( $t_dir . $t_file ),
true,
$t_message
);
}
?>
</table>
<?php
if( false == $g_failed ) {
$t_install_state++;
}
} # end install_state == 0
# got database information, check and install
if( 2 == $t_install_state ) {
?>
<!-- Checking DB support-->
<?php
print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' );
print_test( 'Checking PHP support for database type', db_check_database_support( $f_db_type ), true, 'database is not supported by PHP. Check that it has been compiled into your server.' );
# ADOdb library version check
# PostgreSQL, Oracle and MSSQL require at least 5.19. MySQL should be fine
# with 5.10 but to simplify we align to the requirement of the others.
$t_adodb_version = substr( $ADODB_vers, 1, strpos( $ADODB_vers, ' ' ) - 1 );
print_test( 'Checking ADOdb Library version is at least ' . DB_MIN_VERSION_ADODB,
version_compare( $t_adodb_version, DB_MIN_VERSION_ADODB, '>=' ),
true,
'Current version: ' . $ADODB_vers
);
print_test( 'Setting Database Hostname', '' !== $f_hostname, true, 'host name is blank' );
print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' );
print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' );
print_test( 'Setting Database Name', '' !== $f_database_name || $f_db_type == 'oci8', true, 'database name is blank' );
if( $f_db_type == 'db2' ) {
print_test( 'Setting Database Schema', !is_blank( $f_db_schema ), true, 'must have a schema name for AS400 in the form of DBNAME/SCHEMA' );
}
?>
<tr>
<td bgcolor="#ffffff">
Setting Admin Username
</td>
<?php
if( '' !== $f_admin_username ) {
print_test_result( GOOD );
} else {
print_test_result( BAD, false, 'admin user name is blank, using database user instead' );
$f_admin_username = $f_db_username;
}
?>
</tr>
<tr>
<td bgcolor="#ffffff">
Setting Admin Password
</td>
<?php
if( '' !== $f_admin_password ) {
print_test_result( GOOD );
} else {
print_test_result( BAD, false, 'admin user password is blank, using database user password instead' );
$f_admin_password = $f_db_password;
}
?>
</tr>
<!-- connect to db -->
<tr>
<td bgcolor="#ffffff">
Attempting to connect to database as admin
</td>
<?php
$t_db_open = false;
$g_db = ADONewConnection( $f_db_type );
$t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password );
if( $t_result ) {
# check if db exists for the admin
$t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password, $f_database_name );
if( $t_result ) {
$t_db_open = true;
$f_db_exists = true;
}
if( $f_db_type == 'db2' ) {
$t_result = $g_db->execute( 'set schema ' . $f_db_schema );
if( $t_result === false ) {
print_test_result( BAD, true, 'set schema failed: ' . $g_db->errorMsg() );
}
} else {
print_test_result( GOOD );
}
# due to a bug in ADODB, this call prompts warnings, hence the @
# the check only works on mysql if the database is open
$t_version_info = @$g_db->ServerInfo();
} else {
print_test_result( BAD, true, 'Does administrative user have access to the database? ( ' . db_error_msg() . ' )' );
$t_version_info = null;
}
?>
</tr>
<?php
if( $f_db_exists ) {
?>
<tr>
<td bgcolor="#ffffff">
Attempting to connect to database as user
</td>
<?php
$g_db = ADONewConnection( $f_db_type );
$t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
if( $t_result == true ) {
$t_db_open = true;
if( $f_db_type == 'db2' ) {
$t_result = $g_db->execute( 'set schema ' . $f_db_schema );
if( $t_result === false ) {
print_test_result( BAD, true, 'set schema failed: ' . $g_db->errorMsg() );
}
} else {
print_test_result( GOOD );
}
} else {
print_test_result( BAD, false, 'Database user doesn\'t have access to the database ( ' . db_error_msg() . ' )' );
}
?>
</tr>
<?php
}
if( $t_db_open ) {
?>
<!-- display database version -->
<tr>
<td bgcolor="#ffffff">
Checking Database Server Version
<?php
if( isset( $t_version_info['description'] ) ) {
echo '<br /> Running ' . string_attribute( $f_db_type )
. ' version ' . nl2br( $t_version_info['description'] );
}
?>
</td>
<?php
$t_warning = '';
$t_error = '';
switch( $f_db_type ) {
case 'mysql':
case 'mysqli':
if( version_compare( $t_version_info['version'], DB_MIN_VERSION_MYSQL, '<' ) ) {
$t_error = 'MySQL ' . DB_MIN_VERSION_MYSQL . ' or later is required for installation.';
}
break;
case 'mssql':
case 'mssqlnative':
if( version_compare( $t_version_info['version'], DB_MIN_VERSION_MSSQL, '<' ) ) {
$t_error = 'SQL Server 2005 (' . DB_MIN_VERSION_MSSQL . ') or later is required for installation.';
}
break;
case 'pgsql':
case 'db2':
default:
break;
}
if( is_null( $t_version_info ) ) {
$t_warning = "Unable to determine '$f_db_type' version. ($t_error).";
$t_error = '';
}
print_test_result(
( '' == $t_error ) && ( '' == $t_warning ),
( '' != $t_error ),
$t_error . ' ' . $t_warning
);
?>
</tr>
</table>
<?php
}?>
</table>
<?php
if( false == $g_failed ) {
$t_install_state++;
} else {
$t_install_state--; # a check failed, redisplay the questions
}
} # end 2 == $t_install_state
# system checks have passed, get the database information
if( 1 == $t_install_state ) {
?>
<form method='POST'>
<input name="install" type="hidden" value="2">
<table width="100%" cellpadding="10" cellspacing="1">
<tr>
<td bgcolor="#e8e8e8" colspan="2">
<span class="title">
<?php echo
( $g_database_upgrade ? 'Upgrade Options' : 'Installation Options' ),
( $g_failed ? ': Checks Failed ' : '' )
?>
</span>
</td>
</tr>
<?php
# install-only fields: when upgrading, only display admin username and password
if( !$g_database_upgrade ) {
?>
<!-- Database type selection list -->
<tr>
<td>
Type of Database
</td>
<td>
<!-- Default values for table prefix/suffix -->
<div>
<?php
# These elements are referenced by the db selection list's on change event
# to populate the corresponding fields as appropriate
foreach( $t_prefix_defaults as $t_db_type => $t_defaults ) {
echo '<div id="default_' . $t_db_type . '" class="hidden">';
foreach( $t_defaults as $t_key => $t_value ) {
echo "\n\t" . '<span name="' . $t_key . '">' . $t_value . '</span>';
}
echo "\n" . '</div>' . "\n";
}
?>
</div>
<select id="db_type" name="db_type">
<?php
# Build selection list of available DB types
$t_db_list = array(
'mysqli' => 'MySQL Improved',
'mysql' => 'MySQL',
'mssql' => 'Microsoft SQL Server',
'mssqlnative' => 'Microsoft SQL Server Native Driver',
'pgsql' => 'PostgreSQL',
'oci8' => 'Oracle',
'db2' => 'IBM DB2',
);
# mysql is deprecated as of PHP 5.5.0
if( version_compare( phpversion(), '5.5.0' ) >= 0 ) {
unset( $t_db_list['mysql']);
}
# mssql is not supported with PHP >= 5.3
if( version_compare( phpversion(), '5.3' ) >= 0 ) {
unset( $t_db_list['mssql']);
}
foreach( $t_db_list as $t_db => $t_db_descr ) {
echo '<option value="' . $t_db . '"' .
( $t_db == $f_db_type ? ' selected="selected"' : '' ) . '>' .
$t_db_descr . "</option>\n";
}
?>
</select>
</td>
</tr>
<!-- Database server hostname -->
<tr>
<td>
Hostname (for Database Server)
</td>
<td>
<input name="hostname" type="textbox" value="<?php echo string_attribute( $f_hostname ) ?>">
</td>
</tr>
<!-- Database username and password -->
<tr>
<td>
Username (for Database)
</td>
<td>
<input name="db_username" type="textbox" value="<?php echo string_attribute( $f_db_username ) ?>">
</td>
</tr>
<tr>
<td>
Password (for Database)
</td>
<td>
<input name="db_password" type="password" value="<?php
echo !is_blank( $f_db_password ) && $t_config_exists
? CONFIGURED_PASSWORD
: $f_db_password;
?>">
</td>
</tr>
<!-- Database name -->
<tr>
<td>
Database name (for Database)
</td>
<td>
<input name="database_name" type="textbox" value="<?php echo string_attribute( $f_database_name ) ?>">
</td>
</tr>
<?php
} # end install-only fields
?>
<!-- Admin user and password -->
<tr>
<td>
Admin Username (to <?php echo( !$g_database_upgrade ) ? 'create Database' : 'update Database'?> if required)
</td>
<td>
<input name="admin_username" type="textbox" value="<?php echo string_attribute( $f_admin_username ) ?>">
</td>
</tr>
<tr>
<td>
Admin Password (to <?php echo( !$g_database_upgrade ) ? 'create Database' : 'update Database'?> if required)
</td>
<td>
<input name="admin_password" type="password" value="<?php
echo !is_blank( $f_admin_password ) && $f_admin_password == $f_db_password
? CONFIGURED_PASSWORD
: string_attribute( $f_admin_password );
?>">
</td>
</tr>
<?php
# install-only fields: when upgrading, only display admin username and password
if( !$g_database_upgrade ) {
$t_prefix_labels = array(
'db_table_prefix' => 'Database Table Prefix',
'db_table_plugin_prefix' => 'Database Plugin Table Prefix',
'db_table_suffix' => 'Database Table Suffix',
);
foreach( $t_prefix_defaults[$t_prefix_type] as $t_key => $t_value ) {
echo "<tr>\n\t<td>\n";
echo "\t\t" . $t_prefix_labels[$t_key] . "\n";
echo "\t</td>\n\t<td>\n\t\t";
echo '<input id="' . $t_key . '" name="' . $t_key . '" type="textbox" value="' . $f_db_table_prefix . '">';
echo "\n\t</td>\n</tr>\n\n";
}
# Default timezone, get PHP setting if not defined in Mantis
$t_tz = config_get_global( 'default_timezone' );
if( is_blank( $t_tz ) ) {
$t_tz = @date_default_timezone_get();
}
?>
<!-- Timezone -->
<tr>
<td>
Default Time Zone
</td>
<td>
<select id="timezone" name="timezone">
<?php print_timezone_option_list( $t_tz ) ?>
</select>
</td>
</tr>
<?php
} # end install-only fields
?>
<!-- Printing SQL queries -->
<tr>
<td>
Print SQL Queries instead of Writing to the Database
</td>
<td>
<input name="log_queries" type="checkbox" value="1" <?php echo( $f_log_queries ? 'checked="checked"' : '' )?>>
</td>
</tr>
<!-- Submit button -->
<tr>
<td>
<?php echo ( $g_failed
? 'Please correct failed checks and try again'
: 'Attempt Installation' );
?>
</td>
<td>
<input name="go" type="submit" class="button" value="Install/Upgrade Database">
</td>
</tr>
</table>
</form>
<?php
} # end install_state == 1
# all checks have passed, install the database
if( 3 == $t_install_state ) {
?>
<table width="100%" cellpadding="10" cellspacing="1">
<tr>
<td bgcolor="#e8e8e8" colspan="2">
<span class="title">Installing Database</span>
</td>
</tr>
<?php if( !$f_log_queries ) {?>
<tr>
<td bgcolor="#ffffff">
Create database if it does not exist
</td>
<?php
$t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password, $f_database_name );
if( $f_db_type == 'db2' ) {
$t_rs = $g_db->Execute( "select * from SYSIBM.SCHEMATA WHERE SCHEMA_NAME = '" . $f_db_schema . "' AND SCHEMA_OWNER = '" . $f_db_username . "'" );
if( $t_rs === false ) {
echo '<br />false';
}
if( $t_rs->EOF ) {
$t_result = false;
echo $g_db->errorMsg();
} else {
$t_result = $g_db->execute( 'set schema ' . $f_db_schema );
}
}
$t_db_open = false;
if( $t_result == true ) {
print_test_result( GOOD );
$t_db_open = true;
} else {
# create db
$g_db = ADONewConnection( $f_db_type );
$t_result = $g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password );
$t_dict = NewDataDictionary( $g_db );
if( $f_db_type == 'db2' ) {
$t_rs = $g_db->Execute( 'CREATE SCHEMA ' . $f_db_schema );
if( !$t_rs ) {
$t_result = false;
print_test_result( BAD, true, 'Does administrative user have access to create the database? ( ' . db_error_msg() . ' )' );
$t_install_state--; # db creation failed, allow user to re-enter user/password info
} else {
print_test_result( GOOD );
$t_db_open = true;
}
} else {
$t_sqlarray = $t_dict->CreateDatabase( $f_database_name, array( 'mysql' => 'DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci' ) );
$t_ret = $t_dict->ExecuteSQLArray( $t_sqlarray, false );
if( $t_ret == 2 ) {
print_test_result( GOOD );
$t_db_open = true;
} else {
$t_error = db_error_msg();
if( $f_db_type == 'oci8' ) {
$t_db_exists = preg_match( '/ORA-01920/', $t_error );
} else {
$t_db_exists = strstr( $t_error, 'atabase exists' );
}
if( $t_db_exists ) {
print_test_result( BAD, false, 'Database already exists? ( ' . db_error_msg() . ' )' );
} else {
print_test_result( BAD, true, 'Does administrative user have access to create the database? ( ' . db_error_msg() . ' )' );
$t_install_state--; # db creation failed, allow user to re-enter user/password info
}
}
}
}
?>
</tr>
<?php
# Close the connection and clear the ADOdb object to free memory
$g_db->Close();
$g_db = null;
?>
<tr>
<td bgcolor="#ffffff">
Attempting to connect to database as user
</td>
<?php
$g_db = ADONewConnection( $f_db_type );
$t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
if( $f_db_type == 'db2' ) {
$t_result = $g_db->execute( 'set schema ' . $f_db_schema );
if( $t_result === false ) {
echo $g_db->errorMsg();
}
}
if( $t_result == true ) {
print_test_result( GOOD );
} else {
print_test_result( BAD, false, 'Database user doesn\'t have access to the database ( ' . db_error_msg() . ' )' );
}
$g_db->Close();
?>
</tr>
<?php
}
# install the tables
if( false == $g_failed ) {
$g_db_connected = false;
# fake out database access routines used by config_get
config_set_global( 'db_type', $f_db_type );
# Initialize table prefixes as specified by user
config_set_global( 'db_table_prefix', $f_db_table_prefix );
config_set_global( 'db_table_plugin_prefix', $f_db_table_plugin_prefix );
config_set_global( 'db_table_suffix', $f_db_table_suffix );
# database_api references this
require_once( dirname( __FILE__ ) . '/schema.php' );
$g_db = ADONewConnection( $f_db_type );
$t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password, $f_database_name );
if( !$f_log_queries ) {
$g_db_connected = true;
# fake out database access routines used by config_get
}
$t_last_update = config_get( 'database_version', -1, ALL_USERS, ALL_PROJECTS );
$t_last_id = count( $g_upgrade ) - 1;
$i = $t_last_update + 1;
if( $f_log_queries ) {
echo '<tr><td bgcolor="#ffffff" col_span="2"> Database Creation Suppressed, SQL Queries follow <pre>';
}
# Make sure we do the upgrades using UTF-8 if needed
if( $f_db_type === 'mysql' || $f_db_type === 'mysqli' ) {
$g_db->execute( 'SET NAMES UTF8' );
}
if( $f_db_type == 'db2' ) {
$t_result = $g_db->execute( 'set schema ' . $f_db_schema );
if( $t_result === false ) {
echo $g_db->errorMsg();
}
}
$t_dict = NewDataDictionary( $g_db );
# Special processing for specific schema versions
# This allows execution of additional install steps, which are
# not a Mantis schema upgrade but nevertheless required due to
# changes in the code
if( $t_last_update > 51 && $t_last_update < 189 ) {
# Since MantisBT 1.1.0 / ADOdb 4.96 (corresponding to schema 51)
# 'L' columns are BOOLEAN instead of SMALLINT
# Check for any DB discrepancies and update columns if needed
$t_bool_columns = check_pgsql_bool_columns();
if( $t_bool_columns !== true ) {
# Some columns need converting
$t_msg = "PostgreSQL: check Boolean columns' actual type";
if( is_array( $t_bool_columns ) ) {
print_test(
$t_msg,
count( $t_bool_columns ) == 0,
false,
count( $t_bool_columns ) . ' columns must be converted to BOOLEAN' );
} else {
# We did not get an array => error occured
print_test( $t_msg, false, true, $t_bool_columns );
}
# Convert the columns
foreach( $t_bool_columns as $t_row ) {
extract( $t_row, EXTR_PREFIX_ALL, 'v' );
$t_null = $v_is_nullable ? 'NULL' : 'NOT NULL';
$t_default = is_null( $v_column_default ) ? 'NULL' : $v_column_default;
$t_sqlarray = $t_dict->AlterColumnSQL(
$v_table_name,
$v_column_name . ' L ' . $t_null . ' DEFAULT ' . $t_default );
print_test(
'Converting column ' . $v_table_name . '.' . $v_column_name . ' to BOOLEAN',
2 == $t_dict->ExecuteSQLArray( $t_sqlarray, false ),
true,
print_r( $t_sqlarray, true ) );
if( $g_failed ) {
# Error occured, bail out
break;
}
}
}
}
# End of special processing for specific schema versions
while( ( $i <= $t_last_id ) && !$g_failed ) {
if( !$f_log_queries ) {
echo '<tr><td bgcolor="#ffffff">';
}
$t_sql = true;
$t_target = $g_upgrade[$i][1][0];
switch( $g_upgrade[$i][0] ) {
case 'InsertData':
$t_sqlarray = call_user_func_array( $g_upgrade[$i][0], $g_upgrade[$i][1] );
break;
case 'UpdateSQL':
$t_sqlarray = array(
$g_upgrade[$i][1],
);
$t_target = $g_upgrade[$i][1];
break;
case 'UpdateFunction':
$t_sqlarray = array(
$g_upgrade[$i][1],
);
if( isset( $g_upgrade[$i][2] ) ) {
$t_sqlarray[] = $g_upgrade[$i][2];
}
$t_sql = false;
$t_target = $g_upgrade[$i][1];
break;
case null:
# No-op upgrade step - required for oci8
break;
default:
$t_sqlarray = call_user_func_array( array( $t_dict, $g_upgrade[$i][0] ), $g_upgrade[$i][1] );
# 0: function to call, 1: function params, 2: function to evaluate before calling upgrade, if false, skip upgrade.
if( isset( $g_upgrade[$i][2] ) ) {
if( call_user_func_array( $g_upgrade[$i][2][0], $g_upgrade[$i][2][1] ) ) {
$t_sqlarray = call_user_func_array( array( $t_dict, $g_upgrade[$i][0] ), $g_upgrade[$i][1] );
} else {
$t_sqlarray = array();
}
} else {
$t_sqlarray = call_user_func_array( array( $t_dict, $g_upgrade[$i][0] ), $g_upgrade[$i][1] );
}
break;
}
if( $f_log_queries ) {
if( $t_sql ) {
foreach( $t_sqlarray as $t_sql ) {
# "CREATE OR REPLACE TRIGGER" statements must end with "END;\n/" for Oracle sqlplus
if( $f_db_type == 'oci8' && stripos( $t_sql, 'CREATE OR REPLACE TRIGGER' ) === 0 ) {
$t_sql_end = PHP_EOL . '/';
} else {
$t_sql_end = ';';
}
echo htmlentities( $t_sql ) . $t_sql_end . PHP_EOL . PHP_EOL;
}
}
} else {
echo 'Schema step ' . $i . ': ';
if( is_null( $g_upgrade[$i][0] ) ) {
echo 'No operation';
$t_ret = 2;
} else {
echo $g_upgrade[$i][0] . ' ( ' . $t_target . ' )';
if( $t_sql ) {
$t_ret = $t_dict->ExecuteSQLArray( $t_sqlarray, false );
} else {
if( isset( $t_sqlarray[1] ) ) {