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.

269 lines
9.6KB

  1. <?php
  2. namespace Friendica\Module;
  3. use Friendica\App;
  4. use Friendica\BaseModule;
  5. use Friendica\Core;
  6. use Friendica\Core\L10n;
  7. use Friendica\Core\Renderer;
  8. use Friendica\Util\Strings;
  9. use Friendica\Util\Temporal;
  10. class Install extends BaseModule
  11. {
  12. /**
  13. * Step one - System check
  14. */
  15. const SYSTEM_CHECK = 1;
  16. /**
  17. * Step two - Database configuration
  18. */
  19. const DATABASE_CONFIG = 2;
  20. /**
  21. * Step three - Adapat site settings
  22. */
  23. const SITE_SETTINGS = 3;
  24. /**
  25. * Step four - All steps finished
  26. */
  27. const FINISHED = 4;
  28. /**
  29. * @var int The current step of the wizard
  30. */
  31. private static $currentWizardStep;
  32. /**
  33. * @var Core\Installer The installer
  34. */
  35. private static $installer;
  36. public static function init()
  37. {
  38. $a = self::getApp();
  39. // route: install/testrwrite
  40. // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
  41. if ($a->getArgumentValue(1, '') == 'testrewrite') {
  42. // Status Code 204 means that it worked without content
  43. Core\System::httpExit(204);
  44. }
  45. // We overwrite current theme css, because during install we may not have a working mod_rewrite
  46. // so we may not have a css at all. Here we set a static css file for the install procedure pages
  47. Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
  48. self::$installer = new Core\Installer();
  49. self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
  50. }
  51. public static function post()
  52. {
  53. $a = self::getApp();
  54. switch (self::$currentWizardStep) {
  55. case self::SYSTEM_CHECK:
  56. case self::DATABASE_CONFIG:
  57. // Nothing to do in these steps
  58. break;
  59. case self::SITE_SETTINGS:
  60. $dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
  61. $dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser', '')));
  62. $dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass', '')));
  63. $dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '')));
  64. // If we cannot connect to the database, return to the previous step
  65. if (!self::$installer->checkDB($dbhost, $dbuser, $dbpass, $dbdata)) {
  66. self::$currentWizardStep = self::DATABASE_CONFIG;
  67. }
  68. break;
  69. case self::FINISHED:
  70. $urlpath = $a->getURLPath();
  71. $dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
  72. $dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser', '')));
  73. $dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass', '')));
  74. $dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '')));
  75. $timezone = Strings::escapeTags(trim(defaults($_POST, 'timezone', Core\Installer::DEFAULT_TZ)));
  76. $language = Strings::escapeTags(trim(defaults($_POST, 'language', Core\Installer::DEFAULT_LANG)));
  77. $adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '')));
  78. // If we cannot connect to the database, return to the Database config wizard
  79. if (!self::$installer->checkDB($dbhost, $dbuser, $dbpass, $dbdata)) {
  80. self::$currentWizardStep = self::DATABASE_CONFIG;
  81. return;
  82. }
  83. $phpath = self::$installer->getPHPPath();
  84. if (!self::$installer->createConfig($phpath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $a->getBasePath())) {
  85. return;
  86. }
  87. self::$installer->installDatabase($a->getBasePath());
  88. break;
  89. }
  90. }
  91. public static function content()
  92. {
  93. $a = self::getApp();
  94. $output = '';
  95. $install_title = L10n::t('Friendica Communications Server - Setup');
  96. switch (self::$currentWizardStep) {
  97. case self::SYSTEM_CHECK:
  98. $phppath = defaults($_POST, 'phpath', null);
  99. $status = self::$installer->checkEnvironment($a->getBaseURL(), $phppath);
  100. $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
  101. $output .= Renderer::replaceMacros($tpl, [
  102. '$title' => $install_title,
  103. '$pass' => L10n::t('System check'),
  104. '$checks' => self::$installer->getChecks(),
  105. '$passed' => $status,
  106. '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
  107. '$next' => L10n::t('Next'),
  108. '$reload' => L10n::t('Check again'),
  109. '$phpath' => $phppath,
  110. '$baseurl' => $a->getBaseURL()
  111. ]);
  112. break;
  113. case self::DATABASE_CONFIG:
  114. $dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost' , Core\Installer::DEFAULT_HOST)));
  115. $dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser' , '' )));
  116. $dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass' , '' )));
  117. $dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata' , '' )));
  118. $phpath = Strings::escapeTags(trim(defaults($_POST, 'phpath' , '' )));
  119. $adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '' )));
  120. $tpl = Renderer::getMarkupTemplate('install_db.tpl');
  121. $output .= Renderer::replaceMacros($tpl, [
  122. '$title' => $install_title,
  123. '$pass' => L10n::t('Database connection'),
  124. '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
  125. '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
  126. '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
  127. 'checks' => self::$installer->getChecks(),
  128. '$dbhost' => ['dbhost',
  129. L10n::t('Database Server Name'),
  130. $dbhost,
  131. '',
  132. 'required'],
  133. '$dbuser' => ['dbuser',
  134. L10n::t('Database Login Name'),
  135. $dbuser,
  136. '',
  137. 'required',
  138. 'autofocus'],
  139. '$dbpass' => ['dbpass',
  140. L10n::t('Database Login Password'),
  141. $dbpass,
  142. L10n::t("For security reasons the password must not be empty"),
  143. 'required'],
  144. '$dbdata' => ['dbdata',
  145. L10n::t('Database Name'),
  146. $dbdata,
  147. '',
  148. 'required'],
  149. '$adminmail' => ['adminmail',
  150. L10n::t('Site administrator email address'),
  151. $adminmail,
  152. L10n::t('Your account email address must match this in order to use the web admin panel.'),
  153. 'required',
  154. 'autofocus',
  155. 'email'],
  156. '$lbl_10' => L10n::t('Please select a default timezone for your website'),
  157. '$baseurl' => $a->getBaseURL(),
  158. '$phpath' => $phpath,
  159. '$submit' => L10n::t('Submit')
  160. ]);
  161. break;
  162. case self::SITE_SETTINGS:
  163. $dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
  164. $dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser', '' )));
  165. $dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass', '' )));
  166. $dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '' )));
  167. $phpath = Strings::escapeTags(trim(defaults($_POST, 'phpath', '' )));
  168. $adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '')));
  169. $timezone = defaults($_POST, 'timezone', Core\Installer::DEFAULT_TZ);
  170. /* Installed langs */
  171. $lang_choices = L10n::getAvailableLanguages();
  172. $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
  173. $output .= Renderer::replaceMacros($tpl, [
  174. '$title' => $install_title,
  175. '$checks' => self::$installer->getChecks(),
  176. '$pass' => L10n::t('Site settings'),
  177. '$dbhost' => $dbhost,
  178. '$dbuser' => $dbuser,
  179. '$dbpass' => $dbpass,
  180. '$dbdata' => $dbdata,
  181. '$phpath' => $phpath,
  182. '$adminmail' => ['adminmail', L10n::t('Site administrator email address'), $adminmail, L10n::t('Your account email address must match this in order to use the web admin panel.'), 'required', 'autofocus', 'email'],
  183. '$timezone' => Temporal::getTimezoneField('timezone', L10n::t('Please select a default timezone for your website'), $timezone, ''),
  184. '$language' => ['language',
  185. L10n::t('System Language:'),
  186. Core\Installer::DEFAULT_LANG,
  187. L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
  188. $lang_choices],
  189. '$baseurl' => $a->getBaseURL(),
  190. '$submit' => L10n::t('Submit')
  191. ]);
  192. break;
  193. case self::FINISHED:
  194. $db_return_text = "";
  195. if (count(self::$installer->getChecks()) == 0) {
  196. $txt = '<p style="font-size: 130%;">';
  197. $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
  198. $db_return_text .= $txt;
  199. }
  200. $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
  201. $output .= Renderer::replaceMacros($tpl, [
  202. '$title' => $install_title,
  203. '$checks' => self::$installer->getChecks(),
  204. '$pass' => L10n::t('Installation finished'),
  205. '$text' => $db_return_text . self::whatNext($a),
  206. ]);
  207. break;
  208. }
  209. return $output;
  210. }
  211. /**
  212. * Creates the text for the next steps
  213. *
  214. * @param App $a The global App
  215. *
  216. * @return string The text for the next steps
  217. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  218. */
  219. private static function whatNext($a)
  220. {
  221. $baseurl = $a->getBaseUrl();
  222. return
  223. L10n::t('<h1>What next</h1>')
  224. . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
  225. . L10n::t('Please see the file "INSTALL.txt".')
  226. . "</p><p>"
  227. . 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)
  228. . "</p>";
  229. }
  230. }