All pastes #260255 Raw Edit

Miscellany

public text v1 · immutable
#260255 ·published 2006-11-28 04:38 UTC
rendered paste body
<?php		
// Display Footer on Admin Pages.
//########################################################################

function anigal_admin_footer() {
	//print admin footer in dashboard
	
	$anigal_curr_vers = "0.30"; // current Version of ANIga gallery ?>
	
	<div class="wrap" align="center">powered by <a href="http://www.animalbeach.net/aniga/"><strong>ANIga gallery</strong></a> -- your version is <strong><?php echo $anigal_curr_vers; ?></strong> -- the latest version is <strong><?php echo anigal_get_latest_vers(); ?></strong> -- &copy; <a href="mailto:web@animalbeach.net">Michael Naab</a> 2006
	</div>
	<?php
}

function anigal_get_latest_vers() {
	// retrieve latest version of ANIga
	
	include_once(ABSPATH . WPINC . '/class-snoopy.php');
	$no_version = "(could not retrieve latest version)";
	if (class_exists(snoopy)) {
		$client = new Snoopy();
		$client->_fp_timeout = 4;
		if (@$client->fetch('http://www.animalbeach.net/public/ANIga/version.txt') === false) {
			return $no_version;
		}
		$remote = $client->results;
		if (!$remote || strlen($remote) > 50 ) {
			return $no_version;
		} 
		return $remote;
	}
	else return $no_version;
}


// File and Zip functions
//########################################################################

class anigal_file {

	
	var $image_quality;
	var $image_type;
	var $fname;
	var $msg_out;

	function handle_file($file, $path, $filename, $image_type, $size, $rename = false, $alt_img = false, $msg_out = true, $skip = false ) {
		
		global $aniga_lang;
		$this->image_quality = get_option('anigal_resize_qual');
		// resulting image type (1 = GIF, 2 = JPG, 3 = PNG)
		// enter code of the image type if you want override it
		// or set it to -1 to determine automatically
		$this->image_type = $image_type;
		$this->fname = $filename;
		$this->msg_out = $msg_out;
		
		switch ($size):
		case "thumb":
			$max_x	= get_option('anigal_thumb_size');
			$max_y = get_option('anigal_thumb_size');
			break;
		case "normal":
			$max_x	= get_option('anigal_norm_size');
			$max_y = get_option('anigal_norm_size');
			break;
		case "catalb":
			$max_x	= get_option('anigal_caimg_size');
			$max_y = get_option('anigal_caimg_size');
			break;
		default:
			$max_x	= get_option('anigal_thumb_size');
			$max_y = get_option('anigal_thumb_size');
		endswitch;
		
		if ($alt_img && $file == '') $file = ABSPATH . 'wp-content/plugins/ANIga/noimage.jpg';
		elseif (!$alt_img && $file == '') {
			if ($this->msg_out) return "<p>" . $aniga_lang['misc_no_read'] . "</p>";
			else return;
		}
		
		if ($rename && is_file($path.$this->fname)) {
			for ($i = 1; is_file($path.$this->fname); $i++) {
				$this->fname = $this->rename_file($this->fname, $i);
			}
		}
		
		// generate
		if (get_option('anigal_resize') == 'yes' && $_POST['noresize'] != 'yes')
			return $this->GenerateThumbFile($file, $path, $max_x, $max_y);
		elseif ($skip)
			return;
		else	
			return $this->upload_file($file, $path, $this->fname, false);	 
	}
	
	function upload_file($file, $path, $filename, $rename = false) {
		// move uploaded to directory
	
		global $aniga_lang;
		if ($rename && is_file($path.$filename)) {
			for ($i = 1; is_file($path.$filename); $i++) {
				$filename = $this->rename_file($filename, $i);
			}
		}
		$this->fname = $filename;
		
		if(move_uploaded_file($file, $path.$this->fname)) {
			chmod($path.$this->fname, 0644);
			if ($this->msg_out) return "<p>" . $aniga_lang['picture'] . " $this->fname " . $aniga_lang['misc_upld'] . "</p>";
			else return;
		}
		else {
			if ($this->msg_out) return "<p>" . $aniga_lang['picture'] . " $this->fname " . $aniga_lang['misc_no_upld'] . "</p>";
			else return;
		}
	}
	
	function rename_file($filename, $nr) {
		// rename files if necessary
		
		$pos = strrpos($filename, ".");
		$name = substr($filename, 0, $pos);
		$ext = substr($filename, $pos, strlen($filename)-$pos);
		if(ereg("[0-9]", substr($name, -1) )) 
			$string = substr($name, 0, strlen($name)-1) . "$nr" . "$ext";
		else
			$string = "$name" . "_$nr" . "$ext";
		return $string;
	}
	
	function GenerateThumbFile($file, $path, $max_x, $max_y) {
		// generate thumb from image and save it
		
		global $aniga_lang;
		$to_file = $path.$this->fname;
		$msg = '';
		// get source image size (width/height/type)
		// orig_img_type 1 = GIF, 2 = JPG, 3 = PNG
		list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($file);
		
		// should we override thumb image type?
		$image_type = ($this->image_type != -1 ? $this->image_type : $orig_img_type);
		
		// check for allowed image types
		if ($orig_img_type < 1 or $orig_img_type > 3) {		
			$trans = array("%type%" => $orig_img_type, "%file%" => $this->fname);		
			$msg .= "<p>" . strtr($aniga_lang['misc_imgtyp'], $trans) . "</p>";
			if ($this->msg_out) return $msg;
			else return;
		}
		
		if ($orig_x > $max_x or $orig_y > $max_y) {
		
			// resize
			$per_x = $orig_x / $max_x;
			$per_y = $orig_y / $max_y;
			if ($per_y > $per_x) {
				$max_x = $orig_x / $per_y;
			}
			else {
				$max_y = $orig_y / $per_x;
			}
		
		}
		else {
			// keep original sizes, i.e. just copy
			if (@copy($file, $to_file))	
				$msg .= "<p>" . str_replace("%file%", "'". $this->fname . "'", $aniga_lang['misc_no_res']) . "</p>";
			else
				$msg .= "<p>'$this->fname' " . $aniga_lang['misc_no_upld'] . "</p>";
			if ($this->msg_out) return $msg;
			else return;
		}
		
		if ($image_type == 1) {
			// should use this function for gifs (gifs are palette images)
			$ni = imagecreate($max_x, $max_y);
		}
		else {
			// Create a new true color image
			$ni = ImageCreateTrueColor($max_x,$max_y);
		}
		
		// Fill image with white background (255,255,255)
		$white = imagecolorallocate($ni, 255, 255, 255);
		imagefilledrectangle( $ni, 0, 0, $max_x, $max_y, $white);
		// Create a new image from source file
		$im = $this->ImageCreateFromType($orig_img_type,$file); 
		// Copy the palette from one image to another
		imagepalettecopy($ni,$im);
		// Copy and resize part of an image with resampling
		imagecopyresampled(
		$ni, $im,             // destination, source
		0, 0, 0, 0,           // dstX, dstY, srcX, srcY
		$max_x, $max_y,       // dstW, dstH
		$orig_x, $orig_y);    // srcW, srcH
		
		// save thumb file
		$this->SaveImage($image_type, $ni, $to_file, $this->image_quality);
		
		$msg .= "<p>" . $aniga_lang['picture'] . " $this->fname " . $aniga_lang['misc_added'] . "</p>";
		if ($this->msg_out) return $msg;
		else return;
	}

	function SaveImage($type, $im, $filename, $quality) {
	
		$res = null;
		
		// ImageGIF is not included into some GD2 releases, so it might not work
		// output png if gifs are not supported
		if(!function_exists('imagegif')) $type = 3;
		
		switch ($type) {
		case 1:
			$res = ImageGIF($im,$filename);
			break;
		case 2:
			$res = ImageJPEG($im,$filename,$quality);
			break;
		case 3:
			$res = ImagePNG($im,$filename);
			break;
		}
		
		return $res;	
	}

	function ImageCreateFromType($type,$filename) {
		$im = null;
		switch ($type) {
		case 1:
			$im = ImageCreateFromGif($filename);
			break;
		case 2:
			$im = ImageCreateFromJpeg($filename);
			break;
		case 3:
			$im = ImageCreateFromPNG($filename);
			break;
		}
		return $im;
	}
}


function okfiletype($filename, $allowed) {
	// check if filetype is ok 
	
	for($i = 0; $i < count($allowed); $i++) {
		if(strtolower($allowed[$i]) == strtolower(substr($filename, -3))  || strtolower($allowed[$i]) == strtolower(substr($filename, -4)))
			return true;	
	}
	return false;
}

function anigal_calc_gallery() {
	// calculate the albums and pictures and update the db
	
	global $wpdb;
	$calc_alb_req = "SELECT * FROM $wpdb->anigal_alb";
	$calc_alb_ret = $wpdb->get_results($calc_alb_req);
	if ($calc_alb_ret){
		foreach ($calc_alb_ret as $calc_alb) {
		$count = 0;
		$count_pic_req = "SELECT * FROM $wpdb->anigal_pic WHERE alb_id = $calc_alb->alb_id";
		$count_pic_ret = $wpdb->get_results($count_pic_req);
		$count = $wpdb->num_rows;
		$update_alb = mysql_query("UPDATE $wpdb->anigal_alb SET count_pics = '$count' WHERE alb_id = $calc_alb->alb_id");
		}
	}
	$calc_cat_req = "SELECT * FROM $wpdb->anigal_cat";
	$calc_cat_ret = $wpdb->get_results($calc_cat_req);
	if ($calc_cat_ret){
		foreach ($calc_cat_ret as $calc_cat) {
		$count = 0;
		$count_albs = 0;
		$count_alb_req = "SELECT * FROM $wpdb->anigal_alb WHERE cat_wp_id = $calc_cat->wp_id";
		$count_alb_ret = $wpdb->get_results($count_alb_req);
		if ($count_alb_ret) {
			foreach ($count_alb_ret as $count_alb) {
			$count = $count + $count_alb->count_pics;
			$count_albs++;
			}
		}
		$update_cat = mysql_query("UPDATE $wpdb->anigal_cat SET count_pics = '$count', count_albs = '$count_albs' WHERE wp_id = $calc_cat->wp_id");
		}
	}
}

function anigal_zip_upload($dir, $file_org, $file_tmp, $db_path) {
	// upload and unzip archives with php zip library
	
	global $aniga_lang;	
	$file = $dir."/".$file_org;
	$msgf = "";
	if(move_uploaded_file($file_tmp, $file)) {	
		$zip = zip_open($file);
		$i = 0;
		if ($zip) {
			while ($zip_entry = zip_read($zip)) {
				if (zip_entry_open($zip, $zip_entry, "r")) {
					$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
					$fp = fopen($dir."/".zip_entry_name($zip_entry),"w");
					fwrite($fp,$buf);
					//$msgf .= zip_entry_name($zip_entry)."<br>";
					zip_entry_close($zip_entry);
					$i++;
				}
			}
			zip_close($zip);
			
			$msgf .= "<p>" . str_replace("%file%", "'". basename($file_org). "'", $aniga_lang['misc_the_file']) . " '$db_path'<p />";
		}
		else $msgf .= "<p>" . $aniga_lang['misc_no_read_z'] . "</p>";
		unlink($file);
	}
	else $msgf .= "<p>" . $aniga_lang['misc_no_move_z'] . " '$db_path'</p>";
	return $msgf;
}


//GALLERY TEMPLATE CLASSES
//########################################################################

class anigal_index { // print categories

	var $path;
	var $alg = 'gallery_alignleft';
	
	function heading() { ?>
	
		<h2><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
			
		<?php
	}
			
	function loop_html($wp_id, $cat_id, $name, $description) { 
	
		global $aniga, $aniga_lang; ?>
	
		<div class="gallery_clear gallery_list">
	
		<div class="<?php echo $this->alg; ?>">
			<a href="<?php echo get_permalink($wp_id); ?>">
			<img src="<?php echo $this->path.$cat_id; ?>.jpg" border="0" width="<?php echo get_option('anigal_caimg_size'); ?>" class="gallery_pic_border" alt="<?php echo $description; ?>" />
			</a>
		</div>
		
		<div class="gallery_alignleft gallery_list_p">
		
			<a href="<?php echo get_permalink($wp_id); ?>"><?php echo $name; ?></a>
			<br />
			<span class="gallery_meta"><?php echo $description; ?><br />
			
				<small><?php echo $aniga->c_cat_alb($wp_id); ?> <?php echo $aniga_lang['galleries']; ?> | <?php echo $aniga->c_cat_pic($wp_id); ?> <?php echo $aniga_lang['pictures']; ?> | <?php echo $aniga->c_cat_view($wp_id); ?> <?php echo $aniga_lang['views']; ?> | <?php echo $aniga->c_cat_com($wp_id); ?> <?php echo $aniga_lang['comments']; ?></small>
				
			</span>
		</div>
	
		</div>
		
		<?php	
	}
	
	function meta() {
	
		global $aniga, $aniga_lang; ?>
	
			<div class="gallery_pad gallery_clear">
			<div class="gallery_meta gallery_alt" align="center">
				<small>
				<?php echo $aniga->c_all_alb(); ?> <?php echo $aniga_lang['galleries']; ?> | <?php echo $aniga->c_all_pic(); ?> <?php echo $aniga_lang['pictures']; ?> | <?php echo $aniga->c_all_com(); ?> <?php echo $aniga_lang['comments']; ?> | <?php echo $aniga->c_all_view(); ?> <?php echo $aniga_lang['views']; ?>
				</small>
			</div>
			</div>
		
		<?php
	}
	
	function loop() {
		
		global $aniga, $aniga_lang;
		$aniga->user = $aniga->user();
		$this->heading(); // print heading
		$this->path = get_option('anigal_dirpath').'00-gfx/cat_';
		$get_cat = $aniga->getcategories();
		if ($get_cat) {
			foreach ($get_cat as $cat) {
			
			$this->loop_html($cat->wp_id, $cat->cat_id, $cat->description, $cat->name);

			if ('gallery_alignleft' == $this->alg) $this->alg = 'gallery_alignright';
			else $this->alg = 'gallery_alignleft';
			}
			$this->meta(); // print meta information
		}
		else echo '<div class="gallery_error">' . $aniga_lang['error_index'] . '</div>';
	}
	
}


class anigal_category { // print albums for category

	
	var $path;
	var $alg = 'gallery_alignleft';
	
	function heading() {
		
		global $aniga;
		$aniga->nav_cat(); //NAVIGATION ?>
		
		<h2><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
	
		<?php	
	}
	
	function loop_html($wp_id, $alb_id, $description, $name) {
		
		global $aniga, $aniga_lang; ?>

		<div class="gallery_clear gallery_list">
	
		<div class="<?php echo $this->alg; ?>">
			<a href="<?php echo get_permalink($wp_id); ?>">
			<img src="<?php echo $this->path.$alb_id; ?>.jpg" border="0" width="<?php echo get_option('anigal_caimg_size'); ?>" class="gallery_pic_border" alt="<?php echo $description; ?>" /></a>
		</div>
	
		<div class="gallery_alignleft gallery_list_p">
	
			<a href="<?php echo get_permalink($wp_id); ?>"><?php echo $description; ?></a>
			<br />
			<span class="gallery_meta"><?php echo $name; ?><br />
				<small><?php echo $aniga->c_alb_pic($alb_id); ?> <?php echo $aniga_lang['pictures']; ?> | <?php echo $aniga->c_alb_com($alb_id); ?> <?php echo $aniga_lang['comments']; ?> | <?php echo $aniga->c_alb_view($alb_id); ?> <?php echo $aniga_lang['views']; ?></small>
			</span>
		</div>
	
		</div>
		
	<?php
	}
	
	function meta($wp_id) {
	
		global $aniga, $aniga_lang; ?>
		
		<div class="gallery_pad gallery_clear">
		<div class="gallery_meta gallery_alt" align="center">		
			<small>
			<?php echo $aniga->c_cat_alb($wp_id); ?> <?php echo $aniga_lang['galleries']; ?> | <?php echo $aniga->c_cat_pic($wp_id); ?> <?php echo $aniga_lang['pictures']; ?> | <?php echo $aniga->c_cat_com($wp_id); ?> <?php echo $aniga_lang['comments']; ?> | <?php echo $aniga->c_cat_view($wp_id); ?> <?php echo $aniga_lang['views']; ?>
			</small>
		</div>	
		</div>
	
	<?php
	}
	
	function loop($id) {
			
		global $aniga, $aniga_lang;
		$aniga->user = $aniga->user();
		$this->heading(); // print heading
		$this->path = get_option('anigal_dirpath').'00-gfx/album_';
		
		switch ($aniga->checkcat($id)):
		case 1:
			$get_alb = $aniga->getalbums($id);
			if ($get_alb) {
				foreach ($get_alb as $album) {
				
				$this->loop_html($album->wp_id, $album->alb_id, $album->description, $album->name);		
	
				if ('gallery_alignleft' == $this->alg) $this->alg = 'gallery_alignright';
				else $this->alg = 'gallery_alignleft';
				}
				$this->meta($id); // print meta information
			}
			else echo '<div class="gallery_error">' . $aniga_lang['error_cat_no'] . '</div>';
			break;
			
		case 2:
			echo '<div class="gallery_error">' . $aniga_lang['error_cat_log'] . '</div>';
			break;
			
		case 3:
			echo '<div class="gallery_error">' . $aniga_lang['error_cat_na'] . '</div>';
			break;
		endswitch;
	}
		
}


class anigal_picture { // print thumbnails or picture

	
	var $pid;
	var $id;
	var $alb_id;
	var $cat_wp_id;
	var $album = array();
	var $page = array();
	var $pic = array();
	var $pic_link;
	
	function slideshow_js() {
		include ("slideshow.php");
	}
	
	function slideshow() {
		
		global $aniga, $aniga_lang;
		$album = $this->album;
		$first = $aniga->getfirstpic($album->alb_id);
		
		$this->navthumb(); ?>
		
		<div class="gallery_aligncenter gallery_pad">
			<table cellpadding="0" cellspacing="0" border="0" class="gallery_slds_table" align="center"><tr><td valign="middle" align="center" class="gallery_slds_td"><a href="javascript:SLIDES.hotlink()" title="<?php echo $aniga_lang['sshow_click']; ?>"><img name="SLIDESIMG" src="<?php echo $first->path."normal_".$first->filename; ?>" <?php if (get_option('anigal_slds_filter') == 'yes'): ?>class="gallery_slds_filter"<?php endif; ?> alt="Slideshow Picture" /></a></td></tr></table>
			<div align="center"><small><a href="javascript:SLIDES.previous()">&laquo; <?php echo $aniga_lang['previous']; ?></a> | <a href="javascript:SLIDES.pause()"><?php echo $aniga_lang['pause']; ?></a> | <a href="javascript:SLIDES.play()"><?php echo $aniga_lang['play']; ?></a> | <a href="javascript:SLIDES.shuffle()"><?php echo $aniga_lang['random']; ?></a> | <a href="javascript:SLIDES.next()"><?php echo $aniga_lang['next']; ?> &raquo;</a></small></div>
		</div>
		<script type="text/javascript">
		<!--
		if (document.images)
		{
			SLIDES.set_image(document.images.SLIDESIMG);
			SLIDES.set_textid("SLIDESTEXT");
			SLIDES.update();
		<?php if ($_GET['slide'] != '') echo "SLIDES.goto_slide(".$_GET['slide'].");\r\n"; ?>
			SLIDES.play(); //optional
		<?php if (get_option('anigal_slds_filter') == 'yes'): ?>
			// Create a function to ramp up the image opacity in Mozilla
			var fadein_opacity = 0.04;
			var fadein_img = SLIDES.image;
			function fadein(opacity) {
			  if (typeof opacity != 'undefined') { fadein_opacity = opacity; }
			  if (fadein_opacity < 0.99 && fadein_img && fadein_img.style &&
				  typeof fadein_img.style.MozOpacity != 'undefined') {
			
				fadein_opacity += .05;
				fadein_img.style.MozOpacity = fadein_opacity;
				setTimeout("fadein()", 50);
			  }
			}
			
			// Tell the slideshow to call our function whenever the slide is changed
			SLIDES.post_update_hook = function() { fadein(0.04); }
		<?php endif; ?>
		}
		//-->
		</script>
		<div class="gallery_pad">
			<div class="gallery_alt" align="center">
				<div class="gallery_line_h"><a href="javascript:SLIDES.hotlink()"><?php echo $aniga_lang['sshow_view']; ?></a></div>
				<div id="SLIDESTEXT">&nbsp;</div>
				<noscript><?php echo $aniga_lang['sshow_noscript']; ?></noscript>
			</div>
		</div>		
		
		<?php
	}
	
	function thumbnails() {
	
		global $aniga, $aniga_lang;
		
		$this->navthumb();	
		$this->calcpage();
		
		if ($this->page['page_max'] >= $this->page['page']) { // show thumbnails if pages are ok ?>
					
			<div class="gallery_pad">
			
			<?php
			$this->thumbmenu(); ?>
			
			<table border="0" align="center" cellspacing="0" cellpadding="0">
			
			<?php
			$this->thumbpics(); ?>

			</table>
			
			<?php
			$this->thumbpage(); ?>

			</div>
			
			<?php
			$this->thumbmeta();
			
		}
		else echo '<div class="gallery_error">' . $aniga_lang['error_pic_t'] . '</div>';
	}
	
	function navthumb() {
		
		global $aniga, $aniga_lang;
		$album = $this->album;
		$aniga->nav_thumb($album->cat_wp_id); // NAVIGATION ?>

		<h2 id="picture_nav"><a href="<?php echo get_permalink(); ?>" rel="bookmark" title="<?php echo $aniga_lang['thumbn_back_f']; ?> <?php the_title(); ?>"><?php the_title(); ?></a></h2>
		
		<?php	
	}

	function calcpage() {
		
		global $aniga;
		$album = $this->album;
	
		$this->page['page'] = $_GET['page'];
		if (empty($this->page['page']) || $this->page['page'] < 1) $this->page['page'] = 1;
		
		$this->page['max_pic'] = $aniga->c_alb_pic($album->alb_id);
		
		if (empty($_COOKIE["gal_page" . COOKIEHASH])) $this->page['pt'] = 5 * get_option('anigal_colums'); // first value from array!
		else $this->page['pt'] = $_COOKIE["gal_page" . COOKIEHASH];  
		
		$this->page['page_start'] = ($this->page['page'] * $this->page['pt']) - $this->page['pt'];
		$this->page['page_end'] = $this->page['pt'];
		
		$this->page['prev_page'] = $this->page['page'] - 1;
		$this->page['next_page'] = $this->page['page'] + 1;
		
		if (is_int($this->page['max_pic']/$this->page['pt'])) $this->page['page_max'] = $this->page['max_pic']/$this->page['pt'];
		else $this->page['page_max'] = floor($this->page['max_pic']/$this->page['pt']) + 1;
	}
	
	function thumbmenu() {
		
		global $aniga, $aniga_lang; ?>
	
		<div align="right"><?php echo $aniga_lang['show']; ?> <?php $this->thumbselect($this->id); ?> <?php echo $aniga_lang['per_page']; ?> | <a href="<?php echo anigal_get_permalink($this->id, $this->id, 'slideshow=start&amp;ssid'); ?>"><?php echo $aniga_lang['slideshow']; ?></a>
		</div>
		
	<?php
	}
	
	function thumbcount() {
		// calc the thumb counts
		
		global $aniga_lang;
		$cols = get_option('anigal_colums');
		$thumbs = 5 * $cols;
		$thumbs_count = array($thumbs => $thumbs . ' ' . $aniga_lang['thumbnails'], 2*$thumbs => 2*$thumbs . ' ' . $aniga_lang['thumbnails'], 5*$thumbs => 5*$thumbs . ' ' . $aniga_lang['thumbnails'], 10*$thumbs => 10*$thumbs . ' ' . $aniga_lang['thumbnails'], 999 => $aniga_lang['thumbnails_all']);
		
		return $thumbs_count;
	}
	
	function thumbselect($id) {
		// print the thumbnail-number select-box ?>
		
		<select name="gallery_pages" onchange="location.href='<?php echo anigal_get_permalink($id, '', $do = 'set_page', false); ?>' + this.options[this.selectedIndex].value;" class="gallery_select">

		<?php 
		foreach ($this->thumbcount() as $k => $v) {
			echo '<option value="' . $k . '"';
			if ($_COOKIE["gal_page" . COOKIEHASH] == $k) { echo ' selected="selected"'; }
			echo '>' . $v . '</option>';
		} ?>
		
		</select>
		
	<?php
	}
	
	function thumbpages($page, $prev_page, $next_page, $page_max, $id) {
		// print the thumbnail page navigation
		
		global $aniga_lang;
		if ($page_max != 1) {
			if ($page != 1) { ?>
			
				<span class="gallery_high">&laquo;</span><a href="<?php echo anigal_get_permalink($id, $prev_page, 'page'); ?>"><?php echo $aniga_lang['previous']; ?></a> 

			<?php	
			}		
			for ($j = 1; $j <= $page_max; $j++) {
				if ($j == $page) { 
					echo $j; 	
				}
				else { ?>  
				
					<a href="<?php echo anigal_get_permalink($id, $j, 'page'); ?>"><?php echo $j; ?></a>  

				<?php
				}
			}	
			if ($page != $page_max) { ?>
			
				<a href="<?php echo anigal_get_permalink($id, $next_page, 'page'); ?>"><?php echo $aniga_lang['next']; ?></a><span class="gallery_high">&raquo;</span>

			<?php	
			}
		}
	}
		
	function thumbpage() {
		
		global $aniga; ?>
		
		<div align="right">
					
			<?php
			$this->thumbpages($this->page['page'], $this->page['prev_page'], $this->page['next_page'], $this->page['page_max'], $this->id); ?>
			
		</div>
		
		<?php
	}
	
	function thumbpics() {
		
		global $aniga, $aniga_lang;
		$album = $this->album;	
		$col = 1;
		$col_count = get_option('anigal_colums'); 
		$thumbs = $aniga->getthumbs($album->alb_id, $this->page['page_start'], $this->page['page_end']);	
		foreach ($thumbs as $pic) { 
			
			if ($col == 1) : ?>
			
			<tr>
			
			<?php endif; ?>
					
			<td align="center" valign="middle" class="gallery_thumb_td"><a href="<?php echo anigal_get_permalink($this->id, $pic->pid, 'pid'); ?>"><img src="<?php echo $pic->path . 'thumb_' . $pic->filename; ?>" class="gallery_pic_border" alt ="<?php echo $pic->filename; ?>" /></a>
							
			<?php
			$com = $aniga->c_pic_com($pic->pid);
			switch ($com):
			case 0: ?>
			
				<small><br />&nbsp;</small>
				
				<?php
				break;
				
			case 1: ?>
			
				<small><br />1 <?php echo $aniga_lang['comment']; ?></small>
				
				<?php
				break;
			
			case ($com > 1): ?>
			
				<small><br /><?php echo $com; ?> <?php echo $aniga_lang['comments']; ?></small>
				
				<?php
				break;
			endswitch; ?>
			
			</td>
						
			<?php
			if ($col == $col_count) { ?>
			
			</tr> 
				
				<?php
				$col = 1;
			}						
			else $col++;	
		} // end foreach
	}
	
	function thumbmeta() {
		
		global $aniga, $aniga_lang; 
		$album = $this->album; ?>
	
		<div class="postmetadata alt" align="center">		
			<small><?php echo $this->page['max_pic']; ?> <? echo $aniga_lang['pictures']; ?> | <?php echo $aniga->c_alb_com($album->alb_id); ?> <? echo $aniga_lang['comments']; ?> | <?php echo $aniga->c_alb_view($album->alb_id); ?> <? echo $aniga_lang['views']; ?> | <? echo $aniga_lang['created']; ?> <?php the_time('d.m.Y') ?></small>
		</div>
		
	<?php				
	}

	function navpic($cur_page) {
		
		global $aniga, $aniga_lang;
		$album = $this->album;
		$aniga->nav_thumb($album->cat_wp_id); // NAVIGATION ?>

		<h2 id="picture_nav"><a href="<?php echo anigal_get_permalink($this->id, $cur_page, 'page', ''); ?>" rel="bookmark" title="<? echo $aniga_lang['thumbn_back_f']; ?> <?php the_title(); ?>"><?php the_title(); ?></a></h2>
			
		<?php	
	}
	
	function picture_link() {
	
		global $aniga_lang;
		$picture = $this->pic;
		
		$src = '<img name="SLIDESIMG" src="'.$picture->path . 'normal_' . $picture->filename . '" border="0" alt="' . $picture->filename . '" />';
		$after = '</a>';
		
		switch (get_option("anigal_img_mode")):
		case "link":
			$link = '<a href="' . $picture->path . $picture->filename .'" title="' . $picture->caption . ' - ' . $aniga_lang['full_size'] . ' (' . $picture->width . ' x ' . $picture->height .' px)" >';
			break;
		case "js": ?>
			<script type="text/javascript">
			function openpopup(popurl){
			var winpops=window.open(popurl,"","width=<?php echo $picture->width + 40; ?>,height=<?php echo $picture->height + 40; ?>,scrollbars,resizable")
			}
			</script>
			<?php
			$link = "<a href=\"javascript:openpopup('" . $picture->path . $picture->filename . "')\" >";
			break;
		case "lightbox":
			$link = '<a href="' . $picture->path . $picture->filename .'" rel="lightbox" >';
			break;
		case "none":
			$link = '';
			$after = '';
			break;
		endswitch;
		
		$this->pic_link = $link.$src.$after;
	}
	
	function picture_html_pic($current_pic, $hits, $cur_page) {
		
		global $aniga, $aniga_lang; 
		$picture = $this->pic;
		$this->picture_link(); ?>
	
		<div class="gallery_aligncenter gallery_pad">
		
		<table cellpadding="0" cellspacing="0" border="0" class="gallery_slds_table" align="center"><tr><td valign="middle" align="center" class="gallery_slds_td"><?php echo $this->pic_link; ?></td></tr></table>
		
		<div align="center"><small>
			
			<a href="<?php echo anigal_get_permalink($this->id, $cur_page, 'page', ''); ?>" title="<? echo $aniga_lang['thumbn_back_f']; ?> <?php the_title(); ?>">
			<? echo $aniga_lang['thumbn_back']; ?>
			</a> | 
			<a href="<?php echo anigal_get_permalink($this->id, $current_pic - 1, 'slideshow=start&amp;ssid='.$this->id.'&amp;slide'); ?>"><? echo $aniga_lang['slideshow']; ?></a> | 
			<?php echo $picture->filename . ' | ' . $picture->width . 'x' . $picture->height . 'px | ' . $hits; ?> <? echo $aniga_lang['views']; ?>
			
		</small></div>
		
		</div>
		
	<?php
	}
	
	function picture_html_caption() {
	
		$picture = $this->pic;
		if ($picture->caption): ?>
			
			<div class="gallery_meta gallery_alt gallery_aligncenter">
			<?php echo convert_smilies($picture->caption); ?>
			</div>
			
		<?php
		endif;
	}
	
	function picture_html_nav($prev_id, $prev_path, $prev_filename, $next_id, $next_path, $next_filename) { 
	
		global $aniga_lang; ?>
	
		<div class="postmetadata alt">
			<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0">
			<tr>
			<td width="50%" align="left" valign="top">
			
			<?php if(isset($prev_id)){ ?>
			
				<a href="<?php echo anigal_get_permalink($this->id, $prev_id, 'pid') ?>" title="<? echo $aniga_lang['previous']; ?>">
				<img src="<?php echo $prev_path . 'thumb_' . $prev_filename; ?>" height="75px"  class="gallery_pic_border" align="middle" alt="<? echo $aniga_lang['previous']; ?>" />
				</a>
						
			<?php } else echo '&nbsp;'; ?>
			
			</td>
			<td width="50%" align="right" valign="top">
			
			<?php if(isset($next_id)){ ?>
			
				<a href="<?php echo anigal_get_permalink($this->id, $next_id, 'pid'); ?>" title="<? echo $aniga_lang['next']; ?>">
				<img src="<?php echo $next_path . 'thumb_' . $next_filename; ?>" height="75px"  class="gallery_pic_border" align="middle" alt="<? echo $aniga_lang['next']; ?>" />
				</a>
						
			<?php } else echo '&nbsp;'; ?>
			
			</td>
			</tr></table>
		</div>
		
	<?php	
	}
	
	function picture() {
	
		global $aniga, $aniga_lang;
		$album = $this->album;
		$this->pic = $aniga->getpic($this->pid);
		$picture = $this->pic;
			
		if (empty($this->pid)) { //some checks, to verify that the picture is ok
			echo '<div class="gallery_error">'. $aniga_lang['error_pic_no'] . '</div>';
		}
		
		elseif ($picture->alb_id != $album->alb_id){
			echo '<div class="gallery_error">'. $aniga_lang['error_pic_nf'] . '</div>';
		}
		else { // picture is ok:
		
			switch ($aniga->checkpic($picture->level)): // check user level
			case 1:
		
			$hits = $aniga->updatehits($picture->hits, $picture->pid); // update picture hits

		// do the picture navigation 	
		// ##############################################################
			$gallery_nav = $aniga->getpics($picture->alb_id);
					
			$nav_i = 1;
			$nav_j = 1;
			unset($current_pic);
			unset($prev_id);
			unset($prev_path);
			unset($prev_filename);
			unset($next_id);
			unset($next_path);
			unset($next_filename);
					
			foreach ($gallery_nav as $gal_nav_array) {
				if ($gal_nav_array->pid  == $this->pid) $current_pic = $nav_i;
		
				$nav_i++;
			}
				
			foreach ($gallery_nav as $gal_nav_pic) {
				if($nav_j == ($current_pic - 1)){
					$prev_id = $gal_nav_pic->pid;
					$prev_path = $gal_nav_pic->path;
					$prev_filename = $gal_nav_pic->filename;
				}
				else if($nav_j == ($current_pic + 1)){
					$next_id = $gal_nav_pic->pid;
					$next_path = $gal_nav_pic->path;
					$next_filename = $gal_nav_pic->filename;
				}
				$nav_j++;
			}
			
			$cur_page = $aniga->getcurrentpage($current_pic);
		// ##############################################################
		
			$this->navpic($cur_page); 
			
			$this->picture_html_pic($current_pic, $hits, $cur_page);
						
			$this->picture_html_nav($prev_id, $prev_path, $prev_filename, $next_id, $next_path, $next_filename);

			$this->picture_html_caption();

			$this->comments_templ("/gallery-comments.php");

			break;
			
		case 2:
			$this->navthumb();
			echo '<div class="gallery_error">'. $aniga_lang['error_pic_log'] . '</div>';
			break;
		
		case 3:
			$this->navthumb();
			echo '<div class="gallery_error">'. $aniga_lang['error_pic_na'] . '</div>';
			break;
		endswitch;
		}
	}
	
	function comments_templ( $file = '/gallery-comments.php' ) {
		// copy from comments_template() to show only comments with the right PID
		
		global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
	
		if ( ! (is_single() || is_page() || $withcomments) )
			return;
			
		$req = get_settings('require_name_email');
			
		// make backwards compatible to prior versions
		if (function_exists('wp_get_current_commenter')) {
		
			$commenter = wp_get_current_commenter();
			extract($commenter);		
		}
		else {
		
			$comment_author = '';
			if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
				$comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
				$comment_author = stripslashes($comment_author);
				$comment_author = wp_specialchars($comment_author, true);
			}
			$comment_author_email = '';
			if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
				$comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
				$comment_author_email = stripslashes($comment_author_email);
				$comment_author_email = wp_specialchars($comment_author_email, true);		
			}
			$comment_author_url = '';
			if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
				$comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
				$comment_author_url = stripslashes($comment_author_url);
				$comment_author_url = wp_specialchars($comment_author_url, true);		
			}		
		}
	
		// TODO: Use API instead of SELECTs.
		if ( empty($comment_author) ) {
			$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' AND comment_pid_ID = '$this->pid' ORDER BY comment_date");
		} else {
			$author_db = $wpdb->escape($comment_author);
			$email_db  = $wpdb->escape($comment_author_email);
			$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_pid_ID = '$this->pid' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date");
		}
		
		define('COMMENTS_TEMPLATE', true);
		$include = apply_filters('comments_template', TEMPLATEPATH . $file );
		if ( file_exists( $include ) )
			require( $include );
		else
			require( ABSPATH . 'wp-content/themes/default/comments.php');
	}
		
	function show($id) {
	
		global $aniga, $aniga_lang;
		$aniga->user = $aniga->user();
		$this->pid = $_GET['pid'];
		$this->id = $id;
		$this->album = $aniga->getalbum($this->id);
		
		switch ($aniga->checkalb($this->id)):
		case 1:
			if ($_GET['slideshow'] == 'start' && $_GET['ssid'] == $this->id) { // show slideshow
				
				$this->slideshow();
				
			}
			elseif (empty($this->pid)) { // no pid => show thumbnails
			 
				$this->thumbnails();
				
			}
			else { // there is a pid => show the picture with navigation and comments. 

				$this->picture();
				
			}	
			break;
			
		case 2:
			$this->navthumb();
			echo '<div class="gallery_error">'. $aniga_lang['error_alb_log'] . '</div>';
			break;
		
		case 3:
			$this->navthumb();
			echo '<div class="gallery_error">'. $aniga_lang['error_alb_na'] . '</div>';
			break;
		endswitch;
	}

}


class anigal_db {


	var $user = '0';
	var $allowed_zip = array("zip");
	var $allowed_pic = array("jpg", "jpeg", "gif", "png");

	function user() {
		
		$user = anigal_get_userinfo();
		return $user;
	}
	
	function query($what) {
	
		global $wpdb;
		$level = $this->user;
		switch ($what):
		case "WHERE":
			return "WHERE level <= $level";
		case "AND":
			return "AND level <= $level";
		case "AND.pic":
			return "AND {$wpdb->anigal_pic}.level <= $level";
		case "AND.alb":
			return "AND {$wpdb->anigal_alb}.level <= $level";
		endswitch;
	}
	
	function checkcat($id) {
	
		global $wpdb;
		$cat = $wpdb->get_row("SELECT level FROM $wpdb->anigal_cat WHERE wp_id = $id");
		if ($cat->level <= $this->user) return 1;
		elseif ($this->user == '0') return 2;
		elseif ($this->user >> '0') return 3;
	}
	
	function checkalb($id) {
	
		global $wpdb;
		$alb = $wpdb->get_row("SELECT level, cat_wp_id from $wpdb->anigal_alb WHERE wp_id = $id");
		$cat = $this->checkcat($alb->cat_wp_id);
		if ($alb->level <= $this->user && $cat == 1) return 1;
		elseif ($this->user == '0') return 2;
		elseif ($this->user >> '0') return 3;
	}
	
	function checkpic($level) {
		
		if ($level <= $this->user) return 1;
		elseif ($this->user == '0') return 2;
		elseif ($this->user >> '0') return 3;	
	}
	
	function checkpath() {
	
		global $aniga_lang;
		$msg = '';
		if (substr( get_option('anigal_abspath'), -1) != '/') $msg .= "<p>" . $aniga_lang['misc_w_1'] . "</p>";
		else {
			if ( is_dir( get_option('anigal_abspath') ) ) {
				if ( !is_writable( get_option('anigal_abspath') ) ) $msg .= "<p>" . $aniga_lang['misc_w_2'] . "</p>";
				if ( !is_dir( get_option('anigal_abspath')."00-gfx" ) ) $msg .= "<p>" . $aniga_lang['misc_w_3'] . "</p>";
				elseif ( !is_writable( get_option('anigal_abspath')."00-gfx" ) ) $msg .= "<p>" . $aniga_lang['misc_w_4'] . "</p>";
				if ( !is_dir( get_option('anigal_abspath')."00-single" ) ) $msg .= "<p>" . $aniga_lang['misc_w_5'] . "</p>";
				elseif ( !is_writable( get_option('anigal_abspath')."00-single" ) ) $msg .= "<p>" . $aniga_lang['misc_w_6'] . "</p>";
			}
			else $msg .= "<p>" . $aniga_lang['misc_w_7'] . "</p>";
		}
		if (substr( get_option('anigal_dirpath'), -1) != '/') $msg .= "<p>" . $aniga_lang['misc_w_8'] . "</p>";
		return $msg;	
	}
	

//  #### RETURN DATA FUNCTIONS ###  --------------------------------------
		
	function getcategory($id) {
		// return specific categorie-information from db 
		
		global $wpdb;
		$category = $wpdb->get_row("SELECT * FROM $wpdb->anigal_cat WHERE wp_id=$id");
		return $category;
	}
	
	function getcategories() {
		// get all categories
		
		global $wpdb;
		$gal_cat_req = "SELECT * FROM $wpdb->anigal_cat ".$this->query("WHERE")." ORDER BY cat_id ASC";
		$gal_cat_ret = $wpdb->get_results($gal_cat_req);
		return $gal_cat_ret;
	}
	
	function getcategoryname($cat_id) {
		// return category name from db
		
		global $wpdb;
		$cat = $wpdb->get_row("SELECT description FROM $wpdb->anigal_cat WHERE wp_id=$cat_id");
		return $cat->description;
	}
	
	function getallalbums() {
		// return all albums from db
		
		global $wpdb;
		$gal_cat_req = "SELECT description, wp_id, alb_id, name, count_pics FROM $wpdb->anigal_alb ORDER BY alb_id DESC";
		$gal_cat_ret = $wpdb->get_results($gal_cat_req);
		return $gal_cat_ret;
	}

	function getalbums($id) {
		// get all albums for category
		
		global $wpdb;
		$gal_cat_req = "SELECT * FROM $wpdb->anigal_alb WHERE cat_wp_id = $id ".$this->query("AND")." ORDER BY alb_id DESC";
		$gal_cat_ret = $wpdb->get_results($gal_cat_req);
		return $gal_cat_ret;
	}
	
	function getalbum($id) {
		//return specific album-information from db
		
		global $wpdb;
		$album = $wpdb->get_row("SELECT * FROM $wpdb->anigal_alb WHERE wp_id = $id");
		return $album;
	}
	
	function getfirstpic($alb_id) {
		// return specific picture from db
		
		global $wpdb;
		$picture = $wpdb->get_row("SELECT * FROM $wpdb->anigal_pic WHERE alb_id=$alb_id ".$this->query("AND")." ORDER BY filename LIMIT 1");
		return $picture;
	}
	
	function getpic($pid) {
		 // return specific picture from db
		 
		global $wpdb;
		$picture = $wpdb->get_row("SELECT * FROM $wpdb->anigal_pic WHERE pid=$pid");
		return $picture;
	}
	
	function getthumbs($alb_id, $page_start, $page_end) {
		// return thumbnails from db
		
		global $wpdb;
		$gal_thumbs_req = "SELECT pid, path, filename FROM $wpdb->anigal_pic WHERE alb_id=$alb_id ".$this->query("AND")." ORDER BY filename LIMIT $page_start,$page_end";
		$gal_thumbs = $wpdb->get_results($gal_thumbs_req);
		return $gal_thumbs;
	}
	
	function getpics($alb_id) {
		// return pictures for an album from db
		
		global $wpdb;
		$gal_nav_req = "SELECT * FROM $wpdb->anigal_pic WHERE alb_id=$alb_id ".$this->query("AND")." ORDER BY filename";
		$gal_nav_ret = $wpdb->get_results($gal_nav_req);
		return $gal_nav_ret;
	}
	
	function getcurrentpage($current_pic) {
		// return current page acc. to cookie
		
		if (empty($_COOKIE["gal_page" . COOKIEHASH])) $pt = 5 * get_option('anigal_colums');
		else $pt = $_COOKIE["gal_page" . COOKIEHASH];  
		if (is_int($current_pic/$pt)) $cur_page = $current_pic/$pt;
		else $cur_page = floor($current_pic/$pt) + 1;
		return $cur_page;
	}
	
	function getupdatedpost($id) {
		// return updated post from db
		
		global $wpdb;
		$upd_post = $wpdb->get_row("SELECT post_title, post_content, comment_count, comment_status FROM $wpdb->posts WHERE ID = $id");
		return $upd_post;
	}
	
	function getpostparent($id) {
		// return parent posts for an ID from db
		
		global $wpdb;
		$check_cat_req = "SELECT * FROM $wpdb->posts WHERE post_parent = $id";
		$check_cat_ret = $wpdb->get_results($check_cat_req);
		$count = $wpdb->num_rows;
		return $count;
	}


//  #### COUNT FUNCTIONS ###  --------------------------------------------
	
	function c_all_alb() {
		// return total number of albums
		
		global $wpdb;
		$gal_count_req = "SELECT alb_id FROM $wpdb->anigal_alb ".$this->query("WHERE");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;	
	}
	
	function c_all_pic() {
		// return total number of pictures
		
		global $wpdb;
		$gal_count_req = "SELECT pid FROM $wpdb->anigal_pic ".$this->query("WHERE");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;	
	}
	
	function c_all_com() {
		// return total number of comments for pictures
		
		global $wpdb;
		$gal_count_req = "SELECT ".$wpdb->comments.".comment_ID FROM ".$wpdb->comments.", $wpdb->anigal_pic WHERE ".$wpdb->comments.".comment_pid_ID=".$wpdb->anigal_pic.".pid ".$this->query("AND.pic")." AND ".$wpdb->comments.".comment_approved='1'";
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;	
	}
	
	function c_all_view() {
		// return total number of picture views

		global $wpdb;
		$sum_view = 0;
		$gal_count_req = "SELECT hits FROM $wpdb->anigal_pic ".$this->query("WHERE");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		if (!empty($gal_count_ret)){
			foreach ($gal_count_ret as $gal_count) {
			$sum_view = $sum_view + $gal_count->hits;
			}
		}
		return $sum_view;
	}
	
	function c_cat_alb($wp_id) {
		// return Albums per Category
		
		global $wpdb;
		$gal_count_req = "SELECT alb_id FROM $wpdb->anigal_alb WHERE cat_wp_id=$wp_id ".$this->query("AND");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;			
	}
	
	function c_cat_pic($wp_id) {
		// return Pictures per Category
		
		global $wpdb;
		$gal_count_req = "SELECT ".$wpdb->anigal_pic.".pid FROM $wpdb->anigal_pic, $wpdb->anigal_alb WHERE ".$wpdb->anigal_alb.".cat_wp_id=$wp_id AND ".$wpdb->anigal_pic.".alb_id = ".$wpdb->anigal_alb.".alb_id ".$this->query("AND.pic");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;			
	}

	function c_cat_com($wp_id) {
		// return Comments per Category
		
		global $wpdb;
		$count = 0;
		$gal_counta_req = 'SELECT DISTINCT '.$wpdb->anigal_alb.'.alb_id FROM '.$wpdb->anigal_alb.', '.$wpdb->anigal_cat.' WHERE '.$wpdb->anigal_alb.'.cat_wp_id=' . $wp_id." ".$this->query("AND.alb");
		$gal_counta_ret = $wpdb->get_results($gal_counta_req);
		if (!empty($gal_counta_ret)){
			foreach ($gal_counta_ret as $gal_counta) {
				$gal_countb_req = 'SELECT '.$wpdb->anigal_pic.'.pid FROM '.$wpdb->comments.', '.$wpdb->anigal_pic.' WHERE '.$wpdb->anigal_pic.'.alb_id = ' . $gal_counta->alb_id . ' AND '.$wpdb->anigal_pic.'.pid = '.$wpdb->comments.'.comment_pid_ID '.$this->query("AND.pic").' AND '.$wpdb->comments.'.comment_approved="1"';
				$gal_countb_ret = $wpdb->get_results($gal_countb_req);
				$count = $count + $wpdb->num_rows;
			}
		}
		return $count;
	}
	
	function c_cat_view($wp_id) {
		// return Picture Views per Category

		global $wpdb;
		$sum_view = 0;
		$gal_count_req = "SELECT ".$wpdb->anigal_pic.".hits FROM $wpdb->anigal_pic, $wpdb->anigal_alb  WHERE ".$wpdb->anigal_pic.".alb_id = ".$wpdb->anigal_alb.".alb_id ".$this->query("AND.pic")." AND ".$wpdb->anigal_alb.".cat_wp_id=$wp_id";
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		if (!empty($gal_count_ret)){
			foreach ($gal_count_ret as $gal_count) {
			$sum_view = $sum_view + $gal_count->hits;
			}
		}
		return $sum_view;
	}
	
	function c_alb_view($alb_id) {
		// return Picture Views per Album
		
		global $wpdb;
		$sum_view = 0;
		$gal_count_req = "SELECT hits FROM $wpdb->anigal_pic WHERE alb_id=$alb_id ".$this->query("AND");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		if (!empty($gal_count_ret)){
			foreach ($gal_count_ret as $gal_count) {
			$sum_view = $sum_view + $gal_count->hits;
			}
		}
		return $sum_view;
	}
	
	function c_alb_pic($alb_id) {
		// return Pictures per Album
		
		global $wpdb;
		$gal_count_req = "SELECT pid FROM $wpdb->anigal_pic WHERE alb_id=$alb_id ".$this->query("AND");
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;	
	}
	
	function c_alb_com($alb_id) {
		// return Comments per Album
		
		global $wpdb;
		$gal_count_req = "SELECT ".$wpdb->anigal_pic.".pid FROM ".$wpdb->comments.", ".$wpdb->anigal_pic." WHERE ".$wpdb->anigal_pic.".alb_id = $alb_id AND ".$wpdb->anigal_pic.".pid = ".$wpdb->comments.".comment_pid_ID ".$this->query("AND.pic")." AND ".$wpdb->comments.".comment_approved='1'";
		$gal_count_ret = $wpdb->get_results($gal_count_req);
		return $wpdb->num_rows;	
	}
	
	function c_pic_com($pic_id) {
		// return Comments Count for specific Picture
		
		global $wpdb;
		$gal_give_req = "SELECT comment_ID FROM $wpdb->comments WHERE comment_pid_ID=$pic_id AND comment_approved='1'";
		$gal_give_ret = $wpdb->get_results($gal_give_req);
		return $wpdb->num_rows;
	}


//  #### MISC FUNCTIONS ###  ---------------------------------------------
	
	function updatehits($hits, $pid) {
		// update hits for a picture
		
		global $wpdb;
		$hits = $hits + 1;
		$update_hits = $wpdb->query("UPDATE $wpdb->anigal_pic SET hits = '$hits' WHERE pid = $pid");
		return $hits;
	}
	
	function nav_cat() {
		// print navigation for category page
		
		global $wpdb;
		if ($permalink_wpid = get_option('anigal_base-id')) {
			$gal_title = $wpdb->get_row("SELECT post_title FROM $wpdb->posts WHERE ID=$permalink_wpid"); ?>
			
			
		<?php
		}
		else echo "&raquo;Navigation not ready - check ANIga Options";
	}

	
	function nav_thumb($alb_wp_id) {
		// print navigation for thumbnail and picture page
		
		global $wpdb;
		if ($permalink_wpid = get_option('anigal_base-id')) {
			$gal_title = $wpdb->get_row("SELECT post_title FROM $wpdb->posts WHERE ID=$permalink_wpid"); ?>
			
			
			
		<?php
		}
		else echo "&raquo;Navigation not ready - check ANIga Options";
	}
	
	function top($numb) {
		// print most viewed pictures

		global $wpdb, $aniga_lang;
		$i = 1; ?>
		<div class="gallery_clear">
		<h2><?php echo $numb; ?> <?php echo $aniga_lang['most_viewed']; ?></h2>
		<br />
		
		<?php
		$gal_req = "SELECT ".$wpdb->anigal_pic.".pid, ".$wpdb->anigal_pic.".path, ".$wpdb->anigal_pic.".filename, ".$wpdb->anigal_pic.".hits, ".$wpdb->anigal_alb.".wp_id FROM $wpdb->anigal_pic, $wpdb->anigal_alb WHERE ".$wpdb->anigal_pic.".alb_id = ".$wpdb->anigal_alb.".alb_id ".$this->query("AND.pic")." ORDER BY ".$wpdb->anigal_pic.".hits DESC LIMIT $numb";
		$gal_ret = $wpdb->get_results($gal_req);
		
		if (!empty($gal_ret)){
			foreach ($gal_ret as $toppic) { ?>
			
				<a href="<?php echo anigal_get_permalink($toppic->wp_id, $toppic->pid, 'pid'); ?>" title="<?php echo $toppic->hits; ?> hits"><img src="<?php echo $toppic->path . 'thumb_' . $toppic->filename; ?>" border="0" class="gallery_pic_border" height="<?php if ($i == 1) echo "100px"; elseif ($i == 2) echo "75px"; else echo "50px"; ?>" align="bottom" alt="<?php echo $toppic->hits; ?> hits" /></a>&nbsp;
				
			<?php
			$i++;
			}
		} ?>

		</div>
		
	<?php
	}

}

?>