Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1140 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1140 Rev 1300
1
<?php
1
<?php
2
class Utilisateurs {
2
class Utilisateurs {
3
	const TPL_URL_WS_ANNUAIRE = 'http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';
3
	const TPL_URL_WS_ANNUAIRE = 'https://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';
4
 
4
 
5
	private $courriels = array();
5
	private $courriels = array();
6
	private $identites = array();
6
	private $identites = array();
7
	private $clientRest = null;
7
	private $clientRest = null;
8
 
8
 
9
	/**
9
	/**
10
	* Prend en paramêtre un tableau de courriels.
10
	* Prend en paramêtre un tableau de courriels.
11
	*
11
	*
12
	* @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
12
	* @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
13
	*/
13
	*/
14
	public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
14
	public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
15
		$this->courriels = $courriels;
15
		$this->courriels = $courriels;
16
		$this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
16
		$this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
17
	}
17
	}
18
 
18
 
19
	public function setCourriels($courriels) {
19
	public function setCourriels($courriels) {
20
		$this->courriels = $courriels;
20
		$this->courriels = $courriels;
21
	}
21
	}
22
	/**
22
	/**
23
	 * Lance l'interrogation du service de l'annuaire.
23
	 * Lance l'interrogation du service de l'annuaire.
24
	 *
24
	 *
25
	 * @return null.
25
	 * @return null.
26
	 */
26
	 */
27
	public function chargerIdentites() {
27
	public function chargerIdentites() {
28
		$this->identites = $this->getIdentites($this->courriels);
28
		$this->identites = $this->getIdentites($this->courriels);
29
	}
29
	}
30
 
30
 
31
	/**
31
	/**
32
	* Retourne après avoir interrogé un service de l'annuaire, les intitulés correspondant aux
32
	* Retourne après avoir interrogé un service de l'annuaire, les intitulés correspondant aux
33
	* courriels des utilisateurs.
33
	* courriels des utilisateurs.
34
	*
34
	*
35
	* @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
35
	* @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
36
	* afficher, false en cas d'erreur ou de résultat vide.
36
	* afficher, false en cas d'erreur ou de résultat vide.
37
	*/
37
	*/
38
	public function getIntitules() {
38
	public function getIntitules() {
39
		$this->chargerIdentites();
39
		$this->chargerIdentites();
40
		if (! $this->identites) return false;
40
		if (! $this->identites) return false;
41
 
41
 
42
		$intitules = array();
42
		$intitules = array();
43
		foreach ($this->identites as $courriel => $infos) {
43
		foreach ($this->identites as $courriel => $infos) {
44
			$intitules[$courriel] = $infos['intitule'];
44
			$intitules[$courriel] = $infos['intitule'];
45
		}
45
		}
46
		return $intitules;
46
		return $intitules;
47
	}
47
	}
48
 
48
 
49
	/**
49
	/**
50
	 * Retourne un intitulé en fonction d'un courriel.
50
	 * Retourne un intitulé en fonction d'un courriel.
51
	 *
51
	 *
52
	 * @return String l'intitulé de l'utilisateur ou une chaine vide en cas de problème.
52
	 * @return String l'intitulé de l'utilisateur ou une chaine vide en cas de problème.
53
	 */
53
	 */
54
	public function getIntitule($courriel) {
54
	public function getIntitule($courriel) {
55
		if ($this->contenirCourriel($courriel)) {
55
		if ($this->contenirCourriel($courriel)) {
56
			return $this->identites[$courriel]['intitule'];
56
			return $this->identites[$courriel]['intitule'];
57
		}
57
		}
58
		return '';
58
		return '';
59
	}
59
	}
60
 
60
 
61
	/**
61
	/**
62
	 * Retourne l'identifiant de l'utilisateur en fonction d'un courriel.
62
	 * Retourne l'identifiant de l'utilisateur en fonction d'un courriel.
63
	 *
63
	 *
64
	 * @return String l'id de l'utilisateur ou une chaine vide en cas de problème.
64
	 * @return String l'id de l'utilisateur ou une chaine vide en cas de problème.
65
	 */
65
	 */
66
	public function getId($courriel) {
66
	public function getId($courriel) {
67
		if ($this->contenirCourriel($courriel)) {
67
		if ($this->contenirCourriel($courriel)) {
68
			return $this->identites[$courriel]['id'];
68
			return $this->identites[$courriel]['id'];
69
		}
69
		}
70
		return '';
70
		return '';
71
	}
71
	}
72
 
72
 
73
	private function contenirCourriel($courriel) {
73
	private function contenirCourriel($courriel) {
74
		return ($this->identites && isset($this->identites[$courriel])) ? true : false;
74
		return ($this->identites && isset($this->identites[$courriel])) ? true : false;
75
	}
75
	}
76
 
76
 
77
	private function getIdentites($courriels) {
77
	private function getIdentites($courriels) {
78
		// consulterServiceAnnuaire
78
		// consulterServiceAnnuaire
79
		$courriels = array_unique($courriels);
79
		$courriels = array_unique($courriels);
80
		
80
		
81
		// Trop de courriels dans l'url fait planter la requete
81
		// Trop de courriels dans l'url fait planter la requete
82
		// à cause des limites de taille d'url 
82
		// à cause des limites de taille d'url 
83
		// (150 semble être un bon compromis)
83
		// (150 semble être un bon compromis)
84
		$courriels_spl = array_chunk($courriels, 150);
84
		$courriels_spl = array_chunk($courriels, 150);
85
		$utilisateursInfos = array();
85
		$utilisateursInfos = array();
86
		foreach($courriels_spl as $courriels_st) {
86
		foreach($courriels_spl as $courriels_st) {
87
			$utilisateursInfosSt = json_decode($this->clientRest->consulter(sprintf(self::TPL_URL_WS_ANNUAIRE,
87
			$utilisateursInfosSt = json_decode($this->clientRest->consulter(sprintf(self::TPL_URL_WS_ANNUAIRE,
88
																implode(',', $courriels_st))), true);
88
																implode(',', $courriels_st))), true);
89
			$utilisateursInfos = array_merge($utilisateursInfos, $utilisateursInfosSt);
89
			$utilisateursInfos = array_merge($utilisateursInfos, $utilisateursInfosSt);
90
		}
90
		}
91
		return self::extraireIdentites($utilisateursInfos, $this->courriels);
91
		return self::extraireIdentites($utilisateursInfos, $this->courriels);
92
	}
92
	}
93
 
93
 
94
	static function extraireIdentites($utilisateursInfos, $courriels) {
94
	static function extraireIdentites($utilisateursInfos, $courriels) {
95
		$identites = array();
95
		$identites = array();
96
		foreach ($courriels as $courriel) {
96
		foreach ($courriels as $courriel) {
97
			$info = array('id' => null, 'intitule' => '');
97
			$info = array('id' => null, 'intitule' => '');
98
			if (isset($utilisateursInfos[$courriel])) {
98
			if (isset($utilisateursInfos[$courriel])) {
99
				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
99
				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
100
				$info['id'] = $utilisateursInfos[$courriel]['id'];
100
				$info['id'] = $utilisateursInfos[$courriel]['id'];
101
			} else {
101
			} else {
102
				$info['intitule'] = self::tronquerCourriel($courriel);
102
				$info['intitule'] = self::tronquerCourriel($courriel);
103
			}
103
			}
104
			$identites[$courriel] = $info;
104
			$identites[$courriel] = $info;
105
		}
105
		}
106
		return $identites;
106
		return $identites;
107
	}
107
	}
108
 
108
 
109
	static function tronquerCourriel($courriel) {
109
	static function tronquerCourriel($courriel) {
110
		return str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
110
		return str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
111
	}
111
	}
112
}
112
}
113
?>
113
?>