Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Session;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Wraps another SessionHandlerInterface to prevent writes when not allowed.
|
Chris@0
|
7 */
|
Chris@0
|
8 class WriteSafeSessionHandler implements \SessionHandlerInterface, WriteSafeSessionHandlerInterface {
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * @var \SessionHandlerInterface
|
Chris@0
|
12 */
|
Chris@0
|
13 protected $wrappedSessionHandler;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Whether or not the session is enabled for writing.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var bool
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $sessionWritable;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Constructs a new write safe session handler.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param \SessionHandlerInterface $wrapped_session_handler
|
Chris@0
|
26 * The underlying session handler.
|
Chris@0
|
27 * @param bool $session_writable
|
Chris@0
|
28 * Whether or not the session should be initially writable.
|
Chris@0
|
29 */
|
Chris@0
|
30 public function __construct(\SessionHandlerInterface $wrapped_session_handler, $session_writable = TRUE) {
|
Chris@0
|
31 $this->wrappedSessionHandler = $wrapped_session_handler;
|
Chris@0
|
32 $this->sessionWritable = $session_writable;
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function close() {
|
Chris@0
|
39 return $this->wrappedSessionHandler->close();
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function destroy($session_id) {
|
Chris@0
|
46 return $this->wrappedSessionHandler->destroy($session_id);
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@0
|
52 public function gc($max_lifetime) {
|
Chris@0
|
53 return $this->wrappedSessionHandler->gc($max_lifetime);
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * {@inheritdoc}
|
Chris@0
|
58 */
|
Chris@0
|
59 public function open($save_path, $session_id) {
|
Chris@0
|
60 return $this->wrappedSessionHandler->open($save_path, $session_id);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * {@inheritdoc}
|
Chris@0
|
65 */
|
Chris@0
|
66 public function read($session_id) {
|
Chris@0
|
67 return $this->wrappedSessionHandler->read($session_id);
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 public function write($session_id, $session_data) {
|
Chris@0
|
74 if ($this->isSessionWritable()) {
|
Chris@0
|
75 return $this->wrappedSessionHandler->write($session_id, $session_data);
|
Chris@0
|
76 }
|
Chris@0
|
77 else {
|
Chris@0
|
78 return TRUE;
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function setSessionWritable($flag) {
|
Chris@0
|
86 $this->sessionWritable = (bool) $flag;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function isSessionWritable() {
|
Chris@0
|
93 return $this->sessionWritable;
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 }
|