/** * 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 ($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 . "']"; $this->_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); // 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; }
/** * * * @param $Data * @param array $Options * @return string */ public static function format($Data, $Options = array()) { if (is_string($Options)) { $Options = array('VariableName' => $Options); } $Defaults = array('VariableName' => 'Configuration', 'WrapPHP' => true, 'SafePHP' => true, 'Headings' => true, 'ByLine' => true, 'FormatStyle' => 'Array'); $Options = array_merge($Defaults, $Options); $VariableName = val('VariableName', $Options); $WrapPHP = val('WrapPHP', $Options, true); $SafePHP = val('SafePHP', $Options, true); $ByLine = val('ByLine', $Options, false); $Headings = val('Headings', $Options, true); $FormatStyle = val('FormatStyle', $Options); $Formatter = "Format{$FormatStyle}Assignment"; $FirstLine = ''; $Lines = array(); if ($WrapPHP) { $FirstLine .= "<?php "; } if ($SafePHP) { $FirstLine .= "if (!defined('APPLICATION')) exit();"; } if (!empty($FirstLine)) { $Lines[] = $FirstLine; } if (!is_array($Data)) { return $Lines[0]; } $LastKey = false; foreach ($Data as $Key => $Value) { if ($Headings && $LastKey != $Key && is_array($Value)) { $Lines[] = ''; $Lines[] = '// ' . $Key; $LastKey = $Key; } if ($FormatStyle == 'Array') { $Prefix = '$' . $VariableName . "[" . var_export($Key, true) . "]"; } if ($FormatStyle == 'Dotted') { $Prefix = '$' . $VariableName . "['" . trim(var_export($Key, true), "'"); } $Formatter($Lines, $Prefix, $Value); } if ($ByLine) { $Session = Gdn::session(); $User = $Session->UserID > 0 && is_object($Session->User) ? $Session->User->Name : 'Unknown'; $Lines[] = ''; $Lines[] = '// Last edited by ' . $User . ' (' . RemoteIp() . ')' . Gdn_Format::toDateTime(); } $Result = implode(PHP_EOL, $Lines); return $Result; }
/** * 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; }