#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 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
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


#This is the don philippe news interface
#It is the abstraction layer which communicates to the underlying layers that 
#take care about the databases and files and provides and prepares the data for Pagenews
#
#Current databases supported:
#-the donstdlib::donkvparser which provide a simple datafile format

#others (like RSS,MySQL) are planned for the future
#
#even though there are different data sources are supported, a Pagenews feed is allways
#a donkvparser file which contains header information (for example the RSS URL) 

#in addition, this package manages the storage of comments.
#each posting has its own "feed" in the comment path which stores the comments for the posting

#when talking about "feeds", news posting feeds are ment.
#comment feeds are mentioned each time.



#the constructor parses the file $_[1].donfeed previously and determines
#which type of feed is required.
#it gives back a reference of the needed parser
#this will be determined by the field "PAGENEWS FEEDTYPE" in the first header block
#when new() or create() is called, the other methods in this class aren't available! 


package dnfeeds;

	$feedpath = "restricted/feeds";				#package-wide global var which contains the folder where the feeds are saved
	$compath = "restricted/feeds/comments";		#path were comment-feeds are saved

require donkvparser;
require donstdlib;

require dnfeeds_dkvp;
require dnfeeds_common;

require dnuser;

	

########################################################################
#opens a feed dkvp-file, parses it, determines the type and creates and returns the fitting feed object
#$_[1] must be the feed name.
#for news feeds, leave $_[2] emty. For comment feeds, give the news posting id in $_[2],
#and the comment feed is opened. Comment feeds are specific to one posting ID.
#They can be handled just like a normal feed.

#for comment-feeds are autocreated when non-existent.
#NEWS FEEDS ARE NOT!

sub new{
	my $dkvp = donkvparser->new();
	my $taken_feedpath = $feedpath;
	my $feed;	
	
	#for security...
	donstdlib::check_filename($_[1]);
	donstdlib::check_filename($_[2]);
	
	my $feed_file = "$feedpath/$_[1].dnfeed";						#default: news feed
	#comment-specific:
	if($_[2] ne ""){
		#create if not existent
		$taken_feedpath = $compath;
		$feed_file = "$compath/$_[1].$_[2].dnfeed";				#(hypothetical) comment feed file name
		if(donstdlib::make_sure_that_exists($feed_file,"check") == 0){
			return dnfeeds->create($_[1],"DNFEEDS_DKVP",$_[2])
		};		

	};

	
	#same for all:
		if(donstdlib::make_sure_that_exists($feed_file,"check") == 0){
			return donstdlib::error("donstdlib_filedoesnotexist",$feed_file);
		}
		
	$dkvp->read($feed_file);
	my $feedtype = $dkvp->get_value(0,"PAGENEWS FEEDTYPE","header");
	if($feedtype eq "PAGENEWS NATIVE DKVP"){
			$feed = dnfeeds_dkvp->new($dkvp,$_[1]);
	}else{
		return donstdlib::error("donfeeds_unsupportedfeed",$feedtype);
	};
	$feed->{'feedname'} = $_[1];
	$feed->{'feedpath'} = $taken_feedpath;

	return $feed;	
}; 


########################################################################
#creates a new feed, name in $_[1]
#the type must be in $_[2] (not supported yet, use DNFEEDS_DKVP for DKVP feed)
#for news feeds, leave $_[3] emty. For comment feeds, give the news posting id in $_[3],
#and the comment feed is opened.

sub create{
	my $feed;
	
	donstdlib::check_filename($_[1]);
	donstdlib::check_filename($_[3]);


	if($_[3] eq ""){
		#normal news feed
		$feed = dnfeeds_dkvp->create_feed($_[1],$feedpath);	
	}else{
		#comment feed
		$feed = dnfeeds_dkvp->create_feed("$_[1].$_[3]",$compath);	
	};
		
	return $feed;
};

########################################################################
#returns a list of all feeds
#if $_[1] is non-emty, only the feedtype $_[1] is returned.
#if $_[2] is non-empty, the feed must support the key $_[2]
#(which means the header - key $_[2] must be "yes".

#if $_[3] contains an username, only the feeds which are usable by the username $_[3] are returned
#if $_[4] is non-empty, comment-feeds are returned. in this case, the endings are included
sub get_list_of_feeds{
	my $path;
	if($_[4] eq ""){
	$path = $feedpath;
	}else{
	$path = $compath;
	};
	my @feeds = donstdlib::glob_directory($path,"cutoffendings");

	my	@retarr;	
	my $user = dnuser->new();
	
	foreach $feed (@feeds){
		my $dkvp = donkvparser->new();
			$dkvp->read("$path/$feed.dnfeed");
		my $do_not_add = 0; 				
		if($dkvp->get_value(0,"donotlist","header") eq "yes"){
			$do_not_add = 1;		
		};
		if(($_[1] ne "")&&($dkvp->get_value(0,"PAGENEWS FEEDTYPE","header") ne $_[1])){
			$do_not_add = 1;
		};
		if(($_[2] ne "")&&($dkvp->get_value(0,$_[2],"header") ne "yes")){
			$do_not_add = 1;
		};
		if(($_[3] ne "")&&($user->check_use_of_feed($_[3],$feed,"check") == 0)){
			$do_not_add = 1;
		};
		if($do_not_add != 1){
			push (@retarr,$feed);
		};
	};
	
	return @retarr;
};
########################################################################
#deletes the feed name $_[1]
#must be called without calling new() before.
#for news feeds, leave $_[2] emty. For comment feeds, give the news posting id in $_[2],
#and the comment feed is opened.

#actually, this function should refer to dnfeeds_dkvp, as soon as there are more
#than one feed types.
sub delete_feed{
	if($_[2] eq ""){
		#normal news feed
		donstdlib::delete_file("$feedpath/$_[1].dnfeed");
	}else{
		#comment feed
		donstdlib::delete_file("$compath/$_[1].$_[2].dnfeed","donotgramble");
	};
};

1;

