2018-10-29 09:10:45 -04:00
< ? php
namespace Friendica\Module ;
use Friendica\App ;
use Friendica\BaseModule ;
use Friendica\Core ;
2019-03-14 05:00:05 -04:00
use Friendica\Core\Config\Cache\IConfigCache ;
2018-10-29 09:10:45 -04:00
use Friendica\Core\L10n ;
2018-10-31 10:35:50 -04:00
use Friendica\Core\Renderer ;
2018-11-08 10:14:37 -05:00
use Friendica\Util\Strings ;
2018-10-29 09:10:45 -04:00
use Friendica\Util\Temporal ;
class Install extends BaseModule
{
/**
* Step one - System check
*/
const SYSTEM_CHECK = 1 ;
/**
* Step two - Database configuration
*/
const DATABASE_CONFIG = 2 ;
/**
* Step three - Adapat site settings
*/
const SITE_SETTINGS = 3 ;
/**
* Step four - All steps finished
*/
const FINISHED = 4 ;
/**
* @ var int The current step of the wizard
*/
private static $currentWizardStep ;
2018-10-29 13:44:39 -04:00
/**
* @ var Core\Installer The installer
*/
private static $installer ;
2018-10-29 09:10:45 -04:00
public static function init ()
{
$a = self :: getApp ();
2019-02-23 16:58:35 -05:00
if ( ! $a -> getMode () -> isInstall ()) {
Core\System :: httpExit ( 403 );
}
2018-10-29 09:10:45 -04:00
// route: install/testrwrite
// $baseurl/install/testrwrite to test if rewrite in .htaccess is working
if ( $a -> getArgumentValue ( 1 , '' ) == 'testrewrite' ) {
// Status Code 204 means that it worked without content
Core\System :: httpExit ( 204 );
}
2018-10-29 09:39:09 -04:00
// We overwrite current theme css, because during install we may not have a working mod_rewrite
// so we may not have a css at all. Here we set a static css file for the install procedure pages
2018-10-31 13:25:38 -04:00
Renderer :: $theme [ 'stylesheet' ] = $a -> getBaseURL () . '/view/install/style.css' ;
2018-10-29 09:10:45 -04:00
2018-10-29 13:44:39 -04:00
self :: $installer = new Core\Installer ();
2018-10-29 09:10:45 -04:00
self :: $currentWizardStep = defaults ( $_POST , 'pass' , self :: SYSTEM_CHECK );
}
public static function post ()
{
$a = self :: getApp ();
2019-03-14 05:00:05 -04:00
$configCache = $a -> getConfigCache ();
2018-10-29 09:10:45 -04:00
switch ( self :: $currentWizardStep ) {
case self :: SYSTEM_CHECK :
case self :: DATABASE_CONFIG :
2019-03-14 05:00:05 -04:00
self :: checkSetting ( $configCache , $_POST , 'config' , 'php_path' );
2018-10-29 13:44:39 -04:00
break ;
2018-10-29 09:10:45 -04:00
case self :: SITE_SETTINGS :
2019-03-14 05:00:05 -04:00
self :: checkSetting ( $configCache , $_POST , 'config' , 'php_path' );
self :: checkSetting ( $configCache , $_POST , 'database' , 'hostname' , Core\Installer :: DEFAULT_HOST );
self :: checkSetting ( $configCache , $_POST , 'database' , 'username' , '' );
self :: checkSetting ( $configCache , $_POST , 'database' , 'password' , '' );
self :: checkSetting ( $configCache , $_POST , 'database' , 'database' , '' );
2018-10-29 09:10:45 -04:00
2018-10-29 13:44:39 -04:00
// If we cannot connect to the database, return to the previous step
2019-03-14 05:00:05 -04:00
if ( ! self :: $installer -> checkDB ( $a -> getBasePath (), $configCache , $a -> getProfiler ())) {
2018-10-29 13:44:39 -04:00
self :: $currentWizardStep = self :: DATABASE_CONFIG ;
2018-10-29 09:10:45 -04:00
}
2018-10-29 13:44:39 -04:00
break ;
2018-10-29 09:10:45 -04:00
case self :: FINISHED :
2019-03-14 05:00:05 -04:00
self :: checkSetting ( $configCache , $_POST , 'config' , 'php_path' );
self :: checkSetting ( $configCache , $_POST , 'database' , 'hostname' , Core\Installer :: DEFAULT_HOST );
self :: checkSetting ( $configCache , $_POST , 'database' , 'username' , '' );
self :: checkSetting ( $configCache , $_POST , 'database' , 'password' , '' );
self :: checkSetting ( $configCache , $_POST , 'database' , 'database' , '' );
self :: checkSetting ( $configCache , $_POST , 'system' , 'default_timezone' , Core\Installer :: DEFAULT_TZ );
self :: checkSetting ( $configCache , $_POST , 'system' , 'language' , Core\Installer :: DEFAULT_LANG );
self :: checkSetting ( $configCache , $_POST , 'config' , 'admin_email' , '' );
2018-10-29 09:10:45 -04:00
2018-10-29 13:44:39 -04:00
// If we cannot connect to the database, return to the Database config wizard
2019-03-14 05:00:05 -04:00
if ( ! self :: $installer -> checkDB ( $a -> getBasePath (), $configCache , $a -> getProfiler ())) {
2018-10-29 13:44:39 -04:00
self :: $currentWizardStep = self :: DATABASE_CONFIG ;
2018-10-30 06:30:19 -04:00
return ;
2018-10-29 09:10:45 -04:00
}
2019-03-14 05:00:05 -04:00
if ( ! self :: $installer -> createConfig ( $a , $configCache , $a -> getBasePath ())) {
2018-10-29 13:44:39 -04:00
return ;
2018-10-29 09:10:45 -04:00
}
2019-02-03 16:46:50 -05:00
self :: $installer -> installDatabase ( $a -> getBasePath ());
2018-10-29 09:10:45 -04:00
2018-10-29 13:44:39 -04:00
break ;
2018-10-29 09:10:45 -04:00
}
}
public static function content ()
{
$a = self :: getApp ();
2019-03-14 05:00:05 -04:00
$configCache = $a -> getConfigCache ();
2018-10-29 09:10:45 -04:00
$output = '' ;
2018-10-31 16:03:32 -04:00
$install_title = L10n :: t ( 'Friendica Communications Server - Setup' );
2018-10-29 09:10:45 -04:00
switch ( self :: $currentWizardStep ) {
case self :: SYSTEM_CHECK :
2019-03-14 05:00:05 -04:00
$php_path = $configCache -> get ( 'config' , 'php_path' );
2018-10-29 09:10:45 -04:00
2019-03-14 05:00:05 -04:00
$status = self :: $installer -> checkEnvironment ( $a -> getBaseURL (), $php_path );
2018-10-29 09:10:45 -04:00
2018-10-31 10:44:06 -04:00
$tpl = Renderer :: getMarkupTemplate ( 'install_checks.tpl' );
2018-10-31 10:35:50 -04:00
$output .= Renderer :: replaceMacros ( $tpl , [
2019-03-14 05:00:05 -04:00
'$title' => $install_title ,
'$pass' => L10n :: t ( 'System check' ),
'$checks' => self :: $installer -> getChecks (),
'$passed' => $status ,
'$see_install' => L10n :: t ( 'Please see the file "INSTALL.txt".' ),
'$next' => L10n :: t ( 'Next' ),
'$reload' => L10n :: t ( 'Check again' ),
'$php_path' => $php_path ,
'$baseurl' => $a -> getBaseURL ()
2018-10-29 09:10:45 -04:00
]);
break ;
case self :: DATABASE_CONFIG :
2018-10-31 10:44:06 -04:00
$tpl = Renderer :: getMarkupTemplate ( 'install_db.tpl' );
2018-10-31 10:35:50 -04:00
$output .= Renderer :: replaceMacros ( $tpl , [
2019-03-14 05:00:05 -04:00
'$title' => $install_title ,
'$pass' => L10n :: t ( 'Database connection' ),
'$info_01' => L10n :: t ( 'In order to install Friendica we need to know how to connect to your database.' ),
'$info_02' => L10n :: t ( 'Please contact your hosting provider or site administrator if you have questions about these settings.' ),
'$info_03' => L10n :: t ( 'The database you specify below should already exist. If it does not, please create it before continuing.' ),
'checks' => self :: $installer -> getChecks (),
'$dbhost' => [ 'database-hostname' ,
2018-10-29 09:10:45 -04:00
L10n :: t ( 'Database Server Name' ),
2019-03-14 05:00:05 -04:00
$configCache -> get ( 'database' , 'hostname' ),
2018-10-29 09:10:45 -04:00
'' ,
'required' ],
2019-03-14 05:00:05 -04:00
'$dbuser' => [ 'database-username' ,
2018-10-29 09:10:45 -04:00
L10n :: t ( 'Database Login Name' ),
2019-03-14 05:00:05 -04:00
$configCache -> get ( 'database' , 'username' ),
2018-10-29 09:10:45 -04:00
'' ,
'required' ,
'autofocus' ],
2019-03-14 05:00:05 -04:00
'$dbpass' => [ 'database-password' ,
2018-10-29 09:10:45 -04:00
L10n :: t ( 'Database Login Password' ),
2019-03-14 05:00:05 -04:00
$configCache -> get ( 'database' , 'password' ),
2018-10-29 09:10:45 -04:00
L10n :: t ( " For security reasons the password must not be empty " ),
'required' ],
2019-03-14 05:00:05 -04:00
'$dbdata' => [ 'database-database' ,
2018-10-29 09:10:45 -04:00
L10n :: t ( 'Database Name' ),
2019-03-14 05:00:05 -04:00
$configCache -> get ( 'database' , 'database' ),
2018-10-29 09:10:45 -04:00
'' ,
'required' ],
2019-03-14 05:00:05 -04:00
'$lbl_10' => L10n :: t ( 'Please select a default timezone for your website' ),
'$baseurl' => $a -> getBaseURL (),
'$php_path' => $configCache -> get ( 'config' , 'php_path' ),
'$submit' => L10n :: t ( 'Submit' )
2018-10-29 09:10:45 -04:00
]);
break ;
2018-10-29 13:44:39 -04:00
2018-10-29 09:10:45 -04:00
case self :: SITE_SETTINGS :
/* Installed langs */
$lang_choices = L10n :: getAvailableLanguages ();
2018-10-31 10:44:06 -04:00
$tpl = Renderer :: getMarkupTemplate ( 'install_settings.tpl' );
2018-10-31 10:35:50 -04:00
$output .= Renderer :: replaceMacros ( $tpl , [
2018-10-29 09:10:45 -04:00
'$title' => $install_title ,
2018-10-29 13:44:39 -04:00
'$checks' => self :: $installer -> getChecks (),
2018-10-29 09:10:45 -04:00
'$pass' => L10n :: t ( 'Site settings' ),
2019-03-14 05:00:05 -04:00
'$dbhost' => $configCache -> get ( 'database' , 'hostname' ),
'$dbuser' => $configCache -> get ( 'database' , 'username' ),
'$dbpass' => $configCache -> get ( 'database' , 'password' ),
'$dbdata' => $configCache -> get ( 'database' , 'database' ),
'$phpath' => $configCache -> get ( 'config' , 'php_path' ),
'$adminmail' => [ 'config-admin_email' ,
L10n :: t ( 'Site administrator email address' ),
$configCache -> get ( 'config' , 'admin_email' ),
L10n :: t ( 'Your account email address must match this in order to use the web admin panel.' ),
'required' , 'autofocus' , 'email' ],
'$timezone' => Temporal :: getTimezoneField ( 'system-default_timezone' ,
L10n :: t ( 'Please select a default timezone for your website' ),
$configCache -> get ( 'system' , 'default_timezone' ),
'' ),
'$language' => [ 'system-language' ,
2018-10-29 13:44:39 -04:00
L10n :: t ( 'System Language:' ),
2019-03-14 05:00:05 -04:00
$configCache -> get ( 'system' , 'language' ),
2018-10-29 09:10:45 -04:00
L10n :: t ( 'Set the default language for your Friendica installation interface and to send emails.' ),
$lang_choices ],
'$baseurl' => $a -> getBaseURL (),
'$submit' => L10n :: t ( 'Submit' )
]);
break ;
case self :: FINISHED :
$db_return_text = " " ;
2018-10-29 13:44:39 -04:00
if ( count ( self :: $installer -> getChecks ()) == 0 ) {
2018-10-29 09:10:45 -04:00
$txt = '<p style="font-size: 130%;">' ;
$txt .= L10n :: t ( 'Your Friendica site database has been installed.' ) . EOL ;
$db_return_text .= $txt ;
}
2018-10-31 10:44:06 -04:00
$tpl = Renderer :: getMarkupTemplate ( 'install_finished.tpl' );
2018-10-31 10:35:50 -04:00
$output .= Renderer :: replaceMacros ( $tpl , [
2018-10-29 13:44:39 -04:00
'$title' => $install_title ,
'$checks' => self :: $installer -> getChecks (),
'$pass' => L10n :: t ( 'Installation finished' ),
'$text' => $db_return_text . self :: whatNext ( $a ),
2018-10-29 09:10:45 -04:00
]);
break ;
}
return $output ;
}
/**
* Creates the text for the next steps
*
* @ param App $a The global App
*
* @ return string The text for the next steps
2019-01-06 16:06:53 -05:00
* @ throws \Friendica\Network\HTTPException\InternalServerErrorException
2018-10-29 09:10:45 -04:00
*/
2018-10-29 09:39:09 -04:00
private static function whatNext ( $a )
{
2018-10-29 09:10:45 -04:00
$baseurl = $a -> getBaseUrl ();
return
L10n :: t ( '<h1>What next</h1>' )
2018-10-29 09:39:09 -04:00
. " <p> " . L10n :: t ( 'IMPORTANT: You will need to [manually] setup a scheduled task for the worker.' )
. L10n :: t ( 'Please see the file "INSTALL.txt".' )
. " </p><p> "
. 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 )
. " </p> " ;
2018-10-29 09:10:45 -04:00
}
2019-03-14 05:00:05 -04:00
/**
* Checks the $_POST settings and updates the config Cache for it
*
* @ param IConfigCache $configCache The current config cache
* @ param array $post The $_POST data
* @ param string $cat The category of the setting
* @ param string $key The key of the setting
* @ param null | string $default The default value
*/
private static function checkSetting ( IConfigCache $configCache , array $post , $cat , $key , $default = null )
{
$configCache -> set ( $cat , $key ,
Strings :: escapeTags (
trim ( defaults ( $post , sprintf ( '%s-%s' , $cat , $key ),
( ! isset ( $default ) ? $configCache -> get ( $cat , $key ) : $default ))
)
)
);
}
2018-10-29 09:10:45 -04:00
}