<?php

// Penguin Gallery http://www.penguintutor.com/penguingallery 
// Copyright 2009 Stewart Watkiss
/*  
	This file is a part of Penguin Gallery

	Penguin Gallery 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 3 of the License, or
    (at your option) any later version.

    Penguin Gallery 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 Penguin Gallery.  If not, see <http://www.gnu.org/licenses/>.
*/

if (defined('__DIR__')) {
	$galleryphpdir = __DIR__;
    }
    else {
        $galleryphpdir = dirname(__FILE__);
    }
$galleryphpdir .= "/";

// Include the gallery.cfg file - must be in same directory as gallery.php
include ($galleryphpdir."gallery.cfg");

// galleryconfigfile is for the gallery being viewed - whereas gallery.cfg relates to all galleries
// Check that $galleryconfigfile contains only alphanumeric for security reasons
if (!preg_match('/^[\w-\/]+\.cfg$/', $galleryconfigfile)) {echo "<p>Gallery config file name contains invalid characters.</p>"; exit(2);} 

// Add documrnet root to galleryconfigdir if appropriate
if (preg_match ("?^/[^\.]+/?", $galleryconfigdir)) 
	{$galleryconfigdir = $_SERVER['DOCUMENT_ROOT']."$galleryconfigdir";} 
else	{"Invalid galleryconfigdir  path name\n"; exit (2);}

// As the gallery specific config file is opened later this can be used to override values from the general config file
// We have security checked now open the file adding .cfg to th end
include ($galleryconfigdir.$galleryconfigfile);

// Add document root to file directories
$directory = $_SERVER['DOCUMENT_ROOT'].$directory;
// Do not add to $viewfull though as that is put into URL.


// Get URL of this page (excl server name) - needed for non ajax links to next picture etc.
// Note REQUEST_URI will work before mapping
// doing it this way means we can still use it embedded in a CMS or blog etc.
$thisurl = $_SERVER['REQUEST_URI'];
// Strip out image part as we will use that elsewhere - keep rest of url intact
$thisurl = preg_replace ("/[?&]img=\d+/", "", $thisurl);

// setup javascript variables (as need to include php variables) and load javascript file 
// Note only the last has \n - html doesn't care, but wordpress adds <br /> if we include \n
if (!$silentsetup) {echo "<script type=\"text/javascript\">var galleryconfigfile=\"$galleryconfigfile\";</script>";}
	if (!$silentsetup) {echo "<script type=\"text/javascript\">var imgscript=\"".$gallerydir."galleryimg.php\";</script>";}
	if (!$silentsetup) {echo "<script type=\"text/javascript\">var navscript=\"".$gallerydir."gallerynav.php\";</script>";}
	if (!$silentsetup) {echo "<script type=\"text/javascript\">var slideshowseconds=$slideshowseconds;</script>";}
	if (!$silentsetup) {echo "<script type=\"text/javascript\">var slideshowautorepeat=$slideshowautorepeat;</script>";}
	if (!$silentsetup) {echo "<script type=\"text/javascript\" src=\"".$gallerydir."gallery.js\"></script>\n";}


//if (is_array ($filenames)) {unset $filenames;}
$filenames = array();

// Read in filenames
if (is_dir($directory)) 
{
	if ($dh = opendir($directory))
	{
		while (($file = readdir($dh)) !== false) 
		{
			if (preg_match("/$prefixthumb(.*).jpg/i", $file, $matches))
			{
				$filenames[] = $matches[1];
			}
		}
        closedir($dh);
	}
}

sort ($filenames);

// Add variable with number of images - required for slideshow
if (!$silentsetup) {echo "<script type=\"text/javascript\">var numimages=".sizeof($filenames).";</script>\n";}

?>
