SVN tag for release should only contain scripts in "docs" folder without "mods",...
[atutor.git] / install / include / common.inc.php
1 <?php
2 if (!defined('AT_INCLUDE_PATH')) { exit; }
3 error_reporting(E_ALL ^ E_NOTICE);
4 @set_time_limit(0);
5
6 /* atutor default configuration options */
7 /* used on: ustep1.php, step3.php, step5.php */
8 $_defaults['admin_username'] = ($_POST['old_path'] ? 'admin' : '');
9 $_defaults['admin_password'] = '';
10 $_defaults['admin_email'] = '';
11
12 $_defaults['site_name'] = 'Course Server';
13 $_defaults['header_img'] = '';
14 $_defaults['header_logo'] = '';
15 $_defaults['home_url'] = '';
16
17 $_defaults['email_notification'] = 'TRUE';
18 $_defaults['email_confirmation'] = 'TRUE';
19 $_defaults['allow_instructor_requests'] = 'TRUE';
20 $_defaults['auto_approve_instructors'] = 'FALSE';
21
22 $_defaults['max_file_size'] = '1048576';
23 $_defaults['max_course_size'] = '10485760';
24 $_defaults['max_course_float'] = '2097152';
25 $_defaults['ill_ext'] = 'exe, asp, php, php3, bat, cgi, pl, com, vbs, reg, pcd, pif, scr, bas, inf, vb, vbe, wsc, wsf, wsh';
26 $_defaults['cache_dir'] = '';
27
28 $_defaults['theme_categories'] = 'FALSE';
29 $_defaults['content_dir'] = realpath('../').DIRECTORY_SEPARATOR.'content';
30
31 $_defaults['course_backups'] = 5;
32
33 require('include/classes/sqlutility.php');
34
35
36 function my_add_null_slashes( $string ) {
37     return @mysql_real_escape_string(stripslashes($string));
38 }
39 function my_null_slashes($string) {
40         return $string;
41 }
42
43 if ( get_magic_quotes_gpc() == 1 ) {
44         $addslashes   = 'my_add_null_slashes';
45         $stripslashes = 'stripslashes';
46 } else {
47         $addslashes   = 'mysql_real_escape_string';
48         $stripslashes = 'my_null_slashes';
49 }
50
51     function queryFromFile($sql_file_path){
52                 global $db, $progress, $errors;
53                 
54                 $tables = array();
55
56         if (!file_exists($sql_file_path)) {
57             return false;
58         }
59
60         $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
61         SqlUtility::splitSqlFile($pieces, $sql_query);
62
63             foreach ($pieces as $piece) {
64                 $piece = trim($piece);
65             // [0] contains the prefixed query
66             // [4] contains unprefixed table name
67
68
69                         if ($_POST['tb_prefix'] || ($_POST['tb_prefix'] == '')) {
70                     $prefixed_query = SqlUtility::prefixQuery($piece, $_POST['tb_prefix']);
71                         } else {
72                                 $prefixed_query = $piece;
73                         }
74
75                         if ($prefixed_query != false ) {
76                 $table = $_POST['tb_prefix'].$prefixed_query[4];
77                 if($prefixed_query[1] == 'CREATE TABLE'){
78                     if (mysql_query($prefixed_query[0],$db) !== false) {
79                                                 $progress[] = 'Table <strong>'.$table . '</strong> created successfully.';
80                     } else {
81                                                 if (mysql_errno($db) == 1050) {
82                                                         $progress[] = 'Table <strong>'.$table . '</strong> already exists. Skipping.';
83                                                 } else {
84                                                         $errors[] = 'Table <strong>' . $table . '</strong> creation failed.';
85                                                 }
86                     }
87                 }
88                 elseif($prefixed_query[1] == 'INSERT INTO'){
89                     mysql_query($prefixed_query[0],$db);
90                 }elseif($prefixed_query[1] == 'REPLACE INTO'){
91                     mysql_query($prefixed_query[0],$db);
92                 }elseif($prefixed_query[1] == 'ALTER TABLE'){
93                     if (mysql_query($prefixed_query[0],$db) !== false) {
94                                                 $progress[] = 'Table <strong>'.$table.'</strong> altered successfully.';
95                                         } else {
96                                                 if (mysql_errno($db) == 1060) 
97                                                         $progress[] = 'Table <strong>'.$table . '</strong> fields already exists. Skipping.';
98                                                 elseif (mysql_errno($db) == 1091) 
99                                                         $progress[] = 'Table <strong>'.$table . '</strong> fields already dropped. Skipping.';
100                                                 else
101                                                         $errors[] = 'Table <strong>'.$table.'</strong> alteration failed.';
102                                         }
103
104                 }elseif($prefixed_query[1] == 'DROP TABLE'){
105                     mysql_query($prefixed_query[1] . ' ' .$table,$db);
106                 }elseif($prefixed_query[1] == 'UPDATE'){
107                     mysql_query($prefixed_query[0],$db);
108                 }
109             }
110                 }
111         return true;
112     }
113
114 function print_errors( $errors ) {
115         ?>
116         <br />
117         <table border="0" class="errbox" cellpadding="3" cellspacing="2" width="90%" summary="" align="center">
118         <tr class="errbox">
119         <td>
120                 <h3 class="err"><img src="images/bad.gif" align="top" alt="" class="img" /> Error</h3>
121                 <?php
122                         echo '<ul>';
123                         foreach ($errors as $p) {
124                                 echo '<li>'.$p.'</li>';
125                         }
126                         echo '</ul>';
127                 ?>
128                 </td>
129         </tr>
130         </table>        <br />
131 <?php
132 }
133
134 function print_feedback( $feedback ) {
135         ?>
136         <br />
137         <table border="0" class="fbkbox" cellpadding="3" cellspacing="2" width="90%" summary="" align="center">
138         <tr class="fbkbox">
139         <td><h3 class="feedback2"><img src="images/feedback.gif" align="top" alt="" class="img" /> Feedback</h3>
140                 <?php
141                         echo '<ul>';
142                         foreach ($feedback as $p) {
143                                 echo '<li>'.$p.'</li>';
144                         }
145                         echo '</ul>';
146                 ?></td>
147         </tr>
148         </table>
149         <br />
150 <?php
151 }
152
153 function store_steps($step) {
154
155         global $stripslashes;
156
157         foreach($_POST as $key => $value) {
158                 if (substr($key, 0, strlen('step')) == 'step') {
159                         continue;
160                 } else if ($key == 'step') {
161                         continue;
162                 } else if ($key == 'action') {
163                         continue;
164                 } else if ($key == 'submit') {
165                         continue;
166                 }
167
168                 $_POST['step'.$step][$key] = urlencode($stripslashes($value));
169         }
170 }
171
172
173 function print_hidden($current_step) {
174         for ($i=1; $i<$current_step; $i++) {
175                 if (is_array($_POST['step'.$i])) {
176                         foreach($_POST['step'.$i] as $key => $value) {
177                                 echo '<input type="hidden" name="step'.$i.'['.$key.']" value="'.$value.'" />'."\n";
178                         }
179                 }
180         }
181 }
182
183 function print_progress($step) {
184         global $install_steps;
185         
186         echo '<div class="install"><h3>Installation Progress</h3><p>';
187
188         $num_steps = count($install_steps);
189         for ($i=0; $i<$num_steps; $i++) {
190                 if ($i == $step) {
191                         echo '<strong style="margin-left: 12px; color: #006699;">Step '.$i.': '.$install_steps[$i]['name'].'</strong>';
192                 } else {
193                         echo '<small style="margin-left: 10px; color: gray;">';
194                         if ($step > $i) {
195                                 echo '<img src="../images/check.gif" height="9" width="9" alt="Step Done!" /> ';
196                         } else {
197                                 echo '<img src="../images/clr.gif" height="9" width="9" alt="" /> ';
198                         }
199                         echo 'Step '.$i.': '.$install_steps[$i]['name'].'</small>';
200                 }
201                 if ($i+1 < $num_steps) {
202                         echo '<br />';
203                 }
204         }
205         echo '</p></div><br />';
206
207         echo '<h3>'.$install_steps[$step]['name'].'</h3>';
208 }
209
210
211 if (version_compare(phpversion(), '5.0') < 0) {
212         function scandir($dirstr) {
213                 $files = array();
214                 $fh = opendir($dirstr);
215                 while (false !== ($filename = readdir($fh))) {
216                         array_push($files, $filename);
217                 }
218                 closedir($fh);
219                 return $files;
220         }
221 }
222
223 /** 
224  * Print the HTML of the meta forward codes
225  */
226 function print_meta_redirect(){
227         $body = 'ATutor appears to have been installed already. <br/>';
228         $body .= '<a href="../index.php">Click here<a/> to login.';
229
230         $html = "<html>\n";
231         $html .= '<body>'.$body.'</body>'."\n";
232         $html .= "</html>\n";
233
234         return $html;
235 }
236
237 /**
238  * This function is used for printing variables for debugging.
239  * @access  public
240  * @param   mixed $var  The variable to output
241  * @param   string $title       The name of the variable, or some mark-up identifier.
242  * @author  Joel Kronenberg
243  */
244 function debug($var, $title='') {
245         echo '<pre style="border: 1px black solid; padding: 0px; margin: 10px;" title="debugging box">';
246         if ($title) {
247                 echo '<h4>'.$title.'</h4>';
248         }
249         
250         ob_start();
251         print_r($var);
252         $str = ob_get_contents();
253         ob_end_clean();
254
255         $str = str_replace('<', '&lt;', $str);
256
257         $str = str_replace('[', '<span style="color: red; font-weight: bold;">[', $str);
258         $str = str_replace(']', ']</span>', $str);
259         $str = str_replace('=>', '<span style="color: blue; font-weight: bold;">=></span>', $str);
260         $str = str_replace('Array', '<span style="color: purple; font-weight: bold;">Array</span>', $str);
261         echo $str;
262         echo '</pre>';
263 }
264 ?>