<commit_before><?php

namespace Intersvyaz\HttpSession;

use CDbHttpSession;
use Exception;
use PDO;
use Intersvyaz\Pdo\Oci8;

class DbHttpSession extends CDbHttpSession
{
    /**
     * @inheritdoc
     */
    public function init()
    {
        // Opening db connection first
        $this->getDbConnection()->setActive(true);
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function regenerateID($deleteOldSession = false)
    {
        $oldID = session_id();

        // if no session is started, there is nothing to regenerate
        if (empty($oldID)) {
            return;
        }

        if ($this->getIsStarted()) {
            session_regenerate_id(false);
        }

        $newID = session_id();
        $db = $this->getDbConnection();

        $row = $db->createCommand("SELECT ID,EXPIRE,DATA FROM {$this->sessionTableName} WHERE id=:id")
            ->bindValue(':id', $oldID)->queryRow();

        if ($row !== false) {
            if ($deleteOldSession) {
                $db->createCommand("UPDATE {$this->sessionTableName} SET id=:newID WHERE id=:oldID")
                    ->bindValue(':newID', $newID)
                    ->bindValue(':oldID', $oldID)
                    ->execute();
            } else {
                if ($this->isOci8Driver()) {
                    $fp = $row['DATA'];
                } else {
                    $fp = fopen('php://memory', "rwb");
                    fwrite($fp, stream_get_contents($row['DATA']));
                    fseek($fp, 0);
                    $transaction = $db->beginTransaction();
                }

                $db->createCommand("INSERT INTO {$this->sessionTableName} (id, expire,data)
                      VALUES (:id, :expire, empty_blob()) returning data into :data")
                    ->bindValue(':id', $newID)
                    ->bindValue(':expire', $row['EXPIRE'])
                    ->bindParam(':data', $fp, PDO::PARAM_LOB)
                    ->execute();

                if (!$this->isOci8Driver()) {
                    $transaction->commit();
                    fclose($fp);
                }
            }
        } else {
            // shouldn't reach here normally
            $db->createCommand("INSERT INTO {$this->sessionTableName} (id, expire) values (:ID, :EXPIRE)")
                ->execute(array(
                    'ID' => $newID,
                    'EXPIRE' => time() + $this->getTimeout(),
                ));
        }
    }

    /**
     * @inheritdoc
     */
    protected function createSessionTable($db, $tableName)
    {
        $sql = 'CREATE
           TABLE ' . $this->sessionTableName . '
           (
              "ID"     VARCHAR2(32 BYTE),
              "EXPIRE" NUMBER(10,0) NOT NULL,
              "DATA" BLOB,
               CONSTRAINT YiiSession_PK PRIMARY KEY(ID)
           );';
        $db->createCommand($sql)
            ->execute();
    }

    /**
     * @inheritdoc
     */
    public function readSession($id)
    {
        $data = $this->getDbConnection()
            ->createCommand("SELECT data FROM {$this->sessionTableName} WHERE expire>:expire AND id=:id")
            ->bindValue(':id', $id)
            ->bindValue(':expire', time())
            ->queryRow();

        if ($this->isOci8Driver()) {
            return $data['DATA'];
        }

        return $data === false ? '' : stream_get_contents($data['DATA']);
    }

    /**
     * @inheritdoc
     */
    public function writeSession($id, $data)
    {
        // exception must be caught in session write handler
        // http://us.php.net/manual/en/function.session-set-save-handler.php
        try {
            $expire = time() + $this->getTimeout();
            $db = $this->getDbConnection();
            $sql = "SELECT id FROM {$this->sessionTableName} WHERE id=:id";
            if ($db->createCommand($sql)->bindValue(':id', $id)->queryScalar() === false) {<commit_msg>PHP7 fix<commit_after><?php

namespace Intersvyaz\HttpSession;

use CDbHttpSession;
use Exception;
use PDO;
use Intersvyaz\Pdo\Oci8;

class DbHttpSession extends CDbHttpSession
{
    /**
     * @inheritdoc
     */
    public function init()
    {
        // Opening db connection first
        $this->getDbConnection()->setActive(true);
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function regenerateID($deleteOldSession = false)
    {
        $oldID = session_id();

        // if no session is started, there is nothing to regenerate
        if (empty($oldID)) {
            return;
        }

        if ($this->getIsStarted()) {
            session_regenerate_id($deleteOldSession);
        }

        $newID = session_id();
        $db = $this->getDbConnection();

        $row = $db->createCommand("SELECT ID,EXPIRE,DATA FROM {$this->sessionTableName} WHERE id=:id")
            ->bindValue(':id', $oldID)->queryRow();

        if ($row !== false) {
            if ($deleteOldSession) {
                $db->createCommand("UPDATE {$this->sessionTableName} SET id=:newID WHERE id=:oldID")
                    ->bindValue(':newID', $newID)
                    ->bindValue(':oldID', $oldID)
                    ->execute();
            } else {
                if ($this->isOci8Driver()) {
                    $fp = $row['DATA'];
                } else {
                    $fp = fopen('php://memory', "rwb");
                    fwrite($fp, stream_get_contents($row['DATA']));
                    fseek($fp, 0);
                    $transaction = $db->beginTransaction();
                }

                $db->createCommand("INSERT INTO {$this->sessionTableName} (id, expire,data)
                      VALUES (:id, :expire, empty_blob()) returning data into :data")
                    ->bindValue(':id', $newID)
                    ->bindValue(':expire', $row['EXPIRE'])
                    ->bindParam(':data', $fp, PDO::PARAM_LOB)
                    ->execute();

                if (!$this->isOci8Driver()) {
                    $transaction->commit();
                    fclose($fp);
                }
            }
        } else {
            // shouldn't reach here normally
            $db->createCommand("INSERT INTO {$this->sessionTableName} (id, expire) values (:ID, :EXPIRE)")
                ->execute(array(
                    'ID' => $newID,
                    'EXPIRE' => time() + $this->getTimeout(),
                ));
        }
    }

    /**
     * @inheritdoc
     */
    protected function createSessionTable($db, $tableName)
    {
        $sql = 'CREATE
           TABLE ' . $this->sessionTableName . '
           (
              "ID"     VARCHAR2(32 BYTE),
              "EXPIRE" NUMBER(10,0) NOT NULL,
              "DATA" BLOB,
               CONSTRAINT YiiSession_PK PRIMARY KEY(ID)
           );';
        $db->createCommand($sql)
            ->execute();
    }

    /**
     * @inheritdoc
     */
    public function readSession($id)
    {
        $data = $this->getDbConnection()
            ->createCommand("SELECT data FROM {$this->sessionTableName} WHERE expire>:expire AND id=:id")
            ->bindValue(':id', $id)
            ->bindValue(':expire', time())
            ->queryRow();

        if ($this->isOci8Driver()) {
            return (string)$data['DATA'];
        }

        return $data === false ? '' : stream_get_contents($data['DATA']);
    }

    /**
     * @inheritdoc
     */
    public function writeSession($id, $data)
    {
        // exception must be caught in session write handler
        // http://us.php.net/manual/en/function.session-set-save-handler.php
        try {
            $expire = time() + $this->getTimeout();
            $db = $this->getDbConnection();
            $sql = "SELECT id FROM {$this->sessionTableName} WHERE id=:id";
            if ($db->createCommand($sql)->bindValue(':id', $id)->queryScalar() === false) {
{'fraction_non_alphanumeric': 0.15444260836405238, 'fraction_numerical': 0.002923604931994407, 'mean_word_length': 1.6018518518518519, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 95, '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': '6390428', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2186, 'n_words': 437}