<?php 
/*    
 *    Copyright (c) 2010 VidiScript
 *
 *    This file is part of VidiScript.
 *
 *    VidiScript 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.
 *
 *    VidiScript 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 VidiScript.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    File Name: ajaxplay.php
 *    Description: Functions for ajax media rating
 *    $Date: 2010-02-21 23:16:57 +0000 (Sun, 21 Feb 2010) $
 *    $Revision: 12 $
 */
include 'includes/bootstrap.inc' ;
include 'includes/common.inc' ;
include 'includes/tellafriend.inc' ;
$site_template = getSetting("sitetemplate", $db) ;
include "templates/".$site_template."/modules/tellafriend.module" ;
global $sitepath,$templateimagepath,$db ;
if ($_GET['id'] == 1) {
	global $db ;
	$vidid = quote_smart(trim($_GET['vidid'])) ;
	$num = quote_smart(trim($_GET['num'])) ;
	$db->query("SELECT `total_votes`,`total_value`,`used_ips` FROM rating WHERE id='$vidid'") ;
	if ($db->numRows() > 0) {
		$numres = $db->fetch() ;
		$votes = $numres['total_votes'] ;
		$value = $numres['total_value'] ;
		$ratingexists = true ;
	}
	else {
		$ratingexists = false ;
	}
	$ip = $_SERVER['REMOTE_ADDR'] ;
	if ($ratingexists) {
		$db->query("SELECT id FROM rating WHERE used_ips LIKE '%".$ip."%' AND id='$vidid' ") ;
		$voted = $db->numRows() > 0 ;
		$voted = $db->numRows() > 0 ;
		if (!$voted) {
			$vres = $db->fetch() ;
			$updateips = $vres['used_ips'].$ip.':' ;
			$votes = $votes + 1 ;
			$value = $value + $num ;
			$db->query("UPDATE rating SET `used_ips` = '".$updateips."',`total_votes`=".$votes.",`total_value`=".$value." WHERE id='$vidid'") ;
		}
	}
	else {
		$votes = 1 ;
		$value = $num ;
		$db->query("INSERT INTO rating (`id`,`total_votes`,`total_value`,`used_ips`) VALUES (".$vidid.",".$votes.",".$value.",'".$ip.":')") ;
	}
	return "Ran Function" ;
	echo "Ran Function" ;
}
if ($_GET['id'] == 2) {
	$result = rating_bar($vidid, $_SERVER['REMOTE_ADDR'], $pagelink) ;
	echo $result ;
}
if ($_GET['id'] == 3) {
	tellF(quote_smart($_GET['show']));
}
if ($_GET['id'] == 4) {
	$from = getSetting("contact_email", $db) ;
	$headers = "MIME-Version: 1.0\n" ;
	$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
	$headers .= "X-Priority: 1 (Higuest)\n";
	$headers .= "X-MSMail-Priority: High\n";
	$headers .= "Importance: High\n"; 	
	$headers .= 'From: '.$from."\n" .
    $headers .= 'Reply-To: '.$from. "\n";
	$to = explode(",", $_GET['mail']) ;
	$from = getSetting("contact_email", $db) ;
	foreach ($to as $value) {
		if (! empty($value)) {
			$pagelink = quote_smart(trim($_GET['vid']));
			$messageBody = getEmail('taf_body') ;
			$messageBody = str_replace('[SITENAME]', getSetting("sitename", $db), $messageBody) ;
			$messageBody = str_replace('[URL]', $pagelink, $messageBody) ;
			$messageBody = str_replace('[NAME]', quote_smart(trim($_GET['user'])), $messageBody) ;
			$headers = "From: ".$from."\r\n"."Reply-To: ".$from."\r\n"."X-Mailer: PHP/".phpversion() ;
			mail($value, getEmail('taf_subject'), $messageBody, $headers) ;
		}
	}
	echo "Email sent";
}
?>