Subversion Repositories Applications.papyrus

Rev

Rev 2059 | Rev 2083 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1165 jp_milcent 1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 5.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 1999-2006 Tela Botanica (accueil@tela-botanica.org)                                    |
7
// +------------------------------------------------------------------------------------------------------+
1283 florian 8
// | This file is part of papyrus_bp.                                                                     |
1165 jp_milcent 9
// |                                                                                                      |
10
// | Foobar is free software; you can redistribute it and/or modify                                       |
11
// | it under the terms of the GNU General Public License as published by                                 |
12
// | the Free Software Foundation; either version 2 of the License, or                                    |
13
// | (at your option) any later version.                                                                  |
14
// |                                                                                                      |
15
// | Foobar is distributed in the hope that it will be useful,                                            |
16
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
17
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
18
// | GNU General Public License for more details.                                                         |
19
// |                                                                                                      |
20
// | You should have received a copy of the GNU General Public License                                    |
21
// | along with Foobar; if not, write to the Free Software                                                |
22
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
23
// +------------------------------------------------------------------------------------------------------+
1961 alexandre_ 24
// CVS : $Id: syndication.php,v 1.11 2008-10-29 15:55:56 alexandre_tb Exp $
1165 jp_milcent 25
/**
26
* papyrus_bp - syndication.php
27
*
28
* Description :
29
*
30
*@package papyrus_bp
31
//Auteur original :
32
*@author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
33
//Autres auteurs :
34
*@author        Aucun
35
*@copyright     Tela-Botanica 1999-2006
1961 alexandre_ 36
*@version       $Revision: 1.11 $ $Date: 2008-10-29 15:55:56 $
1165 jp_milcent 37
// +------------------------------------------------------------------------------------------------------+
38
*/
39
 
40
// +------------------------------------------------------------------------------------------------------+
41
// |                                            ENTÊTE du PROGRAMME                                       |
42
// +------------------------------------------------------------------------------------------------------+
43
$GLOBALS['_GEN_commun']['info_applette_nom_fonction'] = 'afficherSyndication';
44
$GLOBALS['_GEN_commun']['info_applette_balise'] = 	'\{\{[Ss]yndication'.
45
													'(?:\s*'.
46
														'(?:'.
47
															'(url="[^"]*")|'.
48
															'(titre="[^"]*")|'.
1471 alexandre_ 49
															'(nb="?\d+"?)|'.
2081 gduche 50
															'(tailledesc="?\d+"?)|'.
51
															'(voirsuite="[^"]*")|'.
52
															'(dureecache="?\d+"?)|'.
53
															'(id="[^"]*")|'.
2059 aurelien 54
															'(nbmax="?\d+"?)|'.
1471 alexandre_ 55
															'(nouvellefenetre="?(?:0|1)"?)|'.
1165 jp_milcent 56
															'(formatdate="[^"]*")|'.
57
															'(formatdatepro="[^"]*")|'.
1471 alexandre_ 58
															'(template=".*")|'.
1165 jp_milcent 59
														')'.
60
													')+'.
61
													'\s*\}\}';
62
// +------------------------------------------------------------------------------------------------------+
63
/** Inclusion du fichier de configuration de cette application.*/
64
require_once GEN_CHEMIN_APPLETTE.'syndication'.GEN_SEP.'configuration'.GEN_SEP.'synd_configuration.inc.php';
65
 
2059 aurelien 66
/** Inclusion du fichier de fonctions de cette application.*/
67
require_once GEN_CHEMIN_APPLETTE.'syndication'.GEN_SEP.'bibliotheque'.GEN_SEP.'syndication.fonct.php';
68
 
1165 jp_milcent 69
// Inclusion des fichiers de traduction de l'applette SYND de Papyrus
70
if (file_exists(SYND_CHEMIN_LANGUE.'synd_langue_'.$GLOBALS['_GEN_commun']['i18n'].'.inc.php')) {
71
    /** Inclusion du fichier de traduction suite à la transaction avec le navigateur.*/
72
    require_once SYND_CHEMIN_LANGUE.'synd_langue_'.$GLOBALS['_GEN_commun']['i18n'].'.inc.php';
73
} else {
74
    /** Inclusion du fichier de traduction par défaut.*/
75
    require_once SYND_CHEMIN_LANGUE.'synd_langue_'.SYND_I18N_DEFAUT.'.inc.php';
76
}
2059 aurelien 77
 
1165 jp_milcent 78
/** Inclusion du fichier de la bibliotheque permettant de manipuler les flux RSS.*/
1283 florian 79
//require_once(MAGPIE_DIR.'rss_fetch.inc');
1529 jp_milcent 80
require_once PAP_CHEMIN_API_PEAR.'XML/Feed/Parser.php';
1165 jp_milcent 81
// +------------------------------------------------------------------------------------------------------+
82
// |                                            CORPS du PROGRAMME                                        |
83
// +------------------------------------------------------------------------------------------------------+
84
/** Fonction afficherSyndication() - Retourne la liste des pages des sites syndiqués.
85
*
86
* Cette fonction retourne la liste des pages des sites syndiqués.
87
*
88
* @param  array contient les arguments de la fonction.
89
* @param  array  tableau global de Papyrus.
90
* @return string XHTML la liste des pages.
91
*/
92
function afficherSyndication($tab_applette_arguments, $_GEN_commun)
93
{
94
	// Initialisation des variables
95
    $sortie = '';
96
	$GLOBALS['_SYNDICATION_']['erreurs'] = array();
97
	$GLOBALS['_SYNDICATION_']['informations'] = array();
98
	$GLOBALS['_SYNDICATION_']['sites'] = array();
2059 aurelien 99
 
100
 
101
		$string_arguments = "";
1165 jp_milcent 102
	//+----------------------------------------------------------------------------------------------------------------+
103
	// Gestion des arguments
104
	$balise = $tab_applette_arguments[0];
105
    $tab_arguments = $tab_applette_arguments;
2081 gduche 106
 
1165 jp_milcent 107
	unset($tab_arguments[0]);
108
    foreach($tab_arguments as $argument) {
109
    	if ($argument != '') {
2059 aurelien 110
	    		$string_arguments .= $argument;
1283 florian 111
	    	$tab_parametres = explode('=', $argument, 2);
1165 jp_milcent 112
	    	$options[$tab_parametres[0]] = trim($tab_parametres[1], '"');
113
    	}
114
    }
2059 aurelien 115
 
116
    $id_cache = md5($string_arguments);
2081 gduche 117
	//Greg : possibilité d'ajouter une durée au cache
118
	if (!isset($options['dureecache'])) {
119
		$options['dureecache'] = CACHE_LIFETIME;
120
	}
2059 aurelien 121
 
2081 gduche 122
	if($contenu_cache = verifierCache($id_cache, $options['dureecache'])) {
2059 aurelien 123
		return $contenu_cache;
124
	}
1961 alexandre_ 125
	//
1165 jp_milcent 126
	//+----------------------------------------------------------------------------------------------------------------+
1283 florian 127
    // Gestion des erreurs de paramétrage
2081 gduche 128
 
1165 jp_milcent 129
	if (!isset($options['url'])) {
130
		$GLOBALS['_SYNDICATION_']['erreurs'][] = sprintf(SYND_LG_ERREUR_URL, $balise);
131
	}
132
	if (!isset($options['titre'])) {
133
		$options['titre'] = '';
134
	}
135
	if (!isset($options['nb'])) {
136
		$options['nb'] = SYND_NOMBRE;
137
	}
2059 aurelien 138
	if (!isset($options['nbmax'])) {
139
		$options['nbmax'] = SYND_NOMBRE_MAX;
140
	}
2081 gduche 141
    	if (!isset($options['nouvellefenetre'])) {
1165 jp_milcent 142
		$options['nouvellefenetre'] = SYND_OUVRIR_LIEN_RSS_NOUVELLE_FENETRE;
143
	}
144
	if (!isset($options['formatdate'])) {
145
		$options['formatdate'] = SYND_FORMAT_DATE;
146
	}
147
	if (!isset($options['formatdatepro'])) {
1471 alexandre_ 148
		$options['formatdatepro'] = false;
1165 jp_milcent 149
	}
150
	if (!isset($options['template'])) {
151
		$options['template'] = SYND_CHEMIN_SQUELETTE.SYND_SQUELETTE_LISTE;
152
	} else {
153
		if (file_exists(SYND_CHEMIN_SQUELETTE.$options['template'])) {
154
			$options['template'] = SYND_CHEMIN_SQUELETTE.$options['template'];
155
		}
156
	}
2081 gduche 157
 
158
 
159
 
160
 
1165 jp_milcent 161
    //+----------------------------------------------------------------------------------------------------------------+
1283 florian 162
    // Recuperation des donnees
1165 jp_milcent 163
    if (count($GLOBALS['_SYNDICATION_']['erreurs']) == 0) {
164
		$tab_url = array_map('trim', explode(',', $options['url']));
165
        foreach ($tab_url as $cle => $url) {
166
			if ($url != '') {
167
				$aso_site = array();
1745 jp_milcent 168
				// Liste des encodages acceptés pour les flux
169
				$encodages = 'UTF-8, ISO-8859-1, ISO-8859-15';
1529 jp_milcent 170
				try {
171
					$feed = new XML_Feed_Parser(file_get_contents($url));
172
				} catch (XML_Feed_Parser_Exception $e) {
1767 alexandre_ 173
					return('Le flux RSS est invalide : ' . $e->getMessage());
1529 jp_milcent 174
				}
2059 aurelien 175
 
1165 jp_milcent 176
				if ($options['template'] != '' && !file_exists($options['template'])) {
177
					$i = 0 ;
178
					$res= '';
1529 jp_milcent 179
					foreach ($feed as $item) {
1165 jp_milcent 180
						// Le test suivant pour savoir s il faut reduire l excendent de description
181
						// Si {all} est present dans le template on ne reduit pas
182
						if (preg_match ('/{all}/', $options['template'])) {
183
							$template = str_replace('{all}', '', $options['template']);
184
							$all = true ;
185
						} else {
186
							$all = false;
187
						}
1529 jp_milcent 188
						if (isset($item->summary)) {
2059 aurelien 189
							$item->description = mb_convert_encoding($item->summary, 'HTML-ENTITIES', $encodages);
1165 jp_milcent 190
						} else {
1529 jp_milcent 191
							if (strlen($item->description) > 200 && !$all) {
1745 jp_milcent 192
								$item->description = 	substr(mb_convert_encoding($item->description, 'HTML-ENTITIES', $encodages), 0, 300).
193
														'... <a href="'.htmlentities($item->link).'">Lire la suite</a>';
1165 jp_milcent 194
							}
195
						}
1529 jp_milcent 196
						if (!isset($item->pubdate)) {
197
							$item->pubdate = date('dmY');
1165 jp_milcent 198
						}
1204 alexandre_ 199
						// Le code ci-apres est pour rattraper les dates du type 01012005 parsees par magpie
200
						// lorsque les flux donne des dates au format iso
1529 jp_milcent 201
						if (preg_match('/^([0-3][0-9])([0-1][0-9])([0-9][0-9][0-9][0-9])$/', $item->pubdate, $match)) {
202
							$item->pubdate = $match[3].'-'.$match[2].'-'.$match[1];
1204 alexandre_ 203
							//echo $item['pubdate'];
204
						}
2059 aurelien 205
						$res .= str_replace ('{num}', ++$i,
206
								str_replace ('{item}', '<a href="'.htmlentities($item->link).'" target="_top">'.mb_convert_encoding($item->title, 'HTML-ENTITIES', $encodages).'</a>',
1529 jp_milcent 207
								str_replace ('{date}', strftime('%d.%m.%Y',strtotime($item->pubdate)),
1745 jp_milcent 208
								str_replace ('{description}', mb_convert_encoding($item->description, 'HTML-ENTITIES', $encodages), $options['template'])))) ;
1165 jp_milcent 209
						$res .= "\n";
210
						if ($i > $options['nb']) {
211
							break;
212
						}
213
					}
214
					return $res;
215
				}
216
				// Gestion du titre
217
				if ( $options['titre'] == '' ) {
1745 jp_milcent 218
					$aso_site['titre'] = mb_convert_encoding($feed->title, 'HTML-ENTITIES', $encodages);
1165 jp_milcent 219
				} else if ( $options['titre'] != '0' ) {
1529 jp_milcent 220
					$aso_site['titre'] = $options['titre'];
1165 jp_milcent 221
				}
2081 gduche 222
 
223
				//Gestion de l'ID
224
				if ( $options['id'] != '' ) {
225
					$aso_site['id'] = $options['id'];
226
				}
227
 
228
				//Gestion du lien vers la suite
229
				if ( $options['voirsuite'] != '' ) {
230
					$aso_site['voirsuite'] = $options['voirsuite'];
231
				}
232
 
1165 jp_milcent 233
				// Gestion de l'url du site
1529 jp_milcent 234
				$aso_site['url'] = htmlentities($feed->link);
235
 
1283 florian 236
				// Ouverture du lien dans une nouvelle fenetre
1165 jp_milcent 237
				$aso_site['ext'] = false;
238
				if ($options['nouvellefenetre'] == 1) {
239
					$aso_site['ext'] = true;
240
				}
1283 florian 241
				// Gestion des pages syndiquees
1165 jp_milcent 242
				$i = 0;
1529 jp_milcent 243
			    $nb_item = $feed->numberEntries;
1961 alexandre_ 244
 
1529 jp_milcent 245
				foreach ($feed as $item) {
2081 gduche 246
 
1170 jp_milcent 247
					if ($options['nb'] != 0 && $nb_item >= $options['nb'] && $i >= $options['nb']) {
248
						break;
249
					}
250
					$i++;
2059 aurelien 251
 
1165 jp_milcent 252
					$aso_page = array();
253
					$aso_page['site'] = $aso_site;
1745 jp_milcent 254
					$aso_page['url'] = htmlentities($item->link);
255
					$aso_page['titre'] = mb_convert_encoding($item->title, 'HTML-ENTITIES', $encodages);
1961 alexandre_ 256
					$aso_page['description'] = mb_convert_encoding($item->description, 'HTML-ENTITIES', $encodages);
2081 gduche 257
 
258
					/* Extraction de l'image si incluse dans description */
259
					$image = "";
260
					$description = $aso_page['description'];
261
 
262
					$taille = $options['tailledesc'];
263
 
264
					if (!(strpos($description, '<img') === false))	{
265
						$pos_debut = strpos($description, '<img');
266
						$pos_fin_image = strpos($description, '>', $posimage) + 1;
267
						$image = substr($description, $pos_debut, ($pos_fin_image - $pos_debut));
268
						$description = substr_replace($description,' ', $pos_debut,  ($pos_fin_image - $pos_debut));
269
					}
270
 
271
					$aso_page['image'] = $image;
272
					$aso_page['description_sans_image'] = $description;
273
 
274
					if ($taille != null)	{
275
 
276
						//Attention : si on coupe simplement, on peut couper une balise en plein milieu.
277
						//On va donc vérifier
278
						//1. recherche de tags simples <i> ou <b>
279
						$description = html_entity_decode($description);
280
						$description = strip_tags($description, "<i><b>");
281
 
282
						//Va-ton couper un mot ?
283
 
284
						if ($description[$taille] != ' ' && $description[$taille+1] != ' ' && $description[$taille-1] != ' ')	{
285
  								$strpos = strpos($description, ' ', $taille);
286
  								if ($strpos === true)	{
287
  									$taille = $strpos;
288
  								}
289
  						}
290
 
291
 
292
						$description = substr($description, 0, $taille);
293
						$description.='(...)';
294
 
295
						$pattern = '#<([ib])[ ]*[a-z]*>(.*?)</\1>#i';
296
						//1. remplacer toutes le	s balises fermées
297
						$chaine = preg_replace($pattern, '$2', $description);
298
						$nb_occurences_i = substr_count($chaine, '<i');
299
						for ($occurences = 0; $occurences < $nb_occurences_i; $occurences++)	{
300
							$description .= "</i>";
301
						}
302
 
303
						$nb_occurences_b = substr_count($chaine, '<b');
304
						for ($occurences = 0; $occurences < $nb_occurences_b; $occurences++)	{
305
							$description .= "</b>";
306
						}
307
					}
308
 
309
					$aso_page['description_light'] = $description;
310
 
311
					//Greg : ajout GUID
312
					$aso_page['guid'] = mb_convert_encoding($item->model->getElementsByTagName('guid')->item(0)->nodeValue, 'HTML-ENTITIES', $encodages);
313
 
1961 alexandre_ 314
					$aso_page['category'] = mb_convert_encoding($item->model->getElementsByTagName('category')->item(0)->nodeValue, 'HTML-ENTITIES', $encodages);
315
					$aso_page['author'] = mb_convert_encoding($item->author, 'HTML-ENTITIES', $encodages);
2081 gduche 316
 
317
 
2059 aurelien 318
					$aso_page['date'] = $item->pubDate;
1165 jp_milcent 319
					if ($options['formatdatepro']) {
1283 florian 320
						switch ($options['formatdatepro']) {
321
							case 'jm' :
1529 jp_milcent 322
								$aso_page['date'] = strftime('%d.%m', $aso_page['date']);
1283 florian 323
								break;
324
							case 'jma' :
1529 jp_milcent 325
								$aso_page['date'] = strftime('%d.%m.%Y', $aso_page['date']);
1283 florian 326
								break;
327
							case 'jmh' :
1529 jp_milcent 328
								$aso_page['date'] = strftime('%d.%m %H:%M', $aso_page['date']);
1283 florian 329
								break;
330
							case 'jmah' :
1529 jp_milcent 331
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
1283 florian 332
								break;
333
							default :
1529 jp_milcent 334
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
1283 florian 335
						}
1165 jp_milcent 336
					} else {
337
						switch ($options['formatdate']) {
338
							case 'jm' :
1529 jp_milcent 339
								$aso_page['date'] = strftime('%d.%m', $aso_page['date']);
1165 jp_milcent 340
								break;
341
							case 'jma' :
1529 jp_milcent 342
								$aso_page['date'] = strftime('%d.%m.%Y', $aso_page['date']);
1165 jp_milcent 343
								break;
344
							case 'jmh' :
1529 jp_milcent 345
								$aso_page['date'] = strftime('%d.%m %H:%M', $aso_page['date']);
1165 jp_milcent 346
								break;
347
							case 'jmah' :
1529 jp_milcent 348
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
1165 jp_milcent 349
								break;
350
							default :
1529 jp_milcent 351
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
1165 jp_milcent 352
						}
353
					}
354
					$aso_site['pages'][] = $aso_page;
355
					$GLOBALS['_SYNDICATION_']['pages'][strtotime($aso_page['date'])] = $aso_page;
356
				}
357
				$GLOBALS['_SYNDICATION_']['sites'][] = $aso_site;
358
			}
359
        }
360
    }
361
	// Trie des pages par date
362
	krsort($GLOBALS['_SYNDICATION_']['pages']);
2059 aurelien 363
 
364
	$GLOBALS['_SYNDICATION_']['sites'] = array_slice($GLOBALS['_SYNDICATION_']['sites'],0,$options['nbmax'],true) ;
365
	$GLOBALS['_SYNDICATION_']['pages'] = array_slice($GLOBALS['_SYNDICATION_']['pages'],0,$options['nbmax'],true) ;
366
 
1165 jp_milcent 367
	//+----------------------------------------------------------------------------------------------------------------+
1283 florian 368
    // Extrait les variables et les ajoutes a l'espace de noms local
1165 jp_milcent 369
	// Gestion des squelettes
370
	extract($GLOBALS['_SYNDICATION_']);
1283 florian 371
	// Demarre le buffer
1165 jp_milcent 372
	ob_start();
373
	// Inclusion du fichier
374
	include($options['template']);
1283 florian 375
	// Recuperer le  contenu du buffer
1165 jp_milcent 376
	$sortie = ob_get_contents();
2081 gduche 377
	creer_cache($id_cache,$sortie, $options['dureecache']);
1283 florian 378
	// Arrete et detruit le buffer
1165 jp_milcent 379
	ob_end_clean();
2059 aurelien 380
 
1165 jp_milcent 381
	//+----------------------------------------------------------------------------------------------------------------+
382
	// Sortie
383
    return $sortie;
384
}
385
 
386
/* +--Fin du code ----------------------------------------------------------------------------------------+
387
*
388
* $Log: not supported by cvs2svn $
1961 alexandre_ 389
* Revision 1.10  2007-12-13 14:10:51  alexandre_tb
390
* Fusion avec la livraison AHA : 13 decembre 2007
391
*
1767 alexandre_ 392
* Revision 1.9  2007-12-03 14:54:14  jp_milcent
393
* Fusion avec la livraison AHA : 3 décembre 2007
394
*
1745 jp_milcent 395
* Revision 1.7.2.2  2007-12-03 14:52:21  jp_milcent
396
* Correction bogue : & dans les urls.
397
*
398
* Revision 1.8  2007-11-30 14:17:30  jp_milcent
399
* Fusion avec la livraison AHA : 30 novembre 2007
400
*
1739 jp_milcent 401
* Revision 1.7.2.1  2007-11-30 14:15:02  jp_milcent
402
* Amélioration du décodage utf8.
403
*
404
* Revision 1.7  2007-07-25 15:09:44  jp_milcent
405
* Fusion avec la livraison Narmer.
406
*
1529 jp_milcent 407
* Revision 1.5.2.4  2007-07-25 15:07:52  jp_milcent
408
* Correction problème url.
409
*
410
* Revision 1.5.2.3  2007-07-25 14:50:21  jp_milcent
411
* Corrections, meilleure utilisation de XML_Feed_Parser.
412
*
413
* Revision 1.5.2.2  2007-07-25 09:45:07  jp_milcent
414
* Utilisation de XML_Feed_Parser de Pear pour l'applette Syndication.
415
*
416
* Revision 1.6  2007-06-25 12:15:07  alexandre_tb
417
* merge from narmer
418
*
1471 alexandre_ 419
* Revision 1.5.2.1  2007-06-06 15:24:37  jp_milcent
420
* Amélioration de la compatibilité avec les anciennes version des balises de l'applette syndication.
421
*
422
* Revision 1.5  2007-04-20 12:50:18  florian
423
* correction bugs suite au merge
424
*
1359 florian 425
* Revision 1.4  2007/03/28 15:53:27  florian
426
* correction pb date, encodage utf-8
427
*
1283 florian 428
* Revision 1.3  2007/01/23 14:17:19  alexandre_tb
429
* backport : hack pour rattraper les dates du type 01012005 parsees par magpie
430
* lorsque les flux donne des dates au format iso
431
*
1204 alexandre_ 432
* Revision 1.2  2006/12/13 17:20:51  jp_milcent
433
* Correction bogue : paramètre nb non pris en compte
434
*
1170 jp_milcent 435
* Revision 1.1  2006/12/13 17:06:36  jp_milcent
436
* Ajout de l'applette Syndication.
1165 jp_milcent 437
*
1170 jp_milcent 438
*
1165 jp_milcent 439
* +-- Fin du code ----------------------------------------------------------------------------------------+
440
*/
2081 gduche 441
?>