Skip to content

Commit e1e204c

Browse files
committed
Session: added own session handler (removed problematic @session_start)
1 parent eb42b07 commit e1e204c

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

src/Tracy/Bar.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
*/
1414
class Bar
1515
{
16+
/** @var Session */
17+
private $session;
18+
1619
/** @var IBarPanel[] */
1720
private $panels = [];
1821

1922

23+
public function __construct(Session $session)
24+
{
25+
$this->session = $session;
26+
}
27+
28+
2029
/**
2130
* Add custom panel.
2231
* @param IBarPanel
@@ -53,8 +62,7 @@ public function getPanel($id)
5362
*/
5463
public function render()
5564
{
56-
@session_start(); // @ session may be already started or it is not possible to start session
57-
$previousBars = & $_SESSION['__NF']['tracybar-2.3'];
65+
$previousBars = & $this->session->getContent()['redirect'];
5866
$isRedirect = preg_match('#^Location:#im', implode("\n", headers_list()));
5967
if ($isRedirect) {
6068
Dumper::fetchLiveData();

src/Tracy/Debugger.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class Debugger
114114
/** @var ILogger */
115115
private static $fireLogger;
116116

117+
/** @var Session */
118+
private static $session;
119+
117120

118121
/**
119122
* Static class - cannot be instantiated.
@@ -180,6 +183,10 @@ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
180183

181184
array_map('class_exists', ['Tracy\Bar', 'Tracy\BlueScreen', 'Tracy\DefaultBarPanel', 'Tracy\Dumper',
182185
'Tracy\FireLogger', 'Tracy\Helpers', 'Tracy\Logger']);
186+
187+
if (!self::$productionMode) {
188+
self::getSession()->open(session_save_path() ?: ini_get('upload_tmp_dir') ?: self::$logDirectory);
189+
}
183190
}
184191

185192

@@ -415,7 +422,7 @@ public static function getBlueScreen()
415422
public static function getBar()
416423
{
417424
if (!self::$bar) {
418-
self::$bar = new Bar;
425+
self::$bar = new Bar(self::getSession());
419426
self::$bar->addPanel($info = new DefaultBarPanel('info'), 'Tracy:info');
420427
$info->cpuUsage = self::$cpuUsage;
421428
self::$bar->addPanel(new DefaultBarPanel('errors'), 'Tracy:errors'); // filled by errorHandler()
@@ -459,6 +466,19 @@ public static function getFireLogger()
459466
}
460467

461468

469+
/**
470+
* @return Session
471+
* @internal
472+
*/
473+
public static function getSession()
474+
{
475+
if (!self::$session) {
476+
self::$session = new Session;
477+
}
478+
return self::$session;
479+
}
480+
481+
462482
/********************* useful tools ****************d*g**/
463483

464484

src/Tracy/Session.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Tracy (https://tracy.nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
namespace Tracy;
9+
10+
11+
/**
12+
* Session.
13+
* @internal
14+
*/
15+
class Session
16+
{
17+
const COOKIE_NAME = 'tracy-session';
18+
19+
/** @var string|NULL */
20+
private $id;
21+
22+
/** @var resource|NULL */
23+
private $handle;
24+
25+
/** @var array|NULL */
26+
private $data;
27+
28+
29+
public function open($directory)
30+
{
31+
$cookie = isset($_COOKIE[self::COOKIE_NAME]) ? $_COOKIE[self::COOKIE_NAME] : NULL;
32+
if ($directory && is_string($cookie) && preg_match('#^[0-9a-f]{10}\z#i', $cookie)) {
33+
$this->id = $cookie;
34+
} elseif ($directory && PHP_SAPI !== 'cli' && !headers_sent()) {
35+
$this->id = substr(md5(uniqid('', TRUE)), 0, 10);
36+
setcookie(self::COOKIE_NAME, $this->id, 0, '/', '', FALSE, TRUE);
37+
} else {
38+
return;
39+
}
40+
41+
$this->handle = fopen($directory . '/tracy.' . $this->id, 'a+');
42+
}
43+
44+
45+
public function getId()
46+
{
47+
return $this->id;
48+
}
49+
50+
51+
public function & getContent()
52+
{
53+
if ($this->handle && $this->data === NULL) {
54+
flock($this->handle, LOCK_EX);
55+
$this->data = @unserialize(stream_get_contents($this->handle)) ?: []; // @ - file may be empty
56+
}
57+
return $this->data;
58+
}
59+
60+
61+
public function __destruct()
62+
{
63+
if ($this->handle && $this->data !== NULL) {
64+
ftruncate($this->handle, 0);
65+
fwrite($this->handle, serialize($this->data));
66+
fclose($this->handle);
67+
$this->handle = NULL;
68+
}
69+
}
70+
71+
}

src/tracy.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
require __DIR__ . '/Tracy/Logger.php';
1818
require __DIR__ . '/Tracy/Debugger.php';
1919
require __DIR__ . '/Tracy/OutputDebugger.php';
20+
require __DIR__ . '/Tracy/Session.php';
2021
require __DIR__ . '/shortcuts.php';

0 commit comments

Comments
 (0)