From 16da53707f58789e682c411b417847aeb74ba681 Mon Sep 17 00:00:00 2001 From: Simon Leblanc Date: Fri, 28 Nov 2014 05:12:21 +0100 Subject: [PATCH 1/2] First implementation of the installer --- app/inc/constants.php.template | 7 +- app/inc/init.php | 13 ++- install.mysql.auto.sql | 8 ++ install.php | 187 +++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 8 deletions(-) create mode 100644 install.mysql.auto.sql create mode 100644 install.php diff --git a/app/inc/constants.php.template b/app/inc/constants.php.template index 82567d6..da74053 100644 --- a/app/inc/constants.php.template +++ b/app/inc/constants.php.template @@ -20,11 +20,8 @@ // FRAMADATE version const VERSION = 0.8; -// Server name -const STUDS_URL = ''; - // Application name -const NOMAPPLICATION = ""; +const NOMAPPLICATION = ''; // Database administrator email const ADRESSEMAILADMIN = ''; @@ -36,7 +33,7 @@ const ADRESSEMAILREPONSEAUTO = ''; const BASE = ''; // Database user -const USERBASE = ""; +const USERBASE = ''; // Database password const USERPASSWD = ''; diff --git a/app/inc/init.php b/app/inc/init.php index 2a13d2b..912d6e5 100644 --- a/app/inc/init.php +++ b/app/inc/init.php @@ -23,9 +23,16 @@ if (ini_get('date.timezone') == '') { // Autoloading of dependencies with Composer require_once __DIR__ . '/../../vendor/autoload.php'; -include_once __DIR__ . '/constants.php'; -include_once __DIR__ . '/i18n.php'; +require_once __DIR__ . '/constants.php'; +require_once __DIR__ . '/i18n.php'; $connect = NewADOConnection(BASE_TYPE); -$connect->Connect(SERVEURBASE, USERBASE, USERPASSWD, BASE); + +if ('pdo' === BASE_TYPE) { + $database_host = 'mysql:host='.SERVEURBASE; +} else { + $database_host = SERVEURBASE; +} + +$connect->Connect($database_host, USERBASE, USERPASSWD, BASE); $err = 0; diff --git a/install.mysql.auto.sql b/install.mysql.auto.sql new file mode 100644 index 0000000..a42245e --- /dev/null +++ b/install.mysql.auto.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS `comments` (`id_comment` int(11) unsigned NOT NULL AUTO_INCREMENT, `id_sondage` char(16) NOT NULL, `comment` text NOT NULL, `usercomment` text, PRIMARY KEY (`id_comment`), KEY `id_sondage` (`id_sondage`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `sondage` (`id_sondage` char(16) NOT NULL, `commentaires` text, `mail_admin` varchar(128) DEFAULT NULL, `nom_admin` varchar(64) DEFAULT NULL, `titre` text, `id_sondage_admin` char(24) DEFAULT NULL, `date_creation` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `date_fin` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `format` varchar(2) DEFAULT NULL, `mailsonde` tinyint(1) DEFAULT '0', `statut` int(11) NOT NULL DEFAULT '1' COMMENT '1 = actif ; 0 = inactif ; ', UNIQUE KEY `id_sondage` (`id_sondage`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `sujet_studs` (`id_sondage` char(16) NOT NULL, `sujet` text, KEY `id_sondage` (`id_sondage`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `user_studs` (`id_users` int(11) unsigned NOT NULL AUTO_INCREMENT, `nom` varchar(64) NOT NULL, `id_sondage` char(16) NOT NULL, `reponses` text NOT NULL, PRIMARY KEY (`id_users`), KEY `id_sondage` (`id_sondage`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ; + +INSERT INTO `sondage` (`id_sondage`, `commentaires`, `mail_admin`, `nom_admin`, `titre`, `id_sondage_admin`, `date_fin`, `format`) VALUES ('aqg259dth55iuhwm','Repas de Noel du service','Stephanie@retaillard.com','Stephanie', 'Repas de Noel','aqg259dth55iuhwmy9d8jlwk', FROM_UNIXTIME('1627100361'),'D+'); +INSERT INTO `sujet_studs` (`id_sondage`, `sujet`) VALUES ('aqg259dth55iuhwm','1225839600@12h,1225839600@19h,1226012400@12h,1226012400@19h,1226876400@12h,1226876400@19h,1227049200@12h,1227049200@19h,1227826800@12h,1227826800@19h'); +INSERT INTO `user_studs` (`nom`, `id_sondage`, `reponses`, `id_users`) VALUES ('marcel','aqg259dth55iuhwm','0110111101','933'), ('paul','aqg259dth55iuhwm','1011010111','935'), ('sophie','aqg259dth55iuhwm','1110110000','945'), ('barack','aqg259dth55iuhwm','0110000','948'), ('takashi','aqg259dth55iuhwm','0000110100','951'), ('albert','aqg259dth55iuhwm','1010110','975'), ('alfred','aqg259dth55iuhwm','0110010','1135'), ('marcs','aqg259dth55iuhwm','0100001010','1143'), ('laure','aqg259dth55iuhwm','0011000','1347'), ('benda','aqg259dth55iuhwm','1101101100','1667'), ('Albert','aqg259dth55iuhwm','1111110011','1668'); diff --git a/install.php b/install.php new file mode 100644 index 0000000..0e12bad --- /dev/null +++ b/install.php @@ -0,0 +1,187 @@ +'; + 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( + '\'\'', + '\'\'', + '\'\'', + '\'\'', + '\'\'', + '\'\'', + '\'\'', + '\'\'', + ), + 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 '
'.$e->getMessage().'
'; + echo "
".$e->getTraceAsString()."
"; + die('installation failed'); + } +} +?> + + + + OpenSondage Installation + + + +
+

OpenSondage Installation

+
+
+ General + +
+ + +
+
+ + +
+
+ + +
+
+
+ Database + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + From 1a5846a156f797849842c63244fe737c5a632b9f Mon Sep 17 00:00:00 2001 From: Simon Leblanc Date: Sun, 30 Nov 2014 01:02:11 +0100 Subject: [PATCH 2/2] move install into his own folder and split code into class --- 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 0e12bad..0000000 --- a/install.php +++ /dev/null @@ -1,187 +0,0 @@ -'; - 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( - '\'\'', - '\'\'', - '\'\'', - '\'\'', - '\'\'', - '\'\'', - '\'\'', - '\'\'', - ), - 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 '
'.$e->getMessage().'
'; - echo "
".$e->getTraceAsString()."
"; - die('installation failed'); - } -} -?> - - - - OpenSondage Installation - - - -
-

OpenSondage Installation

-
-
- General - -
- - -
-
- - -
-
- - -
-
-
- Database - -
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - - diff --git a/install/InstallComposer.php b/install/InstallComposer.php new file mode 100644 index 0000000..a370f95 --- /dev/null +++ b/install/InstallComposer.php @@ -0,0 +1,90 @@ +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 0000000..3595312 --- /dev/null +++ b/install/InstallConfiguration.php @@ -0,0 +1,73 @@ + '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 0000000..749767b --- /dev/null +++ b/install/InstallSql.php @@ -0,0 +1,24 @@ +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 0000000..a758055 --- /dev/null +++ b/install/error.html @@ -0,0 +1,33 @@ + + + + + OpenSondage Installation + + + + + + +
+
+

+ OpenSondage +

+

Make your polls

+ +
+
+

OpenSondage Installation

+ + +
+
+ + diff --git a/install/install.css b/install/install.css new file mode 100644 index 0000000..18f3dc5 --- /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 0000000..004097a --- /dev/null +++ b/install/install.html @@ -0,0 +1,72 @@ + + + + + OpenSondage Installation + + + + + + +
+
+

+ OpenSondage +

+

Make your polls

+ +
+
+

OpenSondage Installation

+
+
+ General + +
+ + +
+
+ + +
+
+ + +
+
+
+ Database + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + 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 0000000..8cf9f3f --- /dev/null +++ b/install/install.php @@ -0,0 +1,43 @@ +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';