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