From 059f4c2fc75eecef383662ac3d103a6f4ddc5e75 Mon Sep 17 00:00:00 2001
From: Simon Leblanc <contact@leblanc-simon.eu>
Date: Sun, 30 Nov 2014 01:02:11 +0100
Subject: [PATCH] move install into his own folder and split code into class

(cherry picked from commit 1a5846a156f797849842c63244fe737c5a632b9f)
---
 install.php                                   | 187 ------------------
 install/InstallComposer.php                   |  90 +++++++++
 install/InstallConfiguration.php              |  73 +++++++
 install/InstallSql.php                        |  24 +++
 install/error.html                            |  33 ++++
 install/install.css                           |  11 ++
 install/install.html                          |  72 +++++++
 .../install.mysql.auto.sql                    |   0
 .../install.mysql.sql                         |   0
 install/install.php                           |  43 ++++
 10 files changed, 346 insertions(+), 187 deletions(-)
 delete mode 100644 install.php
 create mode 100644 install/InstallComposer.php
 create mode 100644 install/InstallConfiguration.php
 create mode 100644 install/InstallSql.php
 create mode 100644 install/error.html
 create mode 100644 install/install.css
 create mode 100644 install/install.html
 rename install.mysql.auto.sql => install/install.mysql.auto.sql (100%)
 rename install.mysql.sql => install/install.mysql.sql (100%)
 create mode 100644 install/install.php

diff --git a/install.php b/install.php
deleted file mode 100644
index 0e12bad6..00000000
--- a/install.php
+++ /dev/null
@@ -1,187 +0,0 @@
-<?php
-
-$configuration_file = __DIR__.'/app/inc/constants.php';
-
-if (file_exists($configuration_file) === true) {
-    header('Location: index.php');
-    exit;
-}
-
-if (isset($_POST['install']) === true) {
-    ini_set('max_execution_time', 0);
-    ob_start();
-
-    // Composer exist ?
-    $locations = array(
-        __DIR__.'/composer.phar',
-        //'/usr/bin/composer',
-        '/usr/bin/composer.phar',
-        //'/usr/local/bin/composer',
-        '/usr/local/bin/composer.phar',
-    );
-    $composer = null;
-    foreach ($locations as $location) {
-        if (file_exists($location) === true) {
-            $composer = $location;
-            break;
-        }
-    }
-
-    // If composer not found, download it !
-    if (null === $composer) {
-        if (!file_put_contents(__DIR__.'/composer.phar', file_get_contents('https://getcomposer.org/composer.phar'))) {
-            die('Installation impossible : impossible to find composer !');
-        }
-        $composer = __DIR__.'/composer.phar';
-    }
-
-    try {
-        echo "Utilisation de ".$composer.'<br />';
-        ob_flush();
-        flush();
-
-        require_once 'phar://'.$composer.'/src/bootstrap.php';
-        ob_flush();
-        flush();
-
-        $composer_home = getenv('COMPOSER_HOME');
-        $personal_home = getenv('HOME');
-        if (empty($composer_home) === true && empty($personal_home) === true) {
-            putenv('COMPOSER_HOME='.sys_get_temp_dir());
-        }
-
-        $application = new \Composer\Console\Application();
-        $application->setAutoExit(false);
-        $command = $application->find('install');
-        $input = new \Symfony\Component\Console\Input\ArrayInput(array(
-            'command' => 'install',
-            '-d' => __DIR__,
-            '-vvv',
-            '--optimize-autoloader',
-        ));
-        $fhandle = fopen('php://output', 'wb');
-        $output = new \Symfony\Component\Console\Output\StreamOutput($fhandle);
-
-        $application->run($input, $output);
-        fclose($fhandle);
-        ob_flush();
-        flush();
-
-        // Save configuration
-        $configuration = file_get_contents($configuration_file.'.template');
-        if (false === $configuration) {
-            throw new \Exception('Impossible to read template configuration');
-        }
-
-        $configuration = str_replace(
-            array(
-                '\'<Application name>\'',
-                '\'<email address>\'',
-                '\'<no-reply@mydomain.com>\'',
-                '\'<database name>\'',
-                '\'<database user>\'',
-                '\'<database password>\'',
-                '\'<database server>\'',
-                '\'<database type>\'',
-            ),
-            array(
-                var_export($_POST['title'], true),
-                var_export($_POST['email'], true),
-                var_export($_POST['no-reply-email'], true),
-                var_export($_POST['db-name'], true),
-                var_export($_POST['db-user'], true),
-                var_export($_POST['db-pass'], true),
-                var_export($_POST['db-host'], true),
-                var_export($_POST['db-type'], true),
-            ),
-            $configuration
-        );
-
-        if (file_put_contents($configuration_file, $configuration) === false) {
-            throw new \Exception('Impossible to save configuration');
-        }
-
-        // Inject database
-        require_once __DIR__.'/app/inc/init.php';
-
-        $sqls = explode("\n", file_get_contents(__DIR__.'/install.mysql.auto.sql'));
-        foreach ($sqls as $sql) {
-            $sql = trim($sql);
-            if (empty($sql) === true) {
-                continue;
-            }
-
-            $query = $connect->Prepare($sql);
-            $cleaning = $connect->Execute($query);
-        }
-
-        ob_flush();
-        flush();
-        ob_end_clean();
-    } catch (Exception $e) {
-        echo '<br /><b>'.$e->getMessage().'</b><br />';
-        echo "<pre>".$e->getTraceAsString()."</pre>";
-        die('installation failed');
-    }
-}
-?><!DOCTYPE html>
-<html lang="en">
-    <head>
-        <meta charset="utf-8" />
-        <title>OpenSondage Installation</title>
-        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
-    </head>
-    <body>
-        <div class="container">
-            <h1>OpenSondage Installation</h1>
-            <form action="" method="post" role="form">
-                <fieldset>
-                    <legend>General</legend>
-
-                    <div class="form-group">
-                        <label for="title">Title</label>
-                        <input type="text" class="form-control" id="title" name="title" placeholder="Application name" required>
-                    </div>
-                    <div class="form-group">
-                        <label for="email">Administrator email</label>
-                        <input type="email" class="form-control" id="email" name="email" placeholder="Email of the administrator" required>
-                    </div>
-                    <div class="form-group">
-                        <label for="no-reply-email">No-reply email</label>
-                        <input type="email" class="form-control" id="no-reply-email" name="no-reply-email" placeholder="Email for automatic responses" required>
-                    </div>
-                </fieldset>
-                <fieldset>
-                    <legend>Database</legend>
-
-                    <div class="form-group">
-                        <label for="db-type">Type</label>
-                        <select name="db-type" id="db-type" required>
-                            <option value="pdo">PDO - MySQL</option>
-                            <option value="mysql">MySQL</option>
-                        </select>
-                    </div>
-                    <div class="form-group">
-                        <label for="db-host">Host</label>
-                        <input type="text" class="form-control" id="db-host" name="db-host" value="localhost" required>
-                    </div>
-                    <div class="form-group">
-                        <label for="db-name">Database name</label>
-                        <input type="text" class="form-control" id="db-name" name="db-name" value="opensondage" required>
-                    </div>
-                    <div class="form-group">
-                        <label for="db-user">Username</label>
-                        <input type="text" class="form-control" id="db-user" name="db-user" value="root" required>
-                    </div>
-                    <div class="form-group">
-                        <label for="db-pass">Password</label>
-                        <input type="password" class="form-control" id="db-pass" name="db-pass" value="">
-                    </div>
-                </fieldset>
-
-                <input type="submit" class="btn btn-default" name="install" value="Install">
-            </form>
-        </div>
-    </body>
-</html>
-
diff --git a/install/InstallComposer.php b/install/InstallComposer.php
new file mode 100644
index 00000000..a370f958
--- /dev/null
+++ b/install/InstallComposer.php
@@ -0,0 +1,90 @@
+<?php
+
+class InstallComposer
+{
+    /**
+     * @var string
+     */
+    private $composer;
+
+    /**
+     * @return bool
+     */
+    public function check()
+    {
+        return file_exists(dirname(__DIR__).'/vendor/autoload.php');
+    }
+
+    public function install()
+    {
+        require_once 'phar://'.$this->getComposer().'/src/bootstrap.php';
+
+        $this->initEnv();
+
+        $application = new \Composer\Console\Application();
+        $application->setAutoExit(false);
+
+        $input = new \Symfony\Component\Console\Input\ArrayInput(array(
+            'command'   => 'install',
+            '-d'        => __DIR__.'/..',
+            '-vvv',
+            '--optimize-autoloader',
+        ));
+        $output = new \Symfony\Component\Console\Output\NullOutput();
+
+        $application->run($input, $output);
+    }
+
+    /**
+     * @return string
+     */
+    private function getComposer()
+    {
+        if (null === $this->composer) {
+            $this->initComposer();
+        }
+
+        return $this->composer;
+    }
+
+    private function initComposer()
+    {
+        // Composer exist ?
+        $locations = array(
+            __DIR__.'/../composer.phar',
+            '/usr/bin/composer.phar',
+            '/usr/local/bin/composer.phar',
+        );
+
+        $this->composer = null;
+        foreach ($locations as $location) {
+            if (file_exists($location) === true) {
+                $this->composer = $location;
+                break;
+            }
+        }
+
+        // If composer not found, download it !
+        if (null === $this->composer) {
+            if (!file_put_contents(
+                __DIR__.'/../composer.phar',
+                file_get_contents('https://getcomposer.org/composer.phar')
+            )
+            ) {
+                throw new \Exception('Impossible to download composer');
+            }
+
+            $this->composer = __DIR__.'/../composer.phar';
+        }
+    }
+
+    private function initEnv()
+    {
+        $composer_home = getenv('COMPOSER_HOME');
+        $personal_home = getenv('HOME');
+        if (empty($composer_home) === true && empty($personal_home) === true) {
+            putenv('COMPOSER_HOME='.sys_get_temp_dir());
+        }
+    }
+
+}
diff --git a/install/InstallConfiguration.php b/install/InstallConfiguration.php
new file mode 100644
index 00000000..3595312e
--- /dev/null
+++ b/install/InstallConfiguration.php
@@ -0,0 +1,73 @@
+<?php
+
+class InstallConfiguration
+{
+    /**
+     * @var array
+     */
+    private $datas;
+
+    /**
+     * @var array
+     */
+    private $checks = array(
+        'title' => 'Application name',
+        'email' => 'email address',
+        'no-reply-email' => 'no-reply@mydomain.com',
+        'db-name' => 'database name',
+        'db-user' => 'database user',
+        'db-pass' => 'database password',
+        'db-host' => 'database server',
+        'db-type' => 'database type',
+    );
+
+    /**
+     * @param array     $datas
+     */
+    public function __construct(array $datas)
+    {
+        $this->datas = $datas;
+    }
+
+    /**
+     * @return bool
+     */
+    public function checkValues()
+    {
+        foreach (array_keys($this->checks) as $key) {
+            if (isset($this->datas[$key]) === false) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    public function copy($template, $destination)
+    {
+        $configuration = file_get_contents($template);
+        if (false === $configuration) {
+            throw new \Exception('Impossible to read template configuration');
+        }
+
+        $configuration = $this->convertConfigurationFile($configuration);
+
+        if (file_put_contents($destination, $configuration) === false) {
+            throw new \Exception('Impossible to save configuration');
+        }
+    }
+
+
+    private function convertConfigurationFile($content)
+    {
+        foreach ($this->checks as $replace => $search) {
+            $content = str_replace(
+                '\'<'.$search.'>\'',
+                var_export($this->datas[$replace], true),
+                $content
+            );
+        }
+
+        return $content;
+    }
+}
diff --git a/install/InstallSql.php b/install/InstallSql.php
new file mode 100644
index 00000000..749767b2
--- /dev/null
+++ b/install/InstallSql.php
@@ -0,0 +1,24 @@
+<?php
+
+class InstallSql
+{
+    public function inject()
+    {
+        require_once __DIR__.'/../app/inc/init.php';
+
+        if ($connect->ErrorMsg() !== '') {
+            throw new \Exception('Bad database configuration : '.$connect->ErrorMsg());
+        }
+
+        $sqls = explode("\n", file_get_contents(__DIR__.'/install.mysql.auto.sql'));
+        foreach ($sqls as $sql) {
+            $sql = trim($sql);
+            if (empty($sql) === true) {
+                continue;
+            }
+
+            $query = $connect->Prepare($sql);
+            $cleaning = $connect->Execute($query);
+        }
+    }
+}
diff --git a/install/error.html b/install/error.html
new file mode 100644
index 00000000..a7580550
--- /dev/null
+++ b/install/error.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8" />
+        <title>OpenSondage Installation</title>
+        <link rel="stylesheet" href="../css/bootstrap.min.css">
+        <link rel="stylesheet" href="../css/style.css">
+        <link rel="stylesheet" href="../css/frama.css">
+        <link rel="stylesheet" href="install.css">
+    </head>
+    <body>
+        <div class="container ombre">
+            <header role="banner">
+                <h1>
+                    <img src="../images/logo-framadate.png" width="360" height="50" alt="OpenSondage" />
+                </h1>
+                <h2>Make your polls</h2>
+                <hr class="trait" role="presentation">
+            </header>
+            <main role="main">
+                <h3>OpenSondage Installation</h3>
+                <div class="alert alert-danger" role="alert">
+                    <?php echo htmlspecialchars($e->getMessage(), ENT_COMPAT | ENT_HTML401, 'UTF-8') ?>
+                </div>
+                <div class="alert alert-info" role="alert">
+                    <pre>
+                    <?php echo htmlspecialchars($e->getTraceAsString(), ENT_COMPAT | ENT_HTML401, 'UTF-8') ?>
+                    </pre>
+                </div>
+            </main>
+        </div>
+    </body>
+</html>
diff --git a/install/install.css b/install/install.css
new file mode 100644
index 00000000..18f3dc53
--- /dev/null
+++ b/install/install.css
@@ -0,0 +1,11 @@
+header {
+    padding-bottom: 0;
+}
+
+main {
+    padding-top: 0;
+}
+
+fieldset {
+    margin: 1.5em 0;
+}
\ No newline at end of file
diff --git a/install/install.html b/install/install.html
new file mode 100644
index 00000000..004097a9
--- /dev/null
+++ b/install/install.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8" />
+        <title>OpenSondage Installation</title>
+        <link rel="stylesheet" href="../css/bootstrap.min.css">
+        <link rel="stylesheet" href="../css/style.css">
+        <link rel="stylesheet" href="../css/frama.css">
+        <link rel="stylesheet" href="install.css">
+    </head>
+    <body>
+        <div class="container ombre">
+            <header role="banner">
+                <h1>
+                    <img src="../images/logo-framadate.png" width="360" height="50" alt="OpenSondage" />
+                </h1>
+                <h2>Make your polls</h2>
+                <hr class="trait" role="presentation">
+            </header>
+            <main role="main">
+                <h3>OpenSondage Installation</h3>
+                <form action="" method="post" role="form">
+                    <fieldset>
+                        <legend>General</legend>
+
+                        <div class="form-group">
+                            <label for="title">Title</label>
+                            <input type="text" class="form-control" id="title" name="title" placeholder="Application name" required>
+                        </div>
+                        <div class="form-group">
+                            <label for="email">Administrator email</label>
+                            <input type="email" class="form-control" id="email" name="email" placeholder="Email of the administrator" required>
+                        </div>
+                        <div class="form-group">
+                            <label for="no-reply-email">No-reply email</label>
+                            <input type="email" class="form-control" id="no-reply-email" name="no-reply-email" placeholder="Email for automatic responses" required>
+                        </div>
+                    </fieldset>
+                    <fieldset>
+                        <legend>Database</legend>
+
+                        <div class="form-group">
+                            <label for="db-type">Type</label>
+                            <select name="db-type" id="db-type" required>
+                                <option value="pdo">PDO - MySQL (recommanded)</option>
+                                <option value="mysql">MySQL</option>
+                            </select>
+                        </div>
+                        <div class="form-group">
+                            <label for="db-host">Host</label>
+                            <input type="text" class="form-control" id="db-host" name="db-host" value="localhost" required>
+                        </div>
+                        <div class="form-group">
+                            <label for="db-name">Database name</label>
+                            <input type="text" class="form-control" id="db-name" name="db-name" value="opensondage" required>
+                        </div>
+                        <div class="form-group">
+                            <label for="db-user">Username</label>
+                            <input type="text" class="form-control" id="db-user" name="db-user" value="root" required>
+                        </div>
+                        <div class="form-group">
+                            <label for="db-pass">Password</label>
+                            <input type="password" class="form-control" id="db-pass" name="db-pass" value="">
+                        </div>
+                    </fieldset>
+
+                    <input type="submit" class="btn btn-success" name="install" value="Install">
+                </form>
+            </main>
+        </div>
+    </body>
+</html>
diff --git a/install.mysql.auto.sql b/install/install.mysql.auto.sql
similarity index 100%
rename from install.mysql.auto.sql
rename to install/install.mysql.auto.sql
diff --git a/install.mysql.sql b/install/install.mysql.sql
similarity index 100%
rename from install.mysql.sql
rename to install/install.mysql.sql
diff --git a/install/install.php b/install/install.php
new file mode 100644
index 00000000..8cf9f3f9
--- /dev/null
+++ b/install/install.php
@@ -0,0 +1,43 @@
+<?php
+
+require_once __DIR__.'/InstallComposer.php';
+require_once __DIR__.'/InstallConfiguration.php';
+require_once __DIR__.'/InstallSql.php';
+
+$configuration_file = __DIR__.'/../app/inc/constants.php';
+
+if (file_exists($configuration_file) === true) {
+    header('Location: ../index.php');
+    exit;
+}
+
+if (isset($_POST['install']) === true) {
+    try {
+        // Composer installation
+        $composer = new InstallComposer();
+        if ($composer->check() === false) {
+            ini_set('max_execution_time', 0);
+            $composer->install();
+        }
+
+        // Save configuration
+        $configuration = new InstallConfiguration($_POST);
+        if ($configuration->checkValues() === false) {
+            throw new \Exception('Bad value for configuration');
+        }
+
+        $configuration->copy($configuration_file.'.template', $configuration_file);
+
+        // Inject database
+        $sql = new InstallSql();
+        $sql->inject();
+
+        header('Location: ../index.php');
+        die();
+    } catch (Exception $e) {
+        require_once __DIR__.'/error.html';
+        die();
+    }
+}
+
+require_once __DIR__.'/install.html';
-- 
GitLab