1<?php
2/***************************************************************************
3*                               dbinformer.php
4*                            -------------------
5*   begin                : Saturday, May 05, 2002
6*   copyright            : (C) 2002 The phpBB Group
7*   email                : n/a
8*
9*   $Id: dbinformer.php,v 1.65 2002/05/04 12:15:00 Blade Exp $
10*
11*   Coded by AL, Techie-Micheal, Blade, and Black Fluffy Lion.
12*   http://www.phpbb.com/phpBB/groupcp.php?g=7330
13*
14***************************************************************************/
15/***************************************************************************
16*
17*   This program is free software; you can redistribute it and/or modify
18*   it under the terms of the GNU General Public License as published by
19*   the Free Software Foundation; either version 2 of the License, or
20*   (at your option) any later version.
21*
22***************************************************************************/
23
24/* magic quotes, borrowed from install.php */
25set_magic_quotes_runtime(0);
26if (!get_magic_quotes_gpc())
27{
28    if (is_array($HTTP_POST_VARS))
29    {
30        while (list($k, $v) = each($HTTP_POST_VARS))
31        {
32            if (is_array($HTTP_POST_VARS[$k]))
33            {
34                while (list($k2, $v2) = each($HTTP_POST_VARS[$k]))
35                {
36                    $HTTP_POST_VARS[$k][$k2] = addslashes($v2);
37                }
38                @reset($HTTP_POST_VARS[$k]);
39            }
40            else
41            {
42                $HTTP_POST_VARS[$k] = addslashes($v);
43            }
44        }
45        @reset($HTTP_POST_VARS);
46    }
47}
48
49$all_connected = false;
50$error = false;
51$error_msg = '';
52$select = false;
53$connect = false;
54
55function make_config($dbms, $dbhost, $dbname, $dbuser, $dbpasswd, $table_prefix)
56{
57    $config_file = '&lt;?php<br />' . "\n";
58    $config_file .= '<br />' . "\n";
59    $config_file .= '//<br />' . "\n";
60    $config_file .= '// phpBB 2.x auto-generated config file<br />' . "\n";
61    $config_file .= '// Do not change anything in this file!<br />' . "\n";
62    $config_file .= '//<br />' . "\n";
63    $config_file .= '<br />' . "\n";
64    $config_file .= '$dbms = \'' . $dbms . '\';<br /><br />' . "\n\n";
65    $config_file .= '$dbhost = \'' . $dbhost . '\';<br />' . "\n";
66    $config_file .= '$dbname = \'' . $dbname . '\';<br />' . "\n";
67    $config_file .= '$dbuser = \'' . $dbuser . '\';<br />' . "\n";
68    $config_file .= '$dbpasswd = \'' . $dbpasswd . '\';<br /><br />' . "\n\n";
69    $config_file .= '$table_prefix = \'' . $table_prefix . '\';<br /><br />' . "\n\n";
70    $config_file .= 'define(\'PHPBB_INSTALLED\', true);<br /><br />' . "\n\n";
71    $config_file .= '?>';
72
73    return $config_file;
74}
75
76function make_download($dbms, $dbhost, $dbname, $dbuser, $dbpasswd, $table_prefix)
77{
78    $config_file = '<?php' . "\n\n";
79    $config_file .= '//' . "\n";
80    $config_file .= '// phpBB 2.x auto-generated config file' . "\n";
81    $config_file .= '// Do not change anything in this file!' . "\n";
82    $config_file .= '//' . "\n\n";
83    $config_file .= '$dbms = \'' . $dbms . '\';' . "\n\n";
84    $config_file .= '$dbhost = \'' . $dbhost . '\';' . "\n";
85    $config_file .= '$dbname = \'' . $dbname . '\';' . "\n";
86    $config_file .= '$dbuser = \'' . $dbuser . '\';' . "\n";
87    $config_file .= '$dbpasswd = \'' . $dbpasswd . '\';' . "\n\n";
88    $config_file .= '$table_prefix = \'' . $table_prefix . '\';' . "\n\n";
89    $config_file .= 'define(\'PHPBB_INSTALLED\', true);' . "\n\n";
90    $config_file .= '?>';
91
92    return $config_file;
93}
94
95/* make all the vars safe to display in form inputs and on the user's screen. Borrowed from usercp_register.php */
96$check_var_list = array('dbms' => 'dbms', 'dbhost' => 'dbhost', 'dbname' => 'dbname', 'dbuser' => 'dbuser', 'dbpasswd' => 'dbpasswd', 'table_prefix' => 'table_prefix');
97
98while (list($var, $param) = each($check_var_list))
99{
100    if (!empty($HTTP_POST_VARS[$param]))
101    {
102        $$var = stripslashes(htmlspecialchars(strip_tags($HTTP_POST_VARS[$param])));
103    }
104}
105
106$available_dbms = array(
107    'mysql' => 'MySQL 3.x',
108    'mysql4' => 'MySQL 4.x',
109    'postgres' => 'PostgreSQL 7.x',
110    'mssql' => 'MS SQL Server 7/2000',
111    'msaccess' => 'MS Access [ ODBC ]',
112    'mssql-odbc' => 'MS SQL Server [ OBDC ]',
113);
114
115if (isset($HTTP_POST_VARS['download_config']) && $HTTP_POST_VARS['download_config'] == true && isset($HTTP_POST_VARS['submit_download_config']) && $HTTP_POST_VARS['submit_download_config'] == 'Download')
116{
117    /* borrowed from install.php */
118    header('Content-Type: text/x-delimtext; name="config.php"');
119    header('Content-disposition: attachment; filename=config.php');
120    echo make_download($dbms, $dbhost, $dbname, $dbuser, $dbpasswd, $table_prefix);
121    return;
122}
123?>
124
125<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
126<html>
127<head>
128<meta http-equiv="Content-Type" content="text/html" />
129<meta http-equiv="Content-Style-Type" content="text/css" />
130<title>phpBB :: dbinformer.php</title>
131<link rel="stylesheet" href="../templates/subSilver/subSilver.css" type="text/css" />
132<style type="text/css">
133<!--
134p,ul,td {font-size:10pt;}
135h3 {font-size:12pt;color:blue}
136//-->
137</style>
138</head>
139<body>
140<table width="100%" border="0" cellspacing="0" cellpadding="10" align="center">
141<tr>
142<td class="bodyline"><table width="100%" border="0" cellspacing="0" cellpadding="0">
143<tr>
144<td>
145<table width="100%" border="0" cellspacing="0" cellpadding="0">
146<tr>
147<td><img src="../templates/subSilver/images/logo_phpBB.gif" border="0" alt="phpBB2 : Creating Communities" vspace="1" /></a></td>
148<td align="center" width="100%" valign="middle"><span class="maintitle">dbinformer.php</span>
149</td>
150</tr>
151</table>
152
153<br /><b><div align="center">
154<a href="#what">What you entered</a> |
155<a href="#connect">Connection to database</a> |
156<a href="#tables">Tables in database</a> |
157<a href="#config">Config file</a>
158</b></div>
159
160<table width="100%" border="0" cellspacing="0" cellpadding="10" align="center">
161<tr>
162<td align="center" width="100%" valign="middle"><span class="maintitle"></span></td>
163</tr>
164<tr>
165<td width="100%">
166<table width="100%" cellpadding="2" cellspacing="1" border="0" class="forumline">
167<tr>
168<th colspan="2">Database Configuration</th>
169</tr>
170<tr>
171<td class="row1" align="right"><span class="gen">Database type: </span></td>
172<td class="row2">
173<form action="<?php echo htmlspecialchars($HTTP_SERVER_VARS['PHP_SELF']); ?>" method="post">
174<select name="dbms">
175<?php
176/* loop through the dbms, with the correct one selected (hopefully!) */
177while (list($var, $param) = each($available_dbms))
178{
179    $selected = ($dbms == $var) ? ' selected="selected"' : '';
180    echo '<option value="' . $var . '"' . $selected . '>' . $param . '</option>';
181}
182?>
183</select></td>
184</tr>
185<tr>
186<td class="row1" align="right"><span class="gen">Database Server Hostname / DSN: </span></td>
187<td class="row2"><input type="text" name="dbhost" value="<?php echo @$dbhost; ?>" /></td>
188</tr>
189<tr>
190<td class="row1" align="right"><span class="gen">Your Database Name: </span></td>
191<td class="row2"><input type="text" name="dbname" value="<?php echo @$dbname; ?>" /></td>
192</tr>
193<tr>
194<td class="row1" align="right"><span class="gen">Database Username: </span></td>
195<td class="row2"><input type="text" name="dbuser" value="<?php echo @$dbuser; ?>" /></td>
196</tr>
197<tr>
198<td class="row1" align="right"><span class="gen">Database Password: </span></td>
199<td class="row2"><input type="password" name="dbpasswd" value="<?php echo @$dbpasswd; ?>" /></td>
200</tr>
201<tr>
202<td class="row1" align="right"><span class="gen">Chosen Prefix: </span></td>
203<td class="row2"><input type="text" name="table_prefix" value="<?php echo @$table_prefix; ?>" /></td>
204</tr>
205<tr>
206<td class="row1" align="right"><span class="gen">Generate a config file: </span></td>
207</td>
208<td class="row2"><input type="checkbox" name="generate_config" value="true" <?php $checked = (isset($HTTP_POST_VARS['generate_config']) && $HTTP_POST_VARS['generate_config'] == true) ? 'checked="checked"' : ''; echo $checked; ?> /></td>
209</tr>
210<tr>
211<td class="catbottom" align="center" colspan="2">
212<input class="mainoption" type="submit" name="submit" value="Submit" /></td>
213</tr>
214</form></td>
215</tr>
216</table>
217<?php
218if (!isset($HTTP_POST_VARS['submit']))
219{
220    echo '<br />Please enter your data.<br />';
221}
222else
223{
224    /* dbal added by Techie-Micheal [and then obliterated by BFL]. weeeeeee! */
225    switch ($dbms)
226    {
227        case 'mysql':
228            if (function_exists(@mysql_connect))
229            {
230                $db = array(
231                    'choice' => 'MySQL 3.x',
232                    'connect' => @mysql_connect($dbhost, $dbuser, $dbpasswd),
233                    'select' => @mysql_select_db($dbname),
234                    'error' => @mysql_error(),
235                    'list' => @mysql_list_tables($dbname),
236                    'fetch' => @mysql_fetch_row,
237                    'close' => @mysql_close()
238                );
239            }
240            else
241            {
242                $error = true;
243                $error_msg = 'You do not have the needed functions available for ' . $available_dbms[$dbms] . '.';
244            }
245        break;
246
247        case 'mysql4':
248            if (function_exists(@mysql_connect))
249            {
250                $db = array(
251                    'choice' => 'MySQL 4.x',
252                    'connect' => @mysql_connect($dbhost, $dbuser, $dbpasswd),
253                    'select' => @mysql_select_db($dbname),
254                    'error' => @mysql_error(),
255                    'list' => @mysql_list_tables($dbname),
256                    'fetch' => @mysql_fetch_row,
257                    'close' => @mysql_close()
258                );
259            }
260            else
261            {
262                $error = true;
263                $error_msg = 'You do not have the needed functions available for ' . $available_dbms[$dbms] . '.';
264            }
265        break;
266
267        case 'msaccess':
268            if (function_exists(@odbc_connect))
269            {
270                $db = array(
271                    'choice' => 'MS Access [ ODBC ]',
272                    'connect' => @odbc_connect($dbhost, $dbuser, $dbpasswd),
273                    'select' => 'na',
274                    'error' => @odbc_errormsg(),
275                    'list' => 'na', /* odbc_tables() */
276                    'fetch' => 'na', /* odbc_fetch_row(), odbc_result_all() */
277                    'close' => @odbc_close()
278                );
279            }
280            else
281            {
282                $error = true;
283                $error_msg = 'You do not have the needed functions available for ' . $available_dbms[$dbms] . '.';
284            }
285        break;
286
287        case 'postgres':
288            if (function_exists(@pg_connect))
289            {
290                $db = array(
291                    'choice' => 'PostgreSQL 7.x',
292                    'connect' => @pg_connect('host=' . $dbhost . ' user=' . $dbuser . ' dbname=' . $dbname . ' password=' . $dbpasswd),
293                    'select' => 'na',
294                    'error' => @pg_last_error(),
295                    'list' => @pg_exec("SELECT relname FROM pg_class WHERE relkind = 'r' AND relname NOT LIKE 'pg\_%'"), /* provided by SuGa */
296                    'fetch' => @pg_fetch_row,
297                    'close' => @pg_close()
298                );
299            }
300            else
301            {
302                $error = true;
303                $error_msg = 'You do not have the needed functions available for ' . $available_dbms[$dbms] . '.';
304            }
305        break;
306        case 'mssql':
307            if (function_exists(@mssql_connect))
308            {
309                $db = array(
310                    'choice' => 'MS SQL Server 7/2000',
311                    'connect' => @mssql_connect($dbhost, $dbuser, $dbpasswd),
312                    'select' => @mssql_select_db($dbname),
313                    'error' => @mssql_get_last_message(),
314                    'list' => 'na',
315                    'fetch' => 'na', /* mssql_fetch_row() */
316                    'close' => @mssql_close()
317                );
318            }
319            else
320            {
321                $error = true;
322                $error_msg = 'You do not have the needed functions available for ' . $available_dbms[$dbms] . '.';
323            }
324        break;
325
326        case 'mssql-odbc':
327            if (function_exists(@odbc_connect))
328            {
329                $db = array(
330                    'choice' => 'MS SQL Server [ ODBC ]',
331                    'connect' => @odbc_connect($dbhost, $dbuser, $dbpasswd),
332                    'select' => 'na',
333                    'error' => @odbc_errormsg(),
334                    'list' => 'na', /* odbc_tables() */
335                    'fetch' => 'na', /* odbc_fetch_row(), odbc_result_all() */
336                    'close' => @odbc_close()
337                );
338            }
339            else
340            {
341                $error = true;
342                $error_msg = 'You do not have the needed functions available for ' . $available_dbms[$dbms] . '.';
343            }
344        break;
345
346        default:
347            $error = true;
348            $error_msg = 'Unrecognised DBMS.';
349        break;
350    }
351
352    if ($error == true && $error_msg != '')
353    {
354        echo '<br /><b>ERROR:</b> ' . $error_msg . '<br />';
355    }
356    else
357    {
358        echo '<a name="what"><h3><u>What you entered</u></h3></a>';
359        echo 'Database Type: <b>' . $db['choice']  . '</b><br />';
360        echo 'Database Server Hostname / DSN: <b>' . $dbhost . '</b><br />';
361        echo 'Your Database Name: <b>' . $dbname . '</b><br />';
362        echo 'Database Username: <b>' . $dbuser .   '</b><br />';
363        echo 'Database Password: <b>' . $dbpasswd   . '</b><br />';
364
365        echo '<a name="connect"><h3><u>Connection to database</u></h3></a>';
366
367        if (!$db['connect'])
368        {
369            echo 'You have not established a connection to <b>' . $db['choice'] . '</b>.<br />';
370            echo '<b>ERROR:</b> <i>' . $db['error'] . '</i><br /><br />';
371        }
372        else
373        {
374            echo 'You have established a connection to <b>' . $db['choice'] . '</b>.<br /><br />';
375            $connect = true;
376        }
377
378        if ($dbms == 'msaccess' || $dbms == 'postgres' || $dbms == 'mssql-odbc')
379        {
380            /* for dbmses which have no db select function */
381            $select = true;
382        }
383        else
384        {
385            if (!$db['select'])
386            {
387                echo 'Your database was not found.<br />';
388                echo '<b>ERROR:</b> <i>' . htmlspecialchars($db['error']) . '</i><br />';
389            }
390            else
391            {
392                echo 'Your database was found.<br />';
393                $select = true;
394            }
395        }
396
397        if ($connect == true && $select == true)
398        {
399            echo '<a name="tables"><h3><u>Tables in database</u></h3></a>';
400            if ($dbms == 'mysql' || $dbms == 'mysql4' || $dbms == 'postgres')
401            {
402                echo '<i>Tables with the table prefix you specified are in bold.</i>';
403                echo '<ul>';
404                while ($table = $db['fetch']($db['list']))
405                {
406                    /* Highlight tables with the table_prefix specified */
407                    if (preg_match("/^$HTTP_POST_VARS[table_prefix]/i", $table[0]))
408                    {
409                        echo '<li><b>' . htmlspecialchars($table[0]) . '</b></li><br />';
410                    }
411                    else
412                    {
413                        echo '<li>' . htmlspecialchars($table[0]) . '</li><br />';
414                    }
415                }
416                echo '</ul>';
417            }
418            else
419            {
420                echo 'Sorry, this feature isn\'t available with ' . $db['choice'] . '.';
421            }
422
423            /* defined a var which is only there if successfully connected to the database and the database is found */
424            $all_connected = true;
425        }
426
427        /* Create a config file if checked and if the connection went OK */
428        if (isset($HTTP_POST_VARS['generate_config']) && $HTTP_POST_VARS['generate_config'] == true)
429        {
430            echo '<a name="config"><h3><u>Config file</u></h3></a>';
431            if ($all_connected != true)
432            {
433                echo 'The database has not been successfully connected to so no config file has been generated.<br />';
434            }
435            else
436            {
437                echo 'Either copy the <b>19</b> lines below and save them as <u>config.php</u> or click on the <u>Download</u> button below. Then upload the file to your phpBB2 root directory (phpBB2/ by default). Make sure that there is nothing (this includes blank spaces) after the <u>?></u>.<br /><br />';
438
439                /* Create our config file */
440                echo '<form action="' . htmlspecialchars($HTTP_SERVER_VARS['PHP_SELF']) . '" method="post"><table cellspacing="1" cellpadding="3" border="0"><tr><td class="code">';
441                echo make_config($dbms, $dbhost, $dbname, $dbuser, $dbpasswd, $table_prefix);
442                echo '</td></tr></table>';
443                echo '<input type="hidden" name="dbms" value="' . $dbms . '" />';
444                echo '<input type="hidden" name="dbhost" value="' . $dbhost . '" />';
445                echo '<input type="hidden" name="dbname" value="' . $dbname . '" />';
446                echo '<input type="hidden" name="dbuser" value="' . $dbuser . '" />';
447                echo '<input type="hidden" name="dbpasswd" value="' . $dbpasswd . '" />';
448                echo '<input type="hidden" name="table_prefix" value="' . $table_prefix . '" />';
449                echo '<input type="hidden" name="download_config" value="true" />';
450                echo '<br /><input type="submit" name="submit_download_config" value="Download" class="mainoption" /><br />';
451            }
452        }
453
454        /* close the connection */
455        if ($all_connected == true)
456        {
457            $db['close'];
458        }
459    }
460}
461
462/* And they all lived happily ever after...
463The End */
464?>
465
466<br /><a href="javascript:scrollTo('0','0');"><b>Return to top</b></a>
467</td>
468</tr>
469</table>
470<div align="center"><span class="copyright">&copy; Copyright 2002 The <a href="http://www.phpbb.com/about.php" target="_phpbb" class="copyright">phpBB Group</a></span></div>
471</td>
472</tr>
473</table>
474</body>
475</html>