Merge pull request #24 from tmos/master

RSS implementation
This commit is contained in:
Tom.C. 2014-05-19 10:58:00 +02:00
commit 2e1021cee0
8 changed files with 178 additions and 13 deletions

5
.gitignore vendored
View file

@ -1,2 +1,7 @@
photos/
thumbs/
db_feed_source
db_old_files
db_rss_timestamp
rss.locker
new_files_list

13
config.php Normal file → Executable file
View file

@ -27,7 +27,7 @@ $sorting_folders = "name"; // Sort folders by: [name][date]
$sorting_files = "name"; // Sort files by: [name][date][size]
$sortdir_folders = "ASC"; // Sort direction of folders: [ASC][DESC]
$sortdir_files = "ASC"; // Sort direction of files: [ASC][DESC]
$lazyload = 1; // 0 = pagination, 1 = display all pictures on one page.
$lazyload = 1; // 0 = pagination, 1 = display all pictures on one page
//LANGUAGE STRINGS
$label_home = "Home"; //Name of home link in breadcrumb navigation
@ -37,8 +37,15 @@ $label_all = "All"; //Text used for link to display all images in one page
$label_noimages = "No images"; //Empty folder text
$label_loading = "Loading..."; //Thumbnail loading text
//RSS SETTINGS
$gallery_link = substr("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 0, strlen("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])-7); // the -7 is to remove "rss.php"
$description = "MiniGal Nano";
$nb_items_rss = 25; //number ef elements to display in the feed. If you add a lot of pictures at the time, consider increasing this number
$rss_refresh_interval = 60;//time, in seconds, between two RSS refresh. for example, 3600 = 1update max per hour, 86400 = 1/day.
$SkipExts = array('html', 'txt', 'php', "gitignore"); //Files with one of this extension will not be displayed on the RSS feed
$SkipObjects = array('UnDossier', 'UnFichier'); //Those files and folders will not be displayed on the RSS feed
//ADVANCED SETTINGS
$thumb_size = 320; //Thumbnail height/width (square thumbs). Changing this will most likely require manual altering of the template file to make it look properly!
$label_max_length = 40; //Maximum chars of a folder name that will be displayed on the folder thumbnail
$display_exif = 0; //Take care, even if not diplayed EXIF are still readable for visitors. May be a good idea to erase EXIF datas...
?>
$display_exif = 0; //Take care, even if not diplayed EXIF are still readable for visitors. May be a good idea to erase EXIF data...
?>

153
rss.php Normal file
View file

@ -0,0 +1,153 @@
<?php
/*===================*/
/*Functions*/
/*===================*/
# Hardly inspired from here : codes-sources.commentcamarche.net/source/35937-creation-d-une-arborescenceI
# 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;
}
# Paul's Simple Diff Algorithm v 0.1 : http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/
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.php");
#$content = "";
$old_files_list = "db_old_files"; //list of files in ./photos
$db_feed_source = "db_feed_source";
$db_rss_timestamp = "db_rss_timestamp";
$Folder = 'photos';
$content = ListFiles($gallery_link, $content, $Folder, $SkipExts, $SkipObjects);
$to_store = "";
// Init files
if (!file_exists($old_files_list))
{
file_put_contents($old_files_list, "");
}
if (!file_exists($db_feed_source))
{
file_put_contents($db_feed_source, "");
}
if (!file_exists($db_rss_timestamp))
{
file_put_contents($db_rss_timestamp, "");
}
/*===================*/
/*Computing*/
/*===================*/
#Todo : ajouter une condition : dois-je regénérer le flux ou utiliser les anciens fichiers ?
$temp = file_get_contents($db_feed_source);
$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)
{
file_put_contents("rss.locker", "");
file_put_contents($db_rss_timestamp, time());
// 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";
}
}
}
}
//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");
}
/*===================*/
/*XML Gen*/
/*===================*/
$temp = explode("\n", $temp);
$pieceOfTitle;
$titleLenght;
echo "
<rss version=\"2.0\">
<channel>
<title>".$title."</title>
<link>".$gallery_link."</link>
<description>".$description."</description>
";
for ($i=0; $i < $nb_items_rss; $i++) {
$pieceOfTitle = strrchr ($temp[$i] , "/");
$titleLenght = strlen($pieceOfTitle) - strlen(strrchr($pieceOfTitle, "."));
echo
"<item>
<title>" . substr($pieceOfTitle, 1, $titleLenght-1) . "</title>
<link>". $temp[$i] . "</link>
<description>
<![CDATA[ <img src=\"" . $temp[$i] . "\"> ]]>
</description>
</item>"
;
if ($temp[$i+1] == NULL)
break;
}
echo"
</channel>
</rss>
";
?>

View file

@ -6,7 +6,7 @@
<meta name="generator" content="MinigalNano <% version %>" />
<title><% title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss/" />
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss.php" />
<link rel="stylesheet" href="<% gallery_root %>css/mediaboxWhite.css" type="text/css" media="screen" />
<style type="text/css">
@ -193,7 +193,7 @@
<a href="http://tomcanac.com/minigal/" title="Tom Canac" target="_blank">
Board theme by Tom Canac
</a> /
<a title="<% title %> RSS" href="rss/">
<a title="<% title %> RSS" href="rss.php">
RSS
</a>
</footer>

View file

@ -6,7 +6,7 @@
<meta name="generator" content="MinigalNano <% version %>" />
<title><% title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss/" />
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss.php" />
<link rel="stylesheet" href="<% gallery_root %>css/mediaboxWhite.css" type="text/css" media="screen" />
<style type="text/css">
@ -206,7 +206,7 @@
<a href="https://github.com/sebsauvage/MinigalNano" title="Powered by MiniGal Nano" target="_blank">
Powered by MiniGal Nano <% version %>
</a> |
<a title="<% title %> RSS" href="rss/">
<a title="<% title %> RSS" href="rss.php">
RSS
</a>
</footer>

View file

@ -6,7 +6,7 @@
<meta name="generator" content="MinigalNano <% version %>" />
<title><% title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss/" />
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss.php" />
<link rel="stylesheet" href="<% gallery_root %>css/mediaboxWhite.css" type="text/css" media="screen" />
<style type="text/css">
@ -192,7 +192,7 @@
<a href="http://tomcanac.com/minigal/" title="Tom Canac" target="_blank">
Board theme by Tom Canac
</a> /
<a title="<% title %> RSS" href="rss/">
<a title="<% title %> RSS" href="rss.php">
RSS
</a>
</footer>

View file

@ -6,7 +6,7 @@
<meta name="generator" content="MinigalNano <% version %>" />
<title><% title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss/" />
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss.php" />
<link rel="stylesheet" href="<% gallery_root %>css/mediaboxWhite.css" type="text/css" media="screen" />
<style type="text/css">
@ -211,7 +211,7 @@
<a href="http://tomcanac.com/minigal/" title="Tom Canac" target="_blank">
Rounded theme by Tom Canac
</a> /
<a title="<% title %> RSS" href="rss/">
<a title="<% title %> RSS" href="rss.php">
RSS
</a>
</footer>

View file

@ -6,7 +6,7 @@
<meta name="generator" content="MinigalNano <% version %>" />
<title><% title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss/" />
<link rel="alternate" type="application/rss+xml" title="<% title %>" href="rss.php" />
<link rel="stylesheet" href="<% gallery_root %>css/mediaboxWhite.css" type="text/css" media="screen" />
<style type="text/css">
@ -189,7 +189,7 @@
<a href="http://tomcanac.com/minigal/" title="Tom Canac" target="_blank">
Squares theme by Tom Canac
</a> /
<a title="<% title %> RSS" href="rss/">
<a title="<% title %> RSS" href="rss.php">
RSS
</a>
</footer>