<commit_before><?php

	include("include/init.php");

	loadlib("http");
	loadlib("random");
	loadlib("foursquare_api");
	loadlib("foursquare_users");

	# Some basic sanity checking like are you already logged in?

	if ($GLOBALS['cfg']['user']['id']){
		header("location: {$GLOBALS['cfg']['abs_root_url']}");
		exit();
	}


	if (! $GLOBALS['cfg']['enable_feature_signin']){
		$GLOBALS['smarty']->display("page_signin_disabled.txt");
		exit();
	}

	$code = get_str("code");

	if (! $code){
		error_404();
	}

	$rsp = foursquare_api_get_auth_token($code);

	if (! $rsp['ok']){
		$GLOBALS['error']['oauth_access_token'] = 1;
		$GLOBALS['smarty']->display("page_auth_callback_foursquare_oauth.txt");
		exit();
	}

	$oauth_token = $rsp['oauth_token'];

	$foursquare_user = foursquare_users_get_by_oauth_token($oauth_token);

	if (($foursquare_user) && ($user_id = $foursquare_user['user_id'])){
		$user = users_get_by_id($user_id);
	}

	# If we don't ensure that new users are allowed to create
	# an account (locally).

	else if (! $GLOBALS['cfg']['enable_feature_signup']){
		$GLOBALS['smarty']->display("page_signup_disabled.txt");
		exit();
	}

	# Hello, new user! This part will create entries in two separate
	# databases: Users and FoursquareUsers that are joined by the primary
	# key on the Users table.

	else {

		$args = array(
			'oauth_token' => $oauth_token,
		);

		$rsp = foursquare_api_call('users/self', $args);

		if (! $rsp['ok']){
			$GLOBALS['error']['foursquare_userinfo'] = 1;
			$GLOBALS['smarty']->display("page_auth_callback_foursquare_oauth.txt");
			exit();
		}

		$foursquare_id = $rsp['rsp']['user']['id'];
		$username = $rsp['rsp']['user']['firstName'];
		$email = $rsp['rsp']['user']['contact']['email-poo'];

		if (! $email){
			$email = "{<EMAIL>";
		}

		$password = random_string(32);

		$user = users_create_user(array(
			"username" => $username,
			"email" => "{<EMAIL>",
			"password" => <PASSWORD>,
		));

		if (! $user){
			$GLOBALS['error']['dberr_user'] = 1;
			$GLOBALS['smarty']->display("page_auth_callback_foursquare_oauth.txt");
			exit();
		}

		$foursquare_user = foursquare_users_create_user(array(
			'user_id' => $user['id'],
			'oauth_token' => $oauth_token,
			'foursquare_id' => $foursquare_id,<commit_msg>use 4sq email or stub w/ 4sq user id<commit_after><?php

	include("include/init.php");

	loadlib("http");
	loadlib("random");
	loadlib("foursquare_api");
	loadlib("foursquare_users");

	# Some basic sanity checking like are you already logged in?

	if ($GLOBALS['cfg']['user']['id']){
		header("location: {$GLOBALS['cfg']['abs_root_url']}");
		exit();
	}


	if (! $GLOBALS['cfg']['enable_feature_signin']){
		$GLOBALS['smarty']->display("page_signin_disabled.txt");
		exit();
	}

	$code = get_str("code");

	if (! $code){
		error_404();
	}

	$rsp = foursquare_api_get_auth_token($code);

	if (! $rsp['ok']){
		$GLOBALS['error']['oauth_access_token'] = 1;
		$GLOBALS['smarty']->display("page_auth_callback_foursquare_oauth.txt");
		exit();
	}

	$oauth_token = $rsp['oauth_token'];

	$foursquare_user = foursquare_users_get_by_oauth_token($oauth_token);

	if (($foursquare_user) && ($user_id = $foursquare_user['user_id'])){
		$user = users_get_by_id($user_id);
	}

	# If we don't ensure that new users are allowed to create
	# an account (locally).

	else if (! $GLOBALS['cfg']['enable_feature_signup']){
		$GLOBALS['smarty']->display("page_signup_disabled.txt");
		exit();
	}

	# Hello, new user! This part will create entries in two separate
	# databases: Users and FoursquareUsers that are joined by the primary
	# key on the Users table.

	else {

		$args = array(
			'oauth_token' => $oauth_token,
		);

		$rsp = foursquare_api_call('users/self', $args);

		if (! $rsp['ok']){
			$GLOBALS['error']['foursquare_userinfo'] = 1;
			$GLOBALS['smarty']->display("page_auth_callback_foursquare_oauth.txt");
			exit();
		}

		$foursquare_id = $rsp['rsp']['user']['id'];
		$username = $rsp['rsp']['user']['firstName'];
		$email = $rsp['rsp']['user']['contact']['email'];

		if (! $email){
			$email = "{<EMAIL>";
		}

		$password = <PASSWORD>_<PASSWORD>(32);

		$user = users_create_user(array(
			"username" => $username,
			"email" => $email,
			"password" => $password,
		));

		if (! $user){
			$GLOBALS['error']['dberr_user'] = 1;
			$GLOBALS['smarty']->display("page_auth_callback_foursquare_oauth.txt");
			exit();
		}

		$foursquare_user = foursquare_users_create_user(array(
			'user_id' => $user['id'],
			'oauth_token' => $oauth_token,
			'foursquare_id' => $foursquare_id,
{'fraction_non_alphanumeric': 0.20295284266196562, 'fraction_numerical': 0.003966505068312032, 'mean_word_length': 5.278008298755187, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5436054', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1696, 'n_words': 289}