Overview

Packages

  • Prefs

Classes

  • Horde_Prefs
  • Horde_Prefs_Cache_Base
  • Horde_Prefs_Cache_Null
  • Horde_Prefs_Cache_Session
  • Horde_Prefs_CategoryManager
  • Horde_Prefs_Exception
  • Horde_Prefs_Identity
  • Horde_Prefs_Scope
  • Horde_Prefs_Storage_Base
  • Horde_Prefs_Storage_File
  • Horde_Prefs_Storage_Imsp
  • Horde_Prefs_Storage_KolabImap
  • Horde_Prefs_Storage_Ldap
  • Horde_Prefs_Storage_Null
  • Horde_Prefs_Storage_Sql
  • Horde_Prefs_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Preferences storage implementation using files in a directory
  4:  *
  5:  * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Thomas Jarosch <thomas.jarosch@intra2net.com>
 11:  * @author   Michael Slusarz <slusarz@horde.org>
 12:  * @category Horde
 13:  * @package  Prefs
 14:  */
 15: class Horde_Prefs_Storage_File extends Horde_Prefs_Storage_Base
 16: {
 17:     /* Current version number of the data format */
 18:     const VERSION = 2;
 19: 
 20:     /**
 21:      * Cached unserialized data of all scopes.
 22:      *
 23:      * @var array
 24:      */
 25:     protected $_fileCache = null;
 26: 
 27:     /**
 28:      * Full path to the current preference file.
 29:      *
 30:      * @var string
 31:      */
 32:     protected $_fullpath;
 33: 
 34:     /**
 35:      * Constructor.
 36:      *
 37:      * @param string $user   The username.
 38:      * @param array $params  Configuration parameters:
 39:      * <pre>
 40:      * 'directory' - (string) [REQUIRED] Preference storage directory.
 41:      * </pre>
 42:      *
 43:      * @throws InvalidArgumentException
 44:      */
 45:     public function __construct($user, array $params = array())
 46:     {
 47:         // Sanity check for directory
 48:         if (empty($params['directory']) || !is_dir($params['directory'])) {
 49:             throw new InvalidArgumentException('Preference storage directory is not available.');
 50:         }
 51:         if (!is_writable($params['directory'])) {
 52:             throw new InvalidArgumentException(sprintf('Directory %s is not writeable.', $params['directory']));
 53:         }
 54: 
 55:         parent::__construct($user, $params);
 56: 
 57:         $this->_fullpath = $this->_params['directory'] . '/' . basename($this->_params['user']) . '.prefs';
 58:     }
 59: 
 60:     /**
 61:      * Retrieves the requested preferences scope from the storage backend.
 62:      *
 63:      * @param Horde_Prefs_Scope $scope_ob  The scope object.
 64:      *
 65:      * @return Horde_Prefs_Scope  The modified scope object.
 66:      * @throws Horde_Prefs_Exception
 67:      */
 68:     public function get($scope_ob)
 69:     {
 70:         if ($this->_loadFileCache() &&
 71:             isset($this->_fileCache[$scope_ob->scope])) {
 72:             foreach ($this->_fileCache[$scope_ob->scope] as $name => $val) {
 73:                 $scope_ob->set($name, $val);
 74:             }
 75:         }
 76: 
 77:         return $scope_ob;
 78:     }
 79: 
 80:     /**
 81:      * Load the preferences from the files.
 82:      *
 83:      * @return boolean  True on success.
 84:      * @throws Horde_Prefs_Exception
 85:      */
 86:     protected function _loadFileCache()
 87:     {
 88:         if (is_null($this->_fileCache)) {
 89:             // Try to read
 90:             if (!file_exists($this->_fullpath)) {
 91:                 $this->_fileCache = array(
 92:                     '__file_version' => self::VERSION
 93:                 );
 94:                 return false;
 95:             }
 96: 
 97:             $this->_fileCache = @unserialize(file_get_contents($this->_fullpath));
 98: 
 99:             // Check version number. We can call format transformations hooks
100:             // in the future.
101:             if (!is_array($this->_fileCache) ||
102:                 !array_key_exists('__file_version', $this->_fileCache) ||
103:                 !($this->_fileCache['__file_version'] == self::VERSION)) {
104:                 if ($this->_fileCache['__file_version'] == 1) {
105:                     $this->updateFileFormat();
106:                 } else {
107:                     throw new Horde_Prefs_Exception(sprintf('Wrong version number found: %s (should be %d)', $this->_fileCache['__file_version'], self::VERSION));
108:                 }
109:             }
110:         }
111: 
112:         return true;
113:     }
114: 
115:     /**
116:      * Stores changed preferences in the storage backend.
117:      *
118:      * @param Horde_Prefs_Scope $scope_ob  The scope object.
119:      *
120:      * @throws Horde_Prefs_Exception
121:      */
122:     public function store($scope_ob)
123:     {
124:         $this->_loadFileCache();
125: 
126:         /* Driver has no support for storing locked status. */
127:         foreach ($scope_ob->getDirty() as $name) {
128:             $value = $scope_ob->get($name);
129:             if (is_null($value)) {
130:                 unset($this->_fileCache[$scope_ob->scope][$name]);
131:             } else {
132:                 $this->_fileCache[$scope_ob->scope][$name] = $value;
133:             }
134:         }
135: 
136:         $tmp_file = Horde_Util::getTempFile('PrefsFile', true, $this->_params['directory']);
137: 
138:         if ((file_put_contents($tmp_file, serialize($this->_fileCache)) === false) ||
139:             (@rename($tmp_file, $this->_fullpath) === false)) {
140:             throw new Horde_Prefs_Exception(sprintf('Write of preferences to %s failed', $this->_fullpath));
141:         }
142:     }
143: 
144:     /**
145:      * Removes preferences from the backend.
146:      *
147:      * @param string $scope  The scope of the prefs to clear. If null, clears
148:      *                       all scopes.
149:      * @param string $pref   The pref to clear. If null, clears the entire
150:      *                       scope.
151:      *
152:      * @throws Horde_Db_Exception
153:      */
154:     public function remove($scope = null, $pref = null)
155:     {
156:         // TODO
157:     }
158: 
159:     /* Helper functions. */
160: 
161:     /**
162:      * Updates format of file.
163:      */
164:     public function updateFileFormat()
165:     {
166:         $new_vers = array('__file_version' => self::VERSION);
167:         unset($this->_fileCache['__file_version']);
168: 
169:         foreach ($this->_fileCache as $scope => $prefs) {
170:             foreach ($prefs as $name => $pref) {
171:                 $new_vers[$scope][$name] = $pref['v'];
172:             }
173:         }
174: 
175:         $this->_fileCache = $new_vers;
176:     }
177: 
178:     /**
179:      * Lists all available scopes.
180:      *
181:      * @since Horde_Prefs 1.1.0
182:      *
183:      * @return array The list of scopes stored in the backend.
184:      */
185:     public function listScopes()
186:     {
187:         $this->_loadFileCache();
188:         return array_diff(
189:             array_keys($this->_fileCache), array('__file_version')
190:         );
191:     }
192: }
193: 
API documentation generated by ApiGen