<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.2 www.exoops.info 2006/03
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Modified for EXOOPS by MAALBAK //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.org/					     //
// ------------------------------------------------------------------------- //
//  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 2 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, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
include("header.php");
include_once(XOOPS_ROOT_PATH."/class/module.errorhandler.php");
 // MyTextSanitizer object
if($_POST['submit']) {
	$eh = new ErrorHandler; //ErrorHandler object
	if(!$xoopsUser){
		$ratinguser = 0;
	}else{
		$ratinguser = $xoopsUser->uid();
	}
    	//Make sure only 1 anonymous from an IP in a single day.
    	$anonwaitdays = 1;
    	$ip = getenv("REMOTE_ADDR");
	$lid = $_POST['lid'];
	$rating = $_POST['rating'];
    	// Check if Rating is Null
    	if ($rating=="--") {
		redirect_header("ratephoto.php?lid=".$lid."",4,_NORATINGSELECTED);
		exit();
    	}
	$lid = $_POST['lid'];
    	// Check if Link POSTER is voting (UNLESS Anonymous users allowed to post)
    	if ($ratinguser != 0) {
        	$result=$db->query("select submitter from ".$db->prefix("myalbum_photos")." where lid=$lid");
        	while(list($ratinguserDB) = $db->fetchRow($result)) {
        		if ($ratinguserDB == $ratinguser) {
				redirect_header("index.php",4,_CANTVOTEYOURSELF);
				exit();
                	}
        	}
    	// Check if REG user is trying to vote twice.
    		$result=$db->query("select ratinguser from ".$db->prefix("myalbum_votedata")." where lid=$lid");
        	while(list($ratinguserDB) = $db->fetchRow($result)) {
        		if ($ratinguserDB == $ratinguser) {
				redirect_header("index.php",4,_MD_VOTEONCE2);
				exit();
                	}
        	}
    	}
     	// Check if ANONYMOUS user is trying to vote more than once per day.
    	if ($ratinguser == 0){
    		$yesterday = (time()-(86400 * $anonwaitdays));
        	$result=$db->query("select count(*) FROM ".$db->prefix("myalbum_votedata")." WHERE lid=$lid AND ratinguser=0 AND ratinghostname = '$ip' AND ratingtimestamp > $yesterday");
    		list($anonvotecount) = $db->fetchRow($result);
    		if ($anonvotecount > 0) {
			redirect_header("index.php",4,_MD_VOTEONCE2);
			exit();
        	}
    	}
	if($rating > 10){
		$rating = 10;
	}
        //All is well.  Add to Line Item Rate to DB.
	$newid = $db->genId($db->prefix("myalbum_votedata")."_ratingid_seq");
	$datetime = time();
    	$db->query("INSERT INTO ".$db->prefix("myalbum_votedata")." (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp) VALUES ($newid, $lid, $ratinguser, $rating, '$ip', $datetime)") or $eh->show("0013");
        //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
        updaterating($lid);
	$ratemessage = _MD_VOTEAPPRE."<br>".sprintf(_MD_THANKURATE,$bcoosConfig[sitename]);
	redirect_header("index.php",2,$ratemessage);
	exit();
} else {
	$result=$db->query("select title from ".$db->prefix("myalbum_photos")." where lid=$lid");
	list($title) = $db->fetchRow($result);
	$bcoosConfig['title'] = _MI_MYALBUM_NAME." - ".$title ;
    	include(XOOPS_ROOT_PATH."/header.php");
    	OpenTable();
    	echo "<b>"._MI_MYALBUM_NAME."</b>";
    	
	$title = $myts->makeTboxData4Show($title);
    	echo "
    	<hr size=1 noshade>
	<table border=0 cellpadding=1 cellspacing=0 width=\"80%\"><tr><td>
    	<h4><center>$title</center></h4>
    	<UL>
     	<LI>"._VOTEONCE."
     	<LI>"._RATINGSCALE."
     	<LI>"._BEOBJECTIVE."
     	<LI>"._CANTVOTEYOURSELF."";
    	echo "
     	</UL>
     	</td></tr>
     	<tr><td align=\"center\">
     	<form method=\"POST\" action=\"ratephoto.php\">
     	<input type=\"hidden\" name=\"lid\" value=\"".$lid."\">
     	<select class=select name=\"rating\">
     	<option>--</option>";
     	for($i=10;$i>0;$i--){
		echo "<option value=\"".$i."\">".$i."</option>\n";
	}
     	echo "</select>&nbsp;&nbsp;<input type=\"submit\" class=button name=\"submit\" value=\""._RATEIT."\">\n";
	echo "&nbsp;<input type=button class=button value="._CANCEL." onclick=\"javascript:history.go(-1)\">\n";
    	echo "</form></td></tr></table>\n";
    	CloseTable();
}
include("footer.php");
?>
