<?php
$sumfingIncl=true;
require '../fb/facebook.php';
require '../config.thing.php';

if(isset($_POST['userId']) && isset($_POST['ticks'])) {
	
	//connect db
	echo dbConn();
	
	$query=mysql_query("SELECT * FROM admin");
	$result=mysql_fetch_assoc($query);
	$ticketwheel=$result['ticketwheel'];
	$title=$result['title'];
	$prize=$result['prize'];
	$wheelimg=$result['wheelimg'];
	
	if($ticketwheel !='') {
	
		$user=escVar($_POST['userId']);
		$ticketAmount=escVar($_POST['ticks']);
			
		$query=mysql_query("SELECT * FROM user WHERE fb_id='".$user."'") or die(mysql_error());;
		$result=mysql_fetch_assoc($query);
		$token=$result['offline_access'];
		
	
		//post to user timeline
		$app_id = FB_APPID;
		$app_secret = FB_SECRET;
						
		$facebook = new Facebook(array(
			 'appId'  => $app_id,
			 'secret' => $app_secret
		));
						   
		$app_namespace = '?sk=app_'.FB_APPID;
										   
		$token=$user_result['offline_access'];
		
		if($ticketAmount > 1) {
			$msg=str_replace("[TICKETAMOUNT]",$ticketAmount.' '.TICKETS,$ticketwheel);
		} else {
			$msg=str_replace("[TICKETAMOUNT]",$ticketAmount.' '.TICKET,$ticketwheel);
		}
						
		$msg =  array(
			'message' => $msg,
			'caption' => $prize,
			'link'    => MY_FB_PAGE_URL . $app_namespace,
			'picture' => MY_HTTPS_URL.'img/post/'.$wheelimg,
			'name'    => MY_APP_NAME,
			'description' => MY_APP_DESCRIPTION,
			'access_token' => $token
		);
						
						
		try {
			$facebook->api('/me/feed', 'POST', $msg);
		} catch (FacebookApiException $e) {
			error_log($e);
		}
					
					
				
	}
                
                
}?>