Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Database based session handler.
-
 
19
 *
-
 
20
 * @package    core
-
 
21
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
-
 
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
23
 */
-
 
24
 
16
 
Línea 25... Línea 17...
25
namespace core\session;
17
namespace core\session;
Línea 26... Línea 18...
26
 
18
 
Línea 32... Línea 24...
32
 * @package    core
24
 * @package    core
33
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
25
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
27
 */
36
class database extends handler implements SessionHandlerInterface {
28
class database extends handler implements SessionHandlerInterface {
-
 
29
 
37
    /** @var \stdClass $record session record */
30
    /** @var int $record session record */
38
    protected $recordid = null;
31
    protected $recordid = null;
Línea 39... Línea 32...
39
 
32
 
40
    /** @var \moodle_database $database session database */
33
    /** @var \moodle_database $database session database */
Línea 60... Línea 53...
60
        if (!empty($CFG->session_database_acquire_lock_timeout)) {
53
        if (!empty($CFG->session_database_acquire_lock_timeout)) {
61
            $this->acquiretimeout = (int)$CFG->session_database_acquire_lock_timeout;
54
            $this->acquiretimeout = (int)$CFG->session_database_acquire_lock_timeout;
62
        }
55
        }
63
    }
56
    }
Línea 64... Línea -...
64
 
-
 
65
    /**
57
 
66
     * Init session handler.
-
 
67
     */
58
    #[\Override]
68
    public function init() {
59
    public function init() {
69
        if (!$this->database->session_lock_supported()) {
60
        if (!$this->database->session_lock_supported()) {
70
            throw new exception('sessionhandlerproblem', 'error', '', null, 'Database does not support session locking');
61
            throw new exception('sessionhandlerproblem', 'error', '', null, 'Database does not support session locking');
Línea 74... Línea 65...
74
        if (!$result) {
65
        if (!$result) {
75
            throw new exception('dbsessionhandlerproblem', 'error');
66
            throw new exception('dbsessionhandlerproblem', 'error');
76
        }
67
        }
77
    }
68
    }
Línea 78... Línea -...
78
 
-
 
79
    /**
-
 
80
     * Check the backend contains data for this session id.
-
 
81
     *
-
 
82
     * Note: this is intended to be called from manager::session_exists() only.
-
 
83
     *
69
 
84
     * @param string $sid
-
 
85
     * @return bool true if session found.
-
 
86
     */
70
    #[\Override]
87
    public function session_exists($sid) {
71
    public function session_exists($sid) {
88
        // It was already checked in the calling code that the record in sessions table exists.
72
        // It was already checked in the calling code that the record in sessions table exists.
89
        return true;
73
        return true;
Línea 90... Línea 74...
90
    }
74
    }
91
 
75
 
-
 
76
    #[\Override]
92
    /**
77
    public function destroy(string $id): bool {
93
     * Kill all active sessions, the core sessions table is
-
 
94
     * purged afterwards.
78
        if (!$session = $this->database->get_record('sessions', ['sid' => $id], 'id, sid')) {
95
     */
79
            if ($id == session_id()) {
-
 
80
                $this->recordid = null;
96
    public function kill_all_sessions() {
81
                $this->lasthash = null;
97
        // Nothing to do, the sessions table is cleared from core.
82
            }
Línea -... Línea 83...
-
 
83
            return true;
98
        return;
84
        }
99
    }
85
 
100
 
86
        if ($this->recordid && ($session->id == $this->recordid)) {
-
 
87
            try {
-
 
88
                $this->database->release_session_lock($this->recordid);
101
    /**
89
            } catch (\Exception $ex) {
-
 
90
                // Log and ignore any problems.
102
     * Kill one session, the session record is removed afterwards.
91
                mtrace('Failed to release session lock: '.$ex->getMessage());
-
 
92
            }
-
 
93
            $this->recordid = null;
103
     * @param string $sid
94
            $this->lasthash = null;
-
 
95
        }
104
     */
96
 
105
    public function kill_session($sid) {
97
        $this->database->delete_records('sessions', ['id' => $session->id]);
Línea 106... Línea 98...
106
        // Nothing to do, the sessions table is purged afterwards.
98
 
107
        return;
99
        return true;
108
    }
100
    }
Línea 149... Línea 141...
149
     * @param string $sid
141
     * @param string $sid
150
     * @return string|false
142
     * @return string|false
151
     */
143
     */
152
    public function read(string $sid): string|false {
144
    public function read(string $sid): string|false {
153
        try {
145
        try {
154
            if (!$record = $this->database->get_record('sessions', array('sid'=>$sid), 'id')) {
146
            if (!$record = $this->database->get_record('sessions', ['sid' => $sid])) {
155
                // Let's cheat and skip locking if this is the first access,
147
                // Let's cheat and skip locking if this is the first access,
156
                // do not create the record here, let the manager do it after session init.
148
                // do not create the record here, let the manager do it after session init.
157
                $this->failed = false;
149
                $this->failed = false;
158
                $this->recordid = null;
150
                $this->recordid = null;
159
                $this->lasthash = sha1('');
151
                $this->lasthash = sha1('');
Línea 250... Línea 242...
250
        }
242
        }
Línea 251... Línea 243...
251
 
243
 
252
        return true;
244
        return true;
Línea 253... Línea -...
253
    }
-
 
254
 
-
 
255
    /**
-
 
256
     * Destroy session handler.
-
 
257
     *
-
 
258
     * {@see http://php.net/manual/en/function.session-set-save-handler.php}
-
 
259
     *
-
 
260
     * @param string $id
-
 
261
     * @return bool success
-
 
262
     */
-
 
263
    public function destroy(string $id): bool {
-
 
264
        if (!$session = $this->database->get_record('sessions', ['sid' => $id], 'id, sid')) {
-
 
265
            if ($id == session_id()) {
-
 
266
                $this->recordid = null;
-
 
267
                $this->lasthash = null;
-
 
268
            }
-
 
269
            return true;
-
 
270
        }
-
 
271
 
-
 
272
        if ($this->recordid && ($session->id == $this->recordid)) {
-
 
273
            try {
-
 
274
                $this->database->release_session_lock($this->recordid);
-
 
275
            } catch (\Exception $ex) {
-
 
276
                // Ignore problems.
-
 
277
            }
-
 
278
            $this->recordid = null;
-
 
279
            $this->lasthash = null;
-
 
280
        }
-
 
281
 
-
 
282
        $this->database->delete_records('sessions', ['id' => $session->id]);
-
 
283
 
-
 
284
        return true;
-
 
285
    }
-
 
286
 
-
 
287
    /**
-
 
288
     * GC session handler.
-
 
289
     *
-
 
290
     * {@see http://php.net/manual/en/function.session-set-save-handler.php}
-
 
291
     *
-
 
292
     * @param int $max_lifetime moodle uses special timeout rules
-
 
293
     * @return bool success
-
 
294
     */
-
 
295
    // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameUnderscore
-
 
296
    public function gc(int $max_lifetime): int|false {
-
 
297
        // This should do something only if cron is not running properly...
-
 
298
        if (!$stalelifetime = ini_get('session.gc_maxlifetime')) {
-
 
299
            return false;
-
 
300
        }
-
 
301
        $params = ['purgebefore' => (time() - $stalelifetime)];
-
 
302
        $count = $this->database->count_records_select('sessions', 'userid = 0 AND timemodified < :purgebefore', $params);
-
 
303
        $this->database->delete_records_select('sessions', 'userid = 0 AND timemodified < :purgebefore', $params);
-
 
304
 
-
 
305
        return $count;
245
    }