Subversion Repositories Applications.reseau

Rev

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