page_title = _('Helpers Installation');
$this->init();
$this->register_form_installation();
$this->process_form();
$this->content = $this->render_content();
}
public function init() {
if( ! isset( $_SESSION[self::FORM_ID] ) ) {
$_SESSION[self::FORM_ID] = array();
}
$this->handle_reset();
}
public function process_form() {
$form = $this->form;
if( ! $form ) {
OpenSim::notify_error( 'Could not create form');
} else {
$next_step_key = $form->get_next_step();
$next_step_label = array_key_exists( $next_step_key, $form->steps ) ? $form->steps[$next_step_key]['label'] : false;
if ( isset( $_POST['step_key'] ) && $_POST['step_key'] == $next_step_key && ! empty( $form->tasks ) ) {
error_log( "Starting tasks of $next_step_key label $next_step_label" );
$result = false;
foreach( $form->tasks as $key => $task ) {
$callback_name = OpenSim::callback_name_string( $task['callback'] );
// error_log( 'starting task ' . $task['label'] );
try {
$result = call_user_func( $task['callback'] );
if( ! $result ) {
throw new OpenSim_Error( $task['error'] ?? null );
}
} catch (Throwable $e) {
$result = false;
// error_log( $message );
OpenSim::notify_error( $e );
break;
}
// if( ! $result ) {
// $message = $callback_name . '() ' . ( $task['error'] ?? 'Failed' );
// error_log( $message );
// OpenSim::notify_error( $message );
// break;
// }
$message = ( $task['label'] ?? $callback_name . '()' ) . ': ' . ( $task['success'] ?? 'Success' );
error_log( '[' . __CLASS__ . '] ' . $message );
OpenSim::notify( $message, 'task-checked' );
}
$prefix = '' . $next_step_label . ': ';
if( ! $result ) {
$message = $prefix . ( $form->steps[$next_step_key]['error'] ?? 'Failed' );
OpenSim::notify_error( $message, 'danger' );
} else if ( $result instanceof Error ) {
$message = $prefix . $result->getMessage();
OpenSim::notify_error( $message, 'danger' );
} else {
$message = $prefix . ( $form->steps[$next_step_key]['success'] ?? 'Success' );
OpenSim::notify( $message, 'success' );
// Register the form again to update values
$form->complete( $next_step_key );
$this->register_form_installation();
}
}
}
}
private function robust_generate_config() {
$template = 'includes/config.example.php';
if ( ! file_exists( $template )) {
OpenSim::notify_error( _('Template file not found.') );
return false;
}
try {
$php_template = file_get_contents($template);
} catch (Throwable $e) {
OpenSim::notify_error( $e );
return false;
}
try {
$config = $_SESSION[self::FORM_ID]['config'] ?? null;
if( empty( $config ) ) {
throw new OpenSim_Error( _('No configuration found.') );
}
} catch (Throwable $e) {
OpenSim::notify_error( $e );
return false;
}
// $config = $_SESSION[self::FORM_ID]['config'] ?? null;
// if( empty( $config ) ) {
// OpenSim::notify_error( __FUNCTION__ . '() ' . _('No configuration found.') );
// return false;
// }
$robust_creds = OpenSim::connectionstring_to_array($config['DatabaseService']['ConnectionString']);
$registrars = array(
'DATA_SRV_W4OSDev' => "http://dev.w4os.org/helpers/register.php",
'DATA_SRV_2do' => 'http://2do.directory/helpers/register.php',
'DATA_SRV_MISearch' => 'http://metaverseink.com/cgi-bin/register.py',
);
$console = array(
'ConsoleUser' => $config['Network']['ConsoleUser'],
'ConsolePass' => $config['Network']['ConsolePass'],
'ConsolePort' => $config['Network']['ConsolePort'],
'numeric' => 123456789,
'boolean_string' => 'true',
);
// Define mapping between config array keys and template constants
$mapping = array(
'OPENSIM_GRID_NAME' => $config['Const']['BaseURL'],
'OPENSIM_LOGIN_URI' => $config['Const']['BaseURL'] . ':' . $config['Const']['PublicPort'],
'OPENSIM_MAIL_SENDER' => "no-reply@" . parse_url($config['Const']['BaseURL'], PHP_URL_HOST),
'ROBUST_DB' => $robust_creds,
'OPENSIM_DB' => true, // Changed from string to boolean
'OPENSIM_DB_HOST' => $robust_creds['host'],
'OPENSIM_DB_PORT' => $robust_creds['port'] ?? null,
'OPENSIM_DB_NAME' => $robust_creds['name'],
'OPENSIM_DB_USER' => $robust_creds['user'],
'OPENSIM_DB_PASS' => $robust_creds['pass'],
'SEARCH_REGISTRARS' => $registrars,
'ROBUST_CONSOLE' => $console,
'CURRENCY_NAME' => $config['LoginService']['Currency'] ?? 'L$',
'CURRENCY_HELPER_URL' => $config['GridInfoService']['economy'] ?? '',
// Add more mappings as needed
);
// Replace placeholders in the template
foreach ($mapping as $constant => $value) {
$pattern = "/define\(\s*'{$constant}'\s*,\s*(?:array\s*\([^;]*?\)|'[^']*'|\"[^\"]*\"|[^)]+)\s*\);/s";
if (is_array($value)) {
$exported = var_export($value, true);
// Remove quotes for numeric and boolean strings if necessary
$exported = preg_replace("/'([0-9]+)'/", '$1', $exported);
$exported = str_replace("'true'", 'true', $exported);
$exported = str_replace("'false'", 'false', $exported);
$replacement = "define( '{$constant}', {$exported} );";
} else if( $value === null ) {
$exported = "NULL";
$replacement = "define( '{$constant}', {$exported} );";
} else if (is_bool($value)) {
$bool = $value ? 'true' : 'false';
$replacement = "define( '{$constant}', {$bool} );";
} else if (is_numeric($value)) {
$replacement = "define( '{$constant}', {$value} );";
} else {
$replacement = "define( '{$constant}', '" . addslashes($value) . "' );";
}
$php_template = preg_replace($pattern, $replacement, $php_template);
}
// Write the updated config to config.php
if( empty( $_SESSION[self::FORM_ID]['config_file'] ) ) {
// Should not happen, it has been validated before
$message = _( 'No config file specified, should be possible at this stage.' );
error_log( 'ERROR ' . __FUNCTION__ . '() ' . $message );
OpenSim::notify_error( __FUNCTION__ . '() ' . _('No config file specified, should not have occured.'), 'danger');
return false;
}
$temp_config_file = $_SESSION[self::FORM_ID]['config_file'] . '.install.temp';
try {
$result = file_put_contents($temp_config_file, $php_template);
if ( ! $result ) {
throw new OpenSim_Error( sprintf(
_( 'Error writing temporary file, make sure the web server has read/write permissions to %s directory.'),
'' . dirname( $temp_config_file ) . '/' . $temp_config_file . '' );
}
try {
include_once( $temp_config_file );
// error_log( 'OPENSIM_GRID_NAME ' . OPENSIM_GRID_NAME );
} catch (Throwable $e) {
OpenSim::notify_error( $e );
return false;
}
// TODO: more extensive tests with all the constants used by the other scripts
$required_constants=array(
'CURRENCY_DB_HOST',
'CURRENCY_DB_NAME',
'CURRENCY_DB_PASS',
'CURRENCY_DB_USER',
'CURRENCY_HELPER_URL',
'CURRENCY_MONEY_TBL',
'CURRENCY_PROVIDER',
'CURRENCY_RATE',
'CURRENCY_RATE_PER',
'CURRENCY_SCRIPT_KEY',
'CURRENCY_TRANSACTION_TBL',
'CURRENCY_USE_MONEYSERVER',
'HYPEVENTS_URL',
'OFFLINE_MESSAGE_TBL',
'OPENSIM_DB',
'OPENSIM_DB_HOST',
'OPENSIM_DB_NAME',
'OPENSIM_DB_PASS',
'OPENSIM_DB_USER',
'OPENSIM_GRID_NAME',
'OPENSIM_MAIL_SENDER',
'SEARCH_DB_HOST',
'SEARCH_DB_NAME',
'SEARCH_DB_PASS',
'SEARCH_DB_USER',
'SEARCH_REGISTRARS',
'SEARCH_TABLE_EVENTS',
);
foreach( $required_constants as $constant ) {
if( ! defined( $constant ) ) {
$missing[$constant] = sprintf( _('Constant %s is missing.'), $constant );
}
$missing_count = ( ! empty( $missing ) ) ? count( $missing ) : 0;
}
if( $missing_count > 0 ) {
$message = sprintf( _('%s error(s) while generating config file.'), $missing_count );
$message .= '