/usr/share/php/Horde/Prefs/Storage/KolabImap.php is in php-horde-prefs 2.5.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | <?php
/**
* Preferences storage implementation for a Kolab IMAP server.
*
* Copyright 2007-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Gunnar Wrobel <p@rdus.de>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Prefs
*/
class Horde_Prefs_Storage_KolabImap extends Horde_Prefs_Storage_Base
{
/**
* Handle for the current Kolab connection.
*
* @var Horde_Kolab_Storage
*/
protected $_kolab;
/**
* Name of the preferences default folder
*
* @var string
*/
protected $_folder;
/**
* Log handler.
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* Constructor.
*
* @param string $user The username.
* @param array $params Configuration parameters.
* <pre>
* 'kolab' - (Horde_Kolab_Storage) [REQUIRED] The storage backend.
* 'folder' - (string) The default name of the preferences folder.
* DEFAULT: _('Preferences')
* </pre>
*
* @throws InvalidArgumentException
*/
public function __construct($user, array $params = array())
{
if (!isset($params['kolab'])) {
throw new InvalidArgumentException('Missing "kolab" parameter.');
}
$this->_kolab = $params['kolab'];
unset($params['kolab']);
if (isset($params['logger'])) {
$this->_logger = $params['logger'];
}
if (isset($params['folder'])) {
$this->_folder = $params['folder'];
} else {
$this->_folder = Horde_Prefs_Translation::t("Preferences");
}
parent::__construct($user, $params);
}
/**
* Retrieves the requested preferences scope from the storage backend.
*
* @param Horde_Prefs_Scope $scope_ob The scope object.
*
* @return Horde_Prefs_Scope The modified scope object.
* @throws Horde_Prefs_Exception
*/
public function get($scope_ob)
{
try {
$data = $this->_getStorage();
} catch (Horde_Prefs_Exception $e) {
$this->_logMissingStorage($e);
return $scope_ob;
}
/** This may not fail (or if it does it is okay to pass the error up */
$query = $data->getQuery(Horde_Kolab_Storage_Data::QUERY_PREFS);
try {
$pref = $query->getApplicationPreferences($scope_ob->scope);
} catch (Horde_Kolab_Storage_Exception $e) {
$this->_logMissingScope($e, $scope_ob->scope);
return $scope_ob;
}
foreach ($this->_prefToArray($pref['pref']) as $key => $value) {
$scope_ob->set($key, $value);
}
return $scope_ob;
}
/**
* Stores changed preferences in the storage backend.
*
* @param Horde_Prefs_Scope $scope_ob The scope object.
*
* @throws Horde_Prefs_Exception
*/
public function store($scope_ob)
{
/** This *must* succeed */
$data = $this->_getStorage(true);
$query = $data->getQuery(Horde_Kolab_Storage_Data::QUERY_PREFS);
try {
$pref = $query->getApplicationPreferences($scope_ob->scope);
} catch (Horde_Kolab_Storage_Exception $e) {
$pref = array('application' => $scope_ob->scope);
}
if (isset($pref['pref'])) {
$new = $this->_prefToArray($pref['pref']);
} else {
$new = array();
}
foreach ($scope_ob->getDirty() as $name) {
$new[$name] = $scope_ob->get($name);
}
$pref['pref'] = $this->_arrayToPref($new);
try {
if (!isset($pref['uid'])) {
$data->create($pref);
} else {
$data->modify($pref);
}
} catch (Horde_Kolab_Storage_Exception $e) {
throw new Horde_Prefs_Exception($e);
}
}
/**
* Removes preferences from the backend.
*
* @param string $scope The scope of the prefs to clear. If null, clears
* all scopes.
* @param string $pref The pref to clear. If null, clears the entire
* scope.
*
* @throws Horde_Prefs_Exception
*/
public function remove($scope = null, $pref = null)
{
try {
$data = $this->_getStorage();
} catch (Horde_Prefs_Exception $e) {
$this->_logMissingStorage($e);
return;
}
if ($scope === null) {
$data->deleteAll();
return;
}
$query = $data->getQuery(Horde_Kolab_Storage_Data::QUERY_PREFS);
try {
$pref = $query->getApplicationPreferences($scope);
} catch (Horde_Kolab_Storage_Exception $e) {
$this->_logMissingScope($e, $scope);
return;
}
if ($pref === null) {
$data->delete($pref['uid']);
return;
}
$new = $this->_prefToArray($pref);
unset($new[$pref]);
$pref['pref'] = $this->_arrayToPref($new);
try {
$data->modify($pref);
} catch (Horde_Kolab_Storage_Exception $e) {
throw new Horde_Prefs_Exception($e);
}
}
/**
* Lists all available scopes.
*
* @return array The list of scopes stored in the backend.
*/
public function listScopes()
{
try {
$data = $this->_getStorage();
} catch (Horde_Prefs_Exception $e) {
$this->_logMissingStorage($e);
return;
}
return $data->getQuery(Horde_Kolab_Storage_Data::QUERY_PREFS)
->getApplications();
}
/* Helper functions. */
/**
* Opens a connection to the Kolab server.
*
* @param boolean $create_missing Create a preferences folder if it is
* missing.
*
* @return Horde_Kolab_Storage_Data The storage backend.
*
* @throws Horde_Prefs_Exception
*/
protected function _getStorage($create_missing = false)
{
$query = $this->_kolab->getList()->getQuery();
if ($folder = $query->getDefault('h-prefs')) {
return $this->_kolab->getData($folder);
}
$folders = $query->listByType('h-prefs');
if (!empty($folders)) {
return $this->_kolab->getData($folders[0]);
}
if (!$create_missing) {
throw new Horde_Prefs_Exception(
'No Kolab storage backend available.'
);
}
$params = $this->getParams();
$folder = $this->_kolab->getList()
->getNamespace()
->constructFolderName($params['user'], $this->_folder);
$this->_kolab->getList()->getListManipulation()->createFolder($folder, 'h-prefs.default');
if ($this->_logger !== null) {
$this->_logger->info(
sprintf(
__CLASS__ . ': Created default Kolab preferences folder "%s".',
$this->_folder
)
);
}
return $this->_kolab->getData($folder);
}
/**
* Convert Kolab preferences data to an array.
*
* @param array $pref The preferences list.
*
* @return array The preferences data as array.
*/
private function _prefToArray($pref)
{
$result = array();
foreach ($pref as $prefstr) {
/** If the string doesn't contain a colon delimiter, skip it. */
if (strpos($prefstr, ':') !== false) {
/** Split the string into its name:value components. */
list($name, $val) = explode(':', $prefstr, 2);
$result[$name] = base64_decode($val);
}
}
return $result;
}
/**
* Convert a key => value list of preferences to the Kolab preferences.
*
* @param array $pref The preferences.
*
* @return array The preferences data as list.
*/
private function _arrayToPref($pref)
{
$result = array();
foreach ($pref as $name => $value) {
if ($value !== null) {
$result[] = $name . ':' . base64_encode($value);
}
}
return $result;
}
/**
* Log the missing backend.
*
* @param Exception $e The exception that occurred.
*
* @return NULL
*/
private function _logMissingStorage(Exception $e)
{
if ($this->_logger !== null) {
$this->_logger->debug(
sprintf(
__CLASS__ . ': Failed retrieving Kolab preferences data storage (%s)',
$e->getMessage()
)
);
}
}
/**
* Log the missing scope.
*
* @param Exception $e The exception that occurred.
* @param string $scope The scope that was attempted to get.
*
* @return NULL
*/
private function _logMissingScope(Exception $e, $scope)
{
if ($this->_logger !== null) {
$this->_logger->debug(
sprintf(
__CLASS__ . ': No preference information available for scope %s (%s).',
$scope,
$e->getMessage()
)
);
}
}
}
|