Subversion Repositories Applications.reseau

Rev

Rev 46 | Rev 52 | 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',
45
		'accueil' => 'accueil@tela-botanica.org'
5 mathias 46
	);
47
 
48
	protected $cheminLog;
49
	protected $pageSource;
46 mathias 50
	protected $serviceDestination;
5 mathias 51
	protected $emailDestination;
52
	protected $action;
51 mathias 53
	protected $lang;
5 mathias 54
 
55
	protected $description;
56
	protected $gravite;
57
	protected $navigateur;
58
	protected $systeme;
59
	protected $coordonnees;
60
 
61
	public function __construct($config, $parametres) {
62
		parent::__construct($config, $parametres);
7 mathias 63
 
5 mathias 64
		$this->pageSource = 'inconnue';
46 mathias 65
		$this->serviceDestination = self::SERVICE_PAR_DEFAUT;
66
		$this->emailDestination = self::$servicesAutorises[self::SERVICE_PAR_DEFAUT];
5 mathias 67
		$this->action = null;
68
 
69
		$this->description = null;
70
		$this->gravite = null;
71
		$this->navigateur = null;
72
		$this->systeme = null;
73
		$this->coordonnees = null;
74
 
10 mathias 75
		$this->cheminLog = $this->config['remarques']['cheminFichierLog'];
5 mathias 76
	}
77
 
78
	/**
79
	 * Méthode appelée par défaut pour charger ce widget
80
	 */
81
	public function executer() {
82
		$this->collecterParametres();
51 mathias 83
		$squelette = dirname(__FILE__) . self::DS . 'squelettes' . self::DS . 'remarques_' . $this->langue . '.tpl.php';
5 mathias 84
 
10 mathias 85
		$widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/remarques/squelettes/css/defaut.css');
86
		$widget['donnees']['url_js'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/remarques/squelettes/js/defaut.js');
46 mathias 87
		$widget['donnees']['service'] = $this->serviceDestination;
5 mathias 88
		$widget['donnees']['page'] = $this->pageSource;
89
		$widget['donnees']['envoye'] = false;
90
 
91
		if ($this->action === 'envoyer') {
92
			$widget['donnees']['envoye'] = true;
93
			// 1) entrée dans le log
94
			try {
95
				$this->log();
96
			} catch (Exception $e) {
97
				echo "Erreur lors de la création de l'entrée dans le fichier log<br/>";
98
			}
99
			// 2) email
100
			try {
101
				$this->email();
102
			} catch (Exception $e) {
103
				echo "Erreur lors de l'envoi de l'email<br/>";
104
			}
105
		}
106
 
107
		$contenu = $this->traiterSquelettePhp($squelette, $widget['donnees']);
108
		$this->envoyer($contenu);
109
	}
110
 
111
	// paramètres du widget en GET et du formulaire en POST
112
	protected function collecterParametres() {
113
		if (isset($_GET['pageSource']) && $_GET['pageSource'] != '') {
114
			$this->pageSource = $_GET['pageSource'];
115
		}
51 mathias 116
		if (isset($_GET['lang']) && $_GET['lang'] != '') {
117
			$this->langue = $_GET['lang'];
118
		} else {
119
			$this->langue = "fr"; // @TODO paramétrer la langue par défaut ?
120
		}
46 mathias 121
		if (isset($_GET['service']) && in_array($_GET['service'], array_keys(self::$servicesAutorises))) {
122
			$this->serviceDestination = $_GET['service'];
123
			$this->emailDestination = self::$servicesAutorises[$_GET['service']];
124
		} else {
125
			// Rétrocompatibilité (déprécié)
126
			if (isset($_GET['email']) && in_array($_GET['email'], self::$servicesAutorises)) {
127
				$this->emailDestination = $_GET['email'];
128
				$this->serviceDestination = array_search($_GET['email'], self::$servicesAutorises);
129
			}
5 mathias 130
		}
131
		if (isset($_POST['action']) && $_POST['action'] != '') {
132
			$this->action = $_POST['action'];
133
		}
134
		// contenu du formulaire
135
		if (isset($_POST['description']) && $_POST['description'] != '') {
12 mathias 136
			$this->description = stripslashes($_POST['description']);
5 mathias 137
		}
138
		if (isset($_POST['gravite']) && $_POST['gravite'] != '') {
139
			$this->gravite = $_POST['gravite'];
140
		}
141
		if (isset($_POST['navigateur']) && $_POST['navigateur'] != '') {
12 mathias 142
			$this->navigateur = stripslashes($_POST['navigateur']);
5 mathias 143
		}
144
		if (isset($_POST['systeme']) && $_POST['systeme'] != '') {
12 mathias 145
			$this->systeme = stripslashes($_POST['systeme']);
5 mathias 146
		}
147
		if (isset($_POST['coordonnees']) && $_POST['coordonnees'] != '') {
12 mathias 148
			$this->coordonnees = stripslashes($_POST['coordonnees']);
5 mathias 149
		}
150
	}
151
 
152
	// ajoute une entrée au log
153
	protected function log() {
15 jpm 154
		$contenu = ''.
155
			date("Y-m-d h:i:s") . "\n".
46 mathias 156
			"Pour: " . $this->serviceDestination . " <" . $this->emailDestination . ">\n".
15 jpm 157
			"Page: " . $this->pageSource . "\n".
158
			"User agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n".
159
			"Contributeur: " . $this->coordonnees . "\n".
160
			"Gravité: " . $this->gravite . "\n".
161
			"Navigateur: " . $this->navigateur . "\n".
162
			"Système: " . $this->systeme . "\n".
163
			"Description:\n  " . str_replace("\n", "\n  ", $this->description).
164
			"\n\n-----------------------------------------------------------------------\n\n";
5 mathias 165
 
166
		file_put_contents($this->cheminLog, $contenu, FILE_APPEND);
167
	}
168
 
169
	// envoie un email
170
	protected function email() {
15 jpm 171
		$contenu = ''.
172
			date("Y-m-d h:i:s") . "\n".
46 mathias 173
			"Pour: " . $this->serviceDestination . " <" . $this->emailDestination . ">\n".
15 jpm 174
			"Page: " . $this->pageSource . "\n".
175
			"User agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n".
176
			"Contributeur: " . $this->coordonnees . "\n".
177
			"Gravité: " . $this->gravite . "\n".
178
			"Navigateur: " . $this->navigateur . "\n".
179
			"Système: " . $this->systeme . "\n".
180
			"Description:\n  " . str_replace("\n", "\n  ", $this->description);
5 mathias 181
 
15 jpm 182
		$entetes = 'Content-Type: text/plain; charset="utf-8" '.
183
			'Content-Transfer-Encoding: 8bit';
7 mathias 184
 
15 jpm 185
		$sujet = substr($this->description, 0, 25).'... '.
186
			'('.($this->coordonnees == '' ? 'inconnu' : $this->coordonnees).')';
5 mathias 187
 
188
		// mode charlot - mais y a rien dans le framework pour faire mieux
7 mathias 189
		mail($this->emailDestination, $sujet, $contenu, $entetes);
5 mathias 190
	}
191
}
192
?>