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