Subversion Repositories Sites.tela-botanica.org

Rev

Rev 376 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
376 jpm 1
<?php
2
 
3
abstract class aModule {
4
 
5
	/*** Constantes : ***/
6
 
7
	const TPL_NULL = 'NULL';
8
	const TPL_PHP = 'PHP';
9
	const TPL_PHP_MAIL = 'PHP_MAIL';
10
	const TPL_IT = 'IT';
11
	const TPL_FPDI = 'FPDI';
12
	const SORTIE_NULL = 'NULL';
13
	const SORTIE_EXIT = 'EXIT';
14
	const SORTIE_MAIL_SMTP = 'email';
15
	const SORTIE_HTML = 'html';
16
	const SORTIE_EXIT_HTML = 'exit.html';
17
	const SORTIE_XML = 'xml';
18
	const SORTIE_PDF = 'pdf';
19
 
20
	/*** Attributs : ***/
21
	private $registre;
22
	private $cache;
23
	private $actions_chainees = null;
24
 
25
	/*** Constructeur : ***/
26
 
27
	public function __construct()
28
    {
29
    	$this->registre = Registre::getInstance();
30
		$this->registre->set('squelette_moteur', aModule::TPL_PHP);
31
		$this->registre->set('format', aModule::SORTIE_HTML);
32
		$applette_dossier = preg_replace('/([a-z])([A-Z])/', '$1_$2', get_class($this));
33
 
34
		// Nous créons automatiquement les chemins vers les différents dossier de l'application
35
		$this->registre->set('chemin_module', SC_CHEMIN_MODULE.strtolower($applette_dossier).DIRECTORY_SEPARATOR);
36
		$this->registre->set('chemin_module_config', $this->registre->get('chemin_module').'configuration'.DIRECTORY_SEPARATOR);
37
		$this->registre->set('chemin_module_squelette', $this->registre->get('chemin_module').'squelettes'.DIRECTORY_SEPARATOR);
38
 
39
		// Nous définissons si oui ou non le cache sera utilisé
40
		if (defined('SC_BOOL_STOCKAGE_CACHE')) {
41
			$this->cache_bool = SC_BOOL_STOCKAGE_CACHE;
42
		} else {
43
			$this->cache_bool = false;
44
			$e = 'La constante SC_BOOL_STOCKAGE_CACHE est indéfinie. Le cache a été désactivé!';
45
			trigger_error($e, E_USER_WARNING);
46
		}
47
 
48
		// Nous chargeons le fichier de config de l'appli s'il existe
49
		if ($this->getConfigFichier()) {
50
			require_once $this->getConfigFichier();
51
		}
52
    }
53
 
54
    /*** Accesseurs : ***/
55
    public function getRegistre()
56
    {
57
    	return $this->registre;
58
    }
59
 
60
    public function setCacheBool($cb)
61
    {
62
    	return $this->cache_bool = $cb;
63
    }
64
 
65
    /*** Méthodes : ***/
66
 
67
    public function getCacheId()
68
    {
69
    	if ($this->getActionNom() != null) {
70
    		$methode_cache_id = 'getCacheId'.$this->getActionNom();
71
    		if (method_exists($this, $methode_cache_id) && !is_null($this->$methode_cache_id())) {
72
    			return call_user_func(array($this, $methode_cache_id));
73
    		}
74
    	}
75
    	return null;
76
    }
77
 
78
    public function getCacheFichier()
79
    {
80
    	$fichier_cache = EF_CHEMIN_STOCKAGE_CACHE.$this->getCacheId();
81
    	if (!is_null($this->getCacheDuree())) {
82
    		$fichier_cache .= '_['.$this->getCacheDuree().']';
83
    	}
381 jpm 84
    	$fichier_cache .= '.cache.'.$this->getRegistre()->get('format');
376 jpm 85
 
86
    	return $fichier_cache;
87
    }
88
 
89
    public function getCacheDuree()
90
    {
91
    	$dlc = null;
381 jpm 92
    	$methode_cache_dlc = 'getCacheDlc'.$this->getActionNom();
376 jpm 93
    	if (method_exists($this, $methode_cache_dlc) && !is_null($this->$methode_cache_dlc())) {
94
    		$dlc = call_user_func(array($this, $methode_cache_dlc)); // dlc en seconde
95
    	}
96
    	return $dlc;
97
    }
98
 
99
    public function getActionNom()
100
    {
101
		if ($this->getRegistre()->get('action') != null) {
381 jpm 102
			return $action = str_replace(' ', '', ucwords(str_replace('_', ' ', $this->getRegistre()->get('action'))));
376 jpm 103
		}
104
		return null;
105
    }
106
 
107
    public function setDebogage($d, $e = E_USER_NOTICE)
108
    {
109
    	if (is_array($d) || is_object($d)) {
110
    		trigger_error(print_r($d, true), $e);
111
    	} else {
112
    		trigger_error($d, $e);
113
    	}
114
    }
115
 
116
    public function setChrono($balise)
117
    {
118
		// Mesure du temps d'éxecution
119
		$class = new ReflectionClass($this);
120
		$GLOBALS['_SCRIPT_']['chrono']->setTemps(array($class->getName().'-'.$balise => microtime()));
121
    }
122
 
123
    public function getSqueletteFichier()
124
    {
125
		// Par défaut le nom du fichier de squelette est construit à partir du nom de l'action.
126
		if (is_null($this->getRegistre()->get('squelette_fichier'))) {
127
			$this->getRegistre()->set('squelette_fichier', $this->getRegistre()->get('action'));
128
		}
129
 
130
		// Nous recherchons s'il existe un squelette spécifique à la distribution
131
		$fichier_tpl_defaut =  	$this->getRegistre()->get('chemin_module_squelette').
132
								$this->getRegistre()->get('squelette_fichier').'.tpl.'.$this->registre->get('format');
133
		if (defined('SC_DISTRIBUTION') && SC_DISTRIBUTION != ''&& !is_null(SC_DISTRIBUTION)) {
134
			$fichier_tpl_projet =  	$this->getRegistre()->get('chemin_module_squelette').
135
									strtolower(SC_DISTRIBUTION).DIRECTORY_SEPARATOR.
136
									$this->getRegistre()->get('squelette_fichier').'.tpl.'.$this->getRegistre()->get('format');
137
			if (file_exists($fichier_tpl_projet)) {
138
				return $fichier_tpl_projet;
139
			}
140
		}
141
		if (file_exists($fichier_tpl_defaut)) {
142
			return $fichier_tpl_defaut;
143
		}
144
		return false;
145
    }
146
 
147
    public function getConfigFichier()
148
    {
149
		$fichier_conf_defaut = $this->getRegistre()->get('chemin_module_config').'config.inc.php';
150
		if (defined('SC_DISTRIBUTION') && SC_DISTRIBUTION != '') {
151
 
152
			$fichier_conf_projet = 	$this->getRegistre()->get('chemin_module_config').
153
									'config.'.strtolower(SC_DISTRIBUTION).'.inc.php';
154
			if (file_exists($fichier_conf_projet)) {
155
				return $fichier_conf_projet;
156
			}
157
		}
158
		if (file_exists($fichier_conf_defaut)) {
159
			return $fichier_conf_defaut;
160
		}
161
		return false;
162
    }
163
 
164
    public function getActionsChainees()
165
    {
166
    	// Création du tableau si nécessaire
167
		if (is_null($this->actions_chainees)) {
168
			$this->actions_chainees = array();
169
			$this->actions_chainees[] = $this->getRegistre()->get('action');
170
		}
171
		return $this->actions_chainees;
172
    }
173
 
174
    public function poursuivreVers($action)
175
    {
176
		// Ajout de l'action suivante
177
   		$this->actions_chainees[] = $action;
178
    }
179
 
180
	public function traiterAction()
181
    {
182
		// Gestion des actions chainées si nécessaire
183
		$sortie = '';
184
		$module_nom = strtolower(get_class($this));
185
		$i = 0;
186
		while ($i < count($this->getActionsChainees())) {
187
			// Initialisation de variables
188
			$actions = $this->getActionsChainees();
189
			$action = $actions[$i++];
190
 
191
			// Remise à défaut des valeurs du Registre pour la prochaine action
192
			$this->getRegistre()->set('action', $action);
193
			$this->getRegistre()->set('format', aModule::SORTIE_HTML);
194
			$this->getRegistre()->set('squelette_fichier', null);
195
			$this->getRegistre()->set('squelette_moteur', aModule::TPL_PHP);
196
 
197
			// Gestion du multilinguisme
381 jpm 198
			/*
199
			if (isset($GLOBALS['_EF_']['i18n'][$module_nom][$action]) || isset($GLOBALS['_EF_']['i18n']['_defaut_'][$action])) {
200
				$this->getRegistre()->set('module_i18n', $GLOBALS['_EF_']['i18n']['_defaut_']['general']);
201
				if (isset($GLOBALS['_EF_']['i18n']['_defaut_'][$action])) {
202
					$this->getRegistre()->set('module_i18n', $GLOBALS['_EF_']['i18n']['_defaut_'][$action]);
203
				}
204
				if (isset($GLOBALS['_EF_']['i18n'][$module_nom][$action])) {
205
					$this->getRegistre()->set('module_i18n', $GLOBALS['_EF_']['i18n'][$module_nom][$action]);
206
				}
207
 
208
				$aso_donnees = $this->getRegistre()->get('squelette_donnees');
209
				$aso_donnees['i18n'] = $this->getRegistre()->get('module_i18n');
210
				$this->getRegistre()->set('squelette_donnees', $aso_donnees);
211
 
212
			}
213
			*/
376 jpm 214
			// Exécution du rendu de l'action
215
			$sortie .= $this->traiterRendu($action);
216
		}
217
 
218
		// Gestion de la sortie finale
219
		return $sortie;
220
    }
221
 
222
	private function traiterRendu()
223
    {
224
		// Gestion du cache : avant toute chose, retour du cache s'il existe
225
    	if ($this->cache_bool) {
226
			//$this->setDebogage($this->getCacheId());
227
			if (!is_null($this->getCacheId())) {
228
				if (file_exists($this->getCacheFichier())) {
229
					// Gestion de la DLC
230
					if (	(is_null($this->getCacheDuree())) ||
231
							(!is_null($this->getCacheDuree()) && (time() < (filemtime($this->getCacheFichier()) + $this->getCacheDuree())))) {
232
						$this->registre->set('sortie', file_get_contents($this->getCacheFichier()));
233
						return $this->traiterSortie();
234
					}
235
				}
236
			}
237
		}
238
 
239
		// Attribution si nécessaire de l'encodage de sortie
240
		if (!$this->registre->get('charset')) {
241
			$this->registre->set('charset', 'ISO-8859-1');
242
		}
243
 
244
    	// Execution de l'action
245
		$methode_action = 'executer';
246
		if ($this->getActionNom() != null) {
247
			$methode_action .= $this->getActionNom();
248
		}
249
    	if (method_exists($this, $methode_action)) {
250
    		call_user_func(array($this, $methode_action));
381 jpm 251
    	} else if (!$this->getSqueletteFichier()) {
252
    		// Tentative de recherche de l'action dans le module des Communs
253
    		$fichier_communs = SC_CHEMIN_MODULE.'communs'.DIRECTORY_SEPARATOR.'Communs.class.php';
254
    		if (file_exists($fichier_communs)) {
255
    			include_once $fichier_communs;
256
				$Commun = new Communs();
257
				$sortie_commun = $Commun->traiterAction();
258
    		}
259
			if (isset($sortie_commun)) {
260
				return $sortie_commun;
261
			} else {
262
    			$e = 'Aucun squelette ou méthode "'.$methode_action.'" n\'existe pour l\'action '.$this->registre->get('action');
263
    			trigger_error($e, E_USER_WARNING);
264
    			return null;
265
			}
376 jpm 266
    	}
267
 
268
    	// Gestion du rendu en fonction du type de template
269
    	switch($this->registre->get('squelette_moteur')) {
270
			case aModule::TPL_PHP_MAIL :
271
			case aModule::TPL_PHP :
272
				$Squelette = new SquelettePhp();
381 jpm 273
				$Squelette->set($this->getRegistre()->get('squelette_donnees'));
376 jpm 274
				if ($this->getSqueletteFichier()) {
275
					$sortie = $Squelette->analyser($this->getSqueletteFichier());
276
					if ($this->registre->get('squelette_moteur') == aModule::TPL_PHP_MAIL) {
277
						// Traitement spécial pour les mails
278
						if (preg_match_all('/<(html|txt|file)(?:>(.*?)<\/\\1>|\s+src="(.*)"\s+type="(.*)"\s*\/>\s*$)/ism', $sortie, $decoupage, PREG_SET_ORDER)) {
279
							$this->registre->set('sortie_mail_mime', $decoupage);
280
						}
281
					} else {
282
						$this->registre->set('sortie', $sortie);
283
					}
284
				} else {
285
					$e = 'Action : '.$this->getRegistre()->get('action').' fichier de squelette introuvable !';
286
					trigger_error($e, E_USER_WARNING);
287
					return null;
288
				}
289
				break;
290
    		case aModule::TPL_NULL :
291
    			// Nous ne faisons rien, nous passons à la gestion du type de sortie
292
    			break;
293
    		default :
294
    			trigger_error('Moteur de squelette inconnu', E_USER_WARNING);
295
    			return null;
296
    	}
297
 
298
    	// Gestion du cache : écriture du fichier
299
		if ($this->cache_bool) {
300
			if (!is_null($this->getCacheId())) {
381 jpm 301
				if (!file_put_contents($this->getCacheFichier(), $this->getRegistre()->get('sortie'))) {
302
					$e = 'Écriture du fichier de cache impossible : '.$this->getCacheFichier();
303
					trigger_error($e, E_USER_WARNING);
304
				}
376 jpm 305
			 }
306
		}
307
 
308
		// Gestion du format de sortie
309
		return $this->traiterSortie();
310
    }
311
 
312
    private function traiterSortie()
313
    {
314
    	switch($this->getRegistre()->get('format')) {
315
			case aModule::SORTIE_HTML :
316
				// +--------------------------------------------------------------------------------------------------+
317
				// A FAIRE : Gestion des statistiques
318
 
319
				// +--------------------------------------------------------------------------------------------------+
320
				// Gestion du cache : affichage de la date du cache
321
				if ($this->cache_bool) {
322
					if (!is_null($this->getCacheId())) {
323
						$e = 	'Cache, généré le '.date('D d M Y à H:i:s', filemtime($this->getCacheFichier())).
324
								' sera généré à nouveau le '.
325
								date('D d M Y à H:i:s', (filemtime($this->getCacheFichier()) + $this->getCacheDuree()))." \n";
326
						trigger_error($e, E_USER_NOTICE);
327
					}
328
				}
329
				return $this->getRegistre()->get('sortie');
330
				break;
331
			case aModule::SORTIE_EXIT_HTML :
381 jpm 332
				echo $this->getRegistre()->get('sortie');
376 jpm 333
				exit();
334
				break;
335
			case aModule::SORTIE_XML :
336
				header('Content-Type: application/xhtml+xml; charset='.$this->registre->get('charset'));
337
				echo $this->registre->get('sortie');
338
				exit();
339
				break;
340
			case aModule::SORTIE_PDF :
341
				header('Content-type: application/pdf');
342
				header('Content-Length: '.strlen($this->getRegistre()->get('sortie')));
343
				header('Content-Disposition: inline; filename='.str_replace(' ', '_', $GLOBALS['_SCRIPT_']['titre_fichier']).'.pdf');//
344
				echo $this->registre->get('sortie');
345
				break;
346
			case aModule::SORTIE_MAIL_SMTP :
347
				//trigger_error(print_r($this->getRegistre()->get('sortie_mail_mime'), true), E_USER_NOTICE);
348
				// TODO : réseoudre le problème de l'autoload pour les fichiers PEAR ci-dessous
349
				include_once 'Mail.php';
350
				include_once 'Mail/smtp.php';
351
				$sortie_mail = '';
352
				// Nous vérifions si nous avons à faire à un mail mime ou pas
353
				if (is_null($this->getRegistre()->get('sortie_mail_mime'))) {
354
					$sortie_mail = $this->getRegistre()->get('sortie');
355
				} else {
356
					// Pour l'instant supporte du html et son alternative en txt plus des fichiers attachés
357
					// TODO : Les mails multiparts contenant des imbrications de html et de txt ne sont pas encore pris en compte...
358
					include_once 'Mail/mime.php';
359
					$MailMime = new Mail_mime("\n");
360
					foreach ($this->getRegistre()->get('sortie_mail_mime') as $valeur) {
361
						switch (strtolower($valeur[1])) {
362
							case 'txt' :
363
								// Syntaxe multiligne: <txt>mettre ici du texte brute</txt>
364
								$MailMime->setTXTBody($valeur[2]);
365
								break;
366
							case 'html' :
367
								// Syntaxe multiligne: <html>mettre ici votre html</html>
368
								$MailMime->setHTMLBody($valeur[2]);
369
								break;
370
							case 'file' :
371
								// Syntaxe sur une ligne: <file src="/tmp/un_test.txt" type="text/plain" />
372
								$e = $MailMime->addAttachment($valeur[3], $valeur[4]);
373
								if ($e instanceof PEAR_Error) {
374
									trigger_error($e->getMessage(), E_USER_NOTICE);
375
								}
376
								break;
377
							default :
378
								trigger_error('Type de balise inconnue :'.$valeur[1], E_USER_WARNING);
379
						}
380
					}
381
					//do not ever try to call these lines in reverse order
382
					$sortie_mail = $MailMime->get();
383
					$this->getRegistre()->set('sortie_mail_smtp_entete', $MailMime->headers($this->getRegistre()->get('sortie_mail_smtp_entete')));
384
				}
385
				$mail_object = new Mail_smtp($this->getRegistre()->get('sortie_mail_smtp_params'));
386
				$message = $mail_object->send(	$this->getRegistre()->get('sortie_mail_smtp_destinataire'),
387
												$this->getRegistre()->get('sortie_mail_smtp_entete'),
388
												$sortie_mail);
389
				$this->getRegistre()->set('sortie_mail_smtp_info', $message);
390
				return null;
391
				break;
392
			case aModule::SORTIE_EXIT :
393
    			// Nous ne faisons rien, nous terminons seulement le programme ici
394
    			exit();
395
    			break;
396
			case aModule::SORTIE_NULL :
397
    			// Nous ne faisons rien, nous retournons null
398
    			return null;
399
    			break;
400
			default :
401
    			trigger_error('Type de sortie inconnu', E_USER_ERROR);
402
		}
403
    }
404
}
405
?>