Merge pull request #77 from bifek/rssclean

clean up spacing and braces according to PSR-2 rules
This commit is contained in:
Tom.C. 2014-11-26 19:51:38 +01:00
commit 4a36279803

287
rss.php
View file

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