diff --git a/install/cli.php b/install/cli.php new file mode 100644 index 0000000000..ede4f82546 --- /dev/null +++ b/install/cli.php @@ -0,0 +1,102 @@ +set($allPostData); + +require_once('core/InstallerConfig.php'); +$installerConfig = new InstallerConfig(); + +if ($installerConfig->get('isInstalled')) { + fwrite(\STDOUT, "Error: EspoCRM is already installed.\n"); + exit; +} + +if (!$installerConfig->get('cliSessionId')) { + session_start(); + $installerConfig->set('cliSessionId', session_id()); + $installerConfig->save(); +} + +$sessonId = session_id($installerConfig->get('cliSessionId')); + +ob_start(); + +try { + require('install/index.php'); +} catch (\Throwable $e) { + fwrite(\STDOUT, "Error: ". $e->getMessage() .".\n"); + exit; +} + +$result = ob_get_contents(); +ob_end_clean(); + +if (preg_match('/"success":false/i', $result)) { + $resultData = json_decode($result, true); + if (empty($resultData)) { + fwrite(\STDOUT, "Error: Unexpected error occurred.\n"); + exit; + } + + fwrite(\STDOUT, "Error: ". (!empty($resultData['errors']) ? print_r($resultData['errors'], true) : $resultData['errorMsg']) .".\n"); + exit; +} + +fwrite(\STDOUT, "Done.\n"); diff --git a/install/core/Installer.php b/install/core/Installer.php index 621f722fd0..dc76f03f40 100644 --- a/install/core/Installer.php +++ b/install/core/Installer.php @@ -248,29 +248,33 @@ class Installer * @param string $language * @return bool */ - public function saveData($database, $language) + public function saveData(array $saveData) { $initData = include('install/core/afterInstall/config.php'); - $siteUrl = $this->getSystemHelper()->getBaseUrl(); $databaseDefaults = $this->app->getContainer()->get('config')->get('database'); $data = [ - 'database' => array_merge($databaseDefaults, $database), - 'language' => $language, - 'siteUrl' => $siteUrl, + 'database' => array_merge($databaseDefaults, $saveData['database']), + 'language' => $saveData['language'], + 'siteUrl' => !empty($saveData['siteUrl']) ? $saveData['siteUrl'] : $this->getSystemHelper()->getBaseUrl(), 'passwordSalt' => $this->getPasswordHash()->generateSalt(), 'cryptKey' => $this->getContainer()->get('crypt')->generateKey(), 'hashSecretKey' => \Espo\Core\Utils\Util::generateSecretKey(), ]; - $owner = $this->getFileManager()->getPermissionUtils()->getDefaultOwner(true); - $group = $this->getFileManager()->getPermissionUtils()->getDefaultGroup(true); - - if (!empty($owner)) { - $data['defaultPermissions']['user'] = $owner; + if (empty($saveData['defaultPermissions']['user'])) { + $saveData['defaultPermissions']['user'] = $this->getFileManager()->getPermissionUtils()->getDefaultOwner(true); } - if (!empty($group)) { - $data['defaultPermissions']['group'] = $group; + + if (empty($saveData['defaultPermissions']['group'])) { + $saveData['defaultPermissions']['group'] = $this->getFileManager()->getPermissionUtils()->getDefaultGroup(true); + } + + if (!empty($saveData['defaultPermissions']['user'])) { + $data['defaultPermissions']['user'] = $saveData['defaultPermissions']['user']; + } + if (!empty($saveData['defaultPermissions']['group'])) { + $data['defaultPermissions']['group'] = $saveData['defaultPermissions']['group']; } $data = array_merge($data, $initData); diff --git a/install/core/PostData.php b/install/core/PostData.php new file mode 100644 index 0000000000..7afe6c0bbb --- /dev/null +++ b/install/core/PostData.php @@ -0,0 +1,45 @@ +init(); + } + + protected function init() + { + if (isset($_POST) && is_array($_POST)) { + $this->data = $_POST; + } + } + + public function set($name, $value = null) + { + if (!is_array($name)) { + $name = [ + $name => $value + ]; + } + + foreach ($name as $key => $value) { + $this->data[$key] = $value; + } + } + + public function get($name, $default = null) + { + if (array_key_exists($name, $this->data)) { + return $this->data[$name]; + } + + return $default; + } + + public function getAll() + { + return $this->data; + } +} diff --git a/install/core/actions/applySett.php b/install/core/actions/applySett.php index 2f50532782..a24f412219 100644 --- a/install/core/actions/applySett.php +++ b/install/core/actions/applySett.php @@ -25,13 +25,13 @@ * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. - ************************************************************************/ + ************************************************************************/ ob_start(); $result = array('success' => true, 'errorMsg' => ''); // save settings -$data = array( +$database = array( 'driver' => 'pdo_mysql', 'dbname' => $_SESSION['install']['db-name'], 'user' => $_SESSION['install']['db-user-name'], @@ -41,10 +41,22 @@ $host = $_SESSION['install']['host-name']; if (strpos($host,':') === false) { $host .= ":"; } -list($data['host'], $data['port']) = explode(':', $host); +list($database['host'], $database['port']) = explode(':', $host); -$lang = (!empty($_SESSION['install']['user-lang']))? $_SESSION['install']['user-lang'] : 'en_US'; -if (!$installer->saveData($data, $lang)) { +$saveData = [ + 'database' => $database, + 'language' => !empty($_SESSION['install']['user-lang']) ? $_SESSION['install']['user-lang'] : 'en_US', + 'siteUrl' => !empty($_SESSION['install']['site-url']) ? $_SESSION['install']['site-url'] : null, +]; + +if (!empty($_SESSION['install']['default-permissions-user']) && !empty($_SESSION['install']['default-permissions-group'])) { + $saveData['defaultPermissions'] = [ + 'user' => $_SESSION['install']['default-permissions-user'], + 'group' => $_SESSION['install']['default-permissions-group'], + ]; +} + +if (!$installer->saveData($saveData)) { $result['success'] = false; $result['errorMsg'] = $langs['messages']['Can not save settings']; } diff --git a/install/core/actions/settingsTest.php b/install/core/actions/settingsTest.php index aa55131437..a9a571c453 100644 --- a/install/core/actions/settingsTest.php +++ b/install/core/actions/settingsTest.php @@ -50,16 +50,18 @@ foreach ($phpRequiredList as $name => $details) { } } -if ($result['success'] && !empty($_REQUEST['dbName']) && !empty($_REQUEST['hostName']) && !empty($_REQUEST['dbUserName'])) { +$allPostData = $postData->getAll(); + +if ($result['success'] && !empty($allPostData['dbName']) && !empty($allPostData['hostName']) && !empty($allPostData['dbUserName'])) { $connect = false; - $dbName = trim($_REQUEST['dbName']); - if (strpos($_REQUEST['hostName'],':') === false) { - $_REQUEST['hostName'] .= ":"; + $dbName = trim($allPostData['dbName']); + if (strpos($allPostData['hostName'],':') === false) { + $allPostData['hostName'] .= ":"; } - list($hostName, $port) = explode(':', trim($_REQUEST['hostName'])); - $dbUserName = trim($_REQUEST['dbUserName']); - $dbUserPass = trim($_REQUEST['dbUserPass']); + list($hostName, $port) = explode(':', trim($allPostData['hostName'])); + $dbUserName = trim($allPostData['dbUserName']); + $dbUserPass = trim($allPostData['dbUserPass']); $databaseParams = [ 'host' => $hostName, @@ -84,7 +86,7 @@ if ($result['success'] && !empty($_REQUEST['dbName']) && !empty($_REQUEST['hostN $databaseRequiredList = $installer->getSystemRequirementList('database', true, ['database' => $databaseParams]); foreach ($databaseRequiredList as $name => $details) { - if (!$details['acceptable']) { + if (!$details['acceptable']) { switch ($details['type']) { case 'version': diff --git a/install/index.php b/install/index.php index 9b6cf968b1..aeac787710 100644 --- a/install/index.php +++ b/install/index.php @@ -27,11 +27,23 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -session_start(); -require_once('../bootstrap.php'); +if (session_status() !== \PHP_SESSION_ACTIVE) { + session_start(); +} + +if (file_exists('../bootstrap.php')) { + require_once('../bootstrap.php'); +} + +if (!isset($postData)) { + require_once('core/PostData.php'); + $postData = new PostData(); +} + +$allPostData = $postData->getAll(); //action -$action = (!empty($_POST['action']))? $_POST['action'] : 'main'; +$action = (!empty($allPostData['action']))? $allPostData['action'] : 'main'; require_once('core/Utils.php'); if (!Utils::checkActionExists($action)) { die('This page does not exist.'); @@ -40,8 +52,8 @@ if (!Utils::checkActionExists($action)) { // temp save all settings $ignoredFields = array('installProcess', 'dbName', 'hostName', 'dbUserName', 'dbUserPass', 'dbDriver'); -if (!empty($_POST)) { - foreach ($_POST as $key => $val) { +if (!empty($allPostData)) { + foreach ($allPostData as $key => $val) { if (!in_array($key, $ignoredFields)) { $_SESSION['install'][$key] = trim($val); } @@ -64,7 +76,7 @@ $systemHelper = new SystemHelper(); $systemConfig = include('application/Espo/Core/defaults/systemConfig.php'); if (isset($systemConfig['requiredPhpVersion']) && version_compare(PHP_VERSION, $systemConfig['requiredPhpVersion'], '<')) { - die(str_replace('{minVersion}', $systemConfig['requiredPhpVersion'], $sanitizedLangs['messages']['phpVersion']) . '.'); + die(str_replace("{minVersion}", $systemConfig['requiredPhpVersion'], $sanitizedLangs['messages']['phpVersion']) . ".\n"); } if (!$systemHelper->initWritable()) { @@ -74,7 +86,7 @@ if (!$systemHelper->initWritable()) { $message = str_replace('{*}', $dir, $message); $message = str_replace('{C}', $systemHelper->getPermissionCommands(array($dir, ''), '775'), $message); $message = str_replace('{CSU}', $systemHelper->getPermissionCommands(array($dir, ''), '775', true), $message); - die($message); + die($message . "\n"); } require_once ('install/vendor/smarty/libs/Smarty.class.php');