dolibarr/htdocs/core/boxes/box_external_rss.php

218 lines
6.9 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>
2015-01-25 00:20:58 +00:00
* Copyright (C) 2015 Frederic France <frederic.france@free.fr>
*
* 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
2018-08-22 08:51:55 +00:00
/**
* @var DoliDB Database handler.
*/
public $db;
2019-02-09 14:24:21 +00:00
public $paramdef; // Params of box definition (not user params)
2011-06-17 19:14:47 +00:00
public $info_box_head = array();
public $info_box_contents = array();
2013-04-23 14:18:26 +00:00
/**
* Constructor
*
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
public function __construct($db, $param)
{
$this->db = $db;
$this->paramdef = $param;
}
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
$url = $conf->global->$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();
$title = $langs->trans("BoxTitleLastRssInfos", $max, $conf->global->$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>';
$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']);
$date = $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['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']
}
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 = utf8_encode($title);
} elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
$title = utf8_decode($title);
}
$title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Gere probleme des apostrophes mal codee/decodee par utf8
$title = preg_replace("/^\s+/", "", $title); // Supprime espaces de debut
$this->info_box_contents["$href"] = "$title";
$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 = utf8_encode($description);
} elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
$description = utf8_decode($description);
}
$description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
$description = preg_replace("/^\s+/", "", $description);
$description = str_replace("\r\n", "", $description);
$tooltip .= '<br>'.$description;
$this->info_box_contents[$line][0] = array(
'td' => 'class="left" width="16"',
'logo' => $this->boximg,
'url' => $href,
'tooltip' => $tooltip,
'target' => 'newrss',
);
$this->info_box_contents[$line][1] = array(
'td' => '',
'text' => $title,
'url' => $href,
'tooltip' => $tooltip,
'maxlength' => 64,
'target' => 'newrss',
);
$this->info_box_contents[$line][2] = array(
'td' => 'class="right nowrap"',
'text' => $date,
);
}
}
2011-06-17 19:14:47 +00:00
2011-11-23 17:28:14 +00:00
/**
* Method to show box
*
* @param array $head Array with properties of box title
* @param array $contents Array with properties of box lines
2016-06-27 11:57:10 +00:00
* @param int $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);
}
}