/**
  * Outputs the array string which is then used in the config file.
  *
  * @param $array array Array of values to store in the config.
  * @param $num_tabs int (Optional) The number of tabs to use in front of the array elements - makes it all nice !
  *
  * @return string A string of text which will make up the array values in the config file.
  */
 function config_array_output($array, $num_tabs = 1)
 {
     if (!is_array($array)) {
         return FALSE;
     }
     $tval = 'array(';
     // allow for two-dimensional arrays
     $array_keys = array_keys($array);
     // check if they are basic numeric keys
     if (is_numeric($array_keys[0]) && $array_keys[0] == 0) {
         $tval .= "'" . implode("','", $array) . "'";
     } else {
         $tabs = "";
         for ($num = 0; $num < $num_tabs; $num++) {
             $tabs .= "\t";
         }
         // non-numeric keys
         foreach ($array as $key => $value) {
             if (is_array($value)) {
                 $num_tabs++;
                 $tval .= "\n" . $tabs . "'" . $key . "' => " . config_array_output($value, $num_tabs) . ",";
             } else {
                 $tval .= "\n" . $tabs . "'" . $key . "' => '" . $value . "',";
             }
         }
         //end foreach
         $tval .= "\n" . $tabs;
     }
     //end if
     $tval .= ')';
     return $tval;
 }
 /**
  * Save the passed array settings into a single config file located in the
  * config directory.
  *
  * @param string $file     The config file to write to.
  * @param array  $settings An array of config setting name/value pairs to be
  *                         written to the file.
  * @param string $module   Name of the module where the config file exists.
  * @param string $config_path
  *
  * @return bool False on error, else true.
  */
 function write_config($file = '', $settings = NULL, $module = '', $config_path = APPPATH)
 {
     if (empty($file) || !is_array($settings)) {
         return FALSE;
     }
     $config_file = "config/{$file}";
     // Look in module first.
     $found = FALSE;
     if ($module) {
         $file_details = Modules::find($config_file, $module, '');
         if (!empty($file_details) and !empty($file_details[0])) {
             $config_file = implode('', $file_details);
             $found = TRUE;
         }
     }
     // Fall back to application directory.
     if (!$found) {
         $config_file = "{$config_path}{$config_file}";
         $found = is_file($config_file . '.php');
     }
     // Load the file and loop through the lines.
     if ($found) {
         $contents = file_get_contents($config_file . '.php');
         $empty = FALSE;
     } else {
         // If the file was not found, create a new file.
         $contents = '';
         $empty = TRUE;
     }
     foreach ($settings as $name => $val) {
         // Is the config setting in the file?
         $start = strpos($contents, '$config[\'' . $name . '\']');
         $end = strpos($contents, ';', $start);
         $search = substr($contents, $start, $end - $start + 1);
         // Format the value to be written to the file.
         if (is_array($val)) {
             // Get the array output.
             $val = config_array_output($val);
         } elseif (!is_numeric($val)) {
             $val = "\"{$val}\"";
         }
         // For a new file, just append the content. For an existing file, search
         // the file's contents and replace the config setting.
         if ($empty) {
             $contents .= '$config[\'' . $name . '\'] = ' . $val . ";\n";
         } else {
             $contents = str_replace($search, '$config[\'' . $name . '\'] = ' . $val . ';', $contents);
         }
     }
     // Make sure the file still has the php opening header in it...
     if (strpos($contents, '<?php') === FALSE) {
         $contents = "<?php defined('BASEPATH') OR exit('No direct script access allowed');\n\n{$contents}";
     }
     // Write the changes out...
     if (!function_exists('write_file')) {
         get_instance()->load->helper('file');
     }
     $result = write_file("{$config_file}.php", $contents);
     return $result !== FALSE;
 }
 /**
  * Save the passed array settings into a single config file located in the
  * /config directory.
  *
  * The $settings passed in should be an array of key/value pairs, where the
  * key is the name of the config setting and the value is its value.
  *
  * @param string $file     The config file to write to.
  * @param array  $settings An array of key/value pairs to be written to the file.
  * @param string $module   Name of the module where the config file exists.
  *
  * @return boolean
  */
 function write_config($file = '', $settings = null, $module = '', $apppath = APPPATH)
 {
     if (empty($file) || !is_array($settings)) {
         return false;
     }
     $configFile = "config/{$file}";
     // Look in module first
     $found = false;
     if ($module) {
         $fileDetails = Modules::find($configFile, $module, '');
         if (!empty($fileDetails) && !empty($fileDetails[0])) {
             $configFile = implode('', $fileDetails);
             $found = true;
         }
     }
     // Fall back to application directory
     if (!$found) {
         $configFile = "{$apppath}{$configFile}";
         $found = is_file($configFile . '.php');
     }
     // Load the file and loop through the lines
     if ($found) {
         $contents = file_get_contents($configFile . '.php');
         $empty = false;
     } else {
         $contents = '';
         $empty = true;
     }
     foreach ($settings as $name => $val) {
         // Is the config setting in the file?
         $start = strpos($contents, '$config[\'' . $name . '\']');
         $end = strpos($contents, ';', $start);
         $search = substr($contents, $start, $end - $start + 1);
         if (is_array($val)) {
             // Get the array output
             $val = config_array_output($val);
         } elseif (!is_numeric($val)) {
             $val = "\"{$val}\"";
         }
         if ($empty) {
             $contents .= '$config[\'' . $name . '\'] = ' . $val . ";\n";
         } else {
             $contents = str_replace($search, '$config[\'' . $name . '\'] = ' . $val . ';', $contents);
         }
     }
     // Backup the file for safety
     $source = $configFile . '.php';
     $dest = ($module == '' ? "{$apppath}archives/{$file}" : $configFile) . '.php.bak';
     if ($empty === false) {
         copy($source, $dest);
     }
     // Make sure the file still has the php opening header in it...
     if (strpos($contents, '<?php') === false) {
         $contents = "<?php defined('BASEPATH') || exit('No direct script access allowed');\n\n{$contents}";
     }
     // Write the changes out...
     if (!function_exists('write_file')) {
         get_instance()->load->helper('file');
     }
     $result = write_file("{$configFile}.php", $contents);
     return $result !== false;
 }