All pastes #141240 Raw Edit

Something

public text v1 · immutable
#141240 ·published 2006-08-20 22:03 UTC
rendered paste body
<?php
/***********************************************************************

  Copyright (C) 2006 Aizu Ikmal Ahmad (aizu@ikmal.com.my)

  This file is part of iFoto.

  iFoto 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.

  iFoto 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

************************************************************************/

/* configuration constant goes here */

define('SORT_BY', 'filename'); //sort the thumbs by using one of these values - 'filename', 'title', 'width', 'height', 'filesize', 'modified'
define('SORT_ORDER', 'asc');
define('STRIP_SORT_NUMBER', false); //this will stip 1_ , 2_ , 3_ ...etc in beginning of the filename in thumbnails title.
define('DISPLAY_HOMEPAGE_LAST_ADDTION', true);

require('includes/thumbnailer.php');

requirement_checker();

$template = 'default';
if(!is_file('templates/'.$template.'.html')){ errorMsg('Sorry, template '.$template.' does not exist.'); }

require('templates/'.$template.'.html');



function home_page(){
	?>
	<div id="homepage">
		<h3><font color="#FF6600">i</font>Foto</h3>
		My Simple Photo Gallery
		<br /><br />
		Click on the left category to view photos.
		<br /><br />
		Current release : 0.6 (Non-MySQL)


		<?php if(DISPLAY_HOMEPAGE_LAST_ADDTION){html_LastAddition();} ?>

	</div>
	<?php
}

function html_stat(){

}

function html_nav(){

    $opendir = "gallery/";
    if ($handle = opendir($opendir)) {
		echo '<ul>';
        echo '<li class="home_button"><a href="?">Home</a></li>';
        while (false !== ($folder = readdir($handle))) {
        	if (!is_file($folder) && $folder != '.' && $folder != '..') {
				if(strlen($folder) > 20){$folder_sub = substr($folder,0,20).'...';}else{$folder_sub = $folder;}
            	if(get_param('dir') == $folder){$style_on_current = 'class="active"';}else{$style_on_current = '';}
				echo '<li '.$style_on_current.'><a href="?dir='.urlencode($folder).'" title="'.$folder.'">'.$folder_sub.'</a></li>';
            }
        }
		closedir($handle);
		echo '</ul>';
    }

}

function html_contents(){

	if(get_param('pic')){

		if(!get_param('dir')){ exit('<pre>Get dir error.</pre>'); }

		$current_image_list = readDirectory(get_param('dir'));

		$total_images = count($current_image_list);

		$search_key = multi_array_search(get_param('pic'), $current_image_list);
		$current_key = $search_key[0];

		@$curr_image = $current_image_list[$current_key];
		@$prev_image = $current_image_list[$current_key-1];
		@$next_image = $current_image_list[$current_key+1];

		?>
		<div id="fullsize">

			<div class="menu">
				<div class="left">
					Photo <?php echo $current_key+1	; ?> from <?php echo $total_images; ?>
				</div>
				<div class="right">
					<?php if($current_key != 0){ ?><a href="?dir=<?php echo urlencode(get_param('dir')); ?>&pic=<?php echo urlencode($prev_image['title']); ?>" title="<?php echo $prev_image['title'];?>">Prev</a> | <?php }else{ ?> Prev | <?php }; ?>
					<?php if($current_key+1 != $total_images){ ?><a href="?dir=<?php echo urlencode(get_param('dir')); ?>&pic=<?php echo urlencode($next_image['title']); ?>" title="<?php echo $next_image['title'];?>">Next</a><?php }else{ ?> Next <?php }; ?>
				</div>
				<div class="clear">&nbsp;</div>
			</div>


			<div class="image_wrapper">
        		<div class="shadow">&nbsp;</div>
				<div class="image">
					<a href="gallery/<?php echo urldecode($curr_image['dir']); ?>/<?php echo urldecode($curr_image['filename']); ?>" target="_blank"><img src="gallery/<?php echo urldecode($curr_image['dir']); ?>/<?php echo urldecode($curr_image['filename']); ?>" width="560px" border="0"></a>
				</div>
			</div>

			<div class="title"><?php echo $curr_image['title']; ?></div>


			<div class="footer">
				<div class="thumb_wrapper prev">
	        		<div class="content">
	            		<div class="shadow">&nbsp;</div>
						<div class="thumb">
							<?php if($current_key != 0){ ?>
							<a href="?dir=<?php echo urlencode($prev_image['dir']); ?>&pic=<?php echo urlencode($prev_image['title']);?>" title="<?php echo $prev_image['title'];?>"><img src="<?php echo $prev_image['thumb'];?>" border="0"/></a>
							<?php }else{ ?>
							<img src="templates/default/first.jpg">
							<?php } ?>
						</div>
					</div>
				</div>

	        	<div class="desc">
					<ul>
	        		<li><b>Filename :</b> <?php echo $curr_image['filename']; ?></li>
					<li><b>Original Size :</b> <?php echo $curr_image['width']; ?>x<?php echo $curr_image['height']; ?> </li>
					<li><b>Uploaded :</b> <?php echo date('l, j F Y',$curr_image['modified']); ?></li>
					<li><b>Filesize :</b> <?php echo number_format($curr_image['filesize']/1024, 2, '.', '').' kb'; ?></li>
					</ul>
				</div>
	        	<div class="thumb_wrapper next">
	        		<div class="container">
	            		<div class="shadow">&nbsp;</div>
						<div class="thumb">
							<?php if($current_key+1 != $total_images){ ?>
							<a href="?dir=<?php echo urlencode($next_image['dir']); ?>&pic=<?php echo urlencode($next_image['title']);?>" title="<?php echo $next_image['title'];?>"><img src="<?php echo $next_image['thumb'];?>" border="0"/></a>
							<?php }else{ ?>
							<img src="templates/default/last.jpg">
							<?php } ?>
						</div>
					</div>
				</div>
				<div class="clear">&nbsp;</div>
			</div>
		</div>
		<?php

	}elseif(get_param('dir')){

		$image_list_arr = readDirectory(get_param('dir'));

		$image_list_arr = multi_array_sort($image_list_arr, SORT_BY, SORT_ORDER);

		if($image_list_arr){

	        foreach($image_list_arr as $image_key => $image){

				?>
	        	<div class="thumb_wrapper tile">
	        		<div class="container">
	            		<div class="shadow">&nbsp;</div>
						<div class="thumb"><a href="?dir=<?php echo urlencode($image['dir']); ?>&pic=<?php echo urlencode($image['title']);?>"><img src="<?php echo $image['thumb']; ?>" border="0"/></a></div>
					</div>
					<div class="title"><?php echo $image['title']; ?></div>
				</div>
				<?

	        }

		}else{
			echo ' Sorry. No photos found in this directory.';
		}

	}else{
		home_page();
	}

}

function html_LastAddition(){

	?>
	<div class="latest">
		<p>Latest Photos Added</p>
		<?php
		$image_list_arr = readLastAddition();

		if($image_list_arr){

	        foreach($image_list_arr as $image_key => $image){

				?>
	        	<div class="thumb_wrapper tile">
	        		<div class="container">
	            		<div class="shadow">&nbsp;</div>
						<div class="thumb"><a href="?dir=<?php echo urlencode($image['dir']); ?>&pic=<?php echo urlencode($image['title']);?>"><img src="data/thumbdata_<?php echo base64_encode($image['dir'] . ']|[' . $image['filename'] . ']|[' . filemtime('gallery/'.$image['dir'].'/'.$image['filename'])); ?>" border="0"/></a></div>
					</div>
					<div class="title"><?php echo $image['title']; ?></div>
				</div>
				<?
	        }

		}else{
			echo ' Sorry. No photos submitted yet.';
		}

		?>
	</div>
	<?php

}

function readDirectory($current_dir = ''){

	$image_list_arr = array();

    $opendir = 'gallery/'.$current_dir;
    if ($handle = opendir($opendir)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != '.' AND $file != '..' AND (substr(strtolower($file),-3) == 'jpg' || substr(strtolower($file),-3) == 'gif' || substr(strtolower($file),-3) == 'png')) {

            	$file_original_name = $file;

				if(!file_exists('data/thumbdata_'.base64_encode($current_dir.']|['.$file.']|['.filemtime('gallery/'.$current_dir.'/'.$file))) AND (substr(strtolower($file), -3) == 'jpg' || substr(strtolower($file), -3) == 'gif')){
					$tis = new ThumbnailImage();
					$tis->src_file  = $opendir.'/'.$file;
					$tis->dest_type = THUMB_JPEG;
					$tis->dest_file = 'data/thumbdata_'.base64_encode($current_dir.']|['.$file . ']|[' . filemtime('gallery/'.$current_dir.'/'.$file));
					$tis->max_width = 120;
					$tis->max_height = 4000;
					$tis->Output();
				}

				$file_title = substr($file,0,-4);
				list($file_width, $file_height, $type, $attr) = getimagesize('gallery/'.$current_dir.'/'.$file);

				if(is_file('data/thumbdata_'.base64_encode($current_dir . ']|[' . $file . ']|[' . filemtime('gallery/'.$current_dir.'/'.$file)))){
					$the_thumbnail = 'data/thumbdata_'.base64_encode($current_dir . ']|[' . $file . ']|[' . filemtime('gallery/'.$current_dir.'/'.$file));
				}else{
					$the_thumbnail = 'img_holder.jpg';
				}

				if(STRIP_SORT_NUMBER AND (is_numeric(substr($file_title,0,1))) AND (substr($file_title,1,1) == '_')){
					$file_title_return = substr($file_title,2);
				}else{
					$file_title_return = $file_title;
				}

            	$image_list_arr[] = array('dir' => $current_dir, 'filename' => $file, 'title' => $file_title_return, 'thumb' => $the_thumbnail, 'width'=> $file_width, 'height' => $file_height, 'filesize' => filesize('gallery/'.$current_dir.'/'.$file), 'modified' => filemtime('gallery/'.$current_dir.'/'.$file));

            }
        }
        closedir($handle);

		return $image_list_arr;

    }else{

    	return false;

    }

}

function readLastAddition(){

	$image_list_arr = array();

    $opendir = 'data';
    if ($handle = opendir($opendir)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != '.' AND $file != '..' AND (substr($file,0,10) == 'thumbdata_')) {
            	$file_data_arr = explode(']|[', base64_decode(str_replace('thumbdata_','',$file)));
            	$image_list_arr[] = array('dir' => $file_data_arr[0], 'filename' => $file_data_arr[1], 'title' => substr($file_data_arr[1],0,-4), 'last_modify' => $file_data_arr[2]);
            }
        }
        closedir($handle);



        $image_list_arr = multi_array_sort($image_list_arr, 'last_modify');

        $i = 1;
        $maximum_img_at_homepage = 4;
		$image_at_homepage = array();
        if($image_list_arr){

			foreach($image_list_arr as $image_list_key => $image_list_value){

				$image_at_homepage[$image_list_key] = $image_list_value;

	        	if($i == $maximum_img_at_homepage){ break; }
	        	$i++;
	        }

        }


        return $image_at_homepage;



    }else{

    	return false;

    }



}

function errorMsg($error_text){

	echo '<b><font color="red">ERROR :</font> '.$error_text.'</b>';
	exit();

}

function requirement_checker(){

	if(!is_writable('data/')){ errorMsg('Oh no! You have make a boo boo...! Please make sure the "data" directory is writable. Click <a href="http://ifoto.ireans.com/faq" target="_blank">here</a> for more info.'); }

	if(function_exists("gd_info")){ $info=gd_info();
		if(!strstr($info['GD Version'],"2.")){errorMsg('Please make sure you PHP compiled with GD2. Click <a href="http://ifoto.ireans.com/faq" target="_blank">here</a> for more information.');}
 	}else{
 		errorMsg('Please make sure you PHP compiled with GD2. Click <a href="http://ifoto.ireans.com/faq" target="_blank">here</a> for more information.');
 	}

}

function get_param($get_param){

	if(@$return_filtered = $_GET[$get_param]){

		$return_filtered = str_replace('<','',$return_filtered);
		$return_filtered = str_replace('>','',$return_filtered);
		$return_filtered = str_replace('"','',$return_filtered);
		$return_filtered = str_replace("'",'',$return_filtered);
		$return_filtered = str_replace(";",' ',$return_filtered);
		$return_filtered = str_replace(":",' ',$return_filtered);
		$return_filtered = stripslashes($return_filtered);

		return $return_filtered;

	}else{
		return false;
	}
}

function multi_array_sort($array='', $key='', $order='asc')
{
	if($array AND $key){

		foreach ($array as $i => $k) {
			$sort_values[$i] = $array[$i][$key];
		}
		if($order == 'desc'){
			rsort ($sort_values);
		}else{
			asort ($sort_values);
		}
		reset ($sort_values);
		while (list ($arr_key, $arr_val) = each ($sort_values)) {
			$sorted_arr[] = $array[$arr_key];
		}
		return $sorted_arr;

	}else{
		return false;
	}

}

function multi_array_search($search_value, $the_array)
{
   if (is_array($the_array))
   {
       foreach ($the_array as $key => $value)
       {
           $result = multi_array_search($search_value, $value);
           if (is_array($result))
           {
               $return = $result;
               array_unshift($return, $key);
               return $return;
           }
           elseif ($result == true)
           {
               $return[] = $key;
               return $return;
           }
       }
       return false;
   }
   else
   {
       if ($search_value == $the_array)
       {
           return true;
       }
       else return false;
   }
}

/* for debug purpose */
function pre($sting_to_pre = ''){

    if($sting_to_pre){
        echo '<pre>';
        print_r($sting_to_pre);
        echo '</pre>';
    }
}

?>