Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionalities #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions application/config/autoload.php
@@ -0,0 +1,4 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$autoload['helper'] = array('date');

23 changes: 23 additions & 0 deletions application/config/bitauth.php
Expand Up @@ -6,6 +6,29 @@
*/
$config['require_user_activation'] = TRUE;

/**
* Email activation settings
*
* Mailtype options: 'html', 'text'
*/
$config['email_activation'] = TRUE;
$config['activation_email_address'] = 'info@example.com';
$config['mailtype'] = 'html';

/**
* Alert system administrator by email if lock out attempt happens
*
*/
$config['locked_out_alert_message'] = TRUE;
$config['locked_out_notify_address'] = 'admin@example.com';

/**
* Recapcha settings
*
*/
$config['recaptcha_public_key'] = '';
$config['recaptcha_private_key'] = '';

/**
* Default group_id users are added to when they first register (if one isn't
* specified)
Expand Down
68 changes: 66 additions & 2 deletions application/controllers/example.php
Expand Up @@ -11,6 +11,9 @@ public function __construct()
{
parent::__construct();

$this->_public_key = $this->config->item('recaptcha_public_key', 'bitauth');
$this->_private_key = $this->config->item('recaptcha_private_key', 'bitauth');

$this->load->library('bitauth');

$this->load->helper('form');
Expand Down Expand Up @@ -102,30 +105,91 @@ public function index()
$this->load->view('example/users', array('bitauth' => $this->bitauth, 'users' => $this->bitauth->get_users()));
}

public function _recaptcha_check()
{
$resp = $this->recaptcha->recaptcha_check_answer($this->_private_key, $_SERVER["REMOTE_ADDR"], $this->input->post('recaptcha_challenge_field'), $this->input->post('recaptcha_response_field'));

if( ! $resp->is_valid)
{
$this->form_validation->set_message('_recaptcha_check', $this->lang->line('bitauth_recaptcha_error'));
return FALSE;
}
else
{
return TRUE;
}
}

/**
* Example::register()
*
*/
public function register()
{
$this->load->library('Recaptcha');
$data['recaptcha'] = $this->recaptcha->recaptcha_get_html($this->_public_key);

if($this->input->post())
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|bitauth_unique_username');
$this->form_validation->set_rules('fullname', 'Fullname', '');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|bitauth_valid_password');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'required|matches[password]');
$this->form_validation->set_rules('recaptcha_response_field', 'Captcha code', 'required|callback__recaptcha_check');

if($this->form_validation->run() == TRUE)
{
unset($_POST['submit'], $_POST['password_conf']);
unset($_POST['submit'], $_POST['password_conf'], $_POST['recaptcha_response_field'], $_POST['recaptcha_challenge_field']);
$this->bitauth->add_user($this->input->post());
redirect('example/login');
}

}

$this->load->view('example/add_user', array('title' => 'Register'));
$this->load->view('example/register', array('title' => 'Register'));
}

public function forgot_password()
{
if($this->input->post())
{
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

if($this->form_validation->run() == TRUE)
{
$this->bitauth->generate_forgot_code($this->form_validation->set_value('email'));
redirect('example');
}
}

$this->load->view('example/forgot_password');
}

/**
* Example::change_password()
*
*/
public function change_password($code = '')
{
if( ! $user = $this->bitauth->get_user_by_forgot_code($code))
{
redirect('example');
}

if($this->input->post())
{
$this->form_validation->set_rules('password', 'Password', 'required|bitauth_valid_password');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'required|matches[password]');

if($this->form_validation->run() == TRUE)
{
$this->bitauth->save_new_password($this->form_validation->set_value('password'), $code);
redirect('example');
}
}

$this->load->view('example/change_password', array('forgot_code' => $code));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions application/language/english/bitauth_lang.php
Expand Up @@ -62,3 +62,23 @@
$lang['bitauth_edit_group_failed'] = 'Updating group failed, please notify an administrator.';
$lang['bitauth_del_group_failed'] = 'Deleting group failed, please notify an administrator.';
$lang['bitauth_lang_not_found'] = '(No language entry for "%s" found!)';

/**
* Email Activation Messages
*/
$lang['bitauth_activation_email_subject'] = 'Activation email';
$lang['bitauth_activation_email_message'] = 'Dear User,<br/><br/>Please click on the following link to activate your account!<br/><br/>Your account details:</br>Username: %s<br/>Password: %s<br/><br/>%s<br/><br/>Thank you';
$lang['bitauth_activation_email_send_error'] = 'Could not send activation email, please contact the webmaster';

/**
* Forgot Password Messages
*/
$lang['bitauth_forgotpassword_email_subject'] = 'Forgotten password reset email';
$lang['bitauth_forgotpassword_email_message'] = 'Dear User,<br/><br/>This is a password reset confirmation email.<br/><br/>%s';
$lang['bitauth_forgotpassword_email_send_error'] = 'Could not send password reset email, please contact the webmaster';

/**
* Recatpcha Messages
*/
$lang['bitauth_recaptcha_error'] = 'Incorrect captcha code entered. Try again!';
$lang['bitauth_recaptcha_regenerate'] = 'I can\'t read, please generate new captcha!';
103 changes: 103 additions & 0 deletions application/libraries/Bitauth.php
Expand Up @@ -55,6 +55,11 @@ public function __construct()
$this->_remember_token_expires = $this->config->item('remember_token_expires', 'bitauth');
$this->_remember_token_updates = $this->config->item('remember_token_updates', 'bitauth');
$this->_require_user_activation = $this->config->item('require_user_activation', 'bitauth');
$this->_email_activation = $this->config->item('email_activation', 'bitauth');
$this->_activation_email_address = $this->config->item('activation_email_address', 'bitauth');
$this->_mailtype = $this->config->item('mailtype', 'bitauth');
$this->_locked_out_alert_message = $this->config->item('locked_out_alert_message', 'bitauth');
$this->_locked_out_notify_address = $this->config->item('locked_out_notify_address', 'bitauth');
$this->_pwd_max_age = $this->config->item('pwd_max_age', 'bitauth');
$this->_pwd_age_notification = $this->config->item('pwd_age_notification', 'bitauth');
$this->_pwd_min_length = $this->config->item('pwd_min_length', 'bitauth');
Expand Down Expand Up @@ -306,6 +311,23 @@ public function locked_out()
if($this->timestamp(strtotime($last->time), 'U') - $this->timestamp(strtotime($first->time), 'U') <= ($this->_mins_login_attempts * 60)
&& $this->timestamp(strtotime($last->time), 'U') >= $this->timestamp(strtotime($this->_mins_login_attempts.' minutes ago'), 'U'))
{
if($this->_locked_out_alert_message)
{
$config['useragent'] = 'bitauth';
$config['mailtype'] = $this->_mailtype;
$this->email_lib->initialize($config);
$this->email_lib->clear();
$this->email_lib->from($this->_locked_out_notify_address);
$this->email_lib->to($this->_locked_out_notify_address);
$this->email_lib->subject('Invalid login attempt on '.base_url());
$this->email_lib->message('User: '.$username.' IP address: '.$_SERVER['REMOTE_ADDR'].' Time: '.mdate("%Y-%m-%d %H:%i:%s", time()));

if( ! $this->email_lib->send())
{
log_message('error', 'Invalid login attempt email send failed.'.$this->email_lib->print_debugger());
}
}

return TRUE;
}
}
Expand Down Expand Up @@ -498,6 +520,11 @@ public function add_user($data, $require_activation = NULL)
if($require_activation)
{
$data['activation_code'] = $this->generate_code();

if($this->_email_activation)
{
$this->_send_email_activation($data['email'], $data['activation_code'], $data['username'], $data['password']);
}
}

// Just in case
Expand Down Expand Up @@ -840,6 +867,52 @@ public function forgot_password($user_id)
return FALSE;
}

/**
* Bitauth::generate_forgot_code()
*
* Sends a generated forgot code to the give email address
*/
public function generate_forgot_code($email)
{
if( ! $user = $this->get_user_by_email($email))
{
return FALSE;
}
$forgot_code = $this->forgot_password($user->user_id);

$config['useragent'] = 'bitauth';
$config['mailtype'] = $this->_mailtype;
$this->email_lib->initialize($config);
$this->email_lib->clear();
$this->email_lib->from($this->_activation_email_address);
$this->email_lib->to($email);
$this->email_lib->subject($this->lang->line('bitauth_forgotpassword_email_subject'));
$this->email_lib->message(sprintf($this->lang->line('bitauth_forgotpassword_email_message'),
'<a href="'.base_url().'example/change_password/'.$forgot_code.'">Click here to reset your password</a>'));

if( ! $this->email_lib->send())
{
log_message('error', $this->email_lib->print_debugger());
show_error($this->lang->line('bitauth_forgotpassword_email_send_error'));
exit;
}
}

/**
* Bitauth::save_new_password()
*
* Saves a newly entered password, and delete forgot_code
*/
public function save_new_password($password, $code)
{
if( ! $user = $this->get_user_by_forgot_code($code))
{
return FALSE;
}
$this->set_password($user->user_id, $password);
$this->update_user($user->user_id, array('forgot_code' => ''));
}

/**
* Bitauth::set_password()
*
Expand Down Expand Up @@ -1572,6 +1645,9 @@ public function _assign_libraries()
$CI->load->library('encrypt');
$this->encrypt = $CI->encrypt;

$CI->load->library('email');
$this->email_lib = $CI->email;

$this->load->database();
$this->db = $CI->db;

Expand All @@ -1593,4 +1669,31 @@ public function _assign_libraries()

}

/**
* Bitauth::_send_email_activation()
*
* Send activation email to activate user account
*/
public function _send_email_activation($user_email, $activation_code, $username, $password)
{
$config['useragent'] = 'bitauth';
$config['mailtype'] = $this->_mailtype;
$this->email_lib->initialize($config);
$this->email_lib->clear();
$this->email_lib->from($this->_activation_email_address);
$this->email_lib->to($user_email);
$this->email_lib->subject($this->lang->line('bitauth_activation_email_subject'));
$this->email_lib->message(sprintf($this->lang->line('bitauth_activation_email_message'),
$username,
$password,
'<a href="'.base_url().'example/activate/'.$activation_code.'">Click here to activate</a>'));

if( ! $this->email_lib->send())
{
log_message('error', $this->email_lib->print_debugger());
show_error($this->lang->line('bitauth_activation_email_send_error'));
exit;
}
}

}