made a copy
[atutor.git] / install / include / step2.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg, Heidi Hazelton */
6 /* http://atutor.ca                                                                                                             */
7 /*                                                                                                                                              */
8 /* This program is free software. You can redistribute it and/or                */
9 /* modify it under the terms of the GNU General Public License                  */
10 /* as published by the Free Software Foundation.                                                */
11 /************************************************************************/
12 // $Id$
13
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16 if(isset($_POST['submit'])) {
17         unset($errors);
18         unset($progress);
19
20         //check DB & table connection
21
22         $db = @mysql_connect($_POST['db_host'] . ':' . $_POST['db_port'], $_POST['db_login'], $_POST['db_password']);
23         if (!$db) {
24                 $errors[] = 'Unable to connect to database server.';
25         } else {
26                 // check mysql version number
27                 $sql = "SELECT VERSION() AS version";
28                 $result = mysql_query($sql, $db);
29                 $row = mysql_fetch_assoc($result);
30                 $row['version'] = str_replace (array('-community-nt', '-max', '-standard'), '', strtolower($row['version']));
31                 if (version_compare($row['version'], '4.1.10', '>=') === FALSE) {
32                         $errors[] = 'MySQL version '.$row['version'].' was detected. ATutor requires version 4.1.10 or later.';
33                 }
34
35                 if (!isset($errors)){
36                         if (!mysql_select_db($_POST['db_name'], $db)) {
37                                 $sql = "CREATE DATABASE `$_POST[db_name]` CHARACTER SET utf8 COLLATE utf8_general_ci";
38                                 $result = mysql_query($sql, $db);
39                                 if (!$result) {
40                                         $errors[] = 'Unable to select or create database <b>'.$_POST['db_name'].'</b>.';
41                                 } else {
42                                         $progress[] = 'Database <b>'.$_POST['db_name'].'</b> created successfully.';
43                                         mysql_select_db($_POST['db_name'], $db);
44                                 }
45                         } else {
46                                 /* Check if the database that existed is in UTF-8, if not, ask for retry */
47                                 $sql = "SHOW CREATE DATABASE `$_POST[db_name]`";
48                                 $result = mysql_query($sql, $db);
49                                 $row = mysql_fetch_assoc($result);
50                                 
51                                 if (!preg_match('/CHARACTER SET utf8/i', $row['Create Database'])){
52                                         $sql2 = 'ALTER DATABASE `'.$_POST['db_name'].'` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci';
53                                         $result2 = mysql_query($sql2);
54                                         if (!$result2){
55                                                 $errors[] = 'Database <b>'.$_POST['db_name'].'</b> is not in UTF8.  Please set the database character set to UTF8 before continuing by using the following query: <br /> ALTER DATABASE `'.$_POST['db_name'].'` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci.  <br />To use ALTER DATABASE, you need the ALTER privilege on the database.  You can also check the MySQL manual <a href="http://dev.mysql.com/doc/refman/4.1/en/alter-database.html" target="mysql_window">here</a>.';
56                                         }
57                                 }
58                         }
59                 }
60
61                 if (!isset($errors)) {
62                         $progress[] = 'Connected to database <b>'.$_POST['db_name'].'</b> successfully.';
63                         $errors = array();
64                         
65                         /* @See include/classes/dbmanager.php */
66                         queryFromFile('db/atutor_schema.sql');
67                         queryFromFile('db/atutor_language_text.sql');
68
69                         if (!$errors) {
70                                 print_progress($step);
71
72                                 unset($_POST['submit']);
73                                 unset($_POST['action']);
74                                 store_steps($step);
75                                 print_feedback($progress);
76
77                                 echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="form">
78                                 <input type="hidden" name="step" value="3" />';
79                                 print_hidden(3);
80                                 echo '<p align="center"><input type="submit" class="button" value="Next &raquo; " name="submit" /></p></form>';
81                                 return;
82                         }
83                 }
84
85         }
86 }
87
88 print_progress($step);
89
90
91 echo '<p>Please enter your database information: </p>';
92
93
94 if (isset($progress)) {
95         print_feedback($progress);
96 }
97
98 if (isset($errors)) {
99         print_errors($errors);
100 }
101
102 ?>
103
104 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
105         <input type="hidden" name="action" value="process" />
106         <input type="hidden" name="step" value="2" />
107         <input type="hidden" name="new_version" value="<?php echo $_POST['new_version']; ?>" />
108
109         <table width="65%" class="tableborder" cellspacing="0" cellpadding="1" border="0" align="center">
110         <tr>
111                 <td class="row1"><div class="required" title="Required Field">*</div><b><label for="db">Database Hostname:</label></b><br />
112                         Hostname of the database server. Default: <kbd>localhost</kbd></td>
113                 <td class="row1" valign="middle"><input type="text" name="db_host" id="db" value="<?php if (!empty($_POST['db_host'])) { echo stripslashes(htmlspecialchars($_POST['db_host'])); } else { echo 'localhost'; } ?>" class="formfield" /></td>
114         </tr>
115         <tr>
116                 <td class="row1"><div class="required" title="Required Field">*</div><b><label for="port">Database Port:</label></b><br />
117                         The port to the database server. Default: <kbd>3306</kbd></td>
118                 <td class="row1"><input type="text" name="db_port" id="port" value="<?php if (!empty($_POST['db_port'])) { echo stripslashes(htmlspecialchars($_POST['db_port'])); } else { echo '3306'; } ?>" class="formfield" /></td>
119         </tr>
120         <tr>
121                 <td class="row1"><div class="required" title="Required Field">*</div><b><label for="username">Database Username:</label></b><br />
122                         The username to the database server.</td>
123                 <td class="row1"><input type="text" name="db_login" id="username" value="<?php echo stripslashes(htmlspecialchars($_POST['db_login'])); ?>" class="formfield" /></td>
124         </tr>
125         <tr>
126                 <td class="row1"><div class="required" title="Required Field">*</div><b><label for="pass">Database Password:</label></b><br />
127                         The password to the database server.</td>
128                 <td class="row1"><input type="text" name="db_password" id="pass" value="<?php echo stripslashes(htmlspecialchars($_POST['db_password'])); ?>" class="formfield" /></td>
129         </tr>
130         <tr>
131                 <td class="row1"><div class="required" title="Required Field">*</div><b><label for="name">Database Name:</label></b><br />
132                         The name of the database to use. It will be created if it does not exist.<br />Default: <kbd>atutor</kbd></td>
133                 <td class="row1"><input type="text" name="db_name" id="name" value="<?php if (!empty($_POST['db_name'])) { echo stripslashes(htmlspecialchars($_POST['db_name'])); } else { echo 'atutor'; } ?>" class="formfield" /></td>
134         </tr>
135         <tr>
136                 <td class="row1"><div class="optional" title="Optional Field">?</div><b><label for="prefix">Table Prefix:</label></b><br />
137                         The prefix to add to table names to avoid conflicts with existing tables.<br />
138                         Default: <kbd>AT_</kbd></td>
139                 <td class="row1"><input type="text" name="tb_prefix" id="prefix" value="<?php if (!empty($_POST['tb_prefix'])) { echo stripslashes(htmlspecialchars($_POST['tb_prefix'])); } else { echo 'AT_'; } ?>" class="formfield" /></td>
140         </tr>
141         </table>
142
143         <br /><br /><p align="center"><input type="submit" class="button" value="Next &raquo; " name="submit" /></p>
144
145 </form>