Blame Automation/Php/Webenv/admin/includes/functions/auth.php

Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
/**
Alain Reguera Delgado 8f60cb
 * Authentication and authorization
Alain Reguera Delgado 8f60cb
 *
Alain Reguera Delgado 8f60cb
 * @category   Logic
Alain Reguera Delgado 8f60cb
 * @package    CentOS-News
Alain Reguera Delgado 8f60cb
 * @author     Alain Reguera Delgado <alain.reguera@gmail.com>
Alain Reguera Delgado 8f60cb
 * @copyright  2009 - CentOS Artwork SIG.
Alain Reguera Delgado 8f60cb
 * @license    GPL
Alain Reguera Delgado 8f60cb
 */
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
//--------------Authentication stuff--------------
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    session_start();
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
//--------------/* Verify Admin access rights  */
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    function check_adminaccess()
Alain Reguera Delgado 8f60cb
    {
Alain Reguera Delgado 8f60cb
        /* Verify session */
Alain Reguera Delgado 8f60cb
        if (!isset($_SESSION['employeetype']))
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            header('Location: '. BASEURL .'admin/login.php');
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
    }
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    /* Check User Access */
Alain Reguera Delgado 8f60cb
    function check_useraccess()
Alain Reguera Delgado 8f60cb
    {
Alain Reguera Delgado 8f60cb
        $timeout = 60 * 30; // In seconds, i.e. 30 minutes.
Alain Reguera Delgado 8f60cb
        $fingerprint = md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
Alain Reguera Delgado 8f60cb
        $redirect_to = BASEURL . 'admin/login.php?loggedout=true';
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Destroy session if ... */
Alain Reguera Delgado 8f60cb
        if (isset($_SESSION['last_active']) && $_SESSION['last_active'] < (time()-$timeout)
Alain Reguera Delgado 8f60cb
           || (isset($_SESSION['fingerprint']) && $_SESSION['fingerprint']!=$fingerprint)
Alain Reguera Delgado 8f60cb
           || isset($_GET['action']) && $_GET['action'] == 'logout') 
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            setcookie(session_name(), '', time()-3600, '/');
Alain Reguera Delgado 8f60cb
            session_destroy();
Alain Reguera Delgado 8f60cb
            header("Location: $redirect_to");
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Regenerate session */
Alain Reguera Delgado 8f60cb
        session_regenerate_id(); 
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Increase session lifetime */
Alain Reguera Delgado 8f60cb
        $_SESSION['last_active'] = time();
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Rebuild session fingerprint */
Alain Reguera Delgado 8f60cb
        $_SESSION['fingerprint'] = $fingerprint;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    }
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    /* Verify username and password */
Alain Reguera Delgado 8f60cb
    function login()
Alain Reguera Delgado 8f60cb
    {
Alain Reguera Delgado 8f60cb
        require_once(ABSPATH . 'admin/includes/classes/ldap.php');
Alain Reguera Delgado 8f60cb
        $ldap = new LDAP;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Inicialize variables */
Alain Reguera Delgado 8f60cb
        $login = array();
Alain Reguera Delgado 8f60cb
        $login['username'] = '';
Alain Reguera Delgado 8f60cb
        $login['password'] = '';
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Validate username input */ 
Alain Reguera Delgado 8f60cb
        if (isset($_POST['username']))
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            $mail_pattern = '/^([a-z0-9+_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,6}$/';
Alain Reguera Delgado 8f60cb
            if (preg_match( $mail_pattern,$_POST['username']))
Alain Reguera Delgado 8f60cb
            {
Alain Reguera Delgado 8f60cb
                $login['username'] = $_POST['username'];
Alain Reguera Delgado 8f60cb
            }
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Validate password input */
Alain Reguera Delgado 8f60cb
        if (isset($_POST['password']))
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            $login['password'] = $ldap->prepare_userpassword($_POST['password']);
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
 
Alain Reguera Delgado 8f60cb
        /* Query LDAP directory looking for username AND password */
Alain Reguera Delgado 8f60cb
        $search = $ldap->get_entries('(&(uid=' . $login['username']  . ')(&(userpassword=' . $login['password'] . ')))');
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        /* Build user's session if match */
Alain Reguera Delgado 8f60cb
        if ($search['count'] == 1)
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            /* Set session information */
Alain Reguera Delgado 8f60cb
            $_SESSION['uid']            = $search[0]['uid'][0];
Alain Reguera Delgado 8f60cb
            $_SESSION['cn']             = $search[0]['cn'][0];
Alain Reguera Delgado 8f60cb
            $_SESSION['employeetype']   = $search[0]['employeetype'][0];
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            /* Set session lasttime access */
Alain Reguera Delgado 8f60cb
            $_SESSION['last_active'] = time();
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            /* Set session fingerprint */
Alain Reguera Delgado 8f60cb
            $fingerprint = md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
Alain Reguera Delgado 8f60cb
            $_SESSION['fingerprint'] = $fingerprint;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            /* Redirect to frontpage */
Alain Reguera Delgado 8f60cb
            header("Location: " . BASEURL);
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            return 0;
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
        else if ($search['count'] > 1)
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            // Login Failed: There are duplicates in the ldap directory database
Alain Reguera Delgado 8f60cb
            return 002;
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
        else
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            // Login Failed: There is no coincidece in the search
Alain Reguera Delgado 8f60cb
            return '001';
Alain Reguera Delgado 8f60cb
        }
Alain Reguera Delgado 8f60cb
    }
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    // User links
Alain Reguera Delgado 8f60cb
    function get_auth_userlinks()
Alain Reguera Delgado 8f60cb
    {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        $html = '
    ' . "\n";
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        if (isset($_SESSION['cn'])) 
Alain Reguera Delgado 8f60cb
        {
Alain Reguera Delgado 8f60cb
            $html .= '
  • ' . $_SESSION['cn'] . ' (' . ucfirst(translate("logout")) . ')
  • ' . "\n";
    Alain Reguera Delgado 8f60cb
                $html .= '
  • ' . ucfirst(translate("admin")) . '
  • ' . "\n";
    Alain Reguera Delgado 8f60cb
            }
    Alain Reguera Delgado 8f60cb
            else
    Alain Reguera Delgado 8f60cb
            {
    Alain Reguera Delgado 8f60cb
                $html .= '
  • ' . ucfirst(translate("login")) . '
  • ' . "\n";
    Alain Reguera Delgado 8f60cb
            }
    Alain Reguera Delgado 8f60cb
    Alain Reguera Delgado 8f60cb
            $html .= '' . "\n";
    Alain Reguera Delgado 8f60cb
    Alain Reguera Delgado 8f60cb
            return $html;
    Alain Reguera Delgado 8f60cb
    Alain Reguera Delgado 8f60cb
        }
    Alain Reguera Delgado 8f60cb
    Alain Reguera Delgado 8f60cb
    ?>