Subversion Repositories Applications.reseau

Rev

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