<commit_before><?php
/**
 * Session Handling
 * MySQL
 *
 * @author      BizLogic <<EMAIL>>
 * @copyright   2016 BizLogic
 * @link        http://bizlogicdev.com
 * @link        http://pricelessphp.com
 * @license     GNU Affero General Public License v3
 *
 * @since       Monday, December 19, 2016, 04:20 AM GMT+1 mknox
 * @edited      $Date$ $Author$
 * @version     $Id$
 */

class Session_Db_Mysql extends Db
{
    public function __construct()
    {
        $this->tableName = DB_TABLE_PREFIX . 'user_session';
        parent::__construct( $this->tableName );
    }
    
    /**
     * Ensure that the DB connection 
     * exists
     * 
     * @return  boolean
    */
    public function _open() 
    {
        if( $this->db ) {
            return true;
        }

        return false;
    }
    
    /**
     * Close the DB connection
     * 
     * @return  boolean
    */
    public function _close() 
    {
        // cleanup
        $this->_gc( SITE_SESSION_TIMEOUT + 600 );
        return true;
    }    
    
    /**
     * Read a session from 
     * the DB
     * 
     * @param   string  $sessionId
     * @param   string
    */
    public function _read( $sessionId ) 
    {
        $data = $this->getBy(
            array(
                array(
                    'column'    => 'session_id',
                    'value'     => $sessionId,
                    'operator'  => '='
                )
            )
        );
        
        if( !empty( $data ) ) {
            return $data['data'];
        }

        return '';
    }  
    
    /**
     * Write a session to the DB
     * 
     * @param   string  $sessionId
     * @param   string  $data
     * @return  boolean
    */
    public function _write( $sessionId, $data = '' ) 
    {
        $existingData = $this->getBy(
            array(
                array(
                    'column'    => 'session_id',
                    'value'     => $sessionId,
                    'operator'  => '='
                )
            )
        );
        
        if( !empty( $existingData ) ) {
            $result = $this->updateById( 
                $existingData['id'], 
                array(
                    'user_id'           => userId(),
                    'data'              => $data,
                    'ip'                => getIp(),
                    'date_expiration'   => time() + SITE_SESSION_TIMEOUT,
                    'date_last_update'  => time(),
                    'user_agent'        => $_SERVER['HTTP_USER_AGENT']
                ) 
            );
        } else {
            $result = $this->insert( $sessionId, $data );            
        }
    
        if( $result > 0 ) {
            return true;
        }
         
        return false;
    }    
    
    /**
     * Destroy a session
     * 
     * @param   string  $sessionId
     * @return  boolean
    */
    public function _destroy( $sessionId ) 
    {
        $result = $this->deleteBySessionId( $sessionId );

        if( $result == 0 ) {
            return true;
        }

        return false;
    }    
    
    /**
     * Garbage Collection
     * 
     * @param   int $maxLifetime
     * @return  int
    */
    public function _gc( $maxLifetime = SITE_SESSION_TIMEOUT )
    {
        // Calculate what is to be deemed old
        $old = ( time() - $maxLifetime );
    
        // query
        $sql = "DELETE FROM ";
        $sql .= " `".mysqli_real_escape_string( $this->db, $this->tableName )."` ";
        $sql .= "WHERE `date_expiration` <= ".mysqli_real_escape_string( $this->db, $old )." ";
        
        $res    = mysqli_query( $this->db, $sql ) OR die( '<pre>SQL Error:  '.mysqli_error( $this->db ).'<br>SQL:  '.$sql.'<br>File:  '.__FILE__.'<br>Line:  '.__LINE__ );        
        $result = mysqli_affected_rows( $this->db );
         
        if( $result > 0 ) {
            return true;
        }
    
        return false;
    }    
    
    /**
     * Delete by Session ID
     *
     * @param	stirng	$id
     * @return	boolean
     */
    public function deleteBySessionId( $sessionId = '' )
    {    
        $sql = "DELETE FROM ";
        $sql .= " `".mysqli_real_escape_string( $this->db, $this->tableName )."` ";
        $sql .= "WHERE `session_id` = '".mysqli_real_escape_string( $this->db, $sessionId )."' ";
        $sql .= "LIMIT 1 ";
    
        $res = mysqli_query( $this->db, $sql ) OR die( '<pre>SQL Error:  '.mysqli_error( $this->db ).'<br>SQL:  '.$sql.'<br>File:  '.__FILE__.'<br>Line:  '.__LINE__ );
    
        return mysqli_affected_rows( $this->db );
    }
    
    /**
     * Insert a new record
     *
     * @param  string   $sessionId
     * @param  string   $data<commit_msg>FIXED:  Typo<commit_after><?php
/**
 * Session Handling
 * MySQL
 *
 * @author      BizLogic <<EMAIL>>
 * @copyright   2016 BizLogic
 * @link        http://bizlogicdev.com
 * @link        http://pricelessphp.com
 * @license     GNU Affero General Public License v3
 *
 * @since       Monday, December 19, 2016, 04:20 AM GMT+1 mknox
 * @edited      $Date$ $Author$
 * @version     $Id$
 */

class Session_Db_Mysql extends Db
{
    public function __construct()
    {
        $this->tableName = DB_TABLE_PREFIX . 'user_session';
        parent::__construct( $this->tableName );
    }
    
    /**
     * Ensure that the DB connection 
     * exists
     * 
     * @return  boolean
    */
    public function _open() 
    {
        if( $this->db ) {
            return true;
        }

        return false;
    }
    
    /**
     * Close the DB connection
     * 
     * @return  boolean
    */
    public function _close() 
    {
        // cleanup
        $this->_gc( SITE_SESSION_TIMEOUT + 600 );
        return true;
    }    
    
    /**
     * Read a session from 
     * the DB
     * 
     * @param   string  $sessionId
     * @param   string
    */
    public function _read( $sessionId ) 
    {
        $data = $this->getBy(
            array(
                array(
                    'column'    => 'session_id',
                    'value'     => $sessionId,
                    'operator'  => '='
                )
            )
        );
        
        if( !empty( $data ) ) {
            return $data['data'];
        }

        return '';
    }  
    
    /**
     * Write a session to the DB
     * 
     * @param   string  $sessionId
     * @param   string  $data
     * @return  boolean
    */
    public function _write( $sessionId, $data = '' ) 
    {
        $existingData = $this->getBy(
            array(
                array(
                    'column'    => 'session_id',
                    'value'     => $sessionId,
                    'operator'  => '='
                )
            )
        );
        
        if( !empty( $existingData ) ) {
            $result = $this->updateById( 
                $existingData['id'], 
                array(
                    'user_id'           => userId(),
                    'data'              => $data,
                    'ip'                => getIp(),
                    'date_expiration'   => time() + SITE_SESSION_TIMEOUT,
                    'date_last_update'  => time(),
                    'user_agent'        => $_SERVER['HTTP_USER_AGENT']
                ) 
            );
        } else {
            $result = $this->insert( $sessionId, $data );            
        }
    
        if( $result > 0 ) {
            return true;
        }
         
        return false;
    }    
    
    /**
     * Destroy a session
     * 
     * @param   string  $sessionId
     * @return  boolean
    */
    public function _destroy( $sessionId ) 
    {
        $result = $this->deleteBySessionId( $sessionId );

        if( $result == 0 ) {
            return true;
        }

        return false;
    }    
    
    /**
     * Garbage Collection
     * 
     * @param   int $maxLifetime
     * @return  int
    */
    public function _gc( $maxLifetime = SITE_SESSION_TIMEOUT )
    {
        // Calculate what is to be deemed old
        $old = ( time() - $maxLifetime );
    
        // query
        $sql = "DELETE FROM ";
        $sql .= " `".mysqli_real_escape_string( $this->db, $this->tableName )."` ";
        $sql .= "WHERE `date_expiration` <= ".mysqli_real_escape_string( $this->db, $old )." ";
        
        $res    = mysqli_query( $this->db, $sql ) OR die( '<pre>SQL Error:  '.mysqli_error( $this->db ).'<br>SQL:  '.$sql.'<br>File:  '.__FILE__.'<br>Line:  '.__LINE__ );        
        $result = mysqli_affected_rows( $this->db );
         
        if( $result > 0 ) {
            return true;
        }
    
        return false;
    }    
    
    /**
     * Delete by Session ID
     *
     * @param	string	$id
     * @return	boolean
     */
    public function deleteBySessionId( $sessionId = '' )
    {    
        $sql = "DELETE FROM ";
        $sql .= " `".mysqli_real_escape_string( $this->db, $this->tableName )."` ";
        $sql .= "WHERE `session_id` = '".mysqli_real_escape_string( $this->db, $sessionId )."' ";
        $sql .= "LIMIT 1 ";
    
        $res = mysqli_query( $this->db, $sql ) OR die( '<pre>SQL Error:  '.mysqli_error( $this->db ).'<br>SQL:  '.$sql.'<br>File:  '.__FILE__.'<br>Line:  '.__LINE__ );
    
        return mysqli_affected_rows( $this->db );
    }
    
    /**
     * Insert a new record
     *
     * @param  string   $sessionId
     * @param  string   $data
{'fraction_non_alphanumeric': 0.13681301158711598, 'fraction_numerical': 0.004889975550122249, 'mean_word_length': 1.2851590964294388, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4200106', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2879, 'n_words': 614}