MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/uds.cafsinfotech.in/Dump/ZRM/application_bk28jun2024/controllers/Dynamic_db.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Dynamic_db  extends Action_controller{
	
	public function __construct(){
		parent::__construct('dynamic_db');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$this->load->view("$this->control_name/manage");
	}
	
	//Dunamic db updated
	public function db_config(){
		$hostname  = $this->input->post('hostname');
		$username  = $this->input->post('username');
		$password  = $this->input->post('password');
		$database  = $this->input->post('database');
		$sql_database  = $this->input->post('sql_database');
		$sql_hostname  = $this->input->post('sql_hostname');

		$db_info   = array('hostname'=>$hostname,'username'=>$username, 'password'=>$password, 'database'=>$database);
		$link = mysqli_connect($db_info['hostname'], $db_info['username'], $db_info['password'], $db_info['database']);
		if(mysqli_connect_errno()){
			$message = mysqli_connect_error();
			echo json_encode(array('success'=>False,'msg'=>$message));
			exit(0);
		}else{
			// if(!$password){
			// 	$password 	= " ";
			// }
			$write_text  = "<?php\n\t
				\$db['default'] = array(
					'dsn'	=> '',
					'hostname' => '$hostname',
					'username' => '$username',
					'password' => '$pwd_encode',
					'database' => '$database',
					'dbdriver' => 'mysqli',
					'dbprefix' => 'cw_',
					'pconnect' => FALSE,
					'sql_hostname' => '$sql_hostname',
					'sql_database' => '$sql_database',
					
				);";
			/** SATHISH START **/
			$app_db_text = "<?php \n\t class dbconnect {
			//MSSQL	
			protected \$db;	
			//MSSQL
			protected \$sql_db;
			
			public function open_db(){
				require '../database.php';
				\$this->host 	= \$db['default']['hostname'];
				\$this->username = \$db['default']['username'];
				\$this->password = \$db['default']['password'];
				\$this->database = \$db['default']['database'];
				\$this->db 		= new mysqli(\$this->host, \$this->username, \$this->password, \$this->database);
				if(mysqli_connect_errno()){
					return false;
				}else{
					return true;
				}
			}
			
			public function runQuery(\$query){
				\$result = mysqli_query(\$this->db,\$query);
				if(!\$result){
					echo('Error description: '.mysqli_error(\$this->db).'<br/>');
					return false;
				}else{
					return \$result;
				}		
			}
			
			public function result(\$result){
				\$data = array();
				while (\$obj = mysqli_fetch_object(\$result)) {
					if(\$obj){				
						\$data[] = \$obj;
					}
				}
				return \$data;
			}
			public function runQuery_insert_id(\$query) {
				\$result    = mysqli_query(\$this->db,\$query);
				\$insert_id = \$this->db->insert_id;
				if(!\$result){
					echo('Error description: '.mysqli_error(\$this->db).'<br/>');
					return false;
				}else{
					return \$insert_id;
				}
			}
			
			public function num_rows(\$result){
				return mysqli_num_rows(\$result);
			}
			
			public function close_db(){
				mysqli_close(\$this->db);
			}
			public function sql_runQuery(\$query) {
				\$result = sqlsrv_query(\$this->sql_db,\$query);
				if(!\$result){
					//echo('Error description: '.sqlsrv_errors(\$this->sql_db).'<br/>');
					return false;
				}else{
					return \$result;
				}		
			}
			public function sql_result(\$result){
				\$data = array();
				while (\$obj = sqlsrv_fetch_object(\$result)){
					if(\$obj){				
						\$data[] = \$obj;
					}
				}
				return \$data;
			}
			//SQL Functions
			public function sql_open_db(){
				require '../database.php';
				\$this->sql_host 	 = \$db['default']['sql_hostname'];
				\$this->sql_database = \$db['default']['sql_database'];
				\$this->sql_db = sqlsrv_connect(\$this->sql_host, array( 'Database'=>\$this->sql_database));
				if(\$this->sql_db === false ) {
					return false;
				}else{
					return true;
				}
			}
		} \n\t ?>";
			$app_db_filename = "./app/dbconnect.php";
			chmod($app_db_filename, 0777, true);
			$app_file = fopen($app_db_filename, "w");
			fwrite($app_file, $app_db_text);
			fclose($app_file);
			chmod($app_file, 0755);
			/** SATHISH END **/
			$file_name    = "./database.php";
			chmod($file_name, 0777, true);
			$file = fopen($file_name, "w");
			fwrite($file, $write_text);
			fclose($file);
			chmod($file, 0755);
			$message ="DB update successfully!!!";
			$this->session->sess_destroy();
			echo json_encode(array('success'=>True,'msg'=>$message));
		}
	}
}
?>