<commit_before><?php
/**
 * This controller manages the connection to the application
 * @copyright  Copyright (c) 2014-2017 <NAME>
 * @license      http://opensource.org/licenses/AGPL-3.0 AGPL-3.0
 * @link            https://github.com/bbalet/jorani
 * @since         0.1.0
 */

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

require_once FCPATH . "vendor/autoload.php";

/**
 * This class manages the connection to the application
 * CodeIgniter uses a cookie to store session's details.
 * Login page uses RSA so as to encrypt the user's password.
 */
class Connection extends CI_Controller {

    /**
     * Default constructor
     * @author <NAME> <<EMAIL>>
     */
    public function __construct() {
        parent::__construct();
        $this->load->library('polyglot');
        if ($this->session->userdata('language') === FALSE) {
            $availableLanguages = explode(",", $this->config->item('languages'));
            $languageCode = $this->polyglot->language2code($this->config->item('language'));
            if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                if (in_array($_SERVER['HTTP_ACCEPT_LANGUAGE'], $availableLanguages)) {
                    $languageCode = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
                }
            }
            $this->session->set_userdata('language_code', $languageCode);
            $this->session->set_userdata('language', $this->polyglot->code2language($languageCode));
        }
        $this->lang->load('session', $this->session->userdata('language'));
        $this->lang->load('global', $this->session->userdata('language'));
    }
    
    /**
     * Generate a random string by using openssl, dev/urandom or random
     * @param int $length optional length of the string
     * @return string random string
     * @author <NAME> <<EMAIL>>
     */
    private function generateRandomString($length = 10) {
        if(function_exists('openssl_random_pseudo_bytes')) {
          $rnd = openssl_random_pseudo_bytes($length, $strong);
          if ($strong === TRUE)
            return base64_encode($rnd);
        }
        $sha =''; $rnd ='';
        if (file_exists('/dev/urandom')) {
          $fp = fopen('/dev/urandom', 'rb');
          if ($fp) {
              if (function_exists('stream_set_read_buffer')) {
                  stream_set_read_buffer($fp, 0);
              }
              $sha = fread($fp, $length);
              fclose($fp);
          }
        }
        for ($i=0; $i<$length; $i++) {
          $sha  = hash('sha256',$sha.mt_rand());
          $char = mt_rand(0,62);
          $rnd .= chr(hexdec($sha[$char].$sha[$char+1]));
        }
        return base64_encode($rnd);
    }
    
    /**
     * Login form
     * @author <NAME> <<EMAIL>>
     */
    public function login() {
        //The login form is not used with SAML2 authentication mode
        if ($this->config->item('saml_enabled') === TRUE) {
            redirect('api/sso');
        }
        
        $data['title'] = lang('session_login_title');
        $data['help'] = $this->help->create_help_link('global_link_doc_page_login');
        $this->load->helper('form');
        $this->load->library('form_validation');
        //Note that we don't receive the password as a clear string
        $this->form_validation->set_rules('login', lang('session_login_field_login'), 'required');

        $data['last_page'] = $this->session->userdata('last_page');
        if ($this->form_validation->run() === FALSE) {
            $data['public_key'] = file_get_contents('./assets/keys/public.pem', TRUE);
            $data['salt'] = $this->generateRandomString(rand(5, 20));<commit_msg>ER:If we are already connected (login bookmarked), then redirect to home<commit_after><?php
/**
 * This controller manages the connection to the application
 * @copyright  Copyright (c) 2014-2017 <NAME>
 * @license      http://opensource.org/licenses/AGPL-3.0 AGPL-3.0
 * @link            https://github.com/bbalet/jorani
 * @since         0.1.0
 */

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

require_once FCPATH . "vendor/autoload.php";

/**
 * This class manages the connection to the application
 * CodeIgniter uses a cookie to store session's details.
 * Login page uses RSA so as to encrypt the user's password.
 */
class Connection extends CI_Controller {

    /**
     * Default constructor
     * @author <NAME> <<EMAIL>>
     */
    public function __construct() {
        parent::__construct();
        $this->load->library('polyglot');
        if ($this->session->userdata('language') === FALSE) {
            $availableLanguages = explode(",", $this->config->item('languages'));
            $languageCode = $this->polyglot->language2code($this->config->item('language'));
            if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                if (in_array($_SERVER['HTTP_ACCEPT_LANGUAGE'], $availableLanguages)) {
                    $languageCode = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
                }
            }
            $this->session->set_userdata('language_code', $languageCode);
            $this->session->set_userdata('language', $this->polyglot->code2language($languageCode));
        }
        $this->lang->load('session', $this->session->userdata('language'));
        $this->lang->load('global', $this->session->userdata('language'));
    }
    
    /**
     * Generate a random string by using openssl, dev/urandom or random
     * @param int $length optional length of the string
     * @return string random string
     * @author <NAME> <<EMAIL>>
     */
    private function generateRandomString($length = 10) {
        if(function_exists('openssl_random_pseudo_bytes')) {
          $rnd = openssl_random_pseudo_bytes($length, $strong);
          if ($strong === TRUE)
            return base64_encode($rnd);
        }
        $sha =''; $rnd ='';
        if (file_exists('/dev/urandom')) {
          $fp = fopen('/dev/urandom', 'rb');
          if ($fp) {
              if (function_exists('stream_set_read_buffer')) {
                  stream_set_read_buffer($fp, 0);
              }
              $sha = fread($fp, $length);
              fclose($fp);
          }
        }
        for ($i=0; $i<$length; $i++) {
          $sha  = hash('sha256',$sha.mt_rand());
          $char = mt_rand(0,62);
          $rnd .= chr(hexdec($sha[$char].$sha[$char+1]));
        }
        return base64_encode($rnd);
    }
    
    /**
     * Login form
     * @author <NAME> <<EMAIL>>
     */
    public function login() {
        //The login form is not used with SAML2 authentication mode
        if ($this->config->item('saml_enabled') === TRUE) {
            redirect('api/sso');
        }
        //If we are already connected (login bookmarked), then redirect to home
        if ($this->session->userdata('logged_in') === TRUE) {
            redirect('home');
        }
        
        $data['title'] = lang('session_login_title');
        $data['help'] = $this->help->create_help_link('global_link_doc_page_login');
        $this->load->helper('form');
        $this->load->library('form_validation');
        //Note that we don't receive the password as a clear string
        $this->form_validation->set_rules('login', lang('session_login_field_login'), 'required');

        $data['last_page'] = $this->session->userdata('last_page');
        if ($this->form_validation->run() === FALSE) {
            $data['public_key'] = file_get_contents('./assets/keys/public.pem', TRUE);
            $data['salt'] = $this->generateRandomString(rand(5, 20));
{'fraction_non_alphanumeric': 0.16316831683168317, 'fraction_numerical': 0.009504950495049505, 'mean_word_length': 2.4928538497003228, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 103, 'https://': 2, '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': '3614798', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2195, 'n_words': 452}