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