<commit_before><?php

/**
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This software is a derived product, based on:
 *
 * Simple Machines Forum (SMF)
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.0 Alpha
 *
 * Handle all of the OpenID interfacing and communications.
 *
 */

if (!defined('ELK'))
	die('No access...');

class OpenID
{
	/**
	 * Openid_uri is the URI given by the user
	 * Validates the URI and changes it to a fully canonical URL
	 * Determines the IDP server and delegation
	 * Optional array of fields to restore when validation complete.
	 * Redirects the user to the IDP for validation
	 *
	 * @param string $openid_uri
	 * @param bool $return = false
	 * @param array $save_fields = array()
	 * @param string $return_action = null
	 * @return string
	 */
	function validate($openid_uri, $return = false, $save_fields = array(), $return_action = null)
	{
		global $scripturl, $boardurl, $modSettings;

		$openid_url = $this->canonize($openid_uri);

		$response_data = $this->getServerInfo($openid_url);
		if ($response_data === false)
			return 'no_data';

		if (($assoc = $this->getAssociation($response_data['server'])) == null)
			$assoc = $this->makeAssociation($response_data['server']);

		// include file for member existence
		require_once(SUBSDIR . '/Members.subs.php');

		// Before we go wherever it is we are going, store the GET and POST data, because it might be useful when we get back.
		$request_time = time();
		// Just in case they are doing something else at this time.
		while (isset($_SESSION['openid']['saved_data'][$request_time]))
			$request_time = md5($request_time);

		$_SESSION['openid']['saved_data'][$request_time] = array(
			'get' => $_GET,
			'post' => $_POST,
			'openid_uri' => $openid_url,
			'cookieTime' => $modSettings['cookieTime'],
		);

		$parameters = array(
			'openid.mode=checkid_setup',
			'openid.trust_root=' . urlencode($scripturl),
			'openid.identity=' . urlencode(empty($response_data['delegate']) ? $openid_url : $response_data['delegate']),
			'openid.assoc_handle=' . urlencode($assoc['handle']),
			'openid.return_to=' . urlencode($scripturl . '?action=openidreturn&sa=' . (!empty($return_action) ? $return_action : $_REQUEST['action']) . '&t=' . $request_time . (!empty($save_fields) ? '&sf=' . base64_encode(serialize($save_fields)) : '')),
		);

		// If they are logging in but don't yet have an account or they are registering, let's request some additional information
		if (($_REQUEST['action'] == 'login2' && !memberExists($openid_url)) || ($_REQUEST['action'] == 'register' || $_REQUEST['action'] == 'register2'))
		{
			// Email is required.
			$parameters[] = 'openid.sreg.required=email';
			// The rest is just optional.
			$parameters[] = 'openid.sreg.optional=nickname,dob,gender';
		}

		$redir_url = $response_data['server'] . '?' . implode('&', $parameters);

		if ($return)
			return $redir_url;
		else
			redirectexit($redir_url);
	}

	/**
	 * Revalidate a user using OpenID.
	 * Note that this function will not return when authentication is required.
	 *
	 * @return boolean
	 */
	function revalidate()
	{
		global $user_settings;

		if (isset($_SESSION['openid_revalidate_time']) && $_SESSION['openid_revalidate_time'] > time() - 60)
		{
			unset($_SESSION['openid_revalidate_time']);
			return true;
		}
		else
			$this->validate($user_settings['openid_uri'], false, null, 'revalidate');

		// We shouldn't get here.
		trigger_error('Hacking attempt...', E_USER_ERROR);
	}

	/**
	 * Retrieve an existing, not expired, association if there is any.
	 *
	 * @param string $server
	 * @param string $handle = null
	 * @param bool $no_delete = false
	 * @return array
	 */
	function getAssociation($server, $handle = null, $no_delete = false)
	{
		$db = database();

		if (!$no_delete)
		{
			// Delete the already expired associations.
			$db->query('openid_delete_assoc_old', '
				DELETE FROM {db_prefix}openid_assoc
				WHERE expires <= {int:current_time}',
				array(
					'current_time' => time(),
				)
			);
		}

		// Get the association that has the longest lifetime from now.
		$request = $db->query('openid_select_assoc', '
			SELECT server_url, handle, secret, issued, expires, assoc_type
			FROM {db_prefix}openid_assoc
			WHERE server_url = {string:server_url}' . ($handle === null ? '' : '
				AND handle = {string:handle}') . '
			ORDER BY expires DESC',
			array(
				'server_url' => $server,
				'handle' => $handle,
			)
		);

		if ($db->num_rows($request) == 0)
			return null;

		$return = $db->fetch_assoc($request);
		$db->free_result($request);

		return $return;
	}

	/**
	 * Create and store an association to the given server.
	 *
	 * @param string $server
	 * @return array
	 */
	function makeAssociation($server)
	{
		global $modSettings, $p;
<commit_msg>globals again. Signed-off-by:<NAME> <<EMAIL>><commit_after><?php

/**
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This software is a derived product, based on:
 *
 * Simple Machines Forum (SMF)
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.0 Alpha
 *
 * Handle all of the OpenID interfacing and communications.
 *
 */

if (!defined('ELK'))
	die('No access...');

class OpenID
{
	/**
	 * Openid_uri is the URI given by the user
	 * Validates the URI and changes it to a fully canonical URL
	 * Determines the IDP server and delegation
	 * Optional array of fields to restore when validation complete.
	 * Redirects the user to the IDP for validation
	 *
	 * @param string $openid_uri
	 * @param bool $return = false
	 * @param array $save_fields = array()
	 * @param string $return_action = null
	 * @return string
	 */
	function validate($openid_uri, $return = false, $save_fields = array(), $return_action = null)
	{
		global $scripturl, $modSettings;

		$openid_url = $this->canonize($openid_uri);

		$response_data = $this->getServerInfo($openid_url);
		if ($response_data === false)
			return 'no_data';

		if (($assoc = $this->getAssociation($response_data['server'])) == null)
			$assoc = $this->makeAssociation($response_data['server']);

		// include file for member existence
		require_once(SUBSDIR . '/Members.subs.php');

		// Before we go wherever it is we are going, store the GET and POST data, because it might be useful when we get back.
		$request_time = time();
		// Just in case they are doing something else at this time.
		while (isset($_SESSION['openid']['saved_data'][$request_time]))
			$request_time = md5($request_time);

		$_SESSION['openid']['saved_data'][$request_time] = array(
			'get' => $_GET,
			'post' => $_POST,
			'openid_uri' => $openid_url,
			'cookieTime' => $modSettings['cookieTime'],
		);

		$parameters = array(
			'openid.mode=checkid_setup',
			'openid.trust_root=' . urlencode($scripturl),
			'openid.identity=' . urlencode(empty($response_data['delegate']) ? $openid_url : $response_data['delegate']),
			'openid.assoc_handle=' . urlencode($assoc['handle']),
			'openid.return_to=' . urlencode($scripturl . '?action=openidreturn&sa=' . (!empty($return_action) ? $return_action : $_REQUEST['action']) . '&t=' . $request_time . (!empty($save_fields) ? '&sf=' . base64_encode(serialize($save_fields)) : '')),
		);

		// If they are logging in but don't yet have an account or they are registering, let's request some additional information
		if (($_REQUEST['action'] == 'login2' && !memberExists($openid_url)) || ($_REQUEST['action'] == 'register' || $_REQUEST['action'] == 'register2'))
		{
			// Email is required.
			$parameters[] = 'openid.sreg.required=email';
			// The rest is just optional.
			$parameters[] = 'openid.sreg.optional=nickname,dob,gender';
		}

		$redir_url = $response_data['server'] . '?' . implode('&', $parameters);

		if ($return)
			return $redir_url;
		else
			redirectexit($redir_url);
	}

	/**
	 * Revalidate a user using OpenID.
	 * Note that this function will not return when authentication is required.
	 *
	 * @return boolean
	 */
	function revalidate()
	{
		global $user_settings;

		if (isset($_SESSION['openid_revalidate_time']) && $_SESSION['openid_revalidate_time'] > time() - 60)
		{
			unset($_SESSION['openid_revalidate_time']);
			return true;
		}
		else
			$this->validate($user_settings['openid_uri'], false, null, 'revalidate');

		// We shouldn't get here.
		trigger_error('Hacking attempt...', E_USER_ERROR);
	}

	/**
	 * Retrieve an existing, not expired, association if there is any.
	 *
	 * @param string $server
	 * @param string $handle = null
	 * @param bool $no_delete = false
	 * @return array
	 */
	function getAssociation($server, $handle = null, $no_delete = false)
	{
		$db = database();

		if (!$no_delete)
		{
			// Delete the already expired associations.
			$db->query('openid_delete_assoc_old', '
				DELETE FROM {db_prefix}openid_assoc
				WHERE expires <= {int:current_time}',
				array(
					'current_time' => time(),
				)
			);
		}

		// Get the association that has the longest lifetime from now.
		$request = $db->query('openid_select_assoc', '
			SELECT server_url, handle, secret, issued, expires, assoc_type
			FROM {db_prefix}openid_assoc
			WHERE server_url = {string:server_url}' . ($handle === null ? '' : '
				AND handle = {string:handle}') . '
			ORDER BY expires DESC',
			array(
				'server_url' => $server,
				'handle' => $handle,
			)
		);

		if ($db->num_rows($request) == 0)
			return null;

		$return = $db->fetch_assoc($request);
		$db->free_result($request);

		return $return;
	}

	/**
	 * Create and store an association to the given server.
	 *
	 * @param string $server
	 * @return array
	 */
	function makeAssociation($server)
	{
		global $p;

{'fraction_non_alphanumeric': 0.15267328719028989, 'fraction_numerical': 0.003009328919650918, 'mean_word_length': 3.9234567901234567, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7292882', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3171, 'n_words': 855}