Minigalnano/rss.php

164 lines
5.1 KiB
PHP
Raw Normal View History

2014-02-25 10:24:36 +01:00
<?php
/*==========================*/
/*Gallery address definition*/
/*==========================*/
$gallery_protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$gallery_domain = $_SERVER['HTTP_HOST'].'/';
$gallery_path = dirname($_SERVER['REQUEST_URI']);
$gallery_link = $gallery_protocol.$gallery_domain.$gallery_path;
2014-02-25 10:24:36 +01:00
/*===================*/
/*Functions*/
/*===================*/
2014-04-30 23:08:23 +02:00
# Hardly inspired from here : codes-sources.commentcamarche.net/source/35937-creation-d-une-arborescenceI
2014-02-25 10:24:36 +01:00
# Listing all files of a folder and sub folders.
function ListFiles($gallery_link, &$content, $Folder, $SkipFileExts, $SkipObjects)
{
$dir = opendir($Folder);
while (false !== ($Current = readdir($dir))) // Loop on all contained on the folder
{
if ($Current !='.' && $Current != '..' && in_array($Current, $SkipObjects)===false)
{
if(is_dir($Folder.'/'.$Current)) // If the current element is a folder
{
ListFiles($gallery_link, $content, $Folder.'/'.$Current, $SkipFileExts, $SkipObjects); // Recursivity
}
else
{
$FileExt = strtolower(substr(strrchr($Current ,'.'),1));
if (in_array($FileExt, $SkipFileExts)===false) // Should we display this extension ?
$current_adress = $gallery_link . "/" . $Folder.'/'. $Current;
$content .= $current_adress. "\n";
}
}
}
closedir($dir);
return $content;
}
2014-04-30 23:08:23 +02:00
# Paul's Simple Diff Algorithm v 0.1 : http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/
2014-02-25 10:24:36 +01:00
function diff($old, $new){
$matrix = array();
$maxlen = 0;
foreach($old as $oindex => $ovalue){
$nkeys = array_keys($new, $ovalue);
foreach($nkeys as $nindex){
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if($matrix[$oindex][$nindex] > $maxlen){
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
return array_merge(
diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
function print_array($array_to_display) {
echo '<pre>';
print_r($array_to_display);
echo '</pre>';
}
/*===================*/
/*Variables*/
/*===================*/
require("config-default.php");
include("config.php");
2014-02-25 10:24:36 +01:00
#$content = "";
$old_files_list = "db_old_files"; //list of files in ./photos
$db_feed_source = "db_feed_source";
2014-05-04 17:28:32 +02:00
$db_rss_timestamp = "db_rss_timestamp";
2014-02-25 10:24:36 +01:00
$Folder = 'photos';
$content = ListFiles($gallery_link, $content, $Folder, $SkipExts, $SkipObjects);
$to_store = "";
// Init files
if (!file_exists($old_files_list))
2014-05-04 16:02:31 +02:00
{
2014-02-25 10:24:36 +01:00
file_put_contents($old_files_list, "");
2014-05-04 16:02:31 +02:00
}
2014-02-25 10:24:36 +01:00
if (!file_exists($db_feed_source))
2014-05-04 16:02:31 +02:00
{
2014-02-25 10:24:36 +01:00
file_put_contents($db_feed_source, "");
2014-05-04 16:02:31 +02:00
}
2014-05-04 17:28:32 +02:00
if (!file_exists($db_rss_timestamp))
{
file_put_contents($db_rss_timestamp, "");
}
2014-02-25 10:24:36 +01:00
/*===================*/
/*Computing*/
/*===================*/
2014-04-30 23:08:23 +02:00
#Todo : ajouter une condition : dois-je regénérer le flux ou utiliser les anciens fichiers ?
2014-05-04 16:02:31 +02:00
$temp = file_get_contents($db_feed_source);
2014-05-04 17:28:32 +02:00
$last_rss_gen = file_get_contents($db_rss_timestamp);
$current_time = time();
//If the RSS generation is already launched, don't do a second generation at the same time
if (($current_time - $last_rss_gen) > $rss_refresh_interval && file_exists("rss.locker") == false)
2014-05-04 16:02:31 +02:00
{
file_put_contents("rss.locker", "");
2014-05-04 17:28:32 +02:00
file_put_contents($db_rss_timestamp, time());
2014-05-04 16:02:31 +02:00
// Load the list from files.
$old_files_list_content = explode("\n", file_get_contents($old_files_list));
$new_files_list_content = explode("\n", $content); #debug
// Generate and stock new elements
$differences = diff($old_files_list_content, $new_files_list_content);
for ($i=0; $i < count($differences); $i++)
{
if (is_array($differences[$i]))
{
for ($j=0; $j < count($differences[$i]["i"]); $j++)
{
if (strlen($differences[$i]["i"][$j]) > 2)
{
$to_store .= $differences[$i]["i"][$j] . "\n";
}
}
}
2014-02-25 10:24:36 +01:00
}
2014-05-04 16:02:31 +02:00
//Add new elements at the top of the feed's source
$temp = $to_store . $temp;
file_put_contents($db_feed_source, $temp);
// Store the current file list for the next generation
file_put_contents($old_files_list, $content);
unlink("rss.locker");
2014-02-25 10:24:36 +01:00
}
/*===================*/
/*XML Gen*/
/*===================*/
$temp = explode("\n", $temp);
$pieceOfTitle;
$titleLenght;
2014-02-25 10:24:36 +01:00
echo "
<rss version=\"2.0\">
<channel>
<title>".$title."</title>
<link>".$gallery_link."</link>
<description>".$description."</description>
";
2014-04-30 23:08:23 +02:00
for ($i=0; $i < $nb_items_rss; $i++) {
$pieceOfTitle = strrchr ($temp[$i] , "/");
$titleLenght = strlen($pieceOfTitle) - strlen(strrchr($pieceOfTitle, "."));
2014-02-25 10:24:36 +01:00
echo
"<item>
<title>" . substr($pieceOfTitle, 1, $titleLenght-1) . "</title>
2014-02-25 10:24:36 +01:00
<link>". $temp[$i] . "</link>
<description>
<![CDATA[ <img src=\"" . $temp[$i] . "\"> ]]>
</description>
</item>"
;
2014-04-30 23:08:23 +02:00
if ($temp[$i+1] == NULL)
break;
2014-02-25 10:24:36 +01:00
}
echo"
</channel>
</rss>
";
?>