<?php 
if (!empty($_GET['id'])) {

	$id = PT_Secure($_GET['id']);

	if ($_GET['type'] == 'video' || $_GET['type'] == 'image') {
		$get_ad = $db->where('id', $id)->getOne(T_VIDEO_ADS);
		if (!empty($get_ad)) {
			$update_clicks = $db->where('id', $get_ad->id)->update(T_VIDEO_ADS, array(
                'clicks' => $db->inc(1)
            ));
			header("Location: " . $get_ad->ad_link);
			exit();
		}
	}

	elseif ($_GET['type'] == 'pagead' && !empty($_SESSION['pagead'])) {
		
		if ($id != $_SESSION['pagead']) {
			error_log("Error -Could not make transaction: The Page ad is Invalid", 0);
			pt_redirect(PT_Link(''));
		}

		$ad_id  = $id;
		$ad     = $db->where('id',$ad_id)->getOne(T_USR_ADS);
		if (!empty($ad)) {
			$ad_owner     = $db->where('id',$ad->user_id)->getOne(T_USERS);
			$con_price    = $pt->config->ad_c_price;
			$ad_trans     = false;
			$ad_url       = urldecode($ad->url);
			$is_owner     = false;
			$ad_tans_data = array(
				'results' => ($ad->results += 1)
			);

			if (IS_LOGGED) {
				$is_owner = ($ad->user_id == $user->id) ? true : false;
			}


			if (!array_key_exists($ad_id, $pt->user_ad_cons['uaid_']) && !$is_owner) {
				$ad_tans_data['spent']               = ($ad->spent += $con_price);
				$ad_trans                            = true;
				$pt->user_ad_cons['uaid_'][$ad->id]  = $ad->id;
				setcookie('_uads', htmlentities(serialize($pt->user_ad_cons)), time() + (10 * 365 * 24 * 60 * 60),'/');
			}

			$update       = $db->where('id',$ad_id)->update(T_USR_ADS,$ad_tans_data);
			if ($update && $ad_trans && !$is_owner) {
				$db->where('id',$ad_owner->id)->update(T_USERS,array('wallet' => ($ad_owner->wallet -= $con_price)));
			}

			header("Location: $ad_url");
			exit();
		}
	}
}

pt_redirect(PT_Link(''));


