Example #1
0
 /**
  * Saves all settings in $Group to $File.
  *
  * @param string $File The full path to the file where the Settings should be saved.
  * @param string $Group The name of the settings group to be saved to the $File.
  * @param boolean $RequireSourceFile Should $File be required to exist in order to save? If true, then values
  * from this file will be merged into the settings array before it is saved.
  * If false, the values in the settings array will overwrite any values
  * existing in the file (if it exists).
  * @return boolean
  */
 public function Save($File = '', $Group = '', $RequireSourceFile = TRUE)
 {
     if ($File == '') {
         $File = $this->_File;
     }
     if ($File == '') {
         trigger_error(ErrorMessage('You must specify a file path to be saved.', 'Configuration', 'Save'), E_USER_ERROR);
     }
     if (!is_writable($File)) {
         throw new Exception(sprintf(T("Unable to write to config file '%s' when saving."), $File));
     }
     if ($Group == '') {
         $Group = $this->CurrentGroup;
     }
     if ($Group == '') {
         $Group = 'Configuration';
     }
     $Data =& $this->_SaveData;
     $this->_Sort($Data);
     // Check for the case when the configuration is the group.
     if (is_array($Data) && count($Data) == 1 && array_key_exists($Group, $Data)) {
         $Data = $Data[$Group];
     }
     // Do a sanity check on the config save.
     if ($File == PATH_LOCAL_CONF . '/config.php') {
         if (!isset($Data['Database'])) {
             if ($Pm = Gdn::PluginManager()) {
                 $Pm->EventArguments['Data'] = $Data;
                 $Pm->EventArguments['Backtrace'] = debug_backtrace();
                 $Pm->FireEvent('ConfigError');
             }
             $this->_SaveData = array();
             $this->_File = '';
             return FALSE;
         }
     }
     $NewLines = array();
     $NewLines[] = "<?php if (!defined('APPLICATION')) exit();";
     $LastName = '';
     foreach ($Data as $Name => $Value) {
         // Write a newline to seperate sections.
         if ($LastName != $Name && is_array($Value)) {
             $NewLines[] = '';
             $NewLines[] = '// ' . $Name;
         }
         $Line = "\$" . $Group . "['" . $Name . "']";
         FormatArrayAssignment($NewLines, $Line, $Value);
     }
     // Record who made the change and when
     if (is_array($NewLines)) {
         $Session = Gdn::Session();
         $User = $Session->UserID > 0 && is_object($Session->User) ? $Session->User->Name : 'Unknown';
         $NewLines[] = '';
         $NewLines[] = '// Last edited by ' . $User . ' (' . RemoteIp() . ')' . Gdn_Format::ToDateTime();
     }
     $FileContents = FALSE;
     if ($NewLines !== FALSE) {
         $FileContents = implode("\n", $NewLines);
     }
     if ($FileContents === FALSE) {
         trigger_error(ErrorMessage('Failed to define configuration file contents.', 'Configuration', 'Save'), E_USER_ERROR);
     }
     $FileKey = sprintf(self::CONFIG_FILE_CACHE_KEY, $File);
     if ($this->Caching() && Gdn::Cache()->Type() == Gdn_Cache::CACHE_TYPE_MEMORY && Gdn::Cache()->ActiveEnabled()) {
         $CachedConfigData = Gdn::Cache()->Store($FileKey, $Data, array(Gdn_Cache::FEATURE_NOPREFIX => TRUE));
     }
     // Infrastructure deployment. Use old method.
     if (PATH_LOCAL_CONF != PATH_CONF) {
         $Result = Gdn_FileSystem::SaveFile($File, $FileContents, LOCK_EX);
     } else {
         $TmpFile = tempnam(PATH_CONF, 'config');
         $Result = FALSE;
         if (file_put_contents($TmpFile, $FileContents) !== FALSE) {
             chmod($TmpFile, 0775);
             $Result = rename($TmpFile, $File);
         }
     }
     if ($Result && function_exists('apc_delete_file')) {
         // This fixes a bug with some configurations of apc.
         @apc_delete_file($File);
     }
     // Clear out the save data array
     $this->_SaveData = array();
     $this->_File = '';
     return $Result;
 }
Example #2
0
 function FormatArrayAssignment(&$Array, $Prefix, $Value)
 {
     if (is_array($Value)) {
         // If $Value doesn't contain a key of "0" OR it does and it's value IS
         // an array, this should be treated as an associative array.
         $IsAssociativeArray = array_key_exists(0, $Value) === FALSE || is_array($Value[0]) === TRUE ? TRUE : FALSE;
         if ($IsAssociativeArray === TRUE) {
             foreach ($Value as $k => $v) {
                 FormatArrayAssignment($Array, $Prefix . "['{$k}']", $v);
             }
         } else {
             // If $Value is not an associative array, just write it like a simple array definition.
             $FormattedValue = array_map(array('Gdn_Format', 'ArrayValueForPhp'), $Value);
             $Array[] = $Prefix .= " = array('" . implode("', '", $FormattedValue) . "');";
         }
     } elseif (is_int($Value)) {
         $Array[] = $Prefix .= ' = ' . $Value . ';';
     } elseif (is_bool($Value)) {
         $Array[] = $Prefix .= ' = ' . ($Value ? 'TRUE' : 'FALSE') . ';';
     } elseif (in_array($Value, array('TRUE', 'FALSE'))) {
         $Array[] = $Prefix .= ' = ' . ($Value == 'TRUE' ? 'TRUE' : 'FALSE') . ';';
     } else {
         $Array[] = $Prefix .= ' = ' . var_export($Value, TRUE) . ';';
     }
 }
Example #3
0
 /**
  * Saves all settings in $Group to $File.
  *
  * @param string $File The full path to the file where the Settings should be saved.
  * @param string $Group The name of the settings group to be saved to the $File.
  * @param boolean $RequireSourceFile Should $File be required to exist in order to save? If true, then values
  * from this file will be merged into the settings array before it is saved.
  * If false, the values in the settings array will overwrite any values
  * existing in the file (if it exists).
  * @return boolean
  */
 public function Save($File = '', $Group = '', $RequireSourceFile = TRUE)
 {
     if ($File == '') {
         $File = $this->_File;
     }
     if ($File == '') {
         trigger_error(ErrorMessage('You must specify a file path to be saved.', 'Configuration', 'Save'), E_USER_ERROR);
     }
     if (!is_writable($File)) {
         throw new Exception(sprintf(T("Unable to write to config file '%s' when saving."), $File));
     }
     if ($Group == '') {
         $Group = $this->CurrentGroup;
     }
     if ($Group == '') {
         $Group = 'Configuration';
     }
     $Data =& $this->_SaveData;
     $this->_Sort($Data);
     // Check for the case when the configuration is the group.
     if (is_array($Data) && count($Data) == 1 && array_key_exists($Group, $Data)) {
         $Data = $Data[$Group];
     }
     $NewLines = array();
     $NewLines[] = "<?php if (!defined('APPLICATION')) exit();";
     $LastName = '';
     foreach ($Data as $Name => $Value) {
         // Write a newline to seperate sections.
         if ($LastName != $Name && is_array($Value)) {
             $NewLines[] = '';
             $NewLines[] = '// ' . $Name;
         }
         $Line = "\$" . $Group . "['" . $Name . "']";
         FormatArrayAssignment($NewLines, $Line, $Value);
     }
     // Record who made the change and when
     if (is_array($NewLines)) {
         $Session = Gdn::Session();
         $User = $Session->UserID > 0 && is_object($Session->User) ? $Session->User->Name : 'Unknown';
         $NewLines[] = '';
         $NewLines[] = '// Last edited by ' . $User . ' (' . RemoteIp() . ')' . Gdn_Format::ToDateTime();
     }
     $FileContents = FALSE;
     if ($NewLines !== FALSE) {
         $FileContents = implode("\n", $NewLines);
     }
     if ($FileContents === FALSE) {
         trigger_error(ErrorMessage('Failed to define configuration file contents.', 'Configuration', 'Save'), E_USER_ERROR);
     }
     // echo 'saving '.$File;
     Gdn_FileSystem::SaveFile($File, $FileContents, LOCK_EX);
     // Call the built in method to remove the dependancy to an external object.
     //file_put_contents($File, $FileContents);
     // Clear out the save data array
     $this->_SaveData = array();
     $this->_File = '';
     return TRUE;
 }