PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyfaq/install/setup.php

http://github.com/thorsten/phpMyFAQ
PHP | 935 lines | 846 code | 51 blank | 38 comment | 46 complexity | 0120cebcca3ed2c3b28208b995d8e907 MD5 | raw file
  1<?php
  2/**
  3 * The main phpMyFAQ Setup
  4 *
  5 * This script checks the complete environment, writes the database connection
  6 * parameters into the file config/database.php and the configuration into the database.
  7 *
  8 * PHP Version 5.3
  9 *
 10 * This Source Code Form is subject to the terms of the Mozilla Public License,
 11 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 12 * obtain one at http://mozilla.org/MPL/2.0/.
 13 *
 14 * @category  phpMyFAQ
 15 * @package   Setup
 16 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
 17 * @author    Tom Rochester <tom.rochester@gmail.com>
 18 * @author    Johannes Schl?ter <johannes@php.net>
 19 * @author    Uwe Pries <uwe.pries@digartis.de>
 20 * @author    Matteo Scaramuccia <matteo@phpmyfaq.de>
 21 * @copyright 2002-2012 phpMyFAQ Team
 22 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 23 * @link      http://www.phpmyfaq.de
 24 * @since     2002-08-20
 25 */
 26
 27define('COPYRIGHT', '&copy; 2001-2012 <a href="http://www.phpmyfaq.de/">phpMyFAQ Team</a> | Follow us on <a href="http://twitter.com/phpMyFAQ">Twitter</a> | All rights reserved.');
 28define('PMF_ROOT_DIR', dirname(__DIR__));
 29define('IS_VALID_PHPMYFAQ', null);
 30
 31if (! defined('DEBUG')) {
 32    define('DEBUG', true);
 33}
 34
 35if ((@ini_get('safe_mode') != 'On' || @ini_get('safe_mode') !== 1)) {
 36    set_time_limit(0);
 37}
 38
 39require PMF_ROOT_DIR . '/config/constants.php';
 40require PMF_ROOT_DIR . '/inc/Autoloader.php';
 41require PMF_ROOT_DIR . '/inc/functions.php';
 42require PMF_ROOT_DIR . '/install/questionnaire.php';
 43
 44//
 45// Initalizing static string wrapper
 46//
 47PMF_String::init('en');
 48
 49$query = $uninst = array();
 50
 51/**
 52 * Print out the HTML5 Footer
 53 *
 54 * @return void
 55 */
 56function HTMLFooter()
 57{
 58    printf(
 59        '</div></div></section><footer><div class="container"><p class="pull-right">%s</p><div></footer></body></html>',
 60        COPYRIGHT
 61    );
 62}
 63
 64$system        = new PMF_System();
 65$instanceSetup = new PMF_Instance_Setup();
 66
 67$instanceSetup->setRootDir(PMF_ROOT_DIR);
 68
 69?>
 70<!doctype html>
 71<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
 72<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
 73<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
 74<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
 75<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
 76<head>
 77    <meta charset="utf-8">
 78    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 79
 80    <title>phpMyFAQ <?php print PMF_System::getVersion(); ?> Setup</title>
 81
 82    <meta name="viewport" content="width=device-width;">
 83    <meta name="application-name" content="phpMyFAQ <?php print PMF_System::getVersion(); ?>">
 84    <meta name="copyright" content="(c) 2001-<?php print date('Y'); ?> phpMyFAQ Team">
 85
 86    <link rel="stylesheet" href="../assets/template/default/css/style.css?v=1">
 87
 88    <script src="../assets/js/libs/modernizr.min.js"></script>
 89    <script src="../assets/js/libs/jquery.min.js"></script>
 90
 91    <link rel="shortcut icon" href="../assets/template/default/favicon.ico">
 92    <link rel="apple-touch-icon" href="../assets/template/default/apple-touch-icon.png">
 93
 94    <script type="text/javascript">
 95        function selectDatabaseSetup(field) {
 96            switch (field.value) {
 97            case 'sqlite':
 98            case 'sqlite3':
 99                $('#dbsqlite').show()
100                $('#dbdatafull').hide();
101                break;
102            default:
103                $('#dbsqlite').hide();
104                $('#dbdatafull').show();
105                break;
106            }
107        }
108    </script>
109</head>
110<body>
111
112<!--[if lt IE 8 ]>
113<div class="internet-explorer-error">
114    Do you know that your Internet Explorer is out of date?<br/>
115    Please use Internet Explorer 8+, Mozilla Firefox 4+, Google Chrome, Apple Safari 5+ or Opera 11+
116</div>
117<![endif]-->
118
119
120<div class="navbar navbar-fixed-top">
121    <div class="navbar-inner">
122        <div class="container">
123            <nav class="nav-collapse">
124                <ul class="nav">
125                    <li><a target="_blank" href="http://www.phpmyfaq.de/documentation.php">Documentation</a></li>
126                    <li><a target="_blank" href="http://www.phpmyfaq.de/support.php">Support</a></li>
127                    <li><a target="_blank" href="http://forum.phpmyfaq.de/">Forums</a></li>
128                    <li><a target="_blank" href="http://faq.phpmyfaq.de/">FAQ</a></li>
129                </ul>
130            </nav>
131        </div>
132    </div>
133</div>
134
135<section id="main">
136    <div class="container">
137        <div class="row" style="padding-left: 20px;">
138            <div class="hero-unit hello-phpmyfaq" style="text-align: center; height: 60px;">
139                <h1>phpMyFAQ <?php print PMF_System::getVersion(); ?> Setup</h1>
140                <p>
141                    Did you already read the <a style="color: #ffffff; text-decoration: underline;"
142                    href="http://www.phpmyfaq.de/documentation.php">documentation</a> carefully before starting the
143                    phpMyFAQ setup? :-)
144                </p>
145            </div>
146        </div>
147        <div class="row" style="padding-left: 20px;">
148<?php
149
150if (version_compare(PHP_VERSION, PMF_System::VERSION_MINIMUM_PHP, '<')) {
151    printf('<p class="alert alert-error">Sorry, but you need PHP %s or later!</p>', PMF_System::VERSION_MINIMUM_PHP);
152    HTMLFooter();
153    die();
154}
155
156if (! function_exists('date_default_timezone_set')) {
157    print '<p class="alert alert-error">Sorry, but setting a default timezone doesn\'t work in your environment!</p>';
158    HTMLFooter();
159    die();
160}
161
162if (! $system->checkDatabase()) {
163    print '<p class="alert alert-error">No supported database detected! Please install one of the following' .
164          ' database systems and enable the corresponding PHP extension in php.ini:</p>';
165    print '<ul>';
166    foreach ($system->getSupportedDatabases() as $database) {
167        printf('    <li>%s</li>', $database[1]);
168    }
169    print '</ul>';
170    HTMLFooter();
171    die();
172}
173
174if (! $system->checkRequiredExtensions()) {
175    print '<p class="alert alert-error">The following extensions are missing! Please enable the PHP extension(s) in ' .
176          'php.ini.</p>';
177    print '<ul>';
178    foreach ($system->getMissingExtensions() as $extension) {
179        printf('    <li>ext/%s</li>', $extension);
180    }
181    print '</ul>';
182    HTMLFooter();
183    die();
184}
185
186if (! $system->checkphpMyFAQInstallation()) {
187    print '<p class="alert alert-error">It seems you\'re already running a version of phpMyFAQ. Please use the ' .
188          '<a href="update.php">update script</a>.</p>';
189    HTMLFooter();
190    die();
191}
192
193$dirs       = array('/attachments', '/config', '/data', '/images');
194$faileddirs = $instanceSetup->checkDirs($dirs);
195
196if (sizeof($faileddirs)) {
197    print '<p class="alert alert-error">The following directory/-ies could not be created or are not writable:</p><ul>';
198    foreach ($faileddirs as $dir) {
199        print "<li>$dir</li>\n";
200    }
201    print '</ul><p class="alert alert-error">Please create it/them manually and/or change access to chmod 755 (or ' .
202          'greater if necessary).</p>';
203    HTMLFooter();
204    die();
205}
206
207if (!isset($_POST["sql_server"]) && !isset($_POST["sql_user"]) && !isset($_POST["sql_db"])) {
208
209    if ((@ini_get('safe_mode') == 'On' || @ini_get('safe_mode') === 1)) {
210        print '<p class="alert alert-error">The PHP safe mode is enabled. You may have problems when phpMyFAQ tries to write ' .
211              ' in some directories.</p>';
212    }
213    if (! extension_loaded('gd')) {
214        print '<p class="alert alert-error">You don\'t have GD support enabled in your PHP installation. Please enable GD ' .
215              'support in your php.ini file otherwise you can\'t use Captchas for spam protection.</p>';
216    }
217    if (! function_exists('imagettftext')) {
218        print '<p class="alert alert-error">You don\'t have Freetype support enabled in the GD extension of your PHP ' .
219              'installation. Please enable Freetype support in GD extension otherwise the Captchas for spam ' .
220              'protection will be quite easy to break.</p>';
221    }
222    if (! extension_loaded('curl') || ! extension_loaded('openssl')) {
223        print '<p class="alert alert-error">You don\'t have cURL and/or OpenSSL support enabled in your PHP installation. ' .
224              'Please enable cURL and/or OpenSSL support in your php.ini file otherwise you can\'t use the Twitter ' .
225              ' support.</p>';
226    }
227?>
228        </div>
229
230        <form class="form-horizontal" action="setup.php" method="post">
231        <div class="row">
232            <div class="span6">
233                <fieldset>
234                <legend>Add your database connection setup</legend>
235                    <div class="control-group">
236                        <label class="control-label" for="sql_type">Database server:</label>
237                        <div class="controls">
238                            <select name="sql_type" id="sql_type" size="1" onchange="selectDatabaseSetup(this);">
239<?php
240    // check what extensions are loaded in PHP
241    foreach ($system->getSupportedDatabases() as $extension => $database) {
242        if (extension_loaded($extension) && version_compare(PHP_VERSION, $database[0]) >= 0) {
243            // prevent MySQLi with zend.ze1_compatibility_mode enabled due to a few cloning isssues
244            if (($extension == 'mysqli') && ini_get('zend.ze1_compatibility_mode')) {
245                continue;
246            }
247            printf('<option value="%s">%s</option>', $extension, $database[1]);
248        }
249    }
250?>
251                            </select>
252                            <p class="help-block">Please select your preferred database type.</p>
253                        </div>
254                    </div>
255
256                    <div id="dbdatafull">
257                        <div class="control-group">
258                            <label class="control-label" for="sql_server">Database hostname:</label>
259                            <div class="controls">
260                                <input type="text" name="sql_server" id="sql_server" />
261                                <p class="help-block">Please enter the host of your database server.</p>
262                            </div>
263                        </div>
264                        <div class="control-group">
265                            <label class="control-label" for="sql_user">Database user:</label>
266                            <div class="controls">
267                                <input type="text" name="sql_user" id="sql_user" />
268                                <p class="help-block">Please enter your database user.</p>
269                            </div>
270                        </div>
271                        <div class="control-group">
272                            <label class="control-label" for="sql_password">Database password:</label>
273                            <div class="controls">
274                                <input name="sql_passwort" type="password" id="sql_password" />
275                                <p class="help-block">Please enter your database password.</p>
276                            </div>
277                        </div>
278                        <div class="control-group">
279                            <label class="control-label" for="sql_db">Database name:</label>
280                            <div class="controls">
281                                <input type="text" name="sql_db" id="sql_db" />
282                                <p class="help-block">Please enter your database name.</p>
283                            </div>
284                        </div>
285                    </div>
286
287                    <div id="dbsqlite" class="hide">
288                        <div class="control-group">
289                            <label class="control-label" for="sql_sqlitefile">SQLite database file:</label>
290                            <div class="controls">
291                                <input type="text" name="sql_sqlitefile" id="sql_sqlitefile"
292                                       value="<?php print dirname(__DIR__); ?>" />
293                                <p class="help-block">
294                                    Please enter the full path to your SQLite datafile which should be outside your
295                                    docroot.
296                                </p>
297                            </div>
298                        </div>
299                    </div>
300
301                    <div class="control-group">
302                        <label class="control-label" for="sqltblpre">Table prefix:</label>
303                        <div class="controls">
304                            <input type="text" name="sqltblpre" id="sqltblpre" />
305                            <p class="help-block">
306                                Please enter a table prefix here if you want to install more phpMyFAQ installations on
307                                one database.
308                            </p>
309                        </div>
310                    </div>
311
312                </fieldset>
313            </div>
314
315            <?php if (extension_loaded('ldap')): ?>
316            <div class="span6">
317                <fieldset>
318                <legend>Add your LDAP setup</legend>
319                    <div class="control-group">
320                        <label class="control-label" for="ldap_enabled">&nbsp;</label>
321                        <div class="controls">
322                            <label class="checkbox">
323                                <input id="ldap_enabled" type="checkbox" name="ldap_enabled" value="yes" />
324                                Enable LDAP support?
325                            </label>
326                            <p class="help-block">You can enable LDAP later if you like.</p>
327                        </div>
328                    </div>
329                    <div class="control-group">
330                        <label class="control-label" for="ldap_server">LDAP server host:</label>
331                        <div class="controls">
332                            <input type="text" name="ldap_server" id="ldap_server" />
333                            <p class="help-block">Please enter the host of your LDAP server.</p>
334                        </div>
335                    </div>
336                    <div class="control-group">
337                        <label class="control-label" for="ldap_port">LDAP server port:</label>
338                        <div class="controls">
339                            <input type="number" name="ldap_port" value="389" id="ldap_port" />
340                            <p class="help-block">Please enter the port of your LDAP server.</p>
341                        </div>
342                    </div>
343                    <div class="control-group">
344                        <label class="control-label" for="ldap_user">LDAP user DN:</label>
345                        <div class="controls">
346                            <input type="text" name="ldap_user" id="ldap_user" />
347                            <p class="help-block">Please enter your specified RDN username.</p>
348                        </div>
349                    </div>
350                    <div class="control-group">
351                        <label class="control-label" for="ldap_password">LDAP password:</label>
352                        <div class="controls">
353                            <input name="ldap_password" type="password" id="ldap_password" />
354                            <p class="help-block">Please enter your LDAP password.</p>
355                        </div>
356                    </div>
357                    <div class="control-group">
358                        <label class="control-label" for="ldap_base">LDAP base DN:</label>
359                        <div class="controls">
360                        <input type="text" name="ldap_base" id="ldap_base" />
361                            <p class="help-block">
362                                Please enter your distinguished name, e.g. 'cn=John Doe,ou=Accounts,o=My Company,c=US'.
363                            </p>
364                        </div>
365                    </div>
366                    <p class="alert alert-info">
367                        You can add additional LDAP configuration and more LDAP servers in the file
368                        config/constants_ldap.php.
369                    </p>
370
371                </fieldset>
372            </div>
373        </div>
374        <div class="row">
375        <?php endif; ?>
376
377            <div class="span6">
378                <fieldset>
379                <legend>Your phpMyFAQ setup</legend>
380                    <div class="control-group">
381                        <label class="control-label" for="language">Default language:</label>
382                        <div class="controls">
383                            <select name="language" size="1" id="language">
384                            <?php
385                                if ($dir = @opendir(PMF_ROOT_DIR . '/lang')) {
386                                    while ($dat = @readdir($dir)) {
387                                        if (substr($dat, -4) == '.php') {
388                                            printf('<option value="%s"', $dat);
389                                            if ($dat == "language_en.php") {
390                                                print ' selected="selected"';
391                                            }
392                                            printf(
393                                                '>%s</option>',
394                                                $languageCodes[substr(strtoupper($dat), 9, 2)]
395                                            );
396                                        }
397                                    }
398                                } else {
399                                    print '<option>english</option>';
400                                }
401                            ?>
402                            </select>
403                            <p class="help-block">Please select your default language.</p>
404                        </div>
405                    </div>
406
407                    <div class="control-group">
408                        <label class="control-label" for="permLevel">Permission level:</label>
409                        <div class="controls">
410                                <select id="permLevel" name="permLevel" size="1" required="required">
411                                    <option value="basic">Basic (no group support)</option>
412                                    <option value="medium">Medium (with group support)</option>
413                            </select>
414                            <p class="help-block">
415                                Complexity of rights and permissions.
416                            </p>
417                        </div>
418                    </div>
419                    <div class="control-group">
420                        <label class="control-label" for="realname">Your name:</label>
421                        <div class="controls">
422                            <input type="text" name="realname" id="realname" required="required" />
423                            <p class="help-block">Please enter your real name.</p>
424                        </div>
425                    </div>
426                    <div class="control-group">
427                        <label class="control-label" for="email">Your email address:</label>
428                        <div class="controls">
429                            <input type="email" name="email" id="email" required="required" />
430                            <p class="help-block">Please enter your email adress.</p>
431                        </div>
432
433                    </div>
434                    <div class="control-group">
435                        <label class="control-label" for="loginname">Your login name:</label>
436                        <div class="controls">
437                            <input type="text" name="loginname" id="loginname" required="required" />
438                            <p class="help-block">Please enter your login name.</p>
439                        </div>
440                    </div>
441                    <div class="control-group">
442                        <label class="control-label" for="password">Your password:</label>
443                        <div class="controls">
444                            <input type="password" name="password" id="password" required="required" />
445                            <p class="help-block">Please enter your password.</p>
446                        </div>
447                    </div>
448                    <div class="control-group">
449                        <label class="control-label" for="password_retype">Retype password:</label>
450                        <div class="controls">
451                            <input type="password" name="password_retyped" id="password_retype" required="required" />
452                            <p class="help-block">Please retype your password.</p>
453                        </div>
454                    </div>
455                </fieldset>
456            </div>
457        </div>
458
459        <div class="row" style="padding-left: 20px; text-align: center;">
460
461            <p class="alert alert-danger">
462                Dude, this is an early alpha version. Please don't install this version on production!
463            </p>
464
465            <input class="btn-primary btn-large" type="submit"
466                   value="Click to install phpMyFAQ <?php print PMF_System::getVersion(); ?>" />
467        </div>
468        <div class="row" style="padding-left: 20px;">
469            <p class="alert alert-info" style="margin-top: 20px;">
470                Your password will be saved with a <strong><?php print PMF_ENCRYPTION_TYPE; ?></strong>
471                encryption. You can change the encryption type for passwords in <em>config/constants.php</em>.
472            </p>
473        </div>
474        </form>
475<?php
476    HTMLFooter();
477} else {
478
479    $dbSetup = array();
480
481    // Check table prefix
482    $dbSetup['dbPrefix'] = $sqltblpre = PMF_Filter::filterInput(INPUT_POST, 'sqltblpre', FILTER_SANITIZE_STRING, '');
483    if (!defined('SQLPREFIX')) {
484        define('SQLPREFIX', $dbSetup['dbPrefix']);
485    }
486
487    // Check database entries
488    $dbSetup['dbType'] = PMF_Filter::filterInput(INPUT_POST, 'sql_type', FILTER_SANITIZE_STRING);
489    if (!is_null($dbSetup['dbType'])) {
490        $dbSetup['dbType'] = trim($dbSetup['dbType']);
491        if (! file_exists(PMF_ROOT_DIR . '/install/' . $dbSetup['dbType'] . '.sql.php')) {
492            printf(
493                '<p class="alert alert-error"><strong>Error:</strong> Invalid server type: %s</p>',
494                $dbSetup['dbType']
495            );
496            HTMLFooter();
497            die();
498        }
499    } else {
500        print "<p class=\"alert alert-error\"><strong>Error:</strong> Please select a database type.</p>\n";
501        HTMLFooter();
502        die();
503    }
504
505    $dbSetup['dbServer'] = PMF_Filter::filterInput(INPUT_POST, 'sql_server', FILTER_SANITIZE_STRING);
506    if (is_null($dbSetup['dbServer']) && ! PMF_System::isSqlite($dbSetup['dbType'])) {
507        print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a database server.</p>\n";
508        HTMLFooter();
509        die();
510    }
511
512    $dbSetup['dbUser'] = PMF_Filter::filterInput(INPUT_POST, 'sql_user', FILTER_SANITIZE_STRING);
513    if (is_null($dbSetup['dbUser']) && ! PMF_System::isSqlite($dbSetup['dbType'])) {
514        print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a database username.</p>\n";
515        HTMLFooter();
516        die();
517    }
518
519    $dbSetup['dbPassword'] = PMF_Filter::filterInput(INPUT_POST, 'sql_passwort', FILTER_UNSAFE_RAW);
520    if (is_null($dbSetup['dbPassword']) && ! PMF_System::isSqlite($dbSetup['dbType'])) {
521        // Password can be empty...
522        $dbSetup['dbPassword'] = '';
523    }
524
525    $dbSetup['dbDatabaseName'] = PMF_Filter::filterInput(INPUT_POST, 'sql_db', FILTER_SANITIZE_STRING);
526    if (is_null($dbSetup['dbDatabaseName']) && ! PMF_System::isSqlite($dbSetup['dbType'])) {
527        print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a database name.</p>\n";
528        HTMLFooter();
529        die();
530    }
531
532    if (PMF_System::isSqlite($dbSetup['dbType'])) {
533        $dbSetup['dbServer'] = PMF_Filter::filterInput(INPUT_POST, 'sql_sqlitefile', FILTER_SANITIZE_STRING);
534        if (is_null($dbSetup['dbServer'])) {
535            print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a SQLite database filename.</p>\n";
536            HTMLFooter();
537            die();
538        }
539    }
540
541    // check database connection
542    require PMF_ROOT_DIR . "/inc/Db.php";
543    require PMF_ROOT_DIR . "/inc/DB/Driver.php";
544    $db = PMF_Db::factory($dbSetup['dbType']);
545    $db->connect($dbSetup['dbServer'], $dbSetup['dbUser'], $dbSetup['dbPassword'], $dbSetup['dbDatabaseName']);
546    if (!$db) {
547        printf("<p class=\"alert alert-error\"><strong>DB Error:</strong> %s</p>\n", $db->error());
548        HTMLFooter();
549        die();
550    }
551
552    $configuration = new PMF_Configuration($db);
553
554    // check LDAP if available
555    $ldapEnabled = PMF_Filter::filterInput(INPUT_POST, 'ldap_enabled', FILTER_SANITIZE_STRING);
556    if (extension_loaded('ldap') && !is_null($ldapEnabled)) {
557
558        $ldapSetup = array();
559
560        // check LDAP entries
561        $ldapSetup['ldapServer'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_server', FILTER_SANITIZE_STRING);
562        if (is_null($ldapSetup['ldapServer'])) {
563            print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a LDAP server.</p>\n";
564            HTMLFooter();
565            die();
566        }
567        
568        $ldapSetup['ldapPort'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_port', FILTER_VALIDATE_INT);
569        if (is_null($ldapSetup['ldapPort'])) {
570            print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a LDAP port.</p>\n";
571            HTMLFooter();
572            die();
573        }
574
575        $ldapSetup['ldapBase'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_base', FILTER_SANITIZE_STRING);
576        if (is_null($ldapSetup['ldapBase'])) {
577            print "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a LDAP base search DN.</p>\n";
578            HTMLFooter();
579            die();
580        }
581
582        // LDAP User and LDAP password are optional
583        $ldapSetup['ldapUser']     = PMF_Filter::filterInput(INPUT_POST, 'ldap_user', FILTER_SANITIZE_STRING, '');
584        $ldapSetup['ldapPassword'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_password', FILTER_SANITIZE_STRING, '');
585
586        // check LDAP connection
587        require PMF_ROOT_DIR . "/inc/Ldap.php";
588        $ldap = new PMF_Ldap($configuration);
589        $ldap->connect(
590            $ldapSetup['ldapServer'],
591            $ldapSetup['ldapPort'],
592            $ldapSetup['ldapBase'],
593            $ldapSetup['ldapUser'],
594            $ldapSetup['ldapPassword']
595        );
596        if (!$ldap) {
597            print "<p class=\"alert alert-error\"><strong>LDAP Error:</strong> " . $ldap->error() . "</p>\n";
598            HTMLFooter();
599            die();
600        }
601    }
602
603    // check loginname
604    $loginname = PMF_Filter::filterInput(INPUT_POST, 'loginname', FILTER_SANITIZE_STRING);
605    if (is_null($loginname)) {
606        print '<p class="alert alert-error"><strong>Error:</strong> Please add a loginname for your account.</p>';
607        HTMLFooter();
608        die();
609    }
610
611    // check user entries
612    $password = PMF_Filter::filterInput(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
613    if (is_null($password)) {
614        print '<p class="alert alert-error"><strong>Error:</strong> Please add a password for the your account.</p>';
615        HTMLFooter();
616        die();
617    }
618    
619    $password_retyped = PMF_Filter::filterInput(INPUT_POST, 'password_retyped', FILTER_SANITIZE_STRING);
620    if (is_null($password_retyped)) {
621        print '<p class="alert alert-error"><strong>Error:</strong> Please add a retyped password.</p>';
622        HTMLFooter();
623        die();
624    }
625    
626    if (strlen($password) <= 5 || strlen($password_retyped) <= 5) {
627        print '<p class="alert alert-error"><strong>Error:</strong> Your password and retyped password are too short.' .
628              ' Please set your password and your retyped password with a minimum of 6 characters.</p>';
629        HTMLFooter();
630        die();
631    }
632    if ($password != $password_retyped) {
633        print '<p class="alert alert-error"><strong>Error:</strong> Your password and retyped password are not equal.' .
634              ' Please check your password and your retyped password.</p>';
635        HTMLFooter();
636        die();
637    }
638
639    $language  = PMF_Filter::filterInput(INPUT_POST, 'language', FILTER_SANITIZE_STRING, 'en');
640    $realname  = PMF_Filter::filterInput(INPUT_POST, 'realname', FILTER_SANITIZE_STRING, '');
641    $email     = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL, '');
642    $permLevel = PMF_Filter::filterInput(INPUT_POST, 'permLevel', FILTER_SANITIZE_STRING, 'basic');
643    
644    // Write the DB variables in database.php
645    if (! $instanceSetup->createDatabaseFile($dbSetup)) {
646        print "<p class=\"alert alert-error\"><strong>Error:</strong> Setup cannot write to ./config/database.php.</p>";
647        HTMLFooter();
648        $system->cleanInstallation();
649        die();
650    }
651
652    // check LDAP if available
653    if (extension_loaded('ldap') && !is_null($ldapEnabled)) {
654        if (! $instanceSetup->createLdapFile($ldapSetup)) {
655            print "<p class=\"alert alert-error\"><strong>Error:</strong> Setup cannot write to ./config/ldap.php.</p>";
656            HTMLFooter();
657            $system->cleanInstallation();
658            die();
659        }
660    }
661
662    // connect to the database using config/database.php
663    require PMF_ROOT_DIR . '/config/database.php';
664    $db = PMF_Db::factory($dbSetup['dbType']);
665    $db->connect($DB['server'], $DB['user'], $DB['password'], $DB['db']);
666    if (!$db) {
667        print "<p class=\"alert alert-error\"><strong>DB Error:</strong> ".$db->error()."</p>\n";
668        HTMLFooter();
669        $system->cleanInstallation();
670        die();
671    }
672
673    require $dbSetup['dbType'] . '.sql.php'; // CREATE TABLES
674    require 'stopwords.sql.php';  // INSERTs for stopwords
675
676    $system->setDatabase($db);
677
678    print '<p>';
679
680    // Erase any table before starting creating the required ones
681    if (! PMF_System::isSqlite($dbSetup['dbType'])) {
682        $system->dropTables($uninst);
683    }
684    
685    // Start creating the required tables
686    $count = 0;
687    foreach ($query as $executeQuery) {
688        $result = @$db->query($executeQuery);
689        if (!$result) {
690            print '<p class="alert alert-error"><strong>Error:</strong> Please install your version of phpMyFAQ once again or send
691            us a <a href=\"http://www.phpmyfaq.de\" target=\"_blank\">bug report</a>.</p>';
692            printf('<p class="alert alert-error"><strong>DB error:</strong> %s</p>', $db->error());
693            printf('<code>%s</code>', htmlentities($executeQuery));
694            $system->dropTables($uninst);
695            $system->cleanInstallation();
696            HTMLFooter();
697            die();
698        }
699        usleep(2500);
700        $count++;
701        if (!($count % 10)) {
702            print '| ';
703        }
704    }
705
706    $link = new PMF_Link(null, $configuration);
707
708    // add main configuration, add personal settings
709    include 'configurationdata.php';
710    $mainConfig['spam.enableCaptchaCode']  = (extension_loaded('gd') ? 'true' : 'false');
711    $mainConfig['main.metaPublisher']      = $realname;
712    $mainConfig['main.administrationMail'] = $email;
713    $mainConfig['main.currentVersion']     = PMF_System::getVersion();
714    $mainConfig['main.currentApiVersion']  = PMF_System::getApiVersion();
715    $mainConfig['main.language']           = $language;
716    $mainConfig['main.languageDetection']  = 'true';
717    $mainConfig['main.phpMyFAQToken']      = md5(uniqid(rand()));
718    $mainConfig['security.permLevel']      = $permLevel;
719
720    foreach ($mainConfig as $name => $value) {
721        $configuration->add($name, $value);
722    }
723
724    $configuration->update(array('main.referenceURL' => $link->getSystemUri('/install/setup.php')));
725
726    // add admin account and rights
727    $admin = new PMF_User($configuration);
728    $admin->createUser($loginname, $password, 1);
729    $admin->setStatus('protected');
730    $adminData = array(
731        'display_name' => $realname,
732        'email'        => $email
733    );
734    $admin->setUserData($adminData);
735
736    // add default rights
737    include 'rightdata.php';
738    foreach ($mainRights as $right) {
739        $rightId = $admin->perm->addRight($right);
740        $admin->perm->grantUserRight($admin->getUserId(), $rightId);
741    }
742    
743    // Add anonymous user account
744    $anonymous = new PMF_User($configuration);
745    $anonymous->createUser('anonymous', null, -1);
746    $anonymous->setStatus('protected');
747    $anonymousData = array(
748        'display_name' => 'Anonymous User',
749        'email'        => null
750    );
751    $anonymous->setUserData($anonymousData);
752
753    // Add master instance
754    $instanceData = array(
755        'url'      => $link->getSystemUri($_SERVER['SCRIPT_NAME']),
756        'instance' => $link->getSystemRelativeUri('install/setup.php'),
757        'comment'  => 'phpMyFAQ ' . PMF_System::getVersion()
758    );
759    $faqInstance = new PMF_Instance($configuration);
760    $faqInstance->addInstance($instanceData);
761
762    $faqInstanceMaster = new PMF_Instance_Master($configuration);
763    $faqInstanceMaster->createMaster($faqInstance);
764
765    print '</p>';
766
767?>
768        <p class="alert alert-success">
769            Wow, looks like the installation worked like a charm. This is pretty cool, isn't it? :-)
770        </p>
771
772        <script type="text/javascript">
773        //<![CDATA[
774        var iframect = 0;
775
776        function iframeUpdated() {
777            if (iframect++ == 0) {
778                return;
779            }
780
781            $('#questionnaireForm').hide();
782            $('#questionnaireThanks').show();
783        }
784
785        function hide(item) {
786            $('#configliste').hide();
787        }
788
789        function show(item) {
790            $('#configliste').show();
791        }
792        //]]>
793        </script>
794        <iframe onload="iframeUpdated();" name="questionaireResult" style="display:none"></iframe>
795        <form action="http://www.phpmyfaq.de/stats/getstatdata.php" method="post" target="questionaireResult"
796              id="questionnaireForm" class="form-horizontal">
797
798            <p>
799                For further development we would like to get some feedback from our users. Therefore we'd ask you to
800                take a few minutes of your time to answer a few questions.
801            </p>
802            <p>
803                If you don't want to participate in the survey, you can directly visit
804                <a href="../index.php">your version of phpMyFAQ</a> or login into your
805                <a href="../admin/index.php">admin section</a>.
806            </p>
807
808            <fieldset>
809                <legend>General questions</legend>
810                <div class="control-group">
811                    <label class="control-label">How do you act like?</label>
812                        <div class="controls">
813                        <select name="q[individual]">
814                            <option>as an individual</option>
815                            <option>as an organisation</option>
816                        </select>
817                    </div>
818                </div>
819                <div class="control-group">
820                    <label class="control-label">What kind of organisation is that?</label>
821                    <div class="controls">
822                        <select name="q[organisation]">
823                             <option>private held</option>
824                             <option>public held</option>
825                             <option>government organisation</option>
826                             <option>foundation</option>
827                             <option>other</option>
828                         </select>
829                    </div>
830                </div>
831             </fieldset>
832
833            <fieldset>
834                <legend>Technical questions</legend>
835                <div class="control-group">
836                    <label class="control-label">Where did you installed phpMyFAQ?</label>
837                    <div class="controls">
838                        <select name="q[server]">
839                            <option>server run by a hosting company</option>
840                            <option>public server run by you/your organisation</option>
841                            <option>private server run by you/your organisation</option>
842                            <option>Don't know</option>
843                        </select>
844                    </div>
845                </div>
846            </fieldset>
847
848            <fieldset>
849                <legend>Beyond our own nose</legend>
850                <div class="control-group">
851                    <label class="control-label">Which PHP software do you also use?</label>
852                    <div class="controls">
853                        <input name="q[other]" />
854                    </div>
855                </div>
856                <div class="control-group">
857                    <label class="control-label">Are you using other web technologies?</label>
858                    <div class="controls">
859                        <label class="checkbox inline">
860                            <input type="checkbox" name="q[other][]" value="ASP" />ASP
861                        </label>
862                        <label class="checkbox inline">
863                            <input type="checkbox" name="q[other][]" value="ASP.NET" />ASP.NET
864                        </label>
865                        <label class="checkbox inline">
866                            <input type="checkbox" name="q[other][]" value="jsp" />JAVA JSP
867                        </label>
868                        <label class="checkbox inline">
869                            <input type="checkbox" name="q[other][]" value="perl" />Perl
870                        </label>
871                        <label class="checkbox inline">
872                            <input type="checkbox" name="q[other][]" value="ruby" />Ruby / Ruby on Rails
873                        </label>
874                        <label class="checkbox inline">
875                            <input type="checkbox" name="q[other][]" value="python" />Python
876                        </label>
877                        <label class="checkbox inline">
878                            <input type="checkbox" name="q[other][]" value="clojure" />Clojure
879                        </label>
880                        <label class="checkbox inline">
881                            <input type="checkbox" name="q[other][]" value="scala" />Scala
882                        </label>
883                        <label class="checkbox inline">
884                            <input type="checkbox" name="q[other][]" value="go" />Go
885                        </label>
886                    </div>
887                </div>
888             </fieldset>
889
890            <p class="alert alert-info">
891                Additional to your input we're going to submit some information about your system setup for statstic
892                purpose.
893            </p>
894            <p class="alert alert-info">
895                We are not storing any personal information. You can see the data by clicking
896                <a href="#" onclick="show('configliste'); return false;">here</a>.
897            </p>
898
899            <div id="configliste" class="hide">
900                <a href="#" onclick="hide('configliste'); return false;">hide again</a>
901                <dl>
902<?php
903$q = new PMF_Questionnaire_Data($mainConfig);
904$options = $q->get();
905array_walk($options, 'data_printer');
906echo '</dl><input type="hidden" name="systemdata" value="'.PMF_String::htmlspecialchars(serialize($q->get()), ENT_QUOTES).'" />';
907?>
908            </div>
909            <p style="text-align: center;">
910                <input class="btn-primary" type="submit" value="Click here to submit the data and finish the installation process" />
911            </p>
912        </form>
913        <div id="questionnaireThanks" style="display:none;">
914            <p class="alert alert-success"><b>Thank you for giving your feedback!</b></p>
915            <p>
916                You can visit <a href="../index.php">your version of phpMyFAQ</a> or login into your
917                <a href="../admin/index.php">admin section</a>.
918            </p>
919        </div>
920<?php
921    // Remove 'setup.php' file
922    if (@unlink(basename($_SERVER['SCRIPT_NAME']))) {
923        print "<p class=\"alert alert-success\">The file <em>./install/setup.php</em> was deleted automatically.</p>\n";
924    } else {
925        print "<p class=\"alert alert-info\">Please delete the file <em>./install/setup.php</em> manually.</p>\n";
926    }
927    // Remove 'update.php' file
928    if (@unlink(dirname($_SERVER["PATH_TRANSLATED"])."/update.php")) {
929        print "<p class=\"alert alert-success\">The file <em>./install/update.php</em> was deleted automatically.</p>\n";
930    } else {
931        print "<p class=\"alert alert-info\">Please delete the file <em>./install/update.php</em> manually.</p>\n";
932    }
933    
934    HTMLFooter();
935}