# Copyright (c) 2002 Sandino Araico Sánchez /* 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 */ # Changelog: # 2002-01-25 Tony J. White tjw@tjw.org - DirectoryMediaIndex (dmi) 1.2 # 2002-03-14 Sandino Araico Sánchez - separated dmi-lib from dmi-1.2.php # Enclosed inside #ifdef duplicates protection # Use $THUMBS_DIR and HOPP's 's' functions # Text file with image title support # Reverse chronological order # Single image navigation # View thumbnails message # 2002-03-16 Sandino Araico Sánchez - Fixed thumbnails link Bug # 2002-04-09 Sandino Araico Sánchez - HOPP templates support # Different templates for dmi-big # 2002-04-20 Sandino Araico Sánchez # Worked around first & last links not showing problem with edges hidden # 2002-09-25 Sandino Araico Sánchez # Fixed broken JPEG images uploaded from old Macs with old MacOS # Remove Resource.FRK from the beginning of the JPEG image if(!defined('__DMI_LIB__')) { define('__DMI_LIB__',1); //**********************************************************************/ include "dmi-autoconf.inc"; //**********************************************************************/ // handles spaces and such function shellWrap($in) { return str_replace("\n", "\\", preg_replace("/([ \'\"&;\`\(\)?!])/", "\n\\1", $in)); } // gets filename without extension function getFileName($in) { $out = basename($in); $out = substr(strrev(strstr(strrev($out), ".")), 0, -1); return $out; } function safeFileName($in) { $in=preg_replace("/[^a-z0-9\.]/i", "_", $in); return $in; } function stripExtension($in) { if(eregi("(.+)\.[a-z]{3,4}$", $in, $out)) { return $out[1]; } else return $in; } function uniquneName($new_name,$PHOTO_DIR) { while (file_exists("$PHOTO_DIR/$new_name")) { $new_name='z'.$new_name; } return $new_name; } // helper function for interpreting getImageSize() return data function extensionize($in, $type) { if($type == 1) { if(!eregi("\.gif$", $in)) return $in.".gif"; } elseif($type == 2) { if(!eregi("\.jpg$", $in)) return $in.".jpg"; } elseif($type == 3) { if(!eregi("\.png$", $in)) return $in.".png"; } elseif($type == 4) { if(!eregi("\.swf$", $in)) return $in.".swf"; } return $in; } // helper function for interpreting getImageSize() return data function getExtension($type) { if($type == 1) return ".gif"; elseif($type == 2) return ".jpg"; elseif($type == 3) return ".png"; elseif($type == 4) return ".swf"; return ""; } // helper function for interpreting getImageSize() return data function getMimeType($type) { if($type == 1) return "image/gif"; elseif($type == 2) return "image/jpeg"; elseif($type == 3) return "image/png"; elseif($type == 4) return "application/x-shockwave-flash"; return ""; } function is_mpeg_video($filename) { if(!is_executable($GLOBALS["MPGINFO_PROG"])) return 0; $is_mpeg = 0; $cmd = $GLOBALS["MPGINFO_PROG"]." ".shellWrap($filename); $out = `$cmd`; $out = str_replace("\r", "", $out); $lines = explode("\n", $out); while(list($num, $line) = each($lines)) { if(preg_match("/^ Mpeg .*Video/", $line)) $is_mpeg = 1; } return $is_mpeg; } // this function is the replacement for /bin/find to make things a // little safer and to possibly allow windows portability function getListing($allow_recurse=1) { if(!is_dir($GLOBALS["PHOTO_DIR"])) { echo "ERROR: ".$GLOBALS["PHOTO_DIR"]." is not a " ." directory!
"; return 0; } $dir_handle = opendir($GLOBALS["PHOTO_DIR"]); while($file = readdir($dir_handle)) { if($file == ".." || $file == ".") continue; $full_file = $GLOBALS["PHOTO_DIR"]."/".$file; $img_info = @getImageSize($full_file); if(!is_array($img_info) && eregi("\.(jpg|jpeg)$",$full_file)) { s_log_error("Perhaps there's a Resource.frk in $full_file"); clean_resource_frk_from_jpeg($full_file); $img_info = @getImageSize($full_file); } if(is_array($img_info)) { $img_info['title']=getImageTitle($full_file); $img_info['ctime']=filectime ($full_file); $img_info['full_file']=$full_file; $img_infos[] = $img_info; $files[] = $file; $thumb_info = @getImageSize($GLOBALS["THUMBS_DIR"] .stripExtension($file)."_thumb" .getExtension($img_info[2])); if(is_array($thumb_info)) { //foreach($thumb_info as $key => $value) // s_log("key: $key\tvalue: $value"); $thumb_infos[] = $thumb_info; } else { if(!defined('_H_CACHE_NO_STORE')) define('_H_CACHE_NO_STORE',1); $thumb_infos[] = ""; } } elseif(is_mpeg_video($file)) { $files[] = $file; $thumb_info = @getImageSize($GLOBALS["THUMBS_DIR"] .stripExtension($file)."_thumb.gif"); if(is_array($thumb_info)) $thumb_infos[] = $thumb_info; else $thumb_infos[] = ""; } $img_info = ""; $thumb_info = ""; } $sort_infos=getSortInfos($img_infos); return array($files, $thumb_infos, $img_infos, $sort_infos); } function clean_resource_frk_from_jpeg($full_file) { $file=fopen($full_file,'r'); if($file) { s_log("Open $full_file for examining"); $content=fread($file,filesize($full_file)); fclose($file); } if($content) { s_log("The image has something"); $str=rawurldecode("%FF%D8%FF%E0%00%10%4A%46%49%46"); //$str=sprintf('%c%C%c%c%c%c%c%c%c%c','0XFF','0XD8','0XFF','0XE0','0X00','0X10','0X4A','0X46','0X49','0X46'); // returned a 000000 string; s_log("str: ".rawurlencode($str)); // $jpeg=strstr($content,$str); //returned just the first 4 chars $offset=strpos ($content,$str); $jpeg=substr ($content,$offset); } if($jpeg) { $strlen=strlen($jpeg); s_log("Found JPEG in image $strlen bytes"); $file=@fopen($full_file,'w'); if($file) { s_log("Open $full_file for writing JPEG"); fwrite($file,$jpeg); fclose($file); } else { s_log_error("Can not open $full_file for writing"); } } } function getSortInfos($img_infos) { if(!is_array($img_infos)) return 0; foreach($img_infos as $key => $img_info) { $sort_infos[$key]=filectime($img_info['full_file']); } arsort($sort_infos); return $sort_infos; } function getImageTitle($full_file) { $text_file=stripExtension($full_file).'.txt'; #s_log("text_file: $text_file"); if(file_exists($text_file)) { $file=fopen($text_file,'r'); if($file) { $text=fread($file,1024); fclose($file); #s_log("Using text file contents: $text"); } } if(empty($text)) $text=str_replace("_", " ", getFileName($full_file)); #s_log("text: $text"); return nl2br(htmlentities($text)); } function getTableHead($numfiles,$FIRST,$WIDTH,$AREA, $template_prefix='fotos') { global $PHP_SELF,$DMI_SCRIPT,$THUMB_LINK; if($FIRST==0) $previous=''; else { $prev_last=($AREA>$numfiles)?$numfiles:$AREA; $prev_last=$prev_last==1?"":" - $prev_last"; //$previous="1$prev_last << "; $replace=array( '###FIRST_URL###' => $PHP_SELF, '###FIRST_NAME###' => "1$prev_last" ); $previous=h_template_get_parsed_file("$template_prefix-first", $replace); if($FIRST>$AREA) { $prev_first=$FIRST-$AREA; $prev_first_num=$prev_first+1; $prev_first_num=$prev_first_num==$FIRST?"":"$prev_first_num - "; //$previous.="$prev_first_num$FIRST < "; $replace=array( '###PREV_URL###' => "$PHP_SELF?FIRST=$prev_first", '###PREV_NAME###' => "$prev_first_num$FIRST" ); $previous.=h_template_get_parsed_file("$template_prefix-previous", $replace); } else if(DMI_NAV_SHOW_EDGES) { $replace=array( '###PREV_URL###' => $PHP_SELF, '###PREV_NAME###' => "1$prev_last" ); $previous.=h_template_get_parsed_file("$template_prefix-previous", $replace); } } $LAST=$FIRST+$AREA; #s_log("FIRST: $FIRST\tLAST: $LAST\tAREA: $AREA\tnumfiles: $numfiles"); //$LAST=$LAST>$numfiles?$numfiles:$LAST; if($LAST<$numfiles) { $next_last=$LAST+$AREA; if($next_last<$numfiles) { $next_first_num=$LAST+1; $next_first_num=$next_first_num==$next_last?'':"$next_first_num - "; //$next=" > $next_first_num$next_last"; $replace=array( '###NEXT_URL###' => "$PHP_SELF?FIRST=$LAST", '###NEXT_NAME###' => "$next_first_num$next_last" ); $next=h_template_get_parsed_file("$template_prefix-next", $replace); if($numfiles-$next_last>$AREA) $next_last=$numfiles-$AREA; } else { $next_last=$LAST; if(DMI_NAV_SHOW_EDGES) { $last_first_num=$next_last+1; $last_first_num=$last_first_num==$numfiles?'':"$last_first_num - "; $replace=array( '###NEXT_URL###' => "$PHP_SELF?FIRST=$next_last", '###NEXT_NAME###' => "$last_first_num$numfiles" ); $next=h_template_get_parsed_file("$template_prefix-next", $replace); } } $last_first_num=$next_last+1; $last_first_num=$last_first_num==$numfiles?'':"$last_first_num - "; //$next.=" >> $last_first_num$numfiles"; $replace=array( '###LAST_URL###' => "$PHP_SELF?FIRST=$next_last", '###LAST_NAME###' => "$last_first_num$numfiles" ); $next.=h_template_get_parsed_file("$template_prefix-last", $replace); } else { $LAST=$numfiles; $next=''; } if(empty($previous)&&empty($next)) return; $first_num=$FIRST+1; if(defined('__DMI_BIG')) { $replace=array( '###CENT_URL###' => "$DMI_SCRIPT?FIRST=$FIRST", '###CENT_NAME###' => "$THUMB_LINK" ); $center=h_template_get_parsed_file("$template_prefix-center-big", $replace); } else { $replace=array( '###CENT_NAME###' => "$first_num - $LAST" ); $center=h_template_get_parsed_file("$template_prefix-center", $replace); } $replace=array( '###PREV###' => $previous, '###CENT###' => $center, '###NEXT###' => $next ); if(defined('__DMI_BIG')) $return=h_template_get_parsed_file("$template_prefix-table_head-big", $replace); else $return=h_template_get_parsed_file("$template_prefix-table_head", $replace); return $return; } function clearBitmaps($dump_dir) { if(!is_dir($GLOBALS["PHOTO_DIR"]."/thumbs/".$dump_dir)) { return 0; } $full_dump_dir = $GLOBALS["PHOTO_DIR"]."/thumbs/".$dump_dir; $bmp_dir_handle = opendir($full_dump_dir); while($file = readdir($bmp_dir_handle)) { if($file == ".." || $file == ".") continue; $full_file = $GLOBALS["PHOTO_DIR"] ."/thumbs/".$dump_dir."/".$file; if(eregi("\.bmp$", $full_file)) $deleted = unlink($full_file); } $success = @rmdir($full_dump_dir); return $success; } function errorImage() { header("Content-type: image/gif"); // this image is image1.gif blatently ripped off from the // apache icons directory $image = "R0lGODlhFAAWAOMAAP/////MM/9mZsz//8zMzJmZmZlmADMzMwAA" ."AAAAAAAAAAAAAAAAAAAAAAAAAAAAACH+TlRoaXMgYXJ0IGlzIGlu" ."IHRoZSBwdWJsaWMgZG9tYWluLiBLZXZpbiBIdWdoZXMsIGtldmlu" ."aEBlaXQuY29tLCBTZXB0ZW1iZXIgMTk5NQAh+QQBAAADACwAAAAA" ."FAAWAAAEbXDISSm6NVckOtLg5YEZQgTcR1oBqq6S2RovjLQYHJ8BT" ."R2FoFB4cwUPkgNgyWReeghAQaVsNi+X5TRpvWa11K4zytwOqoCoOm" ."01V8nicphthYO59Pj9vLT323NYgl1ueoRUQEOKQwc1go+QKhEAOw" ."=="; echo base64_decode($image); exit(); } //**********************************************************************/ } // __DMI_LIB__ ?>