Subversion Repositories Applications.papyrus

Rev

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

Rev Author Line No. Line
2094 drzraf 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
// +------------------------------------------------------------------------------------------------------+
8
// | This file is part of papyrus_bp.                                                                     |
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
// +------------------------------------------------------------------------------------------------------+
24
// CVS : $Id: syndication.php,v 1.11 2008-10-29 15:55:56 alexandre_tb Exp $
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
36
*@version       $Revision: 1.11 $ $Date: 2008-10-29 15:55:56 $
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="[^"]*")|'.
49
															'(nb="?\d+"?)|'.
50
															'(tailledesc="?\d+"?)|'.
51
															'(voirsuite="[^"]*")|'.
52
															'(dureecache="?\d+"?)|'.
53
															'(id="[^"]*")|'.
54
															'(nbmax="?\d+"?)|'.
55
															'(nouvellefenetre="?(?:0|1)"?)|'.
56
															'(formatdate="[^"]*")|'.
57
															'(formatdatepro="[^"]*")|'.
58
															'(template=".*")|'.
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
 
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
 
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
}
77
 
78
/** Inclusion du fichier de la bibliotheque permettant de manipuler les flux RSS.*/
79
//require_once(MAGPIE_DIR.'rss_fetch.inc');
80
require_once PAP_CHEMIN_API_PEAR.'XML/Feed/Parser.php';
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();
99
 
100
 
101
		$string_arguments = "";
102
	//+----------------------------------------------------------------------------------------------------------------+
103
	// Gestion des arguments
104
	$balise = $tab_applette_arguments[0];
105
    $tab_arguments = $tab_applette_arguments;
106
 
107
	unset($tab_arguments[0]);
108
    foreach($tab_arguments as $argument) {
109
    	if ($argument != '') {
110
	    		$string_arguments .= $argument;
111
	    	$tab_parametres = explode('=', $argument, 2);
112
	    	$options[$tab_parametres[0]] = trim($tab_parametres[1], '"');
113
    	}
114
    }
115
 
116
    $id_cache = md5($string_arguments);
117
	//Greg : possibilité d'ajouter une durée au cache
118
	if (!isset($options['dureecache'])) {
119
		$options['dureecache'] = CACHE_LIFETIME;
120
	}
121
 
122
	if($contenu_cache = verifierCache($id_cache, $options['dureecache'])) {
123
		return $contenu_cache;
124
	}
125
	//
126
	//+----------------------------------------------------------------------------------------------------------------+
127
    // Gestion des erreurs de paramétrage
128
 
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
	}
138
	if (!isset($options['nbmax'])) {
139
		$options['nbmax'] = SYND_NOMBRE_MAX;
140
	}
141
    	if (!isset($options['nouvellefenetre'])) {
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'])) {
148
		$options['formatdatepro'] = false;
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
	}
157
 
158
 
159
 
160
 
161
    //+----------------------------------------------------------------------------------------------------------------+
162
    // Recuperation des donnees
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();
168
				// Liste des encodages acceptés pour les flux
169
				$encodages = 'UTF-8, ISO-8859-1, ISO-8859-15';
170
				try {
171
					$xml = file_get_contents($url);
172
					$feed = new XML_Feed_Parser($xml);
173
				} catch (XML_Feed_Parser_Exception $e) {
174
					creer_cache('erreur', $xml, 86400);
175
					return('Le flux RSS est invalide : ' . $e->getMessage());
176
				}
177
 
178
				if ($options['template'] != '' && !file_exists($options['template'])) {
179
					$i = 0 ;
180
					$res= '';
181
					foreach ($feed as $item) {
182
						// Le test suivant pour savoir s il faut reduire l excendent de description
183
						// Si {all} est present dans le template on ne reduit pas
184
						if (preg_match ('/{all}/', $options['template'])) {
185
							$template = str_replace('{all}', '', $options['template']);
186
							$all = true ;
187
						} else {
188
							$all = false;
189
						}
190
						if (isset($item->summary)) {
191
							$item->description = mb_convert_encoding($item->summary, 'HTML-ENTITIES', $encodages);
192
						} else {
193
							if (strlen($item->description) > 200 && !$all) {
194
								$item->description = 	substr(mb_convert_encoding($item->description, 'HTML-ENTITIES', $encodages), 0, 300).
195
														'... <a href="'.htmlentities($item->link).'">Lire la suite</a>';
196
							}
197
						}
198
						if (!isset($item->pubdate)) {
199
							$item->pubdate = date('dmY');
200
						}
201
						// Le code ci-apres est pour rattraper les dates du type 01012005 parsees par magpie
202
						// lorsque les flux donne des dates au format iso
203
						if (preg_match('/^([0-3][0-9])([0-1][0-9])([0-9][0-9][0-9][0-9])$/', $item->pubdate, $match)) {
204
							$item->pubdate = $match[3].'-'.$match[2].'-'.$match[1];
205
							//echo $item['pubdate'];
206
						}
207
						$res .= str_replace ('{num}', ++$i,
208
								str_replace ('{item}', '<a href="'.htmlentities($item->link).'" target="_top">'.mb_convert_encoding($item->title, 'HTML-ENTITIES', $encodages).'</a>',
209
								str_replace ('{date}', strftime('%d.%m.%Y',strtotime($item->pubdate)),
210
								str_replace ('{description}', mb_convert_encoding($item->description, 'HTML-ENTITIES', $encodages), $options['template'])))) ;
211
						$res .= "\n";
212
						if ($i > $options['nb']) {
213
							break;
214
						}
215
					}
216
					return $res;
217
				}
218
				// Gestion du titre
219
				if ( $options['titre'] == '' ) {
220
					$aso_site['titre'] = mb_convert_encoding($feed->title, 'HTML-ENTITIES', $encodages);
221
				} else if ( $options['titre'] != '0' ) {
222
					$aso_site['titre'] = $options['titre'];
223
				}
224
 
225
				//Gestion de l'ID
226
				if ( $options['id'] != '' ) {
227
					$aso_site['id'] = $options['id'];
228
				}
229
 
230
				//Gestion du lien vers la suite
231
				if ( $options['voirsuite'] != '' ) {
232
					$aso_site['voirsuite'] = $options['voirsuite'];
233
				}
234
 
235
				// Gestion de l'url du site
236
				$aso_site['url'] = htmlentities($feed->link);
237
 
238
				// Ouverture du lien dans une nouvelle fenetre
239
				$aso_site['ext'] = false;
240
				if ($options['nouvellefenetre'] == 1) {
241
					$aso_site['ext'] = true;
242
				}
243
				// Gestion des pages syndiquees
244
				$i = 0;
245
			    $nb_item = $feed->numberEntries;
246
 
247
				foreach ($feed as $item) {
248
 
249
					if ($options['nb'] != 0 && $nb_item >= $options['nb'] && $i >= $options['nb']) {
250
						break;
251
					}
252
					$i++;
253
 
254
					$aso_page = array();
255
					$aso_page['site'] = $aso_site;
256
					$aso_page['url'] = htmlentities($item->link);
257
					$aso_page['titre'] = mb_convert_encoding($item->title, 'HTML-ENTITIES', $encodages);
258
					$aso_page['description'] = mb_convert_encoding($item->description, 'HTML-ENTITIES', $encodages);
259
 
260
					/* Extraction de l'image si incluse dans description */
261
					$image = "";
262
					$description = $aso_page['description'];
263
 
264
					$taille = $options['tailledesc'];
265
 
266
					if (!(strpos($description, '<img') === false))	{
267
						$pos_debut = strpos($description, '<img');
268
						$pos_fin_image = strpos($description, '>', $posimage) + 1;
269
						$image = substr($description, $pos_debut, ($pos_fin_image - $pos_debut));
270
						$description = substr_replace($description,' ', $pos_debut,  ($pos_fin_image - $pos_debut));
271
					}
272
 
273
					$aso_page['image'] = $image;
274
					$aso_page['description_sans_image'] = $description;
275
 
276
					if ($taille != null)	{
277
 
278
						//Attention : si on coupe simplement, on peut couper une balise en plein milieu.
279
						//On va donc vérifier
280
						//1. recherche de tags simples <i> ou <b>
281
						$description = html_entity_decode($description);
282
						$description = strip_tags($description, "<i><b>");
283
 
284
						//Va-ton couper un mot ?
285
 
286
						if ($description[$taille] != ' ' && $description[$taille+1] != ' ' && $description[$taille-1] != ' ')	{
287
  								$strpos = strpos($description, ' ', $taille);
288
  								if ($strpos === true)	{
289
  									$taille = $strpos;
290
  								}
291
  						}
292
 
293
 
294
						$description = substr($description, 0, $taille);
295
						$description.='(...)';
296
 
297
						$pattern = '#<([ib])[ ]*[a-z]*>(.*?)</\1>#i';
298
						//1. remplacer toutes le	s balises fermées
299
						$chaine = preg_replace($pattern, '$2', $description);
300
						$nb_occurences_i = substr_count($chaine, '<i');
301
						for ($occurences = 0; $occurences < $nb_occurences_i; $occurences++)	{
302
							$description .= "</i>";
303
						}
304
 
305
						$nb_occurences_b = substr_count($chaine, '<b');
306
						for ($occurences = 0; $occurences < $nb_occurences_b; $occurences++)	{
307
							$description .= "</b>";
308
						}
309
					}
310
 
311
					$aso_page['description_light'] = $description;
312
 
313
					//Greg : ajout GUID
314
					$aso_page['guid'] = mb_convert_encoding($item->model->getElementsByTagName('guid')->item(0)->nodeValue, 'HTML-ENTITIES', $encodages);
315
 
316
					$aso_page['category'] = mb_convert_encoding($item->model->getElementsByTagName('category')->item(0)->nodeValue, 'HTML-ENTITIES', $encodages);
317
					$aso_page['author'] = mb_convert_encoding($item->author, 'HTML-ENTITIES', $encodages);
318
 
319
 
320
					$aso_page['date'] = $item->pubDate;
321
					if ($options['formatdatepro']) {
322
						switch ($options['formatdatepro']) {
323
							case 'jm' :
324
								$aso_page['date'] = strftime('%d.%m', $aso_page['date']);
325
								break;
326
							case 'jma' :
327
								$aso_page['date'] = strftime('%d.%m.%Y', $aso_page['date']);
328
								break;
329
							case 'jmh' :
330
								$aso_page['date'] = strftime('%d.%m %H:%M', $aso_page['date']);
331
								break;
332
							case 'jmah' :
333
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
334
								break;
335
							default :
336
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
337
						}
338
					} else {
339
						switch ($options['formatdate']) {
340
							case 'jm' :
341
								$aso_page['date'] = strftime('%d.%m', $aso_page['date']);
342
								break;
343
							case 'jma' :
344
								$aso_page['date'] = strftime('%d.%m.%Y', $aso_page['date']);
345
								break;
346
							case 'jmh' :
347
								$aso_page['date'] = strftime('%d.%m %H:%M', $aso_page['date']);
348
								break;
349
							case 'jmah' :
350
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
351
								break;
352
							default :
353
								$aso_page['date'] = strftime('%d.%m.%Y %H:%M', $aso_page['date']);
354
						}
355
					}
356
					$aso_site['pages'][] = $aso_page;
357
					$GLOBALS['_SYNDICATION_']['pages'][strtotime($aso_page['date'])] = $aso_page;
358
				}
359
				$GLOBALS['_SYNDICATION_']['sites'][] = $aso_site;
360
			}
361
        }
362
    }
363
	// Trie des pages par date
364
	krsort($GLOBALS['_SYNDICATION_']['pages']);
365
 
366
	$GLOBALS['_SYNDICATION_']['sites'] = array_slice($GLOBALS['_SYNDICATION_']['sites'],0,$options['nbmax'],true) ;
367
	$GLOBALS['_SYNDICATION_']['pages'] = array_slice($GLOBALS['_SYNDICATION_']['pages'],0,$options['nbmax'],true) ;
368
 
369
	//+----------------------------------------------------------------------------------------------------------------+
370
    // Extrait les variables et les ajoutes a l'espace de noms local
371
	// Gestion des squelettes
372
	extract($GLOBALS['_SYNDICATION_']);
373
	// Demarre le buffer
374
	ob_start();
375
	// Inclusion du fichier
376
	include($options['template']);
377
	// Recuperer le  contenu du buffer
378
	$sortie = ob_get_contents();
379
	creer_cache($id_cache,$sortie, $options['dureecache']);
380
	// Arrete et detruit le buffer
381
	ob_end_clean();
382
 
383
	//+----------------------------------------------------------------------------------------------------------------+
384
	// Sortie
385
    return $sortie;
386
}
387
 
388
function transcribe_cp1252_to_latin1($cp1252) {
389
  return strtr(
390
    $cp1252,
391
    array(
392
      "\x80" => "e",  "\x81" => " ",    "\x82" => "'", "\x83" => 'f',
393
      "\x84" => '"',  "\x85" => "...",  "\x86" => "+", "\x87" => "#",
394
      "\x88" => "^",  "\x89" => "0/00", "\x8A" => "S", "\x8B" => "<",
395
      "\x8C" => "OE", "\x8D" => " ",    "\x8E" => "Z", "\x8F" => " ",
396
      "\x90" => " ",  "\x91" => "`",    "\x92" => "'", "\x93" => '"',
397
      "\x94" => '"',  "\x95" => "*",    "\x96" => "-", "\x97" => "--",
398
      "\x98" => "~",  "\x99" => "(TM)", "\x9A" => "s", "\x9B" => ">",
399
      "\x9C" => "oe", "\x9D" => " ",    "\x9E" => "z", "\x9F" => "Y"));
400
}
401
 
402
/* +--Fin du code ----------------------------------------------------------------------------------------+
403
*
404
* $Log: not supported by cvs2svn $
405
* Revision 1.10  2007-12-13 14:10:51  alexandre_tb
406
* Fusion avec la livraison AHA : 13 decembre 2007
407
*
408
* Revision 1.9  2007-12-03 14:54:14  jp_milcent
409
* Fusion avec la livraison AHA : 3 décembre 2007
410
*
411
* Revision 1.7.2.2  2007-12-03 14:52:21  jp_milcent
412
* Correction bogue : & dans les urls.
413
*
414
* Revision 1.8  2007-11-30 14:17:30  jp_milcent
415
* Fusion avec la livraison AHA : 30 novembre 2007
416
*
417
* Revision 1.7.2.1  2007-11-30 14:15:02  jp_milcent
418
* Amélioration du décodage utf8.
419
*
420
* Revision 1.7  2007-07-25 15:09:44  jp_milcent
421
* Fusion avec la livraison Narmer.
422
*
423
* Revision 1.5.2.4  2007-07-25 15:07:52  jp_milcent
424
* Correction problème url.
425
*
426
* Revision 1.5.2.3  2007-07-25 14:50:21  jp_milcent
427
* Corrections, meilleure utilisation de XML_Feed_Parser.
428
*
429
* Revision 1.5.2.2  2007-07-25 09:45:07  jp_milcent
430
* Utilisation de XML_Feed_Parser de Pear pour l'applette Syndication.
431
*
432
* Revision 1.6  2007-06-25 12:15:07  alexandre_tb
433
* merge from narmer
434
*
435
* Revision 1.5.2.1  2007-06-06 15:24:37  jp_milcent
436
* Amélioration de la compatibilité avec les anciennes version des balises de l'applette syndication.
437
*
438
* Revision 1.5  2007-04-20 12:50:18  florian
439
* correction bugs suite au merge
440
*
441
* Revision 1.4  2007/03/28 15:53:27  florian
442
* correction pb date, encodage utf-8
443
*
444
* Revision 1.3  2007/01/23 14:17:19  alexandre_tb
445
* backport : hack pour rattraper les dates du type 01012005 parsees par magpie
446
* lorsque les flux donne des dates au format iso
447
*
448
* Revision 1.2  2006/12/13 17:20:51  jp_milcent
449
* Correction bogue : paramètre nb non pris en compte
450
*
451
* Revision 1.1  2006/12/13 17:06:36  jp_milcent
452
* Ajout de l'applette Syndication.
453
*
454
*
455
* +-- Fin du code ----------------------------------------------------------------------------------------+
456
*/
457
?>