[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/admin/ -> install.php (source)

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


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7