2020-06-05 14:26:57 -05:00
< ? php
/**
* FinalizeController . php
*
* - Description -
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*
* @ package LibreNMS
* @ link http :// librenms . org
* @ copyright 2020 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
namespace App\Http\Controllers\Install ;
2020-06-08 02:21:03 -05:00
use Exception ;
use LibreNMS\Util\EnvHelper ;
2020-06-05 14:26:57 -05:00
class FinalizeController extends \App\Http\Controllers\Controller
{
public function __invoke ()
{
2020-06-08 02:21:03 -05:00
$env = '' ;
2020-06-09 23:24:43 -05:00
$config_file = base_path ( 'config.php' );
2020-06-08 02:21:03 -05:00
$config = $this -> getConfigFileContents ();
$messages = [];
$success = true ;
2020-06-09 23:24:43 -05:00
$config_message = file_exists ( $config_file ) ? trans ( 'install.finish.config_exists' ) : trans ( 'install.finish.config_written' );
2020-06-08 02:21:03 -05:00
try {
2020-06-09 23:24:43 -05:00
$this -> writeConfigFile ( $config , $config_file );
2020-06-08 02:21:03 -05:00
} catch ( Exception $e ) {
$messages [] = $e -> getMessage ();
2020-06-09 23:24:43 -05:00
$config_message = trans ( 'install.finish.config_not_written' );
2020-06-08 02:21:03 -05:00
$success = true ;
}
// write env last only if everything else succeeded
if ( $success ) {
try {
$env = $this -> writeEnvFile ();
} catch ( Exception $e ) {
$messages [] = $e -> getMessage ();
$success = false ;
}
}
2020-06-09 23:24:43 -05:00
if ( $success ) {
// TODO clear session
// session()->forget('install');
// session()->forget('db');
}
2020-06-08 02:21:03 -05:00
return view ( 'install.finish' , [
'env' => $env ,
'config' => $config ,
'messages' => $messages ,
'success' => $success ,
2020-06-09 23:24:43 -05:00
'config_message' => $config_message ,
2020-06-08 02:21:03 -05:00
]);
}
private function writeEnvFile ()
{
return EnvHelper :: setEnv ([
'NODE_ID' => uniqid (),
'DB_HOST' => session ( 'db.host' ),
'DB_PORT' => session ( 'db.port' ),
'DB_USERNAME' => session ( 'db.username' ),
'DB_PASSWORD' => session ( 'db.password' ),
'DB_DATABASE' => session ( 'db.database' ),
'DB_SOCKET' => session ( 'db.socket' ),
2020-06-09 23:24:43 -05:00
], [], base_path ( '.env' )); // TODO unset INSTALL
2020-06-05 14:26:57 -05:00
}
2020-06-06 17:03:32 -05:00
2020-06-09 23:24:43 -05:00
private function writeConfigFile ( $config_contents , $config_file )
2020-06-06 17:03:32 -05:00
{
2020-06-09 23:24:43 -05:00
if ( ! file_exists ( $config_file )) {
$conf = fopen ( $config_file , 'w' );
2020-06-08 02:21:03 -05:00
if ( $conf !== false ) {
2020-06-06 17:03:32 -05:00
if ( fwrite ( $conf , " <?php \n " ) === false ) {
2020-06-08 02:21:03 -05:00
throw new Exception ( " We couldn't create the config.php file, please create this manually before continuing by copying the below into a config.php in the root directory of your install (typically /opt/librenms/) " );
2020-06-06 17:03:32 -05:00
}
2020-06-08 02:21:03 -05:00
2020-06-09 23:24:43 -05:00
$config_contents = stripslashes ( $config_contents );
fwrite ( $conf , $config_contents );
2020-06-06 17:03:32 -05:00
fclose ( $conf );
2020-06-08 02:21:03 -05:00
return ;
2020-06-06 17:03:32 -05:00
}
2020-06-08 02:21:03 -05:00
throw new Exception ( " We couldn't create the config.php file, please create this manually before continuing by copying the below into a config.php in the root directory of your install (typically /opt/librenms/) " );
2020-06-06 17:03:32 -05:00
}
}
2020-06-07 14:53:40 -05:00
public static function enabled ( $steps ) : bool
{
foreach ( $steps as $step => $controller ) {
if ( $step !== 'finish' && ! session ( " install. $step " )) {
return false ;
}
}
return true ;
}
public static function icon () : string
{
return 'fa-check' ;
}
2020-06-06 17:03:32 -05:00
private function getConfigFileContents ()
{
$db = session ( 'db' );
$install_dir = base_path ();
return <<< " EOD "
## Have a look in defaults.inc.php for examples of settings you can set here. DO NOT EDIT defaults.inc.php!
### Database config
2020-06-09 23:24:43 -05:00
\ $config [ 'db_host' ] = '{$db[' host ']}' ;
\ $config [ 'db_port' ] = '{$db[' port ']}' ;
\ $config [ 'db_user' ] = '{$db[' username ']}' ;
\ $config [ 'db_pass' ] = '{$db[' password ']}' ;
\ $config [ 'db_name' ] = '{$db[' database ']}' ;
\ $config [ 'db_socket' ] = '{$db[' unix_socket ']}' ;
2020-06-06 17:03:32 -05:00
// This is the user LibreNMS will run as
//Please ensure this user is created and has the correct permissions to your install
\ $config [ 'user' ] = 'librenms' ;
### Locations - it is recommended to keep the default
2020-06-09 23:24:43 -05:00
#\$config['install_dir'] = "$install_dir";
2020-06-06 17:03:32 -05:00
### This should *only* be set if you want to *force* a particular hostname/port
### It will prevent the web interface being usable form any other hostname
2020-06-09 23:24:43 -05:00
#\$config['base_url'] = "http://librenms.company.com";
2020-06-06 17:03:32 -05:00
### Enable this to use rrdcached. Be sure rrd_dir is within the rrdcached dir
### and that your web server has permission to talk to rrdcached.
2020-06-09 23:24:43 -05:00
#\$config['rrdcached'] = "unix:/var/run/rrdcached.sock";
2020-06-06 17:03:32 -05:00
### Default community
2020-06-09 23:24:43 -05:00
\ $config [ 'snmp' ][ 'community' ] = [ 'public' ];
2020-06-06 17:03:32 -05:00
### Authentication Model
2020-06-09 23:24:43 -05:00
\ $config [ 'auth_mechanism' ] = " mysql " ; # default, other options: ldap, http-auth
#\$config['http_auth_guest'] = "guest"; # remember to configure this user if you use http-auth
2020-06-06 17:03:32 -05:00
### List of RFC1918 networks to allow scanning-based discovery
2020-06-09 23:24:43 -05:00
#\$config['nets'][] = "10.0.0.0/8";
#\$config['nets'][] = "172.16.0.0/12";
#\$config['nets'][] = "192.168.0.0/16";
2020-06-06 17:03:32 -05:00
# Update configuration
2020-06-09 23:24:43 -05:00
#\$config['update_channel'] = 'release'; # uncomment to follow the monthly release channel
#\$config['update'] = 0; # uncomment to completely disable updates
2020-06-06 17:03:32 -05:00
EOD ;
}
2020-06-05 14:26:57 -05:00
}