This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace FMUP; |
||
3 | |||
4 | /** |
||
5 | * Class Response |
||
6 | * @package FMUP |
||
7 | */ |
||
8 | class Response |
||
9 | { |
||
10 | use Sapi\OptionalTrait; |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $headers = array(); |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $body; |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $returnCode = 0; |
||
23 | |||
24 | /** |
||
25 | * Add a header to send in response |
||
26 | * |
||
27 | * @param Response\Header $header |
||
28 | * @return $this |
||
29 | */ |
||
30 | 6 | public function addHeader(Response\Header $header) |
|
31 | { |
||
32 | 6 | if (!array_key_exists($header->getType(), $this->headers)) { |
|
33 | 6 | $this->setHeader($header); |
|
34 | } else { |
||
35 | 4 | array_push($this->headers[$header->getType()], $header); |
|
36 | } |
||
37 | 6 | return $this; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get all headers defined |
||
42 | * @return array |
||
43 | */ |
||
44 | 7 | public function getHeaders() |
|
45 | { |
||
46 | 7 | return $this->headers; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Define a specific header |
||
51 | * @param Response\Header $header |
||
52 | * @return $this |
||
53 | */ |
||
54 | 7 | public function setHeader(Response\Header $header) |
|
55 | { |
||
56 | 7 | $this->headers[$header->getType()] = array($header); |
|
57 | 7 | return $this; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Clear headers or a specific one |
||
62 | * @param string|null $name |
||
63 | * @return $this |
||
64 | */ |
||
65 | 1 | public function clearHeader($name = null) |
|
66 | { |
||
67 | 1 | if (!is_null($name)) { |
|
68 | 1 | unset($this->headers[$name]); |
|
69 | } else { |
||
70 | 1 | $this->headers = array(); |
|
71 | } |
||
72 | 1 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Define the body of the Response |
||
77 | * @param string $body |
||
78 | * @return $this |
||
79 | */ |
||
80 | 8 | public function setBody($body) |
|
81 | { |
||
82 | 8 | $this->body = (string)$body; |
|
83 | 8 | return $this; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Retrieve defined body |
||
88 | * @return string |
||
89 | */ |
||
90 | 9 | public function getBody() |
|
91 | { |
||
92 | 9 | return (string)$this->body; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Sends header and response |
||
97 | */ |
||
98 | 3 | public function send() |
|
99 | { |
||
100 | 3 | if ($this->getSapi()->get() != Sapi::CLI) { |
|
101 | 1 | $strLen = $this->phpStrLen($this->getBody()); |
|
102 | 1 | if ($strLen) { |
|
103 | 1 | $this->setHeader($this->getContentLengthHeader($strLen)); |
|
104 | } |
||
105 | 1 | foreach ($this->getHeaders() as $headers) { |
|
106 | 1 | foreach ($headers as $header) { |
|
107 | /* @var $header Response\Header */ |
||
108 | 1 | $header->render(); |
|
109 | } |
||
110 | } |
||
111 | } |
||
112 | 3 | echo $this->getBody(); |
|
113 | 3 | if ($this->getReturnCode()) { |
|
114 | 1 | $this->exitPhp($this->getReturnCode()); |
|
115 | } |
||
116 | 3 | } |
|
117 | |||
118 | /** |
||
119 | * @param int $size |
||
120 | * @return Response\Header\ContentLength |
||
121 | * @codeCoverageIgnore |
||
122 | */ |
||
123 | protected function getContentLengthHeader($size) |
||
124 | { |
||
125 | return new Response\Header\ContentLength((int)$size); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $string |
||
130 | * @return int |
||
131 | * @codeCoverageIgnore |
||
132 | */ |
||
133 | protected function phpStrLen($string) |
||
134 | { |
||
135 | return strlen($string); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param int $returnCode |
||
140 | * @codeCoverageIgnore |
||
141 | */ |
||
142 | protected function exitPhp($returnCode = 0) |
||
143 | { |
||
144 | exit((int)$returnCode); |
||
0 ignored issues
–
show
|
|||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Define a PHP Cli return code - 0 (default) is success, another error code > 0 for whatever |
||
149 | * @param int $returnCode |
||
150 | * @return $this |
||
151 | */ |
||
152 | 2 | public function setReturnCode($returnCode = 0) |
|
153 | { |
||
154 | 2 | $this->returnCode = (int)$returnCode; |
|
155 | 2 | return $this; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get defined PHP Cli return code - 0 (default) is success, another error code > 0 for whatever |
||
160 | * @return int |
||
161 | */ |
||
162 | 4 | public function getReturnCode() |
|
163 | { |
||
164 | 4 | return (int)$this->returnCode; |
|
165 | } |
||
166 | } |
||
167 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.