[eluser]Twisted1919[/eluser]
This is an additional method to keep your config updated from your admin backend using FTP.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setup{
public $ci ;
public function __construct()
{
$this->ci =& get_instance();
}
public function ftp_config($config_items)
{
$this->ci->load->helper('file');
$this->ci->load->library('ftp');
$file = read_file(APPPATH.'config/config.php');
if(! @file_exists(APPPATH.'config/ftp.php'))
{
return 1 ;
}
if(! is_array($config_items) )
{
return 2 ;
}
$this->ci->config->load('ftp');
$config['hostname'] = $this->ci->config->item('ftp_hostname') ;
$config['username'] = $this->ci->config->item('ftp_username') ;
$config['password'] = $this->ci->config->item('ftp_password') ;
$config['port'] = $this->ci->config->item('ftp_port') ;
$config['debug'] = FALSE;
$ftp_path = $this->ci->config->item('ftp_path') ;
if(!$this->ci->ftp->connect($config))
{
return 3 ;
}
if(empty($ftp_path))
{
$root_folders = $this->ci->ftp->list_files() ;
$known_roots = array('www','httpdocs','wwwroot','public_html');
foreach($root_folders AS $root)
{
if(in_array($root,$known_roots))
{
$ftp_path = '/'.$root.'/' ;
break;
}
}
}
$filename = $ftp_path.'system/application/config/config.php';
if(! $this->ci->ftp->chmod($filename, FILE_WRITE_MODE))
{
$this->ci->ftp->close();
return 4 ;
}
foreach($config_items AS $item=>$value)
{
if($value == 'TRUE' || $value == 'FALSE' || is_numeric($value))
{
$pattern = '/(\$config\[(\'|"){1}'.$item.'(\'|"){1}\])(\s|\t)*={1}(\s|\t)*(TRUE|FALSE|\d+)(\s|\t)*;{1}/ix';
$file = preg_replace($pattern,'$config[\''.$item.'\'] = '.$value.' ;',$file);
}
else
{
$value = str_replace(array("'",'\\'),'',$value);
$pattern = '/(\$config\[(\'|"){1}'.$item.'(\'|"){1}\])(\s|\t)*={1}(\s|\t)*(\'|"){0,1}.*(\'|"){1}(\s|\t)*;{1}/ix';
$file = preg_replace($pattern,'$config[\''.$item.'\'] = \''.$value.'\' ;',$file);
}
}
if(! write_file(APPPATH.'config/config.php', $file) )
{
$this->ci->ftp->close();
return 5 ;
}
if(! $this->ci->ftp->chmod($filename, FILE_READ_MODE))
{
$this->ci->ftp->close();
return 6 ;
}
$this->ci->ftp->close();
return 0 ;
}
}
As you can see , the ftp_config() function allows us to retrieve an array and write its keys/values to our config.php file .
Of course we need a new configuration file for our ftp connection , just create in the config folder and name it
ftp.php:
Code:
$config['ftp_hostname'] = 'my host';
$config['ftp_port'] = '21';
$config['ftp_timeout'] = '90';
$config['ftp_username'] = 'myusername';
$config['ftp_password'] = 'mypassword';
$config['ftp_path'] = '/path/to/my/ci/installation/';
Let's say, in our config.php file we would have :
Code:
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_time_to_update'] = 300;
And we would like to overwrite these values ,then we would do :
Code:
$data['sess_use_database'] = $this->input->post('sess_use_database',TRUE);
$data['sess_table_name'] = $this->input->post('sess_table_name',TRUE);
$data['sess_time_to_update'] = $this->input->post('sess_time_to_update',TRUE);
$this->load->library('setup');
$overwrite = $this->setup->ftp_config($data);
switch($overwrite)
{
case 0:
echo "Everything went OK";
exit;
break;
case 1:
echo "Missing ftp.php FILE";
exit;
break;
case 2:
echo "Please supply an array as argument";
exit;
break;
case 3:
echo "Incorrect FTP Data";
exit;
break;
case 4:
echo "Can't chmod to 0666";
exit;
break;
case 5:
echo "Can't write to config file";
exit;
break;
case 6:
echo "Cannot chmod to 0644";
exit;
break;
}
It's not big deal at all , you have to remember two things :
1. Always the array keys of the array that you pass to the method must have the same name as the ones from config.php file .
2. I did not find any method to escape the single quotes and the backslashes, so i destroy them before i save the configuration file.
Hope it helps