Advertisement
Guest User

Werxe Auth

a guest
Mar 21st, 2012
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 49.51 KB | None | 0 0
  1. <?php
  2. /*
  3. | Werxe_auth
  4. |
  5. | LICENSE
  6. |
  7. | This source file is subject to the GPL license that is bundled
  8. | with this package in the file LICENSE.txt.
  9. |
  10. | @package    Werxe_auth
  11. | @copyright  Copyright (c) 2012, Bruno Gaspar @ Werxe.net
  12. | @license    http://www.gnu.org/licenses/gpl.txt     GPL
  13. */
  14.  
  15.  
  16. /*
  17. | This is an Authentication and ACL library, a 2-in-1 package.
  18. |
  19. | @author     Bruno Gaspar @ Werxe.net <bruno.gaspar@werxe.net>
  20. | @version    1.0 2012-04-15
  21. */
  22. class Werxe_auth
  23. {
  24.     /*
  25.     |---------------------------------------------------------------------
  26.     | Variables.
  27.     |---------------------------------------------------------------------
  28.     */
  29.     public $userID     = 0;         // Stores the ID of the current user.
  30.     public $userPerms  = array();   // Stores the permissions of the current user.
  31.     public $userRoles  = array();   // Stores the roles of the current user.
  32.     public $tables     = array();   // Stores the name of the database tables.
  33.     public $message    = false;     // Stores a success message.
  34.     public $error      = false;     // Stores a unsuccessful message.
  35.     public $messages   = array();   // Stores success messages.
  36.     public $errors     = array();   // Stores unsuccessful messages.
  37.     /* end */
  38.  
  39.  
  40.     /*
  41.     |---------------------------------------------------------------------
  42.     | Function: __construct()
  43.     |---------------------------------------------------------------------
  44.     |
  45.     |   Initializer.
  46.     |
  47.     | @access   public
  48.     | @return   void
  49.     | @author   g l a z z
  50.     */
  51.     public function __construct()
  52.     {
  53.         // Instantiate CodeIgniter.
  54.         //
  55.         $this->CI =& get_instance();
  56.  
  57.         // Load the needed libraries and helpers.
  58.         //
  59.         $this->CI->load->library('email');
  60.         $this->CI->load->library('encrypt');
  61.         $this->CI->load->library('session');
  62.         $this->CI->load->helper('cookie');
  63.         $this->CI->load->helper('language');
  64.  
  65.         // Load the language file.
  66.         //
  67.         $this->CI->lang->load('werxe_auth');
  68.  
  69.         // Load the config file.
  70.         //
  71.         $this->CI->load->config('werxe_auth');
  72.  
  73.         // Assign config variables.
  74.         //
  75.         $this->tables = $this->CI->config->item('tables');
  76.  
  77.         // Is the auto login feature enabled and we are not logged in ?
  78.         //
  79.         if ( ! $this->logged_in() && config_item('auto_login') && get_cookie('email') && get_cookie('remember_code') ):
  80.             // We have the data, try to login.
  81.             //
  82.             $this->remembered_user();
  83.         endif;
  84.  
  85.         // Check if the user is logged in.
  86.         //
  87.         if ( $this->logged_in() ):
  88.             // Get all the needed user information.
  89.             //
  90.             if ( ! $this->user() ):
  91.                 // The user was not found, or cookie/session was invalid.
  92.                 //
  93.                 redirect();
  94.             endif;
  95.         endif;
  96.     }
  97.     /* end */
  98.  
  99.  
  100.     /*
  101.     |---------------------------------------------------------------------
  102.     | Function: login()
  103.     |---------------------------------------------------------------------
  104.     |
  105.     |   Login function.
  106.     |
  107.     | @access   public
  108.     | @param    string
  109.     | @param    string
  110.     | @param    boolean
  111.     | @return   boolean  or  integer
  112.     | @author   g l a z z
  113.     */
  114.     public function login($email = null, $password = null, $remember = false)
  115.     {
  116.         // Sanitaze the passed data.
  117.         //
  118.         $email    = $this->sanitize( $email );
  119.         $password = $this->sanitize( $password );
  120.         $remember = (bool) $remember;
  121.  
  122.         // Do we have the login credentials ?
  123.         //
  124.         if ( ! $email or ! $password ):
  125.             // Set the error message.
  126.             //
  127.             $this->set_error('login_unsuccessful');
  128.  
  129.             // We are done here.
  130.             //
  131.             return false;
  132.         endif;
  133.  
  134.         // Query the database to see if we find the user.
  135.         //
  136.         $qry = $this->CI->db->where('email', $email)->limit(1)->get( $this->tables['users'] );
  137.  
  138.         // Check if the user exists.
  139.         //
  140.         if( $qry->num_rows() == 1 ):
  141.             // Get user information.
  142.             //
  143.             $user = $qry->row();
  144.  
  145.             // Is this user activated ?
  146.             //
  147.             if ( $user->status ):
  148.                 // Validate the password.
  149.                 //
  150.                 if ( $this->check_password($user->password, $password) ):
  151.                     // Update last login.
  152.                     //
  153.                     $this->updateLastLogin($user->id);
  154.  
  155.                     // Save the session.
  156.                     //
  157.                     $session_data = array(
  158.                         'email'   => $user->email,
  159.                         'id'      => $user->id,
  160.                         'uid'     => $user->id,
  161.                         'user_id' => $user->id
  162.                     );
  163.                     $this->CI->session->set_userdata($session_data);
  164.  
  165.                     // Remember login details for next visit ?
  166.                     //
  167.                     if ( $remember && config_item('auto_login') ):
  168.                         // Remember the user.
  169.                         //
  170.                         $this->remember_user($user->id);
  171.                     endif;
  172.  
  173.                     // Set the success message.
  174.                     //
  175.                     $this->set_message('login_successful', null, true);
  176.  
  177.                     // We are done here, return the user id.
  178.                     //
  179.                     return $user->id;
  180.                 else:
  181.                     // Set the error message.
  182.                     //
  183.                     $this->set_error('login_unsuccessful');
  184.  
  185.                     // We are done here.
  186.                     //
  187.                     return false;
  188.                 endif;
  189.  
  190.             // Nope, this user account is not activated.
  191.             //
  192.             else:
  193.                 // Set the error message.
  194.                 //
  195.                 $this->set_error('login_unsuccessful_not_active');
  196.  
  197.                 // We are done here.
  198.                 //
  199.                 return false;
  200.             endif;
  201.  
  202.         // This user account doesn't seem to exist.
  203.         //
  204.         else:
  205.             // Set the error message.
  206.             //
  207.             $this->set_error('login_unsuccessful');
  208.  
  209.             // We are done here.
  210.             //
  211.             return false;
  212.         endif;
  213.     }
  214.     /* end */
  215.  
  216.  
  217.     /*
  218.     |---------------------------------------------------------------------
  219.     | Function: logout()
  220.     |---------------------------------------------------------------------
  221.     |
  222.     |   Logout function.
  223.     |
  224.     | @access   public
  225.     | @return   boolean
  226.     | @author   g l a z z
  227.     */
  228.     public function logout()
  229.     {
  230.         // Clean up the session.
  231.         //
  232.         $sessions = array(
  233.             'email'   => '' ,
  234.             'id'      => '' ,
  235.             'uid'     => '' ,
  236.             'user_id' => ''
  237.         );
  238.         $this->CI->session->unset_userdata( $sessions );
  239.  
  240.         // Delete the remember me cookies, if they exist.
  241.         //
  242.         if ( get_cookie('email') or get_cookie('remember_code') ):
  243.             delete_cookie('email');
  244.             delete_cookie('remember_code');
  245.         endif;
  246.  
  247.         // Destroy the session.
  248.         //
  249.         $this->CI->session->sess_destroy();
  250.  
  251.         // Set the success message.
  252.         //
  253.         $this->set_message('logout_successful', null, true);
  254.  
  255.         // We are done here.
  256.         //
  257.         return true;
  258.     }
  259.     /* end */
  260.  
  261.  
  262.     /*
  263.     |---------------------------------------------------------------------
  264.     | Function: register()
  265.     |---------------------------------------------------------------------
  266.     |
  267.     |   Register function.
  268.     |
  269.     | @access   public
  270.     | @param    array
  271.     | @return   boolean
  272.     | @author   g l a z z
  273.     */
  274.     public function register($params = array())
  275.     {
  276.         // Sanitize the parameters.
  277.         //
  278.         $sanitized = array();
  279.         foreach($params as $key => $value):
  280.             $sanitized[ $key ] = $this->sanitize( $value );
  281.         endforeach;
  282.  
  283.         // Do we have the $email and $password ?
  284.         //
  285.         if ( ! $sanitized['email'] or ! $sanitized['password'] ):
  286.             // Set the success message.
  287.             //
  288.             $this->set_error('account_creation_unsuccessful');
  289.  
  290.             // We are done here.
  291.             //
  292.             return false;
  293.         endif;
  294.  
  295.         // Check if email is already registered.
  296.         //
  297.         if ( $this->check_email($sanitized['email']) ):
  298.             // Set the error message.
  299.             //
  300.             $this->set_error('account_creation_duplicate_email');
  301.  
  302.             // We are done here.
  303.             //
  304.             return false;
  305.         endif;
  306.  
  307.         // Hash the password.
  308.         //
  309.         $sanitized['password'] = $this->hash($sanitized['password']);
  310.  
  311.         // Add more data to the sanitized array.
  312.         //
  313.         $sanitized['ip_address'] = sprintf( '%u', ip2long( $this->CI->input->ip_address() ) );
  314.         $sanitized['date_added'] = time();
  315.         $sanitized['status']     = 1;
  316.  
  317.         // Create the new user.
  318.         //
  319.         $this->CI->db->insert( $this->tables['users'], $sanitized );
  320.  
  321.         // If the user was created with success.
  322.         //
  323.         if ( $userID = $this->CI->db->insert_id() ):
  324.             // Update this new user roles, most likely insert them into his record.
  325.             //
  326.             if ( ! array_key_exists('customer_roles', $sanitized )):
  327.                 $this->userUpdateRolesByName( $userID, $this->CI->config->item('default_roles') );
  328.             else:
  329.                 $this->userUpdateRolesByID( $userID, $sanitized['customer_roles'] );
  330.             endif;
  331.  
  332.             // Do we need to send an activation email ?
  333.             //
  334.             if ( $this->CI->config->item('email_activation') ):
  335.                 // Deactivate the new user and get an activation code.
  336.                 //
  337.                 $activation_code = $this->deactivate($userID);
  338.  
  339.                 // Prepare the data for the activation email.
  340.                 //
  341.                 $data = array(
  342.                     'id'         => $userID,
  343.                     'email'      => $sanitized['email'],
  344.                     'activation' => $activation_code,
  345.                 );
  346.  
  347.                 // Prepare the email message.
  348.                 //
  349.                 $message = $this->CI->load->view('auth/emails/activate', $data, true);
  350.  
  351.                 // Prepare the email library.
  352.                 //
  353.                 $this->CI->email->clear();
  354.                 $this->CI->email->set_newline( "\r\n" );
  355.                 $this->CI->email->from( $this->CI->config->item('admin_email'), $this->CI->config->item('email_title') );
  356.                 $this->CI->email->to( $sanitized['email'] );
  357.                 $this->CI->email->subject( $this->CI->config->item('email_title') . ' - Account Activation' );
  358.                 $this->CI->email->message( $message );
  359.  
  360.                 // Was the email sended with success ?
  361.                 //
  362.                 if ($this->CI->email->send() == TRUE):
  363.                     // Set the success message.
  364.                     //
  365.                     $this->set_message('activation_email_successful');
  366.                 else:
  367.                     // Set the error message.
  368.                     //
  369.                     $this->set_error('activation_email_unsuccessful');
  370.                 endif;
  371.  
  372.             // No activation email needed.
  373.             //
  374.             else:
  375.                 // Set the success message.
  376.                 //
  377.                 $this->set_message('account_creation_successful');
  378.             endif;
  379.  
  380.             // We are done here, return the user id.
  381.             //
  382.             return $userID;
  383.  
  384.         // The user was not created.
  385.         //
  386.         else:
  387.             // Set the success message.
  388.             //
  389.             $this->set_error('account_creation_unsuccessful');
  390.  
  391.             // We are done here.
  392.             //
  393.             return false;
  394.         endif;
  395.     }
  396.     /* end */
  397.  
  398.     /*
  399.     |---------------------------------------------------------------------
  400.     | Function: remember_user()
  401.     |---------------------------------------------------------------------
  402.     |
  403.     |   This function is used to remember a user login details.
  404.     |
  405.     | @access   private
  406.     | @param    integer
  407.     | @return   boolean
  408.     | @author   g l a z z
  409.     */
  410.     private function remember_user( $userID = null )
  411.     {
  412.         // Don't have the userID ?
  413.         //
  414.         if( $userID === null ):
  415.             // Use the id of the current logged in user.
  416.             //
  417.             $userID = $this->userID;
  418.         endif;
  419.  
  420.         // Get some information about this user.
  421.         //
  422.         $user = $this->CI->db->where('id', $userID)->get( $this->tables['users'] )->row();
  423.  
  424.         // Create a new remember code
  425.         //
  426.         $remember_code = sha1( md5( microtime() ) );
  427.  
  428.         // Update the user record.
  429.         //
  430.         $this->CI->db->where('id', $userID)->update( $this->tables['users'], array('remember_code' => $remember_code) );
  431.  
  432.         // Was the user updated with success ?
  433.         //
  434.         if ( $this->CI->db->affected_rows() > -1 ):
  435.             // Set the cookies.
  436.             //
  437.             # Email:
  438.             #
  439.             set_cookie(array(
  440.                 'name'   => 'email',
  441.                 'value'  => $user->email,
  442.                 'expire' => $this->CI->config->item('user_expire'),
  443.             ));
  444.  
  445.             # Remember code:
  446.             #
  447.             set_cookie(array(
  448.                 'name'   => 'remember_code',
  449.                 'value'  => $remember_code,
  450.                 'expire' => $this->CI->config->item('user_expire'),
  451.             ));
  452.  
  453.             // We are done here.
  454.             //
  455.             return true;
  456.         endif;
  457.  
  458.         // We are done here.
  459.         //
  460.         return false;
  461.     }
  462.     /* end */
  463.  
  464.  
  465.     /*
  466.     |---------------------------------------------------------------------
  467.     | Function: remembered_user()
  468.     |---------------------------------------------------------------------
  469.     |
  470.     |   Function to remember users automatically.
  471.     |
  472.     | @access   public
  473.     | @return   boolean
  474.     | @author   g l a z z
  475.     */
  476.     public function remembered_user()
  477.     {
  478.         // Check for valid data.
  479.         //
  480.         if ( ! get_cookie('email') or ! get_cookie('remember_code') ):
  481.             // We are done here.
  482.             //
  483.             return false;
  484.         endif;
  485.  
  486.         // Get some user information.
  487.         //
  488.         $qry = $this->CI->db
  489.                     ->select('email, id')
  490.                     ->where('email', get_cookie('email'))
  491.                     ->where('remember_code', get_cookie('remember_code'))
  492.                     ->limit(1)
  493.                     ->get($this->tables['users']);
  494.  
  495.         // If the user was found, sign them in.
  496.         //
  497.         if ( $qry->num_rows() == 1 ):
  498.             // Get information
  499.             //
  500.             $user = $qry->row();
  501.  
  502.             // Update this user last login.
  503.             //
  504.             $this->updateLastLogin($user->id);
  505.  
  506.             // Save the session.
  507.             //
  508.             $session_data = array(
  509.                 'email'   => $user->email,
  510.                 'id'      => $user->id,
  511.                 'uid'     => $user->id,
  512.                 'user_id' => $user->id,
  513.             );
  514.             $this->CI->session->set_userdata($session_data);
  515.  
  516.             // Extend the users cookies.
  517.             //
  518.             $this->remember_user($user->id);
  519.  
  520.             // We are done here.
  521.             //
  522.             return true;
  523.         endif;
  524.  
  525.         // No user found, we are done here.
  526.         //
  527.         return false;
  528.     }
  529.     /* end */
  530.  
  531.  
  532.     /*
  533.     |---------------------------------------------------------------------
  534.     | Function: user()
  535.     |---------------------------------------------------------------------
  536.     |
  537.     |   This function is used to get all the user information.
  538.     |   It gets:
  539.     |       - Personal information;
  540.     |       - Roles;
  541.     |       - Permissions;
  542.     |
  543.     | @access   public
  544.     | @param    integer
  545.     | @return   boolean
  546.     | @author   g l a z z
  547.     */
  548.     public function user( $userID = null )
  549.     {
  550.         // No user id passed ?
  551.         //
  552.         if ( ! $userID ):
  553.             // Okay, get user id from the session.
  554.             //
  555.             $this->userID = $userID = $this->CI->session->userdata('user_id');
  556.         endif;
  557.  
  558.         // User email.
  559.         //
  560.         $email = $this->CI->session->userdata('email');
  561.  
  562.         // Get this user personal information.
  563.         //
  564.         if ( $this->user = $this->CI->db->where('id', $userID)->where('email', $email)->get('customers')->row() ):
  565.             // Get this users roles.
  566.             //
  567.             $this->userRoles = $this->getUserRoles( $userID );
  568.  
  569.             // Get both roles permissions and user permissions.
  570.             //
  571.             $this->userPerms = $this->getRolePerms();
  572.             $this->userPerms = array_merge( $this->userPerms, $this->getUserPerms( $userID ) );
  573.  
  574.             // User found.
  575.             //
  576.             return true;
  577.         else:
  578.             // Delete cookies and sessions, just to make sure.
  579.             //
  580.             $this->logout();
  581.  
  582.             // User not found.
  583.             //
  584.             return false;
  585.         endif;
  586.     }
  587.     /* end */
  588.  
  589.  
  590.     /*
  591.     |---------------------------------------------------------------------
  592.     | Function: check_email()
  593.     |---------------------------------------------------------------------
  594.     |
  595.     |   This function is used to check if an email is already registered.
  596.     |
  597.     | @access   public
  598.     | @param    string
  599.     | @return   integer
  600.     | @author   g l a z z
  601.     */
  602.     public function check_email($email)
  603.     {
  604.         return $this->CI->db->where('email', $email)->count_all_results( $this->tables['users'] ) > 0;
  605.     }
  606.     /* end */
  607.  
  608.  
  609.     /*
  610.     |---------------------------------------------------------------------
  611.     | Function: logged_in()
  612.     |---------------------------------------------------------------------
  613.     |
  614.     |   This function checks if a user is logged id.
  615.     |
  616.     | @access   public
  617.     | @return   boolean
  618.     | @author   g l a z z
  619.     */
  620.     public function logged_in()
  621.     {
  622.         // Set the user id.
  623.         //
  624.         $this->userID = (bool) $this->CI->session->userdata( 'user_id' );
  625.  
  626.         // Return True / false.
  627.         //
  628.         return (bool) $this->CI->session->userdata( 'email' );
  629.     }
  630.     /* end */
  631.  
  632.  
  633.     /*
  634.     |---------------------------------------------------------------------
  635.     | Function: updateLastLogin()
  636.     |---------------------------------------------------------------------
  637.     |
  638.     |   This function is used to update the last login of a user.
  639.     |
  640.     | @access   public
  641.     | @param    integer
  642.     | @return   boolean
  643.     | @author   g l a z z
  644.     */
  645.     public function updateLastLogin( $userID = null )
  646.     {
  647.         // Don't have the userID ?
  648.         //
  649.         if( $userID === null ):
  650.             // Use the id of the current logged in user.
  651.             //
  652.             $userID = $this->userID;
  653.         endif;
  654.  
  655.         // Get the ip address.
  656.         //
  657.         $ip_address = $this->CI->input->ip_address();
  658.  
  659.         // Update the user record.
  660.         //
  661.         $data = array(
  662.             'last_login' => time(),
  663.             'ip_address' => sprintf('%u', ip2long( $ip_address ) )
  664.         );
  665.         $this->CI->db->where('id', $userID)->update( $this->tables['users'], $data );
  666.  
  667.         // Insert a new record on the logins table.
  668.         //
  669.         $data = array(
  670.             'userID'     => $userID,
  671.             'ip_address' => sprintf('%u', ip2long( $ip_address ) ),
  672.             'date'       => time()
  673.         );
  674.         $this->CI->db->insert( $this->tables['users_logins'], $data);
  675.  
  676.         // We are done here.
  677.         //
  678.         return $this->CI->db->affected_rows() == 1;
  679.     }
  680.     /* end */
  681.  
  682.  
  683.     /*
  684.     |---------------------------------------------------------------------
  685.     | Function: getUserRoles()
  686.     |---------------------------------------------------------------------
  687.     |
  688.     |   This function gets all the user roles.
  689.     |
  690.     | @access   public
  691.     | @param    integer
  692.     | @return   array
  693.     | @author   g l a z z
  694.     */
  695.     public function getUserRoles( $userID = null)
  696.     {
  697.         // Don't have the userID ?
  698.         //
  699.         if( $userID === null ):
  700.             // Use the id of the current logged in user.
  701.             //
  702.             $userID = $this->userID;
  703.         endif;
  704.  
  705.         // Initiate an empty array.
  706.         //
  707.         $return = array();
  708.  
  709.         // Get the user roles.
  710.         //
  711.         $roles = $this->CI->db
  712.             ->select('u_roles.*, roles.roleName')
  713.             ->from($this->tables['users_roles'] . ' as u_roles')
  714.             ->join('roles', 'roles.id = u_roles.roleID', 'left')
  715.             ->where('u_roles.userID', $userID)
  716.             ->order_by('u_roles.roleOrder', 'ASC')
  717.             ->get()->result();
  718.  
  719.         // Loop through the records.
  720.         //
  721.         foreach($roles as $role):
  722.             $return[ $role->roleID ] = array(
  723.                 'roleID'   => $role->roleID,
  724.                 'userID'   => $role->userID,
  725.                 'addDate'  => $role->addDate,
  726.                 'roleName' => $role->roleName
  727.             );     
  728.         endforeach;
  729.  
  730.         // Return the user roles.
  731.         //
  732.         return $return;
  733.     }
  734.     /* end */
  735.  
  736.  
  737.     /*
  738.     |---------------------------------------------------------------------
  739.     | Function: getUserPerms()
  740.     |---------------------------------------------------------------------
  741.     |
  742.     |   This function gets the permissions from a user.
  743.     |
  744.     | @access   public
  745.     | @param    integer
  746.     | @return   array
  747.     | @author   g l a z z
  748.     */
  749.     public function getUserPerms( $userID = null )
  750.     {
  751.         // Don't have the userID ?
  752.         //
  753.         if( $userID === null ):
  754.             // Use the id of the current logged in user.
  755.             //
  756.             $userID = $this->userID;
  757.         endif;
  758.  
  759.         // Initiate an empty array.
  760.         //
  761.         $return = array();
  762.  
  763.         // Get the permissions.
  764.         //
  765.         $permissions = $this->CI->db
  766.             ->select('u_perms.*, permissions.permKey, permissions.permName')
  767.             ->from($this->tables['users_perms'] . ' as u_perms')
  768.             ->join('permissions', 'permissions.id = u_perms.permID', 'LEFT')
  769.             ->where('u_perms.userID', $userID)
  770.             ->order_by('u_perms.addDate', 'ASC')
  771.             ->get()->result();
  772.  
  773.         // Loop through the records.
  774.         //
  775.         foreach($permissions as $perm):
  776.             $return[ $perm->permKey ] = array(
  777.                 'perm'       => $perm->permKey,
  778.                 'inheritted' => ( ($perm->value == '1') ? true : false ),
  779.                 'value'      => ( ($perm->value == '1') ? true : false ),
  780.                 'name'       => $perm->permName,
  781.                 'id'         => $perm->permID
  782.             );
  783.         endforeach;
  784.  
  785.         // Return the permissions.
  786.         //
  787.         return $return;
  788.     }
  789.     /* end */
  790.  
  791.  
  792.     /*
  793.     |---------------------------------------------------------------------
  794.     | Function: getAllRoles()
  795.     |---------------------------------------------------------------------
  796.     |
  797.     |   This function gets all the roles from the database.
  798.     |
  799.     | @access   public
  800.     | @return   array
  801.     | @author   g l a z z
  802.     */
  803.     public function getAllRoles()
  804.     {
  805.         // Return the roles.
  806.         //
  807.         return $this->CI->db
  808.             ->select('roles.*, COUNT(ur.roleID) as usersActive')
  809.             ->join( $this->tables['users_roles'] . ' as ur', 'ur.roleID = roles.id', 'left')
  810.             ->order_by('roles.order', 'ASC')
  811.             ->group_by('roles.id')
  812.             ->get('roles')->result();
  813.     }
  814.     /* end */
  815.  
  816.  
  817.     /*
  818.     |---------------------------------------------------------------------
  819.     | Function: getAllPerms()
  820.     |---------------------------------------------------------------------
  821.     |
  822.     |   This function gets all the permissions from the database.
  823.     |
  824.     | @access   public
  825.     | @return   array
  826.     | @author   g l a z z
  827.     */
  828.     public function getAllPerms()
  829.     {
  830.         // Initiate an empty array.
  831.         //
  832.         $return = array();
  833.  
  834.         // Get the permissions.
  835.         //
  836.         return $perms = $this->CI->db->order_by('permName', 'ASC')->get('permissions')->result();
  837.  
  838.         // Loop through the records.
  839.         //
  840.         foreach($perms as $perm):
  841.             $return[ $perm->permKey ] = array(
  842.                 'id'       => $perm->id,
  843.                 'permName' => $perm->permName,
  844.                 'permKey'  => $perm->permKey
  845.             );
  846.         endforeach;
  847.  
  848.         // Return the permissions.
  849.         //
  850.         return $return;
  851.     }
  852.     /* end */
  853.  
  854.  
  855.     /*
  856.     |---------------------------------------------------------------------
  857.     | Function: getRolePerms()
  858.     |---------------------------------------------------------------------
  859.     |
  860.     |   This function gets the permissions from a role or from roles.
  861.     |
  862.     | @access   public
  863.     | @param    integer | array
  864.     | @return   array
  865.     | @author   g l a z z
  866.     */
  867.     public function getRolePerms( $role = null )
  868.     {
  869.         // We dont have a role passed ?
  870.         //
  871.         if ( ! $role ):
  872.             $role = $this->userRoles;
  873.         endif;
  874.  
  875.         // Initiate an empty array.
  876.         //
  877.         $perms = array();
  878.  
  879.         // Multiple roles ?
  880.         //
  881.         if ( is_array($role) ):
  882.             /*
  883.             // Loop the roles, and get the role permissions.
  884.             //
  885.             foreach($role as $r):
  886.                 // Role ID.
  887.                 //
  888.                 $roleID = $r['roleID'];
  889.  
  890.                 // Get the permissions of this role.
  891.                 //
  892.                 $teste = $this->CI->db
  893.                     ->select('acl_roles_perms.*, permissions.permKey, permissions.permName')
  894.                     ->join('permissions', 'permissions.id = acl_roles_perms.permID', 'LEFT')
  895.                     ->where('acl_roles_perms.roleID', $roleID )
  896.                     //->order_by('acl_roles_perms.roleK', 'DESC')
  897.                     ->get('acl_roles_perms')->result();
  898.  
  899.                 $lol[ $roleID ] = $teste;
  900.             endforeach;
  901.  
  902.             // array_reverse
  903.  
  904.             $lol = array_reverse($lol, true);
  905.             $perms = array();
  906.             foreach($lol as $roleID => $role):
  907.                 foreach($role as $r):
  908.                 $perms[ $r->permKey ] = array(
  909.                     'perm'       => $r->permKey,
  910.                     'inheritted' => true,
  911.                     'value'      => ( ($r->value == '1') ? true : false ),
  912.                     'name'       => $r->permName,
  913.                     'id'         => $r->permID
  914.                 );
  915.                 endforeach;
  916.             endforeach;
  917.  
  918.             return $perms;
  919.             */
  920.            
  921.             // Get the roles id.
  922.             //
  923.             $roles_id = array();
  924.             foreach($role as $r):
  925.                 $roles_id[] = $r['roleID'];
  926.             endforeach;
  927.  
  928.             // Get the roles permissions.
  929.             //
  930.             $roles = $this->CI->db
  931.                 ->select('acl_roles_perms.*, permissions.permKey, permissions.permName')
  932.                 ->join('permissions', 'permissions.id = acl_roles_perms.permID', 'LEFT')
  933.                 ->where_in('acl_roles_perms.roleID', $roles_id )
  934.                 //->order_by('acl_roles_perms.p', 'DESC')
  935.                 ->get('acl_roles_perms')->result();
  936.  
  937.  
  938.         // No, just only one role.
  939.         //
  940.         else:
  941.             // Get the roles permissions.
  942.             //
  943.             $roles = $this->CI->db
  944.                 ->select('acl_roles_perms.*, permissions.permKey, permissions.permName')
  945.                 ->join('permissions', 'permissions.id = acl_roles_perms.permID', 'LEFT')
  946.                 ->where('acl_roles_perms.roleID', $role)
  947.                 //->order_by('acl_roles_perms.value', 'DESC')
  948.                 ->get('acl_roles_perms')->result();
  949.         endif;
  950.  
  951.         // Loop through the records.
  952.         //
  953.         $perms = array();
  954.         foreach($roles as $roleID => $role):
  955.             //echo $roleID . ' - ' . $role->permKey . '<br>';
  956.             $perms[ $role->permKey ] = array(
  957.                 'perm'       => $role->permKey,
  958. /*            */'inheritted' => true,
  959.                 'value'      => $role->value,
  960.                 'name'       => $role->permName,
  961.                 'id'         => $role->permID
  962.             );
  963.         endforeach;
  964.  
  965.         // Return the permissions.
  966.         //
  967.         return $perms;
  968.     }
  969.     /* end */
  970.  
  971.  
  972.     /*
  973.     |---------------------------------------------------------------------
  974.     | Function: createRole()
  975.     |---------------------------------------------------------------------
  976.     |
  977.     |   This function create a new role.
  978.     |
  979.     | @access   public
  980.     | @param    array
  981.     | @return   boolean
  982.     | @author   g l a z z
  983.     */
  984.     public function createRole( $params )
  985.     {
  986.         // Sanitize the parameters.
  987.         //
  988.         $roleName = $this->sanitize( $params['roleName'] );
  989.         $roleDesc = $this->sanitize( $params['roleDesc'] );
  990.  
  991.         // Create the roleKey.
  992.         //
  993.         $roleKey = $this->makeSlug( $roleName );
  994.  
  995.  
  996.         // Check if values are empty.
  997.         //
  998.         if ( empty($roleKey) or empty($roleName) ):
  999.             // Set the error message.
  1000.             //
  1001.             $this->set_error('role_create_unsuccessful');
  1002.  
  1003.             // We are done here.
  1004.             //
  1005.             return false;
  1006.         endif;
  1007.  
  1008.         // Get the last added role, so we can get the order value.
  1009.         //
  1010.         $qry = $this->CI->db->select('order')->order_by('order',' DESC')->get('roles')->row();
  1011.         $order = $qry->order + 10;
  1012.  
  1013.         // Create the new role.
  1014.         //
  1015.         if ( $this->CI->db->insert('roles', array( 'roleKey' => $roleKey, 'roleName' => $roleName, 'roleDesc' => $roleDesc, 'addDate' => time(), 'order' => $order ) ) ):
  1016.  
  1017.             // Get the new Role ID.
  1018.             //
  1019.             $roleID = $this->CI->db->insert_id();
  1020.  
  1021.             // Update the permissions of this role.
  1022.             //
  1023.             foreach ( $params['rolePerms'] as $permID => $value ):
  1024.                 // Insert the role permission.
  1025.                 //
  1026.                 $this->CI->db->query('INSERT INTO `acl_roles_perms` SET `roleID` = ?, `permID` = ?, `value` = ?, `addDate` = ? ON DUPLICATE KEY UPDATE `value`=`value`', array($roleID, $permID, $value, time() ) );
  1027.             endforeach;
  1028.  
  1029.             // Set the success message.
  1030.             //
  1031.             $this->set_message('role_create_successful');
  1032.  
  1033.             // We are done here.
  1034.             //
  1035.             return true;
  1036.         else:
  1037.             // Set the error message.
  1038.             //
  1039.             $this->set_error('role_create_duplicate');
  1040.  
  1041.             // We are done here.
  1042.             //
  1043.             return false;
  1044.         endif;
  1045.     }
  1046.     /* end */
  1047.  
  1048.  
  1049.     /*
  1050.     |---------------------------------------------------------------------
  1051.     | Function: editRole()
  1052.     |---------------------------------------------------------------------
  1053.     |
  1054.     |   This function updates a specific role.
  1055.     |
  1056.     | @access   public
  1057.     | @param    integer
  1058.     | @param    array
  1059.     | @return   boolean
  1060.     | @author   g l a z z
  1061.     */
  1062.     public function editRole( $roleID = null, $params )
  1063.     {
  1064.         // No role id ?
  1065.         //
  1066.         if ( ! $roleID ):
  1067.             // We are done here.
  1068.             //
  1069.             return false;
  1070.         endif;
  1071.  
  1072.         // Sanitize the parameters.
  1073.         //
  1074.         $roleName = $this->sanitize( $params['roleName'] );
  1075.         $roleDesc = $this->sanitize( $params['roleDesc'] );
  1076.  
  1077.         // Create the roleKey.
  1078.         //
  1079.         $roleKey = $this->makeSlug( $roleName );
  1080.  
  1081.         // Check if values are empty.
  1082.         //
  1083.         if ( empty($roleKey) or empty($roleName) ):
  1084.             // Set the success message.
  1085.             //
  1086.             $this->set_error('role_update_unsuccessful');
  1087.  
  1088.             // We are done here.
  1089.             //
  1090.             return false;
  1091.         endif;
  1092.  
  1093.         // Update the role record.
  1094.         //
  1095.         $this->CI->db->where('id', $roleID)->update('roles', array( 'roleKey' => $roleKey, 'roleName' => $roleName, 'roleDesc' => $roleDesc ) );
  1096.  
  1097.         // Update the permissions of this role.
  1098.         //
  1099.         foreach ( $params['rolePerms'] as $permID => $value ):
  1100.             // Are we update the role permission.
  1101.             //
  1102.             $this->CI->db->query('INSERT INTO `acl_roles_perms` SET `roleID` = ?, `permID` = ?, `value` = ?, `addDate` = ? ON DUPLICATE KEY UPDATE `value`= ?', array($roleID, $permID, $value, time(), $value ) );
  1103.         endforeach;
  1104.  
  1105.         // Set the success message.
  1106.         //
  1107.         $this->set_message('role_update_successful', null, true);
  1108.  
  1109.         // We are done here.
  1110.         //
  1111.         return true;
  1112.     }
  1113.     /* end */
  1114.  
  1115.  
  1116.     /*
  1117.     |---------------------------------------------------------------------
  1118.     | Function: deleteRole()
  1119.     |---------------------------------------------------------------------
  1120.     |
  1121.     |   This function deletes a specific role.
  1122.     |
  1123.     | @access   public
  1124.     | @param    integer
  1125.     | @return   boolean
  1126.     | @author   g l a z z
  1127.     */
  1128.     public function deleteRole( $roleID = null )
  1129.     {
  1130.         // Does the user have permission to delete roles ?
  1131.         //
  1132.         if ( ! $this->hasPermission('roles_delete') ):
  1133.             // Set the error message.
  1134.             //
  1135.             $this->set_error('role_crud_no_permission', 'global:delete', true);
  1136.  
  1137.             // We are done here.
  1138.             //
  1139.             return false;
  1140.         endif;
  1141.  
  1142.         // No role id ?
  1143.         //
  1144.         if ( ! $roleID ):
  1145.             // We are done here.
  1146.             //
  1147.             return false;
  1148.         endif;
  1149.  
  1150.         // Is this role protected ?
  1151.         //
  1152.         if ( in_array($roleID, $this->CI->config->item('protected_roles')) ):
  1153.             // Set the error message on the session, so we can redirect the user.
  1154.             //
  1155.             $this->set_error('role_delete_protected', null, true);
  1156.  
  1157.             // We are done here.
  1158.             //
  1159.             return false;
  1160.         endif;
  1161.  
  1162.         // Delete all the records related to this role.
  1163.         //
  1164.         $this->CI->db->where( 'id'     , $roleID )->delete( 'roles' );
  1165.         $this->CI->db->where( 'roleID' , $roleID )->delete( 'acl_roles_perms' );
  1166.         $this->CI->db->where( 'roleID' , $roleID )->delete( $this->tables['users_roles'] );
  1167.  
  1168.         // Set the success message.
  1169.         //
  1170.         $this->set_message('role_delete_successful');
  1171.  
  1172.         // We are done here.
  1173.         //
  1174.         return true;
  1175.     }
  1176.     /* end */
  1177.  
  1178.  
  1179.     /*
  1180.     |---------------------------------------------------------------------
  1181.     | Function: roleExists()
  1182.     |---------------------------------------------------------------------
  1183.     |
  1184.     |   This function checks if a role really exists on the database.
  1185.     |
  1186.     | @access   public
  1187.     | @param    integer
  1188.     | @return   boolean
  1189.     | @author   g l a z z
  1190.     */
  1191.     public function roleExists( $roleID = null )
  1192.     {
  1193.         // No role id ?
  1194.         //
  1195.         if ( ! $roleID ):
  1196.             // We are done here.
  1197.             //
  1198.             return false;
  1199.         endif;
  1200.  
  1201.         // Check if role exists.
  1202.         //
  1203.         return (bool) $this->CI->db->where('id', $roleID)->get('roles')->num_rows;
  1204.     }
  1205.     /* end */
  1206.  
  1207.  
  1208.     /*
  1209.     |---------------------------------------------------------------------
  1210.     | Function: orderRoleByID()
  1211.     |---------------------------------------------------------------------
  1212.     |
  1213.     |   This function orders a role.
  1214.     |
  1215.     | @access   public
  1216.     | @param    integer
  1217.     | @param    string
  1218.     | @return   boolean
  1219.     | @author   g l a z z
  1220.     */
  1221.     public function orderRoleByID($roleID, $order)
  1222.     {
  1223.         // Update the role order.
  1224.         //
  1225.         if( $order == 'up'):
  1226.             $this->CI->db->query('UPDATE `roles` SET `order`=`order`-15 WHERE `id`=?', array($roleID) );
  1227.         else:
  1228.             $this->CI->db->query('UPDATE `roles` SET `order`=`order`+15 WHERE `id`=?', array($roleID) );
  1229.         endif;
  1230.  
  1231.         // Update all the other roles order.
  1232.         //
  1233.         $order = 10;
  1234.         foreach($this->CI->db->order_by('order', 'ASC')->get('roles')->result() as $role):
  1235.             $this->CI->db->where('id', $role->id)->update('roles', array('order' => $order) );
  1236.             $order += 10;
  1237.         endforeach;
  1238.  
  1239.         // We are done here.
  1240.         //
  1241.         return true;
  1242.     }
  1243.     /* end */
  1244.  
  1245.  
  1246.     /*
  1247.     |---------------------------------------------------------------------
  1248.     | Function: createPerm()
  1249.     |---------------------------------------------------------------------
  1250.     |
  1251.     |   This function creates a new permission.
  1252.     |
  1253.     | @access   public
  1254.     | @param    array
  1255.     | @return   boolean
  1256.     | @author   g l a z z
  1257.     */
  1258.     public function createPerm( $params )
  1259.     {
  1260.         // Create the new permission.
  1261.         //
  1262.         if ( $this->CI->db->insert('permissions', array( 'permKey'  => $params['permKey'], 'permName' => $params['permName'], 'addDate' => time() ) ) ):
  1263.  
  1264.             // Get the new Permission ID.
  1265.             //
  1266.             return $this->CI->db->insert_id();
  1267.         else:
  1268.             // We are done here.
  1269.             //
  1270.             return false;
  1271.         endif;
  1272.     }
  1273.     /* end */
  1274.  
  1275.  
  1276.     /*
  1277.     |---------------------------------------------------------------------
  1278.     | Function: editPerm()
  1279.     |---------------------------------------------------------------------
  1280.     |
  1281.     |   This function updates a specific permission.
  1282.     |
  1283.     | @access   public
  1284.     | @param    integer
  1285.     | @param    array
  1286.     | @return   boolean
  1287.     | @author   g l a z z
  1288.     */
  1289.     public function editPerm( $permID = null, $params )
  1290.     {
  1291.         // No perm id ?
  1292.         //
  1293.         if ( ! $permID ):
  1294.             // We are done here.
  1295.             //
  1296.             return false;
  1297.         endif;
  1298.  
  1299.         // Update the role record.
  1300.         //
  1301.         $this->CI->db->where('id', $permID)->update('permissions', array( 'permKey'  => $params['permKey'], 'permName' => $params['permName'] ) );
  1302.  
  1303.         // We are done here.
  1304.         //
  1305.         return true;
  1306.     }
  1307.     /* end */
  1308.  
  1309.  
  1310.     /*
  1311.     |---------------------------------------------------------------------
  1312.     | Function: deletePerm()
  1313.     |---------------------------------------------------------------------
  1314.     |
  1315.     |   This function deletes a specific permission.
  1316.     |
  1317.     | @access   public
  1318.     | @param    integer
  1319.     | @return   boolean
  1320.     | @author   g l a z z
  1321.     */
  1322.     public function deletePerm( $permID = null )
  1323.     {
  1324.         // No perm id ?
  1325.         //
  1326.         if ( ! $permID ):
  1327.             // We are done here.
  1328.             //
  1329.             return false;
  1330.         endif;
  1331.  
  1332.         // Delete all the records related to this permission.
  1333.         //
  1334.         $this->CI->db->where( 'id' , $permID )->delete( 'permissions' );
  1335.         $this->CI->db->where( 'permID' , $permID )->delete( 'acl_roles_perms' );
  1336.         $this->CI->db->where( 'permID' , $permID )->delete( $this->tables['users_perms'] );
  1337.  
  1338.         // We are done here.
  1339.         //
  1340.         return true;
  1341.     }
  1342.     /* end */
  1343.  
  1344.  
  1345.     /*
  1346.     |---------------------------------------------------------------------
  1347.     | Function: hasRole()
  1348.     |---------------------------------------------------------------------
  1349.     |
  1350.     |   This function checks if a user has a certain role.
  1351.     |
  1352.     | @access   public
  1353.     | @param    integer
  1354.     | @return   boolean
  1355.     | @author   g l a z z
  1356.     */
  1357.     public function hasRole( $roleID )
  1358.     {
  1359.         // Loop through the user roles.
  1360.         //
  1361.         foreach($this->userRoles as $role):
  1362.             // Check if we have that role id.
  1363.             //
  1364.             if ( $role['roleID'] === $roleID ):
  1365.                 // We do have.
  1366.                 //
  1367.                 return true;
  1368.             endif;
  1369.         endforeach;
  1370.  
  1371.         // We are done here.
  1372.         //
  1373.         return false;
  1374.     }
  1375.     /* end */
  1376.  
  1377.  
  1378.     /*
  1379.     |---------------------------------------------------------------------
  1380.     | Function: hasPermission()
  1381.     |---------------------------------------------------------------------
  1382.     |
  1383.     |   This function checks if a user has a certain permission key.
  1384.     |
  1385.     | @access   public
  1386.     | @param    string
  1387.     | @return   boolean
  1388.     | @author   g l a z z
  1389.     */
  1390.     public function hasPermission( $permKey )
  1391.     {
  1392.         // Make tne permkey lowercase.
  1393.         //
  1394.         $permKey = strtolower( $permKey );
  1395.  
  1396.         // Check if we have permission or not.
  1397.         //
  1398.         if ( array_key_exists( $permKey, $this->userPerms ) ):
  1399.             if ( $this->userPerms[$permKey]['value'] === '1' || $this->userPerms[$permKey]['value'] === true ):
  1400.                 // We have access :)
  1401.                 //
  1402.                 return true;
  1403.             else:
  1404.                 // No access, we are done here.
  1405.                 //
  1406.                 return false;
  1407.             endif;
  1408.         else:
  1409.             // No access, we are done here.
  1410.             //
  1411.             return false;
  1412.         endif;
  1413.     }
  1414.     /* end */
  1415.  
  1416.  
  1417.     /*
  1418.     |---------------------------------------------------------------------
  1419.     | Function: getPermKeyFromID()
  1420.     |---------------------------------------------------------------------
  1421.     |
  1422.     |   This function gets a permission key.
  1423.     |
  1424.     | @access   public
  1425.     | @param    integer
  1426.     | @return   string
  1427.     | @author   g l a z z
  1428.     */
  1429.     public function getPermKeyFromID( $permID )
  1430.     {
  1431.         // Query the database.
  1432.         //
  1433.         $perm = $this->CI->db->select('permKey')->where('id', $permID)->limit(1)->get('permissions')->row();
  1434.  
  1435.         // Return the permission key.
  1436.         //
  1437.         return $perm->permKey;
  1438.     }
  1439.     /* end */
  1440.  
  1441.  
  1442.     /*
  1443.     |---------------------------------------------------------------------
  1444.     | Function: getPermNameFromID()
  1445.     |---------------------------------------------------------------------
  1446.     |
  1447.     |   This function gets a permission name.
  1448.     |
  1449.     | @access   public
  1450.     | @param    integer
  1451.     | @return   string
  1452.     | @author   g l a z z
  1453.     */
  1454.     public function getPermNameFromID( $permID )
  1455.     {
  1456.         // Query the database.
  1457.         //
  1458.         $perm = $this->CI->db->select('permName')->where('id', $permID)->limit(1)->get('permissions')->row();
  1459.  
  1460.         // Return the permission name.
  1461.         //
  1462.         return $perm->permName;
  1463.     }
  1464.     /* end */
  1465.  
  1466.  
  1467.     /*
  1468.     |---------------------------------------------------------------------
  1469.     | Function: getRoleInfoFromID()
  1470.     |---------------------------------------------------------------------
  1471.     |
  1472.     |   This function gets a role name.
  1473.     |
  1474.     | @access   public
  1475.     | @param    string
  1476.     | @return   boolean
  1477.     | @author   g l a z z
  1478.     */
  1479.     public function getRoleInfoFromID( $roleID )
  1480.     {
  1481.         // Query the database.
  1482.         //
  1483.         return $this->CI->db->where('id', $roleID)->limit(1)->get('roles')->row();
  1484.     }
  1485.     /* end */
  1486.  
  1487.  
  1488.     /*
  1489.     |---------------------------------------------------------------------
  1490.     | Function: getPermInfoFromID()
  1491.     |---------------------------------------------------------------------
  1492.     |
  1493.     |   This function gets a role name.
  1494.     |
  1495.     | @access   public
  1496.     | @param    string
  1497.     | @return   boolean
  1498.     | @author   g l a z z
  1499.     */
  1500.     public function getPermInfoFromID( $permID )
  1501.     {
  1502.         // Query the database.
  1503.         //
  1504.         return $this->CI->db->where('id', $permID)->limit(1)->get('permissions')->row();
  1505.     }
  1506.     /* end */
  1507.  
  1508.  
  1509.     /*
  1510.     |---------------------------------------------------------------------
  1511.     | Function: userUpdateRolesByID()
  1512.     |---------------------------------------------------------------------
  1513.     |
  1514.     |   This function updates the users roles giving its id's.
  1515.     |
  1516.     | @access   public
  1517.     | @param    integer
  1518.     | @param    array
  1519.     | @return   boolean
  1520.     | @author   g l a z z
  1521.     */
  1522.     public function userUpdateRolesByID( $userID = null, $roles )
  1523.     {
  1524.         echo 'userUpdateRolesByID();';
  1525.         die;
  1526.         // Don't have the userID ?
  1527.         //
  1528.         if( $userID === null ):
  1529.             // Use the id of the current logged in user.
  1530.             //
  1531.             $userID = $this->userID;
  1532.         endif;
  1533.  
  1534.         // Check if there are any roles.
  1535.         //
  1536.         if ( ! is_array( $roles ) ):
  1537.             // No roles, we are done here.
  1538.             //
  1539.             return false;
  1540.         endif;
  1541.  
  1542.         // Loop through the roles.
  1543.         //
  1544.         foreach( $roles as $roleID => $value ):
  1545.             // We want to delete or update ?
  1546.             //
  1547.             if ( $value == '0' ):
  1548.                 $this->CI->db->where('userID', $userID)->where('roleID', $roleID)->delete( $this->tables['users_roles'] );
  1549.             else:
  1550.                 $this->CI->db->query('REPLACE INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ?', array( $userID, $roleID, time() ) );
  1551.             endif;
  1552.         endforeach;
  1553.  
  1554.         // We are done here.
  1555.         //
  1556.         return true;
  1557.     }
  1558.     /* end */
  1559.  
  1560.  
  1561.     /*
  1562.     |---------------------------------------------------------------------
  1563.     | Function: userUpdateRolesByName()
  1564.     |---------------------------------------------------------------------
  1565.     |
  1566.     |   This function updates the users roles giving its id's.
  1567.     |
  1568.     | @access   public
  1569.     | @param    integer
  1570.     | @param    array
  1571.     | @return   boolean
  1572.     | @author   g l a z z
  1573.     */
  1574.     public function userUpdateRolesByName( $userID = null, $roles)
  1575.     {
  1576.         // Don't have the userID ?
  1577.         //
  1578.         if( $userID === null ):
  1579.             // Use the id of the current logged in user.
  1580.             //
  1581.             $userID = $this->userID;
  1582.         endif;
  1583.  
  1584.         // Check if there are any roles.
  1585.         //
  1586.         if ( ! is_array( $roles ) ):
  1587.             // No roles, we are done here.
  1588.             //
  1589.             return false;
  1590.         endif;
  1591.  
  1592.         // Loop through the roles.
  1593.         //
  1594.         foreach( $roles as $roleName ):
  1595.             // Get the ID of this role.
  1596.             //
  1597.             $roleID = $this->getRoleIDbyName( $roleName );
  1598.            
  1599.             //$this->CI->db->query('REPLACE INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ?', array( $userID, $roleID, time() ) );
  1600.             $this->CI->db->query('INSERT INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ? ON DUPLICATE KEY UPDATE `roleID`=`roleID`', array($userID, $roleID, time() ) );
  1601.         endforeach;
  1602.  
  1603.         // Loop through the roles.
  1604.         //
  1605.         /*
  1606.         foreach( $roles as $roleID => $value ):
  1607.             // We want to delete or update ?
  1608.             //
  1609.             if ( $value == '0' ):
  1610.                 $this->CI->db->where('userID', $userID)->where('roleID', $roleID)->delete( $this->tables['users_roles'] );
  1611.             else:
  1612.                 $this->CI->db->query('REPLACE INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ?', array( $userID, $roleID, date("Y-m-d H:i:s") ) );
  1613.             endif;
  1614.         endforeach;
  1615.         */
  1616.  
  1617.         // We are done here.
  1618.         //
  1619.         return true;
  1620.     }
  1621.  
  1622.  
  1623.     /*
  1624.     |---------------------------------------------------------------------
  1625.     | Function: userUpdatePermissions()
  1626.     |---------------------------------------------------------------------
  1627.     |
  1628.     |   This function updates the users permissions.
  1629.     |
  1630.     | @access   public
  1631.     | @param    integer
  1632.     | @param    array
  1633.     | @return   boolean
  1634.     | @author   g l a z z
  1635.     */
  1636.     public function userUpdatePermissions( $userID = null, $permissions )
  1637.     {
  1638.         // Don't have the userID ?
  1639.         //
  1640.         if( $userID === null ):
  1641.             // Use the id of the current logged in user.
  1642.             //
  1643.             $userID = $this->userID;
  1644.         endif;
  1645.  
  1646.         // Check if there are any roles.
  1647.         //
  1648.         if ( ! is_array( $permissions ) ):
  1649.             // No roles, we are done here.
  1650.             //
  1651.             return false;
  1652.         endif;
  1653.  
  1654.         // Loop through the roles.
  1655.         //
  1656.         foreach( $permissions as $permID => $value ):
  1657.             // We want to delete or update ?
  1658.             //
  1659.             if ( $value == 'i' ):
  1660.                 $this->CI->db->where('userID', $userID)->where('permID', $permID)->delete('user_perms');
  1661.             else:
  1662.                 $this->CI->db->query('REPLACE INTO `user_perms` SET `userID` = ?, `permID` = ?, `value`= ?, `addDate` = ?', array( $userID, $permID, $value, time() ) );
  1663.             endif;
  1664.         endforeach;
  1665.  
  1666.         // We are done here.
  1667.         //
  1668.         return true;
  1669.     }
  1670.     /* end */
  1671.  
  1672.  
  1673.     /*
  1674.     |---------------------------------------------------------------------
  1675.     | Function: getUserLogins()
  1676.     |---------------------------------------------------------------------
  1677.     |
  1678.     |   This function some successfull logins that a user have made.
  1679.     |
  1680.     | @access   public
  1681.     | @param    integer
  1682.     | @param    integer
  1683.     | @return   string
  1684.     | @author   g l a z z
  1685.     */
  1686.     public function getUserLogins($userID = null, $logins = 10)
  1687.     {
  1688.         // Don't have the userID ?
  1689.         //
  1690.         if( $userID === null ):
  1691.             // Use the id of the current logged in user.
  1692.             //
  1693.             $userID = $this->userID;
  1694.         endif;
  1695.  
  1696.         // Return the user logins.
  1697.         //
  1698.         return $this->CI->db->where('userID', $userID)->order_by('date', 'DESC')->limit($logins)->get($this->tables['users_logins'])->result();
  1699.     }
  1700.     /* end */
  1701.  
  1702.  
  1703.     /*
  1704.     |---------------------------------------------------------------------
  1705.     | Function: getCountryByID()
  1706.     |---------------------------------------------------------------------
  1707.     |
  1708.     |   This function returns the name of a country.
  1709.     |
  1710.     | @access   public
  1711.     | @param    integer
  1712.     | @return   string
  1713.     | @author   g l a z z
  1714.     */
  1715.     public function getCountryByID( $countryID )
  1716.     {
  1717.         // Query the database.
  1718.         //
  1719.         $qry = $this->CI->db->where('id', $countryID)->get('countries')->row();
  1720.  
  1721.         // Return the name of the country.
  1722.         //
  1723.         return $qry->name;
  1724.     }
  1725.     /* end */
  1726.  
  1727.  
  1728.     /*
  1729.     |---------------------------------------------------------------------
  1730.     | Function: getRoleIDbyName()
  1731.     |---------------------------------------------------------------------
  1732.     |
  1733.     |   This function returns the id of a role.
  1734.     |
  1735.     | @access   public
  1736.     | @param    integer
  1737.     | @return   string
  1738.     | @author   g l a z z
  1739.     */
  1740.     public function getRoleIDbyName( $roleName )
  1741.     {
  1742.         // Query the database.
  1743.         //
  1744.         $role = $this->CI->db->where('roleName', $roleName)->get('roles')->row();
  1745.  
  1746.         // Return the RoleID.
  1747.         //
  1748.         return $role->id;
  1749.     }
  1750.     /* end */
  1751.  
  1752.  
  1753.     /*
  1754.     |---------------------------------------------------------------------
  1755.     | Function: makeSlug()
  1756.     |---------------------------------------------------------------------
  1757.     |
  1758.     |   This is used to create more userfriendly url's.
  1759.     |
  1760.     | @access   public
  1761.     | @param    string
  1762.     | @param    string
  1763.     | @return   string
  1764.     | @author   g l a z z
  1765.     */
  1766.     public function makeSlug($str, $delimiter = '_')
  1767.     {
  1768.         // Do some work on the string.
  1769.         //
  1770.         $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  1771.         $str = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
  1772.         $str = preg_replace("/[\/_|+ -]+/", $delimiter, $str);
  1773.         $str = strtolower(trim($str, '-'));
  1774.  
  1775.         // Return the string.
  1776.         //
  1777.         return $str;
  1778.     }
  1779.     /* end */
  1780.  
  1781.  
  1782.     /*
  1783.     |---------------------------------------------------------------------
  1784.     | Function: sanitize()
  1785.     |---------------------------------------------------------------------
  1786.     |
  1787.     |   This function sanitizes some string.
  1788.     |
  1789.     | @access   public
  1790.     | @param    string
  1791.     | @return   string
  1792.     | @author   g l a z z
  1793.     */
  1794.     public function sanitize( $str )
  1795.     {
  1796.         // We have an array ? No problem...
  1797.         //
  1798.         if ( is_array( $str ) ):
  1799.             // Loop through the array.
  1800.             //
  1801.             foreach ( $str as $s):
  1802.                 // Sanitize this string.
  1803.                 $this->sanitize($s);
  1804.             endforeach;
  1805.  
  1806.             // We are done here.
  1807.             //
  1808.             return true;
  1809.         endif;
  1810.  
  1811.         // Sanitize the string.
  1812.         //
  1813.         $str = ini_get( 'magic_quotes_gpc' ) ? stripslashes( $str ) : $str;
  1814.         $str = strip_tags( $str );
  1815.         $str = trim( $str );
  1816.         $str = htmlspecialchars( $str );
  1817.         //$str = mysql_real_escape_string( $str );
  1818.  
  1819.         // Return the sanitized string.
  1820.         //
  1821.         return $str;
  1822.     }
  1823.     /* end */
  1824.  
  1825.  
  1826.     /*
  1827.     |---------------------------------------------------------------------
  1828.     | Function: salt()
  1829.     |---------------------------------------------------------------------
  1830.     |
  1831.     |   Generates a new salt.
  1832.     |
  1833.     | @access   private
  1834.     | @param    integer
  1835.     | @return   string
  1836.     | @author   g l a z z
  1837.     */
  1838.     private static function salt( $length = 22 )
  1839.     {
  1840.         // Return the new salt.
  1841.         //
  1842.         return substr( sha1( mt_rand() ), 0, $length );
  1843.     }
  1844.     /* end */
  1845.  
  1846.  
  1847.     /*
  1848.     |---------------------------------------------------------------------
  1849.     | Function: hash()
  1850.     |---------------------------------------------------------------------
  1851.     |
  1852.     |   Generates the hashed password.
  1853.     |
  1854.     | @access   public
  1855.     | @param    string
  1856.     | @return   string
  1857.     | @author   g l a z z
  1858.     */
  1859.     public static function hash( $password )
  1860.     {
  1861.         // Return the hashed password.
  1862.         //
  1863.         return crypt($password, '$2a$10$' . self::salt());
  1864.     }
  1865.     /* end */
  1866.  
  1867.  
  1868.     /*
  1869.     |---------------------------------------------------------------------
  1870.     | Function: check_password()
  1871.     |---------------------------------------------------------------------
  1872.     |
  1873.     |   This will be used to compare a password against a hash.
  1874.     |
  1875.     | @access   private
  1876.     | @param    string
  1877.     | @param    string
  1878.     | @return   boolean
  1879.     | @author   g l a z z
  1880.     */
  1881.     public function check_password( $hash, $password )
  1882.     {
  1883.         // Get the salt of this password.
  1884.         //
  1885.         $full_salt = substr( $hash, 0, 29 );
  1886.  
  1887.         // Obtain a new hash.
  1888.         //
  1889.         $new_hash = crypt( $password, $full_salt );
  1890.  
  1891.         // Compare both hash's and return true or false.
  1892.         //
  1893.         return (bool) ( $hash == $new_hash );
  1894.     }
  1895.     /* end */
  1896.  
  1897.  
  1898.     /*
  1899.     |---------------------------------------------------------------------
  1900.     | Function: set_error()
  1901.     |---------------------------------------------------------------------
  1902.     |
  1903.     |   Function to set error messages.
  1904.     |
  1905.     | @access   public
  1906.     | @param    string
  1907.     | @param    string | array
  1908.     | @param    boolean
  1909.     | @return   string
  1910.     | @author   g l a z z
  1911.     */
  1912.     public function set_error($line, $args = null, $set_flashdata = false)
  1913.     {
  1914.         // Prepare the line.
  1915.         //
  1916.         $line = lang($line);
  1917.  
  1918.         // We have arguments ?
  1919.         //
  1920.         if ( $args ):
  1921.             // Do we have only one argument ?
  1922.             //
  1923.             if ( ! is_array($args) ):
  1924.                 // Set the error.
  1925.                 //
  1926.                 $this->error = sprintf( $line, lang($args) );
  1927.  
  1928.             // Nope, we have multiple arguments.
  1929.             //
  1930.             else:
  1931.                 // Loop through the arguments.
  1932.                 //
  1933.                 $arr = array();
  1934.                 foreach($args as $arg):
  1935.                     $arr[] = lang( $arg );
  1936.                 endforeach;
  1937.  
  1938.                 // Set the error.
  1939.                 //
  1940.                 $this->error = vsprintf($line, $arr);
  1941.             endif;
  1942.  
  1943.         // No arguments passed.
  1944.         //
  1945.         else:
  1946.             $this->error = $line;
  1947.         endif;
  1948.  
  1949.         // Set flashdata, just if the user gets redirected :)
  1950.         //
  1951.         if ( $set_flashdata ):
  1952.             $this->CI->session->set_flashdata('error', $this->error);
  1953.         endif;
  1954.  
  1955.         // Return the error message.
  1956.         //
  1957.         return $this->show_error();
  1958.     }
  1959.     /* end */
  1960.  
  1961.  
  1962.     /*
  1963.     |---------------------------------------------------------------------
  1964.     | Function: set_message()
  1965.     |---------------------------------------------------------------------
  1966.     |
  1967.     |   Function to set success messages.
  1968.     |
  1969.     | @access   public
  1970.     | @param    string
  1971.     | @param    string | array
  1972.     | @param    boolean
  1973.     | @return   string
  1974.     | @author   g l a z z
  1975.     */
  1976.     public function set_message($line, $args = null, $set_flashdata = false)
  1977.     {
  1978.         // Prepare the line.
  1979.         //
  1980.         $line = lang($line);
  1981.  
  1982.         // We have arguments ?
  1983.         //
  1984.         if ( $args ):
  1985.             // Do we have only one argument ?
  1986.             //
  1987.             if ( ! is_array($args) ):
  1988.                 // Set the message.
  1989.                 //
  1990.                 $this->message = sprintf( $line, lang($args) );
  1991.  
  1992.             // Nope, we have multiple arguments.
  1993.             //
  1994.             else:
  1995.                 // Loop through the arguments.
  1996.                 //
  1997.                 $arr = array();
  1998.                 foreach($args as $arg):
  1999.                     $arr[] = lang( $arg );
  2000.                 endforeach;
  2001.  
  2002.                 // Set the message.
  2003.                 //
  2004.                 $this->message = vsprintf($line, $arr);
  2005.             endif;
  2006.  
  2007.         // No arguments passed.
  2008.         //
  2009.         else:
  2010.             $this->message = $line;
  2011.         endif;
  2012.  
  2013.         // Set flashdata, just if the user gets redirected :)
  2014.         //
  2015.         if ( $set_flashdata ):
  2016.             $this->CI->session->set_flashdata('message', $this->message);
  2017.         endif;
  2018.  
  2019.         // Return the success message.
  2020.         //
  2021.         return $this->show_message();
  2022.     }
  2023.     /* end */
  2024.  
  2025.  
  2026.     /*
  2027.     |---------------------------------------------------------------------
  2028.     | Function: show_error()
  2029.     |---------------------------------------------------------------------
  2030.     |
  2031.     |   Function to return a error message or multiple error messages.
  2032.     |
  2033.     | @access   public
  2034.     | @param    string
  2035.     | @return   string
  2036.     | @author   g l a z z
  2037.     */
  2038.     public function show_error()
  2039.     {
  2040.         // Single error ?
  2041.         //
  2042.         if ( $this->error ):
  2043.             return $this->error;
  2044.  
  2045.         // Do we have Session Flashdata ?
  2046.         //
  2047.         elseif ( $this->CI->session->flashdata('error') ):
  2048.             return $this->CI->session->flashdata('error');
  2049.         endif;
  2050.  
  2051.         // No errors, we are done here.
  2052.         //
  2053.         return false;
  2054.     }
  2055.     /* end */
  2056.  
  2057.  
  2058.     /*
  2059.     |---------------------------------------------------------------------
  2060.     | Function: show_message()
  2061.     |---------------------------------------------------------------------
  2062.     |
  2063.     |   Function to return a success message.
  2064.     |
  2065.     | @access   public
  2066.     | @param    string
  2067.     | @return   string
  2068.     | @author   g l a z z
  2069.     */
  2070.     public function show_message()
  2071.     {
  2072.         // Single success message ?
  2073.         //
  2074.         if ( $this->message ):
  2075.             return $this->message;
  2076.  
  2077.         // Do we have Session Flashdata ?
  2078.         //
  2079.         elseif ( $this->CI->session->flashdata('message') ):
  2080.             return $this->CI->session->flashdata('message');
  2081.         endif;
  2082.  
  2083.         // No success messages, we are done here.
  2084.         //
  2085.         return false;
  2086.     }
  2087.     /* end */
  2088. }
  2089. /* end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement