1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
namespace core\session;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* File based session handler.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @copyright 2013 Petr Skoda {@link http://skodak.org}
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class file extends handler {
|
1441 |
ariadna |
27 |
|
1 |
efrain |
28 |
/** @var string session dir */
|
|
|
29 |
protected $sessiondir;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Create new instance of handler.
|
|
|
33 |
*/
|
|
|
34 |
public function __construct() {
|
|
|
35 |
global $CFG;
|
|
|
36 |
|
|
|
37 |
if (!empty($CFG->session_file_save_path)) {
|
|
|
38 |
$this->sessiondir = $CFG->session_file_save_path;
|
|
|
39 |
} else {
|
|
|
40 |
$this->sessiondir = "$CFG->dataroot/sessions";
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
1441 |
ariadna |
44 |
#[\Override]
|
1 |
efrain |
45 |
public function init() {
|
|
|
46 |
if (preg_match('/^[0-9]+;/', $this->sessiondir)) {
|
|
|
47 |
throw new exception('sessionhandlerproblem', 'error', '', null, 'Multilevel session directories are not supported');
|
|
|
48 |
}
|
|
|
49 |
// Make sure session directory exists and is writable.
|
|
|
50 |
make_writable_directory($this->sessiondir, false);
|
|
|
51 |
if (!is_writable($this->sessiondir)) {
|
|
|
52 |
throw new exception('sessionhandlerproblem', 'error', '', null, 'Session directory is not writable');
|
|
|
53 |
}
|
|
|
54 |
// Need to disable debugging since disk_free_space()
|
|
|
55 |
// will fail on very large partitions (see MDL-19222).
|
1441 |
ariadna |
56 |
// Moodle supports disable_functions = disk_free_space (MDL-43039).
|
|
|
57 |
$freespace = function_exists('disk_free_space') ? disk_free_space($this->sessiondir) : false;
|
|
|
58 |
if (!($freespace > 2048) && ($freespace !== false)) {
|
1 |
efrain |
59 |
throw new exception('sessiondiskfull', 'error');
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// NOTE: we cannot set any lock acquiring timeout here - bad luck.
|
|
|
63 |
ini_set('session.save_handler', 'files');
|
|
|
64 |
ini_set('session.save_path', $this->sessiondir);
|
|
|
65 |
}
|
|
|
66 |
|
1441 |
ariadna |
67 |
#[\Override]
|
1 |
efrain |
68 |
public function session_exists($sid) {
|
|
|
69 |
$sid = clean_param($sid, PARAM_FILE);
|
|
|
70 |
if (!$sid) {
|
|
|
71 |
return false;
|
|
|
72 |
}
|
|
|
73 |
$sessionfile = "$this->sessiondir/sess_$sid";
|
|
|
74 |
return file_exists($sessionfile);
|
|
|
75 |
}
|
|
|
76 |
|
1441 |
ariadna |
77 |
#[\Override]
|
|
|
78 |
public function destroy_all(): bool {
|
1 |
efrain |
79 |
if (is_dir($this->sessiondir)) {
|
|
|
80 |
foreach (glob("$this->sessiondir/sess_*") as $filename) {
|
|
|
81 |
@unlink($filename);
|
|
|
82 |
}
|
|
|
83 |
}
|
1441 |
ariadna |
84 |
|
|
|
85 |
return parent::destroy_all();
|
1 |
efrain |
86 |
}
|
|
|
87 |
|
1441 |
ariadna |
88 |
#[\Override]
|
|
|
89 |
public function destroy(string $id): bool {
|
|
|
90 |
$sid = clean_param($id, PARAM_FILE);
|
1 |
efrain |
91 |
if (!$sid) {
|
1441 |
ariadna |
92 |
return false;
|
1 |
efrain |
93 |
}
|
|
|
94 |
$sessionfile = "$this->sessiondir/sess_$sid";
|
|
|
95 |
if (file_exists($sessionfile)) {
|
|
|
96 |
@unlink($sessionfile);
|
|
|
97 |
}
|
1441 |
ariadna |
98 |
|
|
|
99 |
return parent::destroy($id);
|
1 |
efrain |
100 |
}
|
|
|
101 |
}
|