comparison core/lib/Drupal/Core/Session/SessionHandler.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Session;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
8 use Drupal\Core\Utility\Error;
9 use Symfony\Component\HttpFoundation\RequestStack;
10 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
11
12 /**
13 * Default session handler.
14 */
15 class SessionHandler extends AbstractProxy implements \SessionHandlerInterface {
16
17 use DependencySerializationTrait;
18
19 /**
20 * The request stack.
21 *
22 * @var \Symfony\Component\HttpFoundation\RequestStack
23 */
24 protected $requestStack;
25
26 /**
27 * The database connection.
28 *
29 * @var \Drupal\Core\Database\Connection
30 */
31 protected $connection;
32
33 /**
34 * Constructs a new SessionHandler instance.
35 *
36 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
37 * The request stack.
38 * @param \Drupal\Core\Database\Connection $connection
39 * The database connection.
40 */
41 public function __construct(RequestStack $request_stack, Connection $connection) {
42 $this->requestStack = $request_stack;
43 $this->connection = $connection;
44 }
45
46 /**
47 * {@inheritdoc}
48 */
49 public function open($save_path, $name) {
50 return TRUE;
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 public function read($sid) {
57 $data = '';
58 if (!empty($sid)) {
59 // Read the session data from the database.
60 $query = $this->connection
61 ->queryRange('SELECT session FROM {sessions} WHERE sid = :sid', 0, 1, [':sid' => Crypt::hashBase64($sid)]);
62 $data = (string) $query->fetchField();
63 }
64 return $data;
65 }
66
67 /**
68 * {@inheritdoc}
69 */
70 public function write($sid, $value) {
71 // The exception handler is not active at this point, so we need to do it
72 // manually.
73 try {
74 $request = $this->requestStack->getCurrentRequest();
75 $fields = [
76 'uid' => $request->getSession()->get('uid', 0),
77 'hostname' => $request->getClientIP(),
78 'session' => $value,
79 'timestamp' => REQUEST_TIME,
80 ];
81 $this->connection->merge('sessions')
82 ->keys(['sid' => Crypt::hashBase64($sid)])
83 ->fields($fields)
84 ->execute();
85 return TRUE;
86 }
87 catch (\Exception $exception) {
88 require_once DRUPAL_ROOT . '/core/includes/errors.inc';
89 // If we are displaying errors, then do so with no possibility of a
90 // further uncaught exception being thrown.
91 if (error_displayable()) {
92 print '<h1>Uncaught exception thrown in session handler.</h1>';
93 print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
94 }
95 return FALSE;
96 }
97 }
98
99 /**
100 * {@inheritdoc}
101 */
102 public function close() {
103 return TRUE;
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function destroy($sid) {
110 // Delete session data.
111 $this->connection->delete('sessions')
112 ->condition('sid', Crypt::hashBase64($sid))
113 ->execute();
114
115 return TRUE;
116 }
117
118 /**
119 * {@inheritdoc}
120 */
121 public function gc($lifetime) {
122 // Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
123 // value. For example, if you want user sessions to stay in your database
124 // for three weeks before deleting them, you need to set gc_maxlifetime
125 // to '1814400'. At that value, only after a user doesn't log in after
126 // three weeks (1814400 seconds) will his/her session be removed.
127 $this->connection->delete('sessions')
128 ->condition('timestamp', REQUEST_TIME - $lifetime, '<')
129 ->execute();
130 return TRUE;
131 }
132
133 }