3e4418a93a88bd7124a9c2aa734986f1d233938b
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
23 * PlUserNotFound is raised when a user id cannot be linked to a real user.
24 * The @p results give the list hruids (useful when several users are found).
26 class UserNotFoundException
extends Exception
28 public function __construct($results = array())
30 $this->results
= $results;
31 parent
::__construct();
36 * Represents an user of plat/al (without any further assumption), with a
37 * special focus on always-used properties (identification fields, display name,
38 * forlife/bestalias emails, ...).
39 * NOTE: each implementation of plat/al-code MUST subclass PlUser, and name it
45 * User data enumerations.
47 const GENDER_FEMALE
= true
;
48 const GENDER_MALE
= false
;
49 const FORMAT_HTML
= "html";
50 const FORMAT_TEXT
= "text";
54 * By convention, null means the information hasn't been fetched yet, and
55 * false means the information is not available.
58 // uid is internal user ID (potentially numeric), whereas hruid is a
59 // "human readable" unique ID
60 protected $uid = null
;
61 protected $hruid = null
;
63 // User main email aliases (forlife is the for-life email address, bestalias
64 // is user-chosen preferred email address, email might be any email available
66 protected $forlife = null
;
67 protected $bestalias = null
;
68 protected $email = null
;
70 // Display name is user-chosen name to display (eg. in "Welcome
71 // <display name> !"), while full name is the official full name.
72 protected $display_name = null
;
73 protected $full_name = null
;
75 // Other important parameters used when sending emails.
76 protected $gender = null
; // Acceptable values are GENDER_MALE and GENDER_FEMALE
77 protected $email_format = null
; // Acceptable values are FORMAT_HTML and FORMAT_TEXT
80 protected $perms = null
;
81 protected $perm_flags = null
;
83 // Other properties are listed in this key-value hash map.
84 protected $data = array();
87 * Constructs the PlUser object from an identifier (any identifier which is
88 * understood by getLogin() implementation).
90 * @param $login An user login.
91 * @param $values List of known user properties.
93 public function __construct($login, $values = array())
95 $this->fillFromArray($values);
97 // If the user id was not part of the known values, determines it from
100 $this->uid
= $this->getLogin($login);
103 // Preloads main properties (assumes the loader will lazily get them
104 // from variables already set in the object).
105 $this->loadMainFields();
109 * Get the canonical user id for the @p login.
111 * @param $login An user login.
112 * @return The canonical user id.
113 * @throws UserNotFoundException when login is not found.
115 abstract protected function getLogin($login);
118 * Loads the main properties (hruid, forlife, bestalias, ...) from the
119 * database. Should return immediately when the properties are already
122 abstract protected function loadMainFields();
125 * Accessors to the main properties, ie. those available as top level
133 public function login()
138 public function bestEmail()
140 if (!empty($this->bestalias
)) {
141 return $this->bestalias
;
145 public function forlifeEmail()
147 if (!empty($this->forlife
)) {
148 return $this->forlife
;
153 public function displayName()
155 return $this->display_name
;
157 public function fullName()
159 return $this->full_name
;
162 abstract public function password();
164 // Fallback value is GENDER_MALE.
165 public function isFemale()
167 return $this->gender
== self
::GENDER_FEMALE
;
170 // Fallback value is FORMAT_TEXT.
171 public function isEmailFormatHtml()
173 return $this->email_format
== self
::FORMAT_HTML
;
177 * Other properties are available directly through the $data array, or as
178 * standard object variables, using a getter.
180 public function data()
185 public function __get($name)
187 if (property_exists($this, $name)) {
191 if (isset($this->data
[$name])) {
192 return $this->data
[$name];
198 public function __isset($name)
200 return property_exists($this, $name) ||
isset($this->data
[$name]);
203 public function __unset($name)
205 if (property_exists($this, $name)) {
208 unset($this->data
[$name]);
213 * Fills the object properties using the @p associative array; the intended
214 * user case is to fill the object using SQL obtained arrays.
216 * @param $values Key-value array of user properties.
218 protected function fillFromArray(array $values)
220 // Merge main properties with existing ones.
221 unset($values['data']);
222 foreach ($values as $key => $value) {
223 if (property_exists($this, $key) && !isset($this->$key)) {
224 $this->$key = $value;
228 // Merge all value into the $this->data placeholder.
229 $this->data
= array_merge($this->data
, $values);
233 * Adds properties to the object; this method does not allow the caller to
234 * update core properties (id, ...).
236 * @param $values An associative array of non-core properties.
238 public function addProperties(array $values)
240 foreach ($values as $key => $value) {
241 if (!property_exists($this, $key)) {
242 $this->data
[$key] = $value;
249 * Build the permissions flags for the user.
251 abstract protected function buildPerms();
254 * Check wether the user got the given permission combination.
256 public function checkPerms($perms)
258 if (is_null($this->perm_flags
)) {
261 if (is_null($this->perm_flags
)) {
264 return $this->perm_flags
->hasFlagCombination($perms);
269 * Returns a valid User object built from the @p id and optionnal @p values,
270 * or returns false and calls the callback if the @p id is not valid.
272 public static function get($login, $callback = false
)
274 return User
::getWithValues($login, array(), $callback);
277 public static function getWithValues($login, $values, $callback = false
)
280 $callback = array('User', '_default_user_callback');
284 return new User($login, $values);
285 } catch (UserNotFoundException
$e) {
286 return call_user_func($callback, $login, $e->results
);
290 public static function getWithUID($uid, $callback = false
)
292 return User
::getWithValues(null
, array('uid' => $uid), $callback);
295 // Same as above, but using the silent callback as default.
296 public static function getSilent($login)
298 return User
::getWithValues($login, array(), array('User', '_silent_user_callback'));
301 public static function getSilentWithValues($login, $values)
303 return User
::getWithValues($login, $values, array('User', '_silent_user_callback'));
306 public static function getSilentWithUID($uid)
308 return User
::getWithValues(null
, array('uid' => $uid), array('User', '_silent_user_callback'));
312 * Retrieves User objects corresponding to the @p logins, and eventually
313 * extracts and returns the @p property. If @p strict mode is disabled, it
314 * also includes logins for which no forlife was found (but it still calls
315 * the callback for them).
316 * In all cases, email addresses which are not from the local domains are
319 * @param $logins Array of user logins.
320 * @param $property Property to retrieve from the User objects.
321 * @param $strict Should unvalidated logins be returned as-is or discarded ?
322 * @param $callback Callback to call when a login is unknown to the system.
323 * @return Array of validated user forlife emails.
325 private static function getBulkUserProperties($logins, $property, $strict, $callback)
327 if (!is_array($logins)) {
328 if (strlen(trim($logins)) == 0) {
331 $logins = split("[; ,\r\n\|]+", $logins);
336 foreach ($logins as $i => $login) {
337 $login = trim($login);
342 if (($user = User
::get($login, $callback))) {
343 $list[$i] = $user->$property();
344 } else if (!$strict ||
(User
::isForeignEmailAddress($login) && isvalid_email($login))) {
354 * Returns hruid corresponding to the @p logins. See getBulkUserProperties()
357 public static function getBulkHruid($logins, $callback = false
)
359 return self
::getBulkUserProperties($logins, 'login', true
, $callback);
363 * Returns forlife emails corresponding to the @p logins. See
364 * getBulkUserProperties() for details.
366 public static function getBulkForlifeEmails($logins, $strict = true
, $callback = false
)
368 return self
::getBulkUserProperties($logins, 'forlifeEmail', $strict, $callback);
372 * Predefined callbacks for the user lookup; they are called when a given
373 * login is found not to be associated with any valid user. Silent callback
374 * does nothing; default callback is supposed to display an error message,
375 * using the Platal::page() hook.
377 public static function _silent_user_callback($login, $results)
382 abstract public static function _default_user_callback($login, $results);
385 * Determines if the @p login is an email address, and an email address not
386 * served locally by plat/al.
388 abstract public static function isForeignEmailAddress($email);
390 private static function stripBadChars($text)
392 return str_replace(array(' ', "'"), array('-', ''),
393 strtolower(stripslashes(replace_accent(trim($text)))));
396 /** Creates a username from a first and last name
397 * @param $firstname User's firstname
398 * @param $lasttname User's lastname
399 * return STRING the corresponding username
401 public static function makeUserName($firstname, $lastname)
403 return self
::stripBadChars($firstname) . '.' . self
::stripBadChars($lastname);
407 * Creates a user forlive identifier from:
408 * @param $firstname User's firstname
409 * @param $lasttname User's lastname
410 * @param $category User's promotion or type of account
412 public static function makeHrid($firstname, $lastname, $category)
414 $cat = self
::stripBadChars($category);
416 Platal
::page()->kill("$category is not a suitable category.");
419 return self
::makeUserName($firstname, $lastname) . '.' . $cat;
422 /** Reformats the firstname so that all letters are in lower case,
423 * except the first letter of each part of the name.
425 public static function fixFirstnameCase($firstname)
427 $firstname = strtolower($firstname);
428 $pieces = explode('-', $firstname);
430 foreach ($pieces as $piece) {
431 $subpieces = explode("'", $piece);
434 foreach ($subpieces as $subpiece) {
435 $usubpieces[] = ucwords($subpiece);
437 $upieces[] = implode("'", $usubpieces);
439 return implode('-', $upieces);
443 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: