<?php

/*
    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 3 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, see <http://www.gnu.org/licenses/>.

    Additional conditions: You may not alter or remove or render useless
    the part of this program called: Check Credits
*/


// No Direct Access
defined('_JEXEC') or die('Restricted Access');

// Program Information
$pname = "kac_imagedisplay";
$pversion = "v1.5.2";


// Get Params
$idname = $params->get('idname', '');
$pictureurl = $params->get('pictureurl', '');
$noimageurl = $params->get('noimageurl', '');
$link = $params->get('link', '');
$border = $params->get('border', 0);
$text = $params->get('text', '');
$seperator = $params->get('seperator', '|');
$noimagetext = $params->get('noimagetext', '');
$maxwidth = $params->get('maxwidth', '');
$maxheight = $params->get('maxheight', '');
$forcedcr = $params->get('forcedcr', '');
$forcedspace = $params->get('forcedspace', '');
$pretext = $params->get('pretext', '');
$posttext = $params->get('posttext', '');
$pretextcr = $params->get('pretextcr', '');
$posttextcr = $params->get('posttextcr', '');
$randomimage = $params->get('randomimage', '');
$credit = $params->get('credit', '');
$toggle_dir = $params->get('toggle_dir', '');
$toggle_showfile = $params->get('toggle_showfile', '');
$toggle_sort = $params->get('toggle_sort', '');
$set_month = $params->get('set_month', '');
$set_day = $params->get('set_day', '');
$set_year = $params->get('set_year', '');
$set_mode = $params->get('set_mode', '');
$set_title = $params->get('set_title', '');
$noeventurl = $params->get('noeventurl', '');
$noeventtext = $params->get('noeventtext', '');


// Set Values
$display = '<div id="'.$idname.'">';
$user =& JFactory::getUser();
$month = date("m");
$day = date("d");
$year = date("Y");
$today_date = $year.$month.$day;
if (strlen($set_year) <> 4) { $set_year = "xxxx"; }
$file_year = "";
$file_month = "";
$file_day = "";


// Insert Pretext
if ($pretext <> '') {
  $display .= $pretext.'<br />';
  if ($pretextcr == '1') {
    $display .= '<br />';
  }
}


// Check No Image Exists
if ($noimageurl <> "") {
  $image = 0;
  if (@GetImageSize($noimageurl)) {
    $image = 1;
    list($infowidth, $infoheight) = GetImageSize($noimageurl);
    $script = "";
    if ($maxwidth <> "" && $maxwidth < $infowidth) {
      $ratio = $maxwidth/$infowidth;
      $width = floor($ratio*$infowidth); 
      $script .= 'width="'.$width.'"';
    }
    if ($maxheight <> "" && $maxheight < $infoheight) {
      $ratio = $maxheight/$infoheight;
      $height = floor($ratio*$infoheight); 
      $script .= 'height="'.$height.'"';
    }
  }
  $noimage = '<img src="'.$noimageurl.'"  '.$script.' border="0" alt="'.$noimagetext.'" title="'.$noimagetext.'" />';
}


// Seperate And Count Strings
$pictureurl = explode($seperator, $pictureurl);
$count_pictureurl = count($pictureurl);
$link = explode($seperator, $link);
$text = explode($seperator, $text);
$forcedcr = explode($seperator, $forcedcr);


// If Directory Toggle Is Yes
if ($toggle_dir == "1") {
  if (substr($pictureurl[0], -1) <> "/") {
    $pictureurl[0] .= "/";
  }
  $x = 0;
  $dirname = $pictureurl[0];
  $newlink = $link[0];
  $newtext = $text[0];
  $newforcedcr = $forcedcr[0];
  $dir = @opendir($dirname);
  while(false != ($file = readdir($dir))) { 
    if(($file != "." && $file != "..")) {
      $filetype = explode(".", $file);
      $filetype[1] = strtolower($filetype[1]);
      if ($filetype[1] == "png" || $filetype[1] == "gif" || $filetype[1] == "jpg") {
        if ($set_mode != "0" && $set_year == "xxxx" && $set_month == "xx" && $set_day == "xx") {
          $filedate = explode("-", $file);
            $file_year[$x] = substr($filedate[0], 0, 4);
            $file_month[$x] = substr($filedate[0], 4, 2);
            $file_day[$x] = substr($filedate[0], 6, 2);
        }
        $pictureurl[$x] = $dirname.$file;
        $check_pictureurl[$x] = $pictureurl[$x];
        $link[$x] = $newlink;
        $check_link[$x] = $link[$x];
        $text[$x] = $newtext;
        $check_text[$x] = $text[$x];
        if ($toggle_showfile == "1") {
          $text[$x] .= ' ('.$file.')';
        }
        $x++;
      }
    }
  }
$count_pictureurl = $x;
}


// If Directory Toggle Is Yes and Mode Not Set To Always
if ($set_mode != "0" && $set_year == "xxxx" && $set_month == "xx" && $set_day == "xx") {
  $x = 0;
  $c = 0;
  $pictureurl = "";
  $link = "";
  $text = "";
  while ($x <= $count_pictureurl) {
    $date_check = "1";
    if ($set_mode == "1") {
      if ($file_year[$x] == $year || $file_year[$x] == "xxxx") {
        if ($file_month[$x] == $month || $file_month[$x] == "xx") {
          if ($file_day[$x] == $day || $file_day[$x] == "xx") {
          } else {
            $date_check = "0";
          }
        } else {
          $date_check = "0";
        }
      } else {
        $date_check = "0";
      }
    }
    if ($set_mode == "2") {
      if ($file_year[$x] >= $year || $file_year[$x] == "xxxx") {
        if ($file_month[$x] >= $month || $file_month[$x] == "xx") {
          if ($file_day[$x] >= $day || $file_day[$x] == "xx") {
          } else {
            $date_check = "0";
          }
        } else {
          $date_check = "0";
        }
      } else {
        $date_check = "0";
      }
    }
    if ($date_check == "1") {
      $pictureurl[$c] = $check_pictureurl[$x];
      $link[$c] = $check_link[$x];
      $text[$c] = $check_text[$x];
      $c++;
    }
  $x++;
  }
$count_pictureurl = $c;
}


// Sort By URL
if ($toggle_sort == "1") {
  sort($pictureurl);
}
if ($toggle_sort == "2") {
  rsort($pictureurl);
}


// Start Loop
$x = 0;
while ($x <= $count_pictureurl - 1) {
	

// If Random Iamge Is On
if ($randomimage == "1") {
  $x = rand(0, $count_pictureurl - 1);
}


// Check If Image Exists
$image = 0;
if (@GetImageSize($pictureurl[$x])) {
	$image = 1;
	list($infowidth, $infoheight) = GetImageSize($pictureurl[$x]);
  $script = "";
	if ($maxwidth <> "" && $maxwidth < $infowidth) {
    $ratio = $maxwidth/$infowidth;
    $width = floor($ratio*$infowidth); 
    $script .= 'width="'.$width.'"';
  }
	if ($maxheight <> "" && $maxheight < $infoheight) {
    $ratio = $maxheight/$infoheight;
    $height = floor($ratio*$infoheight); 
    $script .= 'height="'.$height.'"';
  }
}


// Prepare Display
if ($image == 1) {
  $build = "";
  if ($set_title <> "0") {
    $title = explode("/", $pictureurl[$x]);
    $title_count = count($title);
    $title = $title[$title_count-1];
    $title = explode("-", $title);
    $title_count = count($title);
    $title = $title[$title_count-1];
    $title = explode(".", $title);
    $title = $title[0];
    $title = str_replace("_", " ", $title);
    $title = ucwords(strtolower($title));
    $build .= '<br />'.$title.'<br />';
  }
	if ($link[$x] <> "") {
		$build .= '<a href="'.$link[$x].'">';
	}
	$build .= '<img src="'.$pictureurl[$x].'" '.$script.' border="0" alt="'.$text[$x].'" title="'.$text[$x].'" />';
	if ($link <> "") {
		$build .= '</a>';
	}
  if ($set_mode != "0") {
    if ($set_mode == "1") {
      if ($set_year == $year || $set_year == "xxxx") {
        if ($set_month == $month || $set_month == "xx") {
          if ($set_day == $day || $set_day == "xx") {
          } else {
            $build = "";
          }
        } else {
          $build = "";
        }
      } else {
        $build = "";
      }
    }
   if ($set_mode == "2") {
      if ($set_year >= $year || $set_year == "xxxx") {
        if ($set_month >= $month || $set_month == "xx") {
          if ($set_day >= $day || $set_day == "xx") {
          } else {
            $build = "";
          }
        } else {
          $build = "";
        }
      } else {
        $build = "";
      }
    }
  }
	$display .= $build;
} else {
	$display .= $noimage;
}


// Check For Forced Carrage Return After Image and Forced Space
if ($forcedcr[$x] == "y" && $build != "") {
	$display .= '<br />';
} else {
	if ($forcedspace >= '1' && $x <> $count_pictureurl - 1 && $build != "") {
		$loop = 1;
		while ($loop <= $forcedspace) {
			$display .= '&nbsp;';
		$loop++;
		}
	}
}


// If Random Image Is On
if ($randomimage == "1") {
  $x = $count_pictureurl;
}


// Stop Loop
$x++;
}


// Check If No Event Exists
if ($noeventurl <> "" && $count_pictureurl == "0") {
  $image = 0;
  if (@GetImageSize($noeventurl)) {
    $image = 1;
    list($infowidth, $infoheight) = GetImageSize($noimageurl);
    $script = "";
    if ($maxwidth <> "" && $maxwidth < $infowidth) {
      $ratio = $maxwidth/$infowidth;
      $width = floor($ratio*$infowidth); 
      $script .= 'width="'.$width.'"';
    }
    if ($maxheight <> "" && $maxheight < $infoheight) {
      $ratio = $maxheight/$infoheight;
      $height = floor($ratio*$infoheight); 
      $script .= 'height="'.$height.'"';
    }
  }
  $display .= '<img src="'.$noeventurl.'"  '.$script.' border="0" alt="'.$noeventtext.'" title="'.$noeventtext.'" />';
}


// Insert Posttext
if ($posttext <> '') {
  if ($posttextcr == '1') {
    $display .= '<br />';
  }
  $display .= '<br />'.$posttext;
}


// Close Div Tag
$display .= '</div>';


// Check Credits
$creditpre = '<a href="http://kac.herobo.com"><span style="font-size:9px;"><br /></br /><center>';
$poweredby = 'powered by:';
$creditpost = '</center></span></a>';
if ($credit == "0") {
  $display .= $creditpre.'('.$poweredby.' '.$pname.' '.$pversion.')'.$creditpost;
}
if ($credit == "1" && $user->username == "Kimberly2000") {
  $display .= $creditpre.'*('.$poweredby.' '.$pname.' '.$pversion.')*'.$creditpost;
}
$display .= '<div style="display:none">kac1968imagedsplay0803tjac</div>';


// Print Display
echo $display;

?>