3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.8 - 2013-01-09
37 namespace Galette\Core
;
41 use Laminas\Db\Adapter\Adapter
;
44 * Galette installation
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2013-2014 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.8 - 2013-01-09
57 public const STEP_CHECK
= 0;
58 public const STEP_TYPE
= 1;
59 public const STEP_DB
= 2;
60 public const STEP_DB_CHECKS
= 3;
61 public const STEP_VERSION
= 4; //only for update
62 public const STEP_DB_UPGRADE
= 5;
63 public const STEP_DB_INSTALL
= 6;
64 public const STEP_ADMIN
= 7;
65 public const STEP_GALETTE_INIT
= 8;
66 public const STEP_END
= 9;
68 public const INSTALL
= 'i';
69 public const UPDATE
= 'u';
71 //db version/galette version mapper
72 private $versions_mapper = array(
83 private $_installed_version;
92 private $_db_connected;
95 private $_admin_login;
103 public function __construct()
105 $this->_step
= self
::STEP_CHECK
;
107 $this->_version
= str_replace('v', '', GALETTE_VERSION
);
108 $this->_db_connected
= false;
109 $this->_db_prefix
= null;
113 * Return current step title
117 public function getStepTitle()
120 switch ($this->_step
) {
121 case self
::STEP_CHECK
:
122 $step_title = _T("Checks");
124 case self
::STEP_TYPE
:
125 $step_title = _T("Installation mode");
128 $step_title = _T("Database");
130 case self
::STEP_DB_CHECKS
:
131 $step_title = _T("Database access and permissions");
133 case self
::STEP_VERSION
:
134 $step_title = _T("Previous version selection");
136 case self
::STEP_DB_UPGRADE
:
137 $step_title = _T("Datapase upgrade");
139 case self
::STEP_DB_INSTALL
:
140 $step_title = _T("Tables Creation");
142 case self
::STEP_ADMIN
:
143 $step_title = _T("Admin parameters");
145 case self
::STEP_GALETTE_INIT
:
146 $step_title = _T("Galette initialization");
149 $step_title = _T("End!");
156 * HTML validation image
158 * @param boolean $arg Argument
160 * @return html string
162 public function getValidationImage($arg)
164 $img_name = ($arg === true) ?
'valid' : 'invalid';
165 $src = GALETTE_THEME_DIR
. 'images/icon-' . $img_name . '.png';
166 $alt = ($arg === true) ?
_T("Ok") : _T("Ko");
167 $img = '<img src="' . $src . '" alt="' . $alt . '"/>';
176 public function getMode()
186 public function isInstall()
188 return $this->_mode
=== self
::INSTALL
;
196 public function isUpgrade()
198 return $this->_mode
=== self
::UPDATE
;
202 * Set installation mode
204 * @param char $mode Requested mode
208 public function setMode($mode)
210 if ($mode === self
::INSTALL ||
$mode === self
::UPDATE
) {
211 $this->_mode
= $mode;
213 throw new \
UnexpectedValueException('Unknown mode "' . $mode . '"');
218 * Go back to previous step
222 public function atPreviousStep()
224 if ($this->_step
> 0) {
226 $this->_step
- 1 !== self
::STEP_DB_INSTALL
227 && $this->_step
!== self
::STEP_END
229 if ($this->_step
=== self
::STEP_DB_INSTALL
) {
230 $this->_step
= self
::STEP_DB_CHECKS
;
232 if ($this->_step
=== self
::STEP_DB_UPGRADE
) {
233 $this->setInstalledVersion(null);
235 $this->_step
= $this->_step
- 1;
239 if ($this->_step
=== self
::STEP_END
) {
240 $msg = 'Ok man, install is finished already!';
242 $msg = 'It is forbidden to rerun database install!';
244 Analog
::log($msg, Analog
::WARNING
);
250 * Are we at check step?
254 public function isCheckStep()
256 return $this->_step
=== self
::STEP_CHECK
;
260 * Set step to type of installation
264 public function atTypeStep()
266 $this->_step
= self
::STEP_TYPE
;
270 * Are we at type step?
274 public function isTypeStep()
276 return $this->_step
=== self
::STEP_TYPE
;
280 * Set step to database information
284 public function atDbStep()
286 $this->_step
= self
::STEP_DB
;
290 * Are we at database step?
294 public function isDbStep()
296 return $this->_step
=== self
::STEP_DB
;
304 public function postCheckDb()
306 return $this->_step
> self
::STEP_DB_CHECKS
;
312 * @param string $type Database type
313 * @param array $errs Errors array
317 public function setDbType($type, &$errs)
322 $this->_db_type
= $type;
325 $errs[] = _T("Database type unknown");
334 public function getDbType()
336 return $this->_db_type
;
340 * Set connection information
342 * @param string $host Database host
343 * @param string $port Database port
344 * @param string $name Database name
345 * @param string $user Database user name
346 * @param string $pass Database user's password
350 public function setDsn($host, $port, $name, $user, $pass)
352 $this->_db_host
= $host;
353 $this->_db_port
= $port;
354 $this->_db_name
= $name;
355 $this->_db_user
= $user;
356 $this->_db_pass
= $pass;
362 * @param string $prefix Prefix
366 public function setTablesPrefix($prefix)
368 $this->_db_prefix
= $prefix;
372 * Retrieve database host
376 public function getDbHost()
378 return $this->_db_host
;
382 * Retrieve database port
386 public function getDbPort()
388 return $this->_db_port
;
392 * Retrieve database name
396 public function getDbName()
398 return $this->_db_name
;
402 * Retrieve database user
406 public function getDbUser()
408 return $this->_db_user
;
412 * Retrieve database password
416 public function getDbPass()
418 return $this->_db_pass
;
422 * Retrieve tables prefix
426 public function getTablesPrefix()
428 return $this->_db_prefix
;
432 * Set step to database checks
436 public function atDbCheckStep()
438 $this->_step
= self
::STEP_DB_CHECKS
;
442 * Are we at database check step?
446 public function isDbCheckStep()
448 return $this->_step
=== self
::STEP_DB_CHECKS
;
452 * Test database connection
454 * @return true|array true if connection was successfull,
455 * an array with some infos otherwise
457 public function testDbConnexion()
459 return Db
::testConnectivity(
470 * Is database connexion ok?
474 public function isDbConnected()
476 return $this->_db_connected
;
480 * Set step to version selection
484 public function atVersionSelection()
486 $this->_step
= self
::STEP_VERSION
;
490 * Are we at version selection step?
494 public function isVersionSelectionStep()
496 return $this->_step
=== self
::STEP_VERSION
;
500 * Set step to database installation
504 public function atDbInstallStep()
506 $this->_step
= self
::STEP_DB_INSTALL
;
510 * Are we at db installation step?
514 public function isDbinstallStep()
516 return $this->_step
=== self
::STEP_DB_INSTALL
;
520 * Set step to database upgrade
524 public function atDbUpgradeStep()
526 $this->_step
= self
::STEP_DB_UPGRADE
;
530 * Are we at db upgrade step?
534 public function isDbUpgradeStep()
536 return $this->_step
=== self
::STEP_DB_UPGRADE
;
541 * Install/Update SQL scripts
543 * @param string $path Path to scripts (defaults to core scripts)
547 public function getScripts($path = null)
549 if ($path === null) {
550 $path = GALETTE_ROOT
. '/install';
552 $update_scripts = array();
554 if ($this->isUpgrade()) {
555 $update_scripts = self
::getUpdateScripts(
558 $this->_installed_version
561 $update_scripts['current'] = $this->_db_type
. '.sql';
564 return $update_scripts;
568 * List updates scripts from given path
570 * @param string $path Scripts path
571 * @param string $db_type Database type
572 * @param string $version Previous version, defaults to null
574 * @return array If a previous version is provided, update scripts
575 * file path from this one to the latest will be returned.
576 * If no previous version is provided, that will return all
577 * updates versions known.
579 public static function getUpdateScripts(
584 $dh = opendir($path . '/scripts');
585 $php_update_scripts = array();
586 $sql_update_scripts = array();
588 while (($file = readdir($dh)) !== false) {
589 if (preg_match("/upgrade-to-(.*).php/", $file, $ver)) {
590 if ($version === null) {
591 $php_update_scripts[$ver[1]] = $ver[1];
593 if ($version < $ver[1]) {
594 $php_update_scripts[$ver[1]] = $file;
600 "/upgrade-to-(.*)-" . $db_type . ".sql/",
605 if ($version === null) {
606 $sql_update_scripts[$ver[1]] = $ver[1];
608 if ($version < $ver[1]) {
609 $sql_update_scripts[$ver[1]] = $file;
614 $update_scripts = array_merge($sql_update_scripts, $php_update_scripts);
616 ksort($update_scripts);
618 return $update_scripts;
622 * Execute SQL scripts
624 * @param Galette\Core\Db $zdb Database instance
625 * @param string $spath Path to scripts
629 public function executeScripts($zdb, $spath = null)
631 $queries_results = array();
632 $fatal_error = false;
633 $update_scripts = $this->getScripts($spath);
634 $this->_report
= array();
635 $scripts_path = ($spath ?? GALETTE_ROOT
. '/install') . '/scripts/';
637 foreach ($update_scripts as $key => $val) {
638 if (substr($val, -strlen('.sql')) === '.sql') {
639 //just a SQL script, run it
640 $script = fopen($scripts_path . $val, 'r');
642 if ($script === false) {
643 throw new \
RuntimeException(
644 'Unable to read SQL script from ' . $scripts_path . $val
650 @filesize
($scripts_path . $val)
653 $sql_res = $this->executeSql($zdb, $sql_query);
658 //we got an update class
659 include_once $scripts_path . $val;
660 $className = '\Galette\Updates\UpgradeTo' .
661 str_replace('.', '', $key);
667 $updater = new $className();
668 if ($updater instanceof \Galette\Updater\AbstractUpdater
) {
669 $updater->run($zdb, $this);
670 $ret = $updater->getReport();
671 $this->_report
= array_merge($this->_report
, $ret);
675 'Update class does not extends AbstractUpdater!',
680 $ret['message'] = str_replace(
683 _T("%version script has been successfully executed :)")
686 $this->_report
[] = $ret;
687 } catch (\RuntimeException
$e) {
692 $ret['message'] = str_replace(
695 _T("Unable to run %version update script :(")
698 $this->_report
[] = $ret;
703 str_replace('%s', $key, 'Upgrade to %s complete'),
708 return !$fatal_error;
712 * Executes SQL queries
714 * @param Db $zdb Database instance
715 * @param string $sql_query SQL instructions
719 public function executeSql($zdb, $sql_query)
721 $fatal_error = false;
723 // begin : copyright (2002) the phpbb group (support@phpbb.com)
724 // load in the sql parser
725 include_once GALETTE_ROOT
. 'includes/sql_parse.php';
727 $sql_query = preg_replace('/galette_/', $this->_db_prefix
, $sql_query);
728 $sql_query = remove_remarks($sql_query);
730 $sql_query = split_sql_file($sql_query, ';');
732 $zdb->connection
->beginTransaction();
734 $sql_size = sizeof($sql_query);
735 for ($i = 0; $i < $sql_size; $i++
) {
736 $query = trim($sql_query[$i]);
737 if ($query != '' && $query[0] != '-') {
747 Adapter
::QUERY_MODE_EXECUTE
750 } catch (Throwable
$e) {
751 $log_lvl = Analog
::WARNING
;
752 //if error are on drop, DROP, rename or RENAME we can continue
753 $parts = explode(' ', $query, 1);
755 (strcasecmp(trim($parts[0]), 'drop') != 0)
756 && (strcasecmp(trim($parts[0]), 'rename') != 0)
758 $log_lvl = Analog
::ERROR
;
759 $ret['debug'] = $e->getMessage();
760 $ret['query'] = $query;
767 'Error executing query | ' . $e->getMessage(),
772 $queries_results[] = $ret;
777 $zdb->connection
->rollBack();
780 $zdb->connection
->commit();
781 } catch (\PDOException
$e) {
782 //to avoid php8/mysql autocommit issue
783 if ($zdb->isPostgres() ||
(!$zdb->isPostgres() && !str_contains($e->getMessage(), 'no active transaction'))) {
789 $this->_report
= array_merge($this->_report
, $queries_results);
790 return !$fatal_error;
794 * Retrieve database installation report
798 public function getDbInstallReport()
800 return $this->_report
;
804 * Reinitialize report array
808 public function reinitReport()
810 $this->_report
= array();
814 * Set step to super admin information
818 public function atAdminStep()
820 $this->_step
= self
::STEP_ADMIN
;
824 * Are we at super admin information step?
828 public function isAdminStep()
830 return $this->_step
=== self
::STEP_ADMIN
;
834 * Set super administrator information
836 * @param string $login Login
837 * @param string $pass Password
841 public function setAdminInfos($login, $pass)
843 $this->_admin_login
= $login;
844 $this->_admin_pass
= password_hash($pass, PASSWORD_BCRYPT
);
848 * Retrieve super admin login
852 public function getAdminLogin()
854 return $this->_admin_login
;
858 * Retrieve super admin password
862 public function getAdminPass()
864 return $this->_admin_pass
;
868 * Set step to Galette initialization
872 public function atGaletteInitStep()
874 $this->_step
= self
::STEP_GALETTE_INIT
;
878 * Are we at Galette initialization step?
882 public function isGaletteInitStep()
884 return $this->_step
=== self
::STEP_GALETTE_INIT
;
888 * Load existing config
890 * @param array $post_data Data posted
891 * @param array $error_detected Errors array
895 public function loadExistingConfig($post_data, &$error_detected)
897 if (file_exists(GALETTE_CONFIG_PATH
. 'config.inc.php')) {
898 $existing = $this->loadExistingConfigFile($post_data);
900 if ($existing['db_type'] !== null) {
901 $this->setDbType($existing['db_type'], $error_detected);
905 $existing['db_host'] !== null
906 ||
$existing['db_user'] !== null
907 ||
$existing['db_name'] !== null
910 $existing['db_host'],
911 $existing['db_port'],
912 $existing['db_name'],
913 $existing['db_user'],
918 if ($existing['prefix'] !== null) {
919 $this->setTablesPrefix(
927 * Load contents from existing config file
929 * @param array $post_data Data posted
930 * @param boolean $pass Retrieve password
934 private function loadExistingConfigFile($post_data = array(), $pass = false)
945 if (file_exists(GALETTE_CONFIG_PATH
. 'config.inc.php')) {
946 $conf = file_get_contents(GALETTE_CONFIG_PATH
. 'config.inc.php');
947 if ($conf !== false) {
948 if (!isset($post_data['install_dbtype'])) {
950 '/TYPE_DB["\'], ["\'](.*)["\']\);/',
954 if (isset($matches[1])) {
955 $existing['db_type'] = $matches[1];
958 if (!isset($post_data['install_dbhost'])) {
960 '/HOST_DB["\'], ["\'](.*)["\']\);/',
964 if (isset($matches[1])) {
965 $existing['db_host'] = $matches[1];
968 if (!isset($post_data['install_dbport'])) {
970 '/PORT_DB["\'], ["\'](.*)["\']\);/',
974 if (isset($matches[1])) {
975 $existing['db_port'] = $matches[1];
978 if (!isset($post_data['install_dbuser'])) {
980 '/USER_DB["\'], ["\'](.*)["\']\);/',
984 if (isset($matches[1])) {
985 $existing['db_user'] = $matches[1];
988 if (!isset($post_data['install_dbname'])) {
990 '/NAME_DB["\'], ["\'](.*)["\']\);/',
994 if (isset($matches[1])) {
995 $existing['db_name'] = $matches[1];
1000 if (!isset($post_data['install_dbprefix'])) {
1002 '/PREFIX_DB["\'], ["\'](.*)["\']\);/',
1006 if (isset($matches[1])) {
1007 $existing['prefix'] = $matches[1];
1011 if ($pass === true) {
1013 '/PWD_DB["\'], ["\'](.*)["\']\);/',
1017 if (isset($matches[1])) {
1018 $existing['pwd_db'] = $matches[1];
1028 * Write configuration file to disk
1032 public function writeConfFile()
1036 'message' => _T("Write configuration file"),
1040 //if config file is already up-to-date, nothing to write
1041 $existing = $this->loadExistingConfigFile(array(), true);
1044 isset($existing['db_type'])
1045 && $existing['db_type'] == $this->_db_type
1046 && isset($existing['db_host'])
1047 && $existing['db_host'] == $this->_db_host
1048 && isset($existing['db_port'])
1049 && $existing['db_port'] == $this->_db_port
1050 && isset($existing['db_user'])
1051 && $existing['db_user'] == $this->_db_user
1052 && isset($existing['pwd_db'])
1053 && $existing['pwd_db'] == $this->_db_pass
1054 && isset($existing['db_name'])
1055 && $existing['db_name'] == $this->_db_name
1056 && isset($existing['prefix'])
1057 && $existing['prefix'] == $this->_db_prefix
1060 'Config file is already up-to-date, nothing to do.',
1064 $this->_report
[] = array(
1065 'message' => _T("Config file already exists and is up to date"),
1071 $conffile = GALETTE_CONFIG_PATH
. 'config.inc.php';
1073 is_writable(GALETTE_CONFIG_PATH
)
1074 && (!file_exists($conffile) ||
file_exists($conffile) && is_writable($conffile))
1075 && $fd = @fopen
($conffile, 'w')
1078 define('TYPE_DB', '" . $this->_db_type
. "');
1079 define('HOST_DB', '" . $this->_db_host
. "');
1080 define('PORT_DB', '" . $this->_db_port
. "');
1081 define('USER_DB', '" . $this->_db_user
. "');
1082 define('PWD_DB', '" . $this->_db_pass
. "');
1083 define('NAME_DB', '" . $this->_db_name
. "');
1084 define('PREFIX_DB', '" . $this->_db_prefix
. "');
1089 Analog
::log('Configuration file written on disk', Analog
::INFO
);
1094 _T("Unable to create configuration file (%path)")
1096 Analog
::log($str, Analog
::WARNING
);
1097 $ret['error'] = $str;
1100 $this->_report
[] = $ret;
1105 * Initialize Galette relevant objects
1107 * @param I18n $i18n I18n
1108 * @param Db $zdb Database instance
1109 * @param Login $login Loged in instance
1113 public function initObjects(I18n
$i18n, Db
$zdb, Login
$login)
1115 if ($this->isInstall()) {
1116 $preferences = new Preferences($zdb, false);
1117 $ct = new \Galette\Entity\
ContributionsTypes($zdb);
1118 $status = new \Galette\Entity\
Status($zdb);
1119 include_once '../includes/fields_defs/members_fields.php';
1120 include_once '../includes/fields_defs/members_fields_cats.php';
1121 $fc = new \Galette\Entity\
FieldsConfig(
1123 \Galette\Entity\Adherent
::TABLE
,
1125 $members_fields_cats,
1130 $login = new \Galette\Core\
Login($zdb, $i18n);
1131 //$fc = new \Galette\Entity\FieldsCategories();
1132 $texts = new \Galette\Entity\
Texts($preferences);
1133 $titles = new \Galette\Repository\
Titles();
1135 $models = new \Galette\Repository\
PdfModels($zdb, $preferences, $login);
1137 $this->_error
= false;
1139 //Install preferences
1140 $res = $preferences->installInit(
1142 $this->getAdminLogin(),
1143 $this->getAdminPass()
1145 $this->proceedReport(_T("Preferences"), $res);
1147 //Install contributions types
1148 $res = $ct->installInit();
1149 $this->proceedReport(_T("Contributions types"), $res);
1152 $res = $status->installInit();
1153 $this->proceedReport(_T("Status"), $res);
1155 //Install fields configuration and categories
1156 $res = $fc->installInit();
1157 $this->proceedReport(_T("Fields config and categories"), $res);
1160 $res = $texts->installInit(false);
1161 $this->proceedReport(_T("Mails texts"), $res);
1164 $res = $titles->installInit($zdb);
1165 $this->proceedReport(_T("Titles"), $res);
1167 //Install PDF models
1168 $res = $models->installInit(false);
1169 $this->proceedReport(_T("PDF Models"), $res);
1171 return !$this->_error
;
1172 } elseif ($this->isUpgrade()) {
1173 $preferences = new Preferences($zdb);
1174 $preferences->store();
1175 $this->proceedReport(_T("Update preferences"), true);
1177 $models = new \Galette\Repository\
PdfModels($zdb, $preferences, new Login($zdb, $i18n));
1178 $res = $models->installInit(true);
1179 $this->proceedReport(_T("Update models"), true);
1181 $texts = new \Galette\Entity\
Texts($preferences);
1182 $res = $texts->installInit(true);
1183 $this->proceedReport(_T("Mails texts"), true);
1190 * Proceed installation report for each Entity/Repository
1192 * @param string $msg Report message title
1193 * @param mixed $res Initialialization result
1197 private function proceedReport($msg, $res)
1204 if ($res instanceof \Exception
) {
1205 $ret['debug'] = $res->getMessage();
1206 $this->_error
= true;
1210 $this->_report
[] = $ret;
1213 * Retrieve galette initialization report
1217 public function getInitializationReport()
1219 return $this->_report
;
1223 * Set step to database installation
1227 public function atEndStep()
1229 $this->_step
= self
::STEP_END
;
1233 * Are we at end step?
1237 public function isEndStep()
1239 return $this->_step
=== self
::STEP_END
;
1243 * Set installed version if we're upgrading
1245 * @param string $version Installed version
1249 public function setInstalledVersion($version)
1251 $this->_installed_version
= $version;
1255 * Current Galette installed version, according to database
1257 * @param Db $zdb Database instance
1261 public function getCurrentVersion($zdb)
1264 $db_ver = $zdb->getDbVersion(true);
1265 if (isset($this->versions_mapper
[$db_ver])) {
1266 return $this->versions_mapper
[$db_ver];
1270 } catch (\LogicException
$e) {
1276 * Check if step is passed
1278 * @param int $step Step
1282 public function isStepPassed($step)
1284 return $this->_step
> $step;