<?php
/**
* Call to Import & Export functions
* Generation of the rss feed
*
* @author Olivier G <hide@address.com>
* @version 1.0
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @link http://gemibloo.fr
*/
require_once(dirname(__FILE__).'/param/parameters.php');
require_once(dirname(__FILE__)."/application/core/lib/class.MySQL.php");
require_once(dirname(__FILE__)."/application/core/class/class.PluginLoader.php");
$out = "<?xml version='1.0' encoding='utf-8'?><rss version='2.0' xmlns:geo='http://www.w3.org/2003/01/geo/wgs84_pos#'><channel><title>Gemibloo RSS feed</title><link>".GEOBLOG_URL."</link><description></description><language>en-us</language>";
//Check if stored geoRSS file exists and is not too old
if( (FALSE === isset($_GET[entry_id])) && (TRUE === fIsFileValid(RSS_FILENAME)) )
//Return stored file
fReadFile(RSS_FILENAME);
else{
//Import new messages
try{
$import = new PluginLoader(IMPORT_PLUGINS_DIR);
$import->fExecutePlugin('fImport');
if(TRUE === $import->fHasNewData){
//Update distant services if new messages, exports
$export = new PluginLoader(EXPORT_PLUGINS_DIR);
$export->fExecutePlugin('fExport');
}
}catch (Exception $e) {
mail(Import_Mailbox::fGetInstance()->fGetReplyTo(), 'Automatic message: Geoblog import/export executed wrongly', $e->getCode().': '.$e->getMessage());
}
$db = new MySQL();
if (FALSE === $db->Open(DB_DATABASE, DB_SERVER, DB_USR, DB_PWD)){
$out .= '<error>Cannot connect to DB</error></channel></rss>';
echo $out;
exit;
}
//Try to update counter
if(isset($_GET[entry_id]))
$db->Query('UPDATE Gemibloo_main_'.GEMIBLOO_VERSION.' SET seen=seen+1 WHERE id='.$_GET[entry_id].' LIMIT 1;');
else{
//Note: MySQL Subqueries failed when update and select are about the same table
//Extracts most recent entry id
$db->Query('SELECT id FROM Gemibloo_main_'.GEMIBLOO_VERSION.' WHERE 1 ORDER BY timestamp DESC LIMIT 1;');
$db->MoveFirst();
if(!$db->EndOfSeek()){
//Increment counter
$row = $db->Row();
$db->Query('UPDATE Gemibloo_main_'.GEMIBLOO_VERSION.' SET seen=seen+1 WHERE id='.$row->id.' LIMIT 1;');
}
}
//Build geoRSS feed from DB contents
if(FALSE === $db->Query('SELECT * FROM Gemibloo_main_'.GEMIBLOO_VERSION.' WHERE 1 ORDER BY timestamp DESC LIMIT '.MAX_ENTRIES.';')){
$out .= '<error>Cannot get data from DB</error></channel></rss>';
echo $out;
exit;
}
$id_found = FALSE;
$entries = "";
$db->MoveFirst();
//Loop through entries
while (! $db->EndOfSeek()){
$row = $db->Row();
//Check if requested id is in this stream
if(isset($_GET[entry_id]) && ($row->id === intval($_GET[entry_id]))){
$id_found = TRUE;
}
//Append an item section
$entries .= fGetRow($row);
}//while
if(isset($_GET[entry_id]) && (FALSE === $id_found)){
//If id was not found
if(FALSE === $db->Query('SELECT * FROM Gemibloo_main_'.GEMIBLOO_VERSION.' WHERE id='.$_GET[entry_id].' LIMIT 1;')){
$out .= '<error>Cannot get data from DB</error></channel></rss>';
echo $out;
exit;
}
$db->MoveFirst();
if(! $db->EndOfSeek()){
$row = $db->Row();
//Return the requested entry only
$entries = fGetRow($row);
}else
$entries = '';
}//if
$out .= $entries.'</channel></rss>';
//Save current content to be used as cache file
if(FALSE === isset($_GET[entry_id]))
fSaveFile(RSS_FILENAME, $out);
header('Content-Type: text/xml');
//return generated file
echo $out;
}//else
/**
* fFormatDeltaTime function
*
* Format a time in a friendly string
*
* @param float $aDelta Time as seconds
* @return string Time as Hours, Minutes or Seconds
*/
function fFormatDeltaTime($aDelta){
$sec = $aDelta;
if($sec>60){
$min = $sec/60;//mn
if($min>60)
{
$h = $min/60;//h
if($h>24){
//Does not need greater precision than 1 day
return 'about '.floor($h/24).'d';
}else
//Does not need greater precision than 1 hour
return 'about '.floor($h).'h';
}else
//Does not need greater precision than 1 minute
return floor($min).'mn';
}else
return floor($sec).'s';
}//fFormatDeltaTime
/**
* fIsFileValid function
*
* @param string $aFilepath file to be checked
* @return bool TRUE if file still valid, FALSE otherwise
*/
function fIsFileValid($aFilepath){
if(!file_exists ($aFilepath))
return FALSE;
if(time() > (filemtime($aFilepath)+VALIDITY_DELAY))
return FALSE;
return TRUE;
}//fIsFileValid
/**
* fReadFile function
*
* @param string $aFilepath file to be read from
*/
function fReadFile($aFilepath){
$out= '';
if (!$handle = fopen($aFilepath, "r"))
exit;
while (!feof($handle))
$out .= fread($handle, 8192);
fclose($handle);
header('Content-Type: text/xml');
echo $out;
}//fReadFile
/**
* fSaveFile function
*
* @param string $aFilepath file to be created or overwritten
* @return string $aContent content to be saved
*/
function fSaveFile($aFilepath, $aContent){
if (!$handle = fopen($aFilepath, "w"))
exit;
fwrite($handle, $aContent);
fclose($handle);
}//fSaveFile
/**
* fSaveFile function
*
* @param array $aRow row of a recordset
* @return string formated row
*/
function fGetRow($aRow){
return '<item><guid>'.$aRow->id.'</guid><title><![CDATA['.html_entity_decode($aRow->content).']]></title><geo:location><![CDATA['.$aRow->location.']]></geo:location><geo:lat>'.$aRow->lat.'</geo:lat><geo:long>'.$aRow->lng.'</geo:long><pubDate><![CDATA['. fFormatDeltaTime(time() - intval($aRow->timestamp))." ago]]></pubDate><description><![CDATA[<a href='".DATA_URL.$aRow->image."' title='Click to view original size' onclick='window.open(this.href); return false;'><img src='".DATA_URL.$aRow->image."' width='200px' height='".floor(floatval($aRow->ratioHW)*200)."px' alt=''/></a>]]></description></item>";
}//fGetRow
?>