Skip to content

Commit

Permalink
added FileSession, used by default [Closes #362][Closes #356]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 10, 2021
1 parent 3b0b22e commit 9faef5e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 17 deletions.
3 changes: 0 additions & 3 deletions examples/ajax-fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Tracy\Debugger;

// session is required for this functionality
session_start();

// For security reasons, Tracy is visible only on localhost.
// You may force Tracy to run in development mode by passing the Debugger::DEVELOPMENT instead of Debugger::DETECT.
Debugger::enable(Debugger::DETECT, __DIR__ . '/log');
Expand Down
3 changes: 0 additions & 3 deletions examples/ajax-jquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Tracy\Debugger;

// session is required for this functionality
session_start();

// For security reasons, Tracy is visible only on localhost.
// You may force Tracy to run in development mode by passing the Debugger::DEVELOPMENT instead of Debugger::DETECT.
Debugger::enable(Debugger::DETECT, __DIR__ . '/log');
Expand Down
3 changes: 0 additions & 3 deletions examples/preloading.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Tracy\Debugger;

// session is required for this functionality
session_start();

// For security reasons, Tracy is visible only on localhost.
// You may force Tracy to run in development mode by passing the Debugger::DEVELOPMENT instead of Debugger::DETECT.
Debugger::enable(Debugger::DETECT, __DIR__ . '/log');
Expand Down
3 changes: 0 additions & 3 deletions examples/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Tracy\Debugger;

// session is required for this functionality
session_start();

// For security reasons, Tracy is visible only on localhost.
// You may force Tracy to run in development mode by passing the Debugger::DEVELOPMENT instead of Debugger::DETECT.
Debugger::enable(Debugger::DETECT, __DIR__ . '/log');
Expand Down
17 changes: 13 additions & 4 deletions src/Bridges/Nette/TracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getConfigSchema(): Nette\Schema\Schema
'fromEmail' => Expect::email()->dynamic(),
'emailSnooze' => Expect::string()->dynamic(),
'logSeverity' => Expect::anyOf($errorSeverity, Expect::listOf($errorSeverity)),
'storage' => Expect::string(),
'editor' => Expect::string()->dynamic(),
'browser' => Expect::string()->dynamic(),
'errorTemplate' => Expect::string()->dynamic(),
Expand Down Expand Up @@ -89,7 +90,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
$builder = $this->getContainerBuilder();

$options = (array) $this->config;
unset($options['bar'], $options['blueScreen'], $options['netteMailer']);
unset($options['bar'], $options['blueScreen'], $options['netteMailer'], $options['storage']);
if (isset($options['logSeverity'])) {
$options['logSeverity'] = $this->normalizeErrorSeverity($options['logSeverity']);
}
Expand Down Expand Up @@ -139,9 +140,17 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
));
}

if (!$this->cliMode && ($name = $builder->getByType(Nette\Http\Session::class))) {
$initialize->addBody('$this->getService(?)->start();', [$name]);
$initialize->addBody('Tracy\Debugger::dispatch();');
if (!$this->cliMode) {
if ($this->config['storage'] === 'session') {
if ($name = $builder->getByType(Nette\Http\Session::class)) {
$initialize->addBody('$this->getService(?)->start();', [$name]);
}
$initialize->addBody('Tracy\Debugger::dispatch();');

} elseif ($this->config['storage'] !== null) {
$initialize->addBody('Tracy\Debugger::setStorage(new Tracy\FileSession(?));', [$this->config['storage']]);
$initialize->addBody('Tracy\Debugger::dispatch();');
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Tracy/Debugger/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public static function enable($mode = null, string $logDirectory = null, $email
'Logger/FireLogger',
'Logger/Logger',
'Session/SessionHandler',
'Session/FileSession',
'Session/NativeSession',
'Helpers',
] as $path) {
Expand Down Expand Up @@ -474,7 +475,8 @@ public static function setSessionHandler(SessionHandler $storage): void
public static function getSessionHandler(): SessionHandler
{
if (!self::$sessionHandler) {
self::$sessionHandler = new NativeSession;
$dir = session_save_path() ?: ini_get('upload_tmp_dir') ?: sys_get_temp_dir() ?: self::$logDirectory;
self::$sessionHandler = $dir ? new FileSession($dir) : new NativeSession;
}
return self::$sessionHandler;
}
Expand Down
107 changes: 107 additions & 0 deletions src/Tracy/Session/FileSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* This file is part of the Tracy (https://tracy.nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy;


class FileSession implements SessionHandler
{
private const FILE_PREFIX = 'tracy-';
private const COOKIE_LIFETIME = 31557600;

/** @var string */
public $cookieName = 'tracy-session';

/** @var float probability that the clean() routine is started */
public $gcProbability = 0.001;

/** @var string */
private $dir;

/** @var resource */
private $file;

/** @var array */
private $data = [];


public function __construct(string $dir)
{
$this->dir = $dir;
}


public function isAvailable(): bool
{
if (!$this->file) {
$this->open();
}
return (bool) $this->file;
}


private function open(): void
{
$id = $_COOKIE[$this->cookieName] ?? null;
if (
!is_string($id)
|| !preg_match('#^\w{10}\z#i', $id)
|| !($file = @fopen($path = $this->dir . '/' . self::FILE_PREFIX . $id, 'r+')) // intentionally @
) {
$id = Helpers::createId();
setcookie($this->cookieName, $id, time() + self::COOKIE_LIFETIME, '/', '', false, true);

$file = @fopen($path = $this->dir . '/' . self::FILE_PREFIX . $id, 'c+'); // intentionally @
if ($file === false) {
throw new \RuntimeException("Unable to create file '$path'. " . error_get_last()['message']);
}
}

if (!@flock($file, LOCK_EX)) { // intentionally @
throw new \RuntimeException("Unable to acquire exclusive lock on '$path'. ", error_get_last()['message']);
}

$this->file = $file;
$this->data = @unserialize(stream_get_contents($this->file)) ?: []; // @ - file may be empty

if (mt_rand() / mt_getrandmax() < $this->gcProbability) {
$this->clean();
}
}


public function &getData(): array
{
return $this->data;
}


public function clean(): void
{
$old = strtotime('-1 week');
foreach (glob($this->dir . '/' . self::FILE_PREFIX . '*') as $file) {
if (filemtime($file) < $old) {
unlink($file);
}
}
}


public function __destruct()
{
if (!$this->file) {
return;
}
ftruncate($this->file, 0);
fseek($this->file, 0);
fwrite($this->file, serialize($this->data));
fclose($this->file);
$this->file = null;
}
}
1 change: 1 addition & 0 deletions src/tracy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
require __DIR__ . '/Tracy/OutputDebugger/OutputDebugger.php';
require __DIR__ . '/Tracy/Session/SessionHandler.php';
require __DIR__ . '/Tracy/Session/NativeSession.php';
require __DIR__ . '/Tracy/Session/FileSession.php';
require __DIR__ . '/Tracy/Helpers.php';
require __DIR__ . '/Tracy/functions.php';

0 comments on commit 9faef5e

Please sign in to comment.