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