<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: newsapplet.php,v 1.5.2.2 2004/04/18 12:45:44 ajvwaay Exp $



# News Applet 1.0
#
# Add-on to phpnuke 4.4 or later
# Display news headlines in a applet.
#
# by Sivaprasad R.L
# http://netlogger.net


function get_applet_headlines( $cache_file )
{
	global $mpnTables;

    $cache_time = 3600;
    $target = "new";
    $max_items = 10;
    $max_length = 100;
    $time = split(" ", microtime());
	srand((double)microtime()*1000000);
    $cache_time_rnd = 300 - rand(0, 600);
    $separ = "- ";
    $boxstuff = '';

    $cache_file = _DP_CACHE_PAR . $cache_file;

	if ( ! is_file( $cache_file ) || ( ( @filectime( $cache_file ) + ( $cache_time - $time[1] ) + $cache_time_rnd ) < 0 ) || @filesize( $cache_file ) <= 0 )
	{
        if ( (bool)FALSE === ( $fpwrite = @fopen( $cache_file, 'w+b' ) ) )
        {
             return (bool)FALSE;
        }
        else
        {
            $result = mysql_query("select hid, sitename, url, headlinesurl from ".$mpnTables['headlines']." where status='1'");

        	while ( list( $hid, $sitename, $url, $headlinesurl) = mysql_fetch_row($result) )
            {
                $items = 0;

                $cache_file1 = 'newsheadline-' . $hid . '.cache';

                $synd = new RSStoHTML($headlinesurl,$cache_file1,$cache_time,$max_items, $max_length);

                $boxstuff .= $synd->getHtml();

                $cache_file1 = _DP_CACHE_PAR . $cache_file1;

            	if ( @filesize( $cache_file1 ) > 0)
                {
                    if ( ( $fpread = @fopen( $cache_file1, 'rb' ) ) == false )
                    {
                        fclose($fpread);
                        fclose($fpwrite);

                         return (bool)FALSE;
                    }
                    else
                    {
                    	@fputs($fpwrite, "<a href=\"$url\" target=$target>$sitename</a><br><br>");

                    	while(!feof($fpread) )
                        {
                            $buffer = ltrim(Chop(@fgets($fpread, 256)));

                        	if (($buffer == "<item>") && ($items < $max_items))
                            {
                                $title = ltrim(Chop(fgets($fpread, 256)));
                                $link = ltrim(Chop(fgets($fpread, 256)));
                                $title = ereg_replace( "<title>", "", $title );
                                $title = ereg_replace( "</title>", "", $title );
                                $title = ereg_replace( "\"", "\\\"", $title );
                                $title = stripslashes($title);
                                $link = ereg_replace( "<link>", "", $link );
                                $link = ereg_replace( "</link>", "", $link );
                                $link = stripslashes($link);
                            	@fputs($fpwrite, "$separ<a href=\"$link\" target=$target>$title</a><br><br>\r\n");
                                $items++;
                            }
                        }
                    }

                	@fputs($fpwrite, "<br><br>\r\n");
                	fclose($fpread);
                }
            }

            fclose($fpwrite);
        }
    }
    else
    {
        return (bool)TRUE;
    }

    return (bool)TRUE;
}


function oldAppletNews($appletstorynum)
{
    global $locale, $oldnum, $storyhome, $cookie, $categories, $cat, $mpnTables,$phpEx;

    $storynum = $storyhome;

    $boxstuff = '';

    if ($categories == 1)
    {
        $sel = "where catid='$cat'";
    } else {
        $sel = "";
    }
    $result = mysql_query("select sid, title, time, comments from ".$mpnTables['stories']." $sel order by time desc limit $storynum, $oldnum");
    $vari = 0;
    $options = NULL;

    if (isset($cookie[4])) {
        $options .= "&mode=$cookie[4]";
    } else {
        $options .= "&mode=thread";
    }
    if (isset($cookie[5])) {
        $options .= "&order=$cookie[5]";
    } else {
        $options .= "&order=0";
    }
    if (isset($cookie[6])) {
        $options .= "&thold=$cookie[6]";
    } else {
        $options .= "&thold=0";
    }

    $time2 = NULL;
    $a = '';

    while(list($sid, $title, $time, $comments) = mysql_fetch_row($result))
    {
        setlocale (LC_TIME, "$locale");
        ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $time, $datetime2);
        $datetime2 = strftime("".translate("datestring2")."", mktime($datetime2[4],$datetime2[5],$datetime2[6],$datetime2[2],$datetime2[3],$datetime2[1]));
        $datetime2 = ucfirst($datetime2);

        if( $time2 == $datetime2 )
        {
            $boxstuff .= "<br> - <a href=\"article.$phpEx?sid=$sid$options\">$title</a><br>\n";
        }
        else
        {
            if( $a == "" )
            {
                $boxstuff .= "<br><br><b>$datetime2</b><br><br> - <a href=\"article.$phpEx?sid=$sid$options\">$title</a><br>\n";
                $time2 = $datetime2;
                $a = 1;
            }
            else
            {
                $boxstuff .= "<br><br><b>$datetime2</b><br><br> - <a href=\"article.$phpEx?sid=$sid$options\">$title</a><br>\n";
                $time2 = $datetime2;
            }
        }

        $vari++;

        if ($vari==$oldnum)
        {
            if (isset($cookie[3]))
            {
                $storynum = $cookie[3];
            } else {
                $storynum = $storyhome;
            }

            $min = $oldnum + $storynum;

            $boxstuff .= "<br><br><p class='t-right'><a href=\"search.$phpEx?min=$min&type=stories&category=$cat\"><b>".translate("Older Articles")."</b></a></p>\n";
        }
    }

    $filename = 'oldnewsapplet.txt';

    if ( $boxstuff == '' || ! $_SERVER['F_LIB'] -> writeFile( $filename, 'w+b', $boxstuff, _DP_CACHE_PAR ) )
    {
        return (bool)FALSE;
    }

    return (bool)TRUE;
}


?>
