<commit_before><?php

/**
 *  ocs-webserver
 *
 *  Copyright 2016 by pling GmbH.
 *
 *    This file is part of ocs-webserver.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
class Local_Auth_Adapter_Ocs implements Local_Auth_Adapter_Interface
{

    const MD5 = 'enc01';
    const SHA = 'enc02';
    const PASSWORDSALT = '<PASSWORD>';
    const USER_DEAVIVATED = '_deactivated';

    protected $_db;
    protected $_tableName;
    protected $_identity;
    protected $_credential;
    protected $_encryption;
    protected $_resultRow;

    /**
     * __construct() - Sets configuration options
     *
     * @param Zend_Db_Adapter_Abstract $dbAdapter If null, default database adapter assumed
     * @param string                   $tableName
     *
     * @throws Zend_Auth_Adapter_Exception
     */
    public function __construct(Zend_Db_Adapter_Abstract $dbAdapter = null, $tableName = null)
    {
        $this->_db = $dbAdapter;
        if (empty($this->_db)) {
            $this->_db = Zend_Db_Table_Abstract::getDefaultAdapter();
            if (empty($this->_db)) {
                throw new Zend_Auth_Adapter_Exception('No database adapter present');
            }
        }

        $this->_tableName = $tableName;
    }

    public static function getEncryptedPassword($password, $passwordType)
    {
        return $passwordType == Default_Model_DbTable_Member::PASSWORD_TYPE_HIVE ? sha1((self::PASSWORDSALT . $password . self::PASSWORDSALT))
            : md5($password);
    }

    /**
     * Performs an authentication attempt
     *
     * @return Zend_Auth_Result
     * @throws Zend_Exception
     */
    public function authenticate()
    {
        $validator = new Zend_Validate_EmailAddress();
        if ($validator->isValid($this->_identity)) {
            $resultSet = $this->fetchUserByEmail();
        } else {
            $resultSet = $this->fetchUserByUsername();
        }

        if (count($resultSet) == 0) {
            return $this->createAuthResult(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identity,
                array('A record with the supplied identity could not be found.'));
        }

        if (count($resultSet) > 1) {
            return $this->createAuthResult(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $this->_identity,
                array('More than one record matches the supplied identity.'));
        }

        if (empty($resultSet[0]['email_checked'])) {
            return $this->createAuthResult(Local_Auth_Result::MAIL_ADDRESS_NOT_VALIDATED, $resultSet[0]['member_id'],
                array('Mail address not validated.'));
        }

        if ($resultSet[0]['is_active'] == 0) {
            return $this->createAuthResult(Local_Auth_Result::ACCOUNT_INACTIVE, $this->_identity, array('User account is inactive.'));
        }

        $this->_resultRow = array_shift($resultSet);

        return $this->createAuthResult(Zend_Auth_Result::SUCCESS, $this->_identity, array('Authentication successful.'));
    }

    /**
     * @return array
     * @throws Zend_Exception
     */
    private function fetchUserByEmail()
    {
        $sql = "
            SELECT `m`.*, `member_email`.`email_verification_value`, `member_email`.`email_checked` 
            FROM {$this->_tableName} AS m
            JOIN member_email ON m.member_id = member_email.email_member_id AND member_email.email_primary = 1
            WHERE  
            m.is_active = :active AND
            m.is_deleted = :deleted AND 
            m.login_method = :login AND 
            (LOWER(m.mail) = LOWER(:mail) OR
            LOWER(m.mail) = CONCAT(LOWER(:mail),:user_deactivated)
            ) <commit_msg>login<commit_after><?php

/**
 *  ocs-webserver
 *
 *  Copyright 2016 by pling GmbH.
 *
 *    This file is part of ocs-webserver.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
class Local_Auth_Adapter_Ocs implements Local_Auth_Adapter_Interface
{

    const MD5 = 'enc01';
    const SHA = 'enc02';
    const PASSWORDSALT = '<PASSWORD>';
    const USER_DEAVIVATED = '_deactivated';

    protected $_db;
    protected $_tableName;
    protected $_identity;
    protected $_credential;
    protected $_encryption;
    protected $_resultRow;

    /**
     * __construct() - Sets configuration options
     *
     * @param Zend_Db_Adapter_Abstract $dbAdapter If null, default database adapter assumed
     * @param string                   $tableName
     *
     * @throws Zend_Auth_Adapter_Exception
     */
    public function __construct(Zend_Db_Adapter_Abstract $dbAdapter = null, $tableName = null)
    {
        $this->_db = $dbAdapter;
        if (empty($this->_db)) {
            $this->_db = Zend_Db_Table_Abstract::getDefaultAdapter();
            if (empty($this->_db)) {
                throw new Zend_Auth_Adapter_Exception('No database adapter present');
            }
        }

        $this->_tableName = $tableName;
    }

    public static function getEncryptedPassword($password, $passwordType)
    {
        return $passwordType == Default_Model_DbTable_Member::PASSWORD_TYPE_HIVE ? sha1((self::PASSWORDSALT . $password . self::PASSWORDSALT))
            : md5($password);
    }

    /**
     * Performs an authentication attempt
     *
     * @return Zend_Auth_Result
     * @throws Zend_Exception
     */
    public function authenticate()
    {
        $validator = new Zend_Validate_EmailAddress();
        if ($validator->isValid($this->_identity)) {
            $resultSet = $this->fetchUserByEmail();
        } else {
            $resultSet = $this->fetchUserByUsername();
        }

        if (count($resultSet) == 0) {
            return $this->createAuthResult(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identity,
                array('A record with the supplied identity could not be found.'));
        }

        if (count($resultSet) > 1) {
            return $this->createAuthResult(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $this->_identity,
                array('More than one record matches the supplied identity.'));
        }

        if (empty($resultSet[0]['email_checked'])) {
            return $this->createAuthResult(Local_Auth_Result::MAIL_ADDRESS_NOT_VALIDATED, $resultSet[0]['member_id'],
                array('Mail address not validated.'));
        }

        if ($resultSet[0]['is_active'] == 0) {
            return $this->createAuthResult(Local_Auth_Result::ACCOUNT_INACTIVE, $this->_identity, array('User account is inactive.'));
        }

        $this->_resultRow = array_shift($resultSet);

        return $this->createAuthResult(Zend_Auth_Result::SUCCESS, $this->_identity, array('Authentication successful.'));
    }

    /**
     * @return array
     * @throws Zend_Exception
     */
    private function fetchUserByEmail()
    {
        $sql = "
            SELECT `m`.*, `member_email`.`email_verification_value`, `member_email`.`email_checked` 
            FROM {$this->_tableName} AS m
            JOIN member_email ON m.member_id = member_email.email_member_id AND member_email.email_primary = 1
            WHERE  
            m.is_active = :active AND
            m.is_deleted = :deleted AND 
            m.login_method = :login AND 
            (LOWER(m.mail) = LOWER(:mail) OR
            LOWER(m.mail) = CONCAT(LOWER(:mail),:user_deactivated)
            ) AND
{'fraction_non_alphanumeric': 0.10539756239117817, 'fraction_numerical': 0.00441091120139292, 'mean_word_length': 2.5974947807933195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2212518', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2381, 'n_words': 625}