@@ -1515,18 +1515,16 @@ public function createStream(string $content = ''): StreamInterface
1515
1515
1516
1516
public function createStreamFromFile (string $ filename , string $ mode = 'r ' ): StreamInterface
1517
1517
{
1518
- try {
1519
- $ resource = @\fopen ($ filename , $ mode );
1520
- } catch (\Throwable $ e ) {
1521
- throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened. ' , $ filename ));
1518
+ if ('' === $ filename ) {
1519
+ throw new \RuntimeException ('Path cannot be empty ' );
1522
1520
}
1523
1521
1524
- if (false === $ resource ) {
1522
+ if (false === $ resource = @ \fopen ( $ filename , $ mode ) ) {
1525
1523
if ('' === $ mode || false === \in_array ($ mode [0 ], ['r ' , 'w ' , 'a ' , 'x ' , 'c ' ], true )) {
1526
1524
throw new \InvalidArgumentException (\sprintf ('The mode "%s" is invalid. ' , $ mode ));
1527
1525
}
1528
1526
1529
- throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened. ' , $ filename ));
1527
+ throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened: %s ' , $ filename, \error_get_last ()[ ' message ' ] ?? '' ));
1530
1528
}
1531
1529
1532
1530
return Stream::create ($ resource );
@@ -2143,6 +2141,9 @@ public function getAttributes(): array
2143
2141
return $ this ->attributes ;
2144
2142
}
2145
2143
2144
+ /**
2145
+ * @return mixed
2146
+ */
2146
2147
public function getAttribute ($ attribute , $ default = null )
2147
2148
{
2148
2149
if (false === \array_key_exists ($ attribute , $ this ->attributes )) {
@@ -2358,16 +2359,20 @@ public function getSize() /*:?int*/
2358
2359
2359
2360
public function tell (): int
2360
2361
{
2361
- if (false === $ result = \ftell ($ this ->stream )) {
2362
- throw new \RuntimeException ('Unable to determine stream position ' );
2362
+ if (!isset ($ this ->stream )) {
2363
+ throw new \RuntimeException ('Stream is detached ' );
2364
+ }
2365
+
2366
+ if (false === $ result = @\ftell ($ this ->stream )) {
2367
+ throw new \RuntimeException ('Unable to determine stream position: ' . (\error_get_last ()['message ' ] ?? '' ));
2363
2368
}
2364
2369
2365
2370
return $ result ;
2366
2371
}
2367
2372
2368
2373
public function eof (): bool
2369
2374
{
2370
- return !$ this ->stream || \feof ($ this ->stream );
2375
+ return !isset ( $ this ->stream ) || \feof ($ this ->stream );
2371
2376
}
2372
2377
2373
2378
public function isSeekable (): bool
@@ -2377,6 +2382,10 @@ public function isSeekable(): bool
2377
2382
2378
2383
public function seek ($ offset , $ whence = \SEEK_SET ) /*:void*/
2379
2384
{
2385
+ if (!isset ($ this ->stream )) {
2386
+ throw new \RuntimeException ('Stream is detached ' );
2387
+ }
2388
+
2380
2389
if (!$ this ->seekable ) {
2381
2390
throw new \RuntimeException ('Stream is not seekable ' );
2382
2391
}
@@ -2398,15 +2407,19 @@ public function isWritable(): bool
2398
2407
2399
2408
public function write ($ string ): int
2400
2409
{
2410
+ if (!isset ($ this ->stream )) {
2411
+ throw new \RuntimeException ('Stream is detached ' );
2412
+ }
2413
+
2401
2414
if (!$ this ->writable ) {
2402
2415
throw new \RuntimeException ('Cannot write to a non-writable stream ' );
2403
2416
}
2404
2417
2405
2418
// We can't know the size after writing anything
2406
2419
$ this ->size = null ;
2407
2420
2408
- if (false === $ result = \fwrite ($ this ->stream , $ string )) {
2409
- throw new \RuntimeException ('Unable to write to stream ' );
2421
+ if (false === $ result = @ \fwrite ($ this ->stream , $ string )) {
2422
+ throw new \RuntimeException ('Unable to write to stream: ' . ( \error_get_last ()[ ' message ' ] ?? '' ) );
2410
2423
}
2411
2424
2412
2425
return $ result ;
@@ -2419,12 +2432,16 @@ public function isReadable(): bool
2419
2432
2420
2433
public function read ($ length ): string
2421
2434
{
2435
+ if (!isset ($ this ->stream )) {
2436
+ throw new \RuntimeException ('Stream is detached ' );
2437
+ }
2438
+
2422
2439
if (!$ this ->readable ) {
2423
2440
throw new \RuntimeException ('Cannot read from non-readable stream ' );
2424
2441
}
2425
2442
2426
- if (false === $ result = \fread ($ this ->stream , $ length )) {
2427
- throw new \RuntimeException ('Unable to read from stream ' );
2443
+ if (false === $ result = @ \fread ($ this ->stream , $ length )) {
2444
+ throw new \RuntimeException ('Unable to read from stream: ' . ( \error_get_last ()[ ' message ' ] ?? '' ) );
2428
2445
}
2429
2446
2430
2447
return $ result ;
@@ -2433,16 +2450,19 @@ public function read($length): string
2433
2450
public function getContents (): string
2434
2451
{
2435
2452
if (!isset ($ this ->stream )) {
2436
- throw new \RuntimeException ('Unable to read stream contents ' );
2453
+ throw new \RuntimeException ('Stream is detached ' );
2437
2454
}
2438
2455
2439
- if (false === $ contents = \stream_get_contents ($ this ->stream )) {
2440
- throw new \RuntimeException ('Unable to read stream contents ' );
2456
+ if (false === $ contents = @ \stream_get_contents ($ this ->stream )) {
2457
+ throw new \RuntimeException ('Unable to read stream contents: ' . ( \error_get_last ()[ ' message ' ] ?? '' ) );
2441
2458
}
2442
2459
2443
2460
return $ contents ;
2444
2461
}
2445
2462
2463
+ /**
2464
+ * @return mixed
2465
+ */
2446
2466
public function getMetadata ($ key = null )
2447
2467
{
2448
2468
if (!isset ($ this ->stream )) {
@@ -2539,7 +2559,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
2539
2559
2540
2560
if (\UPLOAD_ERR_OK === $ this ->error ) {
2541
2561
// Depending on the value set file or stream variable.
2542
- if (\is_string ($ streamOrFile )) {
2562
+ if (\is_string ($ streamOrFile ) && '' !== $ streamOrFile ) {
2543
2563
$ this ->file = $ streamOrFile ;
2544
2564
} elseif (\is_resource ($ streamOrFile )) {
2545
2565
$ this ->stream = Stream::create ($ streamOrFile );
@@ -2573,11 +2593,11 @@ public function getStream(): StreamInterface
2573
2593
return $ this ->stream ;
2574
2594
}
2575
2595
2576
- try {
2577
- return Stream::create (\fopen ($ this ->file , 'r ' ));
2578
- } catch (\Throwable $ e ) {
2579
- throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened. ' , $ this ->file ));
2596
+ if (false === $ resource = @\fopen ($ this ->file , 'r ' )) {
2597
+ throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened: %s ' , $ this ->file , \error_get_last ()['message ' ] ?? '' ));
2580
2598
}
2599
+
2600
+ return Stream::create ($ resource );
2581
2601
}
2582
2602
2583
2603
public function moveTo ($ targetPath ) /*:void*/
@@ -2589,20 +2609,23 @@ public function moveTo($targetPath) /*:void*/
2589
2609
}
2590
2610
2591
2611
if (null !== $ this ->file ) {
2592
- $ this ->moved = 'cli ' === \PHP_SAPI ? \rename ($ this ->file , $ targetPath ) : \move_uploaded_file ($ this ->file , $ targetPath );
2612
+ $ this ->moved = 'cli ' === \PHP_SAPI ? @\rename ($ this ->file , $ targetPath ) : @\move_uploaded_file ($ this ->file , $ targetPath );
2613
+
2614
+ if (false === $ this ->moved ) {
2615
+ throw new \RuntimeException (\sprintf ('Uploaded file could not be moved to "%s": %s ' , $ targetPath , \error_get_last ()['message ' ] ?? '' ));
2616
+ }
2593
2617
} else {
2594
2618
$ stream = $ this ->getStream ();
2595
2619
if ($ stream ->isSeekable ()) {
2596
2620
$ stream ->rewind ();
2597
2621
}
2598
2622
2599
- try {
2600
- // Copy the contents of a stream into another stream until end-of-file.
2601
- $ dest = Stream::create (\fopen ($ targetPath , 'w ' ));
2602
- } catch (\Throwable $ e ) {
2603
- throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened. ' , $ targetPath ));
2623
+ if (false === $ resource = @\fopen ($ targetPath , 'w ' )) {
2624
+ throw new \RuntimeException (\sprintf ('The file "%s" cannot be opened: %s ' , $ targetPath , \error_get_last ()['message ' ] ?? '' ));
2604
2625
}
2605
2626
2627
+ $ dest = Stream::create ($ resource );
2628
+
2606
2629
while (!$ stream ->eof ()) {
2607
2630
if (!$ dest ->write ($ stream ->read (1048576 ))) {
2608
2631
break ;
@@ -2611,10 +2634,6 @@ public function moveTo($targetPath) /*:void*/
2611
2634
2612
2635
$ this ->moved = true ;
2613
2636
}
2614
-
2615
- if (false === $ this ->moved ) {
2616
- throw new \RuntimeException (\sprintf ('Uploaded file could not be moved to "%s" ' , $ targetPath ));
2617
- }
2618
2637
}
2619
2638
2620
2639
public function getSize (): int
@@ -7876,9 +7895,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7876
7895
$ method = $ request ->getMethod ();
7877
7896
if ($ method == 'POST ' && in_array ($ path , ['login ' , 'register ' , 'password ' ])) {
7878
7897
$ body = $ request ->getParsedBody ();
7879
- $ username = isset ($ body ->username ) ? $ body ->username : '' ;
7880
- $ password = isset ($ body ->password ) ? $ body ->password : '' ;
7881
- $ newPassword = isset ($ body ->newPassword ) ? $ body ->newPassword : '' ;
7898
+ $ usernameFormFieldName = $ this ->getProperty ('usernameFormField ' , 'username ' );
7899
+ $ passwordFormFieldName = $ this ->getProperty ('passwordFormField ' , 'username ' );
7900
+ $ newPasswordFormFieldName = $ this ->getProperty ('newPasswordFormField ' , 'username ' );
7901
+ $ username = isset ($ body ->usernameFormFieldName ) ? $ body ->usernameFormFieldName : '' ;
7902
+ $ password = isset ($ body ->passwordFormFieldName ) ? $ body ->passwordFormFieldName : '' ;
7903
+ $ newPassword = isset ($ body ->newPasswordFormFieldName ) ? $ body ->newPasswordFormFieldName : '' ;
7882
7904
$ tableName = $ this ->getProperty ('usersTable ' , 'users ' );
7883
7905
$ table = $ this ->reflection ->getTable ($ tableName );
7884
7906
$ usernameColumnName = $ this ->getProperty ('usernameColumn ' , 'username ' );
0 commit comments