<?php
if(!class_exists("x64Template"))
require_once("x64Template.php");
/**
* x64 Feeds Generator
*
* This class can be used to generate feeds in different formats, being able to expand the supported feeds by adding the templates using the current tags
*
* NOTES:
* *Caching is supported, it can read/write/manage, if you want to let it do everything, just start the class object, use init(), then set_data(), setup_generator() and finally auto_process(), which is going to use the cache if enabled, process the feeds and show them, by sending the header and finally exiting.
* *If you extend this class and you add a get_data function, it can be used to retrieve the data that is going to be used for the feeds generation
*
*
* @author Atomo64 - www.atomo64.tk
* @package x64 Feeds Generator
* @version 1.1.1
* @copyright Atomo64 2005-2006
*
* GNU General Public License (Version 2, October 2nd, 2005)
*
* 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,
* 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.
*
*/
class x64_feeds
{
/**
* The format to be used. It is the template name
*
* @var string
* @access public
*/
var $format;
/**
* The generated feeds
*
* @var string
* @access public
*/
var $feeds;
/**
* The path to where the feeds generator is located
*
* @var string
* @access public
*/
var $path;
/**
* The file name of the database(or the db name using the get_data function)
*
* @var string
* @access public
*/
var $database;
/**
* The database variables
*
* @var array
* @access private
*/
var $db_var=array();
/**
* Some data for the feeds
*
* @var array
* @access private
*/
var $setup=array();
/**
* The feeds generator configuration
*
* @var array
* @access private
*/
var $config=array();
/**
* Enables/disables data caching, used by the feeds generator, is recommended to keep it true
*
* @var bool
* @access public
*/
var $data_caching;
/**
* Array containing the content of some files, used for caching
*
* @var array
* @access private
* @see $this->data_caching
*/
var $data_cache=array();
/**
* Return the feeds
* Note: you must first call x64_feeds::process()
*
* @return string The feeds
* @access public
*/
function put()
{
return $this->feeds;
}
/**
* Sends the content header, the feeds and exits
* @access public
* @return void
*/
function send()
{
header('Content-type: '.(isset($this->config['mime'])) ? $this->config['mime'] : 'application/xml');
echo $this->feeds;
exit;
}
/**
* Retrieves the feeds from the cache, so it can be used with x64_feeds::put() or x64_feeds::send()
*
* @return bool
* @access public
*/
function read_from_cache()
{
if($this->path===null)
return false;
$file=$this->path."cache/".$this->database."_".$this->format.".php";
if(!file_exists($file))
return false;
//
//If the file was already loaded before then we don't try to read it
// again if data_caching is enabled
//
if($this->data_caching&&isset($this->data_cache[$file]))
$data=$this->data_cache[$file];
else
$data=file($file);
//
//If data_caching is enabled and the file wasn't already loaded
// before then we add to the data_cache
//
if($this->data_caching&&!isset($this->data_cache[$file]))
$this->data_cache[$file]=$data;
//
//We unserialize the cached data
//
$data=unserialize($data[1]);
$this->feeds=base64_decode($data['data']);
return true;
}
/**
* Retrieves when the cached feeds were created/generated
*
* @return mixed Returns false if failed to open the cache file, or the time
* @access public
*/
function get_cache_creation_time()
{
if($this->path===null)
return false;
$file=$this->path."cache/".$this->database."_".$this->format.".php";
if(!file_exists($file))
return false;
//
//If the file was already loaded before then we don't try to read it
// again if data_caching is enabled
//
if($this->data_caching&&isset($this->data_cache[$file]))
$data=$this->data_cache[$file];
else
$data=file($file);
//
//If data_caching is enabled and the file wasn't already loaded
// before then we add to the data_cache
//
if($this->data_caching&&!isset($this->data_cache[$file]))
$this->data_cache[$file]=$data;
$data=unserialize($data[1]);
$time=$data['time'];
settype($time,'integer');
return $time;
}
/**
* Generates and sends the feeds, it supports caching system
*
* @access public
*/
function auto_process()
{
if($this->config['cache'])
{
$time=$this->get_cache_creation_time();
if($time!==false&&((time()-$time)<=$this->config['cache_expire']||$this->config['cache_expire']===0))
{
$this->read_from_cache();
$this->send();
}
}
{
$this->process();
$this->send();
}
}
/**
* Generate the feeds
*
* @return bool False only if caching failed
* @access public
* @see $this->put(), $this->send()
*/
function process($if=false)
{
if(isset($this->get_data))
$this->db_var=$this->get_data();
else
{
$this->db_var=file($this->database);
$this->db_var[0]='';
$this->db_var[count($this->db_var)-1]='';
$this->db_var=$this->array_remove_empties($this->db_var);
}
//
//Here we remove the (possible) extra entries that we got
// according to the limit given by the config
//
if(($n=(count($this->db_var)-$this->config['max']))>0)
{
$n--;
while($n>=0)
$this->db_var[($n--)+$this->config['max']]='';
}
$this->db_var=$this->array_remove_empties($this->db_var);
if(!isset($this->get_data)||!isset($this->get_data_processed))
{
$n=0;
foreach ($this->db_var as $v)
{
$title='';
$author='';
$link='';
$mail='';
$description='';
$category='';
$published='';
$id='';
parse_str($v);
$data[$n]['title']=htmlentities($title);
$data[$n]['author']=htmlentities($author);
$data[$n]['mail']=htmlentities($mail);
$data[$n]['link']=htmlentities($link);
$data[$n]['description']=htmlentities($description);
$data[$n]['category']=htmlentities($category);
$data[$n]['published']=htmlentities(date('D, d M Y H:i:s T',$published));
$data[$n]['published_utc']=htmlentities(gmdate('D, d M Y H:i:s T',$published));
$data[$n]['id']=htmlentities($id);
$n++;
}
}
else
$data=$this->db_var;
$tpl=new x64Template(FALSE);
$tpl->set('title',htmlentities($this->setup['title']));
$tpl->set('creator',htmlentities($this->setup['author']));
$tpl->set('link',htmlentities($this->setup['link']));
$tpl->set('description',htmlentities($this->setup['description']));
$tpl->set('language',htmlentities($this->setup['language']));
$tpl->set('charset',htmlentities($this->setup['charset']));
$tpl->set('generator',htmlentities('Atomo64 Feeds Generator 1.1.1'));
$tpl->set('generator_link',htmlentities('http://www.atomo64.tk/'));
$tpl->set('date',htmlentities(date('D, d M Y H:i:s T')));
$tpl->set('date_utc',htmlentities(gmdate('D, d M Y H:i:s T')));
$tpl->set('items',$data,$if);
$tpl->set('id',htmlentities($this->database));
$tpl->set('last_build',$data[0]['published']);
$tpl->set('last_build_utc',$data[0]['published_utc']);
$link=$this->setup['link'].$this->config['path'];
if($this->config['query_format']=='query')
$link.='format='.urlencode($this->format);
else
$link.=urlencode($this->format).'.xml';
$tpl->set('feed_link',htmlentities($link));
$this->feeds=$tpl->fetch($this->path.'templates/'.$this->format.'.tpl');
if($this->config['cache'])
{
if(($fp=fopen($this->path."cache/".$this->database."_".$this->format.".php","w"))==false)
return false;
$cached="<?php /*\n".
serialize(array('time'=>time(),'data'=>base64_encode($this->feeds))).
"\n */ ?>";
fwrite($fp,$cached,strlen($cached));
fclose($fp);
}
return true;
}
/**
* Change the values that are going to be used to generate the feeds
*
* @param string $title The feeds title
* @param string $author The author of the feeds
* @param string $link The link to your website
* @param string $description A description about what your feeds are about
* @param string $language Language code of the feeds, e.g. english is 'en', spanish is 'es'
* @param string $charset The charset used for the feeds
* @access public
*/
function set_data($title='My Website Feeds',$author='Me',$link='http://mysite.com',$description='Keep updated about what I do',$language='en',$charset='iso-8859-1')
{
$this->setup['title']=$title;
$this->setup['author']=$author;
$this->setup['link']=$link;
$this->setup['language']=$language;
$this->setup['description']=$description;
$this->setup['charset']=$charset;
}
/**
* Change the settings for the feeds generator(usage)
*
* @param int $max_entries The number of max items that are going to be displayed
* @param string $path The path used in the query string
* @param string $query_format The kind of query, e.g. feeds.php?rss200.xml or feeds.php?format=rss200
* @param bool $cache Enables or disables feeds caching
* @param int $cache_expire The cache expiration time(0 is never)
* @param bool $data_caching Enables/disables data caching, used by the feeds generator, is recommended to keep it true
* @param string $mime The content-type header value to be sent to the client
* @access public
*/
function setup_generator($max_entries=15,$path='feeds.php?',$query_format='path',$cache=false,$cache_expire=3600,$data_caching=true,$mime='application/xml')
{
$this->config['max']=$max_entries;
$this->config['path']=$path;
$this->config['query_format']=$query_format;
$this->config['cache']=$cache;
$this->config['cache_expire']=$cache_expire;
$this->config['mime']=$mime;
$this->data_caching=$data_caching;
}
/**
* Class initializer
*
* @param string $database The file name of the database(or the db name using the get_data function)
* @param string $path The path to where the feeds generator is located
* @access public
*/
function x64_feeds($database='',$path='')
{
$this->path=$path;
$this->database=$database;
$this->db_var=array();
}
/**
* Initiates the feeds by setting some vars
*
* @param string $format
* @param bool $no_autosetup By default this function calls the $this->set_data() and$this->setup_generator() functions in order to set some required vars, if your script calls both functions then you can set this value to true
* @return bool false if the template file doesn't exists
* @access public
*/
function init($format='rss200',$no_autosetup=false)
{
if(!file_exists($this->path.'templates/'.$format.'.tpl'))
return false;
$this->format=$format;
if(!$no_autosetup)
{
$this->set_data();
$this->setup_generator();
}
return true;
}
/**
* Removes empty entries from an array
*
* @param array $array
* @return array
* @access private
*/
function array_remove_empties($array)
{
$new_array=array();
$temp='';
foreach ($array as $key=>$value)
{
if(is_array($value))
$new_array[$key]=$this->array_remove_empties($value);
else
{
$temp=trim($value);
if(!empty($temp)||$value===0)
$new_array[$key]=$value;
}
}
return $new_array;
}
}
?>
|