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