Skip to content

Commit d6b6637

Browse files
committed
Support PHP 8.3 and work around unserialize warnings
1 parent adbc8bc commit d6b6637

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
- "8.0"
2424
- "8.1"
2525
- "8.2"
26+
- "8.3"
2627
dependencies:
2728
- "highest"
2829
include:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ composer require cboden/ratchet:^0.4.4
101101
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
102102

103103
This project aims to run on any platform and thus does not require any PHP
104-
extensions and supports running on legacy PHP 5.4 through PHP 8.2+ with limited support for newer PHP.
104+
extensions and supports running on legacy PHP 5.4 through PHP 8.3+ with limited support for newer PHP.
105105
It's *highly recommended to use the latest supported PHP version* for this project.
106106

107107
See above note about [Reviving Ratchet](#reviving-ratchet) for newer PHP support.

src/Ratchet/Session/Serialize/PhpBinaryHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public function unserialize($raw) {
2222
$offset += 1;
2323
$varname = substr($raw, $offset, $num);
2424
$offset += $num;
25-
$data = unserialize(substr($raw, $offset));
25+
26+
// try to unserialize one piece of data from current offset, ignoring any warnings for trailing data on PHP 8.3+
27+
// @link https://wiki.php.net/rfc/unserialize_warn_on_trailing_data
28+
$data = @unserialize(substr($raw, $offset));
2629

2730
$returnData[$varname] = $data;
2831
$offset += strlen(serialize($data));

src/Ratchet/Session/Serialize/PhpHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public function unserialize($raw) {
3838
$num = $pos - $offset;
3939
$varname = substr($raw, $offset, $num);
4040
$offset += $num + 1;
41-
$data = unserialize(substr($raw, $offset));
41+
42+
// try to unserialize one piece of data from current offset, ignoring any warnings for trailing data on PHP 8.3+
43+
// @link https://wiki.php.net/rfc/unserialize_warn_on_trailing_data
44+
$data = @unserialize(substr($raw, $offset));
4245

4346
$returnData[$varname] = $data;
4447
$offset += strlen(serialize($data));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Ratchet\Session\Serialize;
3+
use PHPUnit\Framework\TestCase;
4+
use Ratchet\Session\Serialize\PhpBinaryHandler;
5+
6+
/**
7+
* @covers Ratchet\Session\Serialize\PhpHandler
8+
*/
9+
class PhpBinaryHandlerTest extends TestCase {
10+
protected $_handler;
11+
12+
/**
13+
* @before
14+
*/
15+
public function setUpHandler() {
16+
$this->_handler = new PhpBinaryHandler;
17+
}
18+
19+
public function serializedProvider() {
20+
return array(
21+
array(
22+
"\x0f" . '_sf2_attributes' . 'a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}' . "\x0c" . '_sf2_flashes' . 'a:0:{}'
23+
, array(
24+
'_sf2_attributes' => array(
25+
'hello' => 'world'
26+
, 'last' => 1332872102
27+
)
28+
, '_sf2_flashes' => array()
29+
)
30+
)
31+
);
32+
}
33+
34+
/**
35+
* @dataProvider serializedProvider
36+
*/
37+
public function testUnserialize($in, $expected) {
38+
$this->assertEquals($expected, $this->_handler->unserialize($in));
39+
}
40+
}

0 commit comments

Comments
 (0)