utilisateur.inc.php 82.6 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;
Ayolo's avatar
Ayolo committed
75
76
77
  var $modere;

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

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

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

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

Ayolo's avatar
Ayolo committed
91
  var $tovalid;
Feu's avatar
Feu committed
92

Ayolo's avatar
Ayolo committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  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
109

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

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


128
  var $_grps;
Ayolo's avatar
Ayolo committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  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
146
   * @return true en cas de succès, false sinon
Ayolo's avatar
Ayolo committed
147
148
   */
  function load_by_id ( $id )
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  {
    $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
164
  {
165
    $req = new requete($this->db,
mbriand's avatar
bleh    
mbriand committed
166
167
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.*, `utilisateurs`.*, ".
                       "`utl_etu`.`id_ville` AS `id_ville_parent`, ".
mbriand's avatar
bleh    
mbriand committed
168
                       "`utl_etu`.`id_pays` AS `id_pays_parent` ".
169
170
171
172
173
174
175
                       "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
176
177
178

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

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

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

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

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

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

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

    $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
278
                                  "WHERE $cond " .
Ayolo's avatar
Ayolo committed
279
280
281
282
283
284
285
286
                                  "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
287

Ayolo's avatar
Ayolo committed
288
    $this->id = null;
Feu's avatar
Feu committed
289

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

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

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

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

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

Ayolo's avatar
Ayolo committed
403
404
    if ( is_null($row['derniere_visite_utl']) )
      $this->derniere_visite = null;
Feu's avatar
Feu committed
405
    else
Ayolo's avatar
Ayolo committed
406
      $this->derniere_visite = strtotime($row['derniere_visite_utl']);
Feu's avatar
Feu committed
407
408

    $this->publique = $row['publique_utl'];
Ayolo's avatar
Ayolo committed
409
    $this->publique_mmtpapier = $row['publique_mmtpapier_utl'];
Feu's avatar
Feu committed
410
411
412
413
    $this->tovalid = $row['tovalid_utl'];

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

Ayolo's avatar
Ayolo committed
414
    if ( is_null($row['tout_lu_avant_utl']) )
Feu's avatar
Feu committed
415
      $this->tout_lu_avant = null;
Ayolo's avatar
Ayolo committed
416
    else
Feu's avatar
Feu committed
417
      $this->tout_lu_avant = strtotime($row['tout_lu_avant_utl']);
Ayolo's avatar
Ayolo committed
418
419
  }

420
  function _load_extras($row)
Ayolo's avatar
Ayolo committed
421
422
423
424
425
  {
    if ( $this->etudiant || $this->ancien_etudiant )
    {
      $this->citation = $row["citation"];
      $this->adresse_parents = $row["adresse_parents"];
mbriand's avatar
bleh    
mbriand committed
426
427
      $this->id_ville_parents = $row["id_ville_parents"];
      $this->id_pays_parents = $row["id_pays_parents"];
Ayolo's avatar
Ayolo committed
428
429
430
431
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
      $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
462

463
464
465
    $this->musicien = $row["musicien_utl"];
    $this->taille_tshirt = $row["taille_tshirt_utl"];
    $this->permis_conduire = $row["permis_conduire_utl"];
Feu's avatar
Feu committed
466

Feu's avatar
Feu committed
467
    if ( !is_null($row["date_permis_conduire_utl"]) && $this->permis_conduire )
Feu's avatar
fix    
Feu committed
468
      $this->date_permis_conduire = strtotime($row["date_permis_conduire_utl"]);
469
470
    else
      $this->date_permis_conduire = null;
Feu's avatar
Feu committed
471

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

  function _load_all ( $row )
  {
    $this->_load($row);
    $this->_load_extras($row);
Ayolo's avatar
Ayolo committed
483
  }
Feu's avatar
Feu committed
484
485

  /**
Ayolo's avatar
Ayolo committed
486
487
488
489
490
491
492
493
   */
  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
494

Ayolo's avatar
Ayolo committed
495
496
497
498
499
500
501
502
503
504
505
506
  /** 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
507

Ayolo's avatar
Ayolo committed
508
509
    $this->hash = "valid";
    $this->tovalid = "none";
Feu's avatar
Feu committed
510

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

583
    if ( !$this->is_valid() )
584
585
      return;

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

    if ( $this->utbm )
Ayolo's avatar
Ayolo committed
603
      $this->groupes[10001] = "utbm";
Feu's avatar
Feu committed
604
605

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

608
    if ( $this->etudiant )
Ayolo's avatar
Ayolo committed
609
610
      $this->groupes[10003] = "etudiants-actuels";

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

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

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

Feu's avatar
Feu committed
620
    if ( $this->ancien_etudiant || $this->etudiant )
Ayolo's avatar
Ayolo committed
621
      $this->groupes[10007] = "etudiants-tous";
Feu's avatar
Feu committed
622
623

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

mbriand's avatar
mbriand committed
626
627
628
629
630
631
632
633
    // 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
634
635
636
    if ( $this->ae || $this->amicale || $this->assidu )
      $this->groupes[10012] = "cotisants-tous";

637
638
639
640
641
642
643
644
645
646
647
    $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
648

649
650
651
652
    while ( list($id,$name,$role,$parent) = $req->get_row() )
    {
      if ( $role > 1 )
        $this->groupes[$id+20000] = $name."-bureau";
Feu's avatar
Feu committed
653

654
655
656
      if( !is_null($parent) )
        $this->groupes[$id+30000] = $name."-membres";
    }
Feu's avatar
Feu committed
657

658
659
660
661
662
663
664
665
666
667
668
    $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
669
670
671
672
673
674
675
    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";

  }
676

Feu's avatar
Feu committed
677
678
679
  function _update_mailings ( $oldemail, $newemail )
  {
    require_once($topdir."include/entities/asso.inc.php");
680

Feu's avatar
Feu committed
681
682
683
684
685
686
687
688
689
690
    $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
691
                       "AND (asso.id_asso=1 OR (id_asso_parent IS NOT NULL AND id_asso_parent!=3)) ".
Feu's avatar
Feu committed
692
                       "ORDER BY `asso`.`nom_unix_asso`");
Feu's avatar
Feu committed
693
694
695
696
697

    while ( list($name,$role,$parent) = $req->get_row() )
    {
      if ( $role > 1 )
      {
Feu's avatar
fix    
Feu committed
698
699
        asso::_ml_unsubscribe($this->dbrw,$name.".bureau",$oldemail);
        asso::_ml_subscribe($this->dbrw,$name.".bureau",$newemail);
Feu's avatar
Feu committed
700
701
702
      }
      if( !is_null($parent) )
      {
Feu's avatar
fix    
Feu committed
703
704
        asso::_ml_unsubscribe($this->dbrw,$name.".membres",$oldemail);
        asso::_ml_subscribe($this->dbrw,$name.".membres",$newemail);
Feu's avatar
Feu committed
705
      }
706
707
    }

Feu's avatar
Feu committed
708
  }
709
710
711



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

766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
  /**
   * 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
782
783
    if ( is_null($this->groupes) )
      $this->load_groups();
784

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

Ayolo's avatar
Ayolo committed
788
789
790
791
792
793
  /* 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
794
795
796
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.*, ".
                       "`utl_etu`.`id_ville` AS `id_ville_parent`, ".
                       "`utl_etu`.`id_pays` AS `id_pays_parent` ".
797
798
                       "FROM utilisateurs ".
                       "LEFT JOIN `utl_etu` ON (`utilisateurs`.`id_utilisateur`=`utl_etu`.`id_utilisateur`) ".
Feu's avatar
oups    
Feu committed
799
800
                       "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`) ".
801
802
803
804
805
                       "WHERE ".
                       "`utilisateurs`.`id_utilisateur` = '" . mysql_real_escape_string($this->id). "' ".
                       "LIMIT 1");

    $this->_load_extras($req->get_row());
Ayolo's avatar
Ayolo committed
806
807
808
809
810
811
812
813
814
  }

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

Feu's avatar
Feu committed
816
817
    if ( empty($this->alias) )
      $this->alias = null;
818

Feu's avatar
Feu committed
819
    require_once($topdir."include/cts/cached.inc.php");
Feu's avatar
Feu committed
820
    $cache = new cachedcontents("sig".$this->id);
Feu's avatar
Feu committed
821
    $cache->expire();
822

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

Feu's avatar
Feu committed
843
844
845
846




Ayolo's avatar
Ayolo committed
847
848
    if ( $this->etudiant || $this->ancien_etudiant )
    {
Feu's avatar
Feu committed
849
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
850
851
852
853
854
855
856
857
858
859
860
861
862
                        "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
863
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
864
865
866
867
868
869
870
871
872
873
                        "utl_etu_utbm",
                        array('semestre_utbm' => $this->semestre,
                              'role_utbm' => $this->role,
                              'departement_utbm' => $this->departement,
                              'filiere_utbm' => $this->filiere,
                              'surnom_utbm' => $this->surnom,
                              '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
874

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

Feu's avatar
fix    
Feu committed
877
878
    if ( !$this->permis_conduire )
      $this->date_permis_conduire=null;
879

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


Ayolo's avatar
Ayolo committed
910

911
/*    if ( XML_RPC_USE )
Ayolo's avatar
Ayolo committed
912
913
914
915
916
917
918
919
920
921
    {
              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;
922
    }*/
Ayolo's avatar
Ayolo committed
923
924
925
926
927
928
929
930
931
932
933
934
935
936

    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
937

Ayolo's avatar
Ayolo committed
938
939
940
941
    // 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
942
                       "WHERE `email_utbm`='".mysql_real_escape_string($email_utbm)."' ".
Feu's avatar
debug    
Feu committed
943
                       "AND id_utilisateur !='".mysql_real_escape_string($this->id)."' ");
Feu's avatar
Feu committed
944

Ayolo's avatar
Ayolo committed
945
946
    if ( $req->lines > 0 )
      return false;
Feu's avatar
Feu committed
947

Feu's avatar
fixes    
Feu committed
948
949
950
951
    // 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
952
                       "WHERE `id_utilisateur` = '" . mysql_real_escape_string($this->id) . "'");
Feu's avatar
fixes    
Feu committed
953
954
955
956
957
958
959
960
    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));
    }
961

Ayolo's avatar
Ayolo committed
962
963
964
965
966
967
968
969
970
971
972
973
974
    // 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
975

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

Ayolo's avatar
Ayolo committed
979
980
981
982
983
984
985
986
987
988
989
990
    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
991

Ayolo's avatar
Ayolo committed
992
993
    $this->etudiant = !$ancien;
    $this->ancien_etudiant = $ancien;
Feu's avatar
Feu committed
994

Ayolo's avatar
Ayolo committed
995
996
    $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
997

Ayolo's avatar
Ayolo committed
998
999
1000
1001
    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
1002

Ayolo's avatar
Ayolo committed
1003
1004
1005
1006
1007
1008
    $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));
1009
1010

    // Force le role à "etu" si l'utilisateur est utbm
Feu's avatar
fixes    
Feu committed
1011
1012
    if ( $this->utbm )
    {
1013
      $this->role = "etu";
Feu's avatar
fixes    
Feu committed
1014
1015
1016
      new update($this->dbrw,"utl_etu_utbm",
        array('role_utbm' => $this->role),
        array('id_utilisateur' => $this->id));
1017
    }
Ayolo's avatar
Ayolo committed
1018
  }
1019

Feu's avatar
wip    
Feu committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  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
1037
1038
1039
1040
1041
1042
1043
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

  /** 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
1070
1071
1072
  function set_licence_default_sas( $id_licence, $all = false)
  {
    global $topdir;
Ayolo's avatar
bleh    
Ayolo committed
1073
    require_once($topdir."sas2/include/licence.inc.php");
Ayolo's avatar
bleh    
Ayolo committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
    $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
1090
1091
1092
1093
1094
1095
1096
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
  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
1126
1127
                         $_etudiant=false,
                         $send_email=true)
Ayolo's avatar
Ayolo committed
1128
  {
1129
    $this->type = "std";
Ayolo's avatar
Ayolo committed
1130
1131
1132
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);
    $this->email = $email;
slopez's avatar
bleh    
slopez committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
    $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
1151
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1152
1153
1154
1155
1156
    $this->sexe = $sexe;
    $this->date_naissance = $date_naissance;
    $this->droit_image = $droit_image;

    $this->ae = false;
mbriand's avatar
mbriand committed
1157
1158
    $this->assidu = false;
    $this->amicale = false;
Ayolo's avatar
Ayolo committed
1159
1160
1161
1162
1163
1164
1165
1166
1167
    $this->utbm = $_utbm;
    $this->etudiant = $_etudiant;
    $this->ancien_etudiant = false;
    $this->modere = false;
    $this->publique = true;
    $this->publique_mmtpapier = true;

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

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

Ayolo's avatar
bleh    
Ayolo committed
1199
1200
1201
1202
1203
    if($send_email)
    {
      $this->invalidate ("email");
      $this->send_first_email($this->email,$password);
    }
Feu's avatar
Feu committed
1204

Ayolo's avatar
Ayolo committed
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
    return true;
  }

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

Ayolo's avatar
Ayolo committed
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
    if ( !$this->create_user ( $nom,
                               $prenom,
                               $email,
                               $password,
                               $droit_image,
                               $date_naissance,
                               $sexe,
                               $_utbm,
                               true))
      return false;
Feu's avatar
Feu committed
1229

Ayolo's avatar
Ayolo committed
1230
1231
1232
1233
    if ( $_utbm )
      $this->nom_ecole_etudiant = "utbm";
    else
      $this->nom_ecole_etudiant = $ecole;
Feu's avatar
Feu committed
1234

Ayolo's avatar
Ayolo committed
1235
1236
1237
1238
1239
1240
1241
1242
1243
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
    $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
1275

Ayolo's avatar
Ayolo committed
1276
1277
1278
    $this->role = $role;
    $this->departement = $departement;
    $this->email_utbm = $emailutbm;
Feu's avatar
Feu committed
1279

Ayolo's avatar
Ayolo committed
1280
1281
1282
1283
1284
1285
    $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
1286

Ayolo's avatar
Ayolo committed
1287
1288
1289
1290
1291
1292
1293
1294
    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
1295
  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
1296
  {
1297
    $this->type="std";
1298

Ayolo's avatar
Ayolo committed
1299
1300
1301
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);

Feu's avatar
Feu committed
1302
1303
    if (CheckEmail($emailutbm,1))
      $this->email_utbm = $emailutbm;
Ayolo's avatar
Ayolo committed
1304

Feu's avatar
Feu committed
1305
1306
1307
    $this->utbm = true;

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

slopez's avatar
bleh    
slopez committed
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
    $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
1327
1328
1329

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

Ayolo's avatar
bleh    
Ayolo committed
1331
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
    $this->etudiant = $etudiant==true;

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

    $this->publique = true;
    $this->publique_mmtpapier = true;
Feu's avatar
Feu committed
1341

Ayolo's avatar
Ayolo committed
1342
1343
1344
1345
    $this->droit_image = $droit_image;

    $sql = new insert ($this->dbrw,
                       "utilisateurs",
1346
1347
                       array("type_utl"=>$this->type,
                             "nom_utl" => $this->nom,
Ayolo's avatar
Ayolo committed
1348
1349
1350
1351
1352
1353
                             "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
1354
1355
                             "date_naissance_utl" => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
1356
1357
1358
1359
1360
                             "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
1361
1362
                             "assidu_utl"=>false,
                             "amicale_utl"=>false,
Ayolo's avatar
Ayolo committed
1363
1364
                             "modere_utl"=> $this->modere,
                             "montant_compte"=> 0,
Feu's avatar
Feu committed
1365
                             "publique_utl"=> $this->publique,
Ayolo's avatar
Ayolo committed
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
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
                             "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
1401

Ayolo's avatar
Ayolo committed
1402
1403
      $this->role = $role;
      $this->departement = $departement;
Feu's avatar
Feu committed
1404

Ayolo's avatar
Ayolo committed
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
      $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
1417

Ayolo's avatar
Ayolo committed
1418
1419
1420
1421
1422
1423
1424
1425
    elseif ( $this->email )
      $this->send_first_email($this->email,$password);

    return true;
  }

  function set_email ( $email, $admin=false )
  {
Feu's avatar
Feu committed
1426
    $this->_update_mailings($this->email,$email);
1427

Ayolo's avatar
Ayolo committed
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
    $this->email = $email;

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

    if ( $admin )
      return;

    $this->invalidate("email");
    $this->send_activation_email($email);
  }

  function set_email_utbm ( $email, $admin=false  )
  {
    $this->email_utbm = $email;

    $req = new update($this->dbrw,
        "utl_etu_utbm",
      array(
        'email_utbm' => $this->email_utbm
        ),
      array( 'id_utilisateur' => $this->id));

    if ( $admin ) return;

    $this->invalidate("emailutbm");
    $this->send_activation_email($email);
  }

  function send_activation_email ( $email )
  {

  $body = "Bonjour,
Votre adresse email a été changée.

Pour valider votre adresse email et reactiver votre compte, veuillez vous rendre à l'adresse