Completed
Push — master ( 287393...7ff50d )
by Raffael
18:27 queued 14:12
created

src/app/Balloon.App.Api/Helper.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Api;
13
14
use Balloon\Filesystem\Exception;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Balloon\App\Api\Exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Balloon\Filesystem\Node\File;
16
use Micro\Http\Response;
17
18
class Helper
19
{
20
    /**
21
     * Stream content.
22
     */
23
    public static function streamContent(Response $response, File $file, bool $download = false): ?Response
24
    {
25
        if (true === $download) {
26
            $response->setHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\''.rawurlencode($file->getName()));
0 ignored issues
show
Consider using $file->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
27
            $response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
28
            $response->setHeader('Content-Type', 'application/octet-stream');
29
            $response->setHeader('Content-Length', (string) $file->getSize());
30
            $response->setHeader('Content-Transfer-Encoding', 'binary');
31
        } else {
32
            $response->setHeader('Content-Disposition', 'inline; filename*=UTF-8\'\''.rawurlencode($file->getName()));
0 ignored issues
show
Consider using $file->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
33
            $response->setHeader('Content-Type', $file->getContentType());
34
        }
35
36
        return $response
37
          ->setOutputFormat(null)
38
          ->setBody(function () use ($file) {
39
              $stream = $file->get();
40
              $name = $file->getName();
0 ignored issues
show
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Consider using $file->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
41
42
              if (null === $stream) {
43
                  return;
44
              }
45
46
              $size = $file->getSize();
47
              $length = $size;
48
              $start = 0;
49
              $end = $size - 1;
50
51
              if (isset($_SERVER['HTTP_RANGE'])) {
52
                  header('Accept-Ranges: bytes');
53
                  $c_start = $start;
0 ignored issues
show
$c_start is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
                  $c_end = $end;
55
                  list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
56
57
                  if (strpos($range, ',') !== false) {
58
                      header("Content-Range: bytes $start-$end/$size");
59
60
                      throw new Exception\InvalidRange('invalid offset/limit requested');
61
                  }
62
63
                  if ($range == '-') {
64
                      $c_start = $size - substr($range, 1);
65
                  } else {
66
                      $range = explode('-', $range);
67
                      $c_start = $range[0];
68
                      $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
69
                  }
70
71
                  $c_end = ($c_end > $end) ? $end : $c_end;
72
                  if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
73
                      header("Content-Range: bytes $start-$end/$size");
74
75
                      throw new Exception\InvalidRange('invalid offset/limit requested');
76
                  }
77
78
                  $start = (int) $c_start;
79
                  $end = (int) $c_end;
80
                  $length = (int) $end - $start + 1;
81
                  fseek($stream, $start);
82
                  header('HTTP/1.1 206 Partial Content');
83
                  header("Content-Range: bytes $start-$end/$size");
84
              }
85
86
              header('Content-Length: '.$length);
87
              $buffer = 1024 * 8;
88
89
              while (!feof($stream) && ($p = ftell($stream)) <= $end) {
90
                  if ($p + $buffer > $end) {
91
                      $buffer = $end - $p + 1;
92
                  }
93
94
                  echo fread($stream, $buffer);
95
                  flush();
96
              }
97
98
              fclose($stream);
99
          });
100
    }
101
}
102