<?php
	// ======================================== \
	// Package: Mihalism Multi Host
	// Version: 5.0.0
	// Copyright (c) 2007, 2008, 2009 Mihalism Technologies
	// License: http://www.gnu.org/licenses/gpl.txt GNU Public License
	// LTE: 1251327647 - Wednesday, August 26, 2009, 07:00:47 PM EDT -0400
	// ======================================== /
	
	require_once "./source/includes/data.php";
	require_once "{$mmhclass->info->root_path}source/language/viewer.php";
	
	$mmhclass->templ->page_title = sprintf($mmhclass->lang['001'], $mmhclass->info->config['site_name']);
	
	if ($mmhclass->funcs->is_null($mmhclass->input->get_vars['file']) == true) {
		$mmhclass->templ->error($mmhclass->lang['002'], true);
	} elseif ($mmhclass->funcs->is_file($mmhclass->input->get_vars['file'], $mmhclass->info->root_path.$mmhclass->info->config['upload_path'], true) == false) {
		$mmhclass->templ->error(sprintf($mmhclass->lang['003'], $mmhclass->image->basename($mmhclass->input->get_vars['file'])), true);
	} elseif ($mmhclass->image->is_image($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$mmhclass->input->get_vars['file']) == false) {
		$mmhclass->templ->error(sprintf($mmhclass->lang['004'], $mmhclass->image->basename($mmhclass->input->get_vars['file'])), true);
	} else {
		$filename = $mmhclass->image->basename($mmhclass->input->get_vars['file']);
		
		$file_info = $mmhclass->image->get_image_info($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename);
		$file_logs = $mmhclass->db->fetch_array($mmhclass->db->query("SELECT * FROM `[1]` WHERE `filename` = '[2]' LIMIT 1;", array(MYSQL_FILE_LOGS_TABLE, $filename)));
		$file_sinfo = $mmhclass->db->fetch_array($mmhclass->db->query("SELECT * FROM `[1]` WHERE `filename` = '[2]' LIMIT 1;", array(MYSQL_FILE_STORAGE_TABLE, $filename)));
		$rating_info = $mmhclass->db->fetch_array($mmhclass->db->query("SELECT * FROM `[1]` WHERE `filename` = '[2]' LIMIT 1;", array(MYSQL_FILE_RATINGS_TABLE, $filename)));
		
		if ($mmhclass->funcs->is_null($mmhclass->input->server_vars['http_referer']) == false && stripos($mmhclass->input->server_vars['http_referer'], $mmhclass->info->base_url) === false) {
			$new_viewer_click = $mmhclass->db->query("UPDATE `[1]` SET `viewer_clicks` = `viewer_clicks` + 1 WHERE `filename` = '[2]';", array(MYSQL_FILE_STORAGE_TABLE, $filename));
		}
		
		if ($mmhclass->input->get_vars['act'] == "rate_it" && $mmhclass->funcs->is_null($mmhclass->input->post_vars['rating_id']) == false) {
			$mmhclass->templ->templ_globals['new_file_rating'] = true;
			
			if (in_array($mmhclass->input->server_vars['remote_addr'], explode("|", $rating_info['voted_by'])) == true) {
				$new_rating_html = $failed_image_rating = $mmhclass->templ->error($mmhclass->lang['005'], false);
			} else {
				if ($mmhclass->funcs->is_null($rating_info['rating_id']) == true) {
					$mmhclass->db->query("INSERT INTO `[1]` (`filename`, `total_rating`, `total_votes`, `voted_by`) VALUES ('[2]', '0', '0', '');", array(MYSQL_FILE_RATINGS_TABLE, $filename));
				}
				
				$mmhclass->db->query("UPDATE `[1]` SET `total_rating` = `total_rating` + '[2]', `total_votes` = `total_votes` + 1, `voted_by` = '[3]' WHERE `filename` = '[4]';", array(MYSQL_FILE_RATINGS_TABLE, $mmhclass->input->post_vars['rating_id'], "{$rating_info['voted_by']}|{$mmhclass->input->server_vars['remote_addr']}", $filename));
				
				$new_rating_html = $mmhclass->templ->message(sprintf($mmhclass->lang['006'], $filename), false);
			}
		}
		
		$mmhclass->templ->templ_globals['file_info'] = $file_info;
		$image_size = $mmhclass->image->scaleby_maxwidth($filename, 940);
		
		$mmhclass->templ->templ_vars[] = array(
			 "FILENAME" => $filename,
			 "MIME_TYPE" => $file_info['mime'],
			 "IMAGE_WIDTH" => $file_info['width'],
			 "NEW_RATING_HTML" => $new_rating_html,
			 "IMAGE_HEIGHT" => $file_info['height'],
			 "HIDDEN_COMMENT" => $file_info['comment'],
			 "UPLOAD_PATH" => $mmhclass->info->config['upload_path'],
			 "FILE_LINKS" => $mmhclass->templ->file_results($filename),
			 "FILE_EXTENSION" => $mmhclass->image->file_extension($filename),
			 "TOTAL_FILESIZE" => $mmhclass->image->format_filesize($file_info['bits']),
			 "DATE_UPLOADED" => date($mmhclass->info->config['date_format'], $file_info['mtime']),
			 "REAL_FILENAME" => (($mmhclass->funcs->is_null($file_logs['original_filename']) == false) ? $file_logs['original_filename'] : $filename),		
			 "IMAGE_RESIZE" => (($mmhclass->funcs->is_null($image_size['h']) == false) ? "width: {$image_size['w']}px; height: {$image_size['h']}px;" : NULL),
			 "VIEWER_CLICKS" => $mmhclass->funcs->format_number(($new_viewer_click == false) ? $file_sinfo['viewer_clicks'] : ($file_sinfo['viewer_clicks'] + 1)),
			 "TOTAL_RATINGS" => $mmhclass->funcs->format_number((isset($new_rating_html) == true && isset($failed_image_rating) == false) ? ($rating_info['total_votes'] + 1) : $rating_info['total_votes']),
	  	);
		
		$mmhclass->templ->output("viewer");
	}
	
?>