function write_ini_file($File, $Data)
 {
     $String = write_ini_string($Data);
     Gdn_FileSystem::SaveFile($File, $String);
 }
Exemple #2
0
 /**
  * Defines and loads the locale.
  *
  * @param string $LocaleName The name of the locale to load. Locale definitions are kept in each
  * application's locale folder. For example:
  *  /garden/locale/$LocaleName.php
  *  /people/locale/$LocaleName.php
  * @param array $ApplicationWhiteList An array of application folders that are safe to examine for locale
  *  definitions.
  * @param array $PluginWhiteList An array of plugin folders that are safe to examine for locale
  *  definitions.
  * @param bool $ForceRemapping For speed purposes, the application folders are crawled for locale
  *  sources. Once sources are found, they are saved in the
  *  cache/locale_mapppings.php file. If ForceRemapping is true, this file will
  *  be ignored and the folders will be recrawled and the mapping file will be
  *  re-generated. You can also simply delete the file and it will
  *  automatically force a remapping.
  */
 public function Set($LocaleName, $ApplicationWhiteList, $PluginWhiteList, $ForceRemapping = FALSE)
 {
     $LocaleMappings = PATH_CACHE . DS . 'locale_mappings.php';
     $SafeLocaleName = preg_replace('/([^\\w\\d_-])/', '', $LocaleName);
     // Removes everything from the string except letters, numbers, dashes, and underscores
     $LocaleSources = array();
     if (!is_array($ApplicationWhiteList)) {
         $ApplicationWhiteList = array();
     }
     if (!is_array($PluginWhiteList)) {
         $PluginWhiteList = array();
     }
     if ($ForceRemapping === FALSE && file_exists($LocaleMappings)) {
         include $LocaleMappings;
     } else {
         $LocaleSources = array();
         // Get application-based locale definition files
         $ApplicationLocaleSources = Gdn_FileSystem::FindAll(PATH_APPLICATIONS, CombinePaths(array('locale', $LocaleName, 'definitions.php')), $ApplicationWhiteList);
         if ($ApplicationLocaleSources !== FALSE) {
             $LocaleSources = $ApplicationLocaleSources;
         }
         // Get plugin-based locale definition files
         $PluginLocaleSources = Gdn_FileSystem::FindAll(PATH_PLUGINS, CombinePaths(array('locale', $LocaleName, 'definitions.php')), $PluginWhiteList);
         if ($PluginLocaleSources !== FALSE) {
             $LocaleSources = array_merge($LocaleSources, $PluginLocaleSources);
         }
         // Save the mappings
         $FileContents = array();
         $FileContents[] = "<?php if (!defined('APPLICATION')) exit();";
         $Count = count($LocaleSources);
         for ($i = 0; $i < $Count; ++$i) {
             $FileContents[] = "\$LocaleSources['" . $SafeLocaleName . "'][] = '" . Format::ArrayValueForPhp($LocaleSources[$i]) . "';";
         }
         Gdn_FileSystem::SaveFile($LocaleMappings, implode("\n", $FileContents));
     }
     // Set up defaults
     $Definition = array();
     $this->_Definition = array();
     // Now set the locale name and import all of the sources.
     $this->_Locale = $LocaleName;
     if (!array_key_exists($SafeLocaleName, $LocaleSources)) {
         $LocaleSources[$SafeLocaleName] = array();
     }
     $Count = count($LocaleSources[$SafeLocaleName]);
     for ($i = 0; $i < $Count; ++$i) {
         @(include $LocaleSources[$SafeLocaleName][$i]);
     }
     // Also load any custom defined definitions from the conf directory
     @(include PATH_CONF . DS . 'locale.php');
     // All of the included files should have contained
     // $Definition['Code'] = 'Definition'; assignments. The overwrote each
     // other in the order they were included. Now assign the $Definition array
     // to the local private _Definition property.
     $this->_Definition = $Definition;
 }
Exemple #3
0
 /**
  * Save the provided library's data to the on disk location.
  *
  * @param string $CacheName name of cache library
  * @return void
  */
 public static function SaveCache($CacheName)
 {
     if (!array_key_exists($CacheName, Gdn_LibraryMap::$_Caches)) {
         return FALSE;
     }
     $FileName = Gdn_LibraryMap::$_Caches[$CacheName]['ondisk'];
     $CacheContents = "<?php if (!defined('APPLICATION')) exit();\n" . "Gdn_LibraryMap::PrepareCache('{$CacheName}',";
     Gdn_LibraryMap::RecurseArrayStr(NULL, Gdn_LibraryMap::$_Caches[$CacheName]['cache'], $CacheContents);
     $CacheContents .= ");";
     try {
         Gdn_FileSystem::SaveFile(PATH_CACHE . DS . $FileName, $CacheContents);
     } catch (Exception $e) {
     }
 }
 /**
  * Save the provided library's data to the on disk location.
  *
  * @param string $CacheName name of cache library
  * @return void
  */
 public static function SaveCache($CacheName)
 {
     if ($CacheName != 'locale') {
         return;
     }
     if (!array_key_exists($CacheName, self::$_Caches)) {
         return FALSE;
     }
     $FileName = self::$_Caches[$CacheName]['ondisk'];
     $CacheContents = "";
     foreach (self::$_Caches[$CacheName]['cache'] as $SectionTitle => $SectionData) {
         $CacheContents .= "[{$SectionTitle}]\n";
         foreach ($SectionData as $StoreKey => $StoreValue) {
             $CacheContents .= "{$StoreKey} = \"{$StoreValue}\"\n";
         }
     }
     try {
         Gdn_FileSystem::SaveFile(PATH_LOCAL_CACHE . DS . $FileName, $CacheContents, LOCK_EX);
     } catch (Exception $e) {
     }
 }
 public function EnsureDefinitionFile()
 {
     $Path = $this->LocalePath . '/definitions.php';
     if (file_exists($Path)) {
         unlink($Path);
     }
     $Contents = $this->GetFileHeader() . self::FormatInfoArray('$LocaleInfo', $this->GetInfoArray());
     Gdn_FileSystem::SaveFile($Path, $Contents);
 }
            if ($SkipTranslated) {
                if ($T != T($Code)) {
                    continue;
                }
            }
            $Code = var_export($Code, True);
            $T = var_export($T, True);
            $FileContent .= "\n\$Definition[{$Code}] = {$T};";
        }
    }
    if ($FileContent == '') {
        continue;
    }
    $FileContent = "<?php\n// Date: " . date('r') . "  " . $FileContent;
    $File = $Directory . '/' . strtolower(pathinfo($File, PATHINFO_BASENAME));
    Gdn_FileSystem::SaveFile($File, $FileContent);
}
/**
* Undocumented 
* 
* @param string $File, path to file.
* @return array $Result.
*/
function GetTranslationFromFile($File)
{
    $Result = array();
    $Content = file_get_contents($File);
    if (!$Content) {
        return $Result;
    }
    $AllTokens = token_get_all($Content);
 public function Shutdown()
 {
     if (!GetValue('dirty', $this->MapInfo)) {
         return FALSE;
     }
     if (!GetValue('save', $this->MapInfo)) {
         return FALSE;
     }
     if (!sizeof($this->Map)) {
         return FALSE;
     }
     $MapName = GetValue('name', $this->MapInfo);
     $FileName = GetValue('ondisk', $this->MapInfo);
     $MapContents = '';
     foreach ($this->Map as $SplitTopic => $TopicFiles) {
         $MapContents .= "[{$SplitTopic}]\n";
         foreach ($TopicFiles as $ClassName => $Location) {
             $MapContents .= "{$ClassName} = \"{$Location}\"\n";
         }
     }
     try {
         Gdn_FileSystem::SaveFile($FileName, $MapContents, LOCK_EX);
     } catch (Exception $e) {
         return FALSE;
     }
     return TRUE;
 }
 /**
  * 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;
 }
 /**
  * Save the provided library's data to the on disk location.
  *
  * @param string $CacheName name of cache library
  * @return void
  */
 public static function SaveCache($CacheName)
 {
     if ($CacheName != 'locale') {
         return;
     }
     if (!array_key_exists($CacheName, self::$Caches)) {
         return FALSE;
     }
     $UseCache = Gdn::Cache()->Type() == Gdn_Cache::CACHE_TYPE_MEMORY && Gdn::Cache()->ActiveEnabled();
     if ($UseCache) {
         $CacheKey = sprintf(Gdn_LibraryMap::CACHE_CACHE_NAME_FORMAT, $CacheName);
         $Stored = Gdn::Cache()->Store($CacheKey, self::$Caches[$CacheName]['cache']);
     } else {
         $FileName = self::$Caches[$CacheName]['ondisk'];
         $CacheContents = "";
         foreach (self::$Caches[$CacheName]['cache'] as $SectionTitle => $SectionData) {
             $CacheContents .= "[{$SectionTitle}]\n";
             foreach ($SectionData as $StoreKey => $StoreValue) {
                 $CacheContents .= "{$StoreKey} = \"{$StoreValue}\"\n";
             }
         }
         try {
             Gdn_FileSystem::SaveFile(PATH_CACHE . DS . $FileName, $CacheContents, LOCK_EX);
         } catch (Exception $e) {
         }
     }
 }
Exemple #10
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;
 }