PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/install.php

https://github.com/fusenigk/mantisbt-1
PHP | 1053 lines | 885 code | 100 blank | 68 comment | 221 complexity | 309ac4adb2daeb5cea8b9bb0cbc9a0f3 MD5 | raw file
  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * @package MantisBT
  17. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  18. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  19. * @link http://www.mantisbt.org
  20. */
  21. error_reporting( E_ALL );
  22. /** @todo put this somewhere */
  23. @set_time_limit( 0 );
  24. # Load the MantisDB core in maintenance mode. This mode will assume that
  25. # config_inc.php hasn't been specified. Thus the database will not be opened
  26. # and plugins will not be loaded.
  27. define( 'MANTIS_MAINTENANCE_MODE', true );
  28. @require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' );
  29. require_api( 'install_helper_functions_api.php' );
  30. $g_error_send_page_header = false; # bypass page headers in error handler
  31. $g_failed = false;
  32. $g_database_upgrade = false;
  33. # -------
  34. # print test result
  35. function print_test_result( $p_result, $p_hard_fail = true, $p_message = '' ) {
  36. global $g_failed;
  37. echo '<td ';
  38. if( BAD == $p_result ) {
  39. if( $p_hard_fail ) {
  40. $g_failed = true;
  41. echo 'bgcolor="red">BAD';
  42. } else {
  43. echo 'bgcolor="pink">POSSIBLE PROBLEM';
  44. }
  45. if( '' != $p_message ) {
  46. echo '<br />' . $p_message;
  47. }
  48. }
  49. if( GOOD == $p_result ) {
  50. echo 'bgcolor="green">GOOD';
  51. }
  52. echo '</td>';
  53. }
  54. # -------
  55. # print test header and result
  56. function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_message = '' ) {
  57. echo "\n<tr><td bgcolor=\"#ffffff\">$p_test_description</td>";
  58. print_test_result( $p_result, $p_hard_fail, $p_message );
  59. echo "</tr>\n";
  60. }
  61. # --------
  62. # create an SQLArray to insert data
  63. function InsertData( $p_table, $p_data ) {
  64. $query = "INSERT INTO " . $p_table . $p_data;
  65. return Array( $query );
  66. }
  67. # install_state
  68. # 0 = no checks done
  69. # 1 = server ok, get database information
  70. # 2 = check the database information
  71. # 3 = install the database
  72. # 4 = get additional config file information
  73. # 5 = write the config file
  74. # 6 = post install checks
  75. # 7 = done, link to login or db updater
  76. $t_install_state = gpc_get_int( 'install', 0 );
  77. html_begin();
  78. ?>
  79. <head>
  80. <title> MantisBT Administration - Installation </title>
  81. <link rel="stylesheet" type="text/css" href="admin.css" />
  82. </head>
  83. <body>
  84. <table width="100%" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
  85. <tr class="top-bar">
  86. <td class="links">
  87. [ <a href="index.php">Back to Administration</a> ]
  88. </td>
  89. <td class="title">
  90. <?php
  91. switch( $t_install_state ) {
  92. case 6:
  93. echo "Post Installation Checks";
  94. break;
  95. case 5:
  96. echo "Install Configuration File";
  97. break;
  98. case 4:
  99. echo "Additional Configuration Information";
  100. break;
  101. case 3:
  102. echo "Install Database";
  103. break;
  104. case 2:
  105. echo "Check and Install Database";
  106. break;
  107. case 1:
  108. echo "Database Parameters";
  109. break;
  110. case 0:
  111. default:
  112. echo "Pre-Installation Check";
  113. break;
  114. }
  115. ?>
  116. </td>
  117. </tr>
  118. </table>
  119. <br /><br />
  120. <form method='POST'>
  121. <?php
  122. if( 0 == $t_install_state ) {
  123. ?>
  124. <table width="100%" bgcolor="#222222" cellpadding="10" cellspacing="1">
  125. <tr>
  126. <td bgcolor="#e8e8e8" colspan="2">
  127. <span class="title">Checking Installation...</span>
  128. </td>
  129. </tr>
  130. <?php
  131. }
  132. $t_config_filename = $g_absolute_path . 'config_inc.php';
  133. $t_config_exists = file_exists( $t_config_filename );
  134. $f_hostname = null;
  135. $f_db_type = null;
  136. $f_database_name = null;
  137. $f_db_username = null;
  138. $f_db_password = null;
  139. if( $t_config_exists ) {
  140. if( 0 == $t_install_state ) {
  141. print_test( "Config File Exists - Upgrade", true );
  142. }
  143. # config already exists - probably an upgrade
  144. $f_dsn = config_get( 'dsn', '' );
  145. $f_hostname = config_get( 'hostname', '' );
  146. $f_db_type = config_get( 'db_type', '' );
  147. $f_database_name = config_get( 'database_name', '' );
  148. $f_db_username = config_get( 'db_username', '' );
  149. $f_db_password = config_get( 'db_password', '' );
  150. if( 0 == $t_install_state ) {
  151. print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' );
  152. print_test( 'Checking Database connection settings exist', ( $f_dsn !== '' || ( $f_database_name !== '' && $f_db_username !== '' && $f_hostname !== '' ) ), true, 'database connection settings do not exist?' );
  153. 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.' );
  154. }
  155. $g_db = ADONewConnection( $f_db_type );
  156. $t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
  157. if( $g_db->IsConnected() ) {
  158. $g_db_connected = true;
  159. }
  160. $t_cur_version = config_get( 'database_version', -1 );
  161. if( $t_cur_version > 1 ) {
  162. $g_database_upgrade = true;
  163. $f_db_exists = true;
  164. } else {
  165. if( 0 == $t_install_state ) {
  166. print_test( 'Config File Exists but Database does not', false, false, 'Bad config_inc.php?' );
  167. }
  168. }
  169. } else {
  170. # read control variables with defaults
  171. $f_hostname = gpc_get( 'hostname', config_get( 'hostname', 'localhost' ) );
  172. $f_db_type = gpc_get( 'db_type', config_get( 'db_type', '' ) );
  173. $f_database_name = gpc_get( 'database_name', config_get( 'database_name', 'bugtrack' ) );
  174. $f_db_username = gpc_get( 'db_username', config_get( 'db_username', '' ) );
  175. $f_db_password = gpc_get( 'db_password', config_get( 'db_password', '' ) );
  176. if( CONFIGURED_PASSWORD == $f_db_password ) {
  177. $f_db_password = config_get( 'db_password' );
  178. }
  179. }
  180. $f_admin_username = gpc_get( 'admin_username', '' );
  181. $f_admin_password = gpc_get( 'admin_password', '' );
  182. $f_log_queries = gpc_get_bool( 'log_queries', false );
  183. $f_db_exists = gpc_get_bool( 'db_exists', false );
  184. $f_db_schema = '';
  185. if( $f_db_type == 'db2' ) {
  186. # If schema name is supplied, then separate it from database name.
  187. if( strpos( $f_database_name, '/' ) != false ) {
  188. $f_db2AS400 = $f_database_name;
  189. list( $f_database_name, $f_db_schema ) = explode( '/', $f_db2AS400, 2 );
  190. }
  191. }
  192. if( 0 == $t_install_state ) {
  193. ?>
  194. <!-- Check PHP Version -->
  195. <?php print_test( ' Checking PHP version (your version is ' . phpversion() . ')', check_php_version( phpversion() ), true, 'Upgrade to a more recent version of PHP' );?>
  196. <!-- Check Safe Mode -->
  197. <?php
  198. print_test( 'Checking if safe mode is enabled for install script',
  199. ! ini_get ( 'SAFE_MODE' ),
  200. true,
  201. 'Disable safe_mode in php.ini before proceeding' ) ?>
  202. </table>
  203. <?php
  204. if( false == $g_failed ) {
  205. $t_install_state++;
  206. }
  207. } # end install_state == 0
  208. # got database information, check and install
  209. if( 2 == $t_install_state ) {
  210. ?>
  211. <table width="100%" cellpadding="10" cellspacing="1">
  212. <!-- Setting config variables -->
  213. <?php print_test( 'Setting Database Hostname', '' !== $f_hostname, true, 'host name is blank' )?>
  214. <!-- Setting config variables -->
  215. <?php print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' )?>
  216. <!-- Checking DB support-->
  217. <?php 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.' )?>
  218. <?php print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' )?>
  219. <?php print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' )?>
  220. <?php print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' )?>
  221. <?php
  222. if( $f_db_type == 'db2' ) {
  223. print_test( 'Setting Database Schema', !is_blank( $f_db_schema ), true, 'must have a schema name for AS400 in the form of DBNAME/SCHEMA' );
  224. }
  225. ?>
  226. <tr>
  227. <td bgcolor="#ffffff">
  228. Setting Admin Username
  229. </td>
  230. <?php
  231. if( '' !== $f_admin_username ) {
  232. print_test_result( GOOD );
  233. } else {
  234. print_test_result( BAD, false, 'admin user name is blank, using database user instead' );
  235. $f_admin_username = $f_db_username;
  236. }
  237. ?>
  238. </tr>
  239. <tr>
  240. <td bgcolor="#ffffff">
  241. Setting Admin Password
  242. </td>
  243. <?php
  244. if( '' !== $f_admin_password ) {
  245. print_test_result( GOOD );
  246. } else {
  247. if( '' != $f_db_password ) {
  248. print_test_result( BAD, false, 'admin user password is blank, using database user password instead' );
  249. $f_admin_password = $f_db_password;
  250. } else {
  251. print_test_result( GOOD );
  252. }
  253. }
  254. ?>
  255. </tr>
  256. <!-- connect to db -->
  257. <tr>
  258. <td bgcolor="#ffffff">
  259. Attempting to connect to database as admin
  260. </td>
  261. <?php
  262. $t_db_open = false;
  263. $g_db = ADONewConnection( $f_db_type );
  264. $t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password );
  265. if( $t_result ) {
  266. # check if db exists for the admin
  267. $t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password, $f_database_name );
  268. if( $t_result ) {
  269. $t_db_open = true;
  270. $f_db_exists = true;
  271. }
  272. if( $f_db_type == 'db2' ) {
  273. $result = &$g_db->execute( 'set schema ' . $f_db_schema );
  274. if( $result === false ) {
  275. print_test_result( BAD, true, 'set schema failed: ' . $g_db->errorMsg() );
  276. }
  277. } else {
  278. print_test_result( GOOD );
  279. }
  280. } else {
  281. print_test_result( BAD, true, 'Does administrative user have access to the database? ( ' . db_error_msg() . ' )' );
  282. }
  283. ?>
  284. </tr>
  285. <?php
  286. if( $f_db_exists ) {
  287. ?>
  288. <tr>
  289. <td bgcolor="#ffffff">
  290. Attempting to connect to database as user
  291. </td>
  292. <?php
  293. $g_db = ADONewConnection( $f_db_type );
  294. $t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
  295. if( $t_result == true ) {
  296. $t_db_open = true;
  297. if( $f_db_type == 'db2' ) {
  298. $result = &$g_db->execute( 'set schema ' . $f_db_schema );
  299. if( $result === false ) {
  300. print_test_result( BAD, true, 'set schema failed: ' . $g_db->errorMsg() );
  301. }
  302. } else {
  303. print_test_result( GOOD );
  304. }
  305. } else {
  306. print_test_result( BAD, false, 'Database user doesn\'t have access to the database ( ' . db_error_msg() . ' )' );
  307. }
  308. ?>
  309. </tr>
  310. <?php
  311. }
  312. if( $t_db_open ) {
  313. ?>
  314. <!-- display database version -->
  315. <tr>
  316. <td bgcolor="#ffffff">
  317. Checking Database Server Version
  318. <?php
  319. # due to a bug in ADODB, this call prompts warnings, hence the @
  320. # the check only works on mysql if the database is open
  321. $t_version_info = @$g_db->ServerInfo();
  322. echo '<br /> Running ' . $f_db_type . ' version ' . $t_version_info['description'];
  323. ?>
  324. </td>
  325. <?php
  326. $t_warning = '';
  327. $t_error = '';
  328. switch( $f_db_type ) {
  329. case 'mysql':
  330. case 'mysqli':
  331. if( version_compare( $t_version_info['version'], '4.1.0', '<' ) ) {
  332. $t_error = 'MySQL 4.1.0 or later is required for installation.';
  333. }
  334. break;
  335. case 'pgsql':
  336. case 'mssql':
  337. case 'db2':
  338. default:
  339. break;
  340. }
  341. print_test_result(( '' == $t_error ) && ( '' == $t_warning ), ( '' != $t_error ), $t_error . ' ' . $t_warning );
  342. ?>
  343. </tr>
  344. </table>
  345. <?php
  346. }
  347. if( false == $g_failed ) {
  348. $t_install_state++;
  349. } else {
  350. $t_install_state--; # a check failed, redisplay the questions
  351. }
  352. } # end 2 == $t_install_state
  353. # system checks have passed, get the database information
  354. if( 1 == $t_install_state ) {
  355. ?>
  356. <table width="100%" cellpadding="10" cellspacing="1">
  357. <tr>
  358. <td bgcolor="#e8e8e8" colspan="2">
  359. <span class="title"><?php echo $g_database_upgrade ? 'Upgrade Options' : 'Installation Options'?></span>
  360. </td>
  361. </tr>
  362. <?php if( !$g_database_upgrade ) {?>
  363. <tr>
  364. <td>
  365. Type of Database
  366. </td>
  367. <td>
  368. <select name="db_type">
  369. <?php
  370. if( $f_db_type == 'mysql' ) {
  371. echo '<option value="mysql" selected="selected">MySql (default)</option>';
  372. } else {
  373. echo '<option value="mysql">MySql (default)</option>';
  374. }
  375. if( $f_db_type == 'mysqli' ) {
  376. echo '<option value="mysqli" selected="selected">MySqli</option>';
  377. } else {
  378. echo '<option value="mysqli">MySqli</option>';
  379. }
  380. if( $f_db_type == 'mssql' ) {
  381. echo '<option value="mssql" selected="selected">Microsoft SQL Server (experimental)</option>';
  382. } else {
  383. echo '<option value="mssql">Microsoft SQL Server (experimental)</option>';
  384. }
  385. if( $f_db_type == 'pgsql' ) {
  386. echo '<option value="pgsql" selected="selected">PGSQL (experimental)</option>';
  387. } else {
  388. echo '<option value="pgsql">PGSQL (experimental)</option>';
  389. }
  390. if( $f_db_type == 'oci8' ) {
  391. echo '<option value="oci8" selected="selected">Oracle - oci8 (Experimental)</option>';
  392. } else {
  393. echo '<option value="oci8">Oracle - oci8 (Experimental)</option>';
  394. }
  395. if( $f_db_type == 'db2' ) {
  396. echo '<option value="db2" selected="selected">db2/400 (experimental)</option>';
  397. } else {
  398. echo '<option value="db2">db2/400 (experimental)</option>';
  399. }
  400. ?>
  401. </select>
  402. </td>
  403. </tr>
  404. <?php
  405. }
  406. if( !$g_database_upgrade ) {?>
  407. <tr>
  408. <td>
  409. Hostname (for Database Server)
  410. </td>
  411. <td>
  412. <input name="hostname" type="textbox" value="<?php echo $f_hostname?>"></input>
  413. </td>
  414. </tr>
  415. <?php
  416. }
  417. if( !$g_database_upgrade ) {?>
  418. <tr>
  419. <td>
  420. Username (for Database)
  421. </td>
  422. <td>
  423. <input name="db_username" type="textbox" value="<?php echo $f_db_username?>"></input>
  424. </td>
  425. </tr>
  426. <?php
  427. }
  428. if( !$g_database_upgrade ) {?>
  429. <tr>
  430. <td>
  431. Password (for Database)
  432. </td>
  433. <td>
  434. <input name="db_password" type="password" value="<?php echo( !is_blank( $f_db_password ) ? CONFIGURED_PASSWORD : "" )?>"></input>
  435. </td>
  436. </tr>
  437. <?php
  438. }
  439. if( !$g_database_upgrade ) {?>
  440. <tr>
  441. <td>
  442. Database name (for Database)
  443. </td>
  444. <td>
  445. <input name="database_name" type="textbox" value="<?php echo $f_database_name?>"></input>
  446. </td>
  447. </tr>
  448. <?php
  449. }?>
  450. <tr>
  451. <td>
  452. Admin Username (to <?php echo( !$g_database_upgrade ) ? 'create Database' : 'update Database'?> if required)
  453. </td>
  454. <td>
  455. <input name="admin_username" type="textbox" value="<?php echo $f_admin_username?>"></input>
  456. </td>
  457. </tr>
  458. <tr>
  459. <td>
  460. Admin Password (to <?php echo( !$g_database_upgrade ) ? 'create Database' : 'update Database'?> if required)
  461. </td>
  462. <td>
  463. <input name="admin_password" type="password" value="<?php echo $f_admin_password?>"></input>
  464. </td>
  465. </tr>
  466. <tr>
  467. <td>
  468. Print SQL Queries instead of Writing to the Database
  469. </td>
  470. <td>
  471. <input name="log_queries" type="checkbox" value="1" <?php echo( $f_log_queries ? 'checked="checked"' : '' )?>></input>
  472. </td>
  473. </tr>
  474. <tr>
  475. <td>
  476. Attempt Installation
  477. </td>
  478. <td>
  479. <input name="go" type="submit" value="Install/Upgrade Database"></input>
  480. </td>
  481. </tr>
  482. <input name="install" type="hidden" value="2"></input>
  483. </table>
  484. <?php
  485. } # end install_state == 1
  486. # all checks have passed, install the database
  487. if( 3 == $t_install_state ) {
  488. ?>
  489. <table width="100%" cellpadding="10" cellspacing="1">
  490. <tr>
  491. <td bgcolor="#e8e8e8" colspan="2">
  492. <span class="title">Installing Database</span>
  493. </td>
  494. </tr>
  495. <?php if( !$f_log_queries ) {?>
  496. <tr>
  497. <td bgcolor="#ffffff">
  498. Create database if it does not exist
  499. </td>
  500. <?php
  501. $t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password, $f_database_name );
  502. if( $f_db_type == 'db2' ) {
  503. $rs = $g_db->Execute( "select * from SYSIBM.SCHEMATA WHERE SCHEMA_NAME = '" . $f_db_schema . "' AND SCHEMA_OWNER = '" . $f_db_username . "'" );
  504. if( $rs === false ) {
  505. echo "<br />false";
  506. }
  507. if( $rs->EOF ) {
  508. $t_result = false;
  509. echo $g_db->errorMsg();
  510. } else {
  511. $t_result = &$g_db->execute( 'set schema ' . $f_db_schema );
  512. }
  513. }
  514. $t_db_open = false;
  515. if( $t_result == true ) {
  516. print_test_result( GOOD );
  517. $t_db_open = true;
  518. } else {
  519. // create db
  520. $g_db = ADONewConnection( $f_db_type );
  521. $t_result = $g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password );
  522. $dict = NewDataDictionary( $g_db );
  523. if( $f_db_type == 'db2' ) {
  524. $rs = &$g_db->Execute( "CREATE SCHEMA " . $f_db_schema );
  525. if( !$rs ) {
  526. $t_result = false;
  527. print_test_result( BAD, true, 'Does administrative user have access to create the database? ( ' . db_error_msg() . ' )' );
  528. $t_install_state--; # db creation failed, allow user to re-enter user/password info
  529. } else {
  530. print_test_result( GOOD );
  531. $t_db_open = true;
  532. }
  533. } else {
  534. $sqlarray = $dict->CreateDatabase( $f_database_name, Array( 'mysql' => 'DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci' ) );
  535. $ret = $dict->ExecuteSQLArray( $sqlarray );
  536. if( $ret == 2 ) {
  537. print_test_result( GOOD );
  538. $t_db_open = true;
  539. } else {
  540. $t_error = db_error_msg();
  541. if( strstr( $t_error, 'atabase exists' ) ) {
  542. print_test_result( BAD, false, 'Database already exists? ( ' . db_error_msg() . ' )' );
  543. } else {
  544. print_test_result( BAD, true, 'Does administrative user have access to create the database? ( ' . db_error_msg() . ' )' );
  545. $t_install_state--; # db creation failed, allow user to re-enter user/password info
  546. }
  547. }
  548. }
  549. }
  550. ?>
  551. </tr>
  552. <?php
  553. if( $t_db_open ) {
  554. ?>
  555. <!-- display database version -->
  556. <tr>
  557. <td bgcolor="#ffffff">
  558. Checking Database Server Version
  559. <?php
  560. # due to a bug in ADODB, this call prompts warnings, hence the @
  561. $t_version_info = @$g_db->ServerInfo();
  562. echo '<br /> Running ' . $f_db_type . ' version ' . $t_version_info['description'];
  563. ?>
  564. </td>
  565. <?php
  566. $t_warning = '';
  567. $t_error = '';
  568. switch( $f_db_type ) {
  569. case 'mysql':
  570. case 'mysqli':
  571. if( version_compare( $t_version_info['version'], '4.1.0', '<' ) ) {
  572. $t_error = 'MySQL 4.1.0 or later is required for installation.';
  573. }
  574. break;
  575. case 'pgsql':
  576. case 'mssql':
  577. case 'db2':
  578. default:
  579. break;
  580. }
  581. print_test_result(( '' == $t_error ) && ( '' == $t_warning ), ( '' != $t_error ), $t_error . ' ' . $t_warning );
  582. ?>
  583. </tr>
  584. <?php
  585. }
  586. $g_db->Close();
  587. ?>
  588. <tr>
  589. <td bgcolor="#ffffff">
  590. Attempting to connect to database as user
  591. </td>
  592. <?php
  593. $g_db = ADONewConnection( $f_db_type );
  594. $t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
  595. if( $f_db_type == 'db2' ) {
  596. $result = &$g_db->execute( 'set schema ' . $f_db_schema );
  597. if( $result === false ) {
  598. echo $g_db->errorMsg();
  599. }
  600. }
  601. if( $t_result == true ) {
  602. print_test_result( GOOD );
  603. } else {
  604. print_test_result( BAD, false, 'Database user doesn\'t have access to the database ( ' . db_error_msg() . ' )' );
  605. }
  606. $g_db->Close();
  607. ?>
  608. </tr>
  609. <?php
  610. }
  611. # install the tables
  612. if( false == $g_failed ) {
  613. $g_db_connected = false;
  614. # fake out database access routines used by config_get
  615. $GLOBALS['g_db_type'] = $f_db_type;
  616. # database_api references this
  617. require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'schema.php' );
  618. $g_db = ADONewConnection( $f_db_type );
  619. $t_result = @$g_db->Connect( $f_hostname, $f_admin_username, $f_admin_password, $f_database_name );
  620. if( !$f_log_queries ) {
  621. $g_db_connected = true;
  622. # fake out database access routines used by config_get
  623. }
  624. $t_last_update = config_get( 'database_version', -1, ALL_USERS, ALL_PROJECTS );
  625. $lastid = count( $upgrade ) - 1;
  626. $i = $t_last_update + 1;
  627. if( $f_log_queries ) {
  628. echo '<tr><td bgcolor="#ffffff" col_span="2"> Database Creation Suppressed, SQL Queries follow <pre>';
  629. }
  630. # Make sure we do the upgrades using UTF-8 if needed
  631. if ( $f_db_type === 'mysql' || $f_db_type === 'mysqli' ) {
  632. $g_db->execute( 'SET NAMES UTF8' );
  633. }
  634. if( $f_db_type == 'db2' ) {
  635. $result = &$g_db->execute( 'set schema ' . $f_db_schema );
  636. if( $result === false ) {
  637. echo $g_db->errorMsg();
  638. }
  639. }
  640. while(( $i <= $lastid ) && !$g_failed ) {
  641. if( !$f_log_queries ) {
  642. echo '<tr><td bgcolor="#ffffff">';
  643. }
  644. $dict = NewDataDictionary( $g_db );
  645. $t_sql = true;
  646. $t_target = $upgrade[$i][1][0];
  647. if( $upgrade[$i][0] == 'InsertData' ) {
  648. $sqlarray = call_user_func_array( $upgrade[$i][0], $upgrade[$i][1] );
  649. }
  650. else if( $upgrade[$i][0] == 'UpdateSQL' ) {
  651. $sqlarray = array(
  652. $upgrade[$i][1],
  653. );
  654. $t_target = $upgrade[$i][1];
  655. } else if( $upgrade[$i][0] == 'UpdateFunction' ) {
  656. $sqlarray = array(
  657. $upgrade[$i][1],
  658. );
  659. if( isset( $upgrade[$i][2] ) ) {
  660. $sqlarray[] = $upgrade[$i][2];
  661. }
  662. $t_sql = false;
  663. $t_target = $upgrade[$i][1];
  664. } else {
  665. /* 0: function to call, 1: function params, 2: function to evaluate before calling upgrade, if false, skip upgrade. */
  666. if( isset( $upgrade[$i][2] ) ) {
  667. if( call_user_func_array( $upgrade[$i][2][0], $upgrade[$i][2][1] ) ) {
  668. $sqlarray = call_user_func_array( Array( $dict, $upgrade[$i][0] ), $upgrade[$i][1] );
  669. } else {
  670. $sqlarray = array();
  671. }
  672. } else {
  673. $sqlarray = call_user_func_array( Array( $dict, $upgrade[$i][0] ), $upgrade[$i][1] );
  674. }
  675. }
  676. if( $f_log_queries ) {
  677. if( $t_sql ) {
  678. foreach( $sqlarray as $sql ) {
  679. echo htmlentities( $sql ) . ";\r\n\r\n";
  680. }
  681. }
  682. } else {
  683. echo 'Schema ' . $upgrade[$i][0] . ' ( ' . $t_target . ' )</td>';
  684. if( $t_sql ) {
  685. $ret = $dict->ExecuteSQLArray( $sqlarray );
  686. } else {
  687. if( isset( $sqlarray[1] ) ) {
  688. $ret = call_user_func( 'install_' . $sqlarray[0], $sqlarray[1] );
  689. } else {
  690. $ret = call_user_func( 'install_' . $sqlarray[0] );
  691. }
  692. }
  693. if( $ret == 2 ) {
  694. print_test_result( GOOD );
  695. config_set( 'database_version', $i );
  696. } else {
  697. $all_sql = '';
  698. foreach ( $sqlarray as $single_sql )
  699. $all_sql .= $single_sql . '<br />';
  700. print_test_result( BAD, true, $all_sql . $g_db->ErrorMsg() );
  701. }
  702. echo '</tr>';
  703. }
  704. $i++;
  705. }
  706. if( $f_log_queries ) {
  707. # add a query to set the database version
  708. echo 'INSERT INTO ' . db_get_table( 'config' ) . ' ( value, type, access_reqd, config_id, project_id, user_id ) VALUES (\'' . $lastid . '\', 1, 90, \'database_version\', 0, 0 );' . "\r\n";
  709. echo '</pre><br /><p style="color:red">Your database has not been created yet. Please create the database, then install the tables and data using the information above before proceeding.</p></td></tr>';
  710. }
  711. }
  712. if( false == $g_failed ) {
  713. $t_install_state++;
  714. } else {
  715. $t_install_state--;
  716. }
  717. ?>
  718. </table>
  719. <?php
  720. } # end install_state == 3
  721. # database installed, get any additional information
  722. if( 4 == $t_install_state ) {
  723. /** @todo to be written */
  724. // must post data gathered to preserve it
  725. ?>
  726. <input name="hostname" type="hidden" value="<?php echo $f_hostname?>"></input>
  727. <input name="db_type" type="hidden" value="<?php echo $f_db_type?>"></input>
  728. <input name="database_name" type="hidden" value="<?php echo $f_database_name?>"></input>
  729. <input name="db_username" type="hidden" value="<?php echo $f_db_username?>"></input>
  730. <input name="db_password" type="hidden" value="<?php echo $f_db_password?>"></input>
  731. <input name="admin_username" type="hidden" value="<?php echo $f_admin_username?>"></input>
  732. <input name="admin_password" type="hidden" value="<?php echo $f_admin_password?>"></input>
  733. <input name="log_queries" type="hidden" value="<?php echo( $f_log_queries ? 1 : 0 )?>"></input>
  734. <input name="db_exists" type="hidden" value="<?php echo( $f_db_exists ? 1 : 0 )?>"></input>
  735. <?php
  736. # must post <input name="install" type="hidden" value="5"></input>
  737. # rather than the following line
  738. $t_install_state++;
  739. } # end install_state == 4
  740. # all checks have passed, install the database
  741. if( 5 == $t_install_state ) {
  742. $t_config_filename = $g_absolute_path . 'config_inc.php';
  743. $t_config_exists = file_exists( $t_config_filename );
  744. ?>
  745. <table width="100%" cellpadding="10" cellspacing="1">
  746. <tr>
  747. <td bgcolor="#e8e8e8" colspan="2">
  748. <span class="title">Write Configuration File(s)</span>
  749. </td>
  750. </tr>
  751. <tr>
  752. <td bgcolor="#ffffff">
  753. <?php
  754. if( !$t_config_exists ) {
  755. echo 'Creating Configuration File (config_inc.php)<br />';
  756. echo '<span class="error-msg">(if this file is not created, create it manually with the contents below)</span>';
  757. } else {
  758. echo 'Updating Configuration File (config_inc.php)<br />';
  759. }
  760. ?>
  761. </td>
  762. <?php
  763. $t_config = '<?php' . "\r\n";
  764. $t_config .= "\t\$g_hostname = '$f_hostname';\r\n";
  765. $t_config .= "\t\$g_db_type = '$f_db_type';\r\n";
  766. $t_config .= "\t\$g_database_name = '$f_database_name';\r\n";
  767. $t_config .= "\t\$g_db_username = '$f_db_username';\r\n";
  768. $t_config .= "\t\$g_db_password = '$f_db_password';\r\n";
  769. if( $f_db_type == 'db2' ) {
  770. $t_config .= "\t\$g_db_schema = '$f_db_schema';\r\n";
  771. }
  772. $t_config .= "\r\n";
  773. $t_write_failed = true;
  774. if( !$t_config_exists ) {
  775. if( $fd = @fopen( $t_config_filename, 'w' ) ) {
  776. fwrite( $fd, $t_config );
  777. fclose( $fd );
  778. }
  779. if( file_exists( $t_config_filename ) ) {
  780. print_test_result( GOOD );
  781. $t_write_failed = false;
  782. } else {
  783. print_test_result( BAD, false, 'cannot write ' . $t_config_filename );
  784. }
  785. } else {
  786. # already exists, see if the information is the same
  787. if ( ( $f_hostname != config_get( 'hostname', '' ) ) ||
  788. ( $f_db_type != config_get( 'db_type', '' ) ) ||
  789. ( $f_database_name != config_get( 'database_name', '') ) ||
  790. ( $f_db_schema != config_get( 'db_schema', '') ) ||
  791. ( $f_db_username != config_get( 'db_username', '' ) ) ||
  792. ( $f_db_password != config_get( 'db_password', '' ) ) ) {
  793. print_test_result( BAD, false, 'file ' . $g_absolute_path . 'config_inc.php' . ' already exists and has different settings' );
  794. } else {
  795. print_test_result( GOOD, false );
  796. $t_write_failed = false;
  797. }
  798. }
  799. ?>
  800. </tr>
  801. <?php
  802. if( true == $t_write_failed ) {
  803. echo '<tr><table width="50%" cellpadding="10" cellspacing="1">';
  804. echo '<tr><td>Please add the following lines to ' . $g_absolute_path . 'config_inc.php before continuing to the database upgrade check:</td></tr>';
  805. echo '<tr><td><pre>' . htmlentities( $t_config ) . '</pre></td></tr></table></tr>';
  806. }
  807. ?>
  808. </table>
  809. <?php
  810. if( false == $g_failed ) {
  811. $t_install_state++;
  812. }
  813. }
  814. # end install_state == 5
  815. if( 6 == $t_install_state ) {
  816. # post install checks
  817. ?>
  818. <table width="100%" bgcolor="#222222" cellpadding="10" cellspacing="1">
  819. <tr>
  820. <td bgcolor="#e8e8e8" colspan="2">
  821. <span class="title">Checking Installation...</span>
  822. </td>
  823. </tr>
  824. <!-- Checking register_globals are off -->
  825. <?php print_test( 'Checking for register_globals are off for mantis', !ini_get_bool( 'register_globals' ), false, 'change php.ini to disable register_globals setting' )?>
  826. <tr>
  827. <td bgcolor="#ffffff">
  828. Attempting to connect to database as user
  829. </td>
  830. <?php
  831. $g_db = ADONewConnection( $f_db_type );
  832. $t_result = @$g_db->Connect( $f_hostname, $f_db_username, $f_db_password, $f_database_name );
  833. if( $t_result == true ) {
  834. print_test_result( GOOD );
  835. } else {
  836. print_test_result( BAD, false, 'Database user doesn\'t have access to the database ( ' . db_error_msg() . ' )' );
  837. }
  838. if( $f_db_type == 'db2' ) {
  839. $result = &$g_db->execute( 'set schema ' . $f_db_schema );
  840. if( $result === false ) {
  841. echo $g_db->errorMsg();
  842. }
  843. }
  844. ?>
  845. </tr>
  846. <tr>
  847. <td bgcolor="#ffffff">
  848. checking ability to SELECT records
  849. </td>
  850. <?php
  851. $t_mantis_config_table = db_get_table( 'config' );
  852. $t_query = "SELECT COUNT(*) FROM $t_mantis_config_table";
  853. $t_result = @$g_db->Execute( $t_query );
  854. if( $t_result != false ) {
  855. print_test_result( GOOD );
  856. } else {
  857. print_test_result( BAD, true, 'Database user doesn\'t have SELECT access to the database ( ' . db_error_msg() . ' )' );
  858. }
  859. ?>
  860. </tr>
  861. <tr>
  862. <td bgcolor="#ffffff">
  863. checking ability to INSERT records
  864. </td>
  865. <?php
  866. $t_query = "INSERT INTO $t_mantis_config_table ( value, type, access_reqd, config_id, project_id, user_id ) VALUES ('test', 1, 90, 'database_test', 20, 0 )";
  867. $t_result = @$g_db->Execute( $t_query );
  868. if( $t_result != false ) {
  869. print_test_result( GOOD );
  870. } else {
  871. print_test_result( BAD, true, 'Database user doesn\'t have INSERT access to the database ( ' . db_error_msg() . ' )' );
  872. }
  873. ?>
  874. </tr>
  875. <tr>
  876. <td bgcolor="#ffffff">
  877. checking ability to UPDATE records
  878. </td>
  879. <?php
  880. $t_query = "UPDATE $t_mantis_config_table SET value='test_update' WHERE config_id='database_test'";
  881. $t_result = @$g_db->Execute( $t_query );
  882. if( $t_result != false ) {
  883. print_test_result( GOOD );
  884. } else {
  885. print_test_result( BAD, true, 'Database user doesn\'t have UPDATE access to the database ( ' . db_error_msg() . ' )' );
  886. }
  887. ?>
  888. </tr>
  889. <tr>
  890. <td bgcolor="#ffffff">
  891. checking ability to DELETE records
  892. </td>
  893. <?php
  894. $t_query = "DELETE FROM $t_mantis_config_table WHERE config_id='database_test'";
  895. $t_result = @$g_db->Execute( $t_query );
  896. if( $t_result != false ) {
  897. print_test_result( GOOD );
  898. } else {
  899. print_test_result( BAD, true, 'Database user doesn\'t have DELETE access to the database ( ' . db_error_msg() . ' )' );
  900. }
  901. ?>
  902. </tr>
  903. </table>
  904. <?php
  905. if( false == $g_failed ) {
  906. $t_install_state++;
  907. }
  908. }
  909. # end install_state == 6
  910. if( 7 == $t_install_state ) {
  911. # cleanup and launch upgrade
  912. ?>
  913. <p>Install was successful.</p>
  914. <?php if( $f_db_exists ) {?>
  915. <p><a href="../login_page.php">Continue</a> to log into Mantis</p>
  916. <?php
  917. } else {?>
  918. <p>Please log in as the administrator and <a href="../manage_proj_create_page.php">create</a> your first project.
  919. <?php
  920. }
  921. }
  922. # end install_state == 7
  923. if( $g_failed ) {
  924. ?>
  925. <table width="100%" bgcolor="#222222" cellpadding="10" cellspacing="1">
  926. <tr>
  927. <td bgcolor="#e8e8e8" colspan="2">
  928. <span class="title">Checks Failed...</span>
  929. </td>
  930. </tr>
  931. <tr>
  932. <td bgcolor="#ffffff">Please correct failed checks</td>
  933. <td bgcolor="#ffffff">
  934. <input name="install" type="hidden" value="<?php echo $t_install_state?>"></input>
  935. <input name="hostname" type="hidden" value="<?php echo $f_hostname?>"></input>
  936. <input name="db_type" type="hidden" value="<?php echo $f_db_type?>"></input>
  937. <input name="database_name" type="hidden" value="<?php echo $f_database_name?>"></input>
  938. <input name="db_username" type="hidden" value="<?php echo $f_db_username?>"></input>
  939. <input name="db_password" type="hidden" value="<?php echo $f_db_password?>"></input>
  940. <input name="admin_username" type="hidden" value="<?php echo $f_admin_username?>"></input>
  941. <input name="admin_password" type="hidden" value="<?php echo $f_admin_password?>"></input>
  942. <input name="log_queries" type="hidden" value="<?php echo( $f_log_queries ? 1 : 0 )?>"></input>
  943. <input name="retry" type="submit" value="Retry"></input>
  944. <input name="db_exists" type="hidden" value="<?php echo( $f_db_exists ? 1 : 0 )?>"></input>
  945. </td>
  946. </tr>
  947. </table>
  948. <?php
  949. }
  950. ?>
  951. </form>
  952. </body>
  953. </html>