<commit_before><?php 
/* * * * * * * * * *  The OneLogin SAML 2.0 authentication module for Moodle  * * * * * * * * *
 * 
 * index.php - landing page for auth/onelogin_saml
 * 
 * @originalauthor OneLogin, Inc
 * @author <NAME>, <NAME>
 * @version 2.0
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package auth/onelogin_saml
 * @requires XMLSecLibs v1.2.2
 * @requires php-saml 
 * @copyright 2011-2014 OneLogin.com
 * 
 * @description 
 * Connects to Moodle, builds the configuration, discovers SAML status, and handles the login process accordingly.
 * 
 * Security Assertion Markup Language (SAML) is a standard for logging users into applications based 
 * on their session in another context. This has significant advantages over logging in using a 
 * username/password: no need to type in credentials, no need to remember and renew password, no weak 
 * passwords etc.
 * 
 * Most companies already know the identity of users because they are logged into their Active Directory 
 * domain or intranet. It is natural to use this information to log users into other applications as well 
 * such as web-based application, and one of the more elegant ways of doing this by using SAML.
 * 
 * SAML is very powerful and flexible, but the specification can be quite a handful. Now OneLogin is 
 * releasing this SAML toolkit for your Moodle application to enable you to integrate SAML in seconds 
 * instead of months. Weve filtered the signal from the noise and come up with a simple setup that will 
 * work for most applications out there.
 * 
 */

	global $CFG, $USER, $SESSION, $POST, $_POST, $_GET, $_SERVER, $DB, $SITE;

	define('SAML_INTERNAL', 1);
	define('SAML_RETRIES', 10);
	$SAML_RETRIES = 10;

	// do the normal Moodle bootstraping so we have access to all config and the DB
	require_once('../../config.php');

	require_once('functions.php');


	// Normal form failed
	if (isset($_GET['errorcode']) && $_GET['errorcode'] != 4) {
		$location = $CFG->wwwroot.'/login/index.php?normal&errorcode='.$_GET['errorcode'];
		header('Location: '.$location);
		exit();
	}


	/**
	 * check that the saml session is OK - if not, send to OneLogin for authentication
	 * if good, then do the Moodle login, and send to the home page, or landing page
	 * if otherwise specified
	 */

	$retry = isset($SESSION->retry) ? $SESSION->retry : 0;
	if ($retry == $SAML_RETRIES) {
		// too many tries at logging in
		session_write_close();
		print_error('retriesexceeded', 'auth_onelogin_saml', '', $retry);
	}
	$SESSION->retry = $retry + 1;

	// save the jump target - this is checked later that it starts with $CFG->wwwroot, and cleaned
	if (isset($_GET['wantsurl'])) {
		$wantsurl = $SESSION->wantsurl = $_GET['wantsurl'];
	}

	// check for a wantsurl in the existing Moodle session 
	if (empty($wantsurl) && isset($SESSION->wantsurl)) {
		$wantsurl = $SESSION->wantsurl;
	}

	// get the plugin config for saml
	$pluginconfig = get_config('auth/onelogin_saml');
	require_once '_toolkit_loader.php';
	$settings = get_saml_settings();
	$auth = new Onelogin_Saml2_Auth($settings);

	if (isset($_GET['logout']) && $_GET['logout']) {
		if (isset($_GET['RelayState']) && !empty($_GET['RelayState'])) {
			$location = $_GET['RelayState'];
		}
		else if (isset($wantsurl)) {
			$location = $wantsurl;
		}
		else {
			$location = $CFG->wwwroot;
		}
	    
		if (isset($_GET['normal'])) {
			deleteLocalSession();
		}
		else {
	        if (isset($_GET) && (isset($_GET['SAMLRequest']) || isset($_GET['SAMLResponse']))) {

	            // Delete the local session must be done on processSLO
	            if (isset($_GET['SAMLRequest'])) {
	                deleteLocalSession();
	            }	            
	            $auth->processSLO();
	            if (empty($auth->getErrors())) {
	                deleteLocalSession();
	            }
	            else {
	                print_r(implode(', ', $auth->getErrors()));
	                exit();
	            }
	        }
	        else {
	        	if ($pluginconfig->saml_slo) {
	            	$auth->logout($location);
	            	exit();
	            }
	        }
		}
		header('Location: '.$location);
		exit();
	}


	if (!isset($_POST['SAMLResponse']) && !((isset($_GET['normal']) && $_GET['normal']) || (isset($SESSION->normal) && $SESSION->normal))  && !(isset($_GET['logout']) && $_GET['logout'])) {
		$auth->login();
	} elseif (isset($_POST['SAMLResponse']) && $_POST['SAMLResponse'] && !((isset($_GET['normal']) && $_GET['normal']) || (isset($SESSION->normal) && $SESSION->normal)) && !(isset($_GET['logout']) && $_GET['logout'])) {
		try {
			$auth->processResponse();
			$errors = $auth->getErrors();
			if (empty($errors)) {
				$GLOBALS['onelogin_saml_nameID'] = $onelogin_saml_nameId = $auth->getNameId();
				$GLOBALS['onelogin_saml_login_attributes'] = $saml_attributes = $auth->getAttributes();
				$wantsurl = isset($SESSION->wantsurl) ? $SESSION->wantsurl : FALSE;
			} else {<commit_msg>FIx bug: Can't use method return value in write context<commit_after><?php 
/* * * * * * * * * *  The OneLogin SAML 2.0 authentication module for Moodle  * * * * * * * * *
 * 
 * index.php - landing page for auth/onelogin_saml
 * 
 * @originalauthor OneLogin, Inc
 * @author <NAME>, <NAME>
 * @version 2.0
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package auth/onelogin_saml
 * @requires XMLSecLibs v1.2.2
 * @requires php-saml 
 * @copyright 2011-2014 OneLogin.com
 * 
 * @description 
 * Connects to Moodle, builds the configuration, discovers SAML status, and handles the login process accordingly.
 * 
 * Security Assertion Markup Language (SAML) is a standard for logging users into applications based 
 * on their session in another context. This has significant advantages over logging in using a 
 * username/password: no need to type in credentials, no need to remember and renew password, no weak 
 * passwords etc.
 * 
 * Most companies already know the identity of users because they are logged into their Active Directory 
 * domain or intranet. It is natural to use this information to log users into other applications as well 
 * such as web-based application, and one of the more elegant ways of doing this by using SAML.
 * 
 * SAML is very powerful and flexible, but the specification can be quite a handful. Now OneLogin is 
 * releasing this SAML toolkit for your Moodle application to enable you to integrate SAML in seconds 
 * instead of months. We’ve filtered the signal from the noise and come up with a simple setup that will 
 * work for most applications out there.
 * 
 */

	global $CFG, $USER, $SESSION, $POST, $_POST, $_GET, $_SERVER, $DB, $SITE;

	define('SAML_INTERNAL', 1);
	define('SAML_RETRIES', 10);
	$SAML_RETRIES = 10;

	// do the normal Moodle bootstraping so we have access to all config and the DB
	require_once('../../config.php');

	require_once('functions.php');


	// Normal form failed
	if (isset($_GET['errorcode']) && $_GET['errorcode'] != 4) {
		$location = $CFG->wwwroot.'/login/index.php?normal&errorcode='.$_GET['errorcode'];
		header('Location: '.$location);
		exit();
	}


	/**
	 * check that the saml session is OK - if not, send to OneLogin for authentication
	 * if good, then do the Moodle login, and send to the home page, or landing page
	 * if otherwise specified
	 */

	$retry = isset($SESSION->retry) ? $SESSION->retry : 0;
	if ($retry == $SAML_RETRIES) {
		// too many tries at logging in
		session_write_close();
		print_error('retriesexceeded', 'auth_onelogin_saml', '', $retry);
	}
	$SESSION->retry = $retry + 1;

	// save the jump target - this is checked later that it starts with $CFG->wwwroot, and cleaned
	if (isset($_GET['wantsurl'])) {
		$wantsurl = $SESSION->wantsurl = $_GET['wantsurl'];
	}

	// check for a wantsurl in the existing Moodle session 
	if (empty($wantsurl) && isset($SESSION->wantsurl)) {
		$wantsurl = $SESSION->wantsurl;
	}

	// get the plugin config for saml
	$pluginconfig = get_config('auth/onelogin_saml');
	require_once '_toolkit_loader.php';
	$settings = get_saml_settings();
	$auth = new Onelogin_Saml2_Auth($settings);

	if (isset($_GET['logout']) && $_GET['logout']) {
		if (isset($_GET['RelayState']) && !empty($_GET['RelayState'])) {
			$location = $_GET['RelayState'];
		}
		else if (isset($wantsurl)) {
			$location = $wantsurl;
		}
		else {
			$location = $CFG->wwwroot;
		}
	    
		if (isset($_GET['normal'])) {
			deleteLocalSession();
		}
		else {
	        if (isset($_GET) && (isset($_GET['SAMLRequest']) || isset($_GET['SAMLResponse']))) {

	            // Delete the local session must be done on processSLO
	            if (isset($_GET['SAMLRequest'])) {
	                deleteLocalSession();
	            }	            
	            $auth->processSLO();
	            $errors = $auth->getErrors();
	            if (empty($errors)) {
	                deleteLocalSession();
	            }
	            else {
	                print_r(implode(', ', $errors));
	                exit();
	            }
	        }
	        else {
	        	if ($pluginconfig->saml_slo) {
	            	$auth->logout($location);
	            	exit();
	            }
	        }
		}
		header('Location: '.$location);
		exit();
	}


	if (!isset($_POST['SAMLResponse']) && !((isset($_GET['normal']) && $_GET['normal']) || (isset($SESSION->normal) && $SESSION->normal))  && !(isset($_GET['logout']) && $_GET['logout'])) {
		$auth->login();
	} elseif (isset($_POST['SAMLResponse']) && $_POST['SAMLResponse'] && !((isset($_GET['normal']) && $_GET['normal']) || (isset($SESSION->normal) && $SESSION->normal)) && !(isset($_GET['logout']) && $_GET['logout'])) {
		try {
			$auth->processResponse();
			$errors = $auth->getErrors();
			if (empty($errors)) {
				$GLOBALS['onelogin_saml_nameID'] = $onelogin_saml_nameId = $auth->getNameId();
				$GLOBALS['onelogin_saml_login_attributes'] = $saml_attributes = $auth->getAttributes();
				$wantsurl = isset($SESSION->wantsurl) ? $SESSION->wantsurl : FALSE;
			} else {
{'fraction_non_alphanumeric': 0.15125125125125125, 'fraction_numerical': 0.004804804804804805, 'mean_word_length': 3.312041432887354, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1180138', 'n_tokens_mistral': 3329, 'n_tokens_neox': 3034, 'n_words': 897}