Exemplo n.º 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.
  * @return boolean
  */
 public function Save($File = NULL, $Group = NULL)
 {
     // Plain calls to Gdn::Config()->Save() simply save the dynamic config and return
     if (is_null($File)) {
         return $this->Dynamic->Save();
     }
     // ... otherwise we're trying to extract some of the config for some reason
     if ($File == '') {
         trigger_error(ErrorMessage('You must specify a file path to be saved.', $Group, 'Save'), E_USER_ERROR);
     }
     if (!is_writable($File)) {
         throw new Exception(sprintf(T("Unable to write to config file '%s' when saving."), $File));
     }
     if (empty($Group)) {
         $Group = $this->DefaultGroup;
     }
     $Data =& $this->Data;
     ksort($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 == $this->DefaultPath()) {
         if (!isset($Data['Database'])) {
             if ($Pm = Gdn::PluginManager()) {
                 $Pm->EventArguments['Data'] = $Data;
                 $Pm->EventArguments['Backtrace'] = debug_backtrace();
                 $Pm->FireEvent('ConfigError');
             }
             return FALSE;
         }
     }
     // Build string
     $FileContents = $this->Format($Data, array('VariableName' => $Group, 'Headers' => TRUE, 'ByLine' => TRUE, 'WrapPHP' => TRUE));
     if ($FileContents === FALSE) {
         trigger_error(ErrorMessage('Failed to define configuration file contents.', $Group, 'Save'), E_USER_ERROR);
     }
     $FileKey = sprintf(Gdn_Configuration::CONFIG_FILE_CACHE_KEY, $File);
     if ($this->Caching() && Gdn::Cache()->Type() == Gdn_Cache::CACHE_TYPE_MEMORY && Gdn::Cache()->ActiveEnabled()) {
         Gdn::Cache()->Store($FileKey, $Data, array(Gdn_Cache::FEATURE_NOPREFIX => TRUE, Gdn_Cache::FEATURE_EXPIRY => 3600));
     }
     // Infrastructure deployment. Use old method.
     $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);
     }
     return $Result;
 }