<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied headlines MAXIMUS directly...');

function headlines($bid, $cenbox=0) {
  global $prefix, $db;
  $bid = $db->XMFnum($bid);
  $result = $db->sql_query("SELECT title, content, url, refresh, time FROM ".TABLE_BLOCKS." WHERE bid='$bid'");
  $row = $db->sql_fetchrow($result);
  $title = stripslashes(check_html($row['title'], "nohtml"));
  $content = stripslashes($row['content']);
  $url = stripslashes($row['url']);
  $refresh = $db->XMFnum($row['refresh']);
  $otime = $row['time'];
  $past = time()-$refresh;
  if($otime < $past) {
    $btime = time();
    $rdf = parse_url($url);
    $fp = fsockopen($rdf['host'], 80, $errno, $errstr, 15);
    if(!$fp) {
      $content = "";
      $db->sql_query("UPDATE ".TABLE_BLOCKS." SET content='$content', time='$btime' WHERE bid='$bid'");
      $cont = 0;
      if($cenbox == 0) {
        themesidebox($title, $content);
      } elseif($cenbox == 2 AND function_exists(themeleftbox)) {
        themeleftbox($title, $content);
      } elseif($cenbox == 3 AND function_exists(themerightbox)) {
        themerightbox($title, $content);
      } else {
        themecenterbox($title, $content);
      }
      return;
    }
    if($fp) {
      if($rdf['query'] != '') {
        $rdf['query'] = "?" . $rdf['query'];
      }
      fputs($fp, "GET " . $rdf['path'] . $rdf['query'] . " HTTP/1.0\r\n");
      fputs($fp, "HOST: " . $rdf['host'] . "\r\n\r\n");
      $string = "";
      while(!feof($fp)) {
        $pagetext = fgets($fp,300);
        $string .= chop($pagetext);
      }
      fputs($fp,"Connection: close\r\n\r\n");
      fclose($fp);
      $items = explode("</item>",$string);
      $content = "<font class=\"content\">";
      for($i=0;$i<10;$i++) {
        $link = ereg_replace(".*<link>","",$items[$i]);
        $link = ereg_replace("</link>.*","",$link);
        $title2 = ereg_replace(".*<title>","",$items[$i]);
        $title2 = ereg_replace("</title>.*","",$title2);
        $title2 = stripslashes($title2);
        if($items[$i] == "" AND $cont != 1) {
          $content = "";
          $db->sql_query("UPDATE ".TABLE_BLOCKS." SET content='$content', time='$btime' WHERE bid='$bid'");
          $cont = 0;
          if($cenbox == 0) {
            themesidebox($title, $content);
          } elseif($cenbox == 2 AND function_exists('themeleftbox')) {
            themeleftbox($title, $content);
          } elseif($cenbox == 3 AND function_exists('themerightbox')) {
            themerightbox($title, $content);
          } else {
            themecenterbox($title, $content);
          }
          return;
        } else {
          if(strcmp($link,$title2) AND $items[$i] != "") {
            $cont = 1;
            $content .= "<strong><big>&middot;</big></strong><a href=\"$link\" target=\"new\">$title2</a><br />\n";
          }
        }
      }
    }
    $db->sql_query("UPDATE ".TABLE_BLOCKS." SET content='$content', time='$btime' WHERE bid='$bid'");
  }
  $siteurl = ereg_replace("http://","",$url);
  $siteurl = explode("/",$siteurl);
  if(($cont == 1) OR ($content != "")) {
    $content .= "<br /><a href=\"http://$siteurl[0]\" target=\"blank\"><b>"._HREADMORE."</b></a></font>";
  } elseif(($cont == 0) OR ($content == "")) {
    $content = "<font class=\"content\">"._RSSPROBLEM."</font>";
  }
  if($cenbox == 0) {
    themesidebox($title, $content);
  } elseif($cenbox == 2 AND function_exists('themeleftbox')) {
    themeleftbox($title, $content);
  } elseif($cenbox == 3 AND function_exists('themerightbox')) {
    themerightbox($title, $content);
  } else {
    themecenterbox($title, $content);
  }
}