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