123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace yii\web;
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\db\Connection;
- use yii\db\PdoValue;
- use yii\db\Query;
- use yii\di\Instance;
- use yii\helpers\ArrayHelper;
- class DbSession extends MultiFieldSession
- {
-
- public $db = 'db';
-
- public $sessionTable = '{{%session}}';
-
- protected $fields = [];
-
- public function init()
- {
- parent::init();
- $this->db = Instance::ensure($this->db, Connection::className());
- }
-
- public function regenerateID($deleteOldSession = false)
- {
- $oldID = session_id();
-
- if (empty($oldID)) {
- return;
- }
- parent::regenerateID(false);
- $newID = session_id();
-
- if (empty($newID)) {
- Yii::warning('Failed to generate new session ID', __METHOD__);
- return;
- }
- $row = $this->db->useMaster(function() use ($oldID) {
- return (new Query())->from($this->sessionTable)
- ->where(['id' => $oldID])
- ->createCommand($this->db)
- ->queryOne();
- });
- if ($row !== false) {
- if ($deleteOldSession) {
- $this->db->createCommand()
- ->update($this->sessionTable, ['id' => $newID], ['id' => $oldID])
- ->execute();
- } else {
- $row['id'] = $newID;
- $this->db->createCommand()
- ->insert($this->sessionTable, $row)
- ->execute();
- }
- } else {
-
- $this->db->createCommand()
- ->insert($this->sessionTable, $this->composeFields($newID, ''))
- ->execute();
- }
- }
-
- public function close()
- {
- if ($this->getIsActive()) {
-
- $this->fields = $this->composeFields();
- YII_DEBUG ? session_write_close() : @session_write_close();
- }
- }
-
- public function readSession($id)
- {
- $query = new Query();
- $query->from($this->sessionTable)
- ->where('[[expire]]>:expire AND [[id]]=:id', [':expire' => time(), ':id' => $id]);
- if ($this->readCallback !== null) {
- $fields = $query->one($this->db);
- return $fields === false ? '' : $this->extractData($fields);
- }
- $data = $query->select(['data'])->scalar($this->db);
- return $data === false ? '' : $data;
- }
-
- public function writeSession($id, $data)
- {
-
-
- try {
-
- if ($this->writeCallback && !$this->fields) {
- $this->fields = $this->composeFields();
- }
-
- if (!isset($this->fields['data'])) {
- $this->fields['data'] = $data;
- } else {
- $_SESSION = $this->fields['data'];
- }
-
- $this->fields = array_merge($this->fields, [
- 'id' => $id,
- 'expire' => time() + $this->getTimeout(),
- ]);
- $this->fields = $this->typecastFields($this->fields);
- $this->db->createCommand()->upsert($this->sessionTable, $this->fields)->execute();
- $this->fields = [];
- } catch (\Exception $e) {
- Yii::$app->errorHandler->handleException($e);
- return false;
- }
- return true;
- }
-
- public function destroySession($id)
- {
- $this->db->createCommand()
- ->delete($this->sessionTable, ['id' => $id])
- ->execute();
- return true;
- }
-
- public function gcSession($maxLifetime)
- {
- $this->db->createCommand()
- ->delete($this->sessionTable, '[[expire]]<:expire', [':expire' => time()])
- ->execute();
- return true;
- }
-
- protected function typecastFields($fields)
- {
- if (isset($fields['data']) && !is_array($fields['data']) && !is_object($fields['data'])) {
- $fields['data'] = new PdoValue($fields['data'], \PDO::PARAM_LOB);
- }
- return $fields;
- }
- }
|