utilisateur.inc.php 82.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;
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
taichte    
mbriand committed
166
                       "SELECT *, `utilisateurs`.`id_utilisateur` `id_utilisateur` ".
167
168
169
170
171
172
173
                       "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
174
175
176

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

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

198
    if (ereg("^([A-Za-z0-9\._-]+)@(utbm\.fr|assidu-utbm\.fr)$", $email, $regs))
199
      $req = new requete($this->db,
200
201
202
203
204
205
        "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
206
        "LIMIT 1");
207
    else
208
      $req = new requete($this->db,
209
210
211
212
        "SELECT `utilisateurs`.* FROM `utilisateurs` " .
        "LEFT JOIN `utl_etu_utbm` ON `utl_etu_utbm`.`id_utilisateur` = `utilisateurs`.`id_utilisateur` " .
        "WHERE `utilisateurs`.`email_utl` = '" . mysql_real_escape_string($email) . "' OR " .
        "`utl_etu_utbm`.`email_utbm` = '" . mysql_real_escape_string($email) . "' " .
slopez's avatar
debug    
slopez committed
213
        "LIMIT 1");
Ayolo's avatar
Ayolo committed
214
215
216
217
218
219

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

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

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

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

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

Ayolo's avatar
Ayolo committed
286
    $this->id = null;
Feu's avatar
Feu committed
287

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

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

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

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

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

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

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

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

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

418
  function _load_extras($row)
Ayolo's avatar
Ayolo committed
419
420
421
422
423
424
425
426
427
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
  {
    if ( $this->etudiant || $this->ancien_etudiant )
    {
      $this->citation = $row["citation"];
      $this->adresse_parents = $row["adresse_parents"];
      $this->id_ville_parents = $row["id_ville"];
      $this->id_pays_parents = $row["id_pays"];
      $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
460

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

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

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

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

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

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

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

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

581
    if ( !$this->is_valid() )
582
583
      return;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  }
674

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

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

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

Feu's avatar
Feu committed
706
  }
707
708
709



Ayolo's avatar
Ayolo committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
  /** 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
732
    if(is_null($id) || empty($id))
Ayolo's avatar
bleh    
Ayolo committed
733
      return false;
Ayolo's avatar
Ayolo committed
734
735
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
    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;
  }

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

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

Ayolo's avatar
Ayolo committed
786
787
788
789
790
791
  /* Extra infos management */
  /** Change toutes les informations secondaires de l'utilisateur
   */
  function load_all_extra ()
  {
    $req = new requete($this->db,
792
793
794
                       "SELECT `utl_etu`.*, `utl_etu_utbm`.*, `utl_extra`.* ".
                       "FROM utilisateurs ".
                       "LEFT JOIN `utl_etu` ON (`utilisateurs`.`id_utilisateur`=`utl_etu`.`id_utilisateur`) ".
Feu's avatar
oups    
Feu committed
795
796
                       "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`) ".
797
798
799
800
801
                       "WHERE ".
                       "`utilisateurs`.`id_utilisateur` = '" . mysql_real_escape_string($this->id). "' ".
                       "LIMIT 1");

    $this->_load_extras($req->get_row());
Ayolo's avatar
Ayolo committed
802
803
804
805
806
807
808
809
810
  }

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

Feu's avatar
Feu committed
812
813
    if ( empty($this->alias) )
      $this->alias = null;
814

Feu's avatar
Feu committed
815
    require_once($topdir."include/cts/cached.inc.php");
Feu's avatar
Feu committed
816
    $cache = new cachedcontents("sig".$this->id);
Feu's avatar
Feu committed
817
    $cache->expire();
818

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

Feu's avatar
Feu committed
839
840
841
842




Ayolo's avatar
Ayolo committed
843
844
    if ( $this->etudiant || $this->ancien_etudiant )
    {
Feu's avatar
Feu committed
845
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
846
847
848
849
850
851
852
853
854
855
856
857
858
                        "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
859
      new update($this->dbrw,
Ayolo's avatar
Ayolo committed
860
861
862
863
864
865
866
867
868
869
                        "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
870

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

Feu's avatar
fix    
Feu committed
873
874
    if ( !$this->permis_conduire )
      $this->date_permis_conduire=null;
875

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


Ayolo's avatar
Ayolo committed
906

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

    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
933

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

Ayolo's avatar
Ayolo committed
941
942
    if ( $req->lines > 0 )
      return false;
Feu's avatar
Feu committed
943

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

Ayolo's avatar
Ayolo committed
958
959
960
961
962
963
964
965
966
967
968
969
970
    // 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
971

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

Ayolo's avatar
Ayolo committed
975
976
977
978
979
980
981
982
983
984
985
986
    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
987

Ayolo's avatar
Ayolo committed
988
989
    $this->etudiant = !$ancien;
    $this->ancien_etudiant = $ancien;
Feu's avatar
Feu committed
990

Ayolo's avatar
Ayolo committed
991
992
    $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
993

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

Ayolo's avatar
Ayolo committed
999
1000
1001
1002
1003
1004
    $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));
1005
1006

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

Feu's avatar
wip    
Feu committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
  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
1033
1034
1035
1036
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

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

    $this->ae = false;
mbriand's avatar
mbriand committed
1153
1154
    $this->assidu = false;
    $this->amicale = false;
Ayolo's avatar
Ayolo committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
    $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",
1164
1165
                        array("type_utl"=>$this->type,
                              "nom_utl" => $this->nom,
Ayolo's avatar
Ayolo committed
1166
1167
1168
1169
1170
1171
                              "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
1172
1173
                              "date_naissance_utl" => (is_null($this->date_naissance)
                                ? null : date("Y-m-d",$this->date_naissance)),
Ayolo's avatar
Ayolo committed
1174
1175
1176
1177
1178
                              "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
1179
1180
                              "assidu_utl"=>false,
                              "amicale_utl"=>false,
Ayolo's avatar
Ayolo committed
1181
1182
                              "modere_utl"=> $this->modere,
                              "montant_compte"=> 0,
Feu's avatar
Feu committed
1183
                              "publique_utl"=> $this->publique,
Ayolo's avatar
Ayolo committed
1184
                              "publique_mmtpapier_utl"=>$this->publique_mmtpapier,
slopez's avatar
fixed    
slopez committed
1185
                              "tovalid_utl"=>"none"));
Ayolo's avatar
Ayolo committed
1186
1187
1188
1189
1190
1191
1192
1193

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

Ayolo's avatar
bleh    
Ayolo committed
1195
1196
1197
1198
1199
    if($send_email)
    {
      $this->invalidate ("email");
      $this->send_first_email($this->email,$password);
    }
Feu's avatar
Feu committed
1200

Ayolo's avatar
Ayolo committed
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
    return true;
  }

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

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

Ayolo's avatar
Ayolo committed
1226
1227
1228
1229
    if ( $_utbm )
      $this->nom_ecole_etudiant = "utbm";
    else
      $this->nom_ecole_etudiant = $ecole;
Feu's avatar
Feu committed
1230

Ayolo's avatar
Ayolo committed
1231
1232
1233
1234
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
    $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
1271

Ayolo's avatar
Ayolo committed
1272
1273
1274
    $this->role = $role;
    $this->departement = $departement;
    $this->email_utbm = $emailutbm;
Feu's avatar
Feu committed
1275

Ayolo's avatar
Ayolo committed
1276
1277
1278
1279
1280
1281
    $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
1282

Ayolo's avatar
Ayolo committed
1283
1284
1285
1286
1287
1288
1289
1290
    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
1291
  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
1292
  {
1293
    $this->type="std";
1294

Ayolo's avatar
Ayolo committed
1295
1296
1297
    $this->nom = convertir_nom($nom);
    $this->prenom = convertir_prenom($prenom);

Feu's avatar
Feu committed
1298
1299
    if (CheckEmail($emailutbm,1))
      $this->email_utbm = $emailutbm;
Ayolo's avatar
Ayolo committed
1300

Feu's avatar
Feu committed
1301
1302
1303
    $this->utbm = true;

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

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

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

Ayolo's avatar
bleh    
Ayolo committed
1327
    $this->pass = crypt($password, "ae");
Ayolo's avatar
Ayolo committed
1328
1329
1330
1331
1332
1333
1334
1335
1336
    $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
1337

Ayolo's avatar
Ayolo committed
1338
1339
1340
1341
    $this->droit_image = $droit_image;

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

Ayolo's avatar
Ayolo committed
1398
1399
      $this->role = $role;
      $this->departement = $departement;
Feu's avatar
Feu committed
1400

Ayolo's avatar
Ayolo committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
      $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
1413

Ayolo's avatar
Ayolo committed
1414
1415
1416
1417
1418
1419
1420
1421
    elseif ( $this->email )
      $this->send_first_email($this->email,$password);

    return true;
  }

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

Ayolo's avatar
Ayolo committed
1424
1425
1426
1427
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
    $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
http://ae.utbm.fr/confirm.php?id=" . $this->id . "&hash=" . $this->hash . "

L'équipe info AE";

    $ret = mail($email,
                "[Site AE] Activation de votre compte",
                utf8_decode($body),
                "From: \"AE UTBM\" <ae@utbm.fr>\nReply-To: ae@utbm.fr");

  }

  function send_first_email ( $email, $password )
  {

  $body = "Bonjour,
Votre compte a été crée sur le site de l'AE
1478
".$this->_get_textual_identifier()."
Ayolo's avatar
Ayolo committed
1479
1480
1481
Votre mot de passe: $password

Pour activer votre compte, veuillez vous rendre à l'adresse
Feu's avatar