<?php
session_start();


require('include/twitteroauth/autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;

$db = new SQLite3('ask.db');
if(isset($_GET["oauth_verifier"]) and isset($_GET["oauth_token"])){
	if(isset($_SESSION["uid"])){
		include("twitter.secret");

		define('CONSUMER_KEY', $consumer_key);
		define('CONSUMER_SECRET', $consumer_secret);
		define('OAUTH_CALLBACK', "https://ask.fai.su/from_twitter.php");
		
		$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
		$connection->setTimeouts(10, 15);

		$access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_GET['oauth_verifier']]);

		$user = $db->exec("UPDATE users SET
			tw_oauth_token = '" . $access_token["oauth_token"] .
			"', tw_oauth_verify = '" . $access_token["oauth_token_secret"]  . "' 
			WHERE id = ". $_SESSION["uid"] . ";");
		header("Location: /config");
	}
	echo("You have to be logged in to perform that action");
}
else{
	echo("Paramters not given");
}
?>