Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Compatibility changes & some extras.

  • Loading branch information...
commit 7eb6cc2a587a7d9660cf058af7982575e6d769d7 1 parent d89ade7
@adamgriffiths authored
View
66 config/auth.php
@@ -1,66 +0,0 @@
-<?php
-/**
-* Authentication Library
-*
-* @package Authentication
-* @category Libraries
-* @author Adam Griffiths
-* @link http://adamgriffiths.co.uk
-* @version 1.0.6
-* @copyright Adam Griffiths 2009
-*
-* Auth provides a powerful, lightweight and simple interface for user authentication
-*/
-
-
-/**
-* The array which holds your user groups and their ID.
-* If you have a database table for groups, these ID's must be the same as in the database.
-*/
-$config['auth_groups'] = array(
- 'admin' => '1',
- 'editor' => '2',
- 'user' => '100'
- );
-
-/**
-* The default URI string to redirect to after a successful login.
-*/
-$config['auth_login'] = 'admin/dashboard';
-
-/**
-* The default URI string to redirect to after a successful logout.
-*/
-$config['auth_logout'] = 'login';
-
-/**
-* The URI string to redirect to when a user entered incorrect login details or is not authenticated
-*/
-$config['auth_incorrect_login'] = 'login';
-
-
-/**
-* bool TRUE / FALSE
-* Determines whether or not users will be remembered by the auth library
-*/
-$config['auth_remember'] = TRUE;
-
-/**
-* The following options provide the ability to easily rename the directories
-* for your auth views, models, and controllers.
-*
-* Remember to also update your routes file if you change the controller directory
-* MUST HAVE A TRAILING SLASH!
-*/
-$config['auth_controllers_root'] = 'admin/';
-$config['auth_models_root'] = '';
-$config['auth_views_root'] = 'auth/';
-
-/**
-* Set the names for your user tables below (sans prefix, which will be automatically added)
-* ex.: your table is named `ci_users` with 'ci_' defined as your dbprefix in config/database.php, so set it to 'users' below
-*/
-$config['auth_user_table'] = 'users';
-$config['auth_group_table'] = 'groups';
-
-?>
View
10 controllers/admin/admin.php
@@ -2,20 +2,20 @@
class Admin extends Application
{
- function Admin()
+ public function __construct()
{
- parent::Application();
+ parent::__construct();
}
- function index()
+ public function index()
{
if(logged_in())
{
- $this->auth->view('dashboard');
+ $this->ag_auth->view('dashboard');
}
else
{
- $this->auth->login();
+ $this->ag_auth->login();
}
}
View
22 controllers/admin/users.php
@@ -3,14 +3,14 @@
class Users extends Application
{
- function Users()
+ public function __construct()
{
- parent::Application();
- $this->auth->restrict('admin'); // restrict this controller to admins only
+ parent::__construct();
+ $this->ag_auth->restrict('admin'); // restrict this controller to admins only
$this->load->model($this->models."usermodel", 'users'); // Load the user model - gets lists of users etc
}
- function manage()
+ public function manage()
{
$data = $this->users->users(); // Grab an array of users from the database
$this->table->set_heading('Username', 'Email', 'Actions'); // Setting headings for the table
@@ -21,23 +21,23 @@ function manage()
$this->table->add_row($key['username'], $key['email'], $actions); // Adding row to table
}
- $this->auth->view('users/manage'); // Load the view
+ $this->ag_auth->view('users/manage'); // Load the view
}
- function delete($id)
+ public function delete($id)
{
$this->users->delete($id);
- $this->auth->view('users/delete_success');
+ $this->ag_auth->view('users/delete_success');
}
- function add()
+ public function add()
{
- $this->auth->register(FALSE);
+ $this->ag_auth->register(FALSE);
}
- function edit($id)
+ public function edit($id)
{
- $this->auth->register(FALSE, TRUE, $id);
+ $this->ag_auth->register(FALSE, TRUE, $id);
}
}
View
23 core/MY_Controller.php
@@ -6,7 +6,7 @@
* @category Libraries
* @author Adam Griffiths
* @link http://adamgriffiths.co.uk
-* @version 2.0.0
+* @version 2.0.3
* @copyright Adam Griffiths 2010
*
* Auth provides a powerful, lightweight and simple interface for user authentication .
@@ -14,24 +14,23 @@
class Application extends CI_Controller
{
- function __construct()
+ public function __construct()
{
parent::__construct();
log_message('debug', 'Application Loaded');
- $this->load->library('form_validation');
- $this->load->library('auth');
- $this->load->helper(array('url', 'auth'));
+ $this->load->library(array('form_validation', 'ag_auth'));
+ $this->load->helper(array('url', 'ag_auth'));
- $this->config->load('auth');
+ $this->config->load('ag_auth');
}
- function field_exists($value)
+ public function field_exists($value)
{
$field_name = (valid_email($value) ? 'email' : 'username');
- $query = $this->db->get_where('users', array($field_name => $value));
+ $query = $this->db->get_where($this->config->item('auth_user_table'), array($field_name => $value));
if($query->num_rows() <> 0)
{
@@ -45,9 +44,9 @@ function field_exists($value)
} // if($this->field_exists($value) === TRUE)
- } // function field_exists($value)
+ } // public function field_exists($value)
- function register()
+ public function register()
{
$this->form_validation->set_rules('username', 'Username', 'required|min_length[6]|callback_field_exists');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|matches[password_conf]');
@@ -79,10 +78,10 @@ function register()
echo('7');
} // if($this->form_validation->run() == FALSE)
- } // function register()
+ } // public function register()
- function login($redirect = NULL)
+ public function login($redirect = NULL)
{
if($redirect === NULL)
View
8 helpers/auth_helper.php
@@ -3,7 +3,7 @@
function logged_in()
{
$CI =& get_instance();
- if($CI->auth->logged_in() == TRUE)
+ if($CI->ag_auth->logged_in() == TRUE)
{
return TRUE;
}
@@ -21,7 +21,7 @@ function user_group($group)
{
$CI =& get_instance();
- $system_group = $CI->auth->config['auth_groups'][$group];
+ $system_group = $CI->ag_auth->config['auth_groups'][$group];
if($system_group === $CI->session->userdata('group_id'))
{
@@ -33,14 +33,14 @@ function user_table()
{
$CI =& get_instance();
- return $CI->auth->user_table;
+ return $CI->ag_auth->user_table;
}
function group_table()
{
$CI =& get_instance();
- return $CI->auth->group_table;
+ return $CI->ag_auth->group_table;
}
?>
View
60 libraries/AG_Auth.php
@@ -6,7 +6,7 @@
* @category Libraries
* @author Adam Griffiths
* @link http://adamgriffiths.co.uk
-* @version 2.0.0
+* @version 2.0.3
* @copyright Adam Griffiths 2010
*
* Auth provides a powerful, lightweight and simple interface for user authentication .
@@ -24,10 +24,10 @@ class AG_Auth
* @author Adam Griffiths
* @param array
*
- * The constructor function loads the libraries dependancies and creates the
+ * The constructor public function loads the libraries dependancies and creates the
* login attempts cookie if it does not already exist.
*/
- function __construct($config)
+ public function __construct($config)
{
log_message('debug', 'Auth Library Loaded');
@@ -93,7 +93,7 @@ public function restrict($group = NULL, $single = NULL)
{
redirect($this->config['auth_incorrect_login'], 'refresh');
}
- } // function restrict()
+ } // public function restrict()
/**
@@ -192,40 +192,28 @@ public function logout()
* @access private
* @param string
*/
- private function _generate()
+ private function _generate()
{
- $username = $this->CI->session->userdata('username');
-
- $rand_url = 'http://random.org/strings/?num=1&len=20&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new';
-
- if (ini_get('allow_url_fopen')) {
- // Grab the random string using the easy version if we can
- $token_source = fopen($rand_url, "r");
- $token = fread($token_source, 20);
- } elseif (function_exists('curl_version')) {
- // No easy version, so try cURL
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $rand_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $token = curl_exec($ch);
- curl_close($ch);
- } else {
- // No love either way, generate a random string ourselves
- $length = 20;
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $token = ”;
- for ($i = 0; $i < $length; $i++) {
- $token .= $characters[mt_rand(0, strlen($characters)-1)];
- }
- }
-
- $identifier = $username . $token;
- $identifier = $this->_salt($identifier);
+ $username = $this->CI->session->userdata('username');
+
+ // No love either way, generate a random string ourselves
+ $length = 20;
+ $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+ $token = "";
+
+ for ($i = 0; $i < $length; $i++)
+ {
+ $token .= $characters[mt_rand(0, strlen($characters)-1)];
+ }
+
+ $identifier = $username . $token;
+ $identifier = $this->_salt($identifier);
- $this->CI->db->query("UPDATE `$this->user_table` SET `identifier` = '$identifier', `token` = '$token' WHERE `username` = '$username'");
+ $this->CI->db->query("UPDATE `$this->user_table` SET `identifier` = '$identifier', `token` = '$token' WHERE `username` = '$username'");
- setcookie("logged_in", $identifier, time()+3600, '/');
- }
+ setcookie("logged_in", $identifier, time()+3600, '/');
+
+ }
/**
@@ -266,7 +254,7 @@ private function _verify_cookie()
* @access private
* @param string
*/
- function view($page, $params = NULL)
+ public function view($page, $params = NULL)
{
if($params !== NULL)
{
View
272 libraries/Auth.php
@@ -1,272 +0,0 @@
-<?php
-/**
-* Authentication Library
-*
-* @package Authentication
-* @category Libraries
-* @author Adam Griffiths
-* @link http://adamgriffiths.co.uk
-* @version 2.0.0
-* @copyright Adam Griffiths 2010
-*
-* Auth provides a powerful, lightweight and simple interface for user authentication .
-*/
-
-ob_start();
-
-class Auth
-{
- var $CI; // The CI object
- var $config; // The config items
-
-
- /**
- * @author Adam Griffiths
- * @param array
- *
- * The constructor function loads the libraries dependancies and creates the
- * login attempts cookie if it does not already exist.
- */
- function __construct($config)
- {
- log_message('debug', 'Auth Library Loaded');
-
- $this->config = $config;
-
- $this->CI =& get_instance();
-
- $this->CI->load->database();
- $this->CI->load->library('session');
- $this->CI->load->helper('email');
-
- $this->CI->load->model('auth_model');
-
- $this->CI->lang->load('auth', 'english');
-
- if($this->logged_in() == FALSE)
- {
- if(!array_key_exists('login_attempts', $_COOKIE))
- {
- setcookie("login_attempts", 0, time()+900, '/');
- }
- }
-
- }
-
-
- /**
- * Restricts access to a page
- *
- * Takes a user level (e.g. admin, user etc) and restricts access to that user and above.
- * Example, users can access a profile page, but so can admins (who are above users)
- *
- * @access public
- * @param string
- * @return bool
- */
- function restrict($group = NULL, $single = NULL)
- {
- if($group === NULL)
- {
- if($this->logged_in() == TRUE)
- {
- return TRUE;
- }
- else
- {
- show_error($this->CI->lang->line('insufficient_privs'));
- }
- }
- elseif($this->logged_in() == TRUE)
- {
- $level = $this->config['auth_groups'][$group];
- $user_level = $this->CI->session->userdata('group');
-
- if($user_level > $level OR $single == TRUE && $user_level !== $level)
- {
- show_error($this->CI->lang->line('insufficient_privs'));
- }
-
- return TRUE;
- }
- else
- {
- redirect($this->config['auth_incorrect_login'], 'refresh');
- }
- } // function restrict()
-
-
- /**
- * @author Adam Griffiths
- * @return bool
- *
- * Checks the session data as to whether or not a user is logged in.
- */
- function logged_in()
- {
- if($this->CI->session->userdata('logged_in') === TRUE)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
-
-
- /**
- * @author Adam Griffiths
- * @param string
- * @return string
- *
- * Uses the encryption key set in application/config/config.php to salt the password passed.
- */
- function salt($password)
- {
- return hash("haval256,5", $this->CI->config->item('encryption_key') . $password);
- }
-
-
- /**
- * @author Adam Griffiths
- * @param string
- * @param string
- * @return string / bool
- *
- * Takes a username & optional username type (email/username) and returns the user data
- */
- function get_user($username, $field_type = 'username')
- {
- $user = $this->CI->auth_model->login_check($username, $field_type);
-
- return $user;
- }
-
-
- /**
- * @author Adam Griffiths
- * @param string
- * @param string
- * @param string
- * @return bool
- *
- * Creates a new user account
- */
- function register($username, $password, $email)
- {
- return $this->CI->auth_model->register($username, $password, $email);
- }
-
-
- /**
- * @author Adam Griffiths
- * @param array
- *
- * Takes the user array, adds the logged_in portion and sets the session data from that.
- */
- function login_user($user)
- {
- $user['logged_in'] = TRUE;
-
- $this->CI->session->set_userdata($user);
- }
-
-
- /**
- * Generate a new token/identifier from random.org
- *
- * @author Adam Griffiths
- * @access private
- * @param string
- */
- private function _generate()
- {
- $username = $this->CI->session->userdata('username');
-
- $rand_url = 'http://random.org/strings/?num=1&len=20&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new';
-
- if (ini_get('allow_url_fopen')) {
- // Grab the random string using the easy version if we can
- $token_source = fopen($rand_url, "r");
- $token = fread($token_source, 20);
- } elseif (function_exists('curl_version')) {
- // No easy version, so try cURL
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $rand_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $token = curl_exec($ch);
- curl_close($ch);
- } else {
- // No love either way, generate a random string ourselves
- $length = 20;
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $token = ”;
- for ($i = 0; $i < $length; $i++) {
- $token .= $characters[mt_rand(0, strlen($characters)-1)];
- }
- }
-
- $identifier = $username . $token;
- $identifier = $this->_salt($identifier);
-
- $this->CI->db->query("UPDATE `$this->user_table` SET `identifier` = '$identifier', `token` = '$token' WHERE `username` = '$username'");
-
- setcookie("logged_in", $identifier, time()+3600, '/');
- }
-
-
- /**
- * Verify that a user has a cookie, if not generate one. If the cookie doesn't match the database, log the user out and show them an error.
- *
- * @access private
- * @param string
- */
- private function _verify_cookie()
- {
- if((array_key_exists('login_attempts', $_COOKIE)) && ($_COOKIE['login_attempts'] <= 5))
- {
- $username = $this->CI->session->userdata('username');
- $userdata = $this->CI->db->query("SELECT * FROM `$this->user_table` WHERE `username` = '$username'");
-
- $result = $userdata->row();
-
- $identifier = $result->username . $result->token;
- $identifier = $this->_salt($identifier);
-
- if($identifier !== $_COOKIE['logged_in'])
- {
- $this->CI->session->sess_destroy();
-
- show_error($this->CI->lang->line('logout_perms_error'));
- }
- }
- else
- {
- $this->_generate();
- }
- }
-
-
- /**
- * Load an auth specific view
- *
- * @access private
- * @param string
- */
- function view($page, $params = NULL)
- {
- if($params !== NULL)
- {
- $data['data'] = $params;
- }
-
- $data['page'] = $page;
- $this->CI->load->view($this->config['auth_views_root'].'index', $data);
- }
-
-
-}
-
-/* End of file: Auth.php */
-/* Location: application/libraries/Auth.php */
View
131 libraries/MY_Controller.php
@@ -1,131 +0,0 @@
-<?php
-/**
-* Authentication Library
-*
-* @package Authentication
-* @category Libraries
-* @author Adam Griffiths
-* @link http://adamgriffiths.co.uk
-* @version 1.0.6
-* @copyright Adam Griffiths 2009
-*
-* Auth provides a powerful, lightweight and simple interface for user authentication
-*/
-
-
-
-class Application extends Controller
-{
-
- var $controllers; // These all track root folders for views, models, controllers; specified in Auth config
- var $models;
- var $views;
-
- function Application()
- {
- parent::Controller();
- $this->load->library(array('auth', 'table'));
- $this->load->database();
- $this->load->helper(array('auth', 'url'));
- $this->config->load('auth');
-
- $tmpl = array (
- 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
-
- 'heading_row_start' => '<tr>',
- 'heading_row_end' => '</tr>',
- 'heading_cell_start' => '<th>',
- 'heading_cell_end' => '</th>',
-
- 'row_start' => '<tr>',
- 'row_end' => '</tr>',
- 'cell_start' => '<td>',
- 'cell_end' => '</td>',
-
- 'row_alt_start' => '<tr class="alt">',
- 'row_alt_end' => '</tr>',
- 'cell_alt_start' => '<td>',
- 'cell_alt_end' => '</td>',
-
- 'table_close' => '</table>'
- );
-
- $this->table->set_template($tmpl);
-
- $this->controllers = $this->config->item('auth_controllers_root');
- $this->models = $this->config->item('auth_models_root');
- $this->views = $this->config->item('auth_views_root');
- }
-
- function login()
- {
- $this->auth->login();
- }
-
- function logout()
- {
- $this->auth->logout();
- }
-
- function register()
- {
- $this->auth->register();
- }
-
- function username_check($str)
- {
-
- $auth_type = $this->auth->_auth_type($str);
-
- $user_table = user_table();
- $query = $this->db->query("SELECT * FROM `$user_table` WHERE `$auth_type` = '$str'");
-
- if($query->num_rows === 1)
- {
- return TRUE;
- }
- else
- {
- $this->form_validation->set_message('username_check', $this->lang->line('username_callback_error'));
- return FALSE;
- }
-
- } // function username_check()
-
- function reg_username_check($str)
- {
- $user_table = user_table();
- $query = $this->db->query("SELECT * FROM `$user_table` WHERE `username` = '$str'");
-
- if($query->num_rows <> 0)
- {
- $this->form_validation->set_message('reg_username_check', $this->lang->line('reg_username_callback_error'));
- return FALSE;
- }
- else
- {
- return TRUE;
- }
-
- } // function reg_username_check()
-
- function reg_email_check($str)
- {
- $user_table = user_table();
- $query = $this->db->query("SELECT * FROM `$user_table` WHERE `email` = '$str'");
-
- if($query->num_rows <> 1)
- {
- return TRUE;
- }
- else
- {
- $this->form_validation->set_message('reg_email_check', $this->lang->line('reg_email_callback_error'));
- return FALSE;
- }
-
- } // function reg_email_check()
-
-}
-
-?>
View
10 models/ag_auth_model.php
@@ -6,7 +6,7 @@
* @category Libraries
* @author Adam Griffiths
* @link http://adamgriffiths.co.uk
-* @version 2.0.0
+* @version 2.0.3
* @copyright Adam Griffiths 2010
*
* Auth provides a powerful, lightweight and simple interface for user authentication .
@@ -17,7 +17,7 @@ class AG_Auth_model extends CI_Model
var $user_table; // The user table (prefix + config)
var $group_table; // The group table (prefix + config)
- function __construct()
+ public function __construct()
{
parent::__construct();
@@ -30,7 +30,7 @@ function __construct()
$this->group_table = $this->config->item('auth_group_table');
}
- function login_check($username, $field_type)
+ public function login_check($username, $field_type)
{
$query = $this->db->get_where($this->user_table, array($field_type => $username));
$result = $query->row_array();
@@ -38,7 +38,7 @@ function login_check($username, $field_type)
return $result;
}
- function register($username, $password, $email)
+ public function register($username, $password, $email)
{
if($this->db->set('username', $username)->set('password', $password)->set('email', $email)->set('group', '100')->insert($this->user_table))
{
@@ -48,7 +48,7 @@ function register($username, $password, $email)
return FALSE;
}
- function field_exists($value)
+ public function field_exists($value)
{
$field_name = (valid_email($value) ? 'email' : 'username');
View
68 models/auth_model.php
@@ -1,68 +0,0 @@
-<?php
-/**
-* Authentication Library
-*
-* @package Authentication
-* @category Libraries
-* @author Adam Griffiths
-* @link http://adamgriffiths.co.uk
-* @version 2.0.0
-* @copyright Adam Griffiths 2010
-*
-* Auth provides a powerful, lightweight and simple interface for user authentication .
-*/
-
-class Auth_model extends CI_Model
-{
- var $user_table; // The user table (prefix + config)
- var $group_table; // The group table (prefix + config)
-
- function __construct()
- {
- parent::__construct();
-
- log_message('debug', 'Auth Model Loaded');
-
- $this->config->load('auth');
- $this->load->database();
-
- $this->user_table = $this->config->item('auth_user_table');
- $this->group_table = $this->config->item('auth_group_table');
- }
-
- function login_check($username, $field_type)
- {
- $query = $this->db->get_where($this->user_table, array($field_type => $username));
- $result = $query->row_array();
-
- return $result;
- }
-
- function register($username, $password, $email)
- {
- if($this->db->set('username', $username)->set('password', $password)->set('email', $email)->set('group', '100')->insert($this->user_table))
- {
- return TRUE;
- }
-
- return FALSE;
- }
-
- function field_exists($value)
- {
-
- $field_name = (valid_email($value) ? 'email' : 'username');
-
- $query = $this->db->get_where($this->user_table, array($field_name => $value));
-
- if($query->num_rows() <> 0)
- {
- return FALSE;
- }
-
- return TRUE;
- }
-}
-
-/* End of file: auth_model.php */
-/* Location: application/models/auth_model.php */
View
32 models/usermodel.php
@@ -1,32 +0,0 @@
-<?php
-
-class Usermodel extends Model
-{
-
- function Usermodel()
- {
- parent::Model();
- }
-
- function users()
- {
- $user_table = user_table();
- $query = $this->db->query("SELECT * FROM `$user_table` ORDER BY `id` ASC");
- return $query->result_array();
- }
-
- function delete($id)
- {
- $user_table = user_table();
- $this->db->query("DELETE FROM `$user_table` WHERE `id` = '$id'");
- }
-
- function edit($id)
- {
- $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[40]|callback_username_check');
- $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[12]');
- $this->form_validation->set_rules('remember', 'Remember Me');
- }
-}
-
-?>
Please sign in to comment.
Something went wrong with that request. Please try again.