]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/News.php
3dcd5324c961f01b724df5e4362c5599a8a4e31b
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette's news from RSS feed
10 * Copyright © 2011-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2011-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2011-11-11
43 * News class from rss feed for galette
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2011-2023 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since Available since 0.7dev - 2011-11-11
56 private $cache_filename = '%feed.cache';
58 //number of hours until cache will be invalid
59 private $cache_timeout = 24;
60 private $feed_url = null;
62 private $stream_opts = [
71 * @param string $url Feed URL
72 * @param boolean $nocache Do not try to cache
74 public function __construct($url, $nocache = false)
76 $this->feed_url
= $this->getFeedURL($url);
78 //only if cache should be used
79 if ($nocache === false && GALETTE_MODE
!== 'DEV') {
80 if (!$this->checkCache()) {
91 * Check if cache is valid
95 private function checkCache()
97 $cfile = $this->getCacheFilename();
98 if (file_exists($cfile)) {
100 $dformat = 'Y-m-d H:i:s';
101 $mdate = \DateTime
::createFromFormat(
108 $expire = $mdate->add(
109 new \
DateInterval('PT' . $this->cache_timeout
. 'H')
111 $now = new \
DateTime();
112 $has_expired = $now > $expire;
113 return !$has_expired;
114 } catch (Throwable
$e) {
116 'Unable check cache expiry. Are you sure you have ' .
117 'properly configured PHP timezone settings on your server?',
128 * Creates/update the cache
132 private function makeCache()
135 $cfile = $this->getCacheFilename();
136 $stream = fopen($cfile, 'w+');
148 * Loads entries from cache
152 private function loadCache()
154 $cfile = $this->getCacheFilename();
155 $data = unserialize(file_get_contents($cfile));
157 $refresh_cache = false;
158 $this->posts
= $data;
159 //check if posts were cached
160 if (!is_array($this->posts
) ||
count($this->posts
) == 0) {
162 $refresh_cache = true;
165 if ($refresh_cache === true) {
171 * Complete path to cache file
175 private function getCacheFilename()
177 return GALETTE_CACHE_DIR
. str_replace(
179 md5($this->feed_url
),
180 $this->cache_filename
189 private function parseFeed()
192 if (!ini_get('allow_url_fopen')) {
193 throw new \
RuntimeException(
194 'allow_url_fopen is set to false; cannot load news.'
198 $context = stream_context_create($this->stream_opts
);
199 $data = file_get_contents($this->feed_url
, false, $context);
201 throw new \
Exception();
204 $xml = simplexml_load_string($data);
206 throw new \
Exception();
211 if (isset($xml->entry
)) {
212 //Reading an atom feed
213 foreach ($xml->entry
as $post) {
215 'title' => (string)$post->title
,
216 'url' => (string)$post->link
['href'],
217 'date' => (string)$post->published
219 if (count($posts) == $this->show
) {
223 } elseif (isset($xml->channel
->item
)) {
225 foreach ($xml->channel
->item
as $post) {
227 'title' => (string)$post->title
,
228 'url' => (string)$post->link
,
229 'date' => (string)$post->pubDate
231 if (count($posts) == $this->show
) {
236 throw new \
RuntimeException(
240 $this->posts
= $posts;
241 } catch (Throwable
$e) {
243 'Unable to load feed from "' . $this->feed_url
.
244 '" :( | ' . $e->getMessage(),
256 public function getPosts()
262 * Get feed url, handle Galette website to check available langs
264 * @param string $url Requested URL
268 public function getFeedURL($url)
272 if (strpos($url, 'galette.eu') !== false ||
trim($url) == '') {
273 $url = 'https://galette.eu/site';
274 } elseif (strpos($url, 'localhost:4000') !== false) {
275 $url = 'http://localhost:4000/site';
281 $galette_website_langs = $url . '/langs.json';
282 $context = stream_context_create($this->stream_opts
);
283 $langs = json_decode(file_get_contents($galette_website_langs, false, $context));
285 if ($i18n->getAbbrev() != 'en' && in_array($i18n->getAbbrev(), $langs)) {
286 $url .= '/' . $i18n->getAbbrev();
289 } catch (Throwable
$e) {
291 'Unable to load feed languages from "' . $url .
292 '" :( | ' . $e->getMessage(),