<?php
########################################################################
#
# Project: FreeGallery
# URL: http://www.nabber.org/projects/
# E-mail: hide@address.com
#
# Copyright: (C) 2003-2010, Neil McNab
# License: GNU General Public License Version 2
# (http://www.gnu.org/copyleft/gpl.html)
#
# 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
#
# Filename: $URL: https://freegallery.svn.sourceforge.net/svnroot/freegallery/releases/freegallery-2.0/includes/comment.php $
# Author(s): Neil McNab
# Last Updated: $Date: 2010-05-21 16:53:57 -0700 (Fri, 21 May 2010) $
#
# Description:
# This file generates comments for a file or directory.
#
########################################################################
include_once('gallery_decode.php');
function get_comment($filename, $ext) {
$comment = "";
if (is_dir($filename)) {
$comment = get_dir_comment($filename);
if (empty($comment)) {
$comment = get_galbum_comment($filename);
}
} else {
// try different comment locations
$comment = get_txt_comment($filename);
if (empty($comment)) {
$comment = get_gitem_comment($filename);
}
if (empty($comment)) {
if ($ext == '.jpg' or $ext == '.jpeg' or image_type_to_mime_type($ext) == 'image/jpeg') {
$comment = get_jpeg_comment($filename);
}
elseif ($ext == '.png' or image_type_to_mime_type($ext) == 'image/png') {
$comment = get_png_comment($filename);
}
}
// last resort, use filename as comment
if (empty($comment)) {
$comment = substr(basename($filename), 0, strrpos($filename, "."));
}
}
return $comment;
}
function get_txt_comment($filename) {
$textdesc = "";
if (is_readable($filename . ".txt")) {
$textdesc = file_get_contents($filename . ".txt");
}
return $textdesc;
}
function get_galbum_comment($filename) {
$textdesc = '';
if (!is_readable($filename . "/album.dat"))
return $textdesc;
$Album = get_object($filename . "/album.dat");
if (isset($Album)) {
$textdesc = $Album->fields['description'];
}
return $textdesc;
}
function get_gitem_comment($filename) {
$textdesc = "";
$dir = dirname($filename);
if (!is_readable($dir . "/photos.dat"))
return $textdesc;
$AlbumItem = get_object($dir . "/photos.dat");
if (isset($AlbumItem)) {
for ($j = 0; $j < sizeof($AlbumItem); $j++) {
if (!strcmp($AlbumItem[$j]->image->name . "." . $AlbumItem[$j]->image->type, basename($filename)))
$textdesc = $AlbumItem[$j]->caption;
}
}
return $textdesc;
}
function get_dir_comment($filename) {
$textdesc = "";
if (is_readable($filename . "/desc.txt")) {
$textdesc = file_get_contents($filename . "/desc.txt");
}
return $textdesc;
}
function get_jpeg_comment($filename) {
$handle = fopen($filename, "rb");
# get length
fseek($handle, 0, SEEK_END);
$fend = ftell($handle);
# reset to start of file
fseek($handle, 0, SEEK_SET);
# first bytes of JPEG are always the same
$header = fread($handle, 2);
while(ftell($handle) <= $fend) {
$type_code = fread($handle, 2);
$charlength = fread($handle, 2);
# Subtract off length bytes
$length = ord($charlength[1]) + (ord($charlength[0]) << 8) - 2;
# start of comment indicator
if ((ord($type_code[0]) == 0xff) and (ord($type_code[1]) == 0xfe)) {
$comment_field = fread($handle, $length);
fclose($handle);
return $comment_field;
}
else {
fseek($handle, $length, SEEK_CUR);
}
}
fclose($handle);
return "";
}
function get_png_comment($filename) {
# from RFC 2083
$PNG_COMMENT = "comment";
$crc_size = 4;
$handle = fopen($filename, "rb");
# get length
fseek($handle, 0, SEEK_END);
$fend = ftell($handle);
# reset to start of file
fseek($handle, 0, SEEK_SET);
# first bytes of PNG are always the same
$nullspace = fread($handle, 8);
while(ftell($handle) <= $fend) {
$charlength = fread($handle, 4);
$length = ord($charlength[3]) + (ord($charlength[2]) << 8) +
(ord($charlength[1]) << 16) + (ord($charlength[0]) << 24);
$type_code = fread($handle, 4);
# start of comment indicator
if ($type_code == "tEXt") {
$comment_field = fread($handle, $length);
# parse out keyword and comment data
if (!strcasecmp(substr($comment_field, 0, strlen($PNG_COMMENT)), $PNG_COMMENT)) {
$comment = substr($comment_field, strlen($PNG_COMMENT) + 1);
fclose($handle);
return $comment;
}
}
else {
fseek($handle, $length, SEEK_CUR);
}
# always remove CRC, we aren't checking it
fseek($handle, $crc_size, SEEK_CUR);
}
fclose($handle);
return "";
}
?>