| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * This class provides an interface to all identities a user might have. Its 4 * methods take care of any site-specific restrictions configured in prefs.php 5 * and conf.php. 6 * 7 * $Horde: framework/Prefs/Identity.php,v 1.1.2.2 2006/06/28 20:52:53 jan Exp $ 8 * 9 * Copyright 2001-2006 Jan Schneider <jan@horde.org> 10 * 11 * See the enclosed file COPYING for license information (LGPL). If you 12 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 13 * 14 * @author Jan Schneider <jan@horde.org> 15 * @since Horde 1.3.5 16 * @package Horde_Identity 17 */ 18 class Identity { 19 20 /** 21 * Array containing all the user's identities. 22 * 23 * @var array 24 */ 25 var $_identities = array(); 26 27 /** 28 * A pointer to the user's standard identity. 29 * This one is used by the methods returning values if no other one is 30 * specified. 31 * 32 * @var integer 33 */ 34 var $_default = 0; 35 36 /** 37 * The user whose identities these are. 38 * 39 * @var string 40 */ 41 var $_user = null; 42 43 /** 44 * Array containing all of the properties in this identity. 45 * 46 * @var array 47 */ 48 var $_properties = array('id', 'fullname', 'from_addr'); 49 50 /** 51 * Reference to the prefs object that this Identity points to. 52 * 53 * @var Prefs 54 */ 55 var $_prefs; 56 57 /** 58 * Reads all the user's identities from the prefs object or builds a new 59 * identity from the standard values given in prefs.php. 60 * 61 * @param string $user If specified, we read another user's identities 62 * instead of the current user. 63 */ 64 function Identity($user = null) 65 { 66 $this->_user = $user; 67 if ((is_null($user) || $user == Auth::getAuth()) && 68 isset($GLOBALS['prefs'])) { 69 $this->_prefs = &$GLOBALS['prefs']; 70 } else { 71 $this->_prefs = &Prefs::singleton($GLOBALS['conf']['prefs']['driver'], 72 $GLOBALS['registry']->getApp(), 73 $user, '', null, false); 74 $this->_prefs->retrieve(); 75 } 76 77 if (!($this->_identities = @unserialize($this->_prefs->getValue('identities', false)))) { 78 /* Convert identities from the old format. */ 79 $this->_identities = unserialize($this->_prefs->getValue('identities')); 80 } elseif (is_array($this->_identities)) { 81 $this->_identities = $this->_prefs->convertFromDriver($this->_identities, NLS::getCharset()); 82 } 83 84 $this->setDefault($this->_prefs->getValue('default_identity')); 85 } 86 87 /** 88 * Creates a default identity if none exists yet and sets the preferences 89 * up if the identities are locked. 90 */ 91 function init() 92 { 93 if (!is_array($this->_identities) || (count($this->_identities) <= 0)) { 94 foreach ($this->_properties as $key) { 95 $identity[$key] = $this->_prefs->getValue($key); 96 } 97 if (empty($identity['id'])) { 98 $identity['id'] = _("Default Identity"); 99 } 100 101 $this->_identities = array($identity); 102 $this->verify(0); 103 } 104 105 if ($this->_prefs->isLocked('default_identity')) { 106 foreach ($this->_properties as $key) { 107 $value = $this->getValue($key); 108 if (is_array($value)) { 109 $value = implode("\n", $value); 110 } 111 $this->_prefs->setValue($key, $value); 112 $this->_prefs->setDirty($key, false); 113 } 114 } 115 } 116 117 /** 118 * Saves all identities in the prefs backend. 119 */ 120 function save() 121 { 122 $identities = $this->_identities; 123 if (is_array($identities)) { 124 $identities = $this->_prefs->convertToDriver($identities, NLS::getCharset()); 125 } 126 127 $this->_prefs->setValue('identities', serialize($identities), false); 128 $this->_prefs->setValue('default_identity', $this->_default); 129 } 130 131 /** 132 * Adds a new empty identity to the array of identities. 133 * 134 * @return integer The pointer to the created identity 135 */ 136 function add() 137 { 138 $this->_identities[] = array(); 139 return count($this->_identities) - 1; 140 } 141 142 /** 143 * Removes an identity from the array of identities. 144 * 145 * @param integer $identity The pointer to the identity to be removed 146 * 147 * @return array The removed identity 148 */ 149 function delete($identity) 150 { 151 $deleted = array_splice($this->_identities, $identity, 1); 152 foreach ($this->_identities as $id => $null) { 153 if ($this->setDefault($id)) { 154 break; 155 } 156 } 157 $this->save(); 158 return $deleted; 159 } 160 161 /** 162 * Returns a pointer to the current default identity. 163 * 164 * @return integer The pointer to the current default identity 165 */ 166 function getDefault() 167 { 168 return $this->_default; 169 } 170 171 /** 172 * Sets the current default identity. 173 * If the identity doesn't exist, the old default identity stays the same. 174 * 175 * @param integer $identity The pointer to the new default identity 176 * 177 * @return boolean True on success, false on failure 178 */ 179 function setDefault($identity) 180 { 181 if (isset($this->_identities[$identity])) { 182 $this->_default = $identity; 183 return true; 184 } else { 185 return false; 186 } 187 } 188 189 /** 190 * Returns a property from one of the identities. If this value doesn't 191 * exist or is locked, the property is retrieved from the prefs backend. 192 * 193 * @param string $key The property to retrieve. 194 * @param integer $identity The identity to retrieve the property from. 195 * 196 * @return mixed The value of the property. 197 */ 198 function getValue($key, $identity = null) 199 { 200 if (!isset($identity) || !isset($this->_identities[$identity])) { 201 $identity = $this->_default; 202 } 203 204 if (!isset($this->_identities[$identity][$key]) || $this->_prefs->isLocked($key)) { 205 $val = $this->_prefs->getValue($key); 206 } else { 207 $val = $this->_identities[$identity][$key]; 208 } 209 210 return $val; 211 } 212 213 /** 214 * Returns an array with the specified property from all existing 215 * identities. 216 * 217 * @param string $key The property to retrieve. 218 * 219 * @return array The array with the values from all identities 220 */ 221 function getAll($key) 222 { 223 $list = array(); 224 foreach ($this->_identities as $identity => $null) { 225 $list[$identity] = $this->getValue($key, $identity); 226 } 227 return $list; 228 } 229 230 /** 231 * Sets a property with a specified value. 232 * 233 * @param string $key The property to set 234 * @param mixed $val The value to which the property should be set 235 * @param integer $identity The identity to set the property in. 236 * 237 * @return boolean True on success, false on failure (property was locked) 238 */ 239 function setValue($key, $val, $identity = null) 240 { 241 if (!isset($identity)) { 242 $identity = $this->_default; 243 } 244 245 if (!$this->_prefs->isLocked($key)) { 246 $this->_identities[$identity][$key] = $val; 247 return true; 248 } else { 249 return false; 250 } 251 } 252 253 /** 254 * Returns true if all properties are locked and therefore nothing in the 255 * identities can be changed. 256 * 257 * @return boolean True if all properties are locked, false otherwise 258 */ 259 function isLocked() 260 { 261 foreach ($this->_properties as $key) { 262 if (!$this->_prefs->isLocked($key)) { 263 return false; 264 } 265 } 266 267 return true; 268 } 269 270 /** 271 * Returns true if the given address belongs to one of the identities. 272 * 273 * @param string $key The identity key to search. 274 * @param string $value The value to search for in $key. 275 * 276 * @return boolean True if the $value was found in $key. 277 */ 278 function hasValue($key, $valueA) 279 { 280 $list = $this->getAll($key); 281 foreach ($list as $valueB) { 282 if (strpos(String::lower($valueA), String::lower($valueB)) !== false) { 283 return true; 284 } 285 } 286 return false; 287 } 288 289 /** 290 * Verifies and sanitizes all identity properties. 291 * 292 * @param integer $identity The identity to verify. 293 * 294 * @return bool|object True if the properties are valid or a PEAR_Error 295 * with an error description otherwise. 296 */ 297 function verify($identity = null) 298 { 299 if (!isset($identity)) { 300 $identity = $this->_default; 301 } 302 303 if (!$this->getValue('id', $identity)) { 304 $this->setValue('id', _("Unnamed"), $identity); 305 } 306 307 /* RFC 2822 [3.2.5] does not allow the '\' character to be used in the 308 * personal portion of an e-mail string. */ 309 if (strpos($this->getValue('fullname', $identity), '\\') !== false) { 310 return PEAR::raiseError(_("You cannot have the '\\' character in your full name.")); 311 } 312 313 return true; 314 } 315 316 /** 317 * Generates the from address to use for the default identity. 318 * 319 * @param boolean $fullname Include the fullname information. 320 * 321 * @return string The default from address. 322 */ 323 function getDefaultFromAddress($fullname = false) 324 { 325 $from_addr = ''; 326 327 if ($fullname) { 328 $name = $this->getValue('fullname'); 329 if (!empty($name)) { 330 $from_addr = $name . ' '; 331 } 332 } 333 334 $addr = $this->getValue('from_addr'); 335 if (empty($addr)) { 336 $addr = Auth::getAuth(); 337 } 338 339 return $from_addr . '<' . $addr . '>'; 340 } 341 342 /** 343 * Attempts to return a concrete Identity instance based on $type. 344 * 345 * @param mixed $type The type of concrete Identity subclass to return. 346 * This is based on the storage driver ($type). The 347 * code is dynamically included. If $type is an array, 348 * then we will look in $type[0]/lib/Identity/ for 349 * the subclass implementation named $type[1].php. 350 * @param string $user If specified, we read another user's identities 351 * instead of the current user. 352 * 353 * @return Identity The newly created concrete Identity instance, or false 354 * on an error. 355 */ 356 function &factory($type = 'none', $user = null) 357 { 358 if (is_array($type)) { 359 $app = $type[0]; 360 $type = $type[1]; 361 } 362 363 /* Return a base Identity object if no driver is specified. */ 364 $type = basename($type); 365 if (empty($type) || (strcmp($type, 'none') == 0)) { 366 $instance = &new Identity($user); 367 $instance->init(); 368 return $instance; 369 } 370 371 if (!empty($app)) { 372 require_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/Identity/' . $type . '.php'; 373 } elseif (@file_exists(dirname(__FILE__) . '/Identity/' . $type . '.php')) { 374 require_once dirname(__FILE__) . '/Identity/' . $type . '.php'; 375 } else { 376 @include_once 'Horde/Identity/' . $type . '.php'; 377 } 378 $class = 'Identity_' . $type; 379 if (class_exists($class)) { 380 $instance = &new $class($user); 381 } else { 382 return PEAR::raiseError('Class definition of ' . $class . ' not found.'); 383 } 384 385 $instance->init(); 386 return $instance; 387 } 388 389 /** 390 * Attempts to return a reference to a concrete Identity instance based on 391 * $type. It will only create a new instance if no Identity instance with 392 * the same parameters currently exists. 393 * 394 * This should be used if multiple types of identities (and, thus, 395 * multiple Identity instances) are required. 396 * 397 * This method must be invoked as: $var = &Identity::singleton() 398 * 399 * @param mixed $type The type of concrete Identity subclass to return. 400 * This is based on the storage driver ($type). The 401 * code is dynamically included. If $type is an array, 402 * then we will look in $type[0]/lib/Identity/ for 403 * the subclass implementation named $type[1].php. 404 * @param string $user If specified, we read another user's identities 405 * instead of the current user. 406 * 407 * @return Identity The concrete Identity reference, or false on an error. 408 */ 409 function &singleton($type = 'none', $user = null) 410 { 411 static $instances; 412 if (!isset($instances)) { 413 $instances = array(); 414 } 415 416 $signature = serialize(array($type, $user)); 417 if (!isset($instances[$signature])) { 418 $instances[$signature] = &Identity::factory($type, $user); 419 } 420 421 return $instances[$signature]; 422 } 423 424 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |