PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/boxes/box_external_rss.php

https://github.com/asterix14/dolibarr
PHP | 162 lines | 94 code | 22 blank | 46 comment | 22 complexity | f87b6f0ae6f1adbcbc838b11ebad0236 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Eric Seigne <erics@rycks.com>
  4. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/boxes/box_external_rss.php
  22. * \ingroup external_rss
  23. * \brief Fichier de gestion d'une box pour le module external_rss
  24. */
  25. include_once(DOL_DOCUMENT_ROOT."/core/class/rssparser.class.php");
  26. include_once(DOL_DOCUMENT_ROOT."/core/boxes/modules_boxes.php");
  27. class box_external_rss extends ModeleBoxes {
  28. var $boxcode="lastrssinfos";
  29. var $boximg="object_rss";
  30. var $boxlabel;
  31. var $depends = array("externalrss");
  32. var $db;
  33. var $param;
  34. var $info_box_head = array();
  35. var $info_box_contents = array();
  36. /**
  37. * Constructeur de la classe
  38. */
  39. function box_external_rss($DB,$param)
  40. {
  41. global $langs;
  42. $langs->load("boxes");
  43. $this->db=$DB;
  44. $this->param=$param;
  45. $this->boxlabel=$langs->trans("BoxLastRssInfos");
  46. }
  47. /**
  48. * Load information for box into memory to show them later with this->showBox method.
  49. *
  50. * @param int $max Max numbe rof records to load
  51. * @param int $cachedelay Delay we accept for cache file
  52. */
  53. function loadBox($max=5, $cachedelay=3600)
  54. {
  55. global $user, $langs, $conf;
  56. $langs->load("boxes");
  57. $this->max=$max;
  58. // On recupere numero de param de la boite
  59. preg_match('/^([0-9]+) /',$this->param,$reg);
  60. $site=$reg[1];
  61. // Create dir nor required
  62. // documents/externalrss is created by module activation
  63. // documents/externalrss/tmp is created by rssparser
  64. // Get RSS feed
  65. $url=@constant("EXTERNAL_RSS_URLRSS_".$site);
  66. $rssparser=new RssParser($db);
  67. $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
  68. // INFO on channel
  69. $description=$rssparser->getDescription();
  70. $link=$rssparser->getLink();
  71. $title=$langs->trans("BoxTitleLastRssInfos",$max, @constant("EXTERNAL_RSS_TITLE_". $site));
  72. if ($result < 0 || ! empty($rssparser->error))
  73. {
  74. // Show warning
  75. $title.=" ".img_error($langs->trans("FailedToRefreshDataInfoNotUpToDate",($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown"))));
  76. $this->info_box_head = array('text' => $title,'limit' => 0);
  77. }
  78. else
  79. {
  80. $this->info_box_head = array('text' => $title,
  81. 'sublink' => $link, 'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown")), 'subpicto'=>'object_bookmark');
  82. }
  83. // INFO on items
  84. $items=$rssparser->getItems();
  85. $nbitems=count($items);
  86. for($i = 0; $i < $max && $i < $nbitems; $i++)
  87. {
  88. $item = $items[$i];
  89. // Feed common fields
  90. $href = $item['link'];
  91. $title = urldecode($item['title']);
  92. $date = $item['date_timestamp']; // date will be empty if conversion into timestamp failed
  93. if ($rssparser->getFormat() == 'rss') // If RSS
  94. {
  95. if (! $date && isset($item['pubdate'])) $date=$item['pubdate'];
  96. if (! $date && isset($item['dc']['date'])) $date=$item['dc']['date'];
  97. //$item['dc']['language']
  98. //$item['dc']['publisher']
  99. }
  100. if ($rssparser->getFormat() == 'atom') // If Atom
  101. {
  102. if (! $date && isset($item['issued'])) $date=$item['issued'];
  103. if (! $date && isset($item['modified'])) $date=$item['modified'];
  104. //$item['issued']
  105. //$item['modified']
  106. //$item['atom_content']
  107. }
  108. if (is_numeric($date)) $date=dol_print_date($date,"dayhour");
  109. $isutf8 = utf8_check($title);
  110. if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $title=utf8_encode($title);
  111. elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $title=utf8_decode($title);
  112. $title=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$title); // Gere probleme des apostrophes mal codee/decodee par utf8
  113. $title=preg_replace("/^\s+/","",$title); // Supprime espaces de debut
  114. $this->info_box_contents["$href"]="$title";
  115. $this->info_box_contents[$i][0] = array('td' => 'align="left" width="16"',
  116. 'logo' => $this->boximg,
  117. 'url' => $href,
  118. 'target' => 'newrss');
  119. $this->info_box_contents[$i][1] = array('td' => 'align="left"',
  120. 'text' => $title,
  121. 'url' => $href,
  122. 'maxlength' => 64,
  123. 'target' => 'newrss');
  124. $this->info_box_contents[$i][2] = array('td' => 'align="right" nowrap="1"',
  125. 'text' => $date);
  126. }
  127. }
  128. function showBox($head = null, $contents = null)
  129. {
  130. parent::showBox($this->info_box_head, $this->info_box_contents);
  131. }
  132. }
  133. ?>