dolibarr/htdocs/core/boxes/box_external_rss.php

227 lines
7.7 KiB
PHP
Raw Normal View History

<?php
2005-01-31 12:21:44 +00:00
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
2011-01-24 19:28:28 +00:00
* Copyright (C) 2003 Eric Seigne <erics@rycks.com>
2008-05-02 20:24:38 +00:00
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
2018-10-27 12:43:12 +00:00
* Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2015-2025 Frédéric France <frederic.france@free.fr>
2025-03-09 21:42:59 +00:00
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2005-01-31 12:21:44 +00:00
/**
* \file htdocs/core/boxes/box_external_rss.php
2011-01-24 19:28:28 +00:00
* \ingroup external_rss
* \brief Fichier de gestion d'une box pour le module external_rss
*/
include_once DOL_DOCUMENT_ROOT.'/core/class/rssparser.class.php';
include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
2012-01-26 12:49:15 +00:00
/**
* Class to manage the box to show RSS feeds
*/
2012-02-20 08:42:57 +00:00
class box_external_rss extends ModeleBoxes
{
public $boxcode = "lastrssinfos";
public $boximg = "object_rss";
public $boxlabel = "BoxLastRssInfos";
public $depends = array("externalrss");
2005-02-27 17:57:21 +00:00
/**
* @var string
*/
public $paramdef; // Params of box definition (not user params)
2011-06-17 19:14:47 +00:00
/**
* Constructor
*
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
public function __construct($db, $param)
{
$this->db = $db;
$this->paramdef = $param;
$this->urltoaddentry = DOL_URL_ROOT.'/admin/external_rss.php';
}
2005-02-27 17:57:21 +00:00
/**
2011-11-23 17:28:14 +00:00
* Load data into info_box_contents array to show array later.
*
* @param int $max Maximum number of records to load
* @param int $cachedelay Delay we accept for cache file
* @return void
*/
public function loadBox($max = 5, $cachedelay = 3600)
{
global $user, $langs, $conf;
$langs->load("boxes");
$this->max = $max;
2011-06-17 19:14:47 +00:00
// On recupere numero de param de la boite
2021-04-07 17:38:54 +00:00
$reg = array();
preg_match('/^([0-9]+) /', $this->paramdef, $reg);
$site = $reg[1];
// Create dir nor required
// documents/externalrss is created by module activation
// documents/externalrss/tmp is created by rssparser
2011-06-17 19:14:47 +00:00
$keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
$keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
2017-11-10 23:28:00 +00:00
// Get RSS feed
2023-12-23 19:00:57 +00:00
$url = getDolGlobalString($keyforparamurl);
2011-06-17 19:14:47 +00:00
$rssparser = new RssParser($this->db);
2011-09-12 17:08:02 +00:00
$result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
// INFO on channel
$description = $rssparser->getDescription();
$link = $rssparser->getLink();
2023-12-23 19:00:57 +00:00
$title = $langs->trans("BoxTitleLastRssInfos", $max, getDolGlobalString($keyforparamtitle));
2021-02-23 21:03:23 +00:00
if ($result < 0 || !empty($rssparser->error)) {
// Show warning
$errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
2021-02-23 21:03:23 +00:00
if ($rssparser->error) {
$errormessage .= " - ".$rssparser->error;
}
$title .= " ".img_error($errormessage);
$this->info_box_head = array('text' => $title, 'limit' => 0);
} else {
$this->info_box_head = array(
'text' => $title,
'sublink' => $link,
'subtext' => $langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
'subpicto' => 'globe',
'target' => '_blank',
);
}
// INFO on items
$items = $rssparser->getItems();
//print '<pre>'.print_r($items,true).'</pre>';
2022-09-07 18:45:02 +00:00
// Loop on last items
$nbitems = count($items);
2021-02-23 21:03:23 +00:00
for ($line = 0; $line < $max && $line < $nbitems; $line++) {
$item = $items[$line];
// Feed common fields
$href = $item['link'];
$title = urldecode($item['title']);
2022-05-11 08:28:12 +00:00
$date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
2021-02-23 21:03:23 +00:00
if ($rssparser->getFormat() == 'rss') { // If RSS
if (!$date && isset($item['pubdate'])) {
$date = $item['pubdate'];
}
if (!$date && isset($item['pubDate'])) {
$date = $item['pubDate'];
}
2021-02-23 21:03:23 +00:00
if (!$date && isset($item['dc']['date'])) {
$date = $item['dc']['date'];
}
//$item['dc']['language']
//$item['dc']['publisher']
}
2021-02-23 21:03:23 +00:00
if ($rssparser->getFormat() == 'atom') { // If Atom
if (!$date && isset($item['issued'])) {
$date = $item['issued'];
}
if (!$date && isset($item['modified'])) {
$date = $item['modified'];
}
//$item['issued']
//$item['modified']
//$item['atom_content']
}
2024-04-23 23:11:22 +00:00
if (!is_numeric($date)) {
$timestamp = strtotime($date);
if ($timestamp > 0) {
$date = $timestamp;
}
}
2021-02-23 21:03:23 +00:00
if (is_numeric($date)) {
2021-05-24 22:29:32 +00:00
$date = dol_print_date($date, "dayhour", 'tzuserrel');
2021-02-23 21:03:23 +00:00
}
2011-06-17 19:14:47 +00:00
$isutf8 = utf8_check($title);
2021-02-23 21:03:23 +00:00
if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
$title = mb_convert_encoding($title, 'UTF-8', 'ISO-8859-1');
2021-02-23 21:03:23 +00:00
} elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
$title = mb_convert_encoding($title, 'ISO-8859-1');
2021-02-23 21:03:23 +00:00
}
$title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Manage issue of quotes improperly (de)coded in utf-8
$title = preg_replace("/^\s+/", "", $title); // Remove leading whitespace
$tooltip = $title;
$description = !empty($item['description']) ? $item['description'] : '';
$isutf8 = utf8_check($description);
2021-02-23 21:03:23 +00:00
if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
$description = mb_convert_encoding($description, 'UTF-8', 'ISO-8859-1');
2021-02-23 21:03:23 +00:00
} elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
$description = mb_convert_encoding($description, 'ISO-8859-1');
2021-02-23 21:03:23 +00:00
}
$description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
$description = preg_replace("/^\s+/", "", $description);
$description = str_replace("\r\n", "", $description);
$tooltip .= '<br>'.$description;
2025-03-09 21:42:59 +00:00
// Note: Escaping of value will be done by the showBox rendering method.
$this->info_box_contents[$line][0] = array(
'td' => 'class="left" width="16"',
2022-09-07 18:45:02 +00:00
'text' => img_picto('', 'rss'),
'url' => $href,
2025-03-09 21:42:59 +00:00
'tooltip' => $tooltip,
'target' => 'newrss',
);
$this->info_box_contents[$line][1] = array(
2022-09-07 18:45:02 +00:00
'td' => 'class="tdoverflowmax300"',
2025-03-09 21:42:59 +00:00
'text' => $title,
'url' => $href,
2025-03-09 21:42:59 +00:00
'tooltip' => $tooltip,
2022-09-07 18:45:02 +00:00
'maxlength' => 0,
'target' => 'newrss',
);
$this->info_box_contents[$line][2] = array(
2023-11-09 15:04:05 +00:00
'td' => 'class="right nowraponall"',
2025-03-09 21:42:59 +00:00
'text' => $date,
);
}
}
2011-06-17 19:14:47 +00:00
2011-11-23 17:28:14 +00:00
/**
* Method to show box. Called when the box needs to be displayed.
2011-11-23 17:28:14 +00:00
*
* @param ?array<array{text?:string,sublink?:string,subtext?:string,subpicto?:?string,picto?:string,nbcol?:int,limit?:int,subclass?:string,graph?:int<0,1>,target?:string}> $head Array with properties of box title
* @param ?array<array{tr?:string,td?:string,target?:string,text?:string,text2?:string,textnoformat?:string,tooltip?:string,logo?:string,url?:string,maxlength?:int,asis?:int<0,1>}> $contents Array with properties of box lines
* @param int<0,1> $nooutput No print, only return string
2017-06-12 12:09:00 +00:00
* @return string
2011-11-23 17:28:14 +00:00
*/
public function showBox($head = null, $contents = null, $nooutput = 0)
{
return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
}
}