<commit_before><?php
/* SVN FILE: $Id$ */

/**
 * Enter description here ....
 *
 * @filesource
 * @copyright    Copyright (c) 2006, .
 * @link
 * @package
 * @subpackage
 * @since
 * @version      $Revision$
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2006/08/22 17:31:26 $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Controller :: Users
 *
 * Enter description here...
 *
 * @package
 * @subpackage
 * @since
 */
uses('sanitize', 'neat_string');
class LoginoutController extends AppController
{
	/**
	 * This controller does not use a model
	 *
	 * @var $uses
	 */
	var $uses =  array('User','SysFunction', 'SysParameter', 'Course');
	var $Sanitize;
	var $components = array('rdAuth','Output','sysContainer','userPersonalize');
	var $NeatString;

	function __construct()
	{
		$this->Sanitize = new Sanitize;
		$this->pageTitle = 'Login';
		$this->NeatString = new NeatString;
		parent::__construct();
	}

	function login() {
		$this->autoRender = false;

		// Get iPeer Admin's email address
		$admin_email = $this->sysContainer->getParamByParamCode('system.admin_email');
		$admin_email = $admin_email['parameter_value'];
		$this->set('admin_email', $admin_email);

		//RENDER VIEW IF USER IS LOGGED IN
		if($this->rdAuth->id && $this->rdAuth->role)
		{
			$redirect = 'home/index/';
			$this->redirect($redirect);
			exit;
		}

		$redirect = $this->__renderLoginPage();
		//RENDER LOGIN FORM AND THEN HANDLE POST
		if (empty($this->params['data']))
		{
			//Check for CWL login auth
			$cwlErr = $this->Session->read('CWLErr');
			if ($cwlErr == 'STUDENT_INVALID_LOGIN'){
				$this->set('errmsg', 'Access Denied. <br>Student must login by using UBC CWL Authentication.<br>If you are experiencing any issues regarding iPeer, please contact the iPeer administrator at <a href="mailto:<EMAIL>"><EMAIL></a>.');
				$this->Session->del('CWLErr');
			} else if ($cwlErr!=''){
				$this->set('errmsg', 'Access Denied. <br>You have successfully logged in using your CWL account but you do not have access to the iPeer application.<br>If you are experiencing any issues regarding iPeer, please contact the iPeer administrator at <a href="mailto:<EMAIL>"><EMAIL></a>.');
				$this->Session->del('CWLErr');
			}

			$lastURL = $this->Session->read('URL');
			$accessErr = $this->Session->read('AccessErr');
			$message = $this->Session->read('Message');
			//$a=print_r($this->rdAuth->Session,true); echo "<pre>$a</pre>"; exit;
			if (empty($lastURL)){

				if ($accessErr=='INVALID_LOGIN'){
					$this->set('errmsg', 'Access Denied. <br>Invalid Username/Password Combination.');
					$this->Session->del('URL');
					$this->Session->del('AccessErr');
				}


			} else {


				if ($accessErr=='NO_LOGIN'){
					$this->set('errmsg', 'please log in to see: '.$lastURL);
				}
				if ($accessErr=='NO_PERMISSION'){
					$this->set('errmsg', 'Permission Denied to see: '.$lastURL);
				}
				if ($accessErr=='INVALID_USER'){
					$this->set('errmsg', 'Access Denied. <br>UBC CWL Authentication is only valid for students. <br>If you are experiencing any issues regarding the iPeer, please contact your administrator for details.');
				}
				$this->Session->del('URL');
				$this->Session->del('AccessErr');
			}

			$this->render($redirect);
			exit;
		}

	}

	function loginByDefault() {
		$this->autoRender = false;


		// Get iPeer Admin's email address
		$admin_email = $this->sysContainer->getParamByParamCode('system.admin_email');
		$admin_email = $admin_email['parameter_value'];
		$this->set('admin_email', $admin_email);

		//RENDER VIEW IF USER IS LOGGED IN
		if($this->rdAuth->id && $this->rdAuth->role) {
			$redirect = 'home/index/';
			$this->redirect($redirect);
			exit;
		}


		//$a=print_r($this->params,true); echo "<pre>$a</pre>"; exit;
		//RENDER LOGIN FORM AND THEN HANDLE POST
		if (empty($this->params['data'])) {
			$redirect = 'login';
			$lastURL = $this->Session->read('URL');
			$accessErr = $this->Session->read('AccessErr');
			$message = $this->Session->read('Message');

			if (empty($lastURL)) {
				//$this->set('errmsg', 'Session Expired.  Please log in again.');
			} else {
				if ($accessErr=='NO_LOGIN'){
					$this->set('errmsg', 'Please Login to see '.$lastURL);
					$this->Session->del('URL');
				}
				if ($accessErr=='NO_PERMISSION'){
					$this->set('errmsg', 'Permission Denied to see: '.$lastURL);
					$this->Session->del('URL');
				}
			}

			$this->render($redirect);
		} else {
			$this->Output->filter($this->params['data']);//always filter
			$this->Sanitize->cleanArray($this->params['data']);
			$this->User->recursive = 0;
			$this->params['data']['User']['username'] = $this->Sanitize->paranoid($this->params['data']['User']['username'],array('.','_','-'));
			$this->params['data']['User']['password'] = md5(trim($this->params['data']['User']['password']));
			$this->params['data'] = $this->User->findUser($this->params['data']['User']['username'], $this->params['data']['User']['password']);
			//Check for CWL integration: Student cannot login generally
			if (isset($this->rdAuth->customIntegrateCWL) && $this->rdAuth->customIntegrateCWL) {

				/*

				Code below:

				&& !($this->params['data']['User']['id'] >=4553 && $this->params['data']['User']['id'] <=4583 )

				is a hard coding for Political Science 101-051 upon the request of <NAME>

				*/
				if ($this->params['data']['User']['role'] == "S" ) {
					$this->params['data']['User']['password'] = '';
					$this->Session->write('CWLErr', 'STUDENT_INVALID_LOGIN');
					$redirect = 'loginout/login';
					$this->redirect($redirect);
					exit;
				}
			}

			if ($this->params['data']['User']['id']) {
				//sets up the session vars
				$this->rdAuth->setFromData($this->params['data']['User']);

                //var_dump($this->rdAuth);

				//sets up the system container for accessible functions
				$accessFunction = $this->SysFunction->getAllAccessibleFunction($this->params['data']['User']['role']);
				$accessController = $this->SysFunction->getTopAccessibleFunction($this->params['data']['User']['role']);
				$this->sysContainer->setAccessFunctionList($accessFunction);
				$this->sysContainer->setActionList($accessController);

				//setup my accessible courses
				$myCourses = $this->Course->findAccessibleCoursesList($this->params['data']['User']);
				$this->sysContainer->setMyCourseList($myCourses);

				//clear up the data parameters
				$this->params['data'] = array();
				$redirect = '/home/index/';
				$this->redirect($redirect);
				exit;

			} else {
				$this->params['data']['User']['password'] = '';
				$this->Session->write('AccessErr', 'INVALID_LOGIN');

				$redirect = '/loginout/login';
				$this->redirect($redirect);
				exit;
			}
		}
	}

	function loginByCWL() {
    require_once('XML/RPC.php');
		$this->autoRender = false;

		$ticket = $_GET['ticket'];
		if ($ticket != null) {
			// now get some info about the session

			// the parameters passed to the RPC interface.  the ticket is the
			// first argument for all functions
			$params = array( new XML_RPC_Value($ticket, 'string') );

			// note that the function name is prepended with the string 'session.'
			$msg = new XML_RPC_Message("session.".$CWL['FunctionName'], $params);

			$cli = new XML_RPC_Client($CWL['RPCPath'], $CWL['RPCURL']);
			$cli->setCredentials($CWL['applicationID'], $CWL['applicationPassword']);
			//print_r ($cli);
			//$cli->setDebug(1);

			$resp = $cli->send($msg);
			if (!$resp)
			{
				echo 'Communication error: ' . $cli->errstr;
				exit;
			}

			// print the raw response data

			//echo "<b>Raw Response:</b><br /><pre>";
			//print_r($resp);
			//echo "</pre>";

			if (!$resp->faultCode())
			{
				// an encoded response value
				$val = $resp->value();

				// the actual data we requested
				$data = XML_RPC_decode($val);

				//echo "<b>Response Data:</b><br /><pre>";
				//print_r($data);
				//echo "</pre>";
				if (!empty($data['student_number'])||!empty($data['guest_id'])) {
					$studentNumber = empty($data['student_number'])? $data['guest_id']:$data['student_number'];

					//Check is this CWL login student able to use iPeer
					$this->params['data'] = $this->User->find("username = '".trim($studentNumber)."'");

					if ($this->params['data']['User']['id'])
					{
						//sets up the session vars
						$this->rdAuth->setFromData($this->params['data']['User']);

						//sets up the system container for accessible functions
						$accessFunction = $this->SysFunction->getAllAccessibleFunction($this->params['data']['User']['role']);
						$accessController = $this->SysFunction->getTopAccessibleFunction($this->params['data']['User']['role']);
						$this->sysContainer->setAccessFunctionList($accessFunction);
						$this->sysContainer->setActionList($accessController);

						//setup my accessible courses
						$myCourses = $this->Course->findAccessibleCoursesList($this->params['data']['User']);
						$this->sysContainer->setMyCourseList($myCourses);

						//clear up the data parameters
						$this->params['data'] = array();
						$redirect = '/home/index/';
						$this->redirect($redirect);
					}
					else
					{
						$this->Session->write('CWLErr', 'NO_PERMISSION');
						$redirect = 'loginout/login';
						$this->redirect($redirect);
					}
				} else {
					$this->Session->write('CWLErr', 'INVALID_USER');
					$redirect = 'loginout/login';
					$this->redirect($redirect);
				}

			}
			else
			{
				// error
				echo '<b>Fault Code:</b> ' . $resp->faultCode() . "<br />\n";
				echo '<b>Fault Reason:</b> ' . $resp->faultString() . "<br />\n";
			}

		}

	}

	/**
	 * Cleans up any Session variable that need to be cleaned on logout
	 */
	function clearSession() {
		$this->Session->del('URL');
		$this->Session->del('AccessErr');
		$this->Session->del('Message');
		$this->Session->del('CWLErr');
	}

	/**
	 * Function to logout the user.
	 *
	 */
	function logout()
	{
		$this->autoRender = false;
		$this->rdAuth->logout();
		$this->clearSession();

		$redirect = 'loginout/login';
		$this->redirect($redirect);

	}

	function __renderLoginPage()
	{
		$redirect = 'login';
		//sets up the system parameters to system contrainer
		$allParameters = $this->SysParameter->findAll(null, array('id', 'parameter_code', 'parameter_value', 'parameter_type'));
		$this->sysContainer->setParamList($allParameters);

		$sysParameter = $this->sysContainer->getParamByParamCode('custom.login_control');
		//Check whether we are using the general ipeer login page
		if (isset($sysParameter['parameter_value']) && $sysParameter['parameter_value'] != 'ipeer') {
      global $CWL;
      $this->set('CWL', $CWL);

			//Setup Custom parameter
			$this->Session->write('ipeerSession.customIntegrateCWL', 1);

			//There is a custom setup for login
			$paraLogin = $this->sysContainer->getParamByParamCode('custom.login_page_pathname');
			if (isset($paraLogin['parameter_value'])) {
				$redirect = $paraLogin['parameter_value'];
			}
		} else {<commit_msg>Made logout clear all the session variables, using a foreach loop.<commit_after><?php
/* SVN FILE: $Id$ */

/**
 * Enter description here ....
 *
 * @filesource
 * @copyright    Copyright (c) 2006, .
 * @link
 * @package
 * @subpackage
 * @since
 * @version      $Revision$
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2006/08/22 17:31:26 $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Controller :: Users
 *
 * Enter description here...
 *
 * @package
 * @subpackage
 * @since
 */
uses('sanitize', 'neat_string');
class LoginoutController extends AppController
{
	/**
	 * This controller does not use a model
	 *
	 * @var $uses
	 */
	var $uses =  array('User','SysFunction', 'SysParameter', 'Course');
	var $Sanitize;
	var $components = array('rdAuth','Output','sysContainer','userPersonalize');
	var $NeatString;

	function __construct()
	{
		$this->Sanitize = new Sanitize;
		$this->pageTitle = 'Login';
		$this->NeatString = new NeatString;
		parent::__construct();
	}

	function login() {
		$this->autoRender = false;

		// Get iPeer Admin's email address
		$admin_email = $this->sysContainer->getParamByParamCode('system.admin_email');
		$admin_email = $admin_email['parameter_value'];
		$this->set('admin_email', $admin_email);

		//RENDER VIEW IF USER IS LOGGED IN
		if($this->rdAuth->id && $this->rdAuth->role)
		{
			$redirect = 'home/index/';
			$this->redirect($redirect);
			exit;
		}

		$redirect = $this->__renderLoginPage();
		//RENDER LOGIN FORM AND THEN HANDLE POST
		if (empty($this->params['data']))
		{
			//Check for CWL login auth
			$cwlErr = $this->Session->read('CWLErr');
			if ($cwlErr == 'STUDENT_INVALID_LOGIN'){
				$this->set('errmsg', 'Access Denied. <br>Student must login by using UBC CWL Authentication.<br>If you are experiencing any issues regarding iPeer, please contact the iPeer administrator at <a href="mailto:<EMAIL>"><EMAIL></a>.');
				$this->Session->del('CWLErr');
			} else if ($cwlErr!=''){
				$this->set('errmsg', 'Access Denied. <br>You have successfully logged in using your CWL account but you do not have access to the iPeer application.<br>If you are experiencing any issues regarding iPeer, please contact the iPeer administrator at <a href="mailto:<EMAIL>"><EMAIL></a>.');
				$this->Session->del('CWLErr');
			}

			$lastURL = $this->Session->read('URL');
			$accessErr = $this->Session->read('AccessErr');
			$message = $this->Session->read('Message');
			//$a=print_r($this->rdAuth->Session,true); echo "<pre>$a</pre>"; exit;
			if (empty($lastURL)){

				if ($accessErr=='INVALID_LOGIN'){
					$this->set('errmsg', 'Access Denied. <br>Invalid Username/Password Combination.');
					$this->Session->del('URL');
					$this->Session->del('AccessErr');
				}


			} else {


				if ($accessErr=='NO_LOGIN'){
					$this->set('errmsg', 'please log in to see: '.$lastURL);
				}
				if ($accessErr=='NO_PERMISSION'){
					$this->set('errmsg', 'Permission Denied to see: '.$lastURL);
				}
				if ($accessErr=='INVALID_USER'){
					$this->set('errmsg', 'Access Denied. <br>UBC CWL Authentication is only valid for students. <br>If you are experiencing any issues regarding the iPeer, please contact your administrator for details.');
				}
				$this->Session->del('URL');
				$this->Session->del('AccessErr');
			}

			$this->render($redirect);
			exit;
		}

	}

	function loginByDefault() {
		$this->autoRender = false;


		// Get iPeer Admin's email address
		$admin_email = $this->sysContainer->getParamByParamCode('system.admin_email');
		$admin_email = $admin_email['parameter_value'];
		$this->set('admin_email', $admin_email);

		//RENDER VIEW IF USER IS LOGGED IN
		if($this->rdAuth->id && $this->rdAuth->role) {
			$redirect = 'home/index/';
			$this->redirect($redirect);
			exit;
		}


		//$a=print_r($this->params,true); echo "<pre>$a</pre>"; exit;
		//RENDER LOGIN FORM AND THEN HANDLE POST
		if (empty($this->params['data'])) {
			$redirect = 'login';
			$lastURL = $this->Session->read('URL');
			$accessErr = $this->Session->read('AccessErr');
			$message = $this->Session->read('Message');

			if (empty($lastURL)) {
				//$this->set('errmsg', 'Session Expired.  Please log in again.');
			} else {
				if ($accessErr=='NO_LOGIN'){
					$this->set('errmsg', 'Please Login to see '.$lastURL);
					$this->Session->del('URL');
				}
				if ($accessErr=='NO_PERMISSION'){
					$this->set('errmsg', 'Permission Denied to see: '.$lastURL);
					$this->Session->del('URL');
				}
			}

			$this->render($redirect);
		} else {
			$this->Output->filter($this->params['data']);//always filter
			$this->Sanitize->cleanArray($this->params['data']);
			$this->User->recursive = 0;
			$this->params['data']['User']['username'] = $this->Sanitize->paranoid($this->params['data']['User']['username'],array('.','_','-'));
			$this->params['data']['User']['password'] = md5(trim($this->params['data']['User']['password']));
			$this->params['data'] = $this->User->findUser($this->params['data']['User']['username'], $this->params['data']['User']['password']);
			//Check for CWL integration: Student cannot login generally
			if (isset($this->rdAuth->customIntegrateCWL) && $this->rdAuth->customIntegrateCWL) {

				/*

				Code below:

				&& !($this->params['data']['User']['id'] >=4553 && $this->params['data']['User']['id'] <=4583 )

				is a hard coding for Political Science 101-051 upon the request of <NAME>

				*/
				if ($this->params['data']['User']['role'] == "S" ) {
					$this->params['data']['User']['password'] = '';
					$this->Session->write('CWLErr', 'STUDENT_INVALID_LOGIN');
					$redirect = 'loginout/login';
					$this->redirect($redirect);
					exit;
				}
			}

			if ($this->params['data']['User']['id']) {
				//sets up the session vars
				$this->rdAuth->setFromData($this->params['data']['User']);

                //var_dump($this->rdAuth);

				//sets up the system container for accessible functions
				$accessFunction = $this->SysFunction->getAllAccessibleFunction($this->params['data']['User']['role']);
				$accessController = $this->SysFunction->getTopAccessibleFunction($this->params['data']['User']['role']);
				$this->sysContainer->setAccessFunctionList($accessFunction);
				$this->sysContainer->setActionList($accessController);

				//setup my accessible courses
				$myCourses = $this->Course->findAccessibleCoursesList($this->params['data']['User']);
				$this->sysContainer->setMyCourseList($myCourses);

				//clear up the data parameters
				$this->params['data'] = array();
				$redirect = '/home/index/';
				$this->redirect($redirect);
				exit;

			} else {
				$this->params['data']['User']['password'] = '';
				$this->Session->write('AccessErr', 'INVALID_LOGIN');

				$redirect = '/loginout/login';
				$this->redirect($redirect);
				exit;
			}
		}
	}

	function loginByCWL() {
    require_once('XML/RPC.php');
		$this->autoRender = false;

		$ticket = $_GET['ticket'];
		if ($ticket != null) {
			// now get some info about the session

			// the parameters passed to the RPC interface.  the ticket is the
			// first argument for all functions
			$params = array( new XML_RPC_Value($ticket, 'string') );

			// note that the function name is prepended with the string 'session.'
			$msg = new XML_RPC_Message("session.".$CWL['FunctionName'], $params);

			$cli = new XML_RPC_Client($CWL['RPCPath'], $CWL['RPCURL']);
			$cli->setCredentials($CWL['applicationID'], $CWL['applicationPassword']);
			//print_r ($cli);
			//$cli->setDebug(1);

			$resp = $cli->send($msg);
			if (!$resp)
			{
				echo 'Communication error: ' . $cli->errstr;
				exit;
			}

			// print the raw response data

			//echo "<b>Raw Response:</b><br /><pre>";
			//print_r($resp);
			//echo "</pre>";

			if (!$resp->faultCode())
			{
				// an encoded response value
				$val = $resp->value();

				// the actual data we requested
				$data = XML_RPC_decode($val);

				//echo "<b>Response Data:</b><br /><pre>";
				//print_r($data);
				//echo "</pre>";
				if (!empty($data['student_number'])||!empty($data['guest_id'])) {
					$studentNumber = empty($data['student_number'])? $data['guest_id']:$data['student_number'];

					//Check is this CWL login student able to use iPeer
					$this->params['data'] = $this->User->find("username = '".trim($studentNumber)."'");

					if ($this->params['data']['User']['id'])
					{
						//sets up the session vars
						$this->rdAuth->setFromData($this->params['data']['User']);

						//sets up the system container for accessible functions
						$accessFunction = $this->SysFunction->getAllAccessibleFunction($this->params['data']['User']['role']);
						$accessController = $this->SysFunction->getTopAccessibleFunction($this->params['data']['User']['role']);
						$this->sysContainer->setAccessFunctionList($accessFunction);
						$this->sysContainer->setActionList($accessController);

						//setup my accessible courses
						$myCourses = $this->Course->findAccessibleCoursesList($this->params['data']['User']);
						$this->sysContainer->setMyCourseList($myCourses);

						//clear up the data parameters
						$this->params['data'] = array();
						$redirect = '/home/index/';
						$this->redirect($redirect);
					}
					else
					{
						$this->Session->write('CWLErr', 'NO_PERMISSION');
						$redirect = 'loginout/login';
						$this->redirect($redirect);
					}
				} else {
					$this->Session->write('CWLErr', 'INVALID_USER');
					$redirect = 'loginout/login';
					$this->redirect($redirect);
				}

			}
			else
			{
				// error
				echo '<b>Fault Code:</b> ' . $resp->faultCode() . "<br />\n";
				echo '<b>Fault Reason:</b> ' . $resp->faultString() . "<br />\n";
			}

		}

	}

	/**
	 * Clean out the entire session
	 */
	function clearSession() {
        foreach ($this->Session->read() as $key => $value) {
            $this->Session->del($key);
        }
	}

	/**
	 * Function to logout the user.
	 *
	 */
	function logout()
	{
		$this->autoRender = false;
		$this->rdAuth->logout();
		$this->clearSession();
		$redirect = 'loginout/login';
		$this->redirect($redirect);

	}

	function __renderLoginPage()
	{
		$redirect = 'login';
		//sets up the system parameters to system contrainer
		$allParameters = $this->SysParameter->findAll(null, array('id', 'parameter_code', 'parameter_value', 'parameter_type'));
		$this->sysContainer->setParamList($allParameters);

		$sysParameter = $this->sysContainer->getParamByParamCode('custom.login_control');
		//Check whether we are using the general ipeer login page
		if (isset($sysParameter['parameter_value']) && $sysParameter['parameter_value'] != 'ipeer') {
      global $CWL;
      $this->set('CWL', $CWL);

			//Setup Custom parameter
			$this->Session->write('ipeerSession.customIntegrateCWL', 1);

			//There is a custom setup for login
			$paraLogin = $this->sysContainer->getParamByParamCode('custom.login_page_pathname');
			if (isset($paraLogin['parameter_value'])) {
				$redirect = $paraLogin['parameter_value'];
			}
		} else {
{'fraction_non_alphanumeric': 0.19955492983332576, 'fraction_numerical': 0.0032699032653617332, 'mean_word_length': 3.817326624371035, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 448, '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': '7306027', 'n_tokens_mistral': 8346, 'n_tokens_neox': 6966, 'n_words': 1451}