<?php
/*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2004 Bharat Mediratta
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
* $Id: rss.php,v 1.12.4.1 2004/07/13 07:45:03 cryptographite Exp $
*/
?>
<?php
require(dirname(__FILE__) . '/init.php');

// Set defaults, if RSS has not been setup via config wizard
if (!isset($gallery->app->rssEnabled)) {
    $gallery->app->rssEnabled = "yes";
    $gallery->app->rssMode = "basic";
    $gallery->app->rssHighlight = "";
    $gallery->app->rssVisibleOnly = "yes";
    $gallery->app->rssDCDate = "no";
    $gallery->app->rssBigPhoto = "yes";
    $gallery->app->rssPhotoTag = "yes";
}

$MAXITEMS = 20;

if ($gallery->app->rssEnabled == "no") {
    header("Location: " . makeAlbumHeaderUrl());
}

$gallery->session->offlineAlbums["albums.php"] = true;

function photoSort($a, $b) {
    $aTime = $a["uploadTS"];
    $bTime = $b["uploadTS"];

    if ($aTime < $bTime) {
        return 1;
    } else {
        return -1;
    }
}

function removeUnprintable($string) {
    return preg_replace("/[^[:print:]]/", "", $string);
}


function buildItem($album, $photo)
{
  $userDB = new Gallery_UserDB();
  $user = $userDB->getUserByUid($photo->owner);
  $size = filesize($album->getAlbumDir().'/'.$photo->image->name.'.'.$photo->image->type);
  $item = array(
    "title" => htmlspecialchars(removeUnprintable($photo->getCaption())),
    "link"  => $album->getAlbumDirURL("full").'/'.$photo->image->name.'.'.$photo->image->type,
    "description" => htmlentities($photo->getThumbnailTag($album->getAlbumDirUrl("thumb"))),
    "pubDate" => date("r", $photo->uploadDate),
    "author" => $user->getEmail()." (".$user->getUsername().")",
    "uploadTS" => $photo->uploadDate,
    "enclosure" => array (
      "url" => $album->getAlbumDirURL("full").'/'.$photo->image->name.'.'.$photo->image->type,
      "length" => $size,
      "type" => "image/$photo->image->type"                     
    )
  );
  return $item;
}

function buildAlbum($album)
{
  global $albumDB;
  global $gallery;

  if(!$gallery->user->canReadAlbum($album)) {
    return;
  }

  $photos = array();
  if(!$album->photos) { print $gallery->app->albumDir."/".$album->fields['name']."<br>"; }
  foreach($album->photos as $photo)
  {
    if(!$photo->isHidden()) {
      if($photo->isAlbum())
      {
        $subAlbum = $albumDB->getAlbumByName($photo->isAlbumName);
        $photos = array_merge($photos, buildAlbum($subAlbum));
      }
      else {
        $photos[] = buildItem($album, $photo);
      }
    }
  }
  return $photos;
}

/* Read the album list */
$albumDB = new AlbumDB(FALSE);
$gallery->session->albumName = "";

if(!$_GET['set_albumName']) {
    $photos = array();
    $rootAlbmus = array();
    $title = $gallery->app->galleryTitle;
    foreach($albumDB->albumList as $album) {
        if($album->isRoot()) {
            $rootAlbums[] = $album->fields['name'];
        }
    }
    foreach($rootAlbums as $name) {
        $album = $albumDB->getAlbumByName($name);
        $photos = array_merge($photos, buildAlbum($album));
    }
    list($numPhotos, $numAlbums,) = $albumDB->numAccessibleItems($gallery->user);
    $total_str = pluralize_n2(ngettext(_("1 album"), _("%s albums"), $numAlbums), $numAlbums, _("no albums"));
    $image_str = pluralize_n2(ngettext(_("1 photo"), _("%s photos"), $numPhotos), $numPhotos, _("no photos"));
    $description = sprintf(_("%s in %s"), $image_str, $total_str);
    $url = $gallery->app->photoAlbumURL;
}
else {
    $album = $albumDB->getAlbumByName($_GET['set_albumName']);
    $title = $album->fields['title'];
    $description = htmlentities($album->fields['summary']);
    $photos = buildAlbum($album);
    $url = $gallery->app->photoAlbumURL . '/' . $_GET['set_albumName'];
}
uasort($photos, "photoSort");

header("Content-Type: application/rss+xml");


echo '<' . '?xml version="1.0" ?' . '>';

?>
<Channel                                                                        
    xmlns="http://purl.org/net/rss1.1#"                                         
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"                     
    xmlns:dc="http://purl.org/dc/elements/1.1/"                                 
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"                     
    xmlns:admin="http://webns.net/mvcb/"                                        
    xmlns:content="http://purl.org/rss/1.0/modules/content/"                    
    rdf:about="<?php echo $url ?>">
        <title><?php echo htmlspecialchars($title) ?></title>
        <link><?php echo $url ?></link>
        <description><?php echo htmlspecialchars($description) ?></description>
        <dc:date><?php echo date("r"); ?></dc:date>
        <admin:generatorAgent>Gallery <?php echo $gallery->version; ?></admin:generatorAgent>
<?php
  foreach ($photos as $photo) {
    if($cnt++ > $MAXITEMS) { break; }
?>
    <item rdf:about="<?= $photo['enclosure']['url'] ?>">
      <title><?= $photo['title'] ?> </title>
      <link><?= $photo['link'] ?> </link>
      <description><?= $photo['description'] ?> </description>
      <dc:date><?= $photo['pubDate'] ?> </dc:date>
      <dc:creator><?= $photo['author'] ?> </dc:creator>
    </item>
<?php
  }
?>
</Channel>
