Subversion Repositories Applications.reseau

Rev

Rev 52 | Rev 54 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 mathias 1
<?php
2
/**
10 mathias 3
 * Formulaire de remarques assisté
4
 * - envoie un email et écrit dans un fichier log
5 mathias 5
 *
10 mathias 6
 * Utilisation: http://www.tela-botanica.org/widget:reseau:remarques
7 mathias 7
 * Paramètres GET (optionnels):
10 mathias 8
 *   pageSource : URL de la page depuis laquelle l'utilisateur a cliqué sur "nous contacter"
46 mathias 9
 *   	(exemple: http://www.tela-botanica.org/appli:cel)
10
 *   service : service à l'adresse duquel envoyer le rapport, par défaut webmestre,
11
 *   	limité aux clefs de la liste self::$servicesAutorises
12
 *   	(exemple: cel)
13
 *   email (DÉPRÉCIÉ) : adresse à laquelle envoyer le rapport, par défaut webmestre@tela-botanica.org,
14
 *   	limité aux valeurs de la liste self::$servicesAutorises
15
 *   	(exemple: cel_remarques@tela-botanica.org)
51 mathias 16
 *   lang : langage (un squelette correspondant doit exister)
17
 *   	(exemple: en)
7 mathias 18
 *
5 mathias 19
 * @author	Mathias Chouet <mathias@tela-botanica.org>
20
 * @license	GPL v3 <http://www.gnu.org/licenses/gpl.txt>
21
 * @license	CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
22
 * @version	0.1
23
 * @copyright 2014, Tela Botanica (accueil@tela-botanica.org)
24
 */
10 mathias 25
class Remarques extends WidgetCommun {
5 mathias 26
 
27
	const DS = DIRECTORY_SEPARATOR;
28
 
7 mathias 29
	/**
46 mathias 30
	 * service de destination par défaut, si un service ou une adresse non autorisés sont spécifiés,
31
	 * ou si aucun service ni adresse n'est spécifié
7 mathias 32
	 */
46 mathias 33
	const SERVICE_PAR_DEFAUT = 'webmestre';
7 mathias 34
 
35
	/**
36
	 * liste des adresses de destination autorisées
37
	 */
46 mathias 38
	private static $servicesAutorises = array(
39
		'eflore' => 'eflore_remarques@tela-botanica.org',
40
		'pictoflora' => 'pictoflora_remarques@tela-botanica.org',
41
		'identiplante' => 'identiplante_remarques@tela-botanica.org',
42
		'cel' => 'cel_remarques@tela-botanica.org',
43
		'coel' => 'coel_remarques@tela-botanica.org',
44
		'webmestre' => 'webmestre@tela-botanica.org',
53 mathias 45
		'accueil' => 'accueil@tela-botanica.org',
46
		'smartflore' => 'smartflore@tela-botanica.org'
5 mathias 47
	);
48
 
49
	protected $cheminLog;
50
	protected $pageSource;
46 mathias 51
	protected $serviceDestination;
5 mathias 52
	protected $emailDestination;
53
	protected $action;
52 mathias 54
	protected $langue;
55
	protected $langueDefaut;
5 mathias 56
 
57
	protected $description;
58
	protected $gravite;
59
	protected $navigateur;
60
	protected $systeme;
61
	protected $coordonnees;
62
 
63
	public function __construct($config, $parametres) {
64
		parent::__construct($config, $parametres);
7 mathias 65
 
5 mathias 66
		$this->pageSource = 'inconnue';
46 mathias 67
		$this->serviceDestination = self::SERVICE_PAR_DEFAUT;
68
		$this->emailDestination = self::$servicesAutorises[self::SERVICE_PAR_DEFAUT];
5 mathias 69
		$this->action = null;
70
 
71
		$this->description = null;
72
		$this->gravite = null;
73
		$this->navigateur = null;
74
		$this->systeme = null;
75
		$this->coordonnees = null;
76
 
10 mathias 77
		$this->cheminLog = $this->config['remarques']['cheminFichierLog'];
52 mathias 78
		$this->langueDefaut = $this->config['remarques']['langueDefaut'];
5 mathias 79
	}
80
 
81
	/**
82
	 * Méthode appelée par défaut pour charger ce widget
83
	 */
84
	public function executer() {
85
		$this->collecterParametres();
51 mathias 86
		$squelette = dirname(__FILE__) . self::DS . 'squelettes' . self::DS . 'remarques_' . $this->langue . '.tpl.php';
52 mathias 87
		if (! file_exists($squelette)) {
88
			$squelette = dirname(__FILE__) . self::DS . 'squelettes' . self::DS . 'remarques_' . $this->langueDefaut . '.tpl.php';
89
		}
5 mathias 90
 
10 mathias 91
		$widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/remarques/squelettes/css/defaut.css');
92
		$widget['donnees']['url_js'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/remarques/squelettes/js/defaut.js');
46 mathias 93
		$widget['donnees']['service'] = $this->serviceDestination;
5 mathias 94
		$widget['donnees']['page'] = $this->pageSource;
95
		$widget['donnees']['envoye'] = false;
96
 
97
		if ($this->action === 'envoyer') {
98
			$widget['donnees']['envoye'] = true;
99
			// 1) entrée dans le log
100
			try {
101
				$this->log();
102
			} catch (Exception $e) {
103
				echo "Erreur lors de la création de l'entrée dans le fichier log<br/>";
104
			}
105
			// 2) email
106
			try {
107
				$this->email();
108
			} catch (Exception $e) {
109
				echo "Erreur lors de l'envoi de l'email<br/>";
110
			}
111
		}
112
 
113
		$contenu = $this->traiterSquelettePhp($squelette, $widget['donnees']);
114
		$this->envoyer($contenu);
115
	}
116
 
117
	// paramètres du widget en GET et du formulaire en POST
118
	protected function collecterParametres() {
119
		if (isset($_GET['pageSource']) && $_GET['pageSource'] != '') {
120
			$this->pageSource = $_GET['pageSource'];
121
		}
51 mathias 122
		if (isset($_GET['lang']) && $_GET['lang'] != '') {
123
			$this->langue = $_GET['lang'];
124
		} else {
52 mathias 125
			$this->langue = $this->langueDefaut;
51 mathias 126
		}
46 mathias 127
		if (isset($_GET['service']) && in_array($_GET['service'], array_keys(self::$servicesAutorises))) {
128
			$this->serviceDestination = $_GET['service'];
129
			$this->emailDestination = self::$servicesAutorises[$_GET['service']];
130
		} else {
131
			// Rétrocompatibilité (déprécié)
132
			if (isset($_GET['email']) && in_array($_GET['email'], self::$servicesAutorises)) {
133
				$this->emailDestination = $_GET['email'];
134
				$this->serviceDestination = array_search($_GET['email'], self::$servicesAutorises);
135
			}
5 mathias 136
		}
137
		if (isset($_POST['action']) && $_POST['action'] != '') {
138
			$this->action = $_POST['action'];
139
		}
140
		// contenu du formulaire
141
		if (isset($_POST['description']) && $_POST['description'] != '') {
12 mathias 142
			$this->description = stripslashes($_POST['description']);
5 mathias 143
		}
144
		if (isset($_POST['gravite']) && $_POST['gravite'] != '') {
145
			$this->gravite = $_POST['gravite'];
146
		}
147
		if (isset($_POST['navigateur']) && $_POST['navigateur'] != '') {
12 mathias 148
			$this->navigateur = stripslashes($_POST['navigateur']);
5 mathias 149
		}
150
		if (isset($_POST['systeme']) && $_POST['systeme'] != '') {
12 mathias 151
			$this->systeme = stripslashes($_POST['systeme']);
5 mathias 152
		}
153
		if (isset($_POST['coordonnees']) && $_POST['coordonnees'] != '') {
12 mathias 154
			$this->coordonnees = stripslashes($_POST['coordonnees']);
5 mathias 155
		}
156
	}
157
 
158
	// ajoute une entrée au log
159
	protected function log() {
15 jpm 160
		$contenu = ''.
161
			date("Y-m-d h:i:s") . "\n".
46 mathias 162
			"Pour: " . $this->serviceDestination . " <" . $this->emailDestination . ">\n".
15 jpm 163
			"Page: " . $this->pageSource . "\n".
164
			"User agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n".
165
			"Contributeur: " . $this->coordonnees . "\n".
166
			"Gravité: " . $this->gravite . "\n".
167
			"Navigateur: " . $this->navigateur . "\n".
168
			"Système: " . $this->systeme . "\n".
169
			"Description:\n  " . str_replace("\n", "\n  ", $this->description).
170
			"\n\n-----------------------------------------------------------------------\n\n";
5 mathias 171
 
172
		file_put_contents($this->cheminLog, $contenu, FILE_APPEND);
173
	}
174
 
175
	// envoie un email
176
	protected function email() {
15 jpm 177
		$contenu = ''.
178
			date("Y-m-d h:i:s") . "\n".
46 mathias 179
			"Pour: " . $this->serviceDestination . " <" . $this->emailDestination . ">\n".
15 jpm 180
			"Page: " . $this->pageSource . "\n".
181
			"User agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n".
182
			"Contributeur: " . $this->coordonnees . "\n".
183
			"Gravité: " . $this->gravite . "\n".
184
			"Navigateur: " . $this->navigateur . "\n".
185
			"Système: " . $this->systeme . "\n".
186
			"Description:\n  " . str_replace("\n", "\n  ", $this->description);
5 mathias 187
 
15 jpm 188
		$entetes = 'Content-Type: text/plain; charset="utf-8" '.
189
			'Content-Transfer-Encoding: 8bit';
7 mathias 190
 
15 jpm 191
		$sujet = substr($this->description, 0, 25).'... '.
192
			'('.($this->coordonnees == '' ? 'inconnu' : $this->coordonnees).')';
5 mathias 193
 
194
		// mode charlot - mais y a rien dans le framework pour faire mieux
7 mathias 195
		mail($this->emailDestination, $sujet, $contenu, $entetes);
5 mathias 196
	}
197
}
198
?>