Subversion Repositories Applications.reseau

Rev

Rev 53 | Rev 103 | 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,
54 mathias 11
 *   	limité aux clefs de la liste $this->servicesAutorises
46 mathias 12
 *   	(exemple: cel)
13
 *   email (DÉPRÉCIÉ) : adresse à laquelle envoyer le rapport, par défaut webmestre@tela-botanica.org,
54 mathias 14
 *   	limité aux valeurs de la liste $this->servicesAutorises
46 mathias 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
 
54 mathias 35
	/** liste des adresses de destination autorisées (à régler dans la config) */
36
	protected $servicesAutorises;
5 mathias 37
 
38
	protected $cheminLog;
39
	protected $pageSource;
46 mathias 40
	protected $serviceDestination;
5 mathias 41
	protected $emailDestination;
42
	protected $action;
52 mathias 43
	protected $langue;
44
	protected $langueDefaut;
5 mathias 45
 
46
	protected $description;
47
	protected $gravite;
48
	protected $navigateur;
49
	protected $systeme;
50
	protected $coordonnees;
51
 
52
	public function __construct($config, $parametres) {
53
		parent::__construct($config, $parametres);
7 mathias 54
 
54 mathias 55
		$this->servicesAutorises = $this->config['remarques-services'];
56
 
5 mathias 57
		$this->pageSource = 'inconnue';
46 mathias 58
		$this->serviceDestination = self::SERVICE_PAR_DEFAUT;
54 mathias 59
		$this->emailDestination = $this->servicesAutorises[self::SERVICE_PAR_DEFAUT];
5 mathias 60
		$this->action = null;
61
 
62
		$this->description = null;
63
		$this->gravite = null;
64
		$this->navigateur = null;
65
		$this->systeme = null;
66
		$this->coordonnees = null;
67
 
10 mathias 68
		$this->cheminLog = $this->config['remarques']['cheminFichierLog'];
52 mathias 69
		$this->langueDefaut = $this->config['remarques']['langueDefaut'];
5 mathias 70
	}
71
 
72
	/**
73
	 * Méthode appelée par défaut pour charger ce widget
74
	 */
75
	public function executer() {
76
		$this->collecterParametres();
51 mathias 77
		$squelette = dirname(__FILE__) . self::DS . 'squelettes' . self::DS . 'remarques_' . $this->langue . '.tpl.php';
52 mathias 78
		if (! file_exists($squelette)) {
79
			$squelette = dirname(__FILE__) . self::DS . 'squelettes' . self::DS . 'remarques_' . $this->langueDefaut . '.tpl.php';
80
		}
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
		}
51 mathias 113
		if (isset($_GET['lang']) && $_GET['lang'] != '') {
114
			$this->langue = $_GET['lang'];
115
		} else {
52 mathias 116
			$this->langue = $this->langueDefaut;
51 mathias 117
		}
54 mathias 118
		if (isset($_GET['service']) && in_array($_GET['service'], array_keys($this->servicesAutorises))) {
46 mathias 119
			$this->serviceDestination = $_GET['service'];
54 mathias 120
			$this->emailDestination = $this->servicesAutorises[$_GET['service']];
46 mathias 121
		} else {
122
			// Rétrocompatibilité (déprécié)
54 mathias 123
			if (isset($_GET['email']) && in_array($_GET['email'], $this->servicesAutorises)) {
46 mathias 124
				$this->emailDestination = $_GET['email'];
54 mathias 125
				$this->serviceDestination = array_search($_GET['email'], $this->servicesAutorises);
46 mathias 126
			}
5 mathias 127
		}
128
		if (isset($_POST['action']) && $_POST['action'] != '') {
129
			$this->action = $_POST['action'];
130
		}
131
		// contenu du formulaire
132
		if (isset($_POST['description']) && $_POST['description'] != '') {
12 mathias 133
			$this->description = stripslashes($_POST['description']);
5 mathias 134
		}
135
		if (isset($_POST['gravite']) && $_POST['gravite'] != '') {
136
			$this->gravite = $_POST['gravite'];
137
		}
138
		if (isset($_POST['navigateur']) && $_POST['navigateur'] != '') {
12 mathias 139
			$this->navigateur = stripslashes($_POST['navigateur']);
5 mathias 140
		}
141
		if (isset($_POST['systeme']) && $_POST['systeme'] != '') {
12 mathias 142
			$this->systeme = stripslashes($_POST['systeme']);
5 mathias 143
		}
144
		if (isset($_POST['coordonnees']) && $_POST['coordonnees'] != '') {
12 mathias 145
			$this->coordonnees = stripslashes($_POST['coordonnees']);
5 mathias 146
		}
147
	}
148
 
149
	// ajoute une entrée au log
150
	protected function log() {
15 jpm 151
		$contenu = ''.
152
			date("Y-m-d h:i:s") . "\n".
46 mathias 153
			"Pour: " . $this->serviceDestination . " <" . $this->emailDestination . ">\n".
15 jpm 154
			"Page: " . $this->pageSource . "\n".
155
			"User agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n".
156
			"Contributeur: " . $this->coordonnees . "\n".
157
			"Gravité: " . $this->gravite . "\n".
158
			"Navigateur: " . $this->navigateur . "\n".
159
			"Système: " . $this->systeme . "\n".
160
			"Description:\n  " . str_replace("\n", "\n  ", $this->description).
161
			"\n\n-----------------------------------------------------------------------\n\n";
5 mathias 162
 
163
		file_put_contents($this->cheminLog, $contenu, FILE_APPEND);
164
	}
165
 
166
	// envoie un email
167
	protected function email() {
15 jpm 168
		$contenu = ''.
169
			date("Y-m-d h:i:s") . "\n".
46 mathias 170
			"Pour: " . $this->serviceDestination . " <" . $this->emailDestination . ">\n".
15 jpm 171
			"Page: " . $this->pageSource . "\n".
172
			"User agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n".
173
			"Contributeur: " . $this->coordonnees . "\n".
174
			"Gravité: " . $this->gravite . "\n".
175
			"Navigateur: " . $this->navigateur . "\n".
176
			"Système: " . $this->systeme . "\n".
177
			"Description:\n  " . str_replace("\n", "\n  ", $this->description);
5 mathias 178
 
15 jpm 179
		$entetes = 'Content-Type: text/plain; charset="utf-8" '.
180
			'Content-Transfer-Encoding: 8bit';
7 mathias 181
 
15 jpm 182
		$sujet = substr($this->description, 0, 25).'... '.
183
			'('.($this->coordonnees == '' ? 'inconnu' : $this->coordonnees).')';
5 mathias 184
 
185
		// mode charlot - mais y a rien dans le framework pour faire mieux
7 mathias 186
		mail($this->emailDestination, $sujet, $contenu, $entetes);
5 mathias 187
	}
188
}
189
?>