<?php
/*
Plugin Name: Photo-List
Description: Displays all images in a directory in list fashion
Author: Sebastien Benoit
Author URI: http://www.gwcphoto.ca/contact/
Version: 3.1.0
*/

/*  Long Description: This script can be used as a drop in replacement for Apache's Fancy Indexing, 
        or simply to display images/file list in a directory where the script exists.
        It does a good job of displaying EXIF/IPTC properties of a photo, while also pulling standard
        EXIF GPS coordinates to create a Google maps link to the capture location.
    Notes: You'll need a few icons to compliment this code, namely /icons/folderbw-16.png, /icons/filebw-12.png, 
        /icons/geotag-16.png and /icons/external-12.png (download them from this server if you like). 
        If you are using Apache, you can add them to the Apache2[.2]/icons/ folder so all sites 
        of your server have access to them.
*/

/*  Copyright 2006-2013 Sebastien Benoit (http://www.gwcphoto.ca/contact/)

    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
*/

if (isset($_GET['s'])) die(highlight_file(__FILE__1)); // Show source code with the ?s option

// Mark start time for generation time calculation.
$startTime microtime(true);

require_once(
dirname(__FILE__) . '/photo-list-config.php');
require_once(
dirname(__FILE__) . '/photo-list-functions.php');

/**
 * Main section to initialize all required path and size variables 
 *
 * Set the dimensions of the page based on config.php settings.
 */

// Width of left column
$leftWidth $imageWidthLimit+5;
// Width of right column
$infoWidth 200;
$rightWidth $infoWidth+5;
// Set total page width
$pageWidth $leftWidth+$rightWidth+5;

// Properly set all path related variables
$scriptName basename(getenv('SCRIPT_NAME'));
$documentRoot getenv('DOCUMENT_ROOT');

// Check to see if script is at the root of the server
$uri preg_replace('/\\?.*/'''getenv('REQUEST_URI'));
if ((
$uri == '/' $scriptName) || ($uri == '/')) {
    
$filePath urldecode('/' ltrim(rtrim(getenv('DOCUMENT_ROOT'), '/'), '/') . '/');
    
$titlePath $uriPath '/';
    
$titleLinks 'http://' rtrim(getenv('SERVER_NAME'), '/') . '/';
    
$printHost getenv('SERVER_NAME');
} else {
    
$printHost '';
    
$scriptNameLength strlen($scriptName);
    
// Check to see if the Requesst_URI includes the script name; if so, remove it.
    
if (substr($uri, -$scriptNameLength$scriptNameLength) == $scriptName) {
        
$titlePath urldecode($uriPath substr($uri0, -$scriptNameLength));
    } else {
        
$titlePath urldecode($uriPath $uri);
    }
    
// Check if Document_Root has a trailing slash; if so, remove it to append uriPath to it.
    
$uriPath '/' ltrim(rtrim($uriPath'/'), '/') . '/';
    
$filePath urldecode('/' ltrim(rtrim(getenv('DOCUMENT_ROOT'), '/'), '/') . $uriPath);
    
// Check if on main Photos Page to avoid doing a sizecheck on multiple directories.
    
if ((substr($titlePath, -88) == '/photos/')) {
        
$onPhotosPage true;
    }
    
// Combine all parent links into one cascading string.
    
$titleLinks createParentLinks($titlePath);
}
// Check to see if this is on a windows system (C:/ for example) and if so, remove leading slash.
if (substr($filePath21) == ':') {
    
$filePath urldecode(ltrim($filePath'/'));
}

/**Start HTML document**/
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "https://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
        <title><?php echo $printHost;?><?php echo $titlePath;?> - <?php echo $title;?></title>
        <link rel="shortcut icon" href="http://www.gwcphoto.ca/wp-content/uploads/2010/11/favicon.ico">
        <link rel="stylesheet" href="/photos/photo-list.css" type="text/css" media="screen, projection">
        <style type="text/css">
            #container {width: <?php echo $pageWidth?>px; }
            .container-list-file .list-head {padding-left: <?php echo round(($pageWidth-535)/2); ?>px; }
            .image {width: <?php echo $leftWidth?>px; }
            .image-info {width: <?php echo $rightWidth?>px; }
            .image-info-block {width: <?php echo $infoWidth?>px; }
        </style>
        <script type="text/javascript">
            function externalLinks() {
                if (!document.getElementsByTagName) {
                    return;
                }
                var anchors = document.getElementsByTagName('a');
                for (var i=0; i<anchors.length; i++) {
                    var anchor = anchors[i];
                    if (anchor.getAttribute('href') 
                        && anchor.getAttribute('class') == 'external'
                    ) {
                        anchor.target = '_blank';
                    }
                }
            }
            window.onload = externalLinks;
        </script>
    </head>
    <body>
        <div id="container">
            <div id="header">
                <div class="title">
                    <span class="brand">
                        <?php echo $brand;?>

                    </span>
                    <?php echo $titleLinks;?>

                </div>
            </div>
            <div id="content"><?php

/*Image List*/
if ($handle = @opendir($filePath)) {
    
    
// Create image list array of all images in current directory
    
$imageListArray createImageArray($handle$filePath);

    
// Echo image array
    
if (isset($imageListArray)) {
        
// Sort array by key (DateTimeOriginal|filedate), low to high
        
ksort($imageListArray);
        
reset($imageListArray);
        
$imageNumber 0;
        
// Set plural S for image count
        
$plural 's';
        
$imageCount count($imageListArray);
        if (
$imageCount == 1) {
            
$plural '';
        }
        
// Start image table
        
?>

                <div class="container-list-image">
                    <div class="list-head">
                        <span class="list-head-left"><?php echo $imageCount,' Image',$plural?></span>
                        <span class="list-head-right">Properties</span>
                    </div>
                    <table class="list-image"><?php
        
// Row output loop
        
foreach ($imageListArray AS $file) {
            
// Initialize all recycled variables
            
$fullPath $filePath $file;
            
$imageNumber++;
 
            
// Determine Size, Date and Dimensions of image
            
$fileSize sizeCheck($fullPath);
            
$fileSizeInteger filesize($fullPath);
            
$fileDate '<span title="File Date">' date('Y\-m\-d g\:ia O'filemtime($fullPath)) . '</span>';
            
$safeLink rawurlencode($file);
            
$safeName htmlspecialchars($fileENT_QUOTES);
            
$imageSizeLimitK sizeCheck($imageSizeLimit);
            
$imageSize = @getimagesize($fullPath$imageInfo);
            
            
// If getimagesize() worked, set the image link.
            
if (isset($imageSize)) {
                
$width $imageSize[0];
                
$height $imageSize[1];
                
$sizeTag '<span title="Dimensions">' $width
                         
' x ' $height
                         
' - ' $fileSize
                         
'</span>';
                
// Display links instead for images larger than $imageSizeLimit or wider than {$imageWidthLimit}px
                
if (($width $imageWidthLimit)
                    &&(
$fileSizeInteger $imageSizeLimit)
                ) {
                    
$image 'Image file larger than ' $imageSizeLimitK
                           
' and wider than ' $imageWidthLimit
                           
' pixels. <a name="' $imageNumber
                           
'"><a href="' $uriPath $safeLink
                           
'">Click here to view.</a></a>';
                } elseif (
$fileSizeInteger $imageSizeLimit) {
                    
$image 'Image file larger than ' $imageSizeLimitK
                           
'. <a name="' $imageNumber
                           
'"><a href="' $uriPath $safeLink
                           
'">Click here to view.</a></a>';
                } elseif (
$width $imageWidthLimit) {
                    
$image 'Image is wider than ' $imageWidthLimit
                           
' pixels. <a name="' $imageNumber
                           
'"><a href="' $uriPath $safeLink
                           
'">Click here to view.</a></a>';
                } else {
                    
$image '<img src="' $uriPath $safeLink
                           
'" width="' $width
                           
'" height="' $height
                           
'" alt="Image ' $imageNumber
                           
'">';
                }
            } else {
                
$image 'Not a valid image file.';
                
$sizeTag '<br>' $fileSize;
            }

            
// Get all image info.
            
$imageInfoArray getImageInfo($fullPath);

            
// Echo image row
            
?>

                        <tr><td><a name="<?php echo $imageNumber?>"></a></td></tr>
                        <!--Begin Row <?php echo $imageNumber ;?>-->
                        <tr class="row">
                            <td class="image">
                                <?php echo $image?>

                            </td>
                            <td class="image-info">
                                <div class="image-info-block">
                                    <div class="info-head">
                                        <span class="image-file-name">
                                            <a href="<?php echo $uriPath$safeLink?>"><?php echo $safeName?></a>
                                        </span><br>
                                        <span class="file-date"><?php echo $fileDate?></span><br>
                                        <span class="file-size"><?php echo $sizeTag?></span><?php echo '
                                        <a href="#' 
$imageNumber '" class="image-number">' $imageNumber '</a>';?>

                                    </div><?php
            
if ($imageInfoArray['exifDataExists']) { ?>

                                    <div class="exif-details">
                                        <span title="Capture Time"><?php echo $imageInfoArray['TimeTaken']; ?></span><br>
                                        <span title="Camera Model"><?php echo $imageInfoArray['Model']; ?></span><br>
                                        <?php if ($imageInfoArray['Lens']) echo '<span title="Lens Model">' $imageInfoArray['Lens'] . '</span> @';?><span title="Lens Focal Length"><?php echo $imageInfoArray['Focal']; ?></span><br>
                                        <span title="Shutter Speed"><?php echo $imageInfoArray['ExposureTime']; ?></span>
                                         | <span title="Aperture Value"><?php echo $imageInfoArray['Fnumber']; ?></span>
                                         | <span title="ISO Sensitivity"><?php echo $imageInfoArray['ISO']; ?></span>
                                    </div><?php
            
}
            if (
$imageInfoArray['keywordList'] || $imageInfoArray['creator'] || $imageInfoArray['description']) { ?>

                                    <div class="other-info<?php if ($imageInfoArray['creator']) echo " creator"; if ($imageInfoArray['keywordList']) echo " keywords" ?>"><?php
                
if ($imageInfoArray['description']) { ?>

                                        <span class="keywords"><?php echo $imageInfoArray['description']; ?></span><br><br><?php
                
}
                if (
$imageInfoArray['creator']) { ?>

                                        <span class="creator">Created by: <?php echo $imageInfoArray['creator']; ?></span><br><?php
                
}
                if (
$imageInfoArray['keywordList']) { ?>

                                        <span class="keywords" title="Keywords">Keywords: <?php echo $imageInfoArray['keywordList']; ?></span><?php
                
}
                
?>

                                    </div><?php
            
}
            if (isset(
$imageInfoArray['arrLatLon'][0]) 
                && isset(
$imageInfoArray['arrLatLon'][1])
            ) { 
?>

                                    <div class="GPS-Location">
                                        <div class="geotag" title="Geotagged">
                                            Map: <a href="https://maps.google.ca/maps?z=15&amp;q=loc:<?php echo $imageInfoArray['arrLatLon'][0] . '+' $imageInfoArray['arrLatLon'][1]; ?>
                                                class="external" target="_blank"><?php echo round($imageInfoArray['arrLatLon'][0], 3) . ', ' round($imageInfoArray['arrLatLon'][1], 3); ?></a>
                                        </div>
                                    </div><?php 
            
?>

                                <!--End image-info-block-->
                                </div>
                            </td>
                        <!--End Row <?php echo $imageNumber ;?>-->
                        </tr><?php
        
}
        
?>

                    </table>
                <!--End container-list-image-->
                </div><?php
    
}
}

/*Directory and File List*/
if ($handle opendir($filePath)) {
    
$dirListArray createDirArray($handle$filePath$dirBlacklist);
}
if (
$handle opendir($filePath)) {
    
$fileListArray createFileArray($handle$filePath$fileBlacklist);
}

// Create heading for list if either Directory or File array was created.
if (isset($dirListArray) || isset($fileListArray)) {
    
?>

                <div class="container-list-file">
                    <div class="list-head">
                        <span class="list-head-left">Name</span>
                        <span class="list-head-center">Count</span>
                        <span class="list-head-right">Properties</span>
                    </div>
                    <table class="list-file"><?php
    
}
// Create Directory list.
if (isset($dirListArray)) {
    
array_multisort(array_map('strtolower'$dirListArray), $dirListArray); // Sort array, low to high, case insensitive
    // If on main /photos/ listing page, reverse order so the newest goes to the top.
    
if ($onPhotosPage) {
        
$dirListArray array_reverse($dirListArray);
    }
    
reset($dirListArray);
    
// Row output loop
    
foreach ($dirListArray AS $dir) {
        
$fullPath $filePath $dir;
        
$fileDate date('Y\-m\-d g\:ia O'filemtime($fullPath));
        
$dirItemCount count(scandir($fullPath))-2;
        if (
$onPhotosPage == true) {
            
$fileSize =  '&nbsp;';
        } else {
            
$fileSize sizeCheck($fullPath);
        }
         if (
$fileSize == '&nbsp;') {
            
$fileSize '<span class="file-info-left">&nbsp;</span>';
        } elseif (
$fileSize != '') {
            
$fileSize '<span class="file-info-left">'.$fileSize.' |&nbsp;</span>';
        }
        
$safedir rawurlencode($dir).'/';
        
$safeName htmlspecialchars($dirENT_QUOTES).'/';
        
// Echo directory row
        
?>

                        <tr class="row">
                            <td class="file">
                                <p class="folder">
                                    <a href="<?php echo $uriPath,$safedir?>"><?php echo $safeName?></a>
                                </p>
                            </td>
                            <td class="file-count">
                                <span class="file-count-span"><?php echo $dirItemCount?></span>
                            </td>
                            <td class="file-info">
                                <?php echo $fileSize?>

                                <span class="file-info-right"><?php echo $fileDate?></span>
                            </td>
                        </tr><?php
    
}
}
// Create File list.
if (isset($fileListArray)) {
    
array_multisort(array_map('strtolower'$fileListArray), $fileListArray); // Sort array, low to high, case insensitive
    
reset($fileListArray);
    
// Row output loop
    
foreach ($fileListArray AS $file) {
        
$fullPath $filePath $file;
        
$fileDate date('Y\-m\-d g\:ia O'filemtime($fullPath));
        
$fileSize sizeCheck($fullPath);
        
$safeLink rawurlencode($file);
        
$safeName htmlspecialchars($fileENT_QUOTES);
        
// Echo file row
        
?>

                        <tr class="row">
                            <td class="file">
                                <p class="file">
                                    <a href="<?php echo $uriPath,$safeLink?>"><?php echo $safeName?></a>
                                </p>
                            </td>
                            <td class="file-info">
                                <span class="file-info-left"><?php echo $fileSize?> |&nbsp;</span>
                                <span class="file-info-right"><?php echo $fileDate?></span>
                            </td>
                        </tr><?php
    
}
}
// If Directory/file list was created, close the table.
if (isset($dirListArray) || isset($fileListArray)) {
        
?>

                    </table>
                <!--End container-list-file-->
                </div><?php
}

// Report when there is nothing in the directory
if (!isset($dirListArray)
    &&!isset(
$fileListArray)
    &&!isset(
$imageListArray)
) {
    
?>
                <div class="container-list-file">
                    <div class="list-head">
                        <span class="list-head-right">&nbsp;</span>
                    </div>
                    <table class="list-file empty">
                        <tr>
                            <td>Sorry, there is nothing in this directory.</td>
                        </tr>
                    </table>
                </div><?php
}

// Close the page
?>

            <!--End content-->
            </div>
            <div id="footer" class="grey<?php if(!$displayFooter) { echo ' hiddenfooter'; } ?>">
                <?php echo $footerBrand?> 
                - <span title="(php version)"><?php echo phpversion(); ?></span>
                - <span title="(Raw hit count)"><?php echo hitCounter($filePath 'count.log'); ?></span>
                - <span title="(Page generation time)"><?php echo (round((microtime(true) - $startTime) * 1000)), 'ms'?></span>
                - <a href="<?php echo $uri'?s'?>">Source</a>
            </div>
        <!--End container-->
        </div>
        <script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-19685722-1']);
            _gaq.push(['_setDomainName', '.gwcphoto.ca']);
            _gaq.push(['_trackPageview']);
            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
        </script>
    </body>
</html>