Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
13 KiB

2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace Friendica\Module;
  3. use Friendica\App;
  4. use Friendica\BaseModule;
  5. use Friendica\Core;
  6. use Friendica\Core\Config\Cache\ConfigCache;
  7. use Friendica\Core\L10n;
  8. use Friendica\Core\Renderer;
  9. use Friendica\Network\HTTPException;
  10. use Friendica\Util\BasePath;
  11. use Friendica\Util\Strings;
  12. use Friendica\Util\Temporal;
  13. class Install extends BaseModule
  14. {
  15. /**
  16. * Step one - System check
  17. */
  18. const SYSTEM_CHECK = 1;
  19. /**
  20. * Step two - Base information
  21. */
  22. const BASE_CONFIG = 2;
  23. /**
  24. * Step three - Database configuration
  25. */
  26. const DATABASE_CONFIG = 3;
  27. /**
  28. * Step four - Adapt site settings
  29. */
  30. const SITE_SETTINGS = 4;
  31. /**
  32. * Step five - All steps finished
  33. */
  34. const FINISHED = 5;
  35. /**
  36. * @var int The current step of the wizard
  37. */
  38. private static $currentWizardStep;
  39. /**
  40. * @var Core\Installer The installer
  41. */
  42. private static $installer;
  43. public static function init()
  44. {
  45. $a = self::getApp();
  46. if (!$a->getMode()->isInstall()) {
  47. throw new HTTPException\ForbiddenException();
  48. }
  49. // route: install/testrwrite
  50. // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
  51. // @TODO: Replace with parameter from router
  52. if ($a->getArgumentValue(1, '') == 'testrewrite') {
  53. // Status Code 204 means that it worked without content
  54. throw new HTTPException\NoContentException();
  55. }
  56. self::$installer = new Core\Installer();
  57. // get basic installation information and save them to the config cache
  58. $configCache = $a->getConfigCache();
  59. $basePath = new BasePath($a->getBasePath());
  60. self::$installer->setUpCache($configCache, $basePath->getPath());
  61. // We overwrite current theme css, because during install we may not have a working mod_rewrite
  62. // so we may not have a css at all. Here we set a static css file for the install procedure pages
  63. Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
  64. self::$currentWizardStep = ($_POST['pass'] ?? '') ?: self::SYSTEM_CHECK;
  65. }
  66. public static function post()
  67. {
  68. $a = self::getApp();
  69. $configCache = $a->getConfigCache();
  70. switch (self::$currentWizardStep) {
  71. case self::SYSTEM_CHECK:
  72. case self::BASE_CONFIG:
  73. self::checkSetting($configCache, $_POST, 'config', 'php_path');
  74. break;
  75. case self::DATABASE_CONFIG:
  76. self::checkSetting($configCache, $_POST, 'config', 'php_path');
  77. self::checkSetting($configCache, $_POST, 'config', 'hostname');
  78. self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
  79. self::checkSetting($configCache, $_POST, 'system', 'basepath');
  80. self::checkSetting($configCache, $_POST, 'system', 'urlpath');
  81. break;
  82. case self::SITE_SETTINGS:
  83. self::checkSetting($configCache, $_POST, 'config', 'php_path');
  84. self::checkSetting($configCache, $_POST, 'config', 'hostname');
  85. self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
  86. self::checkSetting($configCache, $_POST, 'system', 'basepath');
  87. self::checkSetting($configCache, $_POST, 'system', 'urlpath');
  88. self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
  89. self::checkSetting($configCache, $_POST, 'database', 'username', '');
  90. self::checkSetting($configCache, $_POST, 'database', 'password', '');
  91. self::checkSetting($configCache, $_POST, 'database', 'database', '');
  92. // If we cannot connect to the database, return to the previous step
  93. if (!self::$installer->checkDB($a->getDBA())) {
  94. self::$currentWizardStep = self::DATABASE_CONFIG;
  95. }
  96. break;
  97. case self::FINISHED:
  98. self::checkSetting($configCache, $_POST, 'config', 'php_path');
  99. self::checkSetting($configCache, $_POST, 'config', 'hostname');
  100. self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
  101. self::checkSetting($configCache, $_POST, 'system', 'basepath');
  102. self::checkSetting($configCache, $_POST, 'system', 'urlpath');
  103. self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
  104. self::checkSetting($configCache, $_POST, 'database', 'username', '');
  105. self::checkSetting($configCache, $_POST, 'database', 'password', '');
  106. self::checkSetting($configCache, $_POST, 'database', 'database', '');
  107. self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
  108. self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
  109. self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
  110. // If we cannot connect to the database, return to the Database config wizard
  111. if (!self::$installer->checkDB($a->getDBA())) {
  112. self::$currentWizardStep = self::DATABASE_CONFIG;
  113. return;
  114. }
  115. if (!self::$installer->createConfig($configCache)) {
  116. return;
  117. }
  118. self::$installer->installDatabase($configCache->get('system', 'basepath'));
  119. break;
  120. }
  121. }
  122. public static function content()
  123. {
  124. $a = self::getApp();
  125. $configCache = $a->getConfigCache();
  126. $output = '';
  127. $install_title = L10n::t('Friendica Communications Server - Setup');
  128. switch (self::$currentWizardStep) {
  129. case self::SYSTEM_CHECK:
  130. $php_path = $configCache->get('config', 'php_path');
  131. $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
  132. $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
  133. $output .= Renderer::replaceMacros($tpl, [
  134. '$title' => $install_title,
  135. '$pass' => L10n::t('System check'),
  136. '$checks' => self::$installer->getChecks(),
  137. '$passed' => $status,
  138. '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
  139. '$next' => L10n::t('Next'),
  140. '$reload' => L10n::t('Check again'),
  141. '$php_path' => $php_path,
  142. ]);
  143. break;
  144. case self::BASE_CONFIG:
  145. $ssl_choices = [
  146. App\BaseURL::SSL_POLICY_NONE => L10n::t("No SSL policy, links will track page SSL state"),
  147. App\BaseURL::SSL_POLICY_FULL => L10n::t("Force all links to use SSL"),
  148. App\BaseURL::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
  149. ];
  150. $tpl = Renderer::getMarkupTemplate('install_base.tpl');
  151. $output .= Renderer::replaceMacros($tpl, [
  152. '$title' => $install_title,
  153. '$pass' => L10n::t('Base settings'),
  154. '$ssl_policy' => ['system-ssl_policy',
  155. L10n::t("SSL link policy"),
  156. $configCache->get('system', 'ssl_policy'),
  157. L10n::t("Determines whether generated links should be forced to use SSL"),
  158. $ssl_choices],
  159. '$hostname' => ['config-hostname',
  160. L10n::t('Host name'),
  161. $configCache->get('config', 'hostname'),
  162. L10n::t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
  163. 'required'],
  164. '$basepath' => ['system-basepath',
  165. L10n::t("Base path to installation"),
  166. $configCache->get('system', 'basepath'),
  167. L10n::t("If the system cannot detect the correct path to your installation, enter the correct path here. This setting should only be set if you are using a restricted system and symbolic links to your webroot."),
  168. 'required'],
  169. '$urlpath' => ['system-urlpath',
  170. L10n::t('Sub path of the URL'),
  171. $configCache->get('system', 'urlpath'),
  172. L10n::t('Overwrite this field in case the sub path determination isn\'t right, otherwise leave it as is. Leaving this field blank means the installation is at the base URL without sub path.'),
  173. ''],
  174. '$php_path' => $configCache->get('config', 'php_path'),
  175. '$submit' => L10n::t('Submit'),
  176. ]);
  177. break;
  178. case self::DATABASE_CONFIG:
  179. $tpl = Renderer::getMarkupTemplate('install_db.tpl');
  180. $output .= Renderer::replaceMacros($tpl, [
  181. '$title' => $install_title,
  182. '$pass' => L10n::t('Database connection'),
  183. '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
  184. '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
  185. '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
  186. 'checks' => self::$installer->getChecks(),
  187. '$hostname' => $configCache->get('config', 'hostname'),
  188. '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
  189. '$basepath' => $configCache->get('system', 'basepath'),
  190. '$urlpath' => $configCache->get('system', 'urlpath'),
  191. '$dbhost' => ['database-hostname',
  192. L10n::t('Database Server Name'),
  193. $configCache->get('database', 'hostname'),
  194. '',
  195. 'required'],
  196. '$dbuser' => ['database-username',
  197. L10n::t('Database Login Name'),
  198. $configCache->get('database', 'username'),
  199. '',
  200. 'required',
  201. 'autofocus'],
  202. '$dbpass' => ['database-password',
  203. L10n::t('Database Login Password'),
  204. $configCache->get('database', 'password'),
  205. L10n::t("For security reasons the password must not be empty"),
  206. 'required'],
  207. '$dbdata' => ['database-database',
  208. L10n::t('Database Name'),
  209. $configCache->get('database', 'database'),
  210. '',
  211. 'required'],
  212. '$lbl_10' => L10n::t('Please select a default timezone for your website'),
  213. '$php_path' => $configCache->get('config', 'php_path'),
  214. '$submit' => L10n::t('Submit')
  215. ]);
  216. break;
  217. case self::SITE_SETTINGS:
  218. /* Installed langs */
  219. $lang_choices = L10n::getAvailableLanguages();
  220. $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
  221. $output .= Renderer::replaceMacros($tpl, [
  222. '$title' => $install_title,
  223. '$checks' => self::$installer->getChecks(),
  224. '$pass' => L10n::t('Site settings'),
  225. '$hostname' => $configCache->get('config', 'hostname'),
  226. '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
  227. '$basepath' => $configCache->get('system', 'basepath'),
  228. '$urlpath' => $configCache->get('system', 'urlpath'),
  229. '$dbhost' => $configCache->get('database', 'hostname'),
  230. '$dbuser' => $configCache->get('database', 'username'),
  231. '$dbpass' => $configCache->get('database', 'password'),
  232. '$dbdata' => $configCache->get('database', 'database'),
  233. '$adminmail' => ['config-admin_email',
  234. L10n::t('Site administrator email address'),
  235. $configCache->get('config', 'admin_email'),
  236. L10n::t('Your account email address must match this in order to use the web admin panel.'),
  237. 'required', 'autofocus', 'email'],
  238. '$timezone' => Temporal::getTimezoneField('system-default_timezone',
  239. L10n::t('Please select a default timezone for your website'),
  240. $configCache->get('system', 'default_timezone'),
  241. ''),
  242. '$language' => ['system-language',
  243. L10n::t('System Language:'),
  244. $configCache->get('system', 'language'),
  245. L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
  246. $lang_choices],
  247. '$php_path' => $configCache->get('config', 'php_path'),
  248. '$submit' => L10n::t('Submit')
  249. ]);
  250. break;
  251. case self::FINISHED:
  252. $db_return_text = "";
  253. if (count(self::$installer->getChecks()) == 0) {
  254. $txt = '<p style="font-size: 130%;">';
  255. $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
  256. $db_return_text .= $txt;
  257. }
  258. $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
  259. $output .= Renderer::replaceMacros($tpl, [
  260. '$title' => $install_title,
  261. '$checks' => self::$installer->getChecks(),
  262. '$pass' => L10n::t('Installation finished'),
  263. '$text' => $db_return_text . self::whatNext($a),
  264. ]);
  265. break;
  266. }
  267. return $output;
  268. }
  269. /**
  270. * Creates the text for the next steps
  271. *
  272. * @param App $a The global App
  273. *
  274. * @return string The text for the next steps
  275. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  276. */
  277. private static function whatNext($a)
  278. {
  279. $baseurl = $a->getBaseUrl();
  280. return
  281. L10n::t('<h1>What next</h1>')
  282. . "<p>" . L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
  283. . L10n::t('Please see the file "INSTALL.txt".')
  284. . "</p><p>"
  285. . L10n::t('Go to your new Friendica node <a href="%s/register">registration page</a> and register as new user. Remember to use the same email you have entered as administrator email. This will allow you to enter the site admin panel.', $baseurl)
  286. . "</p>";
  287. }
  288. /**
  289. * Checks the $_POST settings and updates the config Cache for it
  290. *
  291. * @param ConfigCache $configCache The current config cache
  292. * @param array $post The $_POST data
  293. * @param string $cat The category of the setting
  294. * @param string $key The key of the setting
  295. * @param null|string $default The default value
  296. */
  297. private static function checkSetting(ConfigCache $configCache, array $post, $cat, $key, $default = null)
  298. {
  299. $configCache->set($cat, $key,
  300. Strings::escapeTags(
  301. trim(($post[sprintf('%s-%s', $cat, $key)] ?? '') ?:
  302. ($default ?? $configCache->get($cat, $key))
  303. )
  304. )
  305. );
  306. }
  307. }