1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Prefs_Storage_File extends Horde_Prefs_Storage_Base
16: {
17:
18: const VERSION = 2;
19:
20: 21: 22: 23: 24:
25: protected $_fileCache = null;
26:
27: 28: 29: 30: 31:
32: protected $_fullpath;
33:
34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
45: public function __construct($user, array $params = array())
46: {
47:
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: 62: 63: 64: 65: 66: 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: 82: 83: 84: 85:
86: protected function _loadFileCache()
87: {
88: if (is_null($this->_fileCache)) {
89:
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:
100:
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: 117: 118: 119: 120: 121:
122: public function store($scope_ob)
123: {
124: $this->_loadFileCache();
125:
126:
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: 146: 147: 148: 149: 150: 151: 152: 153:
154: public function remove($scope = null, $pref = null)
155: {
156:
157: }
158:
159:
160:
161: 162: 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: 180: 181: 182: 183: 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: