<?php
/*
 * Copyright (C)
 *   2005,2006 Juliane Clausen <julehamsta@users.sourceforge.net>
 *   2005,2006 Dieter Weber        <uellue@users.sourceforge.net>
 *
 *  gallery.php 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.
 *
 *  gallery.php 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 gallery.php; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * ===========================================================================
 *
 * This is part of the PHPGallue documentation.
 * See the file COPYING in the main directory for copying conditions.
 */



// Path to the libgallue libraries
define('LIBGALLUE''/home/uellue/pub/PHPGallue/libgallue/');
// Path to the configuration file
define('CONFIG''/home/uellue/pub/PHPGallue/config.xml');
// groupID of the group that shall be displayed
define('GROUP'70);

// Get rid of magic quotes -- we are working without safety belt...
if (get_magic_quotes_gpc()) {
        function 
stripslashes_deep($value)
        {
                
$value is_array($value) ?
                
array_map('stripslashes_deep'$value) :
                
stripslashes($value);
                return 
$value;
        }
        if (
is_array($_POST))   $_POST   array_map('stripslashes_deep'$_POST);
        if (
is_array($_GET))    $_GET    array_map('stripslashes_deep'$_GET);
        if (
is_array($_COOKIE)) $_COOKIE array_map('stripslashes_deep'$_COOKIE);
        if (
is_array($_FILES))  $_FILES  array_map('stripslashes_deep'$_FILES);
}
// turn off the most useless PHP feature
set_magic_quotes_runtime(0);

// load the "bootstrapping" code of PHPGallue
require_once LIBGALLUE."/gallery.inc.php";
// construct the gallery object, the main handle to access all
// aspects of a PHPGallue instance
$gallery = new GALGallery(CONFIG);

// load the group that shall be displayed.
$group $gallery->groups->row(array('groupID' => GROUP));
// load all images in this group which are visible to the current user (by defult userID 1)
// Append a tiny SQL snippet that is inserted into an "ORDER BY" clause and puts the oder images
// first, honoring the sortHint field
$images $group->loadImages($gallery->images->visible(), array('`sortHint` ASC'));

// store all values of the field 'imageID' as array in $imageIDs
$imageIDs $images->verticalFetch('imageID');

// create object to handle an filter GET, POST and COOKIE data
$globalvars = new GALGlobalVars(0'gallery.php');
// register the variable which determines the displayed image.
// only allow IDs of images which are contained in $imageIDs
// and use the first entry as default
$globalvars->register('id'GALDescriptor::GET$imageIDs$imageIDs[0], $imageIDs[0]);
// now read the registered variable from $_GET
$globalvars->evaluate($_GET$_POST$_COOKIE);

// now load the image which is displayed big.
// $globalvars->id cannot contain nasty stuff because it's filtered,
// and the row() method itself escapes every argument so SQL injection is impossible
// provided the PHP escaping function (mysql_real_escape_string()) works properly
$image $gallery->images->row(array('imageID' => $globalvars->id));

// preload the thumbnail instances into the cache
preload(GAL_IMG_THUMB$images);

// this function outputs a reasonable <img> tag or a substitute if not possible.
// The $instanceID determines the displayed image size
function imgTag($instanceIDGALImage $image) {
    global 
$gallery;
    
// load the desired instance
    
$instance $image->instance($instanceID);
    
// filter the output so that it can be placed in HTML
    
$title htmlspecialchars($image->titleENT_QUOTES'UTF-8');
    
// check if this instance exists and is visible to the current user
    
if ($instance instanceof GALInstance && $instance->canView($gallery->user)) {
        
// the url() method returns an URL pointing to the image data
        
$url htmlspecialchars($instance->url(), ENT_QUOTES'UTF-8');
        
$x $instance->x;
        
$y $instance->y;
?>
                <img
                    src="<?=$url;?>"
                    width="<?=$x;?>"
                    height="<?=$y;?>"
                    alt="<?=$title;?>"
                    title="<?=$title;?>"
                >
<?php
    
} else {
        
$rawInstance $image->instance(0);
        if (
$rawInstance->canView($gallery->user)) {
            
$url htmlspecialchars($rawInstance->url(), ENT_QUOTES'UTF-8');
?>
        <a href="<?=$url;?>"><?=$title;?></a>
<?php
        
} else {
            echo 
$title;
        }
    }
}
// little helper function to load instances into the cache
// so that they needn't be loaded individually
function preload($instanceIDGALImageIterator $images) {
    global 
$gallery;
    
// well, we already have this array as global variable, but
    // this function is of more general use, so we generate it internally
    
$imageIDs $images->verticalFetch('imageID');
    
// special helper classes to create SQL queries, see the API docs
    
$expr = new GALExpressionList($gallery->database$gallery->instances'imageID'$imageIDs);
    
$expr $expr->eand($gallery->instances->compareExpression('instanceID''='$instanceID));
    
// this automatically loads the instances into the cache
    
$instances $gallery->instances->load($expr);
}
// set the correct content type, PHPGallue works with UTF-8 by default
header('Content-Type: text/html; charset=UTF-8');
$title htmlspecialchars($group->titleENT_QUOTES'UTF-8');
$subtitle htmlspecialchars($image->titleENT_QUOTES'UTF-8');
$comment htmlspecialchars($image->commentENT_QUOTES'UTF-8');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="de">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!-- a few lines of CSS to get everything in shape -->
        <style type="text/css">
            img {
                border: none;
            }
            ul.navi li {
                display: inline;
            }
        </style>
        <title><?="$title - $subtitle";?></title>
    </head>
    <body>
        <h1><?=$title;?></h1>
        <!-- Navigation list -->
        <ul class="navi" >
<?php
// iterate over all loaded images
while ($currImage $images->fetch()) {
    
// generate an URL from the globalvars object where the "id"
    // parameter is set to the image's imageID
    
$url htmlspecialchars($globalvars->url(array('id' => $currImage->imageID)), ENT_QUOTES'UTF-8');
?>
            <li>
                <a href="<?=$url;?>" >
<?php
    imgTag
(GAL_IMG_THUMB$currImage)
?>
                </a>
            </li>
<?php
}
?>
        <!-- end navigation list -->
        </ul>
        <h2><?=$subtitle;?></h2>
        <p>
<?php
$rawInstance 
$image->instance(GAL_IMG_RAW);
if (
$rawInstance->canView($gallery->user)) {
    
$rawURL htmlspecialchars($rawInstance->url(), ENT_QUOTES'UTF-8');
?>
            <a href="<?=$rawURL;?>" >
<?php
    imgTag
(GAL_IMG_MEDIUM$image);
?>
            </a>
<?php
} else {
    
imgTag(GAL_IMG_MEDIUM$image);
}
?>
        </p>
<?php
if ($comment) {
?>
        <p>
            <?=$comment;?>

        </p>
<?php
}
?>
    </body>
</html>