/
RssHandler.php
executable file
·193 lines (177 loc) · 8.31 KB
/
RssHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Core\YesWikiHandler;
// TODO use Symfony XmlEncoder instead
// https://symfony.com/doc/current/components/serializer.html#the-xmlencoder
class RssHandler extends YesWikiHandler
{
public function run()
{
if (!$this->wiki->HasAccess("read") || !$this->wiki->page) {
return null;
}
$urlrss = $this->wiki->href('rss');
if (isset($_GET['id'])) {
$id = filter_input(INPUT_GET, 'id', FILTER_UNSAFE_RAW);
$id = ($id === false) ? "" : htmlspecialchars(strip_tags($id));
} elseif (isset($_GET['id_typeannonce'])) {
$id = filter_input(INPUT_GET, 'id_typeannonce', FILTER_UNSAFE_RAW);
$id = ($id === false) ? "" : htmlspecialchars(strip_tags($id));
}
if (!empty($id) && strval($id) == strval(intval($id))) {
$urlrss .= '&id='.$id;
} else {
$id = '';
}
if (isset($_GET['nbitem'])) {
$nbitem = $_GET['nbitem'];
$urlrss .= '&nbitem='.$nbitem;
} else {
$nbitem = $this->wiki->config['BAZ_NB_ENTREES_FLUX_RSS'];
}
if (isset($_GET['utilisateur'])) {
$utilisateur = $_GET['utilisateur'];
$urlrss .= '&utilisateur='.$utilisateur;
} else {
$utilisateur = '';
}
// chaine de recherche
$q = '';
if (isset($_GET['q']) and !empty($_GET['q'])) {
$q = $_GET['q'];
$urlrss .= '&q='.$q;
}
if (isset($_GET['query'])) {
$query = $_GET['query'];
$urlrss .= '&query='.$query;
$tabquery = array();
$tableau = array();
$tab = explode('|', $query); //découpe la requete autour des |
foreach ($tab as $req) {
$tabdecoup = explode('=', $req, 2);
$tableau[$tabdecoup[0]] = trim($tabdecoup[1]);
}
$query = array_merge($tabquery, $tableau);
} else {
$query = '';
}
$tableau_flux_rss = $this->getService(EntryManager::class)->search(
[
'queries'=>$query,
'formsIds'=>$id,
'user'=>$utilisateur,
'keywords'=>$q
],
true, // filter on read ACL
true // use Guard
);
$GLOBALS['ordre'] = 'desc';
$GLOBALS['champ'] = 'date_creation_fiche';
usort($tableau_flux_rss, 'champCompare');
// Limite le nombre de résultat au nombre de fiches demandées
$tableau_flux_rss = array_slice($tableau_flux_rss, 0, $nbitem);
// setlocale() pour avoir les formats de date valides (w3c) --julien
setlocale(LC_TIME, 'C');
$xml = XML_Util::getXMLDeclaration('1.0', 'UTF-8', 'yes');
$xml .= "\r\n ";
$xml .= XML_Util::createStartElement('rss', array('version' => '2.0',
'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', ));
$xml .= "\r\n ";
$xml .= XML_Util::createStartElement('channel');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('title', null, $this->sanitize(_t('BAZ_DERNIERE_ACTU')));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('link', null, $this->sanitize($this->wiki->config['BAZ_RSS_ADRESSESITE']));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('description', null, $this->sanitize($this->wiki->config['BAZ_RSS_DESCRIPTIONSITE']));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('language', null, 'fr-FR');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('copyright', null, 'Copyright (c) '.date('Y').' '. htmlentities($this->wiki->config['BAZ_RSS_NOMSITE']));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('lastBuildDate', null, date('r'));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('docs', null, 'http://www.stervinou.com/projets/rss/');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('category', null, $this->wiki->config['BAZ_RSS_CATEGORIE']);
$xml .= "\r\n ";
$xml .= XML_Util::createTag('managingEditor', null, $this->wiki->config['BAZ_RSS_MANAGINGEDITOR']);
$xml .= "\r\n ";
$xml .= XML_Util::createTag('webMaster', null, $this->wiki->config['BAZ_RSS_WEBMASTER']);
$xml .= "\r\n ";
$xml .= XML_Util::createTag('ttl', null, '60');
$xml .= "\r\n ";
$xml .= XML_Util::createStartElement('image');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('title', null, $this->sanitize(_t('BAZ_DERNIERE_ACTU')));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('url', null, $this->wiki->config['BAZ_RSS_LOGOSITE']);
$xml .= "\r\n ";
$xml .= XML_Util::createTag('link', null, $this->wiki->config['BAZ_RSS_ADRESSESITE']);
$xml .= "\r\n ";
$xml .= XML_Util::createEndElement('image');
if (count($tableau_flux_rss) > 0) {
// Creation des items : titre + lien + description + date de publication
foreach ($tableau_flux_rss as $ligne) {
$xml .= "\r\n ";
$xml .= XML_Util::createStartElement('item');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('title', null, str_replace('&', '&', $this->sanitize($ligne['bf_titre'])));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('link', null, '<![CDATA[' . $this->wiki->href('', $ligne['id_fiche']) . ']]>');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('guid', null, '<![CDATA[' . $this->wiki->href('', $ligne['id_fiche']) . ']]>');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('dc:creator', null, $ligne['owner']);
$xml .= "\r\n ";
$xml .= XML_Util::createTag(
'description',
null,
'<![CDATA['.preg_replace(
'/data-id=".*"/Ui',
'',
$this->sanitize($this->getService(EntryController::class)->view($ligne))
).']]>'
);
$xml .= "\r\n ";
$xml .= XML_Util::createTag('pubDate', null, date('r', strtotime($ligne['date_creation_fiche'])));
$xml .= "\r\n ";
$xml .= XML_Util::createEndElement('item');
}
} else {
//pas d'annonces
$xml .= "\r\n ";
$xml .= XML_Util::createStartElement('item');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('title', null, $this->sanitize(_t('BAZ_PAS_DE_FICHES')));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('link', null, '<![CDATA['.$this->wiki->config['base_url'].$this->wiki->config['root_page'].']]>');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('guid', null, '<![CDATA['.$this->wiki->config['base_url'].$this->wiki->config['root_page'].']]>');
$xml .= "\r\n ";
$xml .= XML_Util::createTag('description', null, $this->sanitize(_t('BAZ_PAS_DE_FICHES')));
$xml .= "\r\n ";
$xml .= XML_Util::createTag('pubDate', null, date('r', strtotime('01/01/%Y')));
$xml .= "\r\n ";
$xml .= XML_Util::createEndElement('item');
}
$xml .= "\r\n ";
$xml .= XML_Util::createEndElement('channel');
$xml .= "\r\n ";
$xml .= XML_Util::createEndElement('rss');
header('Content-type: text/xml; charset=UTF-8');
return str_replace(
'</image>',
'</image>'."\n"
.' <atom:link href="'. htmlentities((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])
.'" rel="self" type="application/rss+xml" />',
$this->sanitize($xml, ENT_QUOTES, 'UTF-8')
);
}
private function sanitize($string)
{
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
return $string;
}
}