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 20, 2021
1 parent d7c79e5 commit 5d5be6c
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 16 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
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,18 @@ any scripts:
AJAX and redirected requests
----------------------------

Tracy is able to show Debug bar and Bluescreens for AJAX and redirected requests. You just have to start session before Tracy:
Tracy is able to show Debug bar and Bluescreens for AJAX and redirected requests. Tracy keeps the data in a temporary files and uses the `tracy-session` cookie. Tracy can be configured to use a standard PHP session:

```php
session_start();
Debugger::setSessionStorage(new Tracy\NativeSession);
Debugger::enable();
```

In case you use non-standard session handler, you can start Tracy immediately (in order to handle any errors), then initialize your session handler
and then inform Tracy that session is ready to use via `dispatch()`:

```php
Debugger::setSessionStorage(new Tracy\NativeSession);
Debugger::enable();

// initialize session handler
Expand Down
6 changes: 5 additions & 1 deletion src/Bridges/Nette/TracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
));
}

if (!$this->cliMode && ($name = $builder->getByType(Nette\Http\Session::class))) {
if (
!$this->cliMode
&& Tracy\Debugger::getSessionStorage() instanceof Tracy\NativeSession
&& ($name = $builder->getByType(Nette\Http\Session::class))
) {
$initialize->addBody('$this->getService(?)->start();', [$name]);
$initialize->addBody('Tracy\Debugger::dispatch();');
}
Expand Down
8 changes: 7 additions & 1 deletion src/Tracy/Debugger/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public static function enable($mode = null, ?string $logDirectory = null, $email
'Logger/FireLogger',
'Logger/Logger',
'Session/SessionStorage',
'Session/FileSession',
'Session/NativeSession',
'Helpers',
] as $path) {
Expand Down Expand Up @@ -482,7 +483,12 @@ public static function setSessionStorage(SessionStorage $storage): void
public static function getSessionStorage(): SessionStorage
{
if (!self::$sessionStorage) {
self::$sessionStorage = new NativeSession;
self::$sessionStorage = is_dir($dir = session_save_path())
|| is_dir($dir = ini_get('upload_tmp_dir'))
|| is_dir($dir = sys_get_temp_dir())
|| ($dir = self::$logDirectory)
? new FileSession($dir)
: new NativeSession;
}

return self::$sessionStorage;
Expand Down
109 changes: 109 additions & 0 deletions src/Tracy/Session/FileSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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 SessionStorage
{
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 true;
}


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/SessionStorage.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 5d5be6c

Please sign in to comment.