utilisateur.inc.php 85.4 KB
Newer Older
Ayolo's avatar
Ayolo committed
1
<?php
Feu's avatar
Feu committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Copyright 2005,2006,2007
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

Ayolo's avatar
Ayolo committed
23
24
25
26
27
28
29
/** @file Gestion des utilisateurs
 *
 *
 */
require_once("carteae.inc.php");


BenC's avatar
Typo    
BenC committed
30
$GLOBALS["utbm_roles"] = array("etu"=>"Étudiant", "adm"=>"Personnel administratif", "ens"=>"Enseignant", "per"=>"Personnel", "doc"=>"Doctorant","anc"=>"Ancien étudiant","srv"=>"Service");
Sarah's avatar
Sarah committed
31
$GLOBALS["utbm_departements"] = array("tc"=>"TC", "gi"=>"GI", "imap"=>"IMAP", "gesc"=>"GESC", "mc"=>"MC", "edim"=>"EDIM", "huma"=>"Humanités", "na"=>"N/A");
Ayolo's avatar
Ayolo committed
32
33

/**
34
 * Classe permetant la gestion d'un utilisateur
Ayolo's avatar
Ayolo committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 */
class utilisateur extends stdentity
{

  /** Tableau associatif regroupant les groupes dont l'utilisateur est membre.
   * Associe les id des groupes à leur nom.
   * @see load_groups
   */
  var $groupes;

  /** Tableau associatif des paramètres utilisateur
   * @see load_params
   */
  var $params;

50
51
  /** Type d'utilisateur
   * "std" Utilisateur normal
52
   * "srv" Personne morale, un service de l'UTBM.
53
   */
54
  var $type;
Ayolo's avatar
Ayolo committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  var $nom;
  var $prenom;
  var $email;
  var $pass;
  var $hash;
  var $sexe;
  var $date_naissance;
  var $addresse;
  var $id_ville;
  var $id_pays;
  var $tel_maison;
  var $tel_portable;
  var $alias;

  var $utbm;
  var $etudiant;
  var $ancien_etudiant;
  var $ae;
mbriand's avatar
mbriand committed
73
74
  var $assidu;
  var $amicale;
mbriand's avatar
mbriand committed
75
76
  var $crous;
  var $cotisant;
Ayolo's avatar
Ayolo committed
77
78
79
  var $modere;

  var $droit_image;
Ayolo's avatar
bleh    
Ayolo committed
80
  var $id_licence_default_sas;
Ayolo's avatar
Ayolo committed
81
82
83
84
85
  var $montant_compte;
  var $site_web;

  var $date_maj;
  var $derniere_visite;
Feu's avatar
Feu committed
86

Ayolo's avatar
Ayolo committed
87
88
  /** Profil visible de tous, recherchable dans le mmt online */
  var $publique;
Feu's avatar
Feu committed
89

Ayolo's avatar
Ayolo committed
90
91
  /** Publication autorisée dans le matmatronch papier */
  var $publique_mmtpapier;
Feu's avatar
Feu committed
92

Ayolo's avatar
Ayolo committed
93
  var $tovalid;
Feu's avatar
Feu committed
94

Ayolo's avatar
Ayolo committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  var $signature;
  var $tout_lu_avant;


  /* etudiant */
  var $citation;
  var $adresse_parents;
  var $id_ville_parents;
  var $id_pays_parents;
  var $tel_parents;
  var $nom_ecole_etudiant;

  /* utbm */
  var $role;
  var $departement;
  var $email_utbm;
Feu's avatar
Feu committed
111

Ayolo's avatar
Ayolo committed
112
113
114
115
116
  /* utbm (si etudiant ou ancien etudiant) */
  var $semestre;
  var $filiere;
  var $surnom;
  var $promo_utbm;
Feu's avatar
Feu committed
117
  var $date_diplome_utbm;
Ayolo's avatar
Ayolo committed
118

119
120
121
122
123
124
  /* extra */
  var $musicien;
  var $taille_tshirt;
  var $permis_conduire;
  var $date_permis_conduire;
  var $hab_elect;
Ayolo's avatar
bleh    
Ayolo committed
125
  var $afps;
Ayolo's avatar
Ayolo committed
126
  var $sst;
BenC's avatar
BenC committed
127
  var $jabber;
Ayolo's avatar
Ayolo committed
128
129


130
  var $_grps;
Ayolo's avatar
Ayolo committed
131
132
  var $vol;

133
134
135
  // Permet de ne faire qu'une fois le load_extra
  var $extra_loaded;

Ayolo's avatar
Ayolo committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  function utilisateur ( &$db, &$dbrw = null )
  {
    $this->stdentity($db,$dbrw);

    $this->groupes = null;
  }

  function get_display_name()
  {
    return $this->prenom." ".$this->nom;
  }

  /** Charge un utilisateur en fonction de son id
   * En cas d'erreur, l'id est défini à null
   * @param $id id de l'utilisateur
Feu's avatar
Feu committed
151
   * @return true en cas de succès, false sinon
Ayolo's avatar
Ayolo committed
152
153
   */
  function load_by_id ( $id )
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  {
    $req = new requete($this->db, "SELECT * FROM `utilisateurs`
                                   WHERE `id_utilisateur` = '" . mysql_real_escape_string($id) . "'
                                   LIMIT 1");
    if ( $req->lines == 1 )
    {
      $this->_load($req->get_row());
      return true;
    }

    $this->id = null;
    return false;
  }

  function load_all_by_id ( $id )
Ayolo's avatar
Ayolo committed
169
  {
170
    $req = new requete($this->db,
mbriand's avatar
bleh    
mbriand committed
171
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.*, `utilisateurs`.*, ".
mbriand's avatar
bleh    
mbriand committed
172
173
                       "`utl_etu`.`id_ville` AS `id_ville_parents`, ".
                       "`utl_etu`.`id_pays` AS `id_pays_parents` ".
174
175
176
177
178
179
180
                       "FROM utilisateurs ".
                       "LEFT JOIN `utl_etu` ON (`utilisateurs`.`id_utilisateur`=`utl_etu`.`id_utilisateur`) ".
                       "LEFT JOIN `utl_etu_utbm` ON (`utilisateurs`.`id_utilisateur`=`utl_etu_utbm`.`id_utilisateur`) ".
                       "LEFT JOIN `utl_extra` ON (`utilisateurs`.`id_utilisateur`=`utl_extra`.`id_utilisateur`) ".
                       "WHERE ".
                       "`utilisateurs`.`id_utilisateur` = '" . mysql_real_escape_string($id). "' ".
                       "LIMIT 1");
Ayolo's avatar
Ayolo committed
181
182
183

    if ( $req->lines == 1 )
    {
184
      $this->_load_all($req->get_row());
Ayolo's avatar
Ayolo committed
185
186
      return true;
    }
Feu's avatar
Feu committed
187

Ayolo's avatar
Ayolo committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    $this->id = null;
    return false;
  }

  /** Charge un utilisateur en fonction de son adresse email personnelle,
   * ou de son adresse mail utbm.
   * En cas d'erreur, l'id est défini à null
   * @param $email adresse email de l'utilisateur
   */
  function load_by_email ( $email )
  {
    /*if ( intval($email)==$email)
    {
      $this->load_by_id($email);
      return;
    }*/

205
    if (ereg("^([A-Za-z0-9\._-]+)@(utbm\.fr|assidu-utbm\.fr)$", $email, $regs))
206
      $req = new requete($this->db,
207
208
209
210
211
212
        "SELECT `utilisateurs`.* FROM `utilisateurs` " .
        "LEFT JOIN `utl_etu_utbm` ON `utl_etu_utbm`.`id_utilisateur` = `utilisateurs`.`id_utilisateur` " .
        "WHERE `utilisateurs`.`email_utl` = '".mysql_real_escape_string($regs[1]."@utbm.fr")."' " .
        "OR `utilisateurs`.`email_utl` = '".mysql_real_escape_string($regs[1]."@assidu-utbm.fr")."' " .
        "OR `utl_etu_utbm`.`email_utbm` = '".mysql_real_escape_string($regs[1]."@utbm.fr")."' " .
        "OR `utl_etu_utbm`.`email_utbm` = '".mysql_real_escape_string($regs[1]."@assidu-utbm.fr")."' " .
slopez's avatar
debug    
slopez committed
213
        "LIMIT 1");
214
    else
215
      $req = new requete($this->db,
216
217
218
219
        "SELECT `utilisateurs`.* FROM `utilisateurs` " .
        "LEFT JOIN `utl_etu_utbm` ON `utl_etu_utbm`.`id_utilisateur` = `utilisateurs`.`id_utilisateur` " .
        "WHERE `utilisateurs`.`email_utl` = '" . mysql_real_escape_string($email) . "' OR " .
        "`utl_etu_utbm`.`email_utbm` = '" . mysql_real_escape_string($email) . "' " .
slopez's avatar
debug    
slopez committed
220
        "LIMIT 1");
Ayolo's avatar
Ayolo committed
221
222
223
224
225
226

    if ( $req->lines == 1 )
    {
      $this->_load($req->get_row());
      return true;
    }
Feu's avatar
Feu committed
227

Ayolo's avatar
Ayolo committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    $this->id = null;
    return false;
  }

  /** Charge un utilisateur en fonction de son alias
   * En cas d'erreur, l'id est défini à null
   * @param $alias alias de l'utilisateur
   */
  function load_by_alias ( $alias )
  {
    $req = new requete($this->db, "SELECT * FROM `utilisateurs`
                                   WHERE `alias_utl` = '" . mysql_real_escape_string($alias) . "'
                                   LIMIT 1");

    if ( $req->lines == 1 )
    {
      $this->_load($req->get_row());
      return true;
    }
Feu's avatar
Feu committed
247

Ayolo's avatar
Ayolo committed
248
249
250
251
252
253
254
255
256
    $this->id = null;
    return false;

  }
  /**
   * Charge un utilisateur en fonction de son numéro de carte AE.
   * En cas d'erreur, l'id est défini à null
   * @param $num numéro de carte
   */
257
  function load_by_carteae ( $num, $strict=true, $check_expire=true )
Ayolo's avatar
Ayolo committed
258
  {
Feu's avatar
Feu committed
259
    $this->vol = false;
Feu's avatar
Feu committed
260

Ayolo's avatar
bleh    
Ayolo committed
261
262
263
264
265
266
267
268
269
    if ( ereg("^([0-9]+)([a-zA-Z]{1})$", $num, $regs) )
    {
      $cond = "`ae_carte`.`id_carte_ae` = '" . mysql_real_escape_string($regs[1]) . "' AND ".
              "`ae_carte`.`cle_carteae` = '" . strtoupper(mysql_real_escape_string($regs[2])) . "'";
    }
    elseif ( ereg("^([0-9]+) ([a-zA-Z\\-]{1,6})\\.([a-zA-Z\\-]{1,6})$", $num, $regs) )
    {
      $cond = "`ae_carte`.`id_carte_ae` = '" . mysql_real_escape_string($regs[1]) . "'";
    }
Feu's avatar
Feu committed
270
271
    else // voué à disparaitre
    {
Feu's avatar
Feu committed
272
273
274
275
276
      if ( $strict )
      {
        $this->id=null;
        return;
      }
Feu's avatar
Feu committed
277
      $cond = "`ae_carte`.`id_carte_ae` = '" . mysql_real_escape_string(intval($num)) . "'";
Feu's avatar
Feu committed
278
    }
Ayolo's avatar
Ayolo committed
279

280
281
282
    if ($check_expire)
      $cond .= " AND `ae_carte`.`etat_vie_carte_ae`<=".CETAT_EXPIRE;

Ayolo's avatar
Ayolo committed
283
284
285
    $req = new requete($this->db, "SELECT * FROM `utilisateurs` " .
                                  "INNER JOIN `ae_cotisations` ON `ae_cotisations`.`id_utilisateur`=`utilisateurs`.`id_utilisateur` " .
                                  "INNER JOIN `ae_carte` ON `ae_cotisations`.`id_cotisation`=`ae_carte`.`id_cotisation` " .
Feu's avatar
Feu committed
286
                                  "WHERE $cond " .
Ayolo's avatar
Ayolo committed
287
288
289
290
291
292
293
                                  "LIMIT 1");

    if ( $req->lines == 1 )
    {
      $this->_load($req->get_row());
      return true;
    }
Feu's avatar
Feu committed
294

Ayolo's avatar
Ayolo committed
295
    $this->id = null;
Feu's avatar
Feu committed
296

Ayolo's avatar
Ayolo committed
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
    $req = new requete($this->db, "SELECT * FROM `ae_carte`
                                   WHERE `id_carte_ae` = '" . mysql_real_escape_string($id) . "'
                                   AND `etat_vie_carte_ae`>=".CETAT_PERDUE." LIMIT 1");

    if ( $req->lines == 1 )
      $this->vol=true;

    return false;
  }
  /**
   * Charge un utilisateur en fonction de son id de cotisation.
   * En cas d'erreur, l'id est défini à null
   * @param $id_cotisation id de la cotisation
   */
  function load_by_cotisation ( $id_cotisation )
  {
    $req = new requete($this->db, "SELECT * FROM `utilisateurs` " .
                                  "INNER JOIN `ae_cotisations` ON `ae_cotisations`.`id_utilisateur`=`utilisateurs`.`id_utilisateur` " .
                                  "WHERE `ae_cotisations`.`id_cotisation` = '" . mysql_real_escape_string($id_cotisation) . "' " .
                                  "LIMIT 1");

    if ( $req->lines == 1 )
    {
      $this->_load($req->get_row());
      return true;
    }
Feu's avatar
Feu committed
323

Ayolo's avatar
Ayolo committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    $this->id = null;
    return false;
  }

  /** Determine si un alias est disponible.
   * @param $alias Alias à tester (sauf pour l'utilisateur en cours)
   * @return true si disponible, false sinon
   */
  function is_alias_avaible ( $alias )
  {
    $req = new requete($this->db, "SELECT * FROM `utilisateurs` " .
                                  "WHERE `alias_utl` = '" . mysql_real_escape_string($alias) . "' " .
                                  "AND `utilisateurs`.`id_utilisateur`!='".$this->id."'");

    if  ( $req->lines != 0 )
      return false;
Feu's avatar
Feu committed
340

Ayolo's avatar
Ayolo committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
    $req = new requete($this->db, "SELECT * FROM `asso`
                                    WHERE `nom_unix_asso` = '" . mysql_real_escape_string($alias) . "'
                                    LIMIT 1");

    if  ( $req->lines != 0 )
      return false;
    return true;
  }

  /** Determine si une adresse email est disponible
   * @param $email Adresse email à tester (sauf pour l'utilisateur en cours)
   * @return true si disponible, false sinon
   */
  function is_email_avaible ( $email )
  {
Ayolo's avatar
Ayolo committed
356
357
    if(!CheckEmail($email,3))
      return false;
358
    if (ereg("^([A-Za-z0-9\._-]+)@(utbm\.fr|assidu-utbm\.fr)$", $email, $regs))
359
      $req = new requete($this->db,
360
361
362
363
364
365
366
367
        "SELECT `utilisateurs`.* FROM `utilisateurs` " .
        "LEFT JOIN `utl_etu_utbm` ON `utl_etu_utbm`.`id_utilisateur` = `utilisateurs`.`id_utilisateur` " .
        "WHERE (`utilisateurs`.`email_utl` = '".mysql_real_escape_string($regs[1]."@utbm.fr")."' " .
        "OR `utilisateurs`.`email_utl` = '".mysql_real_escape_string($regs[1]."@assidu-utbm.fr")."' " .
        "OR `utl_etu_utbm`.`email_utbm` = '".mysql_real_escape_string($regs[1]."@utbm.fr")."' " .
        "OR `utl_etu_utbm`.`email_utbm` = '".mysql_real_escape_string($regs[1]."@assidu-utbm.fr")."') " .
        "AND `utilisateurs`.`id_utilisateur`!='".$this->id."'");
    else
368
      $req = new requete($this->db,
369
370
371
372
373
        "SELECT `utilisateurs`.* FROM `utilisateurs` " .
        "LEFT JOIN `utl_etu_utbm` ON `utl_etu_utbm`.`id_utilisateur`=`utilisateurs`.`id_utilisateur` " .
        "WHERE (`utilisateurs`.`email_utl`='" . mysql_real_escape_string($email) . "' OR " .
        "`utl_etu_utbm`.`email_utbm`='" . mysql_real_escape_string($email) . "') " .
        "AND `utilisateurs`.`id_utilisateur`!='".$this->id."'");
Ayolo's avatar
Ayolo committed
374
375
376
377
378
379

    return ( $req->lines == 0 );
  }

  function _load ( $row )
  {
380
    $this->extra_loaded = false;
Ayolo's avatar
Ayolo committed
381
    $this->id = $row['id_utilisateur'];
382
    $this->type = $row['type_utl'];
Ayolo's avatar
Ayolo committed
383
384
385
386
387
388
    $this->nom = $row['nom_utl'];
    $this->prenom = $row['prenom_utl'];
    $this->email = $row['email_utl'];
    $this->pass = $row['pass_utl'];
    $this->hash = $row['hash_utl'];
    $this->sexe = $row['sexe_utl'];
mbriand's avatar
mbriand committed
389
    $this->date_naissance = (is_null($row['date_naissance_utl']) ? null : strtotime($row['date_naissance_utl']));
Ayolo's avatar
Ayolo committed
390
391
392
393
394
395
396
397
398
399
    $this->addresse = $row['addresse_utl'];
    $this->id_ville = $row['id_ville'];
    $this->id_pays = $row['id_pays'];
    $this->tel_maison = $row['tel_maison_utl'];
    $this->tel_portable = $row['tel_portable_utl'];
    $this->alias = $row['alias_utl'];
    $this->utbm = $row['utbm_utl'];
    $this->etudiant = $row['etudiant_utl'];
    $this->ancien_etudiant = $row['ancien_etudiant_utl'];
    $this->ae = $row['ae_utl'];
mbriand's avatar
mbriand committed
400
401
    $this->assidu = $row['assidu_utl'];
    $this->amicale = $row['amicale_utl'];
mbriand's avatar
mbriand committed
402
403
    $this->crous = $row['crous_utl'];
    $this->cotisant = $this->ae || $this->assidu || $this->amicale || $this->crous;
Ayolo's avatar
Ayolo committed
404
405
    $this->modere = $row['modere_utl'];
    $this->droit_image = $row['droit_image_utl'];
Ayolo's avatar
bleh    
Ayolo committed
406
    $this->id_licence_default_sas = $row['id_licence_default_sas'];
Ayolo's avatar
Ayolo committed
407
408
409
410
411
    $this->montant_compte = $row['montant_compte'];
    if ( $row['date_maj_utl'] )
      $this->date_maj = strtotime($row['date_maj_utl']);
    else
      $this->date_maj = null;
Feu's avatar
Feu committed
412

Ayolo's avatar
Ayolo committed
413
414
    if ( is_null($row['derniere_visite_utl']) )
      $this->derniere_visite = null;
Feu's avatar
Feu committed
415
    else
Ayolo's avatar
Ayolo committed
416
      $this->derniere_visite = strtotime($row['derniere_visite_utl']);
Feu's avatar
Feu committed
417
418

    $this->publique = $row['publique_utl'];
Ayolo's avatar
Ayolo committed
419
    $this->publique_mmtpapier = $row['publique_mmtpapier_utl'];
Feu's avatar
Feu committed
420
421
422
423
    $this->tovalid = $row['tovalid_utl'];

    $this->signature = $row['signature_utl'];

Ayolo's avatar
Ayolo committed
424
    if ( is_null($row['tout_lu_avant_utl']) )
Feu's avatar
Feu committed
425
      $this->tout_lu_avant = null;
Ayolo's avatar
Ayolo committed
426
    else
Feu's avatar
Feu committed
427
      $this->tout_lu_avant = strtotime($row['tout_lu_avant_utl']);
Ayolo's avatar
Ayolo committed
428
429
  }

430
  function _load_extras($row)
Ayolo's avatar
Ayolo committed
431
  {
432
    $this->extra_loaded = true;
Ayolo's avatar
Ayolo committed
433
434
435
436
    if ( $this->etudiant || $this->ancien_etudiant )
    {
      $this->citation = $row["citation"];
      $this->adresse_parents = $row["adresse_parents"];
mbriand's avatar
bleh    
mbriand committed
437
438
      $this->id_ville_parents = $row["id_ville_parents"];
      $this->id_pays_parents = $row["id_pays_parents"];
Ayolo's avatar
Ayolo committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
      $this->tel_parents = $row["tel_parents"];
      $this->nom_ecole_etudiant = $row["nom_ecole_etudiant"];
    }
    else
    {
      unset($this->adresse_parents);
      unset($this->id_ville_parents);
      unset($this->id_pays_parents);
      unset($this->tel_parents);
      unset($this->nom_ecole_etudiant);
    }

    if ( $this->utbm )
    {
      $this->role = $row["role_utbm"];
      $this->departement = $row["departement_utbm"];
      $this->semestre = $row["semestre_utbm"];
      $this->filiere = $row["filiere_utbm"];
      $this->surnom = $row["surnom_utbm"];
      $this->email_utbm = $row["email_utbm"];
      $this->promo_utbm = $row["promo_utbm"];
      $this->date_diplome_utbm = !is_null($row["date_diplome_utbm"])?strtotime($row["date_diplome_utbm"]):null;
    }
    else
    {
      unset($this->semestre);
      unset($this->role);
      unset($this->departement);
      unset($this->filiere);
      unset($this->surnom);
      unset($this->email_utbm);
      unset($this->promo_utbm);
      unset($this->date_diplome_utbm);
    }
Feu's avatar
Feu committed
473

474
475
476
    $this->musicien = $row["musicien_utl"];
    $this->taille_tshirt = $row["taille_tshirt_utl"];
    $this->permis_conduire = $row["permis_conduire_utl"];
Feu's avatar
Feu committed
477

Feu's avatar
Feu committed
478
    if ( !is_null($row["date_permis_conduire_utl"]) && $this->permis_conduire )
Feu's avatar
fix    
Feu committed
479
      $this->date_permis_conduire = strtotime($row["date_permis_conduire_utl"]);
480
481
    else
      $this->date_permis_conduire = null;
Feu's avatar
Feu committed
482

483
    $this->hab_elect = $row["hab_elect_utl"];
Feu's avatar
Feu committed
484
485
    $this->afps = $row["afps_utl"];
    $this->sst = $row["sst_utl"];
486
    $this->site_web = $row['site_web'];
BenC's avatar
BenC committed
487
    $this->jabber = $row['jabber_utl'];
488
489
490
491
492
493
  }

  function _load_all ( $row )
  {
    $this->_load($row);
    $this->_load_extras($row);
Ayolo's avatar
Ayolo committed
494
  }
Feu's avatar
Feu committed
495
496

  /**
Ayolo's avatar
Ayolo committed
497
498
499
500
501
502
503
504
   */
  function visite ( )
  {
    $req = new update($this->dbrw,
                      "utilisateurs",
                      array("derniere_visite_utl"=>date("Y-m-d H:i:s")),
                      array("id_utilisateur"=>$this->id));
  }
Feu's avatar
Feu committed
505

Ayolo's avatar
Ayolo committed
506
507
508
509
510
511
512
513
514
515
516
517
  /** Active un compte en attente
   */
  function validate ( )
  {
    if ( $this->tovalid == "utbm" )
    {
      $this->utbm = true;
      $req = new update($this->dbrw,
                        "utilisateurs",
                        array("utbm_utl"=>$this->utbm),
                        array("id_utilisateur"=>$this->id));
    }
Feu's avatar
Feu committed
518

Ayolo's avatar
Ayolo committed
519
520
    $this->hash = "valid";
    $this->tovalid = "none";
Feu's avatar
Feu committed
521

Ayolo's avatar
Ayolo committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
    $req = new update($this->dbrw,
                      "utilisateurs",
                      array("hash_utl"=>$this->hash,
                            "tovalid_utl"=>$this->tovalid),
                      array("id_utilisateur"=>$this->id));
  }

  /** Desactive un compte, pour revalidation
   */
  function invalidate ( $reason="email" )
  {
    if ( $reason != $this->tovalid )
    {
      if ( $this->tovalid == "emailutbm" ) // L'etat précédent en attente de validation est annulé
      {
        $this->set_email_utbm ( "", true );
      }
      elseif ( $this->tovalid == "utbm" ) // L'etat précédent en attente de validation est annulé
      {
        $this->utbm = false;
        $req = new update($this->dbrw,
Ayolo's avatar
bleh    
Ayolo committed
543
                          "utilisateurs",
Ayolo's avatar
Ayolo committed
544
545
546
                          array("utbm_utl"=>$this->utbm),
                          array("id_utilisateur"=>$this->id));
        $req = new delete($this->dbrw,
Ayolo's avatar
bleh    
Ayolo committed
547
548
                          "utl_etu_utbm",
                          array("id_utilisateur" => $this->id));
Ayolo's avatar
Ayolo committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
      }
    }
    $this->hash = md5(genere_pass(20));
    $this->tovalid = $reason;
    $req = new update($this->dbrw,
                      "utilisateurs",
                      array("hash_utl"=>$this->hash,
                            "tovalid_utl"=>$this->tovalid),
                      array("id_utilisateur"=>$this->id));
  }

  /** Determine si le mot de passe précisé est le bon
   * @param $password Mot de passe à tester
   * @return true si le mot de passe est correct, false sinon
   */
  function is_password ( $password )
  {
Ayolo's avatar
bleh    
Ayolo committed
566
//    if ($this->pass == crypt($password, substr($this->pass,0,2)))
Ayolo's avatar
bleh    
Ayolo committed
567
    if ($this->pass == crypt($password, "ae"))
Ayolo's avatar
Ayolo committed
568
569
570
571
572
573
574
575
576
      return true;
    return false;
  }

  /** Change le mot de passe de l'utilisateur
   * @param $$new_password Nouveau mot de passe
   */
  function change_password ( $new_password )
  {
Ayolo's avatar
bleh    
Ayolo committed
577
    $this->pass = crypt($new_password, "ae");
Ayolo's avatar
Ayolo committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
    $req = new update($this->dbrw,
                      "utilisateurs",
                      array("pass_utl"=>$this->pass),
                      array("id_utilisateur"=>$this->id));
  }


  /* GROUPS management */
  /** Change les groupes dont l'utilisateur fait parti
   * @see is_in_group
   * @see is_in_group_id
   */
  function load_groups ()
  {
592
    $this->groupes = array();
593

594
    if ( !$this->is_valid() )
595
596
      return;

Ayolo's avatar
Ayolo committed
597
598
599
600
601
602
603
604
605
606
607
608
609
    $req = new requete($this->db,
                       "SELECT `groupe`.`id_groupe`,`groupe`.`nom_groupe`
                        FROM `utl_groupe`
                        INNER JOIN `groupe` ON `utl_groupe`.`id_groupe` = `groupe`.`id_groupe`
                        WHERE `utl_groupe`.`id_utilisateur` = '" . mysql_real_escape_string($this->id) . "'");

    // 1XXXX [flag]
    // 2XXXX [asso]-bureau
    // 3XXXX [asso]-membres

    while ( list($id,$name) = $req->get_row() )
      $this->groupes[$id] = $name;

Feu's avatar
Feu committed
610
    if ( $this->ae )
Ayolo's avatar
Ayolo committed
611
      $this->groupes[10000] = "ae-membres";
Feu's avatar
Feu committed
612
613

    if ( $this->utbm )
Ayolo's avatar
Ayolo committed
614
      $this->groupes[10001] = "utbm";
Feu's avatar
Feu committed
615
616

    if ( $this->ancien_etudiant )
Ayolo's avatar
Ayolo committed
617
      $this->groupes[10002] = "etudiants-anciens";
Feu's avatar
Feu committed
618

619
    if ( $this->etudiant )
Ayolo's avatar
Ayolo committed
620
621
      $this->groupes[10003] = "etudiants-actuels";

Feu's avatar
Feu committed
622
    if ( $this->etudiant && $this->utbm )
Ayolo's avatar
Ayolo committed
623
      $this->groupes[10004] = "etudiants-utbm-actuels";
Feu's avatar
Feu committed
624
625

    if ( $this->ancien_etudiant && $this->utbm )
Ayolo's avatar
Ayolo committed
626
      $this->groupes[10005] = "etudiants-utbm-anciens";
Feu's avatar
Feu committed
627
628

    if ( ( $this->ancien_etudiant || $this->etudiant ) && $this->utbm )
Ayolo's avatar
Ayolo committed
629
630
      $this->groupes[10006] = "etudiants-utbm-tous";

Feu's avatar
Feu committed
631
    if ( $this->ancien_etudiant || $this->etudiant )
Ayolo's avatar
Ayolo committed
632
      $this->groupes[10007] = "etudiants-tous";
Feu's avatar
Feu committed
633
634

    if ( $this->modere )
Ayolo's avatar
Ayolo committed
635
      $this->groupes[10008] = "utilisateurs-valides";
636

mbriand's avatar
mbriand committed
637
638
639
640
641
642
643
644
    // 10009 : voir plus bas

    if ( $this->amicale )
      $this->groupes[10010] = "assidu-membres";

    if ( $this->assidu )
      $this->groupes[10011] = "amicale-membres";

mbriand's avatar
mbriand committed
645
646
647
648
    if ( $this->crous )
      $this->groupes[10013] = "crous-membres";

    if ( $this->cotisant)
mbriand's avatar
mbriand committed
649
650
      $this->groupes[10012] = "cotisants-tous";

651
652
653
654
655
656
657
658
659
    // Verify if our guy was a member at some point
    if ( $this->cotisant )
        $this->groupes[10015] = "cotisants-sympathisants";
    else {
        $req = new requete($this->db, 'SELECT 1 FROM ae_cotisations WHERE id_utilisateur = '.$this->id.' LIMIT 1');
        if ($req->lines > 0)
            $this->groupes[10015] = "cotisants-sympathisants";
    }

660
661
662
663
664
665
666
667
668
669
670
    $req = new requete($this->db,
                       "SELECT `asso`.`id_asso`, ".
                       "`asso`.`nom_unix_asso`, ".
                       "`asso_membre`.`role`, ".
                       "`asso`.`id_asso_parent` " .
                       "FROM `asso_membre` " .
                       "INNER JOIN `asso` ON `asso`.`id_asso`=`asso_membre`.`id_asso` " .
                       "WHERE `asso_membre`.`id_utilisateur`='".$this->id."' " .
                       "AND `asso_membre`.`date_fin` is NULL " .
                       "AND (`asso`.`id_asso_parent` IS NOT NULL OR `asso_membre`.`role` > 1 ) " .
                       "ORDER BY `asso`.`nom_asso`");
Feu's avatar
Feu committed
671

672
673
674
675
    while ( list($id,$name,$role,$parent) = $req->get_row() )
    {
      if ( $role > 1 )
        $this->groupes[$id+20000] = $name."-bureau";
Feu's avatar
Feu committed
676

677
678
      if( !is_null($parent) )
        $this->groupes[$id+30000] = $name."-membres";
jlaval's avatar
jlaval committed
679

680
681
682
      // Si on est dans le bureau de l'AE, si on est président d'un pole (asso fille de AE)
      // ou président asso pôtes (BDS, CETU, BDF) alors on fait partie du groupe CA
      if( ($id == 1 && $role > 1) || (($parent == 1 || $id == 2 || $id == 3 || $id == 51) && $role == ROLEASSO_PRESIDENT))
jlaval's avatar
Test    
jlaval committed
683
          $this->groupes[10014] = "ca-membres";
684
    }
Feu's avatar
Feu committed
685

686
687
688
689
690
691
692
693
694
695
696
    $req = new requete($this->db,
                       "SELECT `id_utilisateur` ".
                       "FROM `asso_membre` ".
                       "INNER JOIN `asso` USING(`id_asso`) ".
                       "WHERE `date_fin` IS NULL and `role`='10' ".
                       "AND `id_utilisateur`='".$this->id."' " .
                       "AND `id_asso_parent` IN (SELECT `id_asso` FROM `asso` WHERE `id_asso_parent`='1')");

    if ( $req->lines > 0 )
      $this->groupes[10009] = "responsables-clubs";

Ayolo's avatar
Ayolo committed
697
698
699
700
701
702
703
    if ( !isset($this->promo_utbm) )
      $this->load_all_extra();

    if ( $this->promo_utbm > 0 )
      $this->groupes[$this->promo_utbm+40000] = "promo".sprintf("%02d",$this->promo_utbm)."-membres";

  }
704

Feu's avatar
Feu committed
705
706
707
  function _update_mailings ( $oldemail, $newemail )
  {
    require_once($topdir."include/entities/asso.inc.php");
708

Feu's avatar
Feu committed
709
710
711
712
713
714
715
716
717
718
    $req = new requete($this->db,
                       "SELECT ".
                       "`asso`.`nom_unix_asso`, ".
                       "`asso_membre`.`role`, ".
                       "`asso`.`id_asso_parent` " .
                       "FROM `asso_membre` " .
                       "INNER JOIN `asso` ON `asso`.`id_asso`=`asso_membre`.`id_asso` " .
                       "WHERE `asso_membre`.`id_utilisateur`='".$this->id."' " .
                       "AND `asso_membre`.`date_fin` is NULL " .
                       "AND (`asso`.`id_asso_parent` IS NOT NULL OR `asso_membre`.`role` > 1 ) " .
Feu's avatar
fix    
Feu committed
719
                       "AND (asso.id_asso=1 OR (id_asso_parent IS NOT NULL AND id_asso_parent!=3)) ".
Feu's avatar
Feu committed
720
                       "ORDER BY `asso`.`nom_unix_asso`");
Feu's avatar
Feu committed
721
722
723
724
725

    while ( list($name,$role,$parent) = $req->get_row() )
    {
      if ( $role > 1 )
      {
Feu's avatar
fix    
Feu committed
726
727
        asso::_ml_unsubscribe($this->dbrw,$name.".bureau",$oldemail);
        asso::_ml_subscribe($this->dbrw,$name.".bureau",$newemail);
Feu's avatar
Feu committed
728
729
730
      }
      if( !is_null($parent) )
      {
Feu's avatar
fix    
Feu committed
731
732
        asso::_ml_unsubscribe($this->dbrw,$name.".membres",$oldemail);
        asso::_ml_subscribe($this->dbrw,$name.".membres",$newemail);
Feu's avatar
Feu committed
733
      }
734
735
    }

Feu's avatar
Feu committed
736
  }
737
738
739



Ayolo's avatar
Ayolo committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
  /** Determine si l'utilisateur est membre du groupe précisé.
   * (Charge automatiquement les groupes)
   * @param $name nom du groupe
   * @return true si l'utilisateur est membre, false sinon
   * @see is_in_group_id
   */
  function is_in_group ( $name )
  {
    if ( is_null($this->groupes) )
      $this->load_groups();

    return in_array($name,$this->groupes);
  }

  /** Determine si l'utilisateur est membre du groupe précisé
   *  (Charge automatiquement les groupes)
   * @param $id id du groupe
   * @return true si l'utilisateur est membre, false sinon
   * @see is_in_group
   */
  function is_in_group_id ( $id )
  {
Ayolo's avatar
bleh    
Ayolo committed
762
    if(is_null($id) || empty($id))
Ayolo's avatar
bleh    
Ayolo committed
763
      return false;
Ayolo's avatar
Ayolo committed
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
    if ( is_null($this->groupes) )
      $this->load_groups();

    return isset($this->groupes[$id]);
  }

  /**
   * Renvoie la liste des id des groupes dont fait parti l'utilisateur séparés par des virgules
   */
  function get_groups_csv ( )
  {
    if ( $this->_grps )
      return $this->_grps;

    if ( is_null($this->groupes) )
      $this->load_groups();

    $this->_grps ="";
    foreach ( $this->groupes as $id => $n )
    {
      if ( $this->_grps ) $this->_grps .= ",";
      $this->_grps .= $id;
    }

    if ( $this->_grps == "" ) // Pour éviter tout un tas de bugs
      $this->_grps ="0";

    return $this->_grps;
  }

794
795
796
797
798
799
800
  /**
   * Renvoie un fragment SQL qui gère les autorisations de groupe de manière
   * permissive en prenant en compte la date de dernière cotisation par rapport
   * à la date passée en paramètre
   */
  function get_grps_authorization_fragment ($date_field, $grps, $id_groupe)
  {
mbriand's avatar
mbriand committed
801
    global $topdir;
mbriand's avatar
mbriand committed
802
    require_once($topdir."include/entities/group.inc.php");
803
804
805
806
807
808
809
810
811
    $fragment = $id_groupe.' IN ('.$grps.')';

    if ($this->ae)
      return $fragment;

    $derniere_cotiz = $this->date_derniere_cotiz_a_lae();
    if (!$derniere_cotiz)
      return $fragment;

jlaval's avatar
jlaval committed
812
813
    if ( is_null($this->groupes) )
      $this->load_groups();
814

mbriand's avatar
mbriand committed
815
    $fragment = "(".$fragment." OR (".$id_groupe." = ".array_search("ae-membres", enumerates_groups($this->db))." AND (".$date_field." <= '".$derniere_cotiz."' OR ".$date_field." IS NULL)))";
mbriand's avatar
mbriand committed
816

mbriand's avatar
mbriand committed
817
    return $fragment;
818
819
  }

Ayolo's avatar
Ayolo committed
820
821
822
823
824
  /* Extra infos management */
  /** Change toutes les informations secondaires de l'utilisateur
   */
  function load_all_extra ()
  {
825
826
827
    if ($this->extra_loaded)
      return;

Ayolo's avatar
Ayolo committed
828
    $req = new requete($this->db,
mbriand's avatar
bleh    
mbriand committed
829
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.*, ".
mbriand's avatar
bleh    
mbriand committed
830
831
                       "`utl_etu`.`id_ville` AS `id_ville_parents`, ".
                       "`utl_etu`.`id_pays` AS `id_pays_parents` ".
832
833
                       "FROM utilisateurs ".
                       "LEFT JOIN `utl_etu` ON (`utilisateurs`.`id_utilisateur`=`utl_etu`.`id_utilisateur`) ".
Feu's avatar
oups    
Feu committed
834
835
                       "LEFT JOIN `utl_etu_utbm` ON (`utilisateurs`.`id_utilisateur`=`utl_etu_utbm`.`id_utilisateur`) ".
                       "LEFT JOIN `utl_extra` ON (`utilisateurs`.`id_utilisateur`=`utl_extra`.`id_utilisateur`) ".
836
837
838
839
840
                       "WHERE ".
                       "`utilisateurs`.`id_utilisateur` = '" . mysql_real_escape_string($this->id). "' ".
                       "LIMIT 1");

    $this->_load_extras($req->get_row());
Ayolo's avatar
Ayolo committed
841
842
843
844
845
846
847
848
849
  }

  /**
   * Sauve les informations de l'utilisateur.
   * Au vu du nombre d'informations, le passage se fait par les variables de l'objet.
   */
  function saveinfos ()
  {
    global $topdir;
850

Feu's avatar
Feu committed
851
852
    if ( empty($this->alias) )
      $this->alias = null;
853

Feu's avatar
Feu committed
854
    require_once($topdir."include/cts/cached.inc.php");
Feu's avatar
Feu committed
855
    $cache = new cachedcontents("sig".$this->id);
Feu's avatar
Feu committed
856
    $cache->expire();
857

Feu's avatar
Feu committed
858
    new update($this->dbrw,
Ayolo's avatar
Ayolo committed
859
860
861
862
                      "utilisateurs",
                      array('nom_utl' => $this->nom,
                            'prenom_utl' => $this->prenom,
                            'sexe_utl' => $this->sexe,
mbriand's avatar
mbriand committed
863
864
                            'date_naissance_utl' => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
865
866
867
868
869
870
871
872
                            'addresse_utl' => $this->addresse,
                            'id_ville' => $this->id_ville,
                            'id_pays' => $this->id_pays,
                            'tel_maison_utl' => $this->tel_maison,
                            'tel_portable_utl' => $this->tel_portable,
                            'alias_utl' => $this->alias,
                            'droit_image_utl' => $this->droit_image==true,
                            'date_maj_utl' => date("Y-m-d H:i:s",$this->date_maj),
Feu's avatar
Feu committed
873
                            'publique_utl'=> $this->publique,
Ayolo's avatar
Ayolo committed
874
875
                            'publique_mmtpapier_utl'=>$this->publique_mmtpapier,
                            'signature_utl' =>$this->signature),
mbriand's avatar
mbriand committed
876
                      array('id_utilisateur' => $this->id));
Ayolo's avatar
Ayolo committed
877

Feu's avatar
Feu committed
878
879
880
881




Ayolo's avatar
Ayolo committed
882
883
    if ( $this->etudiant || $this->ancien_etudiant )
    {
Feu's avatar
Feu committed
884
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
885
886
887
888
889
890
891
892
893
894
895
896
897
                        "utl_etu",
                        array('citation' => $this->citation,
                              'adresse_parents' => $this->adresse_parents,
                              'id_ville' => $this->id_ville_parents,
                              'id_pays' => $this->id_pays_parents,
                              'tel_parents' => $this->tel_parents,
                              'nom_ecole_etudiant' => $this->nom_ecole_etudiant),
                        array('id_utilisateur' => $this->id));

    }

    if ( $this->utbm )
    {
Feu's avatar
Feu committed
898
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
899
900
901
902
903
                        "utl_etu_utbm",
                        array('semestre_utbm' => $this->semestre,
                              'role_utbm' => $this->role,
                              'departement_utbm' => $this->departement,
                              'filiere_utbm' => $this->filiere,
904
                              'surnom_utbm' => (!empty($this->surnom) ? $this->surnom : null),
Ayolo's avatar
Ayolo committed
905
906
907
908
                              'promo_utbm' => $this->promo_utbm,
                              'date_diplome_utbm'=> ($this->date_diplome_utbm!=NULL)?date("Y-m-d H:i:s",$this->date_diplome_utbm):NULL),
                        array( 'id_utilisateur' => $this->id));
    }
Feu's avatar
Feu committed
909

Feu's avatar
Feu committed
910
    $req = new requete($this->db,"SELECT id_utilisateur FROM utl_extra WHERE id_utilisateur='".mysql_real_escape_string($this->id)."'");
911

Feu's avatar
fix    
Feu committed
912
913
    if ( !$this->permis_conduire )
      $this->date_permis_conduire=null;
914

915
916
917
918
919
    if ( $req->lines == 0 )
      new insert($this->dbrw,
                      "utl_extra",
                      array(
                      'id_utilisateur' => $this->id,
920
                      'site_web' => $this->site_web,
921
922
923
                      'musicien_utl'=>$this->musicien,
                      'taille_tshirt_utl'=>$this->taille_tshirt,
                      'permis_conduire_utl'=>$this->permis_conduire,
Feu's avatar
fix    
Feu committed
924
                      'date_permis_conduire_utl'=>is_null($this->date_permis_conduire)?null:date("Y-m-d",$this->date_permis_conduire),
925
926
                      'hab_elect_utl'=>$this->hab_elect,
                      'afps_utl'=>$this->afps,
BenC's avatar
BenC committed
927
                      'sst_utl'=>$this->sst,
BenC's avatar
bleh    
BenC committed
928
                      'jabber_utl'=>$this->jabber));
Feu's avatar
Feu committed
929
930
931
932
    else
      new update($this->dbrw,
                    "utl_extra",
                    array(
933
                    'site_web' => $this->site_web,
Feu's avatar
Feu committed
934
935
936
                    'musicien_utl'=>$this->musicien,
                    'taille_tshirt_utl'=>$this->taille_tshirt,
                    'permis_conduire_utl'=>$this->permis_conduire,
Feu's avatar
fix    
Feu committed
937
                    'date_permis_conduire_utl'=> is_null($this->date_permis_conduire)?null:date("Y-m-d",$this->date_permis_conduire),
Feu's avatar
Feu committed
938
939
                    'hab_elect_utl'=>$this->hab_elect,
                    'afps_utl'=>$this->afps,
BenC's avatar
BenC committed
940
                    'sst_utl'=>$this->sst,
BenC's avatar
bleh    
BenC committed
941
                    'jabber_utl'=>$this->jabber),
Feu's avatar
Feu committed
942
943
944
                    array('id_utilisateur' => $this->id));


Ayolo's avatar
Ayolo committed
945

946
/*    if ( XML_RPC_USE )
Ayolo's avatar
Ayolo committed
947
948
949
950
951
952
953
954
955
956
    {
              require_once($topdir . "include/inscriptions/xmlrpc-client.inc.php");
        $ch = new ClientHelper("mmt", "08084e11");
          $ret = $ch->addUser($this->nom, $this->prenom, $this->email, $this->sexe,
                $this->branche, $this->semestre, date("Y-m-d",$this->date_naissance));

              if ( $ret == FALSE )
          return false;
        else
          return true;
957
    }*/
Ayolo's avatar
Ayolo committed
958
959
960
961
962
963
964
965
966
967
968
969
970
971

    return true;
  }

  /**
   * Transforme l'utilisateur en utilisateur UTBM
   * @param $email_utbm Adresse utbm de l'utilisateur (requise!)
   * @param $admin Précise si la modification a été faite par un administrateur, si c'est le cas le compte ne sera pas invalidé
   */
  function became_utbm ( $email_utbm, $admin=false  )
  {
    // 1- Vérifions que l'adresse email peut donner droit au flag 'utbm'
    if ( !ereg("^([a-zA-Z0-9\.\-]+)@(utbm\.fr|assidu-utbm\.fr)$",$email_utbm) )
      return false;
Feu's avatar
Feu committed
972

Ayolo's avatar
Ayolo committed
973
974
975
976
    // 2- Vérifions qu'elle n'est pas déjà utilisée
    $req = new requete($this->db,
                       "SELECT id_utilisateur ".
                       "FROM `utl_etu_utbm` ".
Feu's avatar
debug    
Feu committed
977
                       "WHERE `email_utbm`='".mysql_real_escape_string($email_utbm)."' ".
Feu's avatar
debug    
Feu committed
978
                       "AND id_utilisateur !='".mysql_real_escape_string($this->id)."' ");
Feu's avatar
Feu committed
979

Ayolo's avatar
Ayolo committed
980
981
    if ( $req->lines > 0 )
      return false;
Feu's avatar
Feu committed
982

Feu's avatar
fixes    
Feu committed
983
984
985
986
    // 3- Vérifions qu'il n'y pas d'entrée pour l'utilisateur
    $req = new requete($this->db,
                       "SELECT id_utilisateur ".
                       "FROM `utl_etu_utbm` ".
Feu's avatar
debug    
Feu committed
987
                       "WHERE `id_utilisateur` = '" . mysql_real_escape_string($this->id) . "'");
Feu's avatar
fixes    
Feu committed
988
989
990
991
992
993
994
995
    if ( $req->lines == 0 )
    {
      // Crée l'entrée dans la table utl_etu_utbm (qui a vocation à devenir utl_utbm)
      $req = new insert($this->dbrw,
                        "utl_etu_utbm",
                        array("id_utilisateur" => $this->id,
                              "email_utbm"=>$email_utbm));
    }
996

Ayolo's avatar
Ayolo committed
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
    // Si c'est un admin qui fait l'opération, on considère que la vérification par email n'est pas requise
    if ( $admin )
    {
      $this->utbm = true;
      $req = new update($this->dbrw,
                        "utilisateurs",
                        array("utbm_utl"=>$this->utbm),
                        array("id_utilisateur"=>$this->id));
      return true;
    }

    // Inavlide le compte, et planifie le gain du flag 'utbm'
    $this->invalidate("utbm");
Feu's avatar
Feu committed
1010

Ayolo's avatar
Ayolo committed
1011
1012
    // Envoie l'email d'activation à l'adresse utbm
    $this->send_activation_email($email_utbm);
Feu's avatar
Feu committed
1013

Ayolo's avatar
Ayolo committed
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
    return true;
  }

  /**
   * Transforme l'utilisateur en étudiant
   * @param $ecole Nom de l'école de l'étudiant (utilisé pour la modération)
   * @param $ancien Précise s'il s'agit d'un ancien étudiant
   * @param $admin Précise si la modification a été faite par un administrateur, si c'est le cas le compte ne sera pas soumi à modération
   */
  function became_etudiant ( $ecole, $ancien=false, $admin=false )
  {
    $this->modere = $admin;
Feu's avatar
Feu committed
1026

Ayolo's avatar
Ayolo committed
1027
1028
    $this->etudiant = !$ancien;
    $this->ancien_etudiant = $ancien;
Feu's avatar
Feu committed
1029

Ayolo's avatar
Ayolo committed
1030
1031
    $req = new requete($this->db,
                       "SELECT id_utilisateur FROM utl_etu WHERE id_utilisateur='".mysql_real_escape_string($this->id)."'");
Feu's avatar
Feu committed
1032

Ayolo's avatar
Ayolo committed
1033
1034
1035
1036
    if ( $req->lines == 0 )
      $req = new insert($this->dbrw,
                        "utl_etu",
                         array("id_utilisateur" => $this->id,"nom_ecole_etudiant"=>$ecole));
Feu's avatar
Feu committed
1037

Ayolo's avatar
Ayolo committed
1038
1039
1040
1041
1042
1043
    $req = new update($this->dbrw,
                      "utilisateurs",
                      array("modere_utl"=> $this->modere,
                            "etudiant_utl"=>$this->etudiant,
                            "ancien_etudiant_utl"=>$this->ancien_etudiant),
                      array("id_utilisateur"=>$this->id));
1044
1045

    // Force le role à "etu" si l'utilisateur est utbm
Feu's avatar
fixes    
Feu committed
1046
1047
    if ( $this->utbm )
    {
1048
      $this->role = "etu";
Feu's avatar
fixes    
Feu committed
1049
1050
1051
      new update($this->dbrw,"utl_etu_utbm",
        array('role_utbm' => $this->role),
        array('id_utilisateur' => $this->id));
1052
    }
Ayolo's avatar
Ayolo committed
1053
  }
1054

Feu's avatar
wip    
Feu committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  function became_notetudiant ( )
  {
    $this->etudiant = 0;
    $this->ancien_etudiant = 0;

    new delete($this->dbrw,
                        "utl_etu",
                         array("id_utilisateur" => $this->id));

    new update($this->dbrw,
                      "utilisateurs",
                      array("etudiant_utl"=>$this->etudiant,
                            "ancien_etudiant_utl"=>$this->ancien_etudiant),
                      array("id_utilisateur"=>$this->id));
  }


Ayolo's avatar
Ayolo committed
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104

  /** Gnration de mot de passe
   * Cette fonction va gnrer une chane alatoire de la longueur
   * spcifie. C'est notamment utile pour gnrer des mots de passe.
   *
   * @param nameLength Longueur de la chane
   *
   * @return La chane alatoire
   */
  function genere_pass ($nameLength=12)
  {
    $NameChars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKMNLOP';
    $Vouel = 'aeiouAEIOU';
    $Name = "";

    for ($index = 1; $index <= $nameLength; $index++)
    {
      if ($index % 3 == 0)
      {
        $randomNumber = rand(1,strlen($Vouel));
        $Name .= substr($Vouel,$randomNumber-1,1);
      }
      else
      {
        $randomNumber = rand(1,strlen($NameChars));
        $Name .= substr($NameChars,$randomNumber-1,1);
      }
    }

  return $Name;

  }

Ayolo's avatar
bleh    
Ayolo committed
1105
1106
1107
  function set_licence_default_sas( $id_licence, $all = false)
  {
    global $topdir;
Ayolo's avatar
bleh    
Ayolo committed
1108
    require_once($topdir."sas2/include/licence.inc.php");
Ayolo's avatar
bleh    
Ayolo committed
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
    $licence=new licence($this->db);
    if($licence->load_by_id($id_licence))
    {
      $this->id_licence_default_sas = $id_licence;
      $req = new update($this->dbrw,
                      "utilisateurs",
                      array('id_licence_default_sas' => $this->id_licence_default_sas),
                      array( 'id_utilisateur' => $this->id));
      if($all)
        $req = new update($this->dbrw,
                          'sas_photos',
                          array('id_licence'=>$this->id_licence_default_sas),
                          array('id_utilisateur_photographe' => $this->id));
    }
  }

Ayolo's avatar
Ayolo committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
  function set_droit_image ( $droit_image )
  {
    $this->droit_image = $droit_image;

    $req = new update($this->dbrw,
                      "utilisateurs",
                      array('droit_image_utl' => $this->droit_image),
                      array( 'id_utilisateur' => $this->id));


    if ( $droit_image )
    {
      $sql = new update ($this->dbrw,
                         "sas_personnes_photos",
                         array("accord_phutl"=> true),
                         array("id_utilisateur"=>$this->id));

      $sql = new requete($this->dbrw,
                         "UPDATE sas_photos SET droits_acquis=1 " .
                         "WHERE droits_acquis=0 AND incomplet=0 AND " .
                         "(SELECT COUNT(*) FROM `sas_personnes_photos` " .
                         "WHERE sas_personnes_photos.`id_photo`=sas_photos.id_photo " .
                         "AND `accord_phutl`='0' " .
                         "AND `modere_phutl`='1')=0");
    }
  }


  function create_user ( $nom,
                         $prenom,
                         $email,
                         $password,
                         $droit_image,
                         $date_naissance,
                         $sexe,
                         $_utbm=false,
Ayolo's avatar
bleh    
Ayolo committed
1161
1162
                         $_etudiant=false,
                         $send_email=true)
Ayolo's avatar
Ayolo committed
1163
  {
1164
    $this->type = "std";
Ayolo's avatar
Ayolo committed
1165
1166
1167
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);
    $this->email = $email;
slopez's avatar
bleh    
slopez committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
    $alias=strtolower($this->prenom{0}.str_replace(' ','',str_replace('-','',$this->nom)));
    if(strlen($alias)>8)
      $alias = substr($alias,0,8);
    $req = new requete($this->db,
                       'SELECT `alias_utl` '.
                       'FROM `utilisateurs` '.
                       'WHERE LOWER(`alias_utl`) LIKE \''.mysql_real_escape_string($alias).'%\' '.
                       'ORDER BY `alias_utl` DESC '.
                       'LIMIT 1');
    if($req->lines==1)
    {
      list($_alias)=$req->get_row();
      if(strlen($_alias)>8)
        $alias.=((int)substr($_alias,-1)+1);
      else
        $alias.=1;
    }
    $this->alias = $alias;
Ayolo's avatar
bleh    
Ayolo committed
1186
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1187
1188
1189
1190
1191
    $this->sexe = $sexe;
    $this->date_naissance = $date_naissance;
    $this->droit_image = $droit_image;

    $this->ae = false;
mbriand's avatar
mbriand committed
1192
1193
    $this->assidu = false;
    $this->amicale = false;
mbriand's avatar
mbriand committed
1194
    $this->crous = false;
Ayolo's avatar
Ayolo committed
1195
1196
1197
1198
    $this->utbm = $_utbm;
    $this->etudiant = $_etudiant;
    $this->ancien_etudiant = false;
    $this->modere = false;
1199
    $this->publique = 2;
Ayolo's avatar
Ayolo committed
1200
1201
1202
1203
    $this->publique_mmtpapier = true;

    $sql = new insert ($this->dbrw,
                       "utilisateurs",
1204
1205
                        array("type_utl"=>$this->type,
                              "nom_utl" => $this->nom,
Ayolo's avatar
Ayolo committed
1206
1207
1208
1209
1210
1211
                              "prenom_utl" => $this->prenom,
                              "email_utl" => $this->email,
                              "alias_utl" => $this->alias,
                              "pass_utl" => $this->pass,
                              "hash_utl" => "",
                              "sexe_utl" => $this->sexe,
mbriand's avatar
mbriand committed
1212
1213
                              "date_naissance_utl" => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
1214
1215
1216
1217
1218
                              "etudiant_utl" => $this->etudiant,
                              "utbm_utl" => $this->utbm,
                              "droit_image_utl" => $this->droit_image,
                              "ancien_etudiant_utl"=> false,
                              "ae_utl"=>false,
mbriand's avatar
mbriand committed
1219
1220
                              "assidu_utl"=>false,
                              "amicale_utl"=>false,
mbriand's avatar
mbriand committed
1221
                              "crous_utl"=>false,
Ayolo's avatar
Ayolo committed
1222
1223
                              "modere_utl"=> $this->modere,
                              "montant_compte"=> 0,
Feu's avatar
Feu committed
1224
                              "publique_utl"=> $this->publique,
Ayolo's avatar
Ayolo committed
1225
                              "publique_mmtpapier_utl"=>$this->publique_mmtpapier,
slopez's avatar
fixed    
slopez committed
1226
                              "tovalid_utl"=>"none"));
Ayolo's avatar
Ayolo committed
1227
1228
1229
1230
1231
1232
1233
1234

    if ( $sql )
      $this->id = $sql->get_id();
    else
    {
      $this->id = null;
      return false;
    }
Feu's avatar
Feu committed
1235

Ayolo's avatar
bleh    
Ayolo committed
1236
1237
1238
1239
1240
    if($send_email)
    {
      $this->invalidate ("email");
      $this->send_first_email($this->email,$password);
    }
1241
1242
1243
    /* on ajoute le nouvel utilisateur au traitement du cache fsearch */
    fsearch_revalidate_cache_for ($this->prenom);
    fsearch_revalidate_cache_for ($this->nom);
Feu's avatar
Feu committed
1244

Ayolo's avatar
Ayolo committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
    return true;
  }

  function create_etudiant_user ( $nom,
                                  $prenom,
                                  $email,
                                  $password,
                                  $droit_image,
                                  $date_naissance,
                                  $sexe,
                                  $ecole,
                                  $_utbm=false)
  {
Feu's avatar
Feu committed
1258

Ayolo's avatar
Ayolo committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
    if ( !$this->create_user ( $nom,
                               $prenom,
                               $email,
                               $password,
                               $droit_image,
                               $date_naissance,
                               $sexe,
                               $_utbm,
                               true))
      return false;
Feu's avatar
Feu committed
1269

Ayolo's avatar
Ayolo committed
1270
1271
1272
1273
    if ( $_utbm )
      $this->nom_ecole_etudiant = "utbm";
    else
      $this->nom_ecole_etudiant = $ecole;
Feu's avatar
Feu committed
1274

Ayolo's avatar
Ayolo committed
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
    $req = new insert($this->dbrw,
                      "utl_etu",
                      array("id_utilisateur" => $this->id,
                            "nom_ecole_etudiant" => $this->nom_ecole_etudiant));
    return true;
  }

  function create_utbm_user ( $nom,
                              $prenom,
                              $emailutbm,
                              $password,
                              $droit_image,
                              $date_naissance,
                              $sexe,
                              $role,
                              $departement)
  {
    if ( $role == "etu" )
    {
      if ( !$this->create_etudiant_user ( $nom,
                                          $prenom,
                                          $emailutbm,
                                          $password,
                                          $droit_image,
                                          $date_naissance,
                                          $sexe,
                                          "utbm",
                                          true))
        return false;
    }
    elseif ( !$this->create_user ( $nom,
                                   $prenom,
                                   $emailutbm,
                                   $password,
                                   $droit_image,
                                   $date_naissance,
                                   $sexe,
                                   true,
                                   false))
      return false;
Feu's avatar
Feu committed
1315

Ayolo's avatar
Ayolo committed
1316
1317
1318
    $this->role = $role;
    $this->departement = $departement;
    $this->email_utbm = $emailutbm;
Feu's avatar
Feu committed
1319

Ayolo's avatar
Ayolo committed
1320
1321
1322
1323
1324
1325
    $req = new insert($this->dbrw,
                      "utl_etu_utbm",
                      array("id_utilisateur" => $this->id,
                            "role_utbm" => $this->role,
                            "departement_utbm" => $this->departement,
                            "email_utbm" => $this->email_utbm));
Feu's avatar
Feu committed
1326

Ayolo's avatar
Ayolo committed
1327
1328
1329
1330
1331
1332
1333
1334
    return true;
  }

  /**
   * Inscription par un admin (uniquement)
   * @todo Fonction à revoir complètement (aussi bien usage dans le site que implémentation)
   * @deprecated
   */
1335
  function new_utbm_user ( $nom, $prenom, $email, $emailutbm, &$password, $semestre, $branche, $promo, $etudiant, $droit_image, $nom_ecole, $date_naissance = null , $sexe = 1)
Ayolo's avatar
Ayolo committed
1336
  {
1337
    $this->type="std";
1338

Ayolo's avatar
Ayolo committed
1339
1340
1341
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);

Feu's avatar
Feu committed
1342
1343
    if (CheckEmail($emailutbm,1))
      $this->email_utbm = $emailutbm;
Ayolo's avatar
Ayolo committed
1344

Feu's avatar
Feu committed
1345
1346
1347
    $this->utbm = true;

    $this->email = $email;
Ayolo's avatar
Ayolo committed
1348

slopez's avatar
bleh    
slopez committed
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
    $alias=strtolower($this->prenom{0}.str_replace(' ','',str_replace('-','',$this->nom)));
    if(strlen($alias)>8)
      $alias = substr($alias,0,8);
    $req = new requete($this->db,
                       'SELECT `alias_utl` '.
                       'FROM `utilisateurs` '.
                       'WHERE LOWER(`alias_utl`) LIKE \''.mysql_real_escape_string($alias).'%\' '.
                       'ORDER BY `alias_utl` DESC '.
                       'LIMIT 1');
    if($req->lines==1)
    {
      list($_alias)=$req->get_row();
      if(strlen($_alias)>8)
        $alias.=((int)substr($_alias,-1)+1);
      else
        $alias.=1;
    }
    $this->alias = $alias;
Ayolo's avatar
Ayolo committed
1367
1368
1369

    if (!$password)
      $password = genere_pass(7);
Feu's avatar
Feu committed
1370

Ayolo's avatar
bleh    
Ayolo committed
1371
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1372
1373
1374
1375
1376
1377
1378
    $this->etudiant = $etudiant==true;

    $this->modere = true;
    $this->sexe = $sexe;
    if ($date_naissance)
      $this->date_naissance = $date_naissance;

1379
    $this->publique = 2;
Ayolo's avatar
Ayolo committed
1380
    $this->publique_mmtpapier = true;
Feu's avatar
Feu committed
1381

Ayolo's avatar
Ayolo committed
1382
1383
1384
1385
    $this->droit_image = $droit_image;

    $sql = new insert ($this->dbrw,
                       "utilisateurs",
1386
1387
                       array("type_utl"=>$this->type,
                             "nom_utl" => $this->nom,
Ayolo's avatar
Ayolo committed
1388
1389
1390
1391
1392
1393
                             "prenom_utl" => $this->prenom,
                             "email_utl" => $this->email,
                             "alias_utl" => $this->alias,
                             "pass_utl" => $this->pass,