utilisateur.inc.php 83.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  var $vol;

  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
148
   * @return true en cas de succès, false sinon
Ayolo's avatar
Ayolo committed
149
150
   */
  function load_by_id ( $id )
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  {
    $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
166
  {
167
    $req = new requete($this->db,
mbriand's avatar
bleh    
mbriand committed
168
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.*, `utilisateurs`.*, ".
mbriand's avatar
bleh    
mbriand committed
169
170
                       "`utl_etu`.`id_ville` AS `id_ville_parents`, ".
                       "`utl_etu`.`id_pays` AS `id_pays_parents` ".
171
172
173
174
175
176
177
                       "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
178
179
180

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

Ayolo's avatar
Ayolo committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    $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;
    }*/

202
    if (ereg("^([A-Za-z0-9\._-]+)@(utbm\.fr|assidu-utbm\.fr)$", $email, $regs))
203
      $req = new requete($this->db,
204
205
206
207
208
209
        "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
210
        "LIMIT 1");
211
    else
212
      $req = new requete($this->db,
213
214
215
216
        "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
217
        "LIMIT 1");
Ayolo's avatar
Ayolo committed
218
219
220
221
222
223

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

Ayolo's avatar
Ayolo committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    $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
244

Ayolo's avatar
Ayolo committed
245
246
247
248
249
250
251
252
253
    $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
   */
254
  function load_by_carteae ( $num, $strict=true )
Ayolo's avatar
Ayolo committed
255
  {
Feu's avatar
Feu committed
256
    $this->vol = false;
Feu's avatar
Feu committed
257

Ayolo's avatar
bleh    
Ayolo committed
258
259
260
261
262
263
264
265
266
    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
267
268
    else // voué à disparaitre
    {
Feu's avatar
Feu committed
269
270
271
272
273
      if ( $strict )
      {
        $this->id=null;
        return;
      }
Feu's avatar
Feu committed
274
      $cond = "`ae_carte`.`id_carte_ae` = '" . mysql_real_escape_string(intval($num)) . "'";
Feu's avatar
Feu committed
275
    }
Ayolo's avatar
Ayolo committed
276
277
278
279

    $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
280
                                  "WHERE $cond " .
Ayolo's avatar
Ayolo committed
281
282
283
284
285
286
287
288
                                  "AND `ae_carte`.`etat_vie_carte_ae`<=".CETAT_EXPIRE." " .
                                  "LIMIT 1");

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

Ayolo's avatar
Ayolo committed
290
    $this->id = null;
Feu's avatar
Feu committed
291

Ayolo's avatar
Ayolo committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    $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
318

Ayolo's avatar
Ayolo committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    $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
335

Ayolo's avatar
Ayolo committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    $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
351
352
    if(!CheckEmail($email,3))
      return false;
353
    if (ereg("^([A-Za-z0-9\._-]+)@(utbm\.fr|assidu-utbm\.fr)$", $email, $regs))
354
      $req = new requete($this->db,
355
356
357
358
359
360
361
362
        "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
363
      $req = new requete($this->db,
364
365
366
367
368
        "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
369
370
371
372
373
374
375

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

  function _load ( $row )
  {
    $this->id = $row['id_utilisateur'];
376
    $this->type = $row['type_utl'];
Ayolo's avatar
Ayolo committed
377
378
379
380
381
382
    $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
383
    $this->date_naissance = (is_null($row['date_naissance_utl']) ? null : strtotime($row['date_naissance_utl']));
Ayolo's avatar
Ayolo committed
384
385
386
387
388
389
390
391
392
393
    $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
394
395
    $this->assidu = $row['assidu_utl'];
    $this->amicale = $row['amicale_utl'];
mbriand's avatar
mbriand committed
396
397
    $this->crous = $row['crous_utl'];
    $this->cotisant = $this->ae || $this->assidu || $this->amicale || $this->crous;
Ayolo's avatar
Ayolo committed
398
399
    $this->modere = $row['modere_utl'];
    $this->droit_image = $row['droit_image_utl'];
Ayolo's avatar
bleh    
Ayolo committed
400
    $this->id_licence_default_sas = $row['id_licence_default_sas'];
Ayolo's avatar
Ayolo committed
401
402
403
404
405
    $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
406

Ayolo's avatar
Ayolo committed
407
408
    if ( is_null($row['derniere_visite_utl']) )
      $this->derniere_visite = null;
Feu's avatar
Feu committed
409
    else
Ayolo's avatar
Ayolo committed
410
      $this->derniere_visite = strtotime($row['derniere_visite_utl']);
Feu's avatar
Feu committed
411
412

    $this->publique = $row['publique_utl'];
Ayolo's avatar
Ayolo committed
413
    $this->publique_mmtpapier = $row['publique_mmtpapier_utl'];
Feu's avatar
Feu committed
414
415
416
417
    $this->tovalid = $row['tovalid_utl'];

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

Ayolo's avatar
Ayolo committed
418
    if ( is_null($row['tout_lu_avant_utl']) )
Feu's avatar
Feu committed
419
      $this->tout_lu_avant = null;
Ayolo's avatar
Ayolo committed
420
    else
Feu's avatar
Feu committed
421
      $this->tout_lu_avant = strtotime($row['tout_lu_avant_utl']);
Ayolo's avatar
Ayolo committed
422
423
  }

424
  function _load_extras($row)
Ayolo's avatar
Ayolo committed
425
426
427
428
429
  {
    if ( $this->etudiant || $this->ancien_etudiant )
    {
      $this->citation = $row["citation"];
      $this->adresse_parents = $row["adresse_parents"];
mbriand's avatar
bleh    
mbriand committed
430
431
      $this->id_ville_parents = $row["id_ville_parents"];
      $this->id_pays_parents = $row["id_pays_parents"];
Ayolo's avatar
Ayolo committed
432
433
434
435
436
437
438
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
      $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
466

467
468
469
    $this->musicien = $row["musicien_utl"];
    $this->taille_tshirt = $row["taille_tshirt_utl"];
    $this->permis_conduire = $row["permis_conduire_utl"];
Feu's avatar
Feu committed
470

Feu's avatar
Feu committed
471
    if ( !is_null($row["date_permis_conduire_utl"]) && $this->permis_conduire )
Feu's avatar
fix    
Feu committed
472
      $this->date_permis_conduire = strtotime($row["date_permis_conduire_utl"]);
473
474
    else
      $this->date_permis_conduire = null;
Feu's avatar
Feu committed
475

476
    $this->hab_elect = $row["hab_elect_utl"];
Feu's avatar
Feu committed
477
478
    $this->afps = $row["afps_utl"];
    $this->sst = $row["sst_utl"];
479
    $this->site_web = $row['site_web'];
BenC's avatar
BenC committed
480
    $this->jabber = $row['jabber_utl'];
481
482
483
484
485
486
  }

  function _load_all ( $row )
  {
    $this->_load($row);
    $this->_load_extras($row);
Ayolo's avatar
Ayolo committed
487
  }
Feu's avatar
Feu committed
488
489

  /**
Ayolo's avatar
Ayolo committed
490
491
492
493
494
495
496
497
   */
  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
498

Ayolo's avatar
Ayolo committed
499
500
501
502
503
504
505
506
507
508
509
510
  /** 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
511

Ayolo's avatar
Ayolo committed
512
513
    $this->hash = "valid";
    $this->tovalid = "none";
Feu's avatar
Feu committed
514

Ayolo's avatar
Ayolo committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
    $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
536
                          "utilisateurs",
Ayolo's avatar
Ayolo committed
537
538
539
                          array("utbm_utl"=>$this->utbm),
                          array("id_utilisateur"=>$this->id));
        $req = new delete($this->dbrw,
Ayolo's avatar
bleh    
Ayolo committed
540
541
                          "utl_etu_utbm",
                          array("id_utilisateur" => $this->id));
Ayolo's avatar
Ayolo committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
      }
    }
    $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
559
//    if ($this->pass == crypt($password, substr($this->pass,0,2)))
Ayolo's avatar
bleh    
Ayolo committed
560
    if ($this->pass == crypt($password, "ae"))
Ayolo's avatar
Ayolo committed
561
562
563
564
565
566
567
568
569
      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
570
    $this->pass = crypt($new_password, "ae");
Ayolo's avatar
Ayolo committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
    $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 ()
  {
585
    $this->groupes = array();
586

587
    if ( !$this->is_valid() )
588
589
      return;

Ayolo's avatar
Ayolo committed
590
591
592
593
594
595
596
597
598
599
600
601
602
    $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
603
    if ( $this->ae )
Ayolo's avatar
Ayolo committed
604
      $this->groupes[10000] = "ae-membres";
Feu's avatar
Feu committed
605
606

    if ( $this->utbm )
Ayolo's avatar
Ayolo committed
607
      $this->groupes[10001] = "utbm";
Feu's avatar
Feu committed
608
609

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

612
    if ( $this->etudiant )
Ayolo's avatar
Ayolo committed
613
614
      $this->groupes[10003] = "etudiants-actuels";

Feu's avatar
Feu committed
615
    if ( $this->etudiant && $this->utbm )
Ayolo's avatar
Ayolo committed
616
      $this->groupes[10004] = "etudiants-utbm-actuels";
Feu's avatar
Feu committed
617
618

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

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

Feu's avatar
Feu committed
624
    if ( $this->ancien_etudiant || $this->etudiant )
Ayolo's avatar
Ayolo committed
625
      $this->groupes[10007] = "etudiants-tous";
Feu's avatar
Feu committed
626
627

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

mbriand's avatar
mbriand committed
630
631
632
633
634
635
636
637
    // 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
638
639
640
641
    if ( $this->crous )
      $this->groupes[10013] = "crous-membres";

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

644
645
646
647
648
649
650
651
652
653
654
    $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
655

656
657
658
659
    while ( list($id,$name,$role,$parent) = $req->get_row() )
    {
      if ( $role > 1 )
        $this->groupes[$id+20000] = $name."-bureau";
Feu's avatar
Feu committed
660

661
662
663
      if( !is_null($parent) )
        $this->groupes[$id+30000] = $name."-membres";
    }
Feu's avatar
Feu committed
664

665
666
667
668
669
670
671
672
673
674
675
    $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
676
677
678
679
680
681
682
    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";

  }
683

Feu's avatar
Feu committed
684
685
686
  function _update_mailings ( $oldemail, $newemail )
  {
    require_once($topdir."include/entities/asso.inc.php");
687

Feu's avatar
Feu committed
688
689
690
691
692
693
694
695
696
697
    $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
698
                       "AND (asso.id_asso=1 OR (id_asso_parent IS NOT NULL AND id_asso_parent!=3)) ".
Feu's avatar
Feu committed
699
                       "ORDER BY `asso`.`nom_unix_asso`");
Feu's avatar
Feu committed
700
701
702
703
704

    while ( list($name,$role,$parent) = $req->get_row() )
    {
      if ( $role > 1 )
      {
Feu's avatar
fix    
Feu committed
705
706
        asso::_ml_unsubscribe($this->dbrw,$name.".bureau",$oldemail);
        asso::_ml_subscribe($this->dbrw,$name.".bureau",$newemail);
Feu's avatar
Feu committed
707
708
709
      }
      if( !is_null($parent) )
      {
Feu's avatar
fix    
Feu committed
710
711
        asso::_ml_unsubscribe($this->dbrw,$name.".membres",$oldemail);
        asso::_ml_subscribe($this->dbrw,$name.".membres",$newemail);
Feu's avatar
Feu committed
712
      }
713
714
    }

Feu's avatar
Feu committed
715
  }
716
717
718



Ayolo's avatar
Ayolo committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
  /** 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
741
    if(is_null($id) || empty($id))
Ayolo's avatar
bleh    
Ayolo committed
742
      return false;
Ayolo's avatar
Ayolo committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
    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;
  }

773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  /**
   * 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)
  {
    $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
789
790
    if ( is_null($this->groupes) )
      $this->load_groups();
791

jlaval's avatar
jlaval committed
792
    $fragment = '('.$fragment.' OR ('.$id_groupe.' = '.array_search('ae_membres', $this->groupes).' AND '.$date_field.' <= '.$derniere_cotiz.'))';
793
794
  }

Ayolo's avatar
Ayolo committed
795
796
797
798
799
800
  /* Extra infos management */
  /** Change toutes les informations secondaires de l'utilisateur
   */
  function load_all_extra ()
  {
    $req = new requete($this->db,
mbriand's avatar
bleh    
mbriand committed
801
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.*, ".
mbriand's avatar
bleh    
mbriand committed
802
803
                       "`utl_etu`.`id_ville` AS `id_ville_parents`, ".
                       "`utl_etu`.`id_pays` AS `id_pays_parents` ".
804
805
                       "FROM utilisateurs ".
                       "LEFT JOIN `utl_etu` ON (`utilisateurs`.`id_utilisateur`=`utl_etu`.`id_utilisateur`) ".
Feu's avatar
oups    
Feu committed
806
807
                       "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`) ".
808
809
810
811
812
                       "WHERE ".
                       "`utilisateurs`.`id_utilisateur` = '" . mysql_real_escape_string($this->id). "' ".
                       "LIMIT 1");

    $this->_load_extras($req->get_row());
Ayolo's avatar
Ayolo committed
813
814
815
816
817
818
819
820
821
  }

  /**
   * 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;
822

Feu's avatar
Feu committed
823
824
    if ( empty($this->alias) )
      $this->alias = null;
825

Feu's avatar
Feu committed
826
    require_once($topdir."include/cts/cached.inc.php");
Feu's avatar
Feu committed
827
    $cache = new cachedcontents("sig".$this->id);
Feu's avatar
Feu committed
828
    $cache->expire();
829

Feu's avatar
Feu committed
830
    new update($this->dbrw,
Ayolo's avatar
Ayolo committed
831
832
833
834
                      "utilisateurs",
                      array('nom_utl' => $this->nom,
                            'prenom_utl' => $this->prenom,
                            'sexe_utl' => $this->sexe,
mbriand's avatar
mbriand committed
835
836
                            'date_naissance_utl' => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
837
838
839
840
841
842
843
844
                            '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
845
                            'publique_utl'=> $this->publique,
Ayolo's avatar
Ayolo committed
846
847
                            'publique_mmtpapier_utl'=>$this->publique_mmtpapier,
                            'signature_utl' =>$this->signature),
mbriand's avatar
mbriand committed
848
                      array('id_utilisateur' => $this->id));
Ayolo's avatar
Ayolo committed
849

Feu's avatar
Feu committed
850
851
852
853




Ayolo's avatar
Ayolo committed
854
855
    if ( $this->etudiant || $this->ancien_etudiant )
    {
Feu's avatar
Feu committed
856
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
857
858
859
860
861
862
863
864
865
866
867
868
869
                        "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
870
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
871
872
873
874
875
                        "utl_etu_utbm",
                        array('semestre_utbm' => $this->semestre,
                              'role_utbm' => $this->role,
                              'departement_utbm' => $this->departement,
                              'filiere_utbm' => $this->filiere,
876
                              'surnom_utbm' => (!empty($this->surnom) ? $this->surnom : null),
Ayolo's avatar
Ayolo committed
877
878
879
880
                              '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
881

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

Feu's avatar
fix    
Feu committed
884
885
    if ( !$this->permis_conduire )
      $this->date_permis_conduire=null;
886

887
888
889
890
891
    if ( $req->lines == 0 )
      new insert($this->dbrw,
                      "utl_extra",
                      array(
                      'id_utilisateur' => $this->id,
892
                      'site_web' => $this->site_web,
893
894
895
                      'musicien_utl'=>$this->musicien,
                      'taille_tshirt_utl'=>$this->taille_tshirt,
                      'permis_conduire_utl'=>$this->permis_conduire,
Feu's avatar
fix    
Feu committed
896
                      'date_permis_conduire_utl'=>is_null($this->date_permis_conduire)?null:date("Y-m-d",$this->date_permis_conduire),
897
898
                      'hab_elect_utl'=>$this->hab_elect,
                      'afps_utl'=>$this->afps,
BenC's avatar
BenC committed
899
                      'sst_utl'=>$this->sst,
BenC's avatar
bleh    
BenC committed
900
                      'jabber_utl'=>$this->jabber));
Feu's avatar
Feu committed
901
902
903
904
    else
      new update($this->dbrw,
                    "utl_extra",
                    array(
905
                    'site_web' => $this->site_web,
Feu's avatar
Feu committed
906
907
908
                    'musicien_utl'=>$this->musicien,
                    'taille_tshirt_utl'=>$this->taille_tshirt,
                    'permis_conduire_utl'=>$this->permis_conduire,
Feu's avatar
fix    
Feu committed
909
                    'date_permis_conduire_utl'=> is_null($this->date_permis_conduire)?null:date("Y-m-d",$this->date_permis_conduire),
Feu's avatar
Feu committed
910
911
                    'hab_elect_utl'=>$this->hab_elect,
                    'afps_utl'=>$this->afps,
BenC's avatar
BenC committed
912
                    'sst_utl'=>$this->sst,
BenC's avatar
bleh    
BenC committed
913
                    'jabber_utl'=>$this->jabber),
Feu's avatar
Feu committed
914
915
916
                    array('id_utilisateur' => $this->id));


Ayolo's avatar
Ayolo committed
917

918
/*    if ( XML_RPC_USE )
Ayolo's avatar
Ayolo committed
919
920
921
922
923
924
925
926
927
928
    {
              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;
929
    }*/
Ayolo's avatar
Ayolo committed
930
931
932
933
934
935
936
937
938
939
940
941
942
943

    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
944

Ayolo's avatar
Ayolo committed
945
946
947
948
    // 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
949
                       "WHERE `email_utbm`='".mysql_real_escape_string($email_utbm)."' ".
Feu's avatar
debug    
Feu committed
950
                       "AND id_utilisateur !='".mysql_real_escape_string($this->id)."' ");
Feu's avatar
Feu committed
951

Ayolo's avatar
Ayolo committed
952
953
    if ( $req->lines > 0 )
      return false;
Feu's avatar
Feu committed
954

Feu's avatar
fixes    
Feu committed
955
956
957
958
    // 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
959
                       "WHERE `id_utilisateur` = '" . mysql_real_escape_string($this->id) . "'");
Feu's avatar
fixes    
Feu committed
960
961
962
963
964
965
966
967
    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));
    }
968

Ayolo's avatar
Ayolo committed
969
970
971
972
973
974
975
976
977
978
979
980
981
    // 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
982

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

Ayolo's avatar
Ayolo committed
986
987
988
989
990
991
992
993
994
995
996
997
    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
998

Ayolo's avatar
Ayolo committed
999
1000
    $this->etudiant = !$ancien;
    $this->ancien_etudiant = $ancien;
Feu's avatar
Feu committed
1001

Ayolo's avatar
Ayolo committed
1002
1003
    $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
1004

Ayolo's avatar
Ayolo committed
1005
1006
1007
1008
    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
1009

Ayolo's avatar
Ayolo committed
1010
1011
1012
1013
1014
1015
    $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));
1016
1017

    // Force le role à "etu" si l'utilisateur est utbm
Feu's avatar
fixes    
Feu committed
1018
1019
    if ( $this->utbm )
    {
1020
      $this->role = "etu";
Feu's avatar
fixes    
Feu committed
1021
1022
1023
      new update($this->dbrw,"utl_etu_utbm",
        array('role_utbm' => $this->role),
        array('id_utilisateur' => $this->id));
1024
    }
Ayolo's avatar
Ayolo committed
1025
  }
1026

Feu's avatar
wip    
Feu committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076

  /** 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
1077
1078
1079
  function set_licence_default_sas( $id_licence, $all = false)
  {
    global $topdir;
Ayolo's avatar
bleh    
Ayolo committed
1080
    require_once($topdir."sas2/include/licence.inc.php");
Ayolo's avatar
bleh    
Ayolo committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
    $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
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
  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
1133
1134
                         $_etudiant=false,
                         $send_email=true)
Ayolo's avatar
Ayolo committed
1135
  {
1136
    $this->type = "std";
Ayolo's avatar
Ayolo committed
1137
1138
1139
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);
    $this->email = $email;
slopez's avatar
bleh    
slopez committed
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
    $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
1158
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1159
1160
1161
1162
1163
    $this->sexe = $sexe;
    $this->date_naissance = $date_naissance;
    $this->droit_image = $droit_image;

    $this->ae = false;
mbriand's avatar
mbriand committed
1164
1165
    $this->assidu = false;
    $this->amicale = false;
mbriand's avatar
mbriand committed
1166
    $this->crous = false;
Ayolo's avatar
Ayolo committed
1167
1168
1169
1170
    $this->utbm = $_utbm;
    $this->etudiant = $_etudiant;
    $this->ancien_etudiant = false;
    $this->modere = false;
1171
    $this->publique = 2;
Ayolo's avatar
Ayolo committed
1172
1173
1174
1175
    $this->publique_mmtpapier = true;

    $sql = new insert ($this->dbrw,
                       "utilisateurs",
1176
1177
                        array("type_utl"=>$this->type,
                              "nom_utl" => $this->nom,
Ayolo's avatar
Ayolo committed
1178
1179
1180
1181
1182
1183
                              "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
1184
1185
                              "date_naissance_utl" => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
1186
1187
1188
1189
1190
                              "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
1191
1192
                              "assidu_utl"=>false,
                              "amicale_utl"=>false,
mbriand's avatar
mbriand committed
1193
                              "crous_utl"=>false,
Ayolo's avatar
Ayolo committed
1194
1195
                              "modere_utl"=> $this->modere,
                              "montant_compte"=> 0,
Feu's avatar
Feu committed
1196
                              "publique_utl"=> $this->publique,
Ayolo's avatar
Ayolo committed
1197
                              "publique_mmtpapier_utl"=>$this->publique_mmtpapier,
slopez's avatar
fixed    
slopez committed
1198
                              "tovalid_utl"=>"none"));
Ayolo's avatar
Ayolo committed
1199
1200
1201
1202
1203
1204
1205
1206

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

Ayolo's avatar
bleh    
Ayolo committed
1208
1209
1210
1211
1212
    if($send_email)
    {
      $this->invalidate ("email");
      $this->send_first_email($this->email,$password);
    }
Feu's avatar
Feu committed
1213

Ayolo's avatar
Ayolo committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    return true;
  }

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

Ayolo's avatar
Ayolo committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
    if ( !$this->create_user ( $nom,
                               $prenom,
                               $email,
                               $password,
                               $droit_image,
                               $date_naissance,
                               $sexe,
                               $_utbm,
                               true))
      return false;
Feu's avatar
Feu committed
1238

Ayolo's avatar
Ayolo committed
1239
1240
1241
1242
    if ( $_utbm )
      $this->nom_ecole_etudiant = "utbm";
    else
      $this->nom_ecole_etudiant = $ecole;
Feu's avatar
Feu committed
1243

Ayolo's avatar
Ayolo committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
    $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
1284

Ayolo's avatar
Ayolo committed
1285
1286
1287
    $this->role = $role;
    $this->departement = $departement;
    $this->email_utbm = $emailutbm;
Feu's avatar
Feu committed
1288

Ayolo's avatar
Ayolo committed
1289
1290
1291
1292
1293
1294
    $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
1295

Ayolo's avatar
Ayolo committed
1296
1297
1298
1299
1300
1301
1302
1303
    return true;
  }

  /**
   * Inscription par un admin (uniquement)
   * @todo Fonction à revoir complètement (aussi bien usage dans le site que implémentation)
   * @deprecated
   */
slopez's avatar
bleh    
slopez committed
1304
  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
1305
  {
1306
    $this->type="std";
1307

Ayolo's avatar
Ayolo committed
1308
1309
1310
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);

Feu's avatar
Feu committed
1311
1312
    if (CheckEmail($emailutbm,1))
      $this->email_utbm = $emailutbm;
Ayolo's avatar
Ayolo committed
1313

Feu's avatar
Feu committed
1314
1315
1316
    $this->utbm = true;

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

slopez's avatar
bleh    
slopez committed
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
    $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
1336
1337
1338

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

Ayolo's avatar
bleh    
Ayolo committed
1340
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1341
1342
1343
1344
1345
1346
1347
    $this->etudiant = $etudiant==true;

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

1348
    $this->publique = 2;
Ayolo's avatar
Ayolo committed
1349
    $this->publique_mmtpapier = true;
Feu's avatar
Feu committed
1350

Ayolo's avatar
Ayolo committed
1351
1352
1353
1354
    $this->droit_image = $droit_image;

    $sql = new insert ($this->dbrw,
                       "utilisateurs",
1355
1356
                       array("type_utl"=>$this->type,
                             "nom_utl" => $this->nom,
Ayolo's avatar
Ayolo committed
1357
1358
1359
1360
1361
1362
                             "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
1363
1364
                             "date_naissance_utl" => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
1365
1366
1367
1368
1369
                             "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
1370
1371
                             "assidu_utl"=>false,
                             "amicale_utl"=>false,
mbriand's avatar
mbriand committed
1372
                             "crous_utl"=>false,
Ayolo's avatar
Ayolo committed
1373
1374
                             "modere_utl"=> $this->modere,
                             "montant_compte"=> 0,
Feu's avatar
Feu committed
1375
                             "publique_utl"=> $this->publique,
Ayolo's avatar
Ayolo committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
                             "publique_mmtpapier_utl"=>$this->publique_mmtpapier,
                             "tovalid_utl"=>"none"));

    if ( $sql )
      $this->id = $sql->get_id();
    else
    {
      $this->id = null;
      return false;
    }

    $this->set_droit_image($this->droit_image);
    $this->invalidate ("email");

    if ($this->etudiant && $nom_ecole)
    {
      $this->nom_ecole_etudiant = $nom_ecole;

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

      if (!$req)
      {
        $this->id = null;
        return false;
      }
    }

    if (strtoupper($nom_ecole) == "UTBM")
    {
      $this->promo_utbm = $promo;
      $this->semestre = $semestre;
      $this->email_utbm = $emailutbm;
Feu's avatar
Feu committed
1411

Ayolo's avatar
Ayolo committed
1412
1413
      $this->role = $role;
      $this->departement = $departement;
Feu's avatar
Feu committed
1414

Ayolo's avatar
Ayolo committed
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
      $req = new insert($this->dbrw,
                        "utl_etu_utbm",
                        array('id_utilisateur' => $this->id,
                              'semestre_utbm'  => $this->semestre,
                              'role_utbm' => $this->role,
                              'departement_utbm' => $this->departement,
                              'promo_utbm'     => $this->promo_utbm,
                              'email_utbm'     => $this->email_utbm));
    }

    if ( $this->email_utbm )
      $this->send_first_email($this->email_utbm,$password);
Feu's avatar
Feu committed
1427

Ayolo's avatar
Ayolo committed