| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 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. * |
| 10 | * * |
| 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. * |
| 15 | * * |
| 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 * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class User extends PlUser |
| 23 | { |
| 24 | private $_profile_fetched = false; |
| 25 | private $_profile = null; |
| 26 | |
| 27 | // Implementation of the login to uid method. |
| 28 | protected function getLogin($login) |
| 29 | { |
| 30 | global $globals; |
| 31 | |
| 32 | if ($login instanceof Profile) { |
| 33 | $this->_profile = $login; |
| 34 | $this->_profile_fetched = true; |
| 35 | $res = XDB::query('SELECT ap.uid |
| 36 | FROM account_profiles AS ap |
| 37 | WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)', |
| 38 | $login->id()); |
| 39 | if ($res->numRows()) { |
| 40 | return $res->fetchOneCell(); |
| 41 | } |
| 42 | throw new UserNotFoundException(); |
| 43 | } |
| 44 | |
| 45 | // If $data is an integer, fetches directly the result. |
| 46 | if (is_numeric($login)) { |
| 47 | $res = XDB::query('SELECT a.uid |
| 48 | FROM accounts AS a |
| 49 | WHERE a.uid = {?}', $login); |
| 50 | if ($res->numRows()) { |
| 51 | return $res->fetchOneCell(); |
| 52 | } |
| 53 | |
| 54 | throw new UserNotFoundException(); |
| 55 | } |
| 56 | |
| 57 | // Checks whether $login is a valid hruid or not. |
| 58 | $res = XDB::query('SELECT a.uid |
| 59 | FROM accounts AS a |
| 60 | WHERE a.hruid = {?}', $login); |
| 61 | if ($res->numRows()) { |
| 62 | return $res->fetchOneCell(); |
| 63 | } |
| 64 | |
| 65 | // From now, $login can only by an email alias, or an email redirection. |
| 66 | // If it doesn't look like a valid address, appends the plat/al's main domain. |
| 67 | $login = trim(strtolower($login)); |
| 68 | if (strstr($login, '@') === false) { |
| 69 | $login = $login . '@' . $globals->mail->domain; |
| 70 | } |
| 71 | |
| 72 | // Checks if $login is a valid alias on the main domains. |
| 73 | list($mbox, $fqdn) = explode('@', $login); |
| 74 | if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) { |
| 75 | $res = XDB::query('SELECT a.uid |
| 76 | FROM accounts AS a |
| 77 | INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\')) |
| 78 | WHERE al.alias = {?}', $mbox); |
| 79 | if ($res->numRows()) { |
| 80 | return $res->fetchOneCell(); |
| 81 | } |
| 82 | |
| 83 | /** TODO: implements this by inspecting the profile. |
| 84 | if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { |
| 85 | $res = XDB::query('SELECT a.uid |
| 86 | FROM accounts AS a |
| 87 | INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie')) |
| 88 | WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]); |
| 89 | if ($res->numRows() == 1) { |
| 90 | return $res->fetchOneCell(); |
| 91 | } |
| 92 | }*/ |
| 93 | |
| 94 | throw new UserNotFoundException(); |
| 95 | } |
| 96 | |
| 97 | // Looks for $login as an email alias from the dedicated alias domain. |
| 98 | if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { |
| 99 | $res = XDB::query("SELECT redirect |
| 100 | FROM virtual_redirect |
| 101 | INNER JOIN virtual USING(vid) |
| 102 | WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); |
| 103 | if ($redir = $res->fetchOneCell()) { |
| 104 | // We now have a valid alias, which has to be translated to an hruid. |
| 105 | list($alias, $alias_fqdn) = explode('@', $redir); |
| 106 | $res = XDB::query("SELECT a.uid |
| 107 | FROM accounts AS a |
| 108 | LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie')) |
| 109 | WHERE al.alias = {?}", $alias); |
| 110 | if ($res->numRows()) { |
| 111 | return $res->fetchOneCell(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | throw new UserNotFoundException(); |
| 116 | } |
| 117 | |
| 118 | // Otherwise, we do suppose $login is an email redirection. |
| 119 | $res = XDB::query("SELECT a.uid |
| 120 | FROM accounts AS a |
| 121 | LEFT JOIN emails AS e ON (e.uid = a.uid) |
| 122 | WHERE e.email = {?}", $login); |
| 123 | if ($res->numRows() == 1) { |
| 124 | return $res->fetchOneCell(); |
| 125 | } |
| 126 | |
| 127 | throw new UserNotFoundException($res->fetchColumn(1)); |
| 128 | } |
| 129 | |
| 130 | // Implementation of the data loader. |
| 131 | protected function loadMainFields() |
| 132 | { |
| 133 | if ($this->hruid !== null && $this->forlife !== null |
| 134 | && $this->bestalias !== null && $this->display_name !== null |
| 135 | && $this->full_name !== null && $this->promo !== null && $this->perms !== null |
| 136 | && $this->gender !== null && $this->email_format !== null) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | global $globals; |
| 141 | /** TODO: promo stuff again */ |
| 142 | /** TODO: fix perms field to fit new perms system */ |
| 143 | $res = XDB::query("SELECT a.hruid, d.promo_display AS promo, |
| 144 | CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife, |
| 145 | CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias, |
| 146 | a.full_name, a.display_name, a.sex = 'female' AS gender, |
| 147 | a.email_format, a.password, |
| 148 | IF (a.state = 'active', at.perms, '') AS perms, |
| 149 | a.is_admin |
| 150 | FROM accounts AS a |
| 151 | INNER JOIN account_types AS at ON (at.type = a.type) |
| 152 | INNER JOIN profile_display AS d ON (d.uid = a.uid) |
| 153 | LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = 'a_vie') |
| 154 | LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags)) |
| 155 | WHERE a.uid = {?}", $this->user_id); |
| 156 | $this->fillFromArray($res->fetchOneAssoc()); |
| 157 | } |
| 158 | |
| 159 | // Specialization of the fillFromArray method, to implement hacks to enable |
| 160 | // lazy loading of user's main properties from the session. |
| 161 | // TODO(vzanotti): remove the conversion hacks once the old codebase will |
| 162 | // stop being used actively. |
| 163 | protected function fillFromArray(array $values) |
| 164 | { |
| 165 | // It might happen that the 'user_id' field is called uid in some places |
| 166 | // (eg. in sessions), so we hard link uid to user_id to prevent useless |
| 167 | // SQL requests. |
| 168 | if (!isset($values['user_id']) && isset($values['uid'])) { |
| 169 | $values['user_id'] = $values['uid']; |
| 170 | } |
| 171 | |
| 172 | // Also, if display_name and full_name are not known, but the user's |
| 173 | // surname and last name are, we can construct the former two. |
| 174 | if (isset($values['prenom']) && isset($values['nom'])) { |
| 175 | if (!isset($values['display_name'])) { |
| 176 | $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']); |
| 177 | } |
| 178 | if (!isset($values['full_name'])) { |
| 179 | $values['full_name'] = $values['prenom'] . ' ' . $values['nom']; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // We also need to convert the gender (usually named "femme"), and the |
| 184 | // email format parameter (valued "texte" instead of "text"). |
| 185 | if (isset($values['femme'])) { |
| 186 | $values['gender'] = (bool) $values['femme']; |
| 187 | } |
| 188 | if (isset($values['mail_fmt'])) { |
| 189 | $values['email_format'] = $values['mail_fmt']; |
| 190 | } |
| 191 | |
| 192 | parent::fillFromArray($values); |
| 193 | } |
| 194 | |
| 195 | // Specialization of the buildPerms method |
| 196 | // This function build 'generic' permissions for the user. It does not take |
| 197 | // into account page specific permissions (e.g X.net group permissions) |
| 198 | protected function buildPerms() |
| 199 | { |
| 200 | if (!is_null($this->perm_flags)) { |
| 201 | return; |
| 202 | } |
| 203 | if ($this->perms === null) { |
| 204 | $this->loadMainFields(); |
| 205 | } |
| 206 | $this->perm_flags = self::makePerms($this->perms, $this->is_admin); |
| 207 | } |
| 208 | |
| 209 | /** Return the main profile attached with this account if any. |
| 210 | */ |
| 211 | public function profile() |
| 212 | { |
| 213 | if (!$this->_profile_fetched) { |
| 214 | $this->_profile_fetched = true; |
| 215 | $this->_profile = Profile::get($this); |
| 216 | } |
| 217 | return $this->_profile; |
| 218 | } |
| 219 | |
| 220 | /** Return true if the user has an associated profile. |
| 221 | */ |
| 222 | public function hasProfile() |
| 223 | { |
| 224 | return !is_null($this->profile()); |
| 225 | } |
| 226 | |
| 227 | /** Get the email alias of the user. |
| 228 | */ |
| 229 | public function emailAlias() |
| 230 | { |
| 231 | global $globals; |
| 232 | return XDB::fetchOneCell("SELECT v.alias |
| 233 | FROM virtual AS v |
| 234 | INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid) |
| 235 | WHERE (vr.redirect = {?} OR vr.redirect = {?}) |
| 236 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
| 237 | $this->forlifeEmail(), $this->m4xForlifeEmail(), $this->id()); |
| 238 | } |
| 239 | |
| 240 | /** Get the alternative forlife email |
| 241 | * TODO: remove this uber-ugly hack. The issue is that you need to remove |
| 242 | * all @m4x.org addresses in virtual_redirect first. |
| 243 | * XXX: This is juste to make code more readable, to be remove as soon as possible |
| 244 | */ |
| 245 | public function m4xForlifeEmail() |
| 246 | { |
| 247 | global $globals; |
| 248 | trigger_error('USING M4X FORLIFE', E_USER_NOTICE); |
| 249 | return $this->login() . '@' . $globals->mail->domain2; |
| 250 | } |
| 251 | |
| 252 | // Return permission flags for a given permission level. |
| 253 | public static function makePerms($perms, $is_admin) |
| 254 | { |
| 255 | $flags = new PlFlagSet($perms); |
| 256 | $flags->addFlag(PERMS_USER); |
| 257 | if ($is_admin) { |
| 258 | $flags->addFlag(PERMS_ADMIN); |
| 259 | } |
| 260 | return $flags; |
| 261 | } |
| 262 | |
| 263 | // Implementation of the default user callback. |
| 264 | public static function _default_user_callback($login, $results) |
| 265 | { |
| 266 | $result_count = count($results); |
| 267 | if ($result_count == 0 || !S::has_perms()) { |
| 268 | Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); |
| 269 | } else { |
| 270 | Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Implementation of the static email locality checker. |
| 275 | public static function isForeignEmailAddress($email) |
| 276 | { |
| 277 | global $globals; |
| 278 | if (strpos($email, '@') === false) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | list($user, $dom) = explode('@', $email); |
| 283 | return $dom != $globals->mail->domain && |
| 284 | $dom != $globals->mail->domain2 && |
| 285 | $dom != $globals->mail->alias_dom && |
| 286 | $dom != $globals->mail->alias_dom2; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 291 | ?> |