###############################################################################
# BizDesign ImageFolio Lite Edition 4.2 (Four-dot-Two)
###############################################################################
#
#          ##          ###        VERSION       :  4.2
#        ####        ##   ##      RELEASED ON   :  02/15/2007
#      ##  ##              ##     LAST MODIFIED :  02/15/2007
#    ##########          ##       
#          ##   ###    ##         
#          ##   ###  ########     
#
###############################################################################
# Released by BizDesign, Inc.
# written by Dirk Koppers and Greg Raaum of BizDesign
#
# Purchasing :  http://imagefolio.com/purchase/
# Support    :  http://imagefolio.com/support/
# Phone      :  (214) 642-9787
# Email      :  sales@bizdesign.com
###############################################################################
# COPYRIGHT AND LICENSE INFORMATION :
#
# Copyright (c) 1999-2007 BizDesign, Inc. All rights reserved.
#
# Selling or distributing the code for this program without prior written
# consent is expressly forbidden.
#
# One licensed copy of the program may reside on a single server, in use by a
# single domain.  For each installed instance of the program, a separate
# license is required.
#
# Licensed users may alter or modify this software, at their own risk, of
# course.  They may also hire others to modify their own copy of the code, as
# long as the code is not transferred to or retained by the individual who is
# hired (unless he/she is also a license holder). Although license-holders
# may modify the code for their use, modified code may NOT be resold or
# distributed.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BIZDESIGN,
# INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# BizDesign, Inc. is not liable for any conduct associated with image gallery
# activity, nor for any binary media posted using this program, including but
# not imited to images, photographs, movies, and clipart.
#
# The user must assume the entire risk of using the program.  Although this
# program has been thoroughly tested on BizDesign's servers, BizDesign does not
# warrant that it works on all servers and will not be held liable for anything,
# including but not limited to, misusage, error, or loss of data.  ANY
# LIABILITY OF THE SELLER WILL BE LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT
# OR REFUND OF PURCHASE PRICE.  Use at your own risk!
###############################################################################
# Do not modify below this line unless you know what you are doing.
###############################################################################

##############################################################################################
#########                     NO EDITTING NEEDED BELOW THIS LINE                     #########
##############################################################################################

use Image::IPTCInfo;
use Image::Info qw(image_info);
use Image::Size;

#eval "use Encode;1;" or $no_decode_encode = 1;    # uncomment this if you want to use the decode_encode routine
#$decode_charset = "MacRoman";                     # uncomment this if you want to use the decode_encode routine

$encode_charset = "ISO-8859-1"; # "UTF-8" or "ISO-8859-1"
&Error('You will need to download and install the Perl Module <a href="http://search.cpan.org/~dankogai/Encode/" target="_blank">Encode.pm</a> on your server. Ask your host for help if needed.') if $decode_charset && $no_decode_encode;
$if_javascript .= qq|<meta http-equiv="Content-Type" content="text/html; charset=$encode_charset">| if $decode_charset;
sub decode_encode {
   my $input = shift;
   #$input =~ s/\015\012?/\n/g; # (CRLF to LF) and (CR to LF)
   #$input =~ s/\\usepackage\[applemac\]\{inputenc\}/\\usepackage\[latin1\]\{inputenc\}/g;
   return encode($encode_charset,decode($decode_charset,$input));
}


##############################################################################################
#########                              CREATE THE TABLE                              #########
##############################################################################################
sub iptc_create_table{
   my ($base) = $iptc_image =~ m,^(.*)\.\w+$,;
   my ($ext) = ($iptc_image =~ /\.([^.]+)$/);
   if ($ext =~ /jpg/i) {
      $iptc_image_path = qq|$image_directory/$iptc_category/$iptc_image|;
      $iptc_image_found = 1;
   }
   elsif (-e "$image_directory/$iptc_category/$mypattern$base.jpg") {
      $iptc_image_path = qq|$image_directory/$iptc_category/$mypattern$base.jpg|;
      $iptc_image_found = 1;
   }
   elsif (-e "$image_directory/$iptc_category/$mypattern$base.JPG") {
      $iptc_image_path = qq|$image_directory/$iptc_category/$mypattern$base.JPG|;
      $iptc_image_found = 1;
   }
   if ($iptc_image_found) {
      if (!$wrap) {
         $nowrap = "nowrap";
      }
      my $iptc_info = new Image::IPTCInfo("$iptc_image_path");
      if (defined($iptc_info)){
         foreach (@ignore_keywords) {
            $ignore_keyword{lc($_)} = 1;
         }
         $tablestart = qq|<table border="0" cellspacing="1" cellpadding="3" $iptc_table_border_color $iptc_table_width><tr><td colspan="2" $iptc_table_heading_bgcolor align="center">$iptc_table_header</td></tr>|;
         foreach $key (sort keys %IPTCfields) {
            $iptcbytes    = $IPTCfields{$key}[0];
            $iptcattr     = $IPTCfields{$key}[1];
            $iptcfield    = $IPTCfields{$key}[2];
            $iptcrule     = $IPTCfields{$key}[3];
            if ($iptcrule==1 or $iptcrule==2) {
               if (!$clickable_search_words) {
                  $iptcrule = 1;
               }
               if ($iptcattr eq 'keywords') {
                  my $keywordsRef = $iptc_info->Keywords();
                  if (defined($keywordsRef)) {
                     if (!$iptcdata){
                        $iptc_table = $tablestart;
                        $iptcdata = 1;
                     }
                     $iptc_table .= qq|<tr><td $iptc_table_field_bgcolor width="30%"><$iptc_field_font>$iptcfield</font></td><td $iptc_table_value_bgcolor width="70%"><$iptc_value_font>|;
                     if ($iptcrule==1) {
                        foreach $iptc_keyword (@$keywordsRef) {
                           $iptc_keyword = &decode_encode($iptc_keyword) if $decode_charset;
                           $iptc_keyword =~ s/(<|\%\W*3\W*c|\&lt)\/?[^>]*(>|\%\W*3\W*e|\&gt)//mig;
                           next if $ignore_keyword{lc($iptc_keyword)};
                           $show_similar .= qq|$iptc_keyword+|;
                           $iptc_keyword =~ s/\+/ /g;
                           $iptc_table .= qq|$iptc_keyword |;
                           $IPTCtag{$iptcattr} .= qq|$iptc_keyword |;
                        }
                     }
                     else {
                        foreach $iptc_keyword (@$keywordsRef) {
                           $iptc_keyword = &decode_encode($iptc_keyword) if $decode_charset;
                           $iptc_keyword =~ s/(<|\%\W*3\W*c|\&lt)\/?[^>]*(>|\%\W*3\W*e|\&gt)//mig;
                           $iptc_keyword_blanked = $iptc_keyword;
                           $iptc_keyword_blanked =~ s/\+/ /g;
                           next if $ignore_keyword{lc($iptc_keyword_blanked)};
                           $iptc_table .= qq|<a href="$imagefolio_url?img=0&search=$iptc_keyword&cat=all&bool=phrase">$iptc_keyword_blanked</a> |;
                           $IPTCtag{$iptcattr} .= qq|<a href="$imagefolio_url?img=0&search=$iptc_keyword&cat=all&bool=phrase">$iptc_keyword_blanked</a> |;
                           $show_similar .= qq|$iptc_keyword+|;
                        }
                     }
                     $iptc_table .= qq|</font></td></tr>|;
                  }
               }
               else {
                  $iptcvalue = $iptc_info->Attribute("$iptcattr");
                  $iptcvalue = &decode_encode($iptcvalue) if $decode_charset;
                  if ($iptcvalue =~ /\S+/) {
                     $iptcvalue =~ s/(<|\%\W*3\W*c|\&lt)\/?[^>]*(>|\%\W*3\W*e|\&gt)//mig;
                     if (!$iptcdata){
                        $iptc_table = $tablestart;
                        $iptcdata = 1;
                     }
                     else {
                        $second_value = 1;
                     }
                     my $start_iptc_row = qq|<tr><td $iptc_table_field_bgcolor width="30%" $nowrap><$iptc_field_font>$iptcfield</font></td><td $iptc_table_value_bgcolor width="70%"><$iptc_value_font>|;
                     $iptc_table .= $start_iptc_row;
                     if ($iptcattr eq 'custom18') {
                        $found_custom18 = 1;
                        ($custom18_date, $custom18_user, $custom18_email, $custom18_approval, $custom18_file_type) = $iptcvalue =~ /\[(.*?)\]/g;
                        if ($custom18_user) {
                           if ($iptcrule==1) {
                              $iptc_table .= $custom18_user;
                              $IPTCtag{$iptcattr} = qq|$custom18_user|;
                           }
                           else {
                              $iptc_table .= qq|<a href="$imagefolio_url?search=USER&username=$custom18_user">$custom18_user</a>|;
                              $IPTCtag{$iptcattr} = qq|<a href="$imagefolio_url?search=USER&username=$custom18_user">$custom18_user</a>|;
                           }
                        }
                        else {
                           $iptc_table =~ s/$start_iptc_row//ig;
                           if (!$second_value) {
                              $iptc_table = ();
                              $iptcdata = ();
                           }
                           next;
                        }
                     }
                     elsif ($iptcattr eq 'custom19') {
                        $iptc_table .= qq|<a href="mailto:$iptcvalue">$iptcvalue</a>|;
                        $IPTCtag{$iptcattr} = qq|<a href="mailto:$iptcvalue">$iptcvalue</a>|;
                     }
                     elsif ($iptcattr eq 'custom20') {
                        if ($iptcvalue !~ /http:\/\//i) {
                           $pre = "http://";
                        }
                        $iptc_table .= qq|<a href="$pre$iptcvalue" target="_blank">$iptcvalue</a>|;
                        $IPTCtag{$iptcattr} = qq|<a href="$pre$iptcvalue" target="_blank">$iptcvalue</a>|;
                     }
                     elsif ($iptcrule==1) {
                        if ($iptcattr =~ /date/i) {
                           @character = split(//, $iptcvalue);
                           if ($eu_date_format) {
                              $iptcvalue = qq|@character[6]@character[7]/@character[4]@character[5]/@character[0]@character[1]@character[2]@character[3]|;
                           }
                           else {
                              $iptcvalue = qq|@character[4]@character[5]/@character[6]@character[7]/@character[0]@character[1]@character[2]@character[3]|;
                           }
                        }
                        $iptcvalue =~ s/\+/ /g;
                        $iptc_table .= $iptcvalue;
                        $IPTCtag{$iptcattr} = qq|$iptcvalue|;
                     }
                     elsif ($iptcbytes >= "128") {
                        @splitwords = split(/\s+/, $iptcvalue);
                        foreach my $splitword (@splitwords) {
                           my $showword = $splitword;
                           $showword =~ s/\+/ /g;
                           $iptc_table .= qq|<a href="$imagefolio_url?img=0&search=$splitword&cat=all&bool=phrase">$showword</a> |;
                           $IPTCtag{$iptcattr} .= qq|<a href="$imagefolio_url?img=0&search=$splitword&cat=all&bool=phrase">$showword</a> |;
                        }
                     }
                     else {
                        $phrasewords = $iptcvalue;
                        $phrasewords =~ s/\s/\+/g;
                        if ($iptcattr =~ /date/i) {
                           @character = split(//, $iptcvalue);
                           if ($eu_date_format) {
                              $iptcvalue = qq|@character[6]@character[7]/@character[4]@character[5]/@character[0]@character[1]@character[2]@character[3]|;
                           }
                           else {
                              $iptcvalue = qq|@character[4]@character[5]/@character[6]@character[7]/@character[0]@character[1]@character[2]@character[3]|;
                           }
                        }
                        $iptcvalue =~ s/\+/ /g;
                        $iptc_table .= qq|<a href="$imagefolio_url?img=0&search=$phrasewords&cat=all&bool=phrase">$iptcvalue</a>|;
                        $IPTCtag{$iptcattr} = qq|<a href="$imagefolio_url?img=0&search=$phrasewords&cat=all&bool=phrase">$iptcvalue</a>|;
                     }
                     $iptc_table .= qq|</font></td></tr>|;
                  }
               }
            }
            elsif ($parse_single_iptc_fields) {
               $iptcvalue = $iptc_info->Attribute("$iptcattr");
               $iptcvalue = &decode_encode($iptcvalue) if $decode_charset;
               if ($iptcvalue =~ /\S+/) {
                  if (!$clickable_search_words) {
                     $iptcrule = 4;
                  }
                  $iptcvalue =~ s/(<|\%\W*3\W*c|\&lt)\/?[^>]*(>|\%\W*3\W*e|\&gt)//mig;
                  if ($iptcrule == 4) {
                     $iptcvalue =~ s/\+/ /g;
                     $IPTCtag{$iptcattr} = qq|$iptcvalue|;
                  }
                  elsif ($iptcbytes >= "128") {
                     @splitwords = split(/\s+/, $iptcvalue);
                     foreach $splitword (@splitwords) {
                        my $showword = $splitword;
                        $showword =~ s/\+/ /g;
                        $IPTCtag{$iptcattr} .= qq|<a href="$imagefolio_url?img=0&search=$splitword&cat=all&bool=phrase">$showword</a> |;
                     }
                  }
                  else {
                     $phrasewords = $iptcvalue;
                     $phrasewords=~s/\s/+/g;
                     if ($iptcattr =~ /date/i) {
                        @character = split(//, $iptcvalue);
                           if ($eu_date_format) {
                              $iptcvalue = qq|@character[6]@character[7]/@character[4]@character[5]/@character[0]@character[1]@character[2]@character[3]|;
                           }
                           else {
                              $iptcvalue = qq|@character[4]@character[5]/@character[6]@character[7]/@character[0]@character[1]@character[2]@character[3]|;
                           }
                     }
                     $iptcvalue =~ s/\+/ /g;
                     $IPTCtag{$iptcattr} = qq|<a href="$imagefolio_url?img=0&search=$phrasewords&cat=all&bool=phrase">$iptcvalue</a>|;
                  }
               }
            }
         }
         if ($iptcdata){
            $iptc_table .= qq|</table>|;
         }
         if (!$found_custom18) {
            $iptcvalue = $iptc_info->Attribute('custom18');
            ($custom18_date, $custom18_user, $custom18_email, $custom18_approval, $custom18_file_type) = $iptcvalue =~ /\[(.*?)\]/g;
         }
#         if ($custom18_user) {
#            foreach my $row (@USERCHECK) {
#               my ($this_row_username,$ifuserpass,$ifuserid,$ifemail,$iffirstname,$iflastname,$this_row_group,$ifusername_display) = split(/\|/,$row);
#               if ($custom18_user eq $this_row_username && $this_row_group eq $ifgroup) {
#                  $custom18_user = $ifusername;
#                  last;
#               }
#            }
#         }
##         else {


         if ($group_media_mod_per==1) {
            if ($CPA{"All"}) {
               $custom18_user = $ifusername;
            }
            else {
               my (@iptc_cats) = split(/\//,$iptc_category);
               foreach my $iptc_cat (@iptc_cats) {
                  $check_iptc_cat .= $iptc_cat;
                  if ($CPA{$check_iptc_cat}) {
                     $custom18_user = $ifusername;
                     last;
                  }
                  $check_iptc_cat .= "/";
               }
            }
         }
#            $custom18_user = $ifusername if ($group_media_mod_per==1 && $CPA{$iptc_category});
##         }
      }
   }
}
sub iptc_thumbnail{
   $iptc_list = ();
   $iptc_list2 = ();
   $custom18_user = ();
   $custom18_approval = ();
   $custom18_file_type = ();
   my $iptc_info = new Image::IPTCInfo("$iptc_thumnail_path");
   if (defined($iptc_info)){
#      $captioniptcvalue = $iptc_info->Attribute("caption/abstract");
#      if ($captioniptcvalue) {
#         $alt_text=$captioniptcvalue;
#         #use "title=" instead of "alt="!
#      }
      foreach $iptc_thumbnails_field (@iptc_thumbnails_fields) {
         if ($iptc_thumbnails_field eq 'keywords') {
            my $keywordsRef = $iptc_info->Keywords();
            if (defined($keywordsRef)) {
               my $iptc_add_list = ();
               foreach $iptc_keyword (@$keywordsRef) {
                  $iptc_keyword =~ s/\+/ /g;
                  $iptc_keyword = &decode_encode($iptc_keyword) if $decode_charset;
                  $iptc_keyword =~ s/(<|\%\W*3\W*c|\&lt)\/?[^>]*(>|\%\W*3\W*e|\&gt)//mig;
                  $iptc_add_list .= qq|$iptc_keyword |;
               }
               $iptc_list .= "$iptc_add_list<br>" if $IPTC_THUMB_INFO{$iptc_thumbnails_field};
               $iptc_list2 .= "$iptc_add_list<br>" if $IPTC_THUMB_POPUP{$iptc_thumbnails_field};
            }
         }
         else {
            $iptcvalue = $iptc_info->Attribute("$iptc_thumbnails_field");
            $iptcvalue =~ s/\+/ /g;
            $iptcvalue = &decode_encode($iptcvalue) if $decode_charset;
            if ($iptcvalue) {
              $iptcvalue =~ s/(<|\%\W*3\W*c|\&lt)\/?[^>]*(>|\%\W*3\W*e|\&gt)//mig;
               if ($iptc_thumbnails_field eq 'custom18') {
                  $found_custom18 = 1;
                  ($custom18_date, $custom18_user, $custom18_email, $custom18_approval, $custom18_file_type) = $iptcvalue =~ /\[(.*?)\]/g;
                  if ($custom18_user) {
                     my $iptc_add_list = qq|By <a href="$imagefolio_url?search=USER&username=$custom18_user"$thumbinfo_target>$custom18_user</a><br>|;
                     $iptc_list .= $iptc_add_list if $IPTC_THUMB_INFO{$iptc_thumbnails_field};
                     $iptc_list2 .= $iptc_add_list if $IPTC_THUMB_POPUP{$iptc_thumbnails_field};
                  }
               }
               elsif ($iptc_thumbnails_field eq 'custom19') {
                  my $iptc_add_list = qq|<a href="mailto:$iptcvalue">$iptcvalue</a><br>|;
                  $iptc_list .= $iptc_add_list if $IPTC_THUMB_INFO{$iptc_thumbnails_field};
                  $iptc_list2 .= $iptc_add_list if $IPTC_THUMB_POPUP{$iptc_thumbnails_field};
               }
               elsif ($iptc_thumbnails_field eq 'custom20') {
                  $pre = ();
                  if ($iptcvalue !~ /http:\/\//i) {
                     $pre = "http://";
                  }
                  my $iptc_add_list = qq|<a href="$pre$iptcvalue" target="_blank">$iptcvalue</a><br>|;
                  $iptc_list .= $iptc_add_list if $IPTC_THUMB_INFO{$iptc_thumbnails_field};
                  $iptc_list2 .= $iptc_add_list if $IPTC_THUMB_POPUP{$iptc_thumbnails_field};
               }
               else {
                  my $iptc_add_list = $iptcvalue . "<br>";
                  $iptc_list .= $iptc_add_list if $IPTC_THUMB_INFO{$iptc_thumbnails_field};
                  $iptc_list2 .= $iptc_add_list if $IPTC_THUMB_POPUP{$iptc_thumbnails_field};
               }
            }
         }
      }
      if (!$found_custom18) {
         $iptcvalue = $iptc_info->Attribute('custom18');
         ($custom18_date, $custom18_user, $custom18_email, $custom18_approval, $custom18_file_type) = $iptcvalue =~ /\[(.*?)\]/g;
      }
#      if ($custom18_user) {
#         foreach my $row (@USERCHECK) {
#            my ($this_row_username,$ifuserpass,$ifuserid,$ifemail,$iffirstname,$iflastname,$this_row_group,$ifusername_display) = split(/\|/,$row);
#            if ($custom18_user eq $this_row_username && $this_row_group eq $ifgroup) {
#               $custom18_user = $ifusername;
#               last;
#            }
#         }
#      }
##      else {
      if ($CPA{"All"}) {
         $custom18_user = $ifusername;
      }
      elsif ($CPA_OK{$path}) {
         $custom18_user = $ifusername;
      }
      elsif ($group_media_mod_per==1) {
         my (@iptc_cats) = split(/\//,$path);
         my $check_iptc_cat = ();
         foreach my $iptc_cat (@iptc_cats) {
            $check_iptc_cat .= $iptc_cat;
            if ($CPA{$check_iptc_cat}) {
               $CPA_OK{$path} = 1;
               $custom18_user = $ifusername;
               last;
            }
            $check_iptc_cat .= "/";
         }
      }
#         $custom18_user = $ifusername if $group_media_mod_per==1;
##      }
   }
}
1;