<?php

 include("../include/config.php");
 include("../include/functions/import.php");
 require_once($config['basedir'] . "/ajax/cpaint2.inc.php"); 

  $cp = new cpaint();
  $cp->register('member_subscribe');
  $cp->register('check_username');
  $cp->register('member_subscribe_series');
  $cp->register('member_subscribe_channel');
  $cp->register('fav_video');
  $cp->register('rate_video');
  $cp->register('rate_series');
  $cp->register('rate_channel');
  $cp->register('video_comments');
  $cp->register('profile_comments');
  $cp->register('channel_comments');
  $cp->register('video_report');
  $cp->register('add_to_friends');
  $cp->register('send_message');
  $cp->register('add_video_to_channel');
  $cp->register('video_share');
  $cp->start();
  $cp->return_data(); 

// Subscribe To Member Begin
function member_subscribe($subscribee,$subscriber)
{
	 global $cp; 
 	 global $conn;
	 
	 if ($subscribee < 1)
	 {
	 	$submessage="0";
	 }
	 else
	 {
	 	$query="insert into members_subscribers set subscriber='".mysql_real_escape_string($subscriber)."', subscribee='".mysql_real_escape_string($subscribee)."'";
	 	$conn->execute($query);
	 	if(mysql_affected_rows()==1)
	 	{ 
			$submessage="1";
	 	}
	 	else
	 	{ 
			$submessage="0";
	 	}
	}
	 $a_result_node =& $cp->add_node("submessage");
 	 $a_result_node->set_data($submessage);
}
// Subscribe To Member End

// Check Username Begin
function check_username($cusernamed)
{
	 global $cp; 
 	 global $conn;
	 	 
	 if ($cusernamed == "")
	 {
	 	$cunamemsg="2";
	 }
	 else
	 {
	 	$query="SELECT USERID FROM members where username='".mysql_real_escape_string($cusernamed)."'";
	 	$conn->execute($query);
	 	if(mysql_affected_rows()>=1)
	 	{ 
			$cunamemsg="0";
	 	}
	 	else
	 	{ 
			$cunamemsg="1";
	 	}
	 }
	 $a_result_node =& $cp->add_node("cunamemsg");
 	 $a_result_node->set_data($cunamemsg);
}
// Check Username End

// Subscribe To Series Begin
function member_subscribe_series($userid,$seriesid)
{
	 global $cp; 
 	 global $conn;
	 
	 if ($userid < 1 || $seriesid < 1)
	 {
	 	$subseriesmessage="0";
	 }
	 else
	 {
	 	$query="insert into videos_series_subscribers set USERID='".mysql_real_escape_string($userid)."', SID='".mysql_real_escape_string($seriesid)."', time='".time()."', date='".date("Y-m-d")."'";
	 	$conn->execute($query);
		if(mysql_affected_rows()==1)
	 	{
			$query="UPDATE videos_series set subscriptions=subscriptions+1 where SID='".mysql_real_escape_string($seriesid)."'";
	 		$conn->execute($query);
			$subseriesmessage="1";
	 	}
	 	else
	 	{ 
			$subseriesmessage="0";
	 	}
	}
	 $a_result_node =& $cp->add_node("subseriesmessage");
 	 $a_result_node->set_data($subseriesmessage);
}
// Subscribe To Series End

// Subscribe To Channel Begin
function member_subscribe_channel($userid,$channelid)
{
	 global $cp; 
 	 global $conn;
	 
	 if (is_numeric($userid) && is_numeric($channelid))
	 {
	 	$query="insert into channels_members set USERID='".mysql_real_escape_string($userid)."', CID='".mysql_real_escape_string($channelid)."', time_joined='".time()."'";
	 	$conn->execute($query);
		if(mysql_affected_rows()==1)
	 	{
			$query="UPDATE channels set subscriptions=subscriptions+1 where CID='".mysql_real_escape_string($channelid)."'";
	 		$conn->execute($query);
			$jchanmessage="1";
	 	}
	 	else
	 	{ 
			$jchanmessage="0";
	 	}
	}
	else
	{
	 	$jchanmessage="0";
	 }
	 
	 $a_result_node =& $cp->add_node("jchanmessage");
 	 $a_result_node->set_data($jchanmessage);
}
// Subscribe To Channel End

// Favorite Video Begin
function fav_video($fuserid,$fvideoid)
{
	 global $cp; 
 	 global $conn;
	 
	 if ($fuserid < 1)
	 {
	 	$favvidmessage="0";
	 }
	 else
	 {
	 	$query="insert into videos_favorited set USERID='".mysql_real_escape_string($fuserid)."', VIDEOID='".mysql_real_escape_string($fvideoid)."'";
	 	$conn->execute($query);
	 	if(mysql_affected_rows()==1)
	 	{ 
			$favvidmessage="1";
	 	}
	 	else
	 	{ 
			$favvidmessage="0";
	 	}
	}
	 $a_result_node =& $cp->add_node("favvidmessage");
 	 $a_result_node->set_data($favvidmessage);
}
// Favorite Video End

// Video Rating Begin
function rate_video($therating,$thevideoid)
{
	global $cp, $conn; 
	
	if ((is_numeric($therating)) && ($therating >= 2) && ($therating <= 10))
	{			
		$alreadyvoted = ajax_rate_video($thevideoid,$therating);

		if($alreadyvoted)
		{
			$a_result_node =& $cp->add_node("vstatus");
			$a_result_node->set_data('1');
		}
		else
		{
			$a_result_node =& $cp->add_node("vstatus");
			$a_result_node->set_data('0');
		}
	
	}
	else
	{
		$a_result_node =& $cp->add_node("vstatus");
		$a_result_node->set_data('2');
	}
}
// Video Rating End

// Series Rating Begin
function rate_series($therating,$theseriesid)
{
	global $cp, $conn; 
	
	if ((is_numeric($therating)) && ($therating >= 2) && ($therating <= 10))
	{			
		$alreadyvoted = ajax_rate_series($theseriesid,$therating);

		if($alreadyvoted)
		{
			$a_result_node =& $cp->add_node("vsstatus");
			$a_result_node->set_data('1');
		}
		else
		{
			$a_result_node =& $cp->add_node("vsstatus");
			$a_result_node->set_data('0');
		}
	
	}
	else
	{
		$a_result_node =& $cp->add_node("vsstatus");
		$a_result_node->set_data('2');
	}
}
// Series Rating End

// Channel Rating Begin
function rate_channel($therating,$thecid)
{
	global $cp, $conn; 
	
	if ((is_numeric($therating)) && ($therating >= 2) && ($therating <= 10))
	{			
		$alreadyvoted = ajax_rate_channel($thecid,$therating);

		if($alreadyvoted)
		{
			$a_result_node =& $cp->add_node("chanstatus");
			$a_result_node->set_data('1');
		}
		else
		{
			$a_result_node =& $cp->add_node("chanstatus");
			$a_result_node->set_data('0');
		}
	
	}
	else
	{
		$a_result_node =& $cp->add_node("chanstatus");
		$a_result_node->set_data('2');
	}
}
// Channel Rating End


// Video Comment Begin
function video_comments($details,$userid,$videoid)
{
	global $cp, $conn; 
    $details=htmlentities(strip_tags($details)); 

	$query="insert into videos_comments set VIDEOID='".mysql_real_escape_string($videoid)."', USERID='".mysql_real_escape_string($userid)."', details='".mysql_real_escape_string($details)."', time_added='".time()."'";
	$conn->execute($query);
	
	if(mysql_affected_rows()==1)
	{
		$query="update videos set commentcount=commentcount+1 WHERE VIDEOID='".mysql_real_escape_string($videoid)."'";
		$conn->execute($query);
	}
	if(mysql_affected_rows()>=1)
	{
		$vcommsg = 1;
	}
	else
	{ 
		$vcommsg = 0;
	}
		
	$a_result_node =& $cp->add_node("vcommsg");
 	$a_result_node->set_data($vcommsg);
}
// Video Comment End


// Profile Comment Begin
function profile_comments($details,$userid,$profileid)
{
	global $cp, $conn; 
    $details=htmlentities(strip_tags($details)); 

	$query="insert into members_comments set PROFILEID='".mysql_real_escape_string($profileid)."', USERID='".mysql_real_escape_string($userid)."', details='".mysql_real_escape_string($details)."', time_added='".time()."'";
	$conn->execute($query);
	
	if(mysql_affected_rows()>=1)
	{
		$pcommsg = 1;
	}
	else
	{ 
		$pcommsg = 0;
	}
		
	$a_result_node =& $cp->add_node("pcommsg");
 	$a_result_node->set_data($pcommsg);
}
// Profile Comment End

// Channel Comment Begin
function channel_comments($details,$userid,$channelid)
{
	global $cp, $conn; 
    $details=htmlentities(strip_tags($details)); 
	
	$query="SELECT USERID FROM channels_members WHERE CID='".mysql_real_escape_string($channelid)."' AND USERID='".mysql_real_escape_string($userid)."'";
	$conn->execute($query);
	if(mysql_affected_rows()<=0)
	{
		$chancommsg = 2;
	}
	else
	{
		$query="insert into channels_comments set CID='".mysql_real_escape_string($channelid)."', USERID='".mysql_real_escape_string($userid)."', details='".mysql_real_escape_string($details)."', time_added='".time()."'";
		$conn->execute($query);
	
		if(mysql_affected_rows()>=1)
		{
			$chancommsg = 1;
		}
		else
		{ 
			$chancommsg = 0;
		}
	}
	$a_result_node =& $cp->add_node("chancommsg");
 	$a_result_node->set_data($chancommsg);
}
// Channel Comment End


// Report Video Begin
function video_report($vrepremarks,$vrepreason,$userid,$videoid)
{
	global $cp, $conn; 
    $vrremarks=htmlentities(strip_tags($vrepremarks));
	$vrreason=htmlentities(strip_tags($vrepreason));

	$query="insert into videos_reports set VIDEOID='".mysql_real_escape_string($videoid)."', USERID='".mysql_real_escape_string($userid)."', reason='".mysql_real_escape_string($vrreason)."', remarks='".mysql_real_escape_string($vrremarks)."', time='".time()."', date='".date("Y-m-d")."'";
	$conn->execute($query);
	
	if(mysql_affected_rows()>=1)
	{
		$vrepmsg = 1;
	}
	else
	{ 
		$vrepmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("vrepmsg");
 	$a_result_node->set_data($vrepmsg);
}
// Report Video End

// Send Message Begin
function send_message($sendmessagesubjectz,$sendmessagetextz,$userid,$profileid)
{
	global $cp, $conn, $config;
    $sendmessagesubjectz=htmlentities(strip_tags($sendmessagesubjectz));
	$sendmessagetextz=htmlentities(strip_tags($sendmessagetextz));
	
	if ($userid != "" && is_numeric($userid) && $profileid != "" && is_numeric($profileid))
	{
		$query="INSERT INTO messages_inbox SET MSGTO='".mysql_real_escape_string($profileid)."', MSGFROM='".mysql_real_escape_string($userid)."', subject='".mysql_real_escape_string($sendmessagesubjectz)."', message='".mysql_real_escape_string($sendmessagetextz)."', time='".time()."'";
		$conn->execute($query);
		$query="insert into messages_sent set MSGTO='".mysql_real_escape_string($profileid)."', MSGFROM='".mysql_real_escape_string($userid)."', subject='".mysql_real_escape_string($sendmessagesubjectz)."', message='".mysql_real_escape_string($sendmessagetextz)."', time='".time()."'";
		$conn->execute($query);
		
		if(mysql_affected_rows()>=1)
		{			
				$query="SELECT email,username FROM members WHERE USERID='".mysql_real_escape_string($profileid)."'";
				$executequery=$conn->execute($query);
				$sendtotempemail = $executequery->fields[email];
				$sendtotemp = $executequery->fields[username];
				
				$query="SELECT email,username FROM members WHERE USERID='".mysql_real_escape_string($userid)."'";
				$executequery=$conn->execute($query);
				$tempemail = $executequery->fields[email];
				$tempusername = $executequery->fields[username];
				
				$sendername = $config['site_name'];
				
				$from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='newmessage'";
            	$executequery = $conn->execute($query);
				$subject = $executequery->fields['subject']." ".$tempusername;
				
				$inboxlink = $config['baseurl']."/myinbox.php";
				
				$sendmailbody = "<html>";
				$sendmailbody .= $sendtotemp.", you just received a new message from $tempusername. To view this message, click here:<br>";
				$sendmailbody .= "<a href=$inboxlink>$inboxlink</a><br><br>";
				$sendmailbody .= "If you cannot click the link, then visit this url:<br>";
				$sendmailbody .= $inboxlink."<br><br>";
				$sendmailbody .= "$sendername";
				$sendmailbody .= "</html>";
				mailme($sendtotempemail,$sendername,$from,$subject,$sendmailbody,$bcc="");

			$sendmessagetomsg = 1;
		}
		else
		{ 
			$sendmessagetomsg = 0;
		}
	}
	else
	{
		$sendmessagetomsg = 0;
	}
		
	$a_result_node =& $cp->add_node("sendmessagetomsg");
 	$a_result_node->set_data($sendmessagetomsg);
}
// Send Message End

// Add To Friends Begin
function add_to_friends($addtofriendsmessage,$userid,$profileid)
{
	global $cp, $conn; 
    $addtofriendsmessage=htmlentities(strip_tags($addtofriendsmessage));
	
	if ($userid != "" && is_numeric($userid) && $profileid != "" && is_numeric($profileid))
	{
		$query="SELECT RID FROM friends_requests WHERE REQUESTER='".mysql_real_escape_string($userid)."' AND REQUESTEE='".mysql_real_escape_string($profileid)."'";
		$conn->execute($query);
		
		if(mysql_affected_rows()>=1)
		{
			$addtofriendsmsg = 2;
		}
		else
		{
			$query="insert into friends_requests set REQUESTER='".mysql_real_escape_string($userid)."', REQUESTEE='".mysql_real_escape_string($profileid)."', message='".mysql_real_escape_string($addtofriendsmessage)."', time='".time()."'";
			$conn->execute($query);
	
			if(mysql_affected_rows()>=1)
			{
				$addtofriendsmsg = 1;
			}
			else
			{ 
				$addtofriendsmsg = 0;
			}
		}
	}
	else
	{
		$addtofriendsmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("addtofriendsmsg");
 	$a_result_node->set_data($addtofriendsmsg);
}
// Add To Friends End

// Add Video To Channel Begin
function add_video_to_channel($vaddtochanvideo,$userid,$videoid)
{
	global $cp, $conn; 
    
	if(is_numeric($vaddtochanvideo) && is_numeric($userid) && is_numeric($videoid))
	{
		$query="SELECT count(*) as total from channels_videos WHERE VIDEOID='".mysql_real_escape_string($videoid)."' AND CID='".mysql_real_escape_string($vaddtochanvideo)."'";
		$results = $conn->execute($query);
		$existsinchantotal = $results->fields[total];
		
		if ($existsinchantotal >= 1)
		{
			$vatchanmsg = 2;
		}
		else
		{
		
			$query="insert into channels_videos set VIDEOID='".mysql_real_escape_string($videoid)."', CID='".mysql_real_escape_string($vaddtochanvideo)."', time_added='".time()."'";
			$conn->execute($query);
	
			if(mysql_affected_rows()>=1)
			{
				$vatchanmsg = 1;
			}
			else
			{ 
				$vatchanmsg = 0;
			}
		
		}
	}
	else
	{
		$vatchanmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("vatchanmsg");
 	$a_result_node->set_data($vatchanmsg);
}
// Add Video To Channel End

// Share Video Begin
function video_share($vsharemessage,$vshareto,$userid,$videoid)
{
	global $cp, $conn, $config; 
    $vsharemessage=htmlentities(strip_tags($vsharemessage));
	$query="SELECT title FROM videos WHERE VIDEOID='".mysql_real_escape_string($videoid)."'";
	$executequery=$conn->execute($query);
	$temptitle = $executequery->fields[title];
	$tempseotitle = seo_clean_titles_sendmail($temptitle);
	$sharevideolink = $config['baseurl']."/videos/".$videoid."/".$tempseotitle;
	$vshareemails = explode(",",$vshareto);
	$vsentemailscount = 0;
	$vsentmailfailed = 0;
	for($vsendloop=0; $vsendloop<count($vshareemails); $vsendloop++)
	{
			$sendtotemp=$vshareemails[$vsendloop];
			
			if(verify_valid_email($sendtotemp))
			{
				
				$query="SELECT email,username FROM members WHERE USERID='".mysql_real_escape_string($userid)."'";
				$executequery=$conn->execute($query);
				$tempemail = $executequery->fields[email];
				$tempusername = $executequery->fields[username];
				
				$sendername = $config['site_name'];
				
				$from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='sharevideo'";
            	$executequery = $conn->execute($query);
				$subject = $tempemail." ".$executequery->fields['subject'];
				
				$sendmailbody = "<html>";
				$sendmailbody .= $tempusername." thought you would enjoy this video:<br>";
				$sendmailbody .= "<a href=$sharevideolink>$sharevideolink</a><br><br>";
				if ($vsharemessage != "")
				{
					$vsharemessage = str_replace("\\n", "<br>", $vsharemessage);
					$sendmailbody .= $tempusername." also included this message for you:<br>";
					$sendmailbody .= $vsharemessage."<br><br>";
				}
				$sendmailbody .= "If you cannot click the link, then visit this url:<br>";
				$sendmailbody .= $sharevideolink."<br><br>";
				$sendmailbody .= "Enjoy The Video,<br>";
				$sendmailbody .= "$sendername";
				$sendmailbody .= "</html>";
				mailme($sendtotemp,$sendername,$from,$subject,$sendmailbody,$bcc="");
				$vsentemailscount++;
			}
			else
			{
				$vsentmailfailed = 1;
			}
	}
	
	if($vsentemailscount>=1)
	{
		if($vsentmailfailed == "1")
		{
			$vshrmsg = 2;
		}
		else
		{
			$vshrmsg = 1;
		}
	}
	else
	{ 
		$vshrmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("vshrmsg");
 	$a_result_node->set_data($vshrmsg);
}
// Share Video End

?>