_sanitize($data['db_hostname']), $database_file); $new = str_replace("%USERNAME%", $this->_sanitize($data['db_username']), $new); $new = str_replace("%PASSWORD%", $this->_sanitize($data['db_password'] ?? ''), $new); $new = str_replace("%DATABASE%", $this->_sanitize($data['db_name']), $new); log_message('info', 'Database config file prepared successfully. Writing to file...'); // Write the new database.php file $handle = fopen($output_path, 'w+'); if ($handle === false) { log_message('error', 'Failed to open target path for writing the database.php file.'); return false; } // Verify file permissions if (is_writable($output_path)) { // Write the file if (fwrite($handle, $new)) { if(file_exists($output_path)) { log_message('info', 'database.php file written successfully.'); return true; } else { log_message('error', 'database.php file not found after writing.'); return false; } } else { return false; } } else { log_message('error', 'database.php path is not writable.'); return false; } } // Function to write the config file function write_configfile($data) { $template_path = 'config/config.php'; $output_path = '../application/config/config.php'; if (isset($_ENV['CI_ENV'])) { $output_path = '../application/config/'.$_ENV['CI_ENV'].'/config.php'; $output_dir = dirname($output_path); if (!is_dir($output_dir)) { if (!mkdir($output_dir, 0755, true)) { log_message('error', 'Failed to create directory: ' . $output_dir); return false; } log_message('info', 'Directory created: ' . $output_dir); } log_message('info', 'CI_ENV is set to ' . $_ENV['CI_ENV'] . '. Using ' . $_ENV['CI_ENV'] . ' config.php config path.'); } else { log_message('info', 'CI_ENV is not set. Using default config.php config path.'); } // if config file already exists, we stop early and fail if (file_exists($output_path)) { log_message('error', 'config.php config file already exists'); return false; } // Open the file $config_file = file_get_contents($template_path); if ($config_file === false) { log_message('error', 'Failed to read config.php template file.'); return false; } log_message('info', 'config.php template file read successfully.'); // creating a unique encryption key $encryptionkey = uniqid(bin2hex(random_bytes(8)), false); $new = str_replace("%baselocator%", strtoupper($data['userlocator']), $config_file); $new = str_replace("%websiteurl%", $this->_sanitize($data['websiteurl']), $new); $new = str_replace("%directory%", $this->_sanitize($data['directory']), $new); $new = str_replace("%callbook%", $this->_sanitize($data['global_call_lookup']), $new); // Username/password-based providers $callbooks_userpass = ['qrz', 'hamqth', 'qrzcq', 'qrzru']; // Token-based providers (single "_token" placeholder) $callbooks_token = ['qrzcall']; $selected = $data['global_call_lookup'] ?? ''; // Substitute the selected provider's credentials, blank out everything else. if (in_array($selected, $callbooks_userpass, true)) { $c_username = '%' . $selected . '_username%'; $c_password = '%' . $selected . '_password%'; $new = str_replace($c_username, $this->_sanitize($data['callbook_username'] ?? ''), $new); $new = str_replace($c_password, $this->_sanitize($data['callbook_password'] ?? ''), $new); } if (in_array($selected, $callbooks_token, true)) { $c_token = '%' . $selected . '_token%'; $new = str_replace($c_token, $this->_sanitize($data['callbook_token'] ?? ''), $new); } // Blank out all non-selected provider placeholders so the generated // config has empty strings for the inactive ones. foreach ($callbooks_userpass as $cb) { if ($cb === $selected) continue; $new = str_replace('%' . $cb . '_username%', '', $new); $new = str_replace('%' . $cb . '_password%', '', $new); } foreach ($callbooks_token as $cb) { if ($cb === $selected) continue; $new = str_replace('%' . $cb . '_token%', '', $new); } $new = str_replace("%encryptionkey%", $encryptionkey, $new); $new = str_replace("'%log_threshold%'", (int)$data['log_threshold'], $new); log_message('info', 'Config.php file prepared successfully. Writing to file...'); // Write the new config.php file $handle = fopen($output_path, 'w+'); if ($handle === false) { log_message('error', 'Failed to open target path for writing the config.php file.'); return false; } // Verify file permissions if (is_writable($output_path)) { // Write the file if (fwrite($handle, $new)) { if(file_exists($output_path)) { log_message('info', 'config.php file written successfully.'); $_SESSION['cron_auth_token'] = hash_hmac('sha256', 'wavelog-cron-v1', $encryptionkey); return true; } else { log_message('error', 'config.php file not found after writing.'); return false; } } else { return false; } } else { log_message('error', 'config.php path is not writable.'); return false; } } private function _sanitize($value){ $value = str_replace('\\', '\\\\', $value ?? ''); return str_replace("'", "\\'", $value); } }