made a copy
[atutor.git] / install / include / ustep2.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 ignore_user_abort(true); 
15 @set_time_limit(0); 
16
17 if (!defined('AT_INCLUDE_PATH')) { exit; }
18
19 function update_one_ver($up_file) {
20         global $progress;
21         $update_file = implode('_',$up_file);
22         queryFromFile('db/'.$update_file.'sql');
23         //$progress[] = 'Successful update from version '.$up_file[2].' to '.$up_file[4];
24         return $up_file[4];
25 }
26
27 $_POST['db_login'] = urldecode($_POST['db_login']);
28 $_POST['db_password'] = urldecode($_POST['db_password']);
29
30         unset($errors);
31
32         //check DB & table connection
33
34         $db = @mysql_connect($_POST['db_host'] . ':' . $_POST['db_port'], $_POST['db_login'], urldecode($_POST['db_password']));
35
36         if (!$db) {
37                 $error_no = mysql_errno();
38                 if ($error_no == 2005) {
39                         $errors[] = 'Unable to connect to database server. Database with hostname '.$_POST['db_host'].' not found.';
40                 } else {
41                         $errors[] = 'Unable to connect to database server. Wrong username/password combination.';
42                 }
43         } else {
44                 if (!mysql_select_db($_POST['db_name'], $db)) {
45                         $errors[] = 'Unable to connect to database <b>'.$_POST['db_name'].'</b>.';
46                 }
47
48                 $sql = "SELECT VERSION() AS version";
49                 $result = mysql_query($sql, $db);
50                 $row = mysql_fetch_assoc($result);
51                 if (version_compare($row['version'], '4.0.2', '>=') === FALSE) {
52                         $errors[] = 'MySQL version '.$row['version'].' was detected. ATutor requires version 4.0.2 or later.';
53                 }
54
55                 if (!$errors) {
56                         $progress[] = 'Connected to database <b>'.$_POST['db_name'].'</b> successfully.';
57                         unset($errors);
58
59                         //Save all the course primary language into session variables iff it has not been set. 
60                         if (!isset($_SESSION['course_info'])){
61                                 $sql = "SELECT a.course_id, a.title, l.language_code, l.char_set FROM ".$_POST['tb_prefix']."courses a left join ".$_POST['tb_prefix']."languages l ON l.language_code = a.primary_language";
62                                 $result = mysql_query($sql, $db);
63                                 while ($row = mysql_fetch_assoc($result)){
64                                         $_SESSION['course_info'][$row['course_id']] = array('char_set'=>$row['char_set'], 'language_code'=>$row['language_code']);
65                                 }
66                         }
67
68                         $sql = "DELETE FROM ".$_POST['tb_prefix']."language_text WHERE `variable`<>'_c_template' AND `variable`<>'_c_msgs'";
69                         @mysql_query($sql, $db);
70
71                         $sql = "DELETE FROM ".$_POST['tb_prefix']."languages WHERE language_code<>'en'";
72                         @mysql_query($sql, $db);
73
74                         //get list of all update scripts minus sql extension
75                         $files = scandir('db'); 
76                         foreach ($files as $file) {
77                                 if(count($file = explode('_',$file))==5) {
78                                         $file[4] = substr($file[4],0,-3);
79                                         $update_files[$file[2]] = $file;
80                                 }
81                         }
82                         
83                         $curr_ver = $_POST['old_version'];
84                         ksort($update_files);
85                         foreach ($update_files as $up_file) {
86                                 if(version_compare($curr_ver, $up_file[4], '<')) {
87                                         update_one_ver($up_file);
88                                 }
89                         }
90                         
91                         /* reset all the accounts to English */
92                         $sql = "UPDATE ".$_POST['tb_prefix']."members SET language='en', creation_date=creation_date, last_login=last_login";
93                         @mysql_query($sql, $db);
94
95                         /* set all the courses to 'en' as primary language if empty. added 1.4.1 */
96                         $sql = "UPDATE ".$_POST['tb_prefix']."courses SET primary_language='en' WHERE primary_language=''";
97                         @mysql_query($sql, $db);
98
99                         queryFromFile('db/atutor_language_text.sql');
100
101                         if (!$errors) {
102                                 print_progress($step);
103
104                                 unset($_POST['submit']);
105                                 store_steps(1);
106                                 print_feedback($progress);
107
108                                 echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="form">
109                                 <input type="hidden" name="step" value="3" />
110                                 <input type="hidden" name="upgrade_action" value="true" />';
111                                 echo '<input type="hidden" name="db_login" value="'.urlencode($_POST['db_login']).'" />';
112                                 echo '<input type="hidden" name="db_password" value="'.urlencode($_POST['db_password']).'" />';
113                                 echo '<input type="hidden" name="db_host" value="'.$_POST['db_host'].'" />';
114                                 echo '<input type="hidden" name="db_name" value="'.$_POST['db_name'].'" />';
115                                 echo '<input type="hidden" name="db_port" value="'.$_POST['db_port'].'" />';
116                                 echo '<input type="hidden" name="tb_prefix" value="'.$_POST['tb_prefix'].'" />';
117                                 echo '<input type="hidden" name="old_version" value="'.$_POST['old_version'].'" />';
118                                 echo '<input type="hidden" name="new_version" value="'.$_POST['new_version'].'" />';
119                                 print_hidden(2);
120                                 echo '<p align="center"><input type="submit" class="button" value=" Next &raquo; " name="submit" /></p></form>';
121                                 return;
122                         }
123                 }
124         }
125
126         print_progress($step);
127
128         unset($_POST['submit']);
129         if (isset($progress)) {
130                 print_feedback($progress);
131         }
132
133         if (isset($errors)) {
134                 print_errors($errors);
135         }
136
137
138         echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="form">
139         <input type="hidden" name="step" value="2" />';
140         store_steps(1);
141         print_hidden(2);
142         
143         if ($found_lang) {
144 ?>
145 <table width="60%" class="tableborder" cellspacing="0" cellpadding="1" border="0" align="center">
146 <tr>
147         <td colspan="2" class="row1"><p><small>All installed language packs and changes made to the default English language will be deleted. You will have to re-install any language packs by downloading the latest versions from ATutor.ca. Some language packs may not currently be available.</small></p></td>
148 </tr>
149 <tr>
150         <td class="row1"><small><b><label for="dir">Continue with the upgrade?</label></b></small></td>
151                 <td class="row1" valign="middle" nowrap="nowrap"><input type="radio" name="override" value="1" id="c2" /><label for="c2">Yes, Continue</label>, <input type="radio" name="override" value="0" id="c1" checked="checked" /><label for="c1">No, Cancel</label></td>
152 </tr>
153 </table><br />
154         <?php
155         }
156
157         echo '<input type="hidden" name="db_login" value="'.urlencode($_POST['db_login']).'" />';
158         echo '<input type="hidden" name="db_password" value="'.urlencode($_POST['db_password']).'" />';
159         echo '<input type="hidden" name="db_host" value="'.$_POST['db_host'].'" />';
160         echo '<input type="hidden" name="db_name" value="'.$_POST['db_name'].'" />';
161         echo '<input type="hidden" name="db_port" value="'.$_POST['db_port'].'" />';
162         echo '<input type="hidden" name="tb_prefix" value="'.$_POST['tb_prefix'].'" />';
163         echo '<input type="hidden" name="old_version" value="'.$_POST['old_version'].'" />';
164         echo '<input type="hidden" name="new_version" value="'.$_POST['new_version'].'" />';
165
166         echo '<p align="center"><input type="submit" class="button" value=" Retry " name="submit" /></p></form>';
167         return;
168 ?>